2 * namei.c - NILFS pathname lookup operations.
4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * Modified for NILFS by Amagai Yoshiji and Ryusuke Konishi.
19 * linux/fs/ext2/namei.c
21 * Copyright (C) 1992, 1993, 1994, 1995
22 * Remy Card (card@masi.ibp.fr)
23 * Laboratoire MASI - Institut Blaise Pascal
24 * Universite Pierre et Marie Curie (Paris VI)
28 * linux/fs/minix/namei.c
30 * Copyright (C) 1991, 1992 Linus Torvalds
32 * Big-endian to little-endian byte-swapping/bitmaps by
33 * David S. Miller (davem@caip.rutgers.edu), 1995
36 #include <linux/pagemap.h>
40 #define NILFS_FID_SIZE_NON_CONNECTABLE \
41 (offsetof(struct nilfs_fid, parent_gen) / 4)
42 #define NILFS_FID_SIZE_CONNECTABLE (sizeof(struct nilfs_fid) / 4)
44 static inline int nilfs_add_nondir(struct dentry
*dentry
, struct inode
*inode
)
46 int err
= nilfs_add_link(dentry
, inode
);
49 d_instantiate_new(dentry
, inode
);
52 inode_dec_link_count(inode
);
53 unlock_new_inode(inode
);
62 static struct dentry
*
63 nilfs_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
68 if (dentry
->d_name
.len
> NILFS_NAME_LEN
)
69 return ERR_PTR(-ENAMETOOLONG
);
71 ino
= nilfs_inode_by_name(dir
, &dentry
->d_name
);
72 inode
= ino
? nilfs_iget(dir
->i_sb
, NILFS_I(dir
)->i_root
, ino
) : NULL
;
73 return d_splice_alias(inode
, dentry
);
77 * By the time this is called, we already have created
78 * the directory cache entry for the new file, but it
79 * is so far negative - it has no inode.
81 * If the create succeeds, we fill in the inode information
82 * with d_instantiate().
84 static int nilfs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
88 struct nilfs_transaction_info ti
;
91 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
94 inode
= nilfs_new_inode(dir
, mode
);
97 inode
->i_op
= &nilfs_file_inode_operations
;
98 inode
->i_fop
= &nilfs_file_operations
;
99 inode
->i_mapping
->a_ops
= &nilfs_aops
;
100 nilfs_mark_inode_dirty(inode
);
101 err
= nilfs_add_nondir(dentry
, inode
);
104 err
= nilfs_transaction_commit(dir
->i_sb
);
106 nilfs_transaction_abort(dir
->i_sb
);
112 nilfs_mknod(struct inode
*dir
, 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 inode
*dir
, struct dentry
*dentry
,
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
| S_IRWXUGO
);
153 err
= PTR_ERR(inode
);
158 inode
->i_op
= &nilfs_symlink_inode_operations
;
159 inode_nohighmem(inode
);
160 inode
->i_mapping
->a_ops
= &nilfs_aops
;
161 err
= page_symlink(inode
, symname
, l
);
165 /* mark_inode_dirty(inode); */
166 /* page_symlink() do this */
168 err
= nilfs_add_nondir(dentry
, inode
);
171 err
= nilfs_transaction_commit(dir
->i_sb
);
173 nilfs_transaction_abort(dir
->i_sb
);
179 nilfs_mark_inode_dirty(inode
);
180 unlock_new_inode(inode
);
185 static int nilfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
186 struct dentry
*dentry
)
188 struct inode
*inode
= d_inode(old_dentry
);
189 struct nilfs_transaction_info ti
;
192 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
196 inode
->i_ctime
= current_time(inode
);
197 inode_inc_link_count(inode
);
200 err
= nilfs_add_link(dentry
, inode
);
202 d_instantiate(dentry
, inode
);
203 err
= nilfs_transaction_commit(dir
->i_sb
);
205 inode_dec_link_count(inode
);
207 nilfs_transaction_abort(dir
->i_sb
);
213 static int nilfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
216 struct nilfs_transaction_info ti
;
219 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
225 inode
= nilfs_new_inode(dir
, S_IFDIR
| mode
);
226 err
= PTR_ERR(inode
);
230 inode
->i_op
= &nilfs_dir_inode_operations
;
231 inode
->i_fop
= &nilfs_dir_operations
;
232 inode
->i_mapping
->a_ops
= &nilfs_aops
;
236 err
= nilfs_make_empty(inode
, dir
);
240 err
= nilfs_add_link(dentry
, inode
);
244 nilfs_mark_inode_dirty(inode
);
245 d_instantiate_new(dentry
, inode
);
248 err
= nilfs_transaction_commit(dir
->i_sb
);
250 nilfs_transaction_abort(dir
->i_sb
);
257 nilfs_mark_inode_dirty(inode
);
258 unlock_new_inode(inode
);
262 nilfs_mark_inode_dirty(dir
);
266 static int nilfs_do_unlink(struct inode
*dir
, struct dentry
*dentry
)
269 struct nilfs_dir_entry
*de
;
274 de
= nilfs_find_entry(dir
, &dentry
->d_name
, &page
);
278 inode
= d_inode(dentry
);
280 if (le64_to_cpu(de
->inode
) != inode
->i_ino
)
283 if (!inode
->i_nlink
) {
284 nilfs_msg(inode
->i_sb
, KERN_WARNING
,
285 "deleting nonexistent file (ino=%lu), %d",
286 inode
->i_ino
, inode
->i_nlink
);
289 err
= nilfs_delete_entry(de
, page
);
293 inode
->i_ctime
= dir
->i_ctime
;
300 static int nilfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
302 struct nilfs_transaction_info ti
;
305 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 0);
309 err
= nilfs_do_unlink(dir
, dentry
);
312 nilfs_mark_inode_dirty(dir
);
313 nilfs_mark_inode_dirty(d_inode(dentry
));
314 err
= nilfs_transaction_commit(dir
->i_sb
);
316 nilfs_transaction_abort(dir
->i_sb
);
321 static int nilfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
323 struct inode
*inode
= d_inode(dentry
);
324 struct nilfs_transaction_info ti
;
327 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 0);
332 if (nilfs_empty_dir(inode
)) {
333 err
= nilfs_do_unlink(dir
, dentry
);
337 nilfs_mark_inode_dirty(inode
);
339 nilfs_mark_inode_dirty(dir
);
343 err
= nilfs_transaction_commit(dir
->i_sb
);
345 nilfs_transaction_abort(dir
->i_sb
);
350 static int nilfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
351 struct inode
*new_dir
, struct dentry
*new_dentry
,
354 struct inode
*old_inode
= d_inode(old_dentry
);
355 struct inode
*new_inode
= d_inode(new_dentry
);
356 struct page
*dir_page
= NULL
;
357 struct nilfs_dir_entry
*dir_de
= NULL
;
358 struct page
*old_page
;
359 struct nilfs_dir_entry
*old_de
;
360 struct nilfs_transaction_info ti
;
363 if (flags
& ~RENAME_NOREPLACE
)
366 err
= nilfs_transaction_begin(old_dir
->i_sb
, &ti
, 1);
371 old_de
= nilfs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
375 if (S_ISDIR(old_inode
->i_mode
)) {
377 dir_de
= nilfs_dotdot(old_inode
, &dir_page
);
383 struct page
*new_page
;
384 struct nilfs_dir_entry
*new_de
;
387 if (dir_de
&& !nilfs_empty_dir(new_inode
))
391 new_de
= nilfs_find_entry(new_dir
, &new_dentry
->d_name
, &new_page
);
394 nilfs_set_link(new_dir
, new_de
, new_page
, old_inode
);
395 nilfs_mark_inode_dirty(new_dir
);
396 new_inode
->i_ctime
= current_time(new_inode
);
398 drop_nlink(new_inode
);
399 drop_nlink(new_inode
);
400 nilfs_mark_inode_dirty(new_inode
);
402 err
= nilfs_add_link(new_dentry
, old_inode
);
407 nilfs_mark_inode_dirty(new_dir
);
412 * Like most other Unix systems, set the ctime for inodes on a
415 old_inode
->i_ctime
= current_time(old_inode
);
417 nilfs_delete_entry(old_de
, old_page
);
420 nilfs_set_link(old_inode
, dir_de
, dir_page
, new_dir
);
423 nilfs_mark_inode_dirty(old_dir
);
424 nilfs_mark_inode_dirty(old_inode
);
426 err
= nilfs_transaction_commit(old_dir
->i_sb
);
438 nilfs_transaction_abort(old_dir
->i_sb
);
445 static struct dentry
*nilfs_get_parent(struct dentry
*child
)
449 struct qstr dotdot
= QSTR_INIT("..", 2);
450 struct nilfs_root
*root
;
452 ino
= nilfs_inode_by_name(d_inode(child
), &dotdot
);
454 return ERR_PTR(-ENOENT
);
456 root
= NILFS_I(d_inode(child
))->i_root
;
458 inode
= nilfs_iget(child
->d_sb
, root
, ino
);
460 return ERR_CAST(inode
);
462 return d_obtain_alias(inode
);
465 static struct dentry
*nilfs_get_dentry(struct super_block
*sb
, u64 cno
,
468 struct nilfs_root
*root
;
471 if (ino
< NILFS_FIRST_INO(sb
) && ino
!= NILFS_ROOT_INO
)
472 return ERR_PTR(-ESTALE
);
474 root
= nilfs_lookup_root(sb
->s_fs_info
, cno
);
476 return ERR_PTR(-ESTALE
);
478 inode
= nilfs_iget(sb
, root
, ino
);
479 nilfs_put_root(root
);
482 return ERR_CAST(inode
);
483 if (gen
&& inode
->i_generation
!= gen
) {
485 return ERR_PTR(-ESTALE
);
487 return d_obtain_alias(inode
);
490 static struct dentry
*nilfs_fh_to_dentry(struct super_block
*sb
, struct fid
*fh
,
491 int fh_len
, int fh_type
)
493 struct nilfs_fid
*fid
= (struct nilfs_fid
*)fh
;
495 if (fh_len
< NILFS_FID_SIZE_NON_CONNECTABLE
||
496 (fh_type
!= FILEID_NILFS_WITH_PARENT
&&
497 fh_type
!= FILEID_NILFS_WITHOUT_PARENT
))
500 return nilfs_get_dentry(sb
, fid
->cno
, fid
->ino
, fid
->gen
);
503 static struct dentry
*nilfs_fh_to_parent(struct super_block
*sb
, struct fid
*fh
,
504 int fh_len
, int fh_type
)
506 struct nilfs_fid
*fid
= (struct nilfs_fid
*)fh
;
508 if (fh_len
< NILFS_FID_SIZE_CONNECTABLE
||
509 fh_type
!= FILEID_NILFS_WITH_PARENT
)
512 return nilfs_get_dentry(sb
, fid
->cno
, fid
->parent_ino
, fid
->parent_gen
);
515 static int nilfs_encode_fh(struct inode
*inode
, __u32
*fh
, int *lenp
,
516 struct inode
*parent
)
518 struct nilfs_fid
*fid
= (struct nilfs_fid
*)fh
;
519 struct nilfs_root
*root
= NILFS_I(inode
)->i_root
;
522 if (parent
&& *lenp
< NILFS_FID_SIZE_CONNECTABLE
) {
523 *lenp
= NILFS_FID_SIZE_CONNECTABLE
;
524 return FILEID_INVALID
;
526 if (*lenp
< NILFS_FID_SIZE_NON_CONNECTABLE
) {
527 *lenp
= NILFS_FID_SIZE_NON_CONNECTABLE
;
528 return FILEID_INVALID
;
531 fid
->cno
= root
->cno
;
532 fid
->ino
= inode
->i_ino
;
533 fid
->gen
= inode
->i_generation
;
536 fid
->parent_ino
= parent
->i_ino
;
537 fid
->parent_gen
= parent
->i_generation
;
538 type
= FILEID_NILFS_WITH_PARENT
;
539 *lenp
= NILFS_FID_SIZE_CONNECTABLE
;
541 type
= FILEID_NILFS_WITHOUT_PARENT
;
542 *lenp
= NILFS_FID_SIZE_NON_CONNECTABLE
;
548 const struct inode_operations nilfs_dir_inode_operations
= {
549 .create
= nilfs_create
,
550 .lookup
= nilfs_lookup
,
552 .unlink
= nilfs_unlink
,
553 .symlink
= nilfs_symlink
,
554 .mkdir
= nilfs_mkdir
,
555 .rmdir
= nilfs_rmdir
,
556 .mknod
= nilfs_mknod
,
557 .rename
= nilfs_rename
,
558 .setattr
= nilfs_setattr
,
559 .permission
= nilfs_permission
,
560 .fiemap
= nilfs_fiemap
,
563 const struct inode_operations nilfs_special_inode_operations
= {
564 .setattr
= nilfs_setattr
,
565 .permission
= nilfs_permission
,
568 const struct inode_operations nilfs_symlink_inode_operations
= {
569 .readlink
= generic_readlink
,
570 .get_link
= page_get_link
,
571 .permission
= nilfs_permission
,
574 const struct export_operations nilfs_export_ops
= {
575 .encode_fh
= nilfs_encode_fh
,
576 .fh_to_dentry
= nilfs_fh_to_dentry
,
577 .fh_to_parent
= nilfs_fh_to_parent
,
578 .get_parent
= nilfs_get_parent
,