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>
41 * Couple of helper functions - make the code slightly cleaner.
44 static inline void ext2_inc_count(struct inode *inode)
47 mark_inode_dirty(inode);
50 static inline void ext2_dec_count(struct inode *inode)
53 mark_inode_dirty(inode);
56 static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode)
58 int err = ext2_add_link(dentry, inode);
60 d_instantiate(dentry, inode);
63 ext2_dec_count(inode);
72 static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
77 if (dentry->d_name.len > EXT2_NAME_LEN)
78 return ERR_PTR(-ENAMETOOLONG);
80 ino = ext2_inode_by_name(dir, dentry);
83 inode = iget(dir->i_sb, ino);
85 return ERR_PTR(-EACCES);
86 vx_propagate_xid(nd, inode);
88 return d_splice_alias(inode, dentry);
91 struct dentry *ext2_get_parent(struct dentry *child)
94 struct dentry *parent;
98 dotdot.d_name.name = "..";
99 dotdot.d_name.len = 2;
101 ino = ext2_inode_by_name(child->d_inode, &dotdot);
103 return ERR_PTR(-ENOENT);
104 inode = iget(child->d_inode->i_sb, ino);
107 return ERR_PTR(-EACCES);
108 parent = d_alloc_anon(inode);
111 parent = ERR_PTR(-ENOMEM);
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.
121 * If the create succeeds, we fill in the inode information
122 * with d_instantiate().
124 static int ext2_create (struct inode * dir, struct dentry * dentry, int mode, struct nameidata *nd)
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;
137 inode->i_mapping->a_ops = &ext2_aops;
138 inode->i_fop = &ext2_file_operations;
140 mark_inode_dirty(inode);
141 err = ext2_add_nondir(dentry, inode);
146 static int ext2_mknod (struct inode * dir, struct dentry *dentry, int mode, dev_t rdev)
148 struct inode * inode;
151 if (!new_valid_dev(rdev))
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;
161 mark_inode_dirty(inode);
162 err = ext2_add_nondir(dentry, inode);
167 static int ext2_symlink (struct inode * dir, struct dentry * dentry,
168 const char * symname)
170 struct super_block * sb = dir->i_sb;
171 int err = -ENAMETOOLONG;
172 unsigned l = strlen(symname)+1;
173 struct inode * inode;
175 if (l > sb->s_blocksize)
178 inode = ext2_new_inode (dir, S_IFLNK | S_IRWXUGO);
179 err = PTR_ERR(inode);
183 if (l > sizeof (EXT2_I(inode)->i_data)) {
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;
189 inode->i_mapping->a_ops = &ext2_aops;
190 err = page_symlink(inode, symname, l);
195 inode->i_op = &ext2_fast_symlink_inode_operations;
196 memcpy((char*)(EXT2_I(inode)->i_data),symname,l);
199 mark_inode_dirty(inode);
201 err = ext2_add_nondir(dentry, inode);
206 ext2_dec_count(inode);
211 static int ext2_link (struct dentry * old_dentry, struct inode * dir,
212 struct dentry *dentry)
214 struct inode *inode = old_dentry->d_inode;
216 if (inode->i_nlink >= EXT2_LINK_MAX)
219 inode->i_ctime = CURRENT_TIME_SEC;
220 ext2_inc_count(inode);
221 atomic_inc(&inode->i_count);
223 return ext2_add_nondir(dentry, inode);
226 static int ext2_mkdir(struct inode * dir, struct dentry * dentry, int mode)
228 struct inode * inode;
231 if (dir->i_nlink >= EXT2_LINK_MAX)
236 inode = ext2_new_inode (dir, S_IFDIR | mode);
237 err = PTR_ERR(inode);
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;
246 inode->i_mapping->a_ops = &ext2_aops;
248 ext2_inc_count(inode);
250 err = ext2_make_empty(inode, dir);
254 err = ext2_add_link(dentry, inode);
258 d_instantiate(dentry, inode);
263 ext2_dec_count(inode);
264 ext2_dec_count(inode);
271 static int ext2_unlink(struct inode * dir, struct dentry *dentry)
273 struct inode * inode = dentry->d_inode;
274 struct ext2_dir_entry_2 * de;
278 de = ext2_find_entry (dir, dentry, &page);
282 err = ext2_delete_entry (de, page);
286 inode->i_ctime = dir->i_ctime;
287 ext2_dec_count(inode);
293 static int ext2_rmdir (struct inode * dir, struct dentry *dentry)
295 struct inode * inode = dentry->d_inode;
296 int err = -ENOTEMPTY;
298 if (ext2_empty_dir(inode)) {
299 err = ext2_unlink(dir, dentry);
302 ext2_dec_count(inode);
309 static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry,
310 struct inode * new_dir, struct dentry * new_dentry )
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;
320 old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
324 if (S_ISDIR(old_inode->i_mode)) {
326 dir_de = ext2_dotdot(old_inode, &dir_page);
332 struct page *new_page;
333 struct ext2_dir_entry_2 *new_de;
336 if (dir_de && !ext2_empty_dir (new_inode))
340 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
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;
347 new_inode->i_nlink--;
348 ext2_dec_count(new_inode);
352 if (new_dir->i_nlink >= EXT2_LINK_MAX)
355 ext2_inc_count(old_inode);
356 err = ext2_add_link(new_dentry, old_inode);
358 ext2_dec_count(old_inode);
362 ext2_inc_count(new_dir);
366 * Like most other Unix systems, set the ctime for inodes on a
368 * ext2_dec_count() will mark the inode dirty.
370 old_inode->i_ctime = CURRENT_TIME_SEC;
372 ext2_delete_entry (old_de, old_page);
373 ext2_dec_count(old_inode);
376 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
377 ext2_dec_count(old_dir);
385 page_cache_release(dir_page);
389 page_cache_release(old_page);
394 struct inode_operations ext2_dir_inode_operations = {
395 .create = ext2_create,
396 .lookup = ext2_lookup,
398 .unlink = ext2_unlink,
399 .symlink = ext2_symlink,
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,
410 .setattr = ext2_setattr,
411 .permission = ext2_permission,
412 .sync_flags = ext2_sync_flags,
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,
422 .setattr = ext2_setattr,
423 .permission = ext2_permission,
424 .sync_flags = ext2_sync_flags,