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 #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
);
56 inode_dec_link_count(inode
);
65 static struct dentry
*
66 nilfs_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
71 if (dentry
->d_name
.len
> NILFS_NAME_LEN
)
72 return ERR_PTR(-ENAMETOOLONG
);
74 ino
= nilfs_inode_by_name(dir
, &dentry
->d_name
);
77 inode
= nilfs_iget(dir
->i_sb
, NILFS_I(dir
)->i_root
, ino
);
79 return ERR_CAST(inode
);
81 return d_splice_alias(inode
, dentry
);
85 * By the time this is called, we already have created
86 * the directory cache entry for the new file, but it
87 * is so far negative - it has no inode.
89 * If the create succeeds, we fill in the inode information
90 * with d_instantiate().
92 static int nilfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
96 struct nilfs_transaction_info ti
;
99 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
102 inode
= nilfs_new_inode(dir
, mode
);
103 err
= PTR_ERR(inode
);
104 if (!IS_ERR(inode
)) {
105 inode
->i_op
= &nilfs_file_inode_operations
;
106 inode
->i_fop
= &nilfs_file_operations
;
107 inode
->i_mapping
->a_ops
= &nilfs_aops
;
108 nilfs_mark_inode_dirty(inode
);
109 err
= nilfs_add_nondir(dentry
, inode
);
112 err
= nilfs_transaction_commit(dir
->i_sb
);
114 nilfs_transaction_abort(dir
->i_sb
);
120 nilfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t rdev
)
123 struct nilfs_transaction_info ti
;
126 if (!new_valid_dev(rdev
))
129 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
132 inode
= nilfs_new_inode(dir
, mode
);
133 err
= PTR_ERR(inode
);
134 if (!IS_ERR(inode
)) {
135 init_special_inode(inode
, inode
->i_mode
, rdev
);
136 nilfs_mark_inode_dirty(inode
);
137 err
= nilfs_add_nondir(dentry
, inode
);
140 err
= nilfs_transaction_commit(dir
->i_sb
);
142 nilfs_transaction_abort(dir
->i_sb
);
147 static int nilfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
150 struct nilfs_transaction_info ti
;
151 struct super_block
*sb
= dir
->i_sb
;
152 unsigned l
= strlen(symname
)+1;
156 if (l
> sb
->s_blocksize
)
157 return -ENAMETOOLONG
;
159 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
163 inode
= nilfs_new_inode(dir
, S_IFLNK
| S_IRWXUGO
);
164 err
= PTR_ERR(inode
);
169 inode
->i_op
= &nilfs_symlink_inode_operations
;
170 inode
->i_mapping
->a_ops
= &nilfs_aops
;
171 err
= page_symlink(inode
, symname
, l
);
175 /* mark_inode_dirty(inode); */
176 /* page_symlink() do this */
178 err
= nilfs_add_nondir(dentry
, inode
);
181 err
= nilfs_transaction_commit(dir
->i_sb
);
183 nilfs_transaction_abort(dir
->i_sb
);
189 nilfs_mark_inode_dirty(inode
);
194 static int nilfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
195 struct dentry
*dentry
)
197 struct inode
*inode
= old_dentry
->d_inode
;
198 struct nilfs_transaction_info ti
;
201 if (inode
->i_nlink
>= NILFS_LINK_MAX
)
204 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
208 inode
->i_ctime
= CURRENT_TIME
;
209 inode_inc_link_count(inode
);
212 err
= nilfs_add_nondir(dentry
, inode
);
214 err
= nilfs_transaction_commit(dir
->i_sb
);
216 nilfs_transaction_abort(dir
->i_sb
);
221 static int nilfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
224 struct nilfs_transaction_info ti
;
227 if (dir
->i_nlink
>= NILFS_LINK_MAX
)
230 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
236 inode
= nilfs_new_inode(dir
, S_IFDIR
| mode
);
237 err
= PTR_ERR(inode
);
241 inode
->i_op
= &nilfs_dir_inode_operations
;
242 inode
->i_fop
= &nilfs_dir_operations
;
243 inode
->i_mapping
->a_ops
= &nilfs_aops
;
247 err
= nilfs_make_empty(inode
, dir
);
251 err
= nilfs_add_link(dentry
, inode
);
255 nilfs_mark_inode_dirty(inode
);
256 d_instantiate(dentry
, inode
);
259 err
= nilfs_transaction_commit(dir
->i_sb
);
261 nilfs_transaction_abort(dir
->i_sb
);
268 nilfs_mark_inode_dirty(inode
);
272 nilfs_mark_inode_dirty(dir
);
276 static int nilfs_do_unlink(struct inode
*dir
, struct dentry
*dentry
)
279 struct nilfs_dir_entry
*de
;
284 de
= nilfs_find_entry(dir
, &dentry
->d_name
, &page
);
288 inode
= dentry
->d_inode
;
290 if (le64_to_cpu(de
->inode
) != inode
->i_ino
)
293 if (!inode
->i_nlink
) {
294 nilfs_warning(inode
->i_sb
, __func__
,
295 "deleting nonexistent file (%lu), %d\n",
296 inode
->i_ino
, inode
->i_nlink
);
299 err
= nilfs_delete_entry(de
, page
);
303 inode
->i_ctime
= dir
->i_ctime
;
310 static int nilfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
312 struct nilfs_transaction_info ti
;
315 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 0);
319 err
= nilfs_do_unlink(dir
, dentry
);
322 nilfs_mark_inode_dirty(dir
);
323 nilfs_mark_inode_dirty(dentry
->d_inode
);
324 err
= nilfs_transaction_commit(dir
->i_sb
);
326 nilfs_transaction_abort(dir
->i_sb
);
331 static int nilfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
333 struct inode
*inode
= dentry
->d_inode
;
334 struct nilfs_transaction_info ti
;
337 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 0);
342 if (nilfs_empty_dir(inode
)) {
343 err
= nilfs_do_unlink(dir
, dentry
);
347 nilfs_mark_inode_dirty(inode
);
349 nilfs_mark_inode_dirty(dir
);
353 err
= nilfs_transaction_commit(dir
->i_sb
);
355 nilfs_transaction_abort(dir
->i_sb
);
360 static int nilfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
361 struct inode
*new_dir
, struct dentry
*new_dentry
)
363 struct inode
*old_inode
= old_dentry
->d_inode
;
364 struct inode
*new_inode
= new_dentry
->d_inode
;
365 struct page
*dir_page
= NULL
;
366 struct nilfs_dir_entry
*dir_de
= NULL
;
367 struct page
*old_page
;
368 struct nilfs_dir_entry
*old_de
;
369 struct nilfs_transaction_info ti
;
372 err
= nilfs_transaction_begin(old_dir
->i_sb
, &ti
, 1);
377 old_de
= nilfs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
381 if (S_ISDIR(old_inode
->i_mode
)) {
383 dir_de
= nilfs_dotdot(old_inode
, &dir_page
);
389 struct page
*new_page
;
390 struct nilfs_dir_entry
*new_de
;
393 if (dir_de
&& !nilfs_empty_dir(new_inode
))
397 new_de
= nilfs_find_entry(new_dir
, &new_dentry
->d_name
, &new_page
);
400 inc_nlink(old_inode
);
401 nilfs_set_link(new_dir
, new_de
, new_page
, old_inode
);
402 nilfs_mark_inode_dirty(new_dir
);
403 new_inode
->i_ctime
= CURRENT_TIME
;
405 drop_nlink(new_inode
);
406 drop_nlink(new_inode
);
407 nilfs_mark_inode_dirty(new_inode
);
411 if (new_dir
->i_nlink
>= NILFS_LINK_MAX
)
414 inc_nlink(old_inode
);
415 err
= nilfs_add_link(new_dentry
, old_inode
);
417 drop_nlink(old_inode
);
418 nilfs_mark_inode_dirty(old_inode
);
423 nilfs_mark_inode_dirty(new_dir
);
428 * Like most other Unix systems, set the ctime for inodes on a
431 old_inode
->i_ctime
= CURRENT_TIME
;
433 nilfs_delete_entry(old_de
, old_page
);
434 drop_nlink(old_inode
);
437 nilfs_set_link(old_inode
, dir_de
, dir_page
, new_dir
);
440 nilfs_mark_inode_dirty(old_dir
);
441 nilfs_mark_inode_dirty(old_inode
);
443 err
= nilfs_transaction_commit(old_dir
->i_sb
);
449 page_cache_release(dir_page
);
453 page_cache_release(old_page
);
455 nilfs_transaction_abort(old_dir
->i_sb
);
462 static struct dentry
*nilfs_get_parent(struct dentry
*child
)
466 struct qstr dotdot
= {.name
= "..", .len
= 2};
467 struct nilfs_root
*root
;
469 ino
= nilfs_inode_by_name(child
->d_inode
, &dotdot
);
471 return ERR_PTR(-ENOENT
);
473 root
= NILFS_I(child
->d_inode
)->i_root
;
475 inode
= nilfs_iget(child
->d_inode
->i_sb
, root
, ino
);
477 return ERR_CAST(inode
);
479 return d_obtain_alias(inode
);
482 static struct dentry
*nilfs_get_dentry(struct super_block
*sb
, u64 cno
,
485 struct nilfs_root
*root
;
488 if (ino
< NILFS_FIRST_INO(sb
) && ino
!= NILFS_ROOT_INO
)
489 return ERR_PTR(-ESTALE
);
491 root
= nilfs_lookup_root(NILFS_SB(sb
)->s_nilfs
, cno
);
493 return ERR_PTR(-ESTALE
);
495 inode
= nilfs_iget(sb
, root
, ino
);
496 nilfs_put_root(root
);
499 return ERR_CAST(inode
);
500 if (gen
&& inode
->i_generation
!= gen
) {
502 return ERR_PTR(-ESTALE
);
504 return d_obtain_alias(inode
);
507 static struct dentry
*nilfs_fh_to_dentry(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_NON_CONNECTABLE
&&
513 fh_len
!= NILFS_FID_SIZE_CONNECTABLE
) ||
514 (fh_type
!= FILEID_NILFS_WITH_PARENT
&&
515 fh_type
!= FILEID_NILFS_WITHOUT_PARENT
))
518 return nilfs_get_dentry(sb
, fid
->cno
, fid
->ino
, fid
->gen
);
521 static struct dentry
*nilfs_fh_to_parent(struct super_block
*sb
, struct fid
*fh
,
522 int fh_len
, int fh_type
)
524 struct nilfs_fid
*fid
= (struct nilfs_fid
*)fh
;
526 if (fh_len
!= NILFS_FID_SIZE_CONNECTABLE
||
527 fh_type
!= FILEID_NILFS_WITH_PARENT
)
530 return nilfs_get_dentry(sb
, fid
->cno
, fid
->parent_ino
, fid
->parent_gen
);
533 static int nilfs_encode_fh(struct dentry
*dentry
, __u32
*fh
, int *lenp
,
536 struct nilfs_fid
*fid
= (struct nilfs_fid
*)fh
;
537 struct inode
*inode
= dentry
->d_inode
;
538 struct nilfs_root
*root
= NILFS_I(inode
)->i_root
;
541 if (*lenp
< NILFS_FID_SIZE_NON_CONNECTABLE
||
542 (connectable
&& *lenp
< NILFS_FID_SIZE_CONNECTABLE
))
545 fid
->cno
= root
->cno
;
546 fid
->ino
= inode
->i_ino
;
547 fid
->gen
= inode
->i_generation
;
549 if (connectable
&& !S_ISDIR(inode
->i_mode
)) {
550 struct inode
*parent
;
552 spin_lock(&dentry
->d_lock
);
553 parent
= dentry
->d_parent
->d_inode
;
554 fid
->parent_ino
= parent
->i_ino
;
555 fid
->parent_gen
= parent
->i_generation
;
556 spin_unlock(&dentry
->d_lock
);
558 type
= FILEID_NILFS_WITH_PARENT
;
559 *lenp
= NILFS_FID_SIZE_CONNECTABLE
;
561 type
= FILEID_NILFS_WITHOUT_PARENT
;
562 *lenp
= NILFS_FID_SIZE_NON_CONNECTABLE
;
568 const struct inode_operations nilfs_dir_inode_operations
= {
569 .create
= nilfs_create
,
570 .lookup
= nilfs_lookup
,
572 .unlink
= nilfs_unlink
,
573 .symlink
= nilfs_symlink
,
574 .mkdir
= nilfs_mkdir
,
575 .rmdir
= nilfs_rmdir
,
576 .mknod
= nilfs_mknod
,
577 .rename
= nilfs_rename
,
578 .setattr
= nilfs_setattr
,
579 .permission
= nilfs_permission
,
580 .fiemap
= nilfs_fiemap
,
583 const struct inode_operations nilfs_special_inode_operations
= {
584 .setattr
= nilfs_setattr
,
585 .permission
= nilfs_permission
,
588 const struct inode_operations nilfs_symlink_inode_operations
= {
589 .readlink
= generic_readlink
,
590 .follow_link
= page_follow_link_light
,
591 .put_link
= page_put_link
,
592 .permission
= nilfs_permission
,
595 const struct export_operations nilfs_export_ops
= {
596 .encode_fh
= nilfs_encode_fh
,
597 .fh_to_dentry
= nilfs_fh_to_dentry
,
598 .fh_to_parent
= nilfs_fh_to_parent
,
599 .get_parent
= nilfs_get_parent
,