Linux 2.6.31.6
[linux/fpc-iii.git] / fs / sysv / namei.c
blob33e047b59b8d659975d6c325835f0d11a2df27d4
1 /*
2 * linux/fs/sysv/namei.c
4 * minix/namei.c
5 * Copyright (C) 1991, 1992 Linus Torvalds
7 * coh/namei.c
8 * Copyright (C) 1993 Pascal Haible, Bruno Haible
10 * sysv/namei.c
11 * Copyright (C) 1993 Bruno Haible
12 * Copyright (C) 1997, 1998 Krzysztof G. Baranowski
15 #include <linux/pagemap.h>
16 #include "sysv.h"
18 static int add_nondir(struct dentry *dentry, struct inode *inode)
20 int err = sysv_add_link(dentry, inode);
21 if (!err) {
22 d_instantiate(dentry, inode);
23 return 0;
25 inode_dec_link_count(inode);
26 iput(inode);
27 return err;
30 static int sysv_hash(struct dentry *dentry, struct qstr *qstr)
32 /* Truncate the name in place, avoids having to define a compare
33 function. */
34 if (qstr->len > SYSV_NAMELEN) {
35 qstr->len = SYSV_NAMELEN;
36 qstr->hash = full_name_hash(qstr->name, qstr->len);
38 return 0;
41 const struct dentry_operations sysv_dentry_operations = {
42 .d_hash = sysv_hash,
45 static struct dentry *sysv_lookup(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
47 struct inode * inode = NULL;
48 ino_t ino;
50 dentry->d_op = dir->i_sb->s_root->d_op;
51 if (dentry->d_name.len > SYSV_NAMELEN)
52 return ERR_PTR(-ENAMETOOLONG);
53 ino = sysv_inode_by_name(dentry);
55 if (ino) {
56 inode = sysv_iget(dir->i_sb, ino);
57 if (IS_ERR(inode))
58 return ERR_CAST(inode);
60 d_add(dentry, inode);
61 return NULL;
64 static int sysv_mknod(struct inode * dir, struct dentry * dentry, int mode, dev_t rdev)
66 struct inode * inode;
67 int err;
69 if (!old_valid_dev(rdev))
70 return -EINVAL;
72 inode = sysv_new_inode(dir, mode);
73 err = PTR_ERR(inode);
75 if (!IS_ERR(inode)) {
76 sysv_set_inode(inode, rdev);
77 mark_inode_dirty(inode);
78 err = add_nondir(dentry, inode);
80 return err;
83 static int sysv_create(struct inode * dir, struct dentry * dentry, int mode, struct nameidata *nd)
85 return sysv_mknod(dir, dentry, mode, 0);
88 static int sysv_symlink(struct inode * dir, struct dentry * dentry,
89 const char * symname)
91 int err = -ENAMETOOLONG;
92 int l = strlen(symname)+1;
93 struct inode * inode;
95 if (l > dir->i_sb->s_blocksize)
96 goto out;
98 inode = sysv_new_inode(dir, S_IFLNK|0777);
99 err = PTR_ERR(inode);
100 if (IS_ERR(inode))
101 goto out;
103 sysv_set_inode(inode, 0);
104 err = page_symlink(inode, symname, l);
105 if (err)
106 goto out_fail;
108 mark_inode_dirty(inode);
109 err = add_nondir(dentry, inode);
110 out:
111 return err;
113 out_fail:
114 inode_dec_link_count(inode);
115 iput(inode);
116 goto out;
119 static int sysv_link(struct dentry * old_dentry, struct inode * dir,
120 struct dentry * dentry)
122 struct inode *inode = old_dentry->d_inode;
124 if (inode->i_nlink >= SYSV_SB(inode->i_sb)->s_link_max)
125 return -EMLINK;
127 inode->i_ctime = CURRENT_TIME_SEC;
128 inode_inc_link_count(inode);
129 atomic_inc(&inode->i_count);
131 return add_nondir(dentry, inode);
134 static int sysv_mkdir(struct inode * dir, struct dentry *dentry, int mode)
136 struct inode * inode;
137 int err = -EMLINK;
139 if (dir->i_nlink >= SYSV_SB(dir->i_sb)->s_link_max)
140 goto out;
141 inode_inc_link_count(dir);
143 inode = sysv_new_inode(dir, S_IFDIR|mode);
144 err = PTR_ERR(inode);
145 if (IS_ERR(inode))
146 goto out_dir;
148 sysv_set_inode(inode, 0);
150 inode_inc_link_count(inode);
152 err = sysv_make_empty(inode, dir);
153 if (err)
154 goto out_fail;
156 err = sysv_add_link(dentry, inode);
157 if (err)
158 goto out_fail;
160 d_instantiate(dentry, inode);
161 out:
162 return err;
164 out_fail:
165 inode_dec_link_count(inode);
166 inode_dec_link_count(inode);
167 iput(inode);
168 out_dir:
169 inode_dec_link_count(dir);
170 goto out;
173 static int sysv_unlink(struct inode * dir, struct dentry * dentry)
175 struct inode * inode = dentry->d_inode;
176 struct page * page;
177 struct sysv_dir_entry * de;
178 int err = -ENOENT;
180 de = sysv_find_entry(dentry, &page);
181 if (!de)
182 goto out;
184 err = sysv_delete_entry (de, page);
185 if (err)
186 goto out;
188 inode->i_ctime = dir->i_ctime;
189 inode_dec_link_count(inode);
190 out:
191 return err;
194 static int sysv_rmdir(struct inode * dir, struct dentry * dentry)
196 struct inode *inode = dentry->d_inode;
197 int err = -ENOTEMPTY;
199 if (sysv_empty_dir(inode)) {
200 err = sysv_unlink(dir, dentry);
201 if (!err) {
202 inode->i_size = 0;
203 inode_dec_link_count(inode);
204 inode_dec_link_count(dir);
207 return err;
211 * Anybody can rename anything with this: the permission checks are left to the
212 * higher-level routines.
214 static int sysv_rename(struct inode * old_dir, struct dentry * old_dentry,
215 struct inode * new_dir, struct dentry * new_dentry)
217 struct inode * old_inode = old_dentry->d_inode;
218 struct inode * new_inode = new_dentry->d_inode;
219 struct page * dir_page = NULL;
220 struct sysv_dir_entry * dir_de = NULL;
221 struct page * old_page;
222 struct sysv_dir_entry * old_de;
223 int err = -ENOENT;
225 old_de = sysv_find_entry(old_dentry, &old_page);
226 if (!old_de)
227 goto out;
229 if (S_ISDIR(old_inode->i_mode)) {
230 err = -EIO;
231 dir_de = sysv_dotdot(old_inode, &dir_page);
232 if (!dir_de)
233 goto out_old;
236 if (new_inode) {
237 struct page * new_page;
238 struct sysv_dir_entry * new_de;
240 err = -ENOTEMPTY;
241 if (dir_de && !sysv_empty_dir(new_inode))
242 goto out_dir;
244 err = -ENOENT;
245 new_de = sysv_find_entry(new_dentry, &new_page);
246 if (!new_de)
247 goto out_dir;
248 inode_inc_link_count(old_inode);
249 sysv_set_link(new_de, new_page, old_inode);
250 new_inode->i_ctime = CURRENT_TIME_SEC;
251 if (dir_de)
252 drop_nlink(new_inode);
253 inode_dec_link_count(new_inode);
254 } else {
255 if (dir_de) {
256 err = -EMLINK;
257 if (new_dir->i_nlink >= SYSV_SB(new_dir->i_sb)->s_link_max)
258 goto out_dir;
260 inode_inc_link_count(old_inode);
261 err = sysv_add_link(new_dentry, old_inode);
262 if (err) {
263 inode_dec_link_count(old_inode);
264 goto out_dir;
266 if (dir_de)
267 inode_inc_link_count(new_dir);
270 sysv_delete_entry(old_de, old_page);
271 inode_dec_link_count(old_inode);
273 if (dir_de) {
274 sysv_set_link(dir_de, dir_page, new_dir);
275 inode_dec_link_count(old_dir);
277 return 0;
279 out_dir:
280 if (dir_de) {
281 kunmap(dir_page);
282 page_cache_release(dir_page);
284 out_old:
285 kunmap(old_page);
286 page_cache_release(old_page);
287 out:
288 return err;
292 * directories can handle most operations...
294 const struct inode_operations sysv_dir_inode_operations = {
295 .create = sysv_create,
296 .lookup = sysv_lookup,
297 .link = sysv_link,
298 .unlink = sysv_unlink,
299 .symlink = sysv_symlink,
300 .mkdir = sysv_mkdir,
301 .rmdir = sysv_rmdir,
302 .mknod = sysv_mknod,
303 .rename = sysv_rename,
304 .getattr = sysv_getattr,