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 static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode)
42 int err = ext2_add_link(dentry, inode);
44 d_instantiate(dentry, inode);
47 inode_dec_link_count(inode);
56 static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
61 if (dentry->d_name.len > EXT2_NAME_LEN)
62 return ERR_PTR(-ENAMETOOLONG);
64 ino = ext2_inode_by_name(dir, dentry);
67 inode = iget(dir->i_sb, ino);
69 return ERR_PTR(-EACCES);
70 vx_propagate_xid(nd, inode);
72 return d_splice_alias(inode, dentry);
75 struct dentry *ext2_get_parent(struct dentry *child)
78 struct dentry *parent;
82 dotdot.d_name.name = "..";
83 dotdot.d_name.len = 2;
85 ino = ext2_inode_by_name(child->d_inode, &dotdot);
87 return ERR_PTR(-ENOENT);
88 inode = iget(child->d_inode->i_sb, ino);
91 return ERR_PTR(-EACCES);
92 parent = d_alloc_anon(inode);
95 parent = ERR_PTR(-ENOMEM);
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.
105 * If the create succeeds, we fill in the inode information
106 * with d_instantiate().
108 static int ext2_create (struct inode * dir, struct dentry * dentry, int mode, struct nameidata *nd)
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;
121 inode->i_mapping->a_ops = &ext2_aops;
122 inode->i_fop = &ext2_file_operations;
124 mark_inode_dirty(inode);
125 err = ext2_add_nondir(dentry, inode);
130 static int ext2_mknod (struct inode * dir, struct dentry *dentry, int mode, dev_t rdev)
132 struct inode * inode;
135 if (!new_valid_dev(rdev))
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;
145 mark_inode_dirty(inode);
146 err = ext2_add_nondir(dentry, inode);
151 static int ext2_symlink (struct inode * dir, struct dentry * dentry,
152 const char * symname)
154 struct super_block * sb = dir->i_sb;
155 int err = -ENAMETOOLONG;
156 unsigned l = strlen(symname)+1;
157 struct inode * inode;
159 if (l > sb->s_blocksize)
162 inode = ext2_new_inode (dir, S_IFLNK | S_IRWXUGO);
163 err = PTR_ERR(inode);
167 if (l > sizeof (EXT2_I(inode)->i_data)) {
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;
173 inode->i_mapping->a_ops = &ext2_aops;
174 err = page_symlink(inode, symname, l);
179 inode->i_op = &ext2_fast_symlink_inode_operations;
180 memcpy((char*)(EXT2_I(inode)->i_data),symname,l);
183 mark_inode_dirty(inode);
185 err = ext2_add_nondir(dentry, inode);
190 inode_dec_link_count(inode);
195 static int ext2_link (struct dentry * old_dentry, struct inode * dir,
196 struct dentry *dentry)
198 struct inode *inode = old_dentry->d_inode;
200 if (inode->i_nlink >= EXT2_LINK_MAX)
203 inode->i_ctime = CURRENT_TIME_SEC;
204 inode_inc_link_count(inode);
205 atomic_inc(&inode->i_count);
207 return ext2_add_nondir(dentry, inode);
210 static int ext2_mkdir(struct inode * dir, struct dentry * dentry, int mode)
212 struct inode * inode;
215 if (dir->i_nlink >= EXT2_LINK_MAX)
218 inode_inc_link_count(dir);
220 inode = ext2_new_inode (dir, S_IFDIR | mode);
221 err = PTR_ERR(inode);
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;
230 inode->i_mapping->a_ops = &ext2_aops;
232 inode_inc_link_count(inode);
234 err = ext2_make_empty(inode, dir);
238 err = ext2_add_link(dentry, inode);
242 d_instantiate(dentry, inode);
247 inode_dec_link_count(inode);
248 inode_dec_link_count(inode);
251 inode_dec_link_count(dir);
255 static int ext2_unlink(struct inode * dir, struct dentry *dentry)
257 struct inode * inode = dentry->d_inode;
258 struct ext2_dir_entry_2 * de;
262 de = ext2_find_entry (dir, dentry, &page);
266 err = ext2_delete_entry (de, page);
270 inode->i_ctime = dir->i_ctime;
271 inode_dec_link_count(inode);
277 static int ext2_rmdir (struct inode * dir, struct dentry *dentry)
279 struct inode * inode = dentry->d_inode;
280 int err = -ENOTEMPTY;
282 if (ext2_empty_dir(inode)) {
283 err = ext2_unlink(dir, dentry);
286 inode_dec_link_count(inode);
287 inode_dec_link_count(dir);
293 static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry,
294 struct inode * new_dir, struct dentry * new_dentry )
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;
304 old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
308 if (S_ISDIR(old_inode->i_mode)) {
310 dir_de = ext2_dotdot(old_inode, &dir_page);
316 struct page *new_page;
317 struct ext2_dir_entry_2 *new_de;
320 if (dir_de && !ext2_empty_dir (new_inode))
324 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
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;
331 new_inode->i_nlink--;
332 inode_dec_link_count(new_inode);
336 if (new_dir->i_nlink >= EXT2_LINK_MAX)
339 inode_inc_link_count(old_inode);
340 err = ext2_add_link(new_dentry, old_inode);
342 inode_dec_link_count(old_inode);
346 inode_inc_link_count(new_dir);
350 * Like most other Unix systems, set the ctime for inodes on a
352 * inode_dec_link_count() will mark the inode dirty.
354 old_inode->i_ctime = CURRENT_TIME_SEC;
356 ext2_delete_entry (old_de, old_page);
357 inode_dec_link_count(old_inode);
360 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
361 inode_dec_link_count(old_dir);
369 page_cache_release(dir_page);
373 page_cache_release(old_page);
378 struct inode_operations ext2_dir_inode_operations = {
379 .create = ext2_create,
380 .lookup = ext2_lookup,
382 .unlink = ext2_unlink,
383 .symlink = ext2_symlink,
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,
394 .setattr = ext2_setattr,
395 .permission = ext2_permission,
396 .sync_flags = ext2_sync_flags,
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,
406 .setattr = ext2_setattr,
407 .permission = ext2_permission,
408 .sync_flags = ext2_sync_flags,