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
24 * linux/fs/ext2/namei.c
26 * Copyright (C) 1992, 1993, 1994, 1995
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
41 #include <linux/pagemap.h>
45 #define NILFS_FID_SIZE_NON_CONNECTABLE \
46 (offsetof(struct nilfs_fid, parent_gen) / 4)
47 #define NILFS_FID_SIZE_CONNECTABLE (sizeof(struct nilfs_fid) / 4)
49 static inline int nilfs_add_nondir(struct dentry *dentry, struct inode *inode)
51 int err = nilfs_add_link(dentry, inode);
53 d_instantiate(dentry, inode);
54 unlock_new_inode(inode);
57 inode_dec_link_count(inode);
58 unlock_new_inode(inode);
67 static struct dentry *
68 nilfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
73 if (dentry->d_name.len > NILFS_NAME_LEN)
74 return ERR_PTR(-ENAMETOOLONG);
76 ino = nilfs_inode_by_name(dir, &dentry->d_name);
77 inode = ino ? nilfs_iget(dir->i_sb, NILFS_I(dir)->i_root, ino) : NULL;
78 return d_splice_alias(inode, dentry);
82 * By the time this is called, we already have created
83 * the directory cache entry for the new file, but it
84 * is so far negative - it has no inode.
86 * If the create succeeds, we fill in the inode information
87 * with d_instantiate().
89 static int nilfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
93 struct nilfs_transaction_info ti;
96 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
99 inode = nilfs_new_inode(dir, mode);
100 err = PTR_ERR(inode);
101 if (!IS_ERR(inode)) {
102 inode->i_op = &nilfs_file_inode_operations;
103 inode->i_fop = &nilfs_file_operations;
104 inode->i_mapping->a_ops = &nilfs_aops;
105 nilfs_mark_inode_dirty(inode);
106 err = nilfs_add_nondir(dentry, inode);
109 err = nilfs_transaction_commit(dir->i_sb);
111 nilfs_transaction_abort(dir->i_sb);
117 nilfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
120 struct nilfs_transaction_info ti;
123 if (!new_valid_dev(rdev))
126 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
129 inode = nilfs_new_inode(dir, mode);
130 err = PTR_ERR(inode);
131 if (!IS_ERR(inode)) {
132 init_special_inode(inode, inode->i_mode, rdev);
133 nilfs_mark_inode_dirty(inode);
134 err = nilfs_add_nondir(dentry, inode);
137 err = nilfs_transaction_commit(dir->i_sb);
139 nilfs_transaction_abort(dir->i_sb);
144 static int nilfs_symlink(struct inode *dir, struct dentry *dentry,
147 struct nilfs_transaction_info ti;
148 struct super_block *sb = dir->i_sb;
149 unsigned l = strlen(symname)+1;
153 if (l > sb->s_blocksize)
154 return -ENAMETOOLONG;
156 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
160 inode = nilfs_new_inode(dir, S_IFLNK | S_IRWXUGO);
161 err = PTR_ERR(inode);
166 inode->i_op = &nilfs_symlink_inode_operations;
167 inode->i_mapping->a_ops = &nilfs_aops;
168 err = page_symlink(inode, symname, l);
172 /* mark_inode_dirty(inode); */
173 /* page_symlink() do this */
175 err = nilfs_add_nondir(dentry, inode);
178 err = nilfs_transaction_commit(dir->i_sb);
180 nilfs_transaction_abort(dir->i_sb);
186 nilfs_mark_inode_dirty(inode);
187 unlock_new_inode(inode);
192 static int nilfs_link(struct dentry *old_dentry, struct inode *dir,
193 struct dentry *dentry)
195 struct inode *inode = d_inode(old_dentry);
196 struct nilfs_transaction_info ti;
199 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
203 inode->i_ctime = CURRENT_TIME;
204 inode_inc_link_count(inode);
207 err = nilfs_add_link(dentry, inode);
209 d_instantiate(dentry, inode);
210 err = nilfs_transaction_commit(dir->i_sb);
212 inode_dec_link_count(inode);
214 nilfs_transaction_abort(dir->i_sb);
220 static int nilfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
223 struct nilfs_transaction_info ti;
226 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
232 inode = nilfs_new_inode(dir, S_IFDIR | mode);
233 err = PTR_ERR(inode);
237 inode->i_op = &nilfs_dir_inode_operations;
238 inode->i_fop = &nilfs_dir_operations;
239 inode->i_mapping->a_ops = &nilfs_aops;
243 err = nilfs_make_empty(inode, dir);
247 err = nilfs_add_link(dentry, inode);
251 nilfs_mark_inode_dirty(inode);
252 d_instantiate(dentry, inode);
253 unlock_new_inode(inode);
256 err = nilfs_transaction_commit(dir->i_sb);
258 nilfs_transaction_abort(dir->i_sb);
265 nilfs_mark_inode_dirty(inode);
266 unlock_new_inode(inode);
270 nilfs_mark_inode_dirty(dir);
274 static int nilfs_do_unlink(struct inode *dir, struct dentry *dentry)
277 struct nilfs_dir_entry *de;
282 de = nilfs_find_entry(dir, &dentry->d_name, &page);
286 inode = d_inode(dentry);
288 if (le64_to_cpu(de->inode) != inode->i_ino)
291 if (!inode->i_nlink) {
292 nilfs_warning(inode->i_sb, __func__,
293 "deleting nonexistent file (%lu), %d\n",
294 inode->i_ino, inode->i_nlink);
297 err = nilfs_delete_entry(de, page);
301 inode->i_ctime = dir->i_ctime;
308 static int nilfs_unlink(struct inode *dir, struct dentry *dentry)
310 struct nilfs_transaction_info ti;
313 err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
317 err = nilfs_do_unlink(dir, dentry);
320 nilfs_mark_inode_dirty(dir);
321 nilfs_mark_inode_dirty(d_inode(dentry));
322 err = nilfs_transaction_commit(dir->i_sb);
324 nilfs_transaction_abort(dir->i_sb);
329 static int nilfs_rmdir(struct inode *dir, struct dentry *dentry)
331 struct inode *inode = d_inode(dentry);
332 struct nilfs_transaction_info ti;
335 err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
340 if (nilfs_empty_dir(inode)) {
341 err = nilfs_do_unlink(dir, dentry);
345 nilfs_mark_inode_dirty(inode);
347 nilfs_mark_inode_dirty(dir);
351 err = nilfs_transaction_commit(dir->i_sb);
353 nilfs_transaction_abort(dir->i_sb);
358 static int nilfs_rename(struct inode *old_dir, struct dentry *old_dentry,
359 struct inode *new_dir, struct dentry *new_dentry)
361 struct inode *old_inode = d_inode(old_dentry);
362 struct inode *new_inode = d_inode(new_dentry);
363 struct page *dir_page = NULL;
364 struct nilfs_dir_entry *dir_de = NULL;
365 struct page *old_page;
366 struct nilfs_dir_entry *old_de;
367 struct nilfs_transaction_info ti;
370 err = nilfs_transaction_begin(old_dir->i_sb, &ti, 1);
375 old_de = nilfs_find_entry(old_dir, &old_dentry->d_name, &old_page);
379 if (S_ISDIR(old_inode->i_mode)) {
381 dir_de = nilfs_dotdot(old_inode, &dir_page);
387 struct page *new_page;
388 struct nilfs_dir_entry *new_de;
391 if (dir_de && !nilfs_empty_dir(new_inode))
395 new_de = nilfs_find_entry(new_dir, &new_dentry->d_name, &new_page);
398 nilfs_set_link(new_dir, new_de, new_page, old_inode);
399 nilfs_mark_inode_dirty(new_dir);
400 new_inode->i_ctime = CURRENT_TIME;
402 drop_nlink(new_inode);
403 drop_nlink(new_inode);
404 nilfs_mark_inode_dirty(new_inode);
406 err = nilfs_add_link(new_dentry, old_inode);
411 nilfs_mark_inode_dirty(new_dir);
416 * Like most other Unix systems, set the ctime for inodes on a
419 old_inode->i_ctime = CURRENT_TIME;
421 nilfs_delete_entry(old_de, old_page);
424 nilfs_set_link(old_inode, dir_de, dir_page, new_dir);
427 nilfs_mark_inode_dirty(old_dir);
428 nilfs_mark_inode_dirty(old_inode);
430 err = nilfs_transaction_commit(old_dir->i_sb);
436 page_cache_release(dir_page);
440 page_cache_release(old_page);
442 nilfs_transaction_abort(old_dir->i_sb);
449 static struct dentry *nilfs_get_parent(struct dentry *child)
453 struct qstr dotdot = QSTR_INIT("..", 2);
454 struct nilfs_root *root;
456 ino = nilfs_inode_by_name(d_inode(child), &dotdot);
458 return ERR_PTR(-ENOENT);
460 root = NILFS_I(d_inode(child))->i_root;
462 inode = nilfs_iget(d_inode(child)->i_sb, root, ino);
464 return ERR_CAST(inode);
466 return d_obtain_alias(inode);
469 static struct dentry *nilfs_get_dentry(struct super_block *sb, u64 cno,
472 struct nilfs_root *root;
475 if (ino < NILFS_FIRST_INO(sb) && ino != NILFS_ROOT_INO)
476 return ERR_PTR(-ESTALE);
478 root = nilfs_lookup_root(sb->s_fs_info, cno);
480 return ERR_PTR(-ESTALE);
482 inode = nilfs_iget(sb, root, ino);
483 nilfs_put_root(root);
486 return ERR_CAST(inode);
487 if (gen && inode->i_generation != gen) {
489 return ERR_PTR(-ESTALE);
491 return d_obtain_alias(inode);
494 static struct dentry *nilfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
495 int fh_len, int fh_type)
497 struct nilfs_fid *fid = (struct nilfs_fid *)fh;
499 if (fh_len < NILFS_FID_SIZE_NON_CONNECTABLE ||
500 (fh_type != FILEID_NILFS_WITH_PARENT &&
501 fh_type != FILEID_NILFS_WITHOUT_PARENT))
504 return nilfs_get_dentry(sb, fid->cno, fid->ino, fid->gen);
507 static struct dentry *nilfs_fh_to_parent(struct super_block *sb, struct fid *fh,
508 int fh_len, int fh_type)
510 struct nilfs_fid *fid = (struct nilfs_fid *)fh;
512 if (fh_len < NILFS_FID_SIZE_CONNECTABLE ||
513 fh_type != FILEID_NILFS_WITH_PARENT)
516 return nilfs_get_dentry(sb, fid->cno, fid->parent_ino, fid->parent_gen);
519 static int nilfs_encode_fh(struct inode *inode, __u32 *fh, int *lenp,
520 struct inode *parent)
522 struct nilfs_fid *fid = (struct nilfs_fid *)fh;
523 struct nilfs_root *root = NILFS_I(inode)->i_root;
526 if (parent && *lenp < NILFS_FID_SIZE_CONNECTABLE) {
527 *lenp = NILFS_FID_SIZE_CONNECTABLE;
528 return FILEID_INVALID;
530 if (*lenp < NILFS_FID_SIZE_NON_CONNECTABLE) {
531 *lenp = NILFS_FID_SIZE_NON_CONNECTABLE;
532 return FILEID_INVALID;
535 fid->cno = root->cno;
536 fid->ino = inode->i_ino;
537 fid->gen = inode->i_generation;
540 fid->parent_ino = parent->i_ino;
541 fid->parent_gen = parent->i_generation;
542 type = FILEID_NILFS_WITH_PARENT;
543 *lenp = NILFS_FID_SIZE_CONNECTABLE;
545 type = FILEID_NILFS_WITHOUT_PARENT;
546 *lenp = NILFS_FID_SIZE_NON_CONNECTABLE;
552 const struct inode_operations nilfs_dir_inode_operations = {
553 .create = nilfs_create,
554 .lookup = nilfs_lookup,
556 .unlink = nilfs_unlink,
557 .symlink = nilfs_symlink,
558 .mkdir = nilfs_mkdir,
559 .rmdir = nilfs_rmdir,
560 .mknod = nilfs_mknod,
561 .rename = nilfs_rename,
562 .setattr = nilfs_setattr,
563 .permission = nilfs_permission,
564 .fiemap = nilfs_fiemap,
567 const struct inode_operations nilfs_special_inode_operations = {
568 .setattr = nilfs_setattr,
569 .permission = nilfs_permission,
572 const struct inode_operations nilfs_symlink_inode_operations = {
573 .readlink = generic_readlink,
574 .follow_link = page_follow_link_light,
575 .put_link = page_put_link,
576 .permission = nilfs_permission,
579 const struct export_operations nilfs_export_ops = {
580 .encode_fh = nilfs_encode_fh,
581 .fh_to_dentry = nilfs_fh_to_dentry,
582 .fh_to_parent = nilfs_fh_to_parent,
583 .get_parent = nilfs_get_parent,