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 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Modified for NILFS by Amagai Yoshiji <amagai@osrg.net>,
21 * Ryusuke Konishi <ryusuke@osrg.net>
24 * linux/fs/ext2/namei.c
26 * Copyright (C) 1992, 1993, 1994, 1995
27 * Remy Card (card@masi.ibp.fr)
28 * Laboratoire MASI - Institut Blaise Pascal
29 * Universite Pierre et Marie Curie (Paris VI)
33 * linux/fs/minix/namei.c
35 * Copyright (C) 1991, 1992 Linus Torvalds
37 * Big-endian to little-endian byte-swapping/bitmaps by
38 * David S. Miller (davem@caip.rutgers.edu), 1995
41 #include <linux/pagemap.h>
45 #define NILFS_FID_SIZE_NON_CONNECTABLE \
46 (offsetof(struct nilfs_fid, parent_gen) / 4)
47 #define NILFS_FID_SIZE_CONNECTABLE (sizeof(struct nilfs_fid) / 4)
49 static inline int nilfs_add_nondir(struct dentry
*dentry
, struct inode
*inode
)
51 int err
= nilfs_add_link(dentry
, inode
);
53 d_instantiate(dentry
, inode
);
56 inode_dec_link_count(inode
);
65 static struct dentry
*
66 nilfs_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
71 if (dentry
->d_name
.len
> NILFS_NAME_LEN
)
72 return ERR_PTR(-ENAMETOOLONG
);
74 ino
= nilfs_inode_by_name(dir
, &dentry
->d_name
);
75 inode
= ino
? nilfs_iget(dir
->i_sb
, NILFS_I(dir
)->i_root
, ino
) : NULL
;
76 return d_splice_alias(inode
, dentry
);
80 * By the time this is called, we already have created
81 * the directory cache entry for the new file, but it
82 * is so far negative - it has no inode.
84 * If the create succeeds, we fill in the inode information
85 * with d_instantiate().
87 static int nilfs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
91 struct nilfs_transaction_info ti
;
94 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
97 inode
= nilfs_new_inode(dir
, mode
);
100 inode
->i_op
= &nilfs_file_inode_operations
;
101 inode
->i_fop
= &nilfs_file_operations
;
102 inode
->i_mapping
->a_ops
= &nilfs_aops
;
103 nilfs_mark_inode_dirty(inode
);
104 err
= nilfs_add_nondir(dentry
, inode
);
107 err
= nilfs_transaction_commit(dir
->i_sb
);
109 nilfs_transaction_abort(dir
->i_sb
);
115 nilfs_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
, dev_t rdev
)
118 struct nilfs_transaction_info ti
;
121 if (!new_valid_dev(rdev
))
124 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
127 inode
= nilfs_new_inode(dir
, mode
);
128 err
= PTR_ERR(inode
);
129 if (!IS_ERR(inode
)) {
130 init_special_inode(inode
, inode
->i_mode
, rdev
);
131 nilfs_mark_inode_dirty(inode
);
132 err
= nilfs_add_nondir(dentry
, inode
);
135 err
= nilfs_transaction_commit(dir
->i_sb
);
137 nilfs_transaction_abort(dir
->i_sb
);
142 static int nilfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
145 struct nilfs_transaction_info ti
;
146 struct super_block
*sb
= dir
->i_sb
;
147 unsigned l
= strlen(symname
)+1;
151 if (l
> sb
->s_blocksize
)
152 return -ENAMETOOLONG
;
154 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
158 inode
= nilfs_new_inode(dir
, S_IFLNK
| S_IRWXUGO
);
159 err
= PTR_ERR(inode
);
164 inode
->i_op
= &nilfs_symlink_inode_operations
;
165 inode
->i_mapping
->a_ops
= &nilfs_aops
;
166 err
= page_symlink(inode
, symname
, l
);
170 /* mark_inode_dirty(inode); */
171 /* page_symlink() do this */
173 err
= nilfs_add_nondir(dentry
, inode
);
176 err
= nilfs_transaction_commit(dir
->i_sb
);
178 nilfs_transaction_abort(dir
->i_sb
);
184 nilfs_mark_inode_dirty(inode
);
189 static int nilfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
190 struct dentry
*dentry
)
192 struct inode
*inode
= old_dentry
->d_inode
;
193 struct nilfs_transaction_info ti
;
196 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 1);
200 inode
->i_ctime
= CURRENT_TIME
;
201 inode_inc_link_count(inode
);
204 err
= nilfs_add_nondir(dentry
, inode
);
206 err
= nilfs_transaction_commit(dir
->i_sb
);
208 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
);
248 err
= nilfs_transaction_commit(dir
->i_sb
);
250 nilfs_transaction_abort(dir
->i_sb
);
257 nilfs_mark_inode_dirty(inode
);
261 nilfs_mark_inode_dirty(dir
);
265 static int nilfs_do_unlink(struct inode
*dir
, struct dentry
*dentry
)
268 struct nilfs_dir_entry
*de
;
273 de
= nilfs_find_entry(dir
, &dentry
->d_name
, &page
);
277 inode
= dentry
->d_inode
;
279 if (le64_to_cpu(de
->inode
) != inode
->i_ino
)
282 if (!inode
->i_nlink
) {
283 nilfs_warning(inode
->i_sb
, __func__
,
284 "deleting nonexistent file (%lu), %d\n",
285 inode
->i_ino
, inode
->i_nlink
);
288 err
= nilfs_delete_entry(de
, page
);
292 inode
->i_ctime
= dir
->i_ctime
;
299 static int nilfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
301 struct nilfs_transaction_info ti
;
304 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 0);
308 err
= nilfs_do_unlink(dir
, dentry
);
311 nilfs_mark_inode_dirty(dir
);
312 nilfs_mark_inode_dirty(dentry
->d_inode
);
313 err
= nilfs_transaction_commit(dir
->i_sb
);
315 nilfs_transaction_abort(dir
->i_sb
);
320 static int nilfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
322 struct inode
*inode
= dentry
->d_inode
;
323 struct nilfs_transaction_info ti
;
326 err
= nilfs_transaction_begin(dir
->i_sb
, &ti
, 0);
331 if (nilfs_empty_dir(inode
)) {
332 err
= nilfs_do_unlink(dir
, dentry
);
336 nilfs_mark_inode_dirty(inode
);
338 nilfs_mark_inode_dirty(dir
);
342 err
= nilfs_transaction_commit(dir
->i_sb
);
344 nilfs_transaction_abort(dir
->i_sb
);
349 static int nilfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
350 struct inode
*new_dir
, struct dentry
*new_dentry
)
352 struct inode
*old_inode
= old_dentry
->d_inode
;
353 struct inode
*new_inode
= new_dentry
->d_inode
;
354 struct page
*dir_page
= NULL
;
355 struct nilfs_dir_entry
*dir_de
= NULL
;
356 struct page
*old_page
;
357 struct nilfs_dir_entry
*old_de
;
358 struct nilfs_transaction_info ti
;
361 err
= nilfs_transaction_begin(old_dir
->i_sb
, &ti
, 1);
366 old_de
= nilfs_find_entry(old_dir
, &old_dentry
->d_name
, &old_page
);
370 if (S_ISDIR(old_inode
->i_mode
)) {
372 dir_de
= nilfs_dotdot(old_inode
, &dir_page
);
378 struct page
*new_page
;
379 struct nilfs_dir_entry
*new_de
;
382 if (dir_de
&& !nilfs_empty_dir(new_inode
))
386 new_de
= nilfs_find_entry(new_dir
, &new_dentry
->d_name
, &new_page
);
389 nilfs_set_link(new_dir
, new_de
, new_page
, old_inode
);
390 nilfs_mark_inode_dirty(new_dir
);
391 new_inode
->i_ctime
= CURRENT_TIME
;
393 drop_nlink(new_inode
);
394 drop_nlink(new_inode
);
395 nilfs_mark_inode_dirty(new_inode
);
397 err
= nilfs_add_link(new_dentry
, old_inode
);
402 nilfs_mark_inode_dirty(new_dir
);
407 * Like most other Unix systems, set the ctime for inodes on a
410 old_inode
->i_ctime
= CURRENT_TIME
;
412 nilfs_delete_entry(old_de
, old_page
);
415 nilfs_set_link(old_inode
, dir_de
, dir_page
, new_dir
);
418 nilfs_mark_inode_dirty(old_dir
);
419 nilfs_mark_inode_dirty(old_inode
);
421 err
= nilfs_transaction_commit(old_dir
->i_sb
);
427 page_cache_release(dir_page
);
431 page_cache_release(old_page
);
433 nilfs_transaction_abort(old_dir
->i_sb
);
440 static struct dentry
*nilfs_get_parent(struct dentry
*child
)
444 struct qstr dotdot
= {.name
= "..", .len
= 2};
445 struct nilfs_root
*root
;
447 ino
= nilfs_inode_by_name(child
->d_inode
, &dotdot
);
449 return ERR_PTR(-ENOENT
);
451 root
= NILFS_I(child
->d_inode
)->i_root
;
453 inode
= nilfs_iget(child
->d_inode
->i_sb
, root
, ino
);
455 return ERR_CAST(inode
);
457 return d_obtain_alias(inode
);
460 static struct dentry
*nilfs_get_dentry(struct super_block
*sb
, u64 cno
,
463 struct nilfs_root
*root
;
466 if (ino
< NILFS_FIRST_INO(sb
) && ino
!= NILFS_ROOT_INO
)
467 return ERR_PTR(-ESTALE
);
469 root
= nilfs_lookup_root(sb
->s_fs_info
, cno
);
471 return ERR_PTR(-ESTALE
);
473 inode
= nilfs_iget(sb
, root
, ino
);
474 nilfs_put_root(root
);
477 return ERR_CAST(inode
);
478 if (gen
&& inode
->i_generation
!= gen
) {
480 return ERR_PTR(-ESTALE
);
482 return d_obtain_alias(inode
);
485 static struct dentry
*nilfs_fh_to_dentry(struct super_block
*sb
, struct fid
*fh
,
486 int fh_len
, int fh_type
)
488 struct nilfs_fid
*fid
= (struct nilfs_fid
*)fh
;
490 if ((fh_len
!= NILFS_FID_SIZE_NON_CONNECTABLE
&&
491 fh_len
!= NILFS_FID_SIZE_CONNECTABLE
) ||
492 (fh_type
!= FILEID_NILFS_WITH_PARENT
&&
493 fh_type
!= FILEID_NILFS_WITHOUT_PARENT
))
496 return nilfs_get_dentry(sb
, fid
->cno
, fid
->ino
, fid
->gen
);
499 static struct dentry
*nilfs_fh_to_parent(struct super_block
*sb
, struct fid
*fh
,
500 int fh_len
, int fh_type
)
502 struct nilfs_fid
*fid
= (struct nilfs_fid
*)fh
;
504 if (fh_len
!= NILFS_FID_SIZE_CONNECTABLE
||
505 fh_type
!= FILEID_NILFS_WITH_PARENT
)
508 return nilfs_get_dentry(sb
, fid
->cno
, fid
->parent_ino
, fid
->parent_gen
);
511 static int nilfs_encode_fh(struct dentry
*dentry
, __u32
*fh
, int *lenp
,
514 struct nilfs_fid
*fid
= (struct nilfs_fid
*)fh
;
515 struct inode
*inode
= dentry
->d_inode
;
516 struct nilfs_root
*root
= NILFS_I(inode
)->i_root
;
519 if (*lenp
< NILFS_FID_SIZE_NON_CONNECTABLE
||
520 (connectable
&& *lenp
< NILFS_FID_SIZE_CONNECTABLE
))
523 fid
->cno
= root
->cno
;
524 fid
->ino
= inode
->i_ino
;
525 fid
->gen
= inode
->i_generation
;
527 if (connectable
&& !S_ISDIR(inode
->i_mode
)) {
528 struct inode
*parent
;
530 spin_lock(&dentry
->d_lock
);
531 parent
= dentry
->d_parent
->d_inode
;
532 fid
->parent_ino
= parent
->i_ino
;
533 fid
->parent_gen
= parent
->i_generation
;
534 spin_unlock(&dentry
->d_lock
);
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 .follow_link
= page_follow_link_light
,
569 .put_link
= page_put_link
,
570 .permission
= nilfs_permission
,
573 const struct export_operations nilfs_export_ops
= {
574 .encode_fh
= nilfs_encode_fh
,
575 .fh_to_dentry
= nilfs_fh_to_dentry
,
576 .fh_to_parent
= nilfs_fh_to_parent
,
577 .get_parent
= nilfs_get_parent
,