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(dentry
, inode
);
50 unlock_new_inode(inode
);
53 inode_dec_link_count(inode
);
54 unlock_new_inode(inode
);
63 static struct dentry
*
64 nilfs_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
69 if (dentry
->d_name
.len
> NILFS_NAME_LEN
)
70 return ERR_PTR(-ENAMETOOLONG
);
72 ino
= nilfs_inode_by_name(dir
, &dentry
->d_name
);
73 inode
= ino
? nilfs_iget(dir
->i_sb
, NILFS_I(dir
)->i_root
, ino
) : NULL
;
74 return d_splice_alias(inode
, dentry
);
78 * By the time this is called, we already have created
79 * the directory cache entry for the new file, but it
80 * is so far negative - it has no inode.
82 * If the create succeeds, we fill in the inode information
83 * with d_instantiate().
85 static int nilfs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
89 struct nilfs_transaction_info ti
;
92 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
95 inode
= nilfs_new_inode(dir
, mode
);
98 inode
->i_op
= &nilfs_file_inode_operations
;
99 inode
->i_fop
= &nilfs_file_operations
;
100 inode
->i_mapping
->a_ops
= &nilfs_aops
;
101 nilfs_mark_inode_dirty(inode
);
102 err
= nilfs_add_nondir(dentry
, inode
);
105 err
= nilfs_transaction_commit(dir
->i_sb
);
107 nilfs_transaction_abort(dir
->i_sb
);
113 nilfs_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
, dev_t rdev
)
116 struct nilfs_transaction_info ti
;
119 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
122 inode
= nilfs_new_inode(dir
, mode
);
123 err
= PTR_ERR(inode
);
124 if (!IS_ERR(inode
)) {
125 init_special_inode(inode
, inode
->i_mode
, rdev
);
126 nilfs_mark_inode_dirty(inode
);
127 err
= nilfs_add_nondir(dentry
, inode
);
130 err
= nilfs_transaction_commit(dir
->i_sb
);
132 nilfs_transaction_abort(dir
->i_sb
);
137 static int nilfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
140 struct nilfs_transaction_info ti
;
141 struct super_block
*sb
= dir
->i_sb
;
142 unsigned int l
= strlen(symname
) + 1;
146 if (l
> sb
->s_blocksize
)
147 return -ENAMETOOLONG
;
149 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
153 inode
= nilfs_new_inode(dir
, S_IFLNK
| S_IRWXUGO
);
154 err
= PTR_ERR(inode
);
159 inode
->i_op
= &nilfs_symlink_inode_operations
;
160 inode_nohighmem(inode
);
161 inode
->i_mapping
->a_ops
= &nilfs_aops
;
162 err
= page_symlink(inode
, symname
, l
);
166 /* mark_inode_dirty(inode); */
167 /* page_symlink() do this */
169 err
= nilfs_add_nondir(dentry
, inode
);
172 err
= nilfs_transaction_commit(dir
->i_sb
);
174 nilfs_transaction_abort(dir
->i_sb
);
180 nilfs_mark_inode_dirty(inode
);
181 unlock_new_inode(inode
);
186 static int nilfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
187 struct dentry
*dentry
)
189 struct inode
*inode
= d_inode(old_dentry
);
190 struct nilfs_transaction_info ti
;
193 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
197 inode
->i_ctime
= CURRENT_TIME
;
198 inode_inc_link_count(inode
);
201 err
= nilfs_add_link(dentry
, inode
);
203 d_instantiate(dentry
, inode
);
204 err
= nilfs_transaction_commit(dir
->i_sb
);
206 inode_dec_link_count(inode
);
208 nilfs_transaction_abort(dir
->i_sb
);
214 static int nilfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
217 struct nilfs_transaction_info ti
;
220 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
226 inode
= nilfs_new_inode(dir
, S_IFDIR
| mode
);
227 err
= PTR_ERR(inode
);
231 inode
->i_op
= &nilfs_dir_inode_operations
;
232 inode
->i_fop
= &nilfs_dir_operations
;
233 inode
->i_mapping
->a_ops
= &nilfs_aops
;
237 err
= nilfs_make_empty(inode
, dir
);
241 err
= nilfs_add_link(dentry
, inode
);
245 nilfs_mark_inode_dirty(inode
);
246 d_instantiate(dentry
, inode
);
247 unlock_new_inode(inode
);
250 err
= nilfs_transaction_commit(dir
->i_sb
);
252 nilfs_transaction_abort(dir
->i_sb
);
259 nilfs_mark_inode_dirty(inode
);
260 unlock_new_inode(inode
);
264 nilfs_mark_inode_dirty(dir
);
268 static int nilfs_do_unlink(struct inode
*dir
, struct dentry
*dentry
)
271 struct nilfs_dir_entry
*de
;
276 de
= nilfs_find_entry(dir
, &dentry
->d_name
, &page
);
280 inode
= d_inode(dentry
);
282 if (le64_to_cpu(de
->inode
) != inode
->i_ino
)
285 if (!inode
->i_nlink
) {
286 nilfs_msg(inode
->i_sb
, KERN_WARNING
,
287 "deleting nonexistent file (ino=%lu), %d",
288 inode
->i_ino
, inode
->i_nlink
);
291 err
= nilfs_delete_entry(de
, page
);
295 inode
->i_ctime
= dir
->i_ctime
;
302 static int nilfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
304 struct nilfs_transaction_info ti
;
307 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 0);
311 err
= nilfs_do_unlink(dir
, dentry
);
314 nilfs_mark_inode_dirty(dir
);
315 nilfs_mark_inode_dirty(d_inode(dentry
));
316 err
= nilfs_transaction_commit(dir
->i_sb
);
318 nilfs_transaction_abort(dir
->i_sb
);
323 static int nilfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
325 struct inode
*inode
= d_inode(dentry
);
326 struct nilfs_transaction_info ti
;
329 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 0);
334 if (nilfs_empty_dir(inode
)) {
335 err
= nilfs_do_unlink(dir
, dentry
);
339 nilfs_mark_inode_dirty(inode
);
341 nilfs_mark_inode_dirty(dir
);
345 err
= nilfs_transaction_commit(dir
->i_sb
);
347 nilfs_transaction_abort(dir
->i_sb
);
352 static int nilfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
353 struct inode
*new_dir
, struct dentry
*new_dentry
)
355 struct inode
*old_inode
= d_inode(old_dentry
);
356 struct inode
*new_inode
= d_inode(new_dentry
);
357 struct page
*dir_page
= NULL
;
358 struct nilfs_dir_entry
*dir_de
= NULL
;
359 struct page
*old_page
;
360 struct nilfs_dir_entry
*old_de
;
361 struct nilfs_transaction_info ti
;
364 err
= nilfs_transaction_begin(old_dir
->i_sb
, &ti
, 1);
369 old_de
= nilfs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
373 if (S_ISDIR(old_inode
->i_mode
)) {
375 dir_de
= nilfs_dotdot(old_inode
, &dir_page
);
381 struct page
*new_page
;
382 struct nilfs_dir_entry
*new_de
;
385 if (dir_de
&& !nilfs_empty_dir(new_inode
))
389 new_de
= nilfs_find_entry(new_dir
, &new_dentry
->d_name
, &new_page
);
392 nilfs_set_link(new_dir
, new_de
, new_page
, old_inode
);
393 nilfs_mark_inode_dirty(new_dir
);
394 new_inode
->i_ctime
= CURRENT_TIME
;
396 drop_nlink(new_inode
);
397 drop_nlink(new_inode
);
398 nilfs_mark_inode_dirty(new_inode
);
400 err
= nilfs_add_link(new_dentry
, old_inode
);
405 nilfs_mark_inode_dirty(new_dir
);
410 * Like most other Unix systems, set the ctime for inodes on a
413 old_inode
->i_ctime
= CURRENT_TIME
;
415 nilfs_delete_entry(old_de
, old_page
);
418 nilfs_set_link(old_inode
, dir_de
, dir_page
, new_dir
);
421 nilfs_mark_inode_dirty(old_dir
);
422 nilfs_mark_inode_dirty(old_inode
);
424 err
= nilfs_transaction_commit(old_dir
->i_sb
);
436 nilfs_transaction_abort(old_dir
->i_sb
);
443 static struct dentry
*nilfs_get_parent(struct dentry
*child
)
447 struct qstr dotdot
= QSTR_INIT("..", 2);
448 struct nilfs_root
*root
;
450 ino
= nilfs_inode_by_name(d_inode(child
), &dotdot
);
452 return ERR_PTR(-ENOENT
);
454 root
= NILFS_I(d_inode(child
))->i_root
;
456 inode
= nilfs_iget(child
->d_sb
, root
, ino
);
458 return ERR_CAST(inode
);
460 return d_obtain_alias(inode
);
463 static struct dentry
*nilfs_get_dentry(struct super_block
*sb
, u64 cno
,
466 struct nilfs_root
*root
;
469 if (ino
< NILFS_FIRST_INO(sb
) && ino
!= NILFS_ROOT_INO
)
470 return ERR_PTR(-ESTALE
);
472 root
= nilfs_lookup_root(sb
->s_fs_info
, cno
);
474 return ERR_PTR(-ESTALE
);
476 inode
= nilfs_iget(sb
, root
, ino
);
477 nilfs_put_root(root
);
480 return ERR_CAST(inode
);
481 if (gen
&& inode
->i_generation
!= gen
) {
483 return ERR_PTR(-ESTALE
);
485 return d_obtain_alias(inode
);
488 static struct dentry
*nilfs_fh_to_dentry(struct super_block
*sb
, struct fid
*fh
,
489 int fh_len
, int fh_type
)
491 struct nilfs_fid
*fid
= (struct nilfs_fid
*)fh
;
493 if (fh_len
< NILFS_FID_SIZE_NON_CONNECTABLE
||
494 (fh_type
!= FILEID_NILFS_WITH_PARENT
&&
495 fh_type
!= FILEID_NILFS_WITHOUT_PARENT
))
498 return nilfs_get_dentry(sb
, fid
->cno
, fid
->ino
, fid
->gen
);
501 static struct dentry
*nilfs_fh_to_parent(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_CONNECTABLE
||
507 fh_type
!= FILEID_NILFS_WITH_PARENT
)
510 return nilfs_get_dentry(sb
, fid
->cno
, fid
->parent_ino
, fid
->parent_gen
);
513 static int nilfs_encode_fh(struct inode
*inode
, __u32
*fh
, int *lenp
,
514 struct inode
*parent
)
516 struct nilfs_fid
*fid
= (struct nilfs_fid
*)fh
;
517 struct nilfs_root
*root
= NILFS_I(inode
)->i_root
;
520 if (parent
&& *lenp
< NILFS_FID_SIZE_CONNECTABLE
) {
521 *lenp
= NILFS_FID_SIZE_CONNECTABLE
;
522 return FILEID_INVALID
;
524 if (*lenp
< NILFS_FID_SIZE_NON_CONNECTABLE
) {
525 *lenp
= NILFS_FID_SIZE_NON_CONNECTABLE
;
526 return FILEID_INVALID
;
529 fid
->cno
= root
->cno
;
530 fid
->ino
= inode
->i_ino
;
531 fid
->gen
= inode
->i_generation
;
534 fid
->parent_ino
= parent
->i_ino
;
535 fid
->parent_gen
= parent
->i_generation
;
536 type
= FILEID_NILFS_WITH_PARENT
;
537 *lenp
= NILFS_FID_SIZE_CONNECTABLE
;
539 type
= FILEID_NILFS_WITHOUT_PARENT
;
540 *lenp
= NILFS_FID_SIZE_NON_CONNECTABLE
;
546 const struct inode_operations nilfs_dir_inode_operations
= {
547 .create
= nilfs_create
,
548 .lookup
= nilfs_lookup
,
550 .unlink
= nilfs_unlink
,
551 .symlink
= nilfs_symlink
,
552 .mkdir
= nilfs_mkdir
,
553 .rmdir
= nilfs_rmdir
,
554 .mknod
= nilfs_mknod
,
555 .rename
= nilfs_rename
,
556 .setattr
= nilfs_setattr
,
557 .permission
= nilfs_permission
,
558 .fiemap
= nilfs_fiemap
,
561 const struct inode_operations nilfs_special_inode_operations
= {
562 .setattr
= nilfs_setattr
,
563 .permission
= nilfs_permission
,
566 const struct inode_operations nilfs_symlink_inode_operations
= {
567 .readlink
= generic_readlink
,
568 .get_link
= page_get_link
,
569 .permission
= nilfs_permission
,
572 const struct export_operations nilfs_export_ops
= {
573 .encode_fh
= nilfs_encode_fh
,
574 .fh_to_dentry
= nilfs_fh_to_dentry
,
575 .fh_to_parent
= nilfs_fh_to_parent
,
576 .get_parent
= nilfs_get_parent
,