Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / fs / ext2 / namei.c
1 /*
2  * linux/fs/ext2/namei.c
3  *
4  * Rewrite to pagecache. Almost all code had been changed, so blame me
5  * if the things go wrong. Please, send bug reports to
6  * viro@parcelfarce.linux.theplanet.co.uk
7  *
8  * Stuff here is basically a glue between the VFS and generic UNIXish
9  * filesystem that keeps everything in pagecache. All knowledge of the
10  * directory layout is in fs/ext2/dir.c - it turned out to be easily separatable
11  * and it's easier to debug that way. In principle we might want to
12  * generalize that a bit and turn it into a library. Or not.
13  *
14  * The only non-static object here is ext2_dir_inode_operations.
15  *
16  * TODO: get rid of kmap() use, add readahead.
17  *
18  * Copyright (C) 1992, 1993, 1994, 1995
19  * Remy Card (card@masi.ibp.fr)
20  * Laboratoire MASI - Institut Blaise Pascal
21  * Universite Pierre et Marie Curie (Paris VI)
22  *
23  *  from
24  *
25  *  linux/fs/minix/namei.c
26  *
27  *  Copyright (C) 1991, 1992  Linus Torvalds
28  *
29  *  Big-endian to little-endian byte-swapping/bitmaps by
30  *        David S. Miller (davem@caip.rutgers.edu), 1995
31  */
32
33 #include <linux/pagemap.h>
34 #include <linux/vserver/xid.h>
35 #include "ext2.h"
36 #include "xattr.h"
37 #include "acl.h"
38 #include "xip.h"
39
40 static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode)
41 {
42         int err = ext2_add_link(dentry, inode);
43         if (!err) {
44                 d_instantiate(dentry, inode);
45                 return 0;
46         }
47         inode_dec_link_count(inode);
48         iput(inode);
49         return err;
50 }
51
52 /*
53  * Methods themselves.
54  */
55
56 static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
57 {
58         struct inode * inode;
59         ino_t ino;
60         
61         if (dentry->d_name.len > EXT2_NAME_LEN)
62                 return ERR_PTR(-ENAMETOOLONG);
63
64         ino = ext2_inode_by_name(dir, dentry);
65         inode = NULL;
66         if (ino) {
67                 inode = iget(dir->i_sb, ino);
68                 if (!inode)
69                         return ERR_PTR(-EACCES);
70                 vx_propagate_xid(nd, inode);
71         }
72         return d_splice_alias(inode, dentry);
73 }
74
75 struct dentry *ext2_get_parent(struct dentry *child)
76 {
77         unsigned long ino;
78         struct dentry *parent;
79         struct inode *inode;
80         struct dentry dotdot;
81
82         dotdot.d_name.name = "..";
83         dotdot.d_name.len = 2;
84
85         ino = ext2_inode_by_name(child->d_inode, &dotdot);
86         if (!ino)
87                 return ERR_PTR(-ENOENT);
88         inode = iget(child->d_inode->i_sb, ino);
89
90         if (!inode)
91                 return ERR_PTR(-EACCES);
92         parent = d_alloc_anon(inode);
93         if (!parent) {
94                 iput(inode);
95                 parent = ERR_PTR(-ENOMEM);
96         }
97         return parent;
98
99
100 /*
101  * By the time this is called, we already have created
102  * the directory cache entry for the new file, but it
103  * is so far negative - it has no inode.
104  *
105  * If the create succeeds, we fill in the inode information
106  * with d_instantiate(). 
107  */
108 static int ext2_create (struct inode * dir, struct dentry * dentry, int mode, struct nameidata *nd)
109 {
110         struct inode * inode = ext2_new_inode (dir, mode);
111         int err = PTR_ERR(inode);
112         if (!IS_ERR(inode)) {
113                 inode->i_op = &ext2_file_inode_operations;
114                 if (ext2_use_xip(inode->i_sb)) {
115                         inode->i_mapping->a_ops = &ext2_aops_xip;
116                         inode->i_fop = &ext2_xip_file_operations;
117                 } else if (test_opt(inode->i_sb, NOBH)) {
118                         inode->i_mapping->a_ops = &ext2_nobh_aops;
119                         inode->i_fop = &ext2_file_operations;
120                 } else {
121                         inode->i_mapping->a_ops = &ext2_aops;
122                         inode->i_fop = &ext2_file_operations;
123                 }
124                 mark_inode_dirty(inode);
125                 err = ext2_add_nondir(dentry, inode);
126         }
127         return err;
128 }
129
130 static int ext2_mknod (struct inode * dir, struct dentry *dentry, int mode, dev_t rdev)
131 {
132         struct inode * inode;
133         int err;
134
135         if (!new_valid_dev(rdev))
136                 return -EINVAL;
137
138         inode = ext2_new_inode (dir, mode);
139         err = PTR_ERR(inode);
140         if (!IS_ERR(inode)) {
141                 init_special_inode(inode, inode->i_mode, rdev);
142 #ifdef CONFIG_EXT2_FS_XATTR
143                 inode->i_op = &ext2_special_inode_operations;
144 #endif
145                 mark_inode_dirty(inode);
146                 err = ext2_add_nondir(dentry, inode);
147         }
148         return err;
149 }
150
151 static int ext2_symlink (struct inode * dir, struct dentry * dentry,
152         const char * symname)
153 {
154         struct super_block * sb = dir->i_sb;
155         int err = -ENAMETOOLONG;
156         unsigned l = strlen(symname)+1;
157         struct inode * inode;
158
159         if (l > sb->s_blocksize)
160                 goto out;
161
162         inode = ext2_new_inode (dir, S_IFLNK | S_IRWXUGO);
163         err = PTR_ERR(inode);
164         if (IS_ERR(inode))
165                 goto out;
166
167         if (l > sizeof (EXT2_I(inode)->i_data)) {
168                 /* slow symlink */
169                 inode->i_op = &ext2_symlink_inode_operations;
170                 if (test_opt(inode->i_sb, NOBH))
171                         inode->i_mapping->a_ops = &ext2_nobh_aops;
172                 else
173                         inode->i_mapping->a_ops = &ext2_aops;
174                 err = page_symlink(inode, symname, l);
175                 if (err)
176                         goto out_fail;
177         } else {
178                 /* fast symlink */
179                 inode->i_op = &ext2_fast_symlink_inode_operations;
180                 memcpy((char*)(EXT2_I(inode)->i_data),symname,l);
181                 inode->i_size = l-1;
182         }
183         mark_inode_dirty(inode);
184
185         err = ext2_add_nondir(dentry, inode);
186 out:
187         return err;
188
189 out_fail:
190         inode_dec_link_count(inode);
191         iput (inode);
192         goto out;
193 }
194
195 static int ext2_link (struct dentry * old_dentry, struct inode * dir,
196         struct dentry *dentry)
197 {
198         struct inode *inode = old_dentry->d_inode;
199
200         if (inode->i_nlink >= EXT2_LINK_MAX)
201                 return -EMLINK;
202
203         inode->i_ctime = CURRENT_TIME_SEC;
204         inode_inc_link_count(inode);
205         atomic_inc(&inode->i_count);
206
207         return ext2_add_nondir(dentry, inode);
208 }
209
210 static int ext2_mkdir(struct inode * dir, struct dentry * dentry, int mode)
211 {
212         struct inode * inode;
213         int err = -EMLINK;
214
215         if (dir->i_nlink >= EXT2_LINK_MAX)
216                 goto out;
217
218         inode_inc_link_count(dir);
219
220         inode = ext2_new_inode (dir, S_IFDIR | mode);
221         err = PTR_ERR(inode);
222         if (IS_ERR(inode))
223                 goto out_dir;
224
225         inode->i_op = &ext2_dir_inode_operations;
226         inode->i_fop = &ext2_dir_operations;
227         if (test_opt(inode->i_sb, NOBH))
228                 inode->i_mapping->a_ops = &ext2_nobh_aops;
229         else
230                 inode->i_mapping->a_ops = &ext2_aops;
231
232         inode_inc_link_count(inode);
233
234         err = ext2_make_empty(inode, dir);
235         if (err)
236                 goto out_fail;
237
238         err = ext2_add_link(dentry, inode);
239         if (err)
240                 goto out_fail;
241
242         d_instantiate(dentry, inode);
243 out:
244         return err;
245
246 out_fail:
247         inode_dec_link_count(inode);
248         inode_dec_link_count(inode);
249         iput(inode);
250 out_dir:
251         inode_dec_link_count(dir);
252         goto out;
253 }
254
255 static int ext2_unlink(struct inode * dir, struct dentry *dentry)
256 {
257         struct inode * inode = dentry->d_inode;
258         struct ext2_dir_entry_2 * de;
259         struct page * page;
260         int err = -ENOENT;
261
262         de = ext2_find_entry (dir, dentry, &page);
263         if (!de)
264                 goto out;
265
266         err = ext2_delete_entry (de, page);
267         if (err)
268                 goto out;
269
270         inode->i_ctime = dir->i_ctime;
271         inode_dec_link_count(inode);
272         err = 0;
273 out:
274         return err;
275 }
276
277 static int ext2_rmdir (struct inode * dir, struct dentry *dentry)
278 {
279         struct inode * inode = dentry->d_inode;
280         int err = -ENOTEMPTY;
281
282         if (ext2_empty_dir(inode)) {
283                 err = ext2_unlink(dir, dentry);
284                 if (!err) {
285                         inode->i_size = 0;
286                         inode_dec_link_count(inode);
287                         inode_dec_link_count(dir);
288                 }
289         }
290         return err;
291 }
292
293 static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry,
294         struct inode * new_dir, struct dentry * new_dentry )
295 {
296         struct inode * old_inode = old_dentry->d_inode;
297         struct inode * new_inode = new_dentry->d_inode;
298         struct page * dir_page = NULL;
299         struct ext2_dir_entry_2 * dir_de = NULL;
300         struct page * old_page;
301         struct ext2_dir_entry_2 * old_de;
302         int err = -ENOENT;
303
304         old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
305         if (!old_de)
306                 goto out;
307
308         if (S_ISDIR(old_inode->i_mode)) {
309                 err = -EIO;
310                 dir_de = ext2_dotdot(old_inode, &dir_page);
311                 if (!dir_de)
312                         goto out_old;
313         }
314
315         if (new_inode) {
316                 struct page *new_page;
317                 struct ext2_dir_entry_2 *new_de;
318
319                 err = -ENOTEMPTY;
320                 if (dir_de && !ext2_empty_dir (new_inode))
321                         goto out_dir;
322
323                 err = -ENOENT;
324                 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
325                 if (!new_de)
326                         goto out_dir;
327                 inode_inc_link_count(old_inode);
328                 ext2_set_link(new_dir, new_de, new_page, old_inode);
329                 new_inode->i_ctime = CURRENT_TIME_SEC;
330                 if (dir_de)
331                         new_inode->i_nlink--;
332                 inode_dec_link_count(new_inode);
333         } else {
334                 if (dir_de) {
335                         err = -EMLINK;
336                         if (new_dir->i_nlink >= EXT2_LINK_MAX)
337                                 goto out_dir;
338                 }
339                 inode_inc_link_count(old_inode);
340                 err = ext2_add_link(new_dentry, old_inode);
341                 if (err) {
342                         inode_dec_link_count(old_inode);
343                         goto out_dir;
344                 }
345                 if (dir_de)
346                         inode_inc_link_count(new_dir);
347         }
348
349         /*
350          * Like most other Unix systems, set the ctime for inodes on a
351          * rename.
352          * inode_dec_link_count() will mark the inode dirty.
353          */
354         old_inode->i_ctime = CURRENT_TIME_SEC;
355
356         ext2_delete_entry (old_de, old_page);
357         inode_dec_link_count(old_inode);
358
359         if (dir_de) {
360                 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
361                 inode_dec_link_count(old_dir);
362         }
363         return 0;
364
365
366 out_dir:
367         if (dir_de) {
368                 kunmap(dir_page);
369                 page_cache_release(dir_page);
370         }
371 out_old:
372         kunmap(old_page);
373         page_cache_release(old_page);
374 out:
375         return err;
376 }
377
378 struct inode_operations ext2_dir_inode_operations = {
379         .create         = ext2_create,
380         .lookup         = ext2_lookup,
381         .link           = ext2_link,
382         .unlink         = ext2_unlink,
383         .symlink        = ext2_symlink,
384         .mkdir          = ext2_mkdir,
385         .rmdir          = ext2_rmdir,
386         .mknod          = ext2_mknod,
387         .rename         = ext2_rename,
388 #ifdef CONFIG_EXT2_FS_XATTR
389         .setxattr       = generic_setxattr,
390         .getxattr       = generic_getxattr,
391         .listxattr      = ext2_listxattr,
392         .removexattr    = generic_removexattr,
393 #endif
394         .setattr        = ext2_setattr,
395         .permission     = ext2_permission,
396         .sync_flags     = ext2_sync_flags,
397 };
398
399 struct inode_operations ext2_special_inode_operations = {
400 #ifdef CONFIG_EXT2_FS_XATTR
401         .setxattr       = generic_setxattr,
402         .getxattr       = generic_getxattr,
403         .listxattr      = ext2_listxattr,
404         .removexattr    = generic_removexattr,
405 #endif
406         .setattr        = ext2_setattr,
407         .permission     = ext2_permission,
408         .sync_flags     = ext2_sync_flags,
409 };