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
)
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 user_namespace
*mnt_userns
, struct inode
*dir
,
76 struct dentry
*dentry
, umode_t mode
, bool excl
)
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 user_namespace
*mnt_userns
, struct inode
*dir
,
104 struct dentry
*dentry
, umode_t mode
, dev_t rdev
)
107 struct nilfs_transaction_info ti
;
110 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
113 inode
= nilfs_new_inode(dir
, mode
);
114 err
= PTR_ERR(inode
);
115 if (!IS_ERR(inode
)) {
116 init_special_inode(inode
, inode
->i_mode
, rdev
);
117 nilfs_mark_inode_dirty(inode
);
118 err
= nilfs_add_nondir(dentry
, inode
);
121 err
= nilfs_transaction_commit(dir
->i_sb
);
123 nilfs_transaction_abort(dir
->i_sb
);
128 static int nilfs_symlink(struct user_namespace
*mnt_userns
, struct inode
*dir
,
129 struct dentry
*dentry
, const char *symname
)
131 struct nilfs_transaction_info ti
;
132 struct super_block
*sb
= dir
->i_sb
;
133 unsigned int l
= strlen(symname
) + 1;
137 if (l
> sb
->s_blocksize
)
138 return -ENAMETOOLONG
;
140 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
144 inode
= nilfs_new_inode(dir
, S_IFLNK
| 0777);
145 err
= PTR_ERR(inode
);
150 inode
->i_op
= &nilfs_symlink_inode_operations
;
151 inode_nohighmem(inode
);
152 inode
->i_mapping
->a_ops
= &nilfs_aops
;
153 err
= page_symlink(inode
, symname
, l
);
157 /* mark_inode_dirty(inode); */
158 /* page_symlink() do this */
160 err
= nilfs_add_nondir(dentry
, inode
);
163 err
= nilfs_transaction_commit(dir
->i_sb
);
165 nilfs_transaction_abort(dir
->i_sb
);
171 nilfs_mark_inode_dirty(inode
);
172 unlock_new_inode(inode
);
177 static int nilfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
178 struct dentry
*dentry
)
180 struct inode
*inode
= d_inode(old_dentry
);
181 struct nilfs_transaction_info ti
;
184 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
188 inode
->i_ctime
= current_time(inode
);
189 inode_inc_link_count(inode
);
192 err
= nilfs_add_link(dentry
, inode
);
194 d_instantiate(dentry
, inode
);
195 err
= nilfs_transaction_commit(dir
->i_sb
);
197 inode_dec_link_count(inode
);
199 nilfs_transaction_abort(dir
->i_sb
);
205 static int nilfs_mkdir(struct user_namespace
*mnt_userns
, struct inode
*dir
,
206 struct dentry
*dentry
, umode_t mode
)
209 struct nilfs_transaction_info ti
;
212 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
218 inode
= nilfs_new_inode(dir
, S_IFDIR
| mode
);
219 err
= PTR_ERR(inode
);
223 inode
->i_op
= &nilfs_dir_inode_operations
;
224 inode
->i_fop
= &nilfs_dir_operations
;
225 inode
->i_mapping
->a_ops
= &nilfs_aops
;
229 err
= nilfs_make_empty(inode
, dir
);
233 err
= nilfs_add_link(dentry
, inode
);
237 nilfs_mark_inode_dirty(inode
);
238 d_instantiate_new(dentry
, inode
);
241 err
= nilfs_transaction_commit(dir
->i_sb
);
243 nilfs_transaction_abort(dir
->i_sb
);
250 nilfs_mark_inode_dirty(inode
);
251 unlock_new_inode(inode
);
255 nilfs_mark_inode_dirty(dir
);
259 static int nilfs_do_unlink(struct inode
*dir
, struct dentry
*dentry
)
262 struct nilfs_dir_entry
*de
;
267 de
= nilfs_find_entry(dir
, &dentry
->d_name
, &page
);
271 inode
= d_inode(dentry
);
273 if (le64_to_cpu(de
->inode
) != inode
->i_ino
)
276 if (!inode
->i_nlink
) {
277 nilfs_warn(inode
->i_sb
,
278 "deleting nonexistent file (ino=%lu), %d",
279 inode
->i_ino
, inode
->i_nlink
);
282 err
= nilfs_delete_entry(de
, page
);
286 inode
->i_ctime
= dir
->i_ctime
;
293 static int nilfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
295 struct nilfs_transaction_info ti
;
298 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 0);
302 err
= nilfs_do_unlink(dir
, dentry
);
305 nilfs_mark_inode_dirty(dir
);
306 nilfs_mark_inode_dirty(d_inode(dentry
));
307 err
= nilfs_transaction_commit(dir
->i_sb
);
309 nilfs_transaction_abort(dir
->i_sb
);
314 static int nilfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
316 struct inode
*inode
= d_inode(dentry
);
317 struct nilfs_transaction_info ti
;
320 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 0);
325 if (nilfs_empty_dir(inode
)) {
326 err
= nilfs_do_unlink(dir
, dentry
);
330 nilfs_mark_inode_dirty(inode
);
332 nilfs_mark_inode_dirty(dir
);
336 err
= nilfs_transaction_commit(dir
->i_sb
);
338 nilfs_transaction_abort(dir
->i_sb
);
343 static int nilfs_rename(struct user_namespace
*mnt_userns
,
344 struct inode
*old_dir
, struct dentry
*old_dentry
,
345 struct inode
*new_dir
, struct dentry
*new_dentry
,
348 struct inode
*old_inode
= d_inode(old_dentry
);
349 struct inode
*new_inode
= d_inode(new_dentry
);
350 struct page
*dir_page
= NULL
;
351 struct nilfs_dir_entry
*dir_de
= NULL
;
352 struct page
*old_page
;
353 struct nilfs_dir_entry
*old_de
;
354 struct nilfs_transaction_info ti
;
357 if (flags
& ~RENAME_NOREPLACE
)
360 err
= nilfs_transaction_begin(old_dir
->i_sb
, &ti
, 1);
365 old_de
= nilfs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
369 if (S_ISDIR(old_inode
->i_mode
)) {
371 dir_de
= nilfs_dotdot(old_inode
, &dir_page
);
377 struct page
*new_page
;
378 struct nilfs_dir_entry
*new_de
;
381 if (dir_de
&& !nilfs_empty_dir(new_inode
))
385 new_de
= nilfs_find_entry(new_dir
, &new_dentry
->d_name
, &new_page
);
388 nilfs_set_link(new_dir
, new_de
, new_page
, old_inode
);
389 nilfs_mark_inode_dirty(new_dir
);
390 new_inode
->i_ctime
= current_time(new_inode
);
392 drop_nlink(new_inode
);
393 drop_nlink(new_inode
);
394 nilfs_mark_inode_dirty(new_inode
);
396 err
= nilfs_add_link(new_dentry
, old_inode
);
401 nilfs_mark_inode_dirty(new_dir
);
406 * Like most other Unix systems, set the ctime for inodes on a
409 old_inode
->i_ctime
= current_time(old_inode
);
411 nilfs_delete_entry(old_de
, old_page
);
414 nilfs_set_link(old_inode
, dir_de
, dir_page
, new_dir
);
417 nilfs_mark_inode_dirty(old_dir
);
418 nilfs_mark_inode_dirty(old_inode
);
420 err
= nilfs_transaction_commit(old_dir
->i_sb
);
432 nilfs_transaction_abort(old_dir
->i_sb
);
439 static struct dentry
*nilfs_get_parent(struct dentry
*child
)
443 struct nilfs_root
*root
;
445 ino
= nilfs_inode_by_name(d_inode(child
), &dotdot_name
);
447 return ERR_PTR(-ENOENT
);
449 root
= NILFS_I(d_inode(child
))->i_root
;
451 inode
= nilfs_iget(child
->d_sb
, root
, ino
);
453 return ERR_CAST(inode
);
455 return d_obtain_alias(inode
);
458 static struct dentry
*nilfs_get_dentry(struct super_block
*sb
, u64 cno
,
461 struct nilfs_root
*root
;
464 if (ino
< NILFS_FIRST_INO(sb
) && ino
!= NILFS_ROOT_INO
)
465 return ERR_PTR(-ESTALE
);
467 root
= nilfs_lookup_root(sb
->s_fs_info
, cno
);
469 return ERR_PTR(-ESTALE
);
471 inode
= nilfs_iget(sb
, root
, ino
);
472 nilfs_put_root(root
);
475 return ERR_CAST(inode
);
476 if (gen
&& inode
->i_generation
!= gen
) {
478 return ERR_PTR(-ESTALE
);
480 return d_obtain_alias(inode
);
483 static struct dentry
*nilfs_fh_to_dentry(struct super_block
*sb
, struct fid
*fh
,
484 int fh_len
, int fh_type
)
486 struct nilfs_fid
*fid
= (struct nilfs_fid
*)fh
;
488 if (fh_len
< NILFS_FID_SIZE_NON_CONNECTABLE
||
489 (fh_type
!= FILEID_NILFS_WITH_PARENT
&&
490 fh_type
!= FILEID_NILFS_WITHOUT_PARENT
))
493 return nilfs_get_dentry(sb
, fid
->cno
, fid
->ino
, fid
->gen
);
496 static struct dentry
*nilfs_fh_to_parent(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_CONNECTABLE
||
502 fh_type
!= FILEID_NILFS_WITH_PARENT
)
505 return nilfs_get_dentry(sb
, fid
->cno
, fid
->parent_ino
, fid
->parent_gen
);
508 static int nilfs_encode_fh(struct inode
*inode
, __u32
*fh
, int *lenp
,
509 struct inode
*parent
)
511 struct nilfs_fid
*fid
= (struct nilfs_fid
*)fh
;
512 struct nilfs_root
*root
= NILFS_I(inode
)->i_root
;
515 if (parent
&& *lenp
< NILFS_FID_SIZE_CONNECTABLE
) {
516 *lenp
= NILFS_FID_SIZE_CONNECTABLE
;
517 return FILEID_INVALID
;
519 if (*lenp
< NILFS_FID_SIZE_NON_CONNECTABLE
) {
520 *lenp
= NILFS_FID_SIZE_NON_CONNECTABLE
;
521 return FILEID_INVALID
;
524 fid
->cno
= root
->cno
;
525 fid
->ino
= inode
->i_ino
;
526 fid
->gen
= inode
->i_generation
;
529 fid
->parent_ino
= parent
->i_ino
;
530 fid
->parent_gen
= parent
->i_generation
;
531 type
= FILEID_NILFS_WITH_PARENT
;
532 *lenp
= NILFS_FID_SIZE_CONNECTABLE
;
534 type
= FILEID_NILFS_WITHOUT_PARENT
;
535 *lenp
= NILFS_FID_SIZE_NON_CONNECTABLE
;
541 const struct inode_operations nilfs_dir_inode_operations
= {
542 .create
= nilfs_create
,
543 .lookup
= nilfs_lookup
,
545 .unlink
= nilfs_unlink
,
546 .symlink
= nilfs_symlink
,
547 .mkdir
= nilfs_mkdir
,
548 .rmdir
= nilfs_rmdir
,
549 .mknod
= nilfs_mknod
,
550 .rename
= nilfs_rename
,
551 .setattr
= nilfs_setattr
,
552 .permission
= nilfs_permission
,
553 .fiemap
= nilfs_fiemap
,
554 .fileattr_get
= nilfs_fileattr_get
,
555 .fileattr_set
= nilfs_fileattr_set
,
558 const struct inode_operations nilfs_special_inode_operations
= {
559 .setattr
= nilfs_setattr
,
560 .permission
= nilfs_permission
,
563 const struct inode_operations nilfs_symlink_inode_operations
= {
564 .get_link
= page_get_link
,
565 .permission
= nilfs_permission
,
568 const struct export_operations nilfs_export_ops
= {
569 .encode_fh
= nilfs_encode_fh
,
570 .fh_to_dentry
= nilfs_fh_to_dentry
,
571 .fh_to_parent
= nilfs_fh_to_parent
,
572 .get_parent
= nilfs_get_parent
,