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(child->d_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 inode_nohighmem(inode);
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 inode->i_link = (char*)EXT2_I(inode)->i_data;
197 memcpy(inode->i_link, symname, l);
200 mark_inode_dirty(inode);
202 err = ext2_add_nondir(dentry, inode);
207 inode_dec_link_count(inode);
208 unlock_new_inode(inode);
213 static int ext2_link (struct dentry * old_dentry, struct inode * dir,
214 struct dentry *dentry)
216 struct inode *inode = d_inode(old_dentry);
219 err = dquot_initialize(dir);
223 inode->i_ctime = current_time(inode);
224 inode_inc_link_count(inode);
227 err = ext2_add_link(dentry, inode);
229 d_instantiate(dentry, inode);
232 inode_dec_link_count(inode);
237 static int ext2_mkdir(struct inode * dir, struct dentry * dentry, umode_t mode)
239 struct inode * inode;
242 err = dquot_initialize(dir);
246 inode_inc_link_count(dir);
248 inode = ext2_new_inode(dir, S_IFDIR | mode, &dentry->d_name);
249 err = PTR_ERR(inode);
253 inode->i_op = &ext2_dir_inode_operations;
254 inode->i_fop = &ext2_dir_operations;
255 if (test_opt(inode->i_sb, NOBH))
256 inode->i_mapping->a_ops = &ext2_nobh_aops;
258 inode->i_mapping->a_ops = &ext2_aops;
260 inode_inc_link_count(inode);
262 err = ext2_make_empty(inode, dir);
266 err = ext2_add_link(dentry, inode);
270 d_instantiate_new(dentry, inode);
275 inode_dec_link_count(inode);
276 inode_dec_link_count(inode);
277 unlock_new_inode(inode);
280 inode_dec_link_count(dir);
284 static int ext2_unlink(struct inode * dir, struct dentry *dentry)
286 struct inode * inode = d_inode(dentry);
287 struct ext2_dir_entry_2 * de;
291 err = dquot_initialize(dir);
295 de = ext2_find_entry (dir, &dentry->d_name, &page);
301 err = ext2_delete_entry (de, page);
305 inode->i_ctime = dir->i_ctime;
306 inode_dec_link_count(inode);
312 static int ext2_rmdir (struct inode * dir, struct dentry *dentry)
314 struct inode * inode = d_inode(dentry);
315 int err = -ENOTEMPTY;
317 if (ext2_empty_dir(inode)) {
318 err = ext2_unlink(dir, dentry);
321 inode_dec_link_count(inode);
322 inode_dec_link_count(dir);
328 static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry,
329 struct inode * new_dir, struct dentry * new_dentry,
332 struct inode * old_inode = d_inode(old_dentry);
333 struct inode * new_inode = d_inode(new_dentry);
334 struct page * dir_page = NULL;
335 struct ext2_dir_entry_2 * dir_de = NULL;
336 struct page * old_page;
337 struct ext2_dir_entry_2 * old_de;
340 if (flags & ~RENAME_NOREPLACE)
343 err = dquot_initialize(old_dir);
347 err = dquot_initialize(new_dir);
351 old_de = ext2_find_entry (old_dir, &old_dentry->d_name, &old_page);
357 if (S_ISDIR(old_inode->i_mode)) {
359 dir_de = ext2_dotdot(old_inode, &dir_page);
365 struct page *new_page;
366 struct ext2_dir_entry_2 *new_de;
369 if (dir_de && !ext2_empty_dir (new_inode))
373 new_de = ext2_find_entry (new_dir, &new_dentry->d_name, &new_page);
376 ext2_set_link(new_dir, new_de, new_page, old_inode, 1);
377 new_inode->i_ctime = current_time(new_inode);
379 drop_nlink(new_inode);
380 inode_dec_link_count(new_inode);
382 err = ext2_add_link(new_dentry, old_inode);
386 inode_inc_link_count(new_dir);
390 * Like most other Unix systems, set the ctime for inodes on a
393 old_inode->i_ctime = current_time(old_inode);
394 mark_inode_dirty(old_inode);
396 ext2_delete_entry (old_de, old_page);
399 if (old_dir != new_dir)
400 ext2_set_link(old_inode, dir_de, dir_page, new_dir, 0);
405 inode_dec_link_count(old_dir);
422 const struct inode_operations ext2_dir_inode_operations = {
423 .create = ext2_create,
424 .lookup = ext2_lookup,
426 .unlink = ext2_unlink,
427 .symlink = ext2_symlink,
431 .rename = ext2_rename,
432 #ifdef CONFIG_EXT2_FS_XATTR
433 .listxattr = ext2_listxattr,
435 .setattr = ext2_setattr,
436 .get_acl = ext2_get_acl,
437 .set_acl = ext2_set_acl,
438 .tmpfile = ext2_tmpfile,
441 const struct inode_operations ext2_special_inode_operations = {
442 #ifdef CONFIG_EXT2_FS_XATTR
443 .listxattr = ext2_listxattr,
445 .setattr = ext2_setattr,
446 .get_acl = ext2_get_acl,
447 .set_acl = ext2_set_acl,