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, 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, ino);
92 return ERR_CAST(inode);
93 return d_obtain_alias(inode);
97 * By the time this is called, we already have created
98 * the directory cache entry for the new file, but it
99 * is so far negative - it has no inode.
101 * If the create succeeds, we fill in the inode information
102 * with d_instantiate().
104 static int nilfs_create(struct inode *dir, struct dentry *dentry, int mode,
105 struct nameidata *nd)
108 struct nilfs_transaction_info ti;
111 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
114 inode = nilfs_new_inode(dir, mode);
115 err = PTR_ERR(inode);
116 if (!IS_ERR(inode)) {
117 inode->i_op = &nilfs_file_inode_operations;
118 inode->i_fop = &nilfs_file_operations;
119 inode->i_mapping->a_ops = &nilfs_aops;
120 nilfs_mark_inode_dirty(inode);
121 err = nilfs_add_nondir(dentry, inode);
124 err = nilfs_transaction_commit(dir->i_sb);
126 nilfs_transaction_abort(dir->i_sb);
132 nilfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
135 struct nilfs_transaction_info ti;
138 if (!new_valid_dev(rdev))
141 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
144 inode = nilfs_new_inode(dir, mode);
145 err = PTR_ERR(inode);
146 if (!IS_ERR(inode)) {
147 init_special_inode(inode, inode->i_mode, rdev);
148 nilfs_mark_inode_dirty(inode);
149 err = nilfs_add_nondir(dentry, inode);
152 err = nilfs_transaction_commit(dir->i_sb);
154 nilfs_transaction_abort(dir->i_sb);
159 static int nilfs_symlink(struct inode *dir, struct dentry *dentry,
162 struct nilfs_transaction_info ti;
163 struct super_block *sb = dir->i_sb;
164 unsigned l = strlen(symname)+1;
168 if (l > sb->s_blocksize)
169 return -ENAMETOOLONG;
171 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
175 inode = nilfs_new_inode(dir, S_IFLNK | S_IRWXUGO);
176 err = PTR_ERR(inode);
181 inode->i_op = &nilfs_symlink_inode_operations;
182 inode->i_mapping->a_ops = &nilfs_aops;
183 err = page_symlink(inode, symname, l);
187 /* mark_inode_dirty(inode); */
188 /* page_symlink() do this */
190 err = nilfs_add_nondir(dentry, inode);
193 err = nilfs_transaction_commit(dir->i_sb);
195 nilfs_transaction_abort(dir->i_sb);
201 nilfs_mark_inode_dirty(inode);
206 static int nilfs_link(struct dentry *old_dentry, struct inode *dir,
207 struct dentry *dentry)
209 struct inode *inode = old_dentry->d_inode;
210 struct nilfs_transaction_info ti;
213 if (inode->i_nlink >= NILFS_LINK_MAX)
216 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
220 inode->i_ctime = CURRENT_TIME;
221 inode_inc_link_count(inode);
222 atomic_inc(&inode->i_count);
224 err = nilfs_add_nondir(dentry, inode);
226 err = nilfs_transaction_commit(dir->i_sb);
228 nilfs_transaction_abort(dir->i_sb);
233 static int nilfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
236 struct nilfs_transaction_info ti;
239 if (dir->i_nlink >= NILFS_LINK_MAX)
242 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
248 inode = nilfs_new_inode(dir, S_IFDIR | mode);
249 err = PTR_ERR(inode);
253 inode->i_op = &nilfs_dir_inode_operations;
254 inode->i_fop = &nilfs_dir_operations;
255 inode->i_mapping->a_ops = &nilfs_aops;
259 err = nilfs_make_empty(inode, dir);
263 err = nilfs_add_link(dentry, inode);
267 nilfs_mark_inode_dirty(inode);
268 d_instantiate(dentry, inode);
271 err = nilfs_transaction_commit(dir->i_sb);
273 nilfs_transaction_abort(dir->i_sb);
280 nilfs_mark_inode_dirty(inode);
284 nilfs_mark_inode_dirty(dir);
288 static int nilfs_do_unlink(struct inode *dir, struct dentry *dentry)
291 struct nilfs_dir_entry *de;
296 de = nilfs_find_entry(dir, &dentry->d_name, &page);
300 inode = dentry->d_inode;
302 if (le64_to_cpu(de->inode) != inode->i_ino)
305 if (!inode->i_nlink) {
306 nilfs_warning(inode->i_sb, __func__,
307 "deleting nonexistent file (%lu), %d\n",
308 inode->i_ino, inode->i_nlink);
311 err = nilfs_delete_entry(de, page);
315 inode->i_ctime = dir->i_ctime;
322 static int nilfs_unlink(struct inode *dir, struct dentry *dentry)
324 struct nilfs_transaction_info ti;
327 err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
331 err = nilfs_do_unlink(dir, dentry);
334 nilfs_mark_inode_dirty(dir);
335 nilfs_mark_inode_dirty(dentry->d_inode);
336 err = nilfs_transaction_commit(dir->i_sb);
338 nilfs_transaction_abort(dir->i_sb);
343 static int nilfs_rmdir(struct inode *dir, struct dentry *dentry)
345 struct inode *inode = dentry->d_inode;
346 struct nilfs_transaction_info ti;
349 err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
354 if (nilfs_empty_dir(inode)) {
355 err = nilfs_do_unlink(dir, dentry);
359 nilfs_mark_inode_dirty(inode);
361 nilfs_mark_inode_dirty(dir);
365 err = nilfs_transaction_commit(dir->i_sb);
367 nilfs_transaction_abort(dir->i_sb);
372 static int nilfs_rename(struct inode *old_dir, struct dentry *old_dentry,
373 struct inode *new_dir, struct dentry *new_dentry)
375 struct inode *old_inode = old_dentry->d_inode;
376 struct inode *new_inode = new_dentry->d_inode;
377 struct page *dir_page = NULL;
378 struct nilfs_dir_entry *dir_de = NULL;
379 struct page *old_page;
380 struct nilfs_dir_entry *old_de;
381 struct nilfs_transaction_info ti;
384 err = nilfs_transaction_begin(old_dir->i_sb, &ti, 1);
389 old_de = nilfs_find_entry(old_dir, &old_dentry->d_name, &old_page);
393 if (S_ISDIR(old_inode->i_mode)) {
395 dir_de = nilfs_dotdot(old_inode, &dir_page);
401 struct page *new_page;
402 struct nilfs_dir_entry *new_de;
405 if (dir_de && !nilfs_empty_dir(new_inode))
409 new_de = nilfs_find_entry(new_dir, &new_dentry->d_name, &new_page);
412 inc_nlink(old_inode);
413 nilfs_set_link(new_dir, new_de, new_page, old_inode);
414 nilfs_mark_inode_dirty(new_dir);
415 new_inode->i_ctime = CURRENT_TIME;
417 drop_nlink(new_inode);
418 drop_nlink(new_inode);
419 nilfs_mark_inode_dirty(new_inode);
423 if (new_dir->i_nlink >= NILFS_LINK_MAX)
426 inc_nlink(old_inode);
427 err = nilfs_add_link(new_dentry, old_inode);
429 drop_nlink(old_inode);
430 nilfs_mark_inode_dirty(old_inode);
435 nilfs_mark_inode_dirty(new_dir);
440 * Like most other Unix systems, set the ctime for inodes on a
443 old_inode->i_ctime = CURRENT_TIME;
445 nilfs_delete_entry(old_de, old_page);
446 drop_nlink(old_inode);
449 nilfs_set_link(old_inode, dir_de, dir_page, new_dir);
452 nilfs_mark_inode_dirty(old_dir);
453 nilfs_mark_inode_dirty(old_inode);
455 err = nilfs_transaction_commit(old_dir->i_sb);
461 page_cache_release(dir_page);
465 page_cache_release(old_page);
467 nilfs_transaction_abort(old_dir->i_sb);
471 const struct inode_operations nilfs_dir_inode_operations = {
472 .create = nilfs_create,
473 .lookup = nilfs_lookup,
475 .unlink = nilfs_unlink,
476 .symlink = nilfs_symlink,
477 .mkdir = nilfs_mkdir,
478 .rmdir = nilfs_rmdir,
479 .mknod = nilfs_mknod,
480 .rename = nilfs_rename,
481 .setattr = nilfs_setattr,
482 .permission = nilfs_permission,
485 const struct inode_operations nilfs_special_inode_operations = {
486 .setattr = nilfs_setattr,
487 .permission = nilfs_permission,
490 const struct inode_operations nilfs_symlink_inode_operations = {
491 .readlink = generic_readlink,
492 .follow_link = page_follow_link_light,
493 .put_link = page_put_link,