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
);
70 if (inode
== ERR_PTR(-ESTALE
)) {
71 nilfs_error(dir
->i_sb
,
72 "deleted inode referenced: %lu", ino
);
77 return d_splice_alias(inode
, dentry
);
81 * By the time this is called, we already have created
82 * the directory cache entry for the new file, but it
83 * is so far negative - it has no inode.
85 * If the create succeeds, we fill in the inode information
86 * with d_instantiate().
88 static int nilfs_create(struct mnt_idmap
*idmap
, struct inode
*dir
,
89 struct dentry
*dentry
, umode_t mode
, bool excl
)
92 struct nilfs_transaction_info ti
;
95 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
98 inode
= nilfs_new_inode(dir
, mode
);
100 if (!IS_ERR(inode
)) {
101 inode
->i_op
= &nilfs_file_inode_operations
;
102 inode
->i_fop
= &nilfs_file_operations
;
103 inode
->i_mapping
->a_ops
= &nilfs_aops
;
104 nilfs_mark_inode_dirty(inode
);
105 err
= nilfs_add_nondir(dentry
, inode
);
108 err
= nilfs_transaction_commit(dir
->i_sb
);
110 nilfs_transaction_abort(dir
->i_sb
);
116 nilfs_mknod(struct mnt_idmap
*idmap
, struct inode
*dir
,
117 struct dentry
*dentry
, umode_t mode
, dev_t rdev
)
120 struct nilfs_transaction_info ti
;
123 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
126 inode
= nilfs_new_inode(dir
, mode
);
127 err
= PTR_ERR(inode
);
128 if (!IS_ERR(inode
)) {
129 init_special_inode(inode
, inode
->i_mode
, rdev
);
130 nilfs_mark_inode_dirty(inode
);
131 err
= nilfs_add_nondir(dentry
, inode
);
134 err
= nilfs_transaction_commit(dir
->i_sb
);
136 nilfs_transaction_abort(dir
->i_sb
);
141 static int nilfs_symlink(struct mnt_idmap
*idmap
, struct inode
*dir
,
142 struct dentry
*dentry
, const char *symname
)
144 struct nilfs_transaction_info ti
;
145 struct super_block
*sb
= dir
->i_sb
;
146 unsigned int l
= strlen(symname
) + 1;
150 if (l
> sb
->s_blocksize
)
151 return -ENAMETOOLONG
;
153 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
157 inode
= nilfs_new_inode(dir
, S_IFLNK
| 0777);
158 err
= PTR_ERR(inode
);
163 inode
->i_op
= &nilfs_symlink_inode_operations
;
164 inode_nohighmem(inode
);
165 mapping_set_gfp_mask(inode
->i_mapping
,
166 mapping_gfp_constraint(inode
->i_mapping
,
168 inode
->i_mapping
->a_ops
= &nilfs_aops
;
169 err
= page_symlink(inode
, symname
, l
);
173 /* mark_inode_dirty(inode); */
174 /* page_symlink() do this */
176 err
= nilfs_add_nondir(dentry
, inode
);
179 err
= nilfs_transaction_commit(dir
->i_sb
);
181 nilfs_transaction_abort(dir
->i_sb
);
187 nilfs_mark_inode_dirty(inode
);
188 unlock_new_inode(inode
);
193 static int nilfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
194 struct dentry
*dentry
)
196 struct inode
*inode
= d_inode(old_dentry
);
197 struct nilfs_transaction_info ti
;
200 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
204 inode_set_ctime_current(inode
);
205 inode_inc_link_count(inode
);
208 err
= nilfs_add_link(dentry
, inode
);
210 d_instantiate(dentry
, inode
);
211 err
= nilfs_transaction_commit(dir
->i_sb
);
213 inode_dec_link_count(inode
);
215 nilfs_transaction_abort(dir
->i_sb
);
221 static int nilfs_mkdir(struct mnt_idmap
*idmap
, struct inode
*dir
,
222 struct dentry
*dentry
, umode_t mode
)
225 struct nilfs_transaction_info ti
;
228 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
234 inode
= nilfs_new_inode(dir
, S_IFDIR
| mode
);
235 err
= PTR_ERR(inode
);
239 inode
->i_op
= &nilfs_dir_inode_operations
;
240 inode
->i_fop
= &nilfs_dir_operations
;
241 inode
->i_mapping
->a_ops
= &nilfs_aops
;
245 err
= nilfs_make_empty(inode
, dir
);
249 err
= nilfs_add_link(dentry
, inode
);
253 nilfs_mark_inode_dirty(inode
);
254 d_instantiate_new(dentry
, inode
);
257 err
= nilfs_transaction_commit(dir
->i_sb
);
259 nilfs_transaction_abort(dir
->i_sb
);
266 nilfs_mark_inode_dirty(inode
);
267 unlock_new_inode(inode
);
271 nilfs_mark_inode_dirty(dir
);
275 static int nilfs_do_unlink(struct inode
*dir
, struct dentry
*dentry
)
278 struct nilfs_dir_entry
*de
;
282 de
= nilfs_find_entry(dir
, &dentry
->d_name
, &folio
);
288 inode
= d_inode(dentry
);
290 if (le64_to_cpu(de
->inode
) != inode
->i_ino
)
293 if (!inode
->i_nlink
) {
294 nilfs_warn(inode
->i_sb
,
295 "deleting nonexistent file (ino=%lu), %d",
296 inode
->i_ino
, inode
->i_nlink
);
299 err
= nilfs_delete_entry(de
, folio
);
300 folio_release_kmap(folio
, de
);
304 inode_set_ctime_to_ts(inode
, inode_get_ctime(dir
));
311 static int nilfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
313 struct nilfs_transaction_info ti
;
316 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 0);
320 err
= nilfs_do_unlink(dir
, dentry
);
323 nilfs_mark_inode_dirty(dir
);
324 nilfs_mark_inode_dirty(d_inode(dentry
));
325 err
= nilfs_transaction_commit(dir
->i_sb
);
327 nilfs_transaction_abort(dir
->i_sb
);
332 static int nilfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
334 struct inode
*inode
= d_inode(dentry
);
335 struct nilfs_transaction_info ti
;
338 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 0);
343 if (nilfs_empty_dir(inode
)) {
344 err
= nilfs_do_unlink(dir
, dentry
);
348 nilfs_mark_inode_dirty(inode
);
350 nilfs_mark_inode_dirty(dir
);
354 err
= nilfs_transaction_commit(dir
->i_sb
);
356 nilfs_transaction_abort(dir
->i_sb
);
361 static int nilfs_rename(struct mnt_idmap
*idmap
,
362 struct inode
*old_dir
, struct dentry
*old_dentry
,
363 struct inode
*new_dir
, struct dentry
*new_dentry
,
366 struct inode
*old_inode
= d_inode(old_dentry
);
367 struct inode
*new_inode
= d_inode(new_dentry
);
368 struct folio
*dir_folio
= NULL
;
369 struct nilfs_dir_entry
*dir_de
= NULL
;
370 struct folio
*old_folio
;
371 struct nilfs_dir_entry
*old_de
;
372 struct nilfs_transaction_info ti
;
375 if (flags
& ~RENAME_NOREPLACE
)
378 err
= nilfs_transaction_begin(old_dir
->i_sb
, &ti
, 1);
382 old_de
= nilfs_find_entry(old_dir
, &old_dentry
->d_name
, &old_folio
);
383 if (IS_ERR(old_de
)) {
384 err
= PTR_ERR(old_de
);
388 if (S_ISDIR(old_inode
->i_mode
)) {
390 dir_de
= nilfs_dotdot(old_inode
, &dir_folio
);
396 struct folio
*new_folio
;
397 struct nilfs_dir_entry
*new_de
;
400 if (dir_de
&& !nilfs_empty_dir(new_inode
))
403 new_de
= nilfs_find_entry(new_dir
, &new_dentry
->d_name
,
405 if (IS_ERR(new_de
)) {
406 err
= PTR_ERR(new_de
);
409 nilfs_set_link(new_dir
, new_de
, new_folio
, old_inode
);
410 folio_release_kmap(new_folio
, new_de
);
411 nilfs_mark_inode_dirty(new_dir
);
412 inode_set_ctime_current(new_inode
);
414 drop_nlink(new_inode
);
415 drop_nlink(new_inode
);
416 nilfs_mark_inode_dirty(new_inode
);
418 err
= nilfs_add_link(new_dentry
, old_inode
);
423 nilfs_mark_inode_dirty(new_dir
);
428 * Like most other Unix systems, set the ctime for inodes on a
431 inode_set_ctime_current(old_inode
);
433 nilfs_delete_entry(old_de
, old_folio
);
436 nilfs_set_link(old_inode
, dir_de
, dir_folio
, new_dir
);
437 folio_release_kmap(dir_folio
, dir_de
);
440 folio_release_kmap(old_folio
, old_de
);
442 nilfs_mark_inode_dirty(old_dir
);
443 nilfs_mark_inode_dirty(old_inode
);
445 err
= nilfs_transaction_commit(old_dir
->i_sb
);
450 folio_release_kmap(dir_folio
, dir_de
);
452 folio_release_kmap(old_folio
, old_de
);
454 nilfs_transaction_abort(old_dir
->i_sb
);
461 static struct dentry
*nilfs_get_parent(struct dentry
*child
)
465 struct nilfs_root
*root
;
467 res
= nilfs_inode_by_name(d_inode(child
), &dotdot_name
, &ino
);
471 root
= NILFS_I(d_inode(child
))->i_root
;
473 return d_obtain_alias(nilfs_iget(child
->d_sb
, root
, ino
));
476 static struct dentry
*nilfs_get_dentry(struct super_block
*sb
, u64 cno
,
479 struct nilfs_root
*root
;
482 if (ino
< NILFS_FIRST_INO(sb
) && ino
!= NILFS_ROOT_INO
)
483 return ERR_PTR(-ESTALE
);
485 root
= nilfs_lookup_root(sb
->s_fs_info
, cno
);
487 return ERR_PTR(-ESTALE
);
489 inode
= nilfs_iget(sb
, root
, ino
);
490 nilfs_put_root(root
);
493 return ERR_CAST(inode
);
494 if (gen
&& inode
->i_generation
!= gen
) {
496 return ERR_PTR(-ESTALE
);
498 return d_obtain_alias(inode
);
501 static struct dentry
*nilfs_fh_to_dentry(struct super_block
*sb
, struct fid
*fh
,
502 int fh_len
, int fh_type
)
504 struct nilfs_fid
*fid
= (struct nilfs_fid
*)fh
;
506 if (fh_len
< NILFS_FID_SIZE_NON_CONNECTABLE
||
507 (fh_type
!= FILEID_NILFS_WITH_PARENT
&&
508 fh_type
!= FILEID_NILFS_WITHOUT_PARENT
))
511 return nilfs_get_dentry(sb
, fid
->cno
, fid
->ino
, fid
->gen
);
514 static struct dentry
*nilfs_fh_to_parent(struct super_block
*sb
, struct fid
*fh
,
515 int fh_len
, int fh_type
)
517 struct nilfs_fid
*fid
= (struct nilfs_fid
*)fh
;
519 if (fh_len
< NILFS_FID_SIZE_CONNECTABLE
||
520 fh_type
!= FILEID_NILFS_WITH_PARENT
)
523 return nilfs_get_dentry(sb
, fid
->cno
, fid
->parent_ino
, fid
->parent_gen
);
526 static int nilfs_encode_fh(struct inode
*inode
, __u32
*fh
, int *lenp
,
527 struct inode
*parent
)
529 struct nilfs_fid
*fid
= (struct nilfs_fid
*)fh
;
530 struct nilfs_root
*root
= NILFS_I(inode
)->i_root
;
533 if (parent
&& *lenp
< NILFS_FID_SIZE_CONNECTABLE
) {
534 *lenp
= NILFS_FID_SIZE_CONNECTABLE
;
535 return FILEID_INVALID
;
537 if (*lenp
< NILFS_FID_SIZE_NON_CONNECTABLE
) {
538 *lenp
= NILFS_FID_SIZE_NON_CONNECTABLE
;
539 return FILEID_INVALID
;
542 fid
->cno
= root
->cno
;
543 fid
->ino
= inode
->i_ino
;
544 fid
->gen
= inode
->i_generation
;
547 fid
->parent_ino
= parent
->i_ino
;
548 fid
->parent_gen
= parent
->i_generation
;
549 type
= FILEID_NILFS_WITH_PARENT
;
550 *lenp
= NILFS_FID_SIZE_CONNECTABLE
;
552 type
= FILEID_NILFS_WITHOUT_PARENT
;
553 *lenp
= NILFS_FID_SIZE_NON_CONNECTABLE
;
559 const struct inode_operations nilfs_dir_inode_operations
= {
560 .create
= nilfs_create
,
561 .lookup
= nilfs_lookup
,
563 .unlink
= nilfs_unlink
,
564 .symlink
= nilfs_symlink
,
565 .mkdir
= nilfs_mkdir
,
566 .rmdir
= nilfs_rmdir
,
567 .mknod
= nilfs_mknod
,
568 .rename
= nilfs_rename
,
569 .setattr
= nilfs_setattr
,
570 .permission
= nilfs_permission
,
571 .fiemap
= nilfs_fiemap
,
572 .fileattr_get
= nilfs_fileattr_get
,
573 .fileattr_set
= nilfs_fileattr_set
,
576 const struct inode_operations nilfs_special_inode_operations
= {
577 .setattr
= nilfs_setattr
,
578 .permission
= nilfs_permission
,
581 const struct inode_operations nilfs_symlink_inode_operations
= {
582 .get_link
= page_get_link
,
583 .permission
= nilfs_permission
,
586 const struct export_operations nilfs_export_ops
= {
587 .encode_fh
= nilfs_encode_fh
,
588 .fh_to_dentry
= nilfs_fh_to_dentry
,
589 .fh_to_parent
= nilfs_fh_to_parent
,
590 .get_parent
= nilfs_get_parent
,