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
6 * viro@parcelfarce.linux.theplanet.co.uk
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.
14 * The only non-static object here is ext2_dir_inode_operations.
16 * TODO: get rid of kmap() use, add readahead.
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)
25 * linux/fs/minix/namei.c
27 * Copyright (C) 1991, 1992 Linus Torvalds
29 * Big-endian to little-endian byte-swapping/bitmaps by
30 * David S. Miller (davem@caip.rutgers.edu), 1995
33 #include <linux/pagemap.h>
34 #include <linux/vserver/xid.h>
40 * Couple of helper functions - make the code slightly cleaner.
43 static inline void ext2_inc_count(struct inode *inode)
46 mark_inode_dirty(inode);
49 static inline void ext2_dec_count(struct inode *inode)
52 mark_inode_dirty(inode);
55 static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode)
57 int err = ext2_add_link(dentry, inode);
59 d_instantiate(dentry, inode);
62 ext2_dec_count(inode);
71 static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
76 if (dentry->d_name.len > EXT2_NAME_LEN)
77 return ERR_PTR(-ENAMETOOLONG);
79 ino = ext2_inode_by_name(dir, dentry);
82 inode = iget(dir->i_sb, ino);
84 return ERR_PTR(-EACCES);
85 vx_propagate_xid(nd, inode);
88 return d_splice_alias(inode, dentry);
93 struct dentry *ext2_get_parent(struct dentry *child)
96 struct dentry *parent;
100 dotdot.d_name.name = "..";
101 dotdot.d_name.len = 2;
103 ino = ext2_inode_by_name(child->d_inode, &dotdot);
105 return ERR_PTR(-ENOENT);
106 inode = iget(child->d_inode->i_sb, ino);
109 return ERR_PTR(-EACCES);
110 parent = d_alloc_anon(inode);
113 parent = ERR_PTR(-ENOMEM);
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.
123 * If the create succeeds, we fill in the inode information
124 * with d_instantiate().
126 static int ext2_create (struct inode * dir, struct dentry * dentry, int mode, struct nameidata *nd)
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;
136 inode->i_mapping->a_ops = &ext2_aops;
137 mark_inode_dirty(inode);
138 err = ext2_add_nondir(dentry, inode);
143 static int ext2_mknod (struct inode * dir, struct dentry *dentry, int mode, dev_t rdev)
145 struct inode * inode;
148 if (!new_valid_dev(rdev))
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;
158 mark_inode_dirty(inode);
159 err = ext2_add_nondir(dentry, inode);
164 static int ext2_symlink (struct inode * dir, struct dentry * dentry,
165 const char * symname)
167 struct super_block * sb = dir->i_sb;
168 int err = -ENAMETOOLONG;
169 unsigned l = strlen(symname)+1;
170 struct inode * inode;
172 if (l > sb->s_blocksize)
175 inode = ext2_new_inode (dir, S_IFLNK | S_IRWXUGO);
176 err = PTR_ERR(inode);
180 if (l > sizeof (EXT2_I(inode)->i_data)) {
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;
186 inode->i_mapping->a_ops = &ext2_aops;
187 err = page_symlink(inode, symname, l);
192 inode->i_op = &ext2_fast_symlink_inode_operations;
193 memcpy((char*)(EXT2_I(inode)->i_data),symname,l);
196 mark_inode_dirty(inode);
198 err = ext2_add_nondir(dentry, inode);
203 ext2_dec_count(inode);
208 static int ext2_link (struct dentry * old_dentry, struct inode * dir,
209 struct dentry *dentry)
211 struct inode *inode = old_dentry->d_inode;
213 if (inode->i_nlink >= EXT2_LINK_MAX)
216 inode->i_ctime = CURRENT_TIME_SEC;
217 ext2_inc_count(inode);
218 atomic_inc(&inode->i_count);
220 return ext2_add_nondir(dentry, inode);
223 static int ext2_mkdir(struct inode * dir, struct dentry * dentry, int mode)
225 struct inode * inode;
228 if (dir->i_nlink >= EXT2_LINK_MAX)
233 inode = ext2_new_inode (dir, S_IFDIR | mode);
234 err = PTR_ERR(inode);
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;
243 inode->i_mapping->a_ops = &ext2_aops;
245 ext2_inc_count(inode);
247 err = ext2_make_empty(inode, dir);
251 err = ext2_add_link(dentry, inode);
255 d_instantiate(dentry, inode);
260 ext2_dec_count(inode);
261 ext2_dec_count(inode);
268 static int ext2_unlink(struct inode * dir, struct dentry *dentry)
270 struct inode * inode = dentry->d_inode;
271 struct ext2_dir_entry_2 * de;
275 de = ext2_find_entry (dir, dentry, &page);
279 err = ext2_delete_entry (de, page);
283 inode->i_ctime = dir->i_ctime;
284 ext2_dec_count(inode);
290 static int ext2_rmdir (struct inode * dir, struct dentry *dentry)
292 struct inode * inode = dentry->d_inode;
293 int err = -ENOTEMPTY;
295 if (ext2_empty_dir(inode)) {
296 err = ext2_unlink(dir, dentry);
299 ext2_dec_count(inode);
306 static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry,
307 struct inode * new_dir, struct dentry * new_dentry )
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;
317 old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
321 if (S_ISDIR(old_inode->i_mode)) {
323 dir_de = ext2_dotdot(old_inode, &dir_page);
329 struct page *new_page;
330 struct ext2_dir_entry_2 *new_de;
333 if (dir_de && !ext2_empty_dir (new_inode))
337 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
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;
344 new_inode->i_nlink--;
345 ext2_dec_count(new_inode);
349 if (new_dir->i_nlink >= EXT2_LINK_MAX)
352 ext2_inc_count(old_inode);
353 err = ext2_add_link(new_dentry, old_inode);
355 ext2_dec_count(old_inode);
359 ext2_inc_count(new_dir);
363 * Like most other Unix systems, set the ctime for inodes on a
365 * ext2_dec_count() will mark the inode dirty.
367 old_inode->i_ctime = CURRENT_TIME_SEC;
369 ext2_delete_entry (old_de, old_page);
370 ext2_dec_count(old_inode);
373 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
374 ext2_dec_count(old_dir);
382 page_cache_release(dir_page);
386 page_cache_release(old_page);
391 struct inode_operations ext2_dir_inode_operations = {
392 .create = ext2_create,
393 .lookup = ext2_lookup,
395 .unlink = ext2_unlink,
396 .symlink = ext2_symlink,
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,
407 .setattr = ext2_setattr,
408 .permission = ext2_permission,
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,
418 .setattr = ext2_setattr,
419 .permission = ext2_permission,