linux 2.6.16.38 w/ vs2.0.3-rc1
[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 /*
41  * Couple of helper functions - make the code slightly cleaner.
42  */
43
44 static inline void ext2_inc_count(struct inode *inode)
45 {
46         inode->i_nlink++;
47         mark_inode_dirty(inode);
48 }
49
50 static inline void ext2_dec_count(struct inode *inode)
51 {
52         inode->i_nlink--;
53         mark_inode_dirty(inode);
54 }
55
56 static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode)
57 {
58         int err = ext2_add_link(dentry, inode);
59         if (!err) {
60                 d_instantiate(dentry, inode);
61                 return 0;
62         }
63         ext2_dec_count(inode);
64         iput(inode);
65         return err;
66 }
67
68 /*
69  * Methods themselves.
70  */
71
72 static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
73 {
74         struct inode * inode;
75         ino_t ino;
76         
77         if (dentry->d_name.len > EXT2_NAME_LEN)
78                 return ERR_PTR(-ENAMETOOLONG);
79
80         ino = ext2_inode_by_name(dir, dentry);
81         inode = NULL;
82         if (ino) {
83                 inode = iget(dir->i_sb, ino);
84                 if (!inode)
85                         return ERR_PTR(-EACCES);
86                 vx_propagate_xid(nd, inode);
87         }
88         return d_splice_alias(inode, dentry);
89 }
90
91 struct dentry *ext2_get_parent(struct dentry *child)
92 {
93         unsigned long ino;
94         struct dentry *parent;
95         struct inode *inode;
96         struct dentry dotdot;
97
98         dotdot.d_name.name = "..";
99         dotdot.d_name.len = 2;
100
101         ino = ext2_inode_by_name(child->d_inode, &dotdot);
102         if (!ino)
103                 return ERR_PTR(-ENOENT);
104         inode = iget(child->d_inode->i_sb, ino);
105
106         if (!inode)
107                 return ERR_PTR(-EACCES);
108         parent = d_alloc_anon(inode);
109         if (!parent) {
110                 iput(inode);
111                 parent = ERR_PTR(-ENOMEM);
112         }
113         return parent;
114
115
116 /*
117  * By the time this is called, we already have created
118  * the directory cache entry for the new file, but it
119  * is so far negative - it has no inode.
120  *
121  * If the create succeeds, we fill in the inode information
122  * with d_instantiate(). 
123  */
124 static int ext2_create (struct inode * dir, struct dentry * dentry, int mode, struct nameidata *nd)
125 {
126         struct inode * inode = ext2_new_inode (dir, mode);
127         int err = PTR_ERR(inode);
128         if (!IS_ERR(inode)) {
129                 inode->i_op = &ext2_file_inode_operations;
130                 if (ext2_use_xip(inode->i_sb)) {
131                         inode->i_mapping->a_ops = &ext2_aops_xip;
132                         inode->i_fop = &ext2_xip_file_operations;
133                 } else if (test_opt(inode->i_sb, NOBH)) {
134                         inode->i_mapping->a_ops = &ext2_nobh_aops;
135                         inode->i_fop = &ext2_file_operations;
136                 } else {
137                         inode->i_mapping->a_ops = &ext2_aops;
138                         inode->i_fop = &ext2_file_operations;
139                 }
140                 mark_inode_dirty(inode);
141                 err = ext2_add_nondir(dentry, inode);
142         }
143         return err;
144 }
145
146 static int ext2_mknod (struct inode * dir, struct dentry *dentry, int mode, dev_t rdev)
147 {
148         struct inode * inode;
149         int err;
150
151         if (!new_valid_dev(rdev))
152                 return -EINVAL;
153
154         inode = ext2_new_inode (dir, mode);
155         err = PTR_ERR(inode);
156         if (!IS_ERR(inode)) {
157                 init_special_inode(inode, inode->i_mode, rdev);
158 #ifdef CONFIG_EXT2_FS_XATTR
159                 inode->i_op = &ext2_special_inode_operations;
160 #endif
161                 mark_inode_dirty(inode);
162                 err = ext2_add_nondir(dentry, inode);
163         }
164         return err;
165 }
166
167 static int ext2_symlink (struct inode * dir, struct dentry * dentry,
168         const char * symname)
169 {
170         struct super_block * sb = dir->i_sb;
171         int err = -ENAMETOOLONG;
172         unsigned l = strlen(symname)+1;
173         struct inode * inode;
174
175         if (l > sb->s_blocksize)
176                 goto out;
177
178         inode = ext2_new_inode (dir, S_IFLNK | S_IRWXUGO);
179         err = PTR_ERR(inode);
180         if (IS_ERR(inode))
181                 goto out;
182
183         if (l > sizeof (EXT2_I(inode)->i_data)) {
184                 /* slow symlink */
185                 inode->i_op = &ext2_symlink_inode_operations;
186                 if (test_opt(inode->i_sb, NOBH))
187                         inode->i_mapping->a_ops = &ext2_nobh_aops;
188                 else
189                         inode->i_mapping->a_ops = &ext2_aops;
190                 err = page_symlink(inode, symname, l);
191                 if (err)
192                         goto out_fail;
193         } else {
194                 /* fast symlink */
195                 inode->i_op = &ext2_fast_symlink_inode_operations;
196                 memcpy((char*)(EXT2_I(inode)->i_data),symname,l);
197                 inode->i_size = l-1;
198         }
199         mark_inode_dirty(inode);
200
201         err = ext2_add_nondir(dentry, inode);
202 out:
203         return err;
204
205 out_fail:
206         ext2_dec_count(inode);
207         iput (inode);
208         goto out;
209 }
210
211 static int ext2_link (struct dentry * old_dentry, struct inode * dir,
212         struct dentry *dentry)
213 {
214         struct inode *inode = old_dentry->d_inode;
215
216         if (inode->i_nlink >= EXT2_LINK_MAX)
217                 return -EMLINK;
218
219         inode->i_ctime = CURRENT_TIME_SEC;
220         ext2_inc_count(inode);
221         atomic_inc(&inode->i_count);
222
223         return ext2_add_nondir(dentry, inode);
224 }
225
226 static int ext2_mkdir(struct inode * dir, struct dentry * dentry, int mode)
227 {
228         struct inode * inode;
229         int err = -EMLINK;
230
231         if (dir->i_nlink >= EXT2_LINK_MAX)
232                 goto out;
233
234         ext2_inc_count(dir);
235
236         inode = ext2_new_inode (dir, S_IFDIR | mode);
237         err = PTR_ERR(inode);
238         if (IS_ERR(inode))
239                 goto out_dir;
240
241         inode->i_op = &ext2_dir_inode_operations;
242         inode->i_fop = &ext2_dir_operations;
243         if (test_opt(inode->i_sb, NOBH))
244                 inode->i_mapping->a_ops = &ext2_nobh_aops;
245         else
246                 inode->i_mapping->a_ops = &ext2_aops;
247
248         ext2_inc_count(inode);
249
250         err = ext2_make_empty(inode, dir);
251         if (err)
252                 goto out_fail;
253
254         err = ext2_add_link(dentry, inode);
255         if (err)
256                 goto out_fail;
257
258         d_instantiate(dentry, inode);
259 out:
260         return err;
261
262 out_fail:
263         ext2_dec_count(inode);
264         ext2_dec_count(inode);
265         iput(inode);
266 out_dir:
267         ext2_dec_count(dir);
268         goto out;
269 }
270
271 static int ext2_unlink(struct inode * dir, struct dentry *dentry)
272 {
273         struct inode * inode = dentry->d_inode;
274         struct ext2_dir_entry_2 * de;
275         struct page * page;
276         int err = -ENOENT;
277
278         de = ext2_find_entry (dir, dentry, &page);
279         if (!de)
280                 goto out;
281
282         err = ext2_delete_entry (de, page);
283         if (err)
284                 goto out;
285
286         inode->i_ctime = dir->i_ctime;
287         ext2_dec_count(inode);
288         err = 0;
289 out:
290         return err;
291 }
292
293 static int ext2_rmdir (struct inode * dir, struct dentry *dentry)
294 {
295         struct inode * inode = dentry->d_inode;
296         int err = -ENOTEMPTY;
297
298         if (ext2_empty_dir(inode)) {
299                 err = ext2_unlink(dir, dentry);
300                 if (!err) {
301                         inode->i_size = 0;
302                         ext2_dec_count(inode);
303                         ext2_dec_count(dir);
304                 }
305         }
306         return err;
307 }
308
309 static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry,
310         struct inode * new_dir, struct dentry * new_dentry )
311 {
312         struct inode * old_inode = old_dentry->d_inode;
313         struct inode * new_inode = new_dentry->d_inode;
314         struct page * dir_page = NULL;
315         struct ext2_dir_entry_2 * dir_de = NULL;
316         struct page * old_page;
317         struct ext2_dir_entry_2 * old_de;
318         int err = -ENOENT;
319
320         old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
321         if (!old_de)
322                 goto out;
323
324         if (S_ISDIR(old_inode->i_mode)) {
325                 err = -EIO;
326                 dir_de = ext2_dotdot(old_inode, &dir_page);
327                 if (!dir_de)
328                         goto out_old;
329         }
330
331         if (new_inode) {
332                 struct page *new_page;
333                 struct ext2_dir_entry_2 *new_de;
334
335                 err = -ENOTEMPTY;
336                 if (dir_de && !ext2_empty_dir (new_inode))
337                         goto out_dir;
338
339                 err = -ENOENT;
340                 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
341                 if (!new_de)
342                         goto out_dir;
343                 ext2_inc_count(old_inode);
344                 ext2_set_link(new_dir, new_de, new_page, old_inode);
345                 new_inode->i_ctime = CURRENT_TIME_SEC;
346                 if (dir_de)
347                         new_inode->i_nlink--;
348                 ext2_dec_count(new_inode);
349         } else {
350                 if (dir_de) {
351                         err = -EMLINK;
352                         if (new_dir->i_nlink >= EXT2_LINK_MAX)
353                                 goto out_dir;
354                 }
355                 ext2_inc_count(old_inode);
356                 err = ext2_add_link(new_dentry, old_inode);
357                 if (err) {
358                         ext2_dec_count(old_inode);
359                         goto out_dir;
360                 }
361                 if (dir_de)
362                         ext2_inc_count(new_dir);
363         }
364
365         /*
366          * Like most other Unix systems, set the ctime for inodes on a
367          * rename.
368          * ext2_dec_count() will mark the inode dirty.
369          */
370         old_inode->i_ctime = CURRENT_TIME_SEC;
371
372         ext2_delete_entry (old_de, old_page);
373         ext2_dec_count(old_inode);
374
375         if (dir_de) {
376                 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
377                 ext2_dec_count(old_dir);
378         }
379         return 0;
380
381
382 out_dir:
383         if (dir_de) {
384                 kunmap(dir_page);
385                 page_cache_release(dir_page);
386         }
387 out_old:
388         kunmap(old_page);
389         page_cache_release(old_page);
390 out:
391         return err;
392 }
393
394 struct inode_operations ext2_dir_inode_operations = {
395         .create         = ext2_create,
396         .lookup         = ext2_lookup,
397         .link           = ext2_link,
398         .unlink         = ext2_unlink,
399         .symlink        = ext2_symlink,
400         .mkdir          = ext2_mkdir,
401         .rmdir          = ext2_rmdir,
402         .mknod          = ext2_mknod,
403         .rename         = ext2_rename,
404 #ifdef CONFIG_EXT2_FS_XATTR
405         .setxattr       = generic_setxattr,
406         .getxattr       = generic_getxattr,
407         .listxattr      = ext2_listxattr,
408         .removexattr    = generic_removexattr,
409 #endif
410         .setattr        = ext2_setattr,
411         .permission     = ext2_permission,
412         .sync_flags     = ext2_sync_flags,
413 };
414
415 struct inode_operations ext2_special_inode_operations = {
416 #ifdef CONFIG_EXT2_FS_XATTR
417         .setxattr       = generic_setxattr,
418         .getxattr       = generic_getxattr,
419         .listxattr      = ext2_listxattr,
420         .removexattr    = generic_removexattr,
421 #endif
422         .setattr        = ext2_setattr,
423         .permission     = ext2_permission,
424         .sync_flags     = ext2_sync_flags,
425 };