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/quotaops.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_new(dentry, inode);
46 inode_dec_link_count(inode);
47 unlock_new_inode(inode);
56 static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, unsigned int flags)
61 if (dentry->d_name.len > EXT2_NAME_LEN)
62 return ERR_PTR(-ENAMETOOLONG);
64 ino = ext2_inode_by_name(dir, &dentry->d_name);
67 inode = ext2_iget(dir->i_sb, ino);
68 if (inode == ERR_PTR(-ESTALE)) {
69 ext2_error(dir->i_sb, __func__,
70 "deleted inode referenced: %lu",
75 return d_splice_alias(inode, dentry);
78 struct dentry *ext2_get_parent(struct dentry *child)
80 struct qstr dotdot = QSTR_INIT("..", 2);
81 unsigned long ino = ext2_inode_by_name(d_inode(child), &dotdot);
83 return ERR_PTR(-ENOENT);
84 return d_obtain_alias(ext2_iget(d_inode(child)->i_sb, ino));
88 * By the time this is called, we already have created
89 * the directory cache entry for the new file, but it
90 * is so far negative - it has no inode.
92 * If the create succeeds, we fill in the inode information
93 * with d_instantiate().
95 static int ext2_create (struct inode * dir, struct dentry * dentry, umode_t mode, bool excl)
100 err = dquot_initialize(dir);
104 inode = ext2_new_inode(dir, mode, &dentry->d_name);
106 return PTR_ERR(inode);
108 inode->i_op = &ext2_file_inode_operations;
109 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 return ext2_add_nondir(dentry, inode);
120 static int ext2_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
122 struct inode *inode = ext2_new_inode(dir, mode, NULL);
124 return PTR_ERR(inode);
126 inode->i_op = &ext2_file_inode_operations;
127 if (test_opt(inode->i_sb, NOBH)) {
128 inode->i_mapping->a_ops = &ext2_nobh_aops;
129 inode->i_fop = &ext2_file_operations;
131 inode->i_mapping->a_ops = &ext2_aops;
132 inode->i_fop = &ext2_file_operations;
134 mark_inode_dirty(inode);
135 d_tmpfile(dentry, inode);
136 unlock_new_inode(inode);
140 static int ext2_mknod (struct inode * dir, struct dentry *dentry, umode_t mode, dev_t rdev)
142 struct inode * inode;
145 err = dquot_initialize(dir);
149 inode = ext2_new_inode (dir, mode, &dentry->d_name);
150 err = PTR_ERR(inode);
151 if (!IS_ERR(inode)) {
152 init_special_inode(inode, inode->i_mode, rdev);
153 #ifdef CONFIG_EXT2_FS_XATTR
154 inode->i_op = &ext2_special_inode_operations;
156 mark_inode_dirty(inode);
157 err = ext2_add_nondir(dentry, inode);
162 static int ext2_symlink (struct inode * dir, struct dentry * dentry,
163 const char * symname)
165 struct super_block * sb = dir->i_sb;
166 int err = -ENAMETOOLONG;
167 unsigned l = strlen(symname)+1;
168 struct inode * inode;
170 if (l > sb->s_blocksize)
173 err = dquot_initialize(dir);
177 inode = ext2_new_inode (dir, S_IFLNK | S_IRWXUGO, &dentry->d_name);
178 err = PTR_ERR(inode);
182 if (l > sizeof (EXT2_I(inode)->i_data)) {
184 inode->i_op = &ext2_symlink_inode_operations;
185 if (test_opt(inode->i_sb, NOBH))
186 inode->i_mapping->a_ops = &ext2_nobh_aops;
188 inode->i_mapping->a_ops = &ext2_aops;
189 err = page_symlink(inode, symname, l);
194 inode->i_op = &ext2_fast_symlink_inode_operations;
195 inode->i_link = (char*)EXT2_I(inode)->i_data;
196 memcpy(inode->i_link, symname, l);
199 mark_inode_dirty(inode);
201 err = ext2_add_nondir(dentry, inode);
206 inode_dec_link_count(inode);
207 unlock_new_inode(inode);
212 static int ext2_link (struct dentry * old_dentry, struct inode * dir,
213 struct dentry *dentry)
215 struct inode *inode = d_inode(old_dentry);
218 err = dquot_initialize(dir);
222 inode->i_ctime = CURRENT_TIME_SEC;
223 inode_inc_link_count(inode);
226 err = ext2_add_link(dentry, inode);
228 d_instantiate(dentry, inode);
231 inode_dec_link_count(inode);
236 static int ext2_mkdir(struct inode * dir, struct dentry * dentry, umode_t mode)
238 struct inode * inode;
241 err = dquot_initialize(dir);
245 inode_inc_link_count(dir);
247 inode = ext2_new_inode(dir, S_IFDIR | mode, &dentry->d_name);
248 err = PTR_ERR(inode);
252 inode->i_op = &ext2_dir_inode_operations;
253 inode->i_fop = &ext2_dir_operations;
254 if (test_opt(inode->i_sb, NOBH))
255 inode->i_mapping->a_ops = &ext2_nobh_aops;
257 inode->i_mapping->a_ops = &ext2_aops;
259 inode_inc_link_count(inode);
261 err = ext2_make_empty(inode, dir);
265 err = ext2_add_link(dentry, inode);
269 d_instantiate_new(dentry, inode);
274 inode_dec_link_count(inode);
275 inode_dec_link_count(inode);
276 unlock_new_inode(inode);
279 inode_dec_link_count(dir);
283 static int ext2_unlink(struct inode * dir, struct dentry *dentry)
285 struct inode * inode = d_inode(dentry);
286 struct ext2_dir_entry_2 * de;
290 err = dquot_initialize(dir);
294 de = ext2_find_entry (dir, &dentry->d_name, &page);
300 err = ext2_delete_entry (de, page);
304 inode->i_ctime = dir->i_ctime;
305 inode_dec_link_count(inode);
311 static int ext2_rmdir (struct inode * dir, struct dentry *dentry)
313 struct inode * inode = d_inode(dentry);
314 int err = -ENOTEMPTY;
316 if (ext2_empty_dir(inode)) {
317 err = ext2_unlink(dir, dentry);
320 inode_dec_link_count(inode);
321 inode_dec_link_count(dir);
327 static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry,
328 struct inode * new_dir, struct dentry * new_dentry )
330 struct inode * old_inode = d_inode(old_dentry);
331 struct inode * new_inode = d_inode(new_dentry);
332 struct page * dir_page = NULL;
333 struct ext2_dir_entry_2 * dir_de = NULL;
334 struct page * old_page;
335 struct ext2_dir_entry_2 * old_de;
338 err = dquot_initialize(old_dir);
342 err = dquot_initialize(new_dir);
346 old_de = ext2_find_entry (old_dir, &old_dentry->d_name, &old_page);
352 if (S_ISDIR(old_inode->i_mode)) {
354 dir_de = ext2_dotdot(old_inode, &dir_page);
360 struct page *new_page;
361 struct ext2_dir_entry_2 *new_de;
364 if (dir_de && !ext2_empty_dir (new_inode))
368 new_de = ext2_find_entry (new_dir, &new_dentry->d_name, &new_page);
371 ext2_set_link(new_dir, new_de, new_page, old_inode, 1);
372 new_inode->i_ctime = CURRENT_TIME_SEC;
374 drop_nlink(new_inode);
375 inode_dec_link_count(new_inode);
377 err = ext2_add_link(new_dentry, old_inode);
381 inode_inc_link_count(new_dir);
385 * Like most other Unix systems, set the ctime for inodes on a
388 old_inode->i_ctime = CURRENT_TIME_SEC;
389 mark_inode_dirty(old_inode);
391 ext2_delete_entry (old_de, old_page);
394 if (old_dir != new_dir)
395 ext2_set_link(old_inode, dir_de, dir_page, new_dir, 0);
398 page_cache_release(dir_page);
400 inode_dec_link_count(old_dir);
408 page_cache_release(dir_page);
412 page_cache_release(old_page);
417 const struct inode_operations ext2_dir_inode_operations = {
418 .create = ext2_create,
419 .lookup = ext2_lookup,
421 .unlink = ext2_unlink,
422 .symlink = ext2_symlink,
426 .rename = ext2_rename,
427 #ifdef CONFIG_EXT2_FS_XATTR
428 .setxattr = generic_setxattr,
429 .getxattr = generic_getxattr,
430 .listxattr = ext2_listxattr,
431 .removexattr = generic_removexattr,
433 .setattr = ext2_setattr,
434 .get_acl = ext2_get_acl,
435 .set_acl = ext2_set_acl,
436 .tmpfile = ext2_tmpfile,
439 const struct inode_operations ext2_special_inode_operations = {
440 #ifdef CONFIG_EXT2_FS_XATTR
441 .setxattr = generic_setxattr,
442 .getxattr = generic_getxattr,
443 .listxattr = ext2_listxattr,
444 .removexattr = generic_removexattr,
446 .setattr = ext2_setattr,
447 .get_acl = ext2_get_acl,
448 .set_acl = ext2_set_acl,