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