2 * namei.c - NILFS pathname lookup operations.
4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Modified for NILFS by Amagai Yoshiji <amagai@osrg.net>,
21 * Ryusuke Konishi <ryusuke@osrg.net>
24 * linux/fs/ext2/namei.c
26 * Copyright (C) 1992, 1993, 1994, 1995
27 * Remy Card (card@masi.ibp.fr)
28 * Laboratoire MASI - Institut Blaise Pascal
29 * Universite Pierre et Marie Curie (Paris VI)
33 * linux/fs/minix/namei.c
35 * Copyright (C) 1991, 1992 Linus Torvalds
37 * Big-endian to little-endian byte-swapping/bitmaps by
38 * David S. Miller (davem@caip.rutgers.edu), 1995
41 #include <linux/pagemap.h>
45 static inline int nilfs_add_nondir(struct dentry *dentry, struct inode *inode)
47 int err = nilfs_add_link(dentry, inode);
49 d_instantiate(dentry, inode);
52 inode_dec_link_count(inode);
61 static struct dentry *
62 nilfs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
67 if (dentry->d_name.len > NILFS_NAME_LEN)
68 return ERR_PTR(-ENAMETOOLONG);
70 ino = nilfs_inode_by_name(dir, &dentry->d_name);
73 inode = nilfs_iget(dir->i_sb, NILFS_I(dir)->i_root, ino);
75 return ERR_CAST(inode);
77 return d_splice_alias(inode, dentry);
80 struct dentry *nilfs_get_parent(struct dentry *child)
84 struct qstr dotdot = {.name = "..", .len = 2};
86 ino = nilfs_inode_by_name(child->d_inode, &dotdot);
88 return ERR_PTR(-ENOENT);
90 inode = nilfs_iget(child->d_inode->i_sb,
91 NILFS_I(child->d_inode)->i_root, ino);
93 return ERR_CAST(inode);
94 return d_obtain_alias(inode);
98 * By the time this is called, we already have created
99 * the directory cache entry for the new file, but it
100 * is so far negative - it has no inode.
102 * If the create succeeds, we fill in the inode information
103 * with d_instantiate().
105 static int nilfs_create(struct inode *dir, struct dentry *dentry, int mode,
106 struct nameidata *nd)
109 struct nilfs_transaction_info ti;
112 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
115 inode = nilfs_new_inode(dir, mode);
116 err = PTR_ERR(inode);
117 if (!IS_ERR(inode)) {
118 inode->i_op = &nilfs_file_inode_operations;
119 inode->i_fop = &nilfs_file_operations;
120 inode->i_mapping->a_ops = &nilfs_aops;
121 nilfs_mark_inode_dirty(inode);
122 err = nilfs_add_nondir(dentry, inode);
125 err = nilfs_transaction_commit(dir->i_sb);
127 nilfs_transaction_abort(dir->i_sb);
133 nilfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
136 struct nilfs_transaction_info ti;
139 if (!new_valid_dev(rdev))
142 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
145 inode = nilfs_new_inode(dir, mode);
146 err = PTR_ERR(inode);
147 if (!IS_ERR(inode)) {
148 init_special_inode(inode, inode->i_mode, rdev);
149 nilfs_mark_inode_dirty(inode);
150 err = nilfs_add_nondir(dentry, inode);
153 err = nilfs_transaction_commit(dir->i_sb);
155 nilfs_transaction_abort(dir->i_sb);
160 static int nilfs_symlink(struct inode *dir, struct dentry *dentry,
163 struct nilfs_transaction_info ti;
164 struct super_block *sb = dir->i_sb;
165 unsigned l = strlen(symname)+1;
169 if (l > sb->s_blocksize)
170 return -ENAMETOOLONG;
172 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
176 inode = nilfs_new_inode(dir, S_IFLNK | S_IRWXUGO);
177 err = PTR_ERR(inode);
182 inode->i_op = &nilfs_symlink_inode_operations;
183 inode->i_mapping->a_ops = &nilfs_aops;
184 err = page_symlink(inode, symname, l);
188 /* mark_inode_dirty(inode); */
189 /* page_symlink() do this */
191 err = nilfs_add_nondir(dentry, inode);
194 err = nilfs_transaction_commit(dir->i_sb);
196 nilfs_transaction_abort(dir->i_sb);
202 nilfs_mark_inode_dirty(inode);
207 static int nilfs_link(struct dentry *old_dentry, struct inode *dir,
208 struct dentry *dentry)
210 struct inode *inode = old_dentry->d_inode;
211 struct nilfs_transaction_info ti;
214 if (inode->i_nlink >= NILFS_LINK_MAX)
217 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
221 inode->i_ctime = CURRENT_TIME;
222 inode_inc_link_count(inode);
223 atomic_inc(&inode->i_count);
225 err = nilfs_add_nondir(dentry, inode);
227 err = nilfs_transaction_commit(dir->i_sb);
229 nilfs_transaction_abort(dir->i_sb);
234 static int nilfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
237 struct nilfs_transaction_info ti;
240 if (dir->i_nlink >= NILFS_LINK_MAX)
243 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
249 inode = nilfs_new_inode(dir, S_IFDIR | mode);
250 err = PTR_ERR(inode);
254 inode->i_op = &nilfs_dir_inode_operations;
255 inode->i_fop = &nilfs_dir_operations;
256 inode->i_mapping->a_ops = &nilfs_aops;
260 err = nilfs_make_empty(inode, dir);
264 err = nilfs_add_link(dentry, inode);
268 nilfs_mark_inode_dirty(inode);
269 d_instantiate(dentry, inode);
272 err = nilfs_transaction_commit(dir->i_sb);
274 nilfs_transaction_abort(dir->i_sb);
281 nilfs_mark_inode_dirty(inode);
285 nilfs_mark_inode_dirty(dir);
289 static int nilfs_do_unlink(struct inode *dir, struct dentry *dentry)
292 struct nilfs_dir_entry *de;
297 de = nilfs_find_entry(dir, &dentry->d_name, &page);
301 inode = dentry->d_inode;
303 if (le64_to_cpu(de->inode) != inode->i_ino)
306 if (!inode->i_nlink) {
307 nilfs_warning(inode->i_sb, __func__,
308 "deleting nonexistent file (%lu), %d\n",
309 inode->i_ino, inode->i_nlink);
312 err = nilfs_delete_entry(de, page);
316 inode->i_ctime = dir->i_ctime;
323 static int nilfs_unlink(struct inode *dir, struct dentry *dentry)
325 struct nilfs_transaction_info ti;
328 err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
332 err = nilfs_do_unlink(dir, dentry);
335 nilfs_mark_inode_dirty(dir);
336 nilfs_mark_inode_dirty(dentry->d_inode);
337 err = nilfs_transaction_commit(dir->i_sb);
339 nilfs_transaction_abort(dir->i_sb);
344 static int nilfs_rmdir(struct inode *dir, struct dentry *dentry)
346 struct inode *inode = dentry->d_inode;
347 struct nilfs_transaction_info ti;
350 err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
355 if (nilfs_empty_dir(inode)) {
356 err = nilfs_do_unlink(dir, dentry);
360 nilfs_mark_inode_dirty(inode);
362 nilfs_mark_inode_dirty(dir);
366 err = nilfs_transaction_commit(dir->i_sb);
368 nilfs_transaction_abort(dir->i_sb);
373 static int nilfs_rename(struct inode *old_dir, struct dentry *old_dentry,
374 struct inode *new_dir, struct dentry *new_dentry)
376 struct inode *old_inode = old_dentry->d_inode;
377 struct inode *new_inode = new_dentry->d_inode;
378 struct page *dir_page = NULL;
379 struct nilfs_dir_entry *dir_de = NULL;
380 struct page *old_page;
381 struct nilfs_dir_entry *old_de;
382 struct nilfs_transaction_info ti;
385 err = nilfs_transaction_begin(old_dir->i_sb, &ti, 1);
390 old_de = nilfs_find_entry(old_dir, &old_dentry->d_name, &old_page);
394 if (S_ISDIR(old_inode->i_mode)) {
396 dir_de = nilfs_dotdot(old_inode, &dir_page);
402 struct page *new_page;
403 struct nilfs_dir_entry *new_de;
406 if (dir_de && !nilfs_empty_dir(new_inode))
410 new_de = nilfs_find_entry(new_dir, &new_dentry->d_name, &new_page);
413 inc_nlink(old_inode);
414 nilfs_set_link(new_dir, new_de, new_page, old_inode);
415 nilfs_mark_inode_dirty(new_dir);
416 new_inode->i_ctime = CURRENT_TIME;
418 drop_nlink(new_inode);
419 drop_nlink(new_inode);
420 nilfs_mark_inode_dirty(new_inode);
424 if (new_dir->i_nlink >= NILFS_LINK_MAX)
427 inc_nlink(old_inode);
428 err = nilfs_add_link(new_dentry, old_inode);
430 drop_nlink(old_inode);
431 nilfs_mark_inode_dirty(old_inode);
436 nilfs_mark_inode_dirty(new_dir);
441 * Like most other Unix systems, set the ctime for inodes on a
444 old_inode->i_ctime = CURRENT_TIME;
446 nilfs_delete_entry(old_de, old_page);
447 drop_nlink(old_inode);
450 nilfs_set_link(old_inode, dir_de, dir_page, new_dir);
453 nilfs_mark_inode_dirty(old_dir);
454 nilfs_mark_inode_dirty(old_inode);
456 err = nilfs_transaction_commit(old_dir->i_sb);
462 page_cache_release(dir_page);
466 page_cache_release(old_page);
468 nilfs_transaction_abort(old_dir->i_sb);
472 const struct inode_operations nilfs_dir_inode_operations = {
473 .create = nilfs_create,
474 .lookup = nilfs_lookup,
476 .unlink = nilfs_unlink,
477 .symlink = nilfs_symlink,
478 .mkdir = nilfs_mkdir,
479 .rmdir = nilfs_rmdir,
480 .mknod = nilfs_mknod,
481 .rename = nilfs_rename,
482 .setattr = nilfs_setattr,
483 .permission = nilfs_permission,
486 const struct inode_operations nilfs_special_inode_operations = {
487 .setattr = nilfs_setattr,
488 .permission = nilfs_permission,
491 const struct inode_operations nilfs_symlink_inode_operations = {
492 .readlink = generic_readlink,
493 .follow_link = page_follow_link_light,
494 .put_link = page_put_link,