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
);
48 d_instantiate(dentry
, inode
);
49 unlock_new_inode(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 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
;
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(dentry
, inode
);
246 unlock_new_inode(inode
);
249 err
= nilfs_transaction_commit(dir
->i_sb
);
251 nilfs_transaction_abort(dir
->i_sb
);
258 nilfs_mark_inode_dirty(inode
);
259 unlock_new_inode(inode
);
263 nilfs_mark_inode_dirty(dir
);
267 static int nilfs_do_unlink(struct inode
*dir
, struct dentry
*dentry
)
270 struct nilfs_dir_entry
*de
;
275 de
= nilfs_find_entry(dir
, &dentry
->d_name
, &page
);
279 inode
= d_inode(dentry
);
281 if (le64_to_cpu(de
->inode
) != inode
->i_ino
)
284 if (!inode
->i_nlink
) {
285 nilfs_warning(inode
->i_sb
, __func__
,
286 "deleting nonexistent file (%lu), %d\n",
287 inode
->i_ino
, inode
->i_nlink
);
290 err
= nilfs_delete_entry(de
, page
);
294 inode
->i_ctime
= dir
->i_ctime
;
301 static int nilfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
303 struct nilfs_transaction_info ti
;
306 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 0);
310 err
= nilfs_do_unlink(dir
, dentry
);
313 nilfs_mark_inode_dirty(dir
);
314 nilfs_mark_inode_dirty(d_inode(dentry
));
315 err
= nilfs_transaction_commit(dir
->i_sb
);
317 nilfs_transaction_abort(dir
->i_sb
);
322 static int nilfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
324 struct inode
*inode
= d_inode(dentry
);
325 struct nilfs_transaction_info ti
;
328 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 0);
333 if (nilfs_empty_dir(inode
)) {
334 err
= nilfs_do_unlink(dir
, dentry
);
338 nilfs_mark_inode_dirty(inode
);
340 nilfs_mark_inode_dirty(dir
);
344 err
= nilfs_transaction_commit(dir
->i_sb
);
346 nilfs_transaction_abort(dir
->i_sb
);
351 static int nilfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
352 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 err
= nilfs_transaction_begin(old_dir
->i_sb
, &ti
, 1);
368 old_de
= nilfs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
372 if (S_ISDIR(old_inode
->i_mode
)) {
374 dir_de
= nilfs_dotdot(old_inode
, &dir_page
);
380 struct page
*new_page
;
381 struct nilfs_dir_entry
*new_de
;
384 if (dir_de
&& !nilfs_empty_dir(new_inode
))
388 new_de
= nilfs_find_entry(new_dir
, &new_dentry
->d_name
, &new_page
);
391 nilfs_set_link(new_dir
, new_de
, new_page
, old_inode
);
392 nilfs_mark_inode_dirty(new_dir
);
393 new_inode
->i_ctime
= CURRENT_TIME
;
395 drop_nlink(new_inode
);
396 drop_nlink(new_inode
);
397 nilfs_mark_inode_dirty(new_inode
);
399 err
= nilfs_add_link(new_dentry
, old_inode
);
404 nilfs_mark_inode_dirty(new_dir
);
409 * Like most other Unix systems, set the ctime for inodes on a
412 old_inode
->i_ctime
= CURRENT_TIME
;
414 nilfs_delete_entry(old_de
, old_page
);
417 nilfs_set_link(old_inode
, dir_de
, dir_page
, new_dir
);
420 nilfs_mark_inode_dirty(old_dir
);
421 nilfs_mark_inode_dirty(old_inode
);
423 err
= nilfs_transaction_commit(old_dir
->i_sb
);
435 nilfs_transaction_abort(old_dir
->i_sb
);
442 static struct dentry
*nilfs_get_parent(struct dentry
*child
)
446 struct qstr dotdot
= QSTR_INIT("..", 2);
447 struct nilfs_root
*root
;
449 ino
= nilfs_inode_by_name(d_inode(child
), &dotdot
);
451 return ERR_PTR(-ENOENT
);
453 root
= NILFS_I(d_inode(child
))->i_root
;
455 inode
= nilfs_iget(child
->d_sb
, root
, ino
);
457 return ERR_CAST(inode
);
459 return d_obtain_alias(inode
);
462 static struct dentry
*nilfs_get_dentry(struct super_block
*sb
, u64 cno
,
465 struct nilfs_root
*root
;
468 if (ino
< NILFS_FIRST_INO(sb
) && ino
!= NILFS_ROOT_INO
)
469 return ERR_PTR(-ESTALE
);
471 root
= nilfs_lookup_root(sb
->s_fs_info
, cno
);
473 return ERR_PTR(-ESTALE
);
475 inode
= nilfs_iget(sb
, root
, ino
);
476 nilfs_put_root(root
);
479 return ERR_CAST(inode
);
480 if (gen
&& inode
->i_generation
!= gen
) {
482 return ERR_PTR(-ESTALE
);
484 return d_obtain_alias(inode
);
487 static struct dentry
*nilfs_fh_to_dentry(struct super_block
*sb
, struct fid
*fh
,
488 int fh_len
, int fh_type
)
490 struct nilfs_fid
*fid
= (struct nilfs_fid
*)fh
;
492 if (fh_len
< NILFS_FID_SIZE_NON_CONNECTABLE
||
493 (fh_type
!= FILEID_NILFS_WITH_PARENT
&&
494 fh_type
!= FILEID_NILFS_WITHOUT_PARENT
))
497 return nilfs_get_dentry(sb
, fid
->cno
, fid
->ino
, fid
->gen
);
500 static struct dentry
*nilfs_fh_to_parent(struct super_block
*sb
, struct fid
*fh
,
501 int fh_len
, int fh_type
)
503 struct nilfs_fid
*fid
= (struct nilfs_fid
*)fh
;
505 if (fh_len
< NILFS_FID_SIZE_CONNECTABLE
||
506 fh_type
!= FILEID_NILFS_WITH_PARENT
)
509 return nilfs_get_dentry(sb
, fid
->cno
, fid
->parent_ino
, fid
->parent_gen
);
512 static int nilfs_encode_fh(struct inode
*inode
, __u32
*fh
, int *lenp
,
513 struct inode
*parent
)
515 struct nilfs_fid
*fid
= (struct nilfs_fid
*)fh
;
516 struct nilfs_root
*root
= NILFS_I(inode
)->i_root
;
519 if (parent
&& *lenp
< NILFS_FID_SIZE_CONNECTABLE
) {
520 *lenp
= NILFS_FID_SIZE_CONNECTABLE
;
521 return FILEID_INVALID
;
523 if (*lenp
< NILFS_FID_SIZE_NON_CONNECTABLE
) {
524 *lenp
= NILFS_FID_SIZE_NON_CONNECTABLE
;
525 return FILEID_INVALID
;
528 fid
->cno
= root
->cno
;
529 fid
->ino
= inode
->i_ino
;
530 fid
->gen
= inode
->i_generation
;
533 fid
->parent_ino
= parent
->i_ino
;
534 fid
->parent_gen
= parent
->i_generation
;
535 type
= FILEID_NILFS_WITH_PARENT
;
536 *lenp
= NILFS_FID_SIZE_CONNECTABLE
;
538 type
= FILEID_NILFS_WITHOUT_PARENT
;
539 *lenp
= NILFS_FID_SIZE_NON_CONNECTABLE
;
545 const struct inode_operations nilfs_dir_inode_operations
= {
546 .create
= nilfs_create
,
547 .lookup
= nilfs_lookup
,
549 .unlink
= nilfs_unlink
,
550 .symlink
= nilfs_symlink
,
551 .mkdir
= nilfs_mkdir
,
552 .rmdir
= nilfs_rmdir
,
553 .mknod
= nilfs_mknod
,
554 .rename
= nilfs_rename
,
555 .setattr
= nilfs_setattr
,
556 .permission
= nilfs_permission
,
557 .fiemap
= nilfs_fiemap
,
560 const struct inode_operations nilfs_special_inode_operations
= {
561 .setattr
= nilfs_setattr
,
562 .permission
= nilfs_permission
,
565 const struct inode_operations nilfs_symlink_inode_operations
= {
566 .readlink
= generic_readlink
,
567 .get_link
= page_get_link
,
568 .permission
= nilfs_permission
,
571 const struct export_operations nilfs_export_ops
= {
572 .encode_fh
= nilfs_encode_fh
,
573 .fh_to_dentry
= nilfs_fh_to_dentry
,
574 .fh_to_parent
= nilfs_fh_to_parent
,
575 .get_parent
= nilfs_get_parent
,