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
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
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 qstr dotdot = QSTR_INIT("..", 2);
444 struct nilfs_root *root;
446 ino = nilfs_inode_by_name(d_inode(child), &dotdot);
448 return ERR_PTR(-ENOENT);
450 root = NILFS_I(d_inode(child))->i_root;
452 inode = nilfs_iget(child->d_sb, root, ino);
454 return ERR_CAST(inode);
456 return d_obtain_alias(inode);
459 static struct dentry *nilfs_get_dentry(struct super_block *sb, u64 cno,
462 struct nilfs_root *root;
465 if (ino < NILFS_FIRST_INO(sb) && ino != NILFS_ROOT_INO)
466 return ERR_PTR(-ESTALE);
468 root = nilfs_lookup_root(sb->s_fs_info, cno);
470 return ERR_PTR(-ESTALE);
472 inode = nilfs_iget(sb, root, ino);
473 nilfs_put_root(root);
476 return ERR_CAST(inode);
477 if (gen && inode->i_generation != gen) {
479 return ERR_PTR(-ESTALE);
481 return d_obtain_alias(inode);
484 static struct dentry *nilfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
485 int fh_len, int fh_type)
487 struct nilfs_fid *fid = (struct nilfs_fid *)fh;
489 if (fh_len < NILFS_FID_SIZE_NON_CONNECTABLE ||
490 (fh_type != FILEID_NILFS_WITH_PARENT &&
491 fh_type != FILEID_NILFS_WITHOUT_PARENT))
494 return nilfs_get_dentry(sb, fid->cno, fid->ino, fid->gen);
497 static struct dentry *nilfs_fh_to_parent(struct super_block *sb, struct fid *fh,
498 int fh_len, int fh_type)
500 struct nilfs_fid *fid = (struct nilfs_fid *)fh;
502 if (fh_len < NILFS_FID_SIZE_CONNECTABLE ||
503 fh_type != FILEID_NILFS_WITH_PARENT)
506 return nilfs_get_dentry(sb, fid->cno, fid->parent_ino, fid->parent_gen);
509 static int nilfs_encode_fh(struct inode *inode, __u32 *fh, int *lenp,
510 struct inode *parent)
512 struct nilfs_fid *fid = (struct nilfs_fid *)fh;
513 struct nilfs_root *root = NILFS_I(inode)->i_root;
516 if (parent && *lenp < NILFS_FID_SIZE_CONNECTABLE) {
517 *lenp = NILFS_FID_SIZE_CONNECTABLE;
518 return FILEID_INVALID;
520 if (*lenp < NILFS_FID_SIZE_NON_CONNECTABLE) {
521 *lenp = NILFS_FID_SIZE_NON_CONNECTABLE;
522 return FILEID_INVALID;
525 fid->cno = root->cno;
526 fid->ino = inode->i_ino;
527 fid->gen = inode->i_generation;
530 fid->parent_ino = parent->i_ino;
531 fid->parent_gen = parent->i_generation;
532 type = FILEID_NILFS_WITH_PARENT;
533 *lenp = NILFS_FID_SIZE_CONNECTABLE;
535 type = FILEID_NILFS_WITHOUT_PARENT;
536 *lenp = NILFS_FID_SIZE_NON_CONNECTABLE;
542 const struct inode_operations nilfs_dir_inode_operations = {
543 .create = nilfs_create,
544 .lookup = nilfs_lookup,
546 .unlink = nilfs_unlink,
547 .symlink = nilfs_symlink,
548 .mkdir = nilfs_mkdir,
549 .rmdir = nilfs_rmdir,
550 .mknod = nilfs_mknod,
551 .rename = nilfs_rename,
552 .setattr = nilfs_setattr,
553 .permission = nilfs_permission,
554 .fiemap = nilfs_fiemap,
557 const struct inode_operations nilfs_special_inode_operations = {
558 .setattr = nilfs_setattr,
559 .permission = nilfs_permission,
562 const struct inode_operations nilfs_symlink_inode_operations = {
563 .get_link = page_get_link,
564 .permission = nilfs_permission,
567 const struct export_operations nilfs_export_ops = {
568 .encode_fh = nilfs_encode_fh,
569 .fh_to_dentry = nilfs_fh_to_dentry,
570 .fh_to_parent = nilfs_fh_to_parent,
571 .get_parent = nilfs_get_parent,