MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / fs / ext2 / namei.c
blob4abd2f06b387a0ba689e71b91adf04140277b282
1 /*
2 * linux/fs/ext2/namei.c
4 * Rewrite to pagecache. Almost all code had been changed, so blame me
5 * if the things go wrong. Please, send bug reports to viro@math.psu.edu
7 * Stuff here is basically a glue between the VFS and generic UNIXish
8 * filesystem that keeps everything in pagecache. All knowledge of the
9 * directory layout is in fs/ext2/dir.c - it turned out to be easily separatable
10 * and it's easier to debug that way. In principle we might want to
11 * generalize that a bit and turn it into a library. Or not.
13 * The only non-static object here is ext2_dir_inode_operations.
15 * TODO: get rid of kmap() use, add readahead.
17 * Copyright (C) 1992, 1993, 1994, 1995
18 * Remy Card (card@masi.ibp.fr)
19 * Laboratoire MASI - Institut Blaise Pascal
20 * Universite Pierre et Marie Curie (Paris VI)
22 * from
24 * linux/fs/minix/namei.c
26 * Copyright (C) 1991, 1992 Linus Torvalds
28 * Big-endian to little-endian byte-swapping/bitmaps by
29 * David S. Miller (davem@caip.rutgers.edu), 1995
32 #include <linux/pagemap.h>
33 #include "ext2.h"
34 #include "xattr.h"
35 #include "acl.h"
38 * Couple of helper functions - make the code slightly cleaner.
41 static inline void ext2_inc_count(struct inode *inode)
43 inode->i_nlink++;
44 mark_inode_dirty(inode);
47 static inline void ext2_dec_count(struct inode *inode)
49 inode->i_nlink--;
50 mark_inode_dirty(inode);
53 static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode)
55 int err = ext2_add_link(dentry, inode);
56 if (!err) {
57 d_instantiate(dentry, inode);
58 return 0;
60 ext2_dec_count(inode);
61 iput(inode);
62 return err;
66 * Methods themselves.
69 static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
71 struct inode * inode;
72 ino_t ino;
74 if (dentry->d_name.len > EXT2_NAME_LEN)
75 return ERR_PTR(-ENAMETOOLONG);
77 ino = ext2_inode_by_name(dir, dentry);
78 inode = NULL;
79 if (ino) {
80 inode = iget(dir->i_sb, ino);
81 if (!inode)
82 return ERR_PTR(-EACCES);
84 if (inode)
85 return d_splice_alias(inode, dentry);
86 d_add(dentry, inode);
87 return NULL;
90 struct dentry *ext2_get_parent(struct dentry *child)
92 unsigned long ino;
93 struct dentry *parent;
94 struct inode *inode;
95 struct dentry dotdot;
97 dotdot.d_name.name = "..";
98 dotdot.d_name.len = 2;
100 ino = ext2_inode_by_name(child->d_inode, &dotdot);
101 if (!ino)
102 return ERR_PTR(-ENOENT);
103 inode = iget(child->d_inode->i_sb, ino);
105 if (!inode)
106 return ERR_PTR(-EACCES);
107 parent = d_alloc_anon(inode);
108 if (!parent) {
109 iput(inode);
110 parent = ERR_PTR(-ENOMEM);
112 return parent;
116 * By the time this is called, we already have created
117 * the directory cache entry for the new file, but it
118 * is so far negative - it has no inode.
120 * If the create succeeds, we fill in the inode information
121 * with d_instantiate().
123 static int ext2_create (struct inode * dir, struct dentry * dentry, int mode, struct nameidata *nd)
125 struct inode * inode = ext2_new_inode (dir, mode);
126 int err = PTR_ERR(inode);
127 if (!IS_ERR(inode)) {
128 inode->i_op = &ext2_file_inode_operations;
129 inode->i_fop = &ext2_file_operations;
130 if (test_opt(inode->i_sb, NOBH))
131 inode->i_mapping->a_ops = &ext2_nobh_aops;
132 else
133 inode->i_mapping->a_ops = &ext2_aops;
134 mark_inode_dirty(inode);
135 err = ext2_add_nondir(dentry, inode);
137 return err;
140 static int ext2_mknod (struct inode * dir, struct dentry *dentry, int mode, dev_t rdev)
142 struct inode * inode;
143 int err;
145 if (!new_valid_dev(rdev))
146 return -EINVAL;
148 inode = ext2_new_inode (dir, mode);
149 err = PTR_ERR(inode);
150 if (!IS_ERR(inode)) {
151 init_special_inode(inode, inode->i_mode, rdev);
152 #ifdef CONFIG_EXT2_FS_XATTR
153 inode->i_op = &ext2_special_inode_operations;
154 #endif
155 mark_inode_dirty(inode);
156 err = ext2_add_nondir(dentry, inode);
158 return err;
161 static int ext2_symlink (struct inode * dir, struct dentry * dentry,
162 const char * symname)
164 struct super_block * sb = dir->i_sb;
165 int err = -ENAMETOOLONG;
166 unsigned l = strlen(symname)+1;
167 struct inode * inode;
169 if (l > sb->s_blocksize)
170 goto out;
172 inode = ext2_new_inode (dir, S_IFLNK | S_IRWXUGO);
173 err = PTR_ERR(inode);
174 if (IS_ERR(inode))
175 goto out;
177 if (l > sizeof (EXT2_I(inode)->i_data)) {
178 /* slow symlink */
179 inode->i_op = &ext2_symlink_inode_operations;
180 if (test_opt(inode->i_sb, NOBH))
181 inode->i_mapping->a_ops = &ext2_nobh_aops;
182 else
183 inode->i_mapping->a_ops = &ext2_aops;
184 err = page_symlink(inode, symname, l);
185 if (err)
186 goto out_fail;
187 } else {
188 /* fast symlink */
189 inode->i_op = &ext2_fast_symlink_inode_operations;
190 memcpy((char*)(EXT2_I(inode)->i_data),symname,l);
191 inode->i_size = l-1;
193 mark_inode_dirty(inode);
195 err = ext2_add_nondir(dentry, inode);
196 out:
197 return err;
199 out_fail:
200 ext2_dec_count(inode);
201 iput (inode);
202 goto out;
205 static int ext2_link (struct dentry * old_dentry, struct inode * dir,
206 struct dentry *dentry)
208 struct inode *inode = old_dentry->d_inode;
210 if (inode->i_nlink >= EXT2_LINK_MAX)
211 return -EMLINK;
213 inode->i_ctime = CURRENT_TIME;
214 ext2_inc_count(inode);
215 atomic_inc(&inode->i_count);
217 return ext2_add_nondir(dentry, inode);
220 static int ext2_mkdir(struct inode * dir, struct dentry * dentry, int mode)
222 struct inode * inode;
223 int err = -EMLINK;
225 if (dir->i_nlink >= EXT2_LINK_MAX)
226 goto out;
228 ext2_inc_count(dir);
230 inode = ext2_new_inode (dir, S_IFDIR | mode);
231 err = PTR_ERR(inode);
232 if (IS_ERR(inode))
233 goto out_dir;
235 inode->i_op = &ext2_dir_inode_operations;
236 inode->i_fop = &ext2_dir_operations;
237 if (test_opt(inode->i_sb, NOBH))
238 inode->i_mapping->a_ops = &ext2_nobh_aops;
239 else
240 inode->i_mapping->a_ops = &ext2_aops;
242 ext2_inc_count(inode);
244 err = ext2_make_empty(inode, dir);
245 if (err)
246 goto out_fail;
248 err = ext2_add_link(dentry, inode);
249 if (err)
250 goto out_fail;
252 d_instantiate(dentry, inode);
253 out:
254 return err;
256 out_fail:
257 ext2_dec_count(inode);
258 ext2_dec_count(inode);
259 iput(inode);
260 out_dir:
261 ext2_dec_count(dir);
262 goto out;
265 static int ext2_unlink(struct inode * dir, struct dentry *dentry)
267 struct inode * inode = dentry->d_inode;
268 struct ext2_dir_entry_2 * de;
269 struct page * page;
270 int err = -ENOENT;
272 de = ext2_find_entry (dir, dentry, &page);
273 if (!de)
274 goto out;
276 err = ext2_delete_entry (de, page);
277 if (err)
278 goto out;
280 inode->i_ctime = dir->i_ctime;
281 ext2_dec_count(inode);
282 err = 0;
283 out:
284 return err;
287 static int ext2_rmdir (struct inode * dir, struct dentry *dentry)
289 struct inode * inode = dentry->d_inode;
290 int err = -ENOTEMPTY;
292 if (ext2_empty_dir(inode)) {
293 err = ext2_unlink(dir, dentry);
294 if (!err) {
295 inode->i_size = 0;
296 ext2_dec_count(inode);
297 ext2_dec_count(dir);
300 return err;
303 static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry,
304 struct inode * new_dir, struct dentry * new_dentry )
306 struct inode * old_inode = old_dentry->d_inode;
307 struct inode * new_inode = new_dentry->d_inode;
308 struct page * dir_page = NULL;
309 struct ext2_dir_entry_2 * dir_de = NULL;
310 struct page * old_page;
311 struct ext2_dir_entry_2 * old_de;
312 int err = -ENOENT;
314 old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
315 if (!old_de)
316 goto out;
318 if (S_ISDIR(old_inode->i_mode)) {
319 err = -EIO;
320 dir_de = ext2_dotdot(old_inode, &dir_page);
321 if (!dir_de)
322 goto out_old;
325 if (new_inode) {
326 struct page *new_page;
327 struct ext2_dir_entry_2 *new_de;
329 err = -ENOTEMPTY;
330 if (dir_de && !ext2_empty_dir (new_inode))
331 goto out_dir;
333 err = -ENOENT;
334 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
335 if (!new_de)
336 goto out_dir;
337 ext2_inc_count(old_inode);
338 ext2_set_link(new_dir, new_de, new_page, old_inode);
339 new_inode->i_ctime = CURRENT_TIME;
340 if (dir_de)
341 new_inode->i_nlink--;
342 ext2_dec_count(new_inode);
343 } else {
344 if (dir_de) {
345 err = -EMLINK;
346 if (new_dir->i_nlink >= EXT2_LINK_MAX)
347 goto out_dir;
349 ext2_inc_count(old_inode);
350 err = ext2_add_link(new_dentry, old_inode);
351 if (err) {
352 ext2_dec_count(old_inode);
353 goto out_dir;
355 if (dir_de)
356 ext2_inc_count(new_dir);
360 * Like most other Unix systems, set the ctime for inodes on a
361 * rename.
362 * ext2_dec_count() will mark the inode dirty.
364 old_inode->i_ctime = CURRENT_TIME;
366 ext2_delete_entry (old_de, old_page);
367 ext2_dec_count(old_inode);
369 if (dir_de) {
370 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
371 ext2_dec_count(old_dir);
373 return 0;
376 out_dir:
377 if (dir_de) {
378 kunmap(dir_page);
379 page_cache_release(dir_page);
381 out_old:
382 kunmap(old_page);
383 page_cache_release(old_page);
384 out:
385 return err;
388 struct inode_operations ext2_dir_inode_operations = {
389 .create = ext2_create,
390 .lookup = ext2_lookup,
391 .link = ext2_link,
392 .unlink = ext2_unlink,
393 .symlink = ext2_symlink,
394 .mkdir = ext2_mkdir,
395 .rmdir = ext2_rmdir,
396 .mknod = ext2_mknod,
397 .rename = ext2_rename,
398 .setxattr = ext2_setxattr,
399 .getxattr = ext2_getxattr,
400 .listxattr = ext2_listxattr,
401 .removexattr = ext2_removexattr,
402 .setattr = ext2_setattr,
403 .permission = ext2_permission,
406 struct inode_operations ext2_special_inode_operations = {
407 .setxattr = ext2_setxattr,
408 .getxattr = ext2_getxattr,
409 .listxattr = ext2_listxattr,
410 .removexattr = ext2_removexattr,
411 .setattr = ext2_setattr,
412 .permission = ext2_permission,