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>
39 static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode)
41 int err = ext2_add_link(dentry, inode);
43 d_instantiate(dentry, inode);
44 unlock_new_inode(inode);
47 inode_dec_link_count(inode);
48 unlock_new_inode(inode);
57 static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
62 if (dentry->d_name.len > EXT2_NAME_LEN)
63 return ERR_PTR(-ENAMETOOLONG);
65 ino = ext2_inode_by_name(dir, &dentry->d_name);
68 inode = ext2_iget(dir->i_sb, ino);
69 if (unlikely(IS_ERR(inode))) {
70 if (PTR_ERR(inode) == -ESTALE) {
71 ext2_error(dir->i_sb, __func__,
72 "deleted inode referenced: %lu",
76 return ERR_CAST(inode);
80 return d_splice_alias(inode, dentry);
83 struct dentry *ext2_get_parent(struct dentry *child)
85 struct qstr dotdot = {.name = "..", .len = 2};
86 unsigned long ino = ext2_inode_by_name(child->d_inode, &dotdot);
88 return ERR_PTR(-ENOENT);
89 return d_obtain_alias(ext2_iget(child->d_inode->i_sb, ino));
93 * By the time this is called, we already have created
94 * the directory cache entry for the new file, but it
95 * is so far negative - it has no inode.
97 * If the create succeeds, we fill in the inode information
98 * with d_instantiate().
100 static int ext2_create (struct inode * dir, struct dentry * dentry, int mode, struct nameidata *nd)
102 struct inode * inode = ext2_new_inode (dir, mode);
103 int err = PTR_ERR(inode);
104 if (!IS_ERR(inode)) {
105 inode->i_op = &ext2_file_inode_operations;
106 if (ext2_use_xip(inode->i_sb)) {
107 inode->i_mapping->a_ops = &ext2_aops_xip;
108 inode->i_fop = &ext2_xip_file_operations;
109 } else if (test_opt(inode->i_sb, NOBH)) {
110 inode->i_mapping->a_ops = &ext2_nobh_aops;
111 inode->i_fop = &ext2_file_operations;
113 inode->i_mapping->a_ops = &ext2_aops;
114 inode->i_fop = &ext2_file_operations;
116 mark_inode_dirty(inode);
117 err = ext2_add_nondir(dentry, inode);
122 static int ext2_mknod (struct inode * dir, struct dentry *dentry, int mode, dev_t rdev)
124 struct inode * inode;
127 if (!new_valid_dev(rdev))
130 inode = ext2_new_inode (dir, mode);
131 err = PTR_ERR(inode);
132 if (!IS_ERR(inode)) {
133 init_special_inode(inode, inode->i_mode, rdev);
134 #ifdef CONFIG_EXT2_FS_XATTR
135 inode->i_op = &ext2_special_inode_operations;
137 mark_inode_dirty(inode);
138 err = ext2_add_nondir(dentry, inode);
143 static int ext2_symlink (struct inode * dir, struct dentry * dentry,
144 const char * symname)
146 struct super_block * sb = dir->i_sb;
147 int err = -ENAMETOOLONG;
148 unsigned l = strlen(symname)+1;
149 struct inode * inode;
151 if (l > sb->s_blocksize)
154 inode = ext2_new_inode (dir, S_IFLNK | S_IRWXUGO);
155 err = PTR_ERR(inode);
159 if (l > sizeof (EXT2_I(inode)->i_data)) {
161 inode->i_op = &ext2_symlink_inode_operations;
162 if (test_opt(inode->i_sb, NOBH))
163 inode->i_mapping->a_ops = &ext2_nobh_aops;
165 inode->i_mapping->a_ops = &ext2_aops;
166 err = page_symlink(inode, symname, l);
171 inode->i_op = &ext2_fast_symlink_inode_operations;
172 memcpy((char*)(EXT2_I(inode)->i_data),symname,l);
175 mark_inode_dirty(inode);
177 err = ext2_add_nondir(dentry, inode);
182 inode_dec_link_count(inode);
183 unlock_new_inode(inode);
188 static int ext2_link (struct dentry * old_dentry, struct inode * dir,
189 struct dentry *dentry)
191 struct inode *inode = old_dentry->d_inode;
194 if (inode->i_nlink >= EXT2_LINK_MAX)
197 inode->i_ctime = CURRENT_TIME_SEC;
198 inode_inc_link_count(inode);
199 atomic_inc(&inode->i_count);
201 err = ext2_add_link(dentry, inode);
203 d_instantiate(dentry, inode);
206 inode_dec_link_count(inode);
211 static int ext2_mkdir(struct inode * dir, struct dentry * dentry, int mode)
213 struct inode * inode;
216 if (dir->i_nlink >= EXT2_LINK_MAX)
219 inode_inc_link_count(dir);
221 inode = ext2_new_inode (dir, S_IFDIR | mode);
222 err = PTR_ERR(inode);
226 inode->i_op = &ext2_dir_inode_operations;
227 inode->i_fop = &ext2_dir_operations;
228 if (test_opt(inode->i_sb, NOBH))
229 inode->i_mapping->a_ops = &ext2_nobh_aops;
231 inode->i_mapping->a_ops = &ext2_aops;
233 inode_inc_link_count(inode);
235 err = ext2_make_empty(inode, dir);
239 err = ext2_add_link(dentry, inode);
243 d_instantiate(dentry, inode);
244 unlock_new_inode(inode);
249 inode_dec_link_count(inode);
250 inode_dec_link_count(inode);
251 unlock_new_inode(inode);
254 inode_dec_link_count(dir);
258 static int ext2_unlink(struct inode * dir, struct dentry *dentry)
260 struct inode * inode = dentry->d_inode;
261 struct ext2_dir_entry_2 * de;
265 de = ext2_find_entry (dir, &dentry->d_name, &page);
269 err = ext2_delete_entry (de, page);
273 inode->i_ctime = dir->i_ctime;
274 inode_dec_link_count(inode);
280 static int ext2_rmdir (struct inode * dir, struct dentry *dentry)
282 struct inode * inode = dentry->d_inode;
283 int err = -ENOTEMPTY;
285 if (ext2_empty_dir(inode)) {
286 err = ext2_unlink(dir, dentry);
289 inode_dec_link_count(inode);
290 inode_dec_link_count(dir);
296 static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry,
297 struct inode * new_dir, struct dentry * new_dentry )
299 struct inode * old_inode = old_dentry->d_inode;
300 struct inode * new_inode = new_dentry->d_inode;
301 struct page * dir_page = NULL;
302 struct ext2_dir_entry_2 * dir_de = NULL;
303 struct page * old_page;
304 struct ext2_dir_entry_2 * old_de;
307 old_de = ext2_find_entry (old_dir, &old_dentry->d_name, &old_page);
311 if (S_ISDIR(old_inode->i_mode)) {
313 dir_de = ext2_dotdot(old_inode, &dir_page);
319 struct page *new_page;
320 struct ext2_dir_entry_2 *new_de;
323 if (dir_de && !ext2_empty_dir (new_inode))
327 new_de = ext2_find_entry (new_dir, &new_dentry->d_name, &new_page);
330 inode_inc_link_count(old_inode);
331 ext2_set_link(new_dir, new_de, new_page, old_inode, 1);
332 new_inode->i_ctime = CURRENT_TIME_SEC;
334 drop_nlink(new_inode);
335 inode_dec_link_count(new_inode);
339 if (new_dir->i_nlink >= EXT2_LINK_MAX)
342 inode_inc_link_count(old_inode);
343 err = ext2_add_link(new_dentry, old_inode);
345 inode_dec_link_count(old_inode);
349 inode_inc_link_count(new_dir);
353 * Like most other Unix systems, set the ctime for inodes on a
355 * inode_dec_link_count() will mark the inode dirty.
357 old_inode->i_ctime = CURRENT_TIME_SEC;
359 ext2_delete_entry (old_de, old_page);
360 inode_dec_link_count(old_inode);
363 if (old_dir != new_dir)
364 ext2_set_link(old_inode, dir_de, dir_page, new_dir, 0);
365 inode_dec_link_count(old_dir);
373 page_cache_release(dir_page);
377 page_cache_release(old_page);
382 const struct inode_operations ext2_dir_inode_operations = {
383 .create = ext2_create,
384 .lookup = ext2_lookup,
386 .unlink = ext2_unlink,
387 .symlink = ext2_symlink,
391 .rename = ext2_rename,
392 #ifdef CONFIG_EXT2_FS_XATTR
393 .setxattr = generic_setxattr,
394 .getxattr = generic_getxattr,
395 .listxattr = ext2_listxattr,
396 .removexattr = generic_removexattr,
398 .setattr = ext2_setattr,
399 .permission = ext2_permission,
402 const struct inode_operations ext2_special_inode_operations = {
403 #ifdef CONFIG_EXT2_FS_XATTR
404 .setxattr = generic_setxattr,
405 .getxattr = generic_getxattr,
406 .listxattr = ext2_listxattr,
407 .removexattr = generic_removexattr,
409 .setattr = ext2_setattr,
410 .permission = ext2_permission,