1 // SPDX-License-Identifier: GPL-2.0+
3 * namei.c - NILFS pathname lookup operations.
5 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
7 * Modified for NILFS by Amagai Yoshiji and Ryusuke Konishi.
10 * linux/fs/ext2/namei.c
12 * Copyright (C) 1992, 1993, 1994, 1995
13 * Remy Card (card@masi.ibp.fr)
14 * Laboratoire MASI - Institut Blaise Pascal
15 * Universite Pierre et Marie Curie (Paris VI)
19 * linux/fs/minix/namei.c
21 * Copyright (C) 1991, 1992 Linus Torvalds
23 * Big-endian to little-endian byte-swapping/bitmaps by
24 * David S. Miller (davem@caip.rutgers.edu), 1995
27 #include <linux/pagemap.h>
31 #define NILFS_FID_SIZE_NON_CONNECTABLE \
32 (offsetof(struct nilfs_fid, parent_gen) / 4)
33 #define NILFS_FID_SIZE_CONNECTABLE (sizeof(struct nilfs_fid) / 4)
35 static inline int nilfs_add_nondir(struct dentry
*dentry
, struct inode
*inode
)
37 int err
= nilfs_add_link(dentry
, inode
);
40 d_instantiate_new(dentry
, inode
);
43 inode_dec_link_count(inode
);
44 unlock_new_inode(inode
);
53 static struct dentry
*
54 nilfs_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
59 if (dentry
->d_name
.len
> NILFS_NAME_LEN
)
60 return ERR_PTR(-ENAMETOOLONG
);
62 ino
= nilfs_inode_by_name(dir
, &dentry
->d_name
);
63 inode
= ino
? nilfs_iget(dir
->i_sb
, NILFS_I(dir
)->i_root
, ino
) : NULL
;
64 return d_splice_alias(inode
, dentry
);
68 * By the time this is called, we already have created
69 * the directory cache entry for the new file, but it
70 * is so far negative - it has no inode.
72 * If the create succeeds, we fill in the inode information
73 * with d_instantiate().
75 static int nilfs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
79 struct nilfs_transaction_info ti
;
82 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
85 inode
= nilfs_new_inode(dir
, mode
);
88 inode
->i_op
= &nilfs_file_inode_operations
;
89 inode
->i_fop
= &nilfs_file_operations
;
90 inode
->i_mapping
->a_ops
= &nilfs_aops
;
91 nilfs_mark_inode_dirty(inode
);
92 err
= nilfs_add_nondir(dentry
, inode
);
95 err
= nilfs_transaction_commit(dir
->i_sb
);
97 nilfs_transaction_abort(dir
->i_sb
);
103 nilfs_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
, dev_t rdev
)
106 struct nilfs_transaction_info ti
;
109 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
112 inode
= nilfs_new_inode(dir
, mode
);
113 err
= PTR_ERR(inode
);
114 if (!IS_ERR(inode
)) {
115 init_special_inode(inode
, inode
->i_mode
, rdev
);
116 nilfs_mark_inode_dirty(inode
);
117 err
= nilfs_add_nondir(dentry
, inode
);
120 err
= nilfs_transaction_commit(dir
->i_sb
);
122 nilfs_transaction_abort(dir
->i_sb
);
127 static int nilfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
130 struct nilfs_transaction_info ti
;
131 struct super_block
*sb
= dir
->i_sb
;
132 unsigned int l
= strlen(symname
) + 1;
136 if (l
> sb
->s_blocksize
)
137 return -ENAMETOOLONG
;
139 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
143 inode
= nilfs_new_inode(dir
, S_IFLNK
| 0777);
144 err
= PTR_ERR(inode
);
149 inode
->i_op
= &nilfs_symlink_inode_operations
;
150 inode_nohighmem(inode
);
151 inode
->i_mapping
->a_ops
= &nilfs_aops
;
152 err
= page_symlink(inode
, symname
, l
);
156 /* mark_inode_dirty(inode); */
157 /* page_symlink() do this */
159 err
= nilfs_add_nondir(dentry
, inode
);
162 err
= nilfs_transaction_commit(dir
->i_sb
);
164 nilfs_transaction_abort(dir
->i_sb
);
170 nilfs_mark_inode_dirty(inode
);
171 unlock_new_inode(inode
);
176 static int nilfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
177 struct dentry
*dentry
)
179 struct inode
*inode
= d_inode(old_dentry
);
180 struct nilfs_transaction_info ti
;
183 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
187 inode
->i_ctime
= current_time(inode
);
188 inode_inc_link_count(inode
);
191 err
= nilfs_add_link(dentry
, inode
);
193 d_instantiate(dentry
, inode
);
194 err
= nilfs_transaction_commit(dir
->i_sb
);
196 inode_dec_link_count(inode
);
198 nilfs_transaction_abort(dir
->i_sb
);
204 static int nilfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
207 struct nilfs_transaction_info ti
;
210 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
216 inode
= nilfs_new_inode(dir
, S_IFDIR
| mode
);
217 err
= PTR_ERR(inode
);
221 inode
->i_op
= &nilfs_dir_inode_operations
;
222 inode
->i_fop
= &nilfs_dir_operations
;
223 inode
->i_mapping
->a_ops
= &nilfs_aops
;
227 err
= nilfs_make_empty(inode
, dir
);
231 err
= nilfs_add_link(dentry
, inode
);
235 nilfs_mark_inode_dirty(inode
);
236 d_instantiate_new(dentry
, inode
);
239 err
= nilfs_transaction_commit(dir
->i_sb
);
241 nilfs_transaction_abort(dir
->i_sb
);
248 nilfs_mark_inode_dirty(inode
);
249 unlock_new_inode(inode
);
253 nilfs_mark_inode_dirty(dir
);
257 static int nilfs_do_unlink(struct inode
*dir
, struct dentry
*dentry
)
260 struct nilfs_dir_entry
*de
;
265 de
= nilfs_find_entry(dir
, &dentry
->d_name
, &page
);
269 inode
= d_inode(dentry
);
271 if (le64_to_cpu(de
->inode
) != inode
->i_ino
)
274 if (!inode
->i_nlink
) {
275 nilfs_msg(inode
->i_sb
, KERN_WARNING
,
276 "deleting nonexistent file (ino=%lu), %d",
277 inode
->i_ino
, inode
->i_nlink
);
280 err
= nilfs_delete_entry(de
, page
);
284 inode
->i_ctime
= dir
->i_ctime
;
291 static int nilfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
293 struct nilfs_transaction_info ti
;
296 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 0);
300 err
= nilfs_do_unlink(dir
, dentry
);
303 nilfs_mark_inode_dirty(dir
);
304 nilfs_mark_inode_dirty(d_inode(dentry
));
305 err
= nilfs_transaction_commit(dir
->i_sb
);
307 nilfs_transaction_abort(dir
->i_sb
);
312 static int nilfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
314 struct inode
*inode
= d_inode(dentry
);
315 struct nilfs_transaction_info ti
;
318 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 0);
323 if (nilfs_empty_dir(inode
)) {
324 err
= nilfs_do_unlink(dir
, dentry
);
328 nilfs_mark_inode_dirty(inode
);
330 nilfs_mark_inode_dirty(dir
);
334 err
= nilfs_transaction_commit(dir
->i_sb
);
336 nilfs_transaction_abort(dir
->i_sb
);
341 static int nilfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
342 struct inode
*new_dir
, struct dentry
*new_dentry
,
345 struct inode
*old_inode
= d_inode(old_dentry
);
346 struct inode
*new_inode
= d_inode(new_dentry
);
347 struct page
*dir_page
= NULL
;
348 struct nilfs_dir_entry
*dir_de
= NULL
;
349 struct page
*old_page
;
350 struct nilfs_dir_entry
*old_de
;
351 struct nilfs_transaction_info ti
;
354 if (flags
& ~RENAME_NOREPLACE
)
357 err
= nilfs_transaction_begin(old_dir
->i_sb
, &ti
, 1);
362 old_de
= nilfs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
366 if (S_ISDIR(old_inode
->i_mode
)) {
368 dir_de
= nilfs_dotdot(old_inode
, &dir_page
);
374 struct page
*new_page
;
375 struct nilfs_dir_entry
*new_de
;
378 if (dir_de
&& !nilfs_empty_dir(new_inode
))
382 new_de
= nilfs_find_entry(new_dir
, &new_dentry
->d_name
, &new_page
);
385 nilfs_set_link(new_dir
, new_de
, new_page
, old_inode
);
386 nilfs_mark_inode_dirty(new_dir
);
387 new_inode
->i_ctime
= current_time(new_inode
);
389 drop_nlink(new_inode
);
390 drop_nlink(new_inode
);
391 nilfs_mark_inode_dirty(new_inode
);
393 err
= nilfs_add_link(new_dentry
, old_inode
);
398 nilfs_mark_inode_dirty(new_dir
);
403 * Like most other Unix systems, set the ctime for inodes on a
406 old_inode
->i_ctime
= current_time(old_inode
);
408 nilfs_delete_entry(old_de
, old_page
);
411 nilfs_set_link(old_inode
, dir_de
, dir_page
, new_dir
);
414 nilfs_mark_inode_dirty(old_dir
);
415 nilfs_mark_inode_dirty(old_inode
);
417 err
= nilfs_transaction_commit(old_dir
->i_sb
);
429 nilfs_transaction_abort(old_dir
->i_sb
);
436 static struct dentry
*nilfs_get_parent(struct dentry
*child
)
440 struct qstr dotdot
= QSTR_INIT("..", 2);
441 struct nilfs_root
*root
;
443 ino
= nilfs_inode_by_name(d_inode(child
), &dotdot
);
445 return ERR_PTR(-ENOENT
);
447 root
= NILFS_I(d_inode(child
))->i_root
;
449 inode
= nilfs_iget(child
->d_sb
, root
, ino
);
451 return ERR_CAST(inode
);
453 return d_obtain_alias(inode
);
456 static struct dentry
*nilfs_get_dentry(struct super_block
*sb
, u64 cno
,
459 struct nilfs_root
*root
;
462 if (ino
< NILFS_FIRST_INO(sb
) && ino
!= NILFS_ROOT_INO
)
463 return ERR_PTR(-ESTALE
);
465 root
= nilfs_lookup_root(sb
->s_fs_info
, cno
);
467 return ERR_PTR(-ESTALE
);
469 inode
= nilfs_iget(sb
, root
, ino
);
470 nilfs_put_root(root
);
473 return ERR_CAST(inode
);
474 if (gen
&& inode
->i_generation
!= gen
) {
476 return ERR_PTR(-ESTALE
);
478 return d_obtain_alias(inode
);
481 static struct dentry
*nilfs_fh_to_dentry(struct super_block
*sb
, struct fid
*fh
,
482 int fh_len
, int fh_type
)
484 struct nilfs_fid
*fid
= (struct nilfs_fid
*)fh
;
486 if (fh_len
< NILFS_FID_SIZE_NON_CONNECTABLE
||
487 (fh_type
!= FILEID_NILFS_WITH_PARENT
&&
488 fh_type
!= FILEID_NILFS_WITHOUT_PARENT
))
491 return nilfs_get_dentry(sb
, fid
->cno
, fid
->ino
, fid
->gen
);
494 static struct dentry
*nilfs_fh_to_parent(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_CONNECTABLE
||
500 fh_type
!= FILEID_NILFS_WITH_PARENT
)
503 return nilfs_get_dentry(sb
, fid
->cno
, fid
->parent_ino
, fid
->parent_gen
);
506 static int nilfs_encode_fh(struct inode
*inode
, __u32
*fh
, int *lenp
,
507 struct inode
*parent
)
509 struct nilfs_fid
*fid
= (struct nilfs_fid
*)fh
;
510 struct nilfs_root
*root
= NILFS_I(inode
)->i_root
;
513 if (parent
&& *lenp
< NILFS_FID_SIZE_CONNECTABLE
) {
514 *lenp
= NILFS_FID_SIZE_CONNECTABLE
;
515 return FILEID_INVALID
;
517 if (*lenp
< NILFS_FID_SIZE_NON_CONNECTABLE
) {
518 *lenp
= NILFS_FID_SIZE_NON_CONNECTABLE
;
519 return FILEID_INVALID
;
522 fid
->cno
= root
->cno
;
523 fid
->ino
= inode
->i_ino
;
524 fid
->gen
= inode
->i_generation
;
527 fid
->parent_ino
= parent
->i_ino
;
528 fid
->parent_gen
= parent
->i_generation
;
529 type
= FILEID_NILFS_WITH_PARENT
;
530 *lenp
= NILFS_FID_SIZE_CONNECTABLE
;
532 type
= FILEID_NILFS_WITHOUT_PARENT
;
533 *lenp
= NILFS_FID_SIZE_NON_CONNECTABLE
;
539 const struct inode_operations nilfs_dir_inode_operations
= {
540 .create
= nilfs_create
,
541 .lookup
= nilfs_lookup
,
543 .unlink
= nilfs_unlink
,
544 .symlink
= nilfs_symlink
,
545 .mkdir
= nilfs_mkdir
,
546 .rmdir
= nilfs_rmdir
,
547 .mknod
= nilfs_mknod
,
548 .rename
= nilfs_rename
,
549 .setattr
= nilfs_setattr
,
550 .permission
= nilfs_permission
,
551 .fiemap
= nilfs_fiemap
,
554 const struct inode_operations nilfs_special_inode_operations
= {
555 .setattr
= nilfs_setattr
,
556 .permission
= nilfs_permission
,
559 const struct inode_operations nilfs_symlink_inode_operations
= {
560 .get_link
= page_get_link
,
561 .permission
= nilfs_permission
,
564 const struct export_operations nilfs_export_ops
= {
565 .encode_fh
= nilfs_encode_fh
,
566 .fh_to_dentry
= nilfs_fh_to_dentry
,
567 .fh_to_parent
= nilfs_fh_to_parent
,
568 .get_parent
= nilfs_get_parent
,