1 // SPDX-License-Identifier: GPL-2.0+
3 * 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
)
60 if (dentry
->d_name
.len
> NILFS_NAME_LEN
)
61 return ERR_PTR(-ENAMETOOLONG
);
63 res
= nilfs_inode_by_name(dir
, &dentry
->d_name
, &ino
);
69 inode
= nilfs_iget(dir
->i_sb
, NILFS_I(dir
)->i_root
, ino
);
72 return d_splice_alias(inode
, dentry
);
76 * By the time this is called, we already have created
77 * the directory cache entry for the new file, but it
78 * is so far negative - it has no inode.
80 * If the create succeeds, we fill in the inode information
81 * with d_instantiate().
83 static int nilfs_create(struct mnt_idmap
*idmap
, struct inode
*dir
,
84 struct dentry
*dentry
, umode_t mode
, bool excl
)
87 struct nilfs_transaction_info ti
;
90 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
93 inode
= nilfs_new_inode(dir
, mode
);
96 inode
->i_op
= &nilfs_file_inode_operations
;
97 inode
->i_fop
= &nilfs_file_operations
;
98 inode
->i_mapping
->a_ops
= &nilfs_aops
;
99 nilfs_mark_inode_dirty(inode
);
100 err
= nilfs_add_nondir(dentry
, inode
);
103 err
= nilfs_transaction_commit(dir
->i_sb
);
105 nilfs_transaction_abort(dir
->i_sb
);
111 nilfs_mknod(struct mnt_idmap
*idmap
, struct inode
*dir
,
112 struct dentry
*dentry
, umode_t mode
, dev_t rdev
)
115 struct nilfs_transaction_info ti
;
118 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
121 inode
= nilfs_new_inode(dir
, mode
);
122 err
= PTR_ERR(inode
);
123 if (!IS_ERR(inode
)) {
124 init_special_inode(inode
, inode
->i_mode
, rdev
);
125 nilfs_mark_inode_dirty(inode
);
126 err
= nilfs_add_nondir(dentry
, inode
);
129 err
= nilfs_transaction_commit(dir
->i_sb
);
131 nilfs_transaction_abort(dir
->i_sb
);
136 static int nilfs_symlink(struct mnt_idmap
*idmap
, struct inode
*dir
,
137 struct dentry
*dentry
, const char *symname
)
139 struct nilfs_transaction_info ti
;
140 struct super_block
*sb
= dir
->i_sb
;
141 unsigned int l
= strlen(symname
) + 1;
145 if (l
> sb
->s_blocksize
)
146 return -ENAMETOOLONG
;
148 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
152 inode
= nilfs_new_inode(dir
, S_IFLNK
| 0777);
153 err
= PTR_ERR(inode
);
158 inode
->i_op
= &nilfs_symlink_inode_operations
;
159 inode_nohighmem(inode
);
160 mapping_set_gfp_mask(inode
->i_mapping
,
161 mapping_gfp_constraint(inode
->i_mapping
,
163 inode
->i_mapping
->a_ops
= &nilfs_aops
;
164 err
= page_symlink(inode
, symname
, l
);
168 /* mark_inode_dirty(inode); */
169 /* page_symlink() do this */
171 err
= nilfs_add_nondir(dentry
, inode
);
174 err
= nilfs_transaction_commit(dir
->i_sb
);
176 nilfs_transaction_abort(dir
->i_sb
);
182 nilfs_mark_inode_dirty(inode
);
183 unlock_new_inode(inode
);
188 static int nilfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
189 struct dentry
*dentry
)
191 struct inode
*inode
= d_inode(old_dentry
);
192 struct nilfs_transaction_info ti
;
195 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
199 inode_set_ctime_current(inode
);
200 inode_inc_link_count(inode
);
203 err
= nilfs_add_link(dentry
, inode
);
205 d_instantiate(dentry
, inode
);
206 err
= nilfs_transaction_commit(dir
->i_sb
);
208 inode_dec_link_count(inode
);
210 nilfs_transaction_abort(dir
->i_sb
);
216 static int nilfs_mkdir(struct mnt_idmap
*idmap
, struct inode
*dir
,
217 struct dentry
*dentry
, umode_t mode
)
220 struct nilfs_transaction_info ti
;
223 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
229 inode
= nilfs_new_inode(dir
, S_IFDIR
| mode
);
230 err
= PTR_ERR(inode
);
234 inode
->i_op
= &nilfs_dir_inode_operations
;
235 inode
->i_fop
= &nilfs_dir_operations
;
236 inode
->i_mapping
->a_ops
= &nilfs_aops
;
240 err
= nilfs_make_empty(inode
, dir
);
244 err
= nilfs_add_link(dentry
, inode
);
248 nilfs_mark_inode_dirty(inode
);
249 d_instantiate_new(dentry
, inode
);
252 err
= nilfs_transaction_commit(dir
->i_sb
);
254 nilfs_transaction_abort(dir
->i_sb
);
261 nilfs_mark_inode_dirty(inode
);
262 unlock_new_inode(inode
);
266 nilfs_mark_inode_dirty(dir
);
270 static int nilfs_do_unlink(struct inode
*dir
, struct dentry
*dentry
)
273 struct nilfs_dir_entry
*de
;
277 de
= nilfs_find_entry(dir
, &dentry
->d_name
, &folio
);
283 inode
= d_inode(dentry
);
285 if (le64_to_cpu(de
->inode
) != inode
->i_ino
)
288 if (!inode
->i_nlink
) {
289 nilfs_warn(inode
->i_sb
,
290 "deleting nonexistent file (ino=%lu), %d",
291 inode
->i_ino
, inode
->i_nlink
);
294 err
= nilfs_delete_entry(de
, folio
);
295 folio_release_kmap(folio
, de
);
299 inode_set_ctime_to_ts(inode
, inode_get_ctime(dir
));
306 static int nilfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
308 struct nilfs_transaction_info ti
;
311 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 0);
315 err
= nilfs_do_unlink(dir
, dentry
);
318 nilfs_mark_inode_dirty(dir
);
319 nilfs_mark_inode_dirty(d_inode(dentry
));
320 err
= nilfs_transaction_commit(dir
->i_sb
);
322 nilfs_transaction_abort(dir
->i_sb
);
327 static int nilfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
329 struct inode
*inode
= d_inode(dentry
);
330 struct nilfs_transaction_info ti
;
333 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 0);
338 if (nilfs_empty_dir(inode
)) {
339 err
= nilfs_do_unlink(dir
, dentry
);
343 nilfs_mark_inode_dirty(inode
);
345 nilfs_mark_inode_dirty(dir
);
349 err
= nilfs_transaction_commit(dir
->i_sb
);
351 nilfs_transaction_abort(dir
->i_sb
);
356 static int nilfs_rename(struct mnt_idmap
*idmap
,
357 struct inode
*old_dir
, struct dentry
*old_dentry
,
358 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 folio
*dir_folio
= NULL
;
364 struct nilfs_dir_entry
*dir_de
= NULL
;
365 struct folio
*old_folio
;
366 struct nilfs_dir_entry
*old_de
;
367 struct nilfs_transaction_info ti
;
370 if (flags
& ~RENAME_NOREPLACE
)
373 err
= nilfs_transaction_begin(old_dir
->i_sb
, &ti
, 1);
377 old_de
= nilfs_find_entry(old_dir
, &old_dentry
->d_name
, &old_folio
);
378 if (IS_ERR(old_de
)) {
379 err
= PTR_ERR(old_de
);
383 if (S_ISDIR(old_inode
->i_mode
)) {
385 dir_de
= nilfs_dotdot(old_inode
, &dir_folio
);
391 struct folio
*new_folio
;
392 struct nilfs_dir_entry
*new_de
;
395 if (dir_de
&& !nilfs_empty_dir(new_inode
))
398 new_de
= nilfs_find_entry(new_dir
, &new_dentry
->d_name
,
400 if (IS_ERR(new_de
)) {
401 err
= PTR_ERR(new_de
);
404 nilfs_set_link(new_dir
, new_de
, new_folio
, old_inode
);
405 folio_release_kmap(new_folio
, new_de
);
406 nilfs_mark_inode_dirty(new_dir
);
407 inode_set_ctime_current(new_inode
);
409 drop_nlink(new_inode
);
410 drop_nlink(new_inode
);
411 nilfs_mark_inode_dirty(new_inode
);
413 err
= nilfs_add_link(new_dentry
, old_inode
);
418 nilfs_mark_inode_dirty(new_dir
);
423 * Like most other Unix systems, set the ctime for inodes on a
426 inode_set_ctime_current(old_inode
);
428 nilfs_delete_entry(old_de
, old_folio
);
431 nilfs_set_link(old_inode
, dir_de
, dir_folio
, new_dir
);
432 folio_release_kmap(dir_folio
, dir_de
);
435 folio_release_kmap(old_folio
, old_de
);
437 nilfs_mark_inode_dirty(old_dir
);
438 nilfs_mark_inode_dirty(old_inode
);
440 err
= nilfs_transaction_commit(old_dir
->i_sb
);
445 folio_release_kmap(dir_folio
, dir_de
);
447 folio_release_kmap(old_folio
, old_de
);
449 nilfs_transaction_abort(old_dir
->i_sb
);
456 static struct dentry
*nilfs_get_parent(struct dentry
*child
)
460 struct nilfs_root
*root
;
462 res
= nilfs_inode_by_name(d_inode(child
), &dotdot_name
, &ino
);
466 root
= NILFS_I(d_inode(child
))->i_root
;
468 return d_obtain_alias(nilfs_iget(child
->d_sb
, root
, ino
));
471 static struct dentry
*nilfs_get_dentry(struct super_block
*sb
, u64 cno
,
474 struct nilfs_root
*root
;
477 if (ino
< NILFS_FIRST_INO(sb
) && ino
!= NILFS_ROOT_INO
)
478 return ERR_PTR(-ESTALE
);
480 root
= nilfs_lookup_root(sb
->s_fs_info
, cno
);
482 return ERR_PTR(-ESTALE
);
484 inode
= nilfs_iget(sb
, root
, ino
);
485 nilfs_put_root(root
);
488 return ERR_CAST(inode
);
489 if (gen
&& inode
->i_generation
!= gen
) {
491 return ERR_PTR(-ESTALE
);
493 return d_obtain_alias(inode
);
496 static struct dentry
*nilfs_fh_to_dentry(struct super_block
*sb
, struct fid
*fh
,
497 int fh_len
, int fh_type
)
499 struct nilfs_fid
*fid
= (struct nilfs_fid
*)fh
;
501 if (fh_len
< NILFS_FID_SIZE_NON_CONNECTABLE
||
502 (fh_type
!= FILEID_NILFS_WITH_PARENT
&&
503 fh_type
!= FILEID_NILFS_WITHOUT_PARENT
))
506 return nilfs_get_dentry(sb
, fid
->cno
, fid
->ino
, fid
->gen
);
509 static struct dentry
*nilfs_fh_to_parent(struct super_block
*sb
, struct fid
*fh
,
510 int fh_len
, int fh_type
)
512 struct nilfs_fid
*fid
= (struct nilfs_fid
*)fh
;
514 if (fh_len
< NILFS_FID_SIZE_CONNECTABLE
||
515 fh_type
!= FILEID_NILFS_WITH_PARENT
)
518 return nilfs_get_dentry(sb
, fid
->cno
, fid
->parent_ino
, fid
->parent_gen
);
521 static int nilfs_encode_fh(struct inode
*inode
, __u32
*fh
, int *lenp
,
522 struct inode
*parent
)
524 struct nilfs_fid
*fid
= (struct nilfs_fid
*)fh
;
525 struct nilfs_root
*root
= NILFS_I(inode
)->i_root
;
528 if (parent
&& *lenp
< NILFS_FID_SIZE_CONNECTABLE
) {
529 *lenp
= NILFS_FID_SIZE_CONNECTABLE
;
530 return FILEID_INVALID
;
532 if (*lenp
< NILFS_FID_SIZE_NON_CONNECTABLE
) {
533 *lenp
= NILFS_FID_SIZE_NON_CONNECTABLE
;
534 return FILEID_INVALID
;
537 fid
->cno
= root
->cno
;
538 fid
->ino
= inode
->i_ino
;
539 fid
->gen
= inode
->i_generation
;
542 fid
->parent_ino
= parent
->i_ino
;
543 fid
->parent_gen
= parent
->i_generation
;
544 type
= FILEID_NILFS_WITH_PARENT
;
545 *lenp
= NILFS_FID_SIZE_CONNECTABLE
;
547 type
= FILEID_NILFS_WITHOUT_PARENT
;
548 *lenp
= NILFS_FID_SIZE_NON_CONNECTABLE
;
554 const struct inode_operations nilfs_dir_inode_operations
= {
555 .create
= nilfs_create
,
556 .lookup
= nilfs_lookup
,
558 .unlink
= nilfs_unlink
,
559 .symlink
= nilfs_symlink
,
560 .mkdir
= nilfs_mkdir
,
561 .rmdir
= nilfs_rmdir
,
562 .mknod
= nilfs_mknod
,
563 .rename
= nilfs_rename
,
564 .setattr
= nilfs_setattr
,
565 .permission
= nilfs_permission
,
566 .fiemap
= nilfs_fiemap
,
567 .fileattr_get
= nilfs_fileattr_get
,
568 .fileattr_set
= nilfs_fileattr_set
,
571 const struct inode_operations nilfs_special_inode_operations
= {
572 .setattr
= nilfs_setattr
,
573 .permission
= nilfs_permission
,
576 const struct inode_operations nilfs_symlink_inode_operations
= {
577 .get_link
= page_get_link
,
578 .permission
= nilfs_permission
,
581 const struct export_operations nilfs_export_ops
= {
582 .encode_fh
= nilfs_encode_fh
,
583 .fh_to_dentry
= nilfs_fh_to_dentry
,
584 .fh_to_parent
= nilfs_fh_to_parent
,
585 .get_parent
= nilfs_get_parent
,