2 * linux/fs/ext2/namei.c
4 * Rewrite to pagecache. Almost all code had been changed, so blame me
5 * if the things go wrong. Please, send bug reports to
6 * viro@parcelfarce.linux.theplanet.co.uk
8 * Stuff here is basically a glue between the VFS and generic UNIXish
9 * filesystem that keeps everything in pagecache. All knowledge of the
10 * directory layout is in fs/ext2/dir.c - it turned out to be easily separatable
11 * and it's easier to debug that way. In principle we might want to
12 * generalize that a bit and turn it into a library. Or not.
14 * The only non-static object here is ext2_dir_inode_operations.
16 * TODO: get rid of kmap() use, add readahead.
18 * Copyright (C) 1992, 1993, 1994, 1995
19 * Remy Card (card@masi.ibp.fr)
20 * Laboratoire MASI - Institut Blaise Pascal
21 * Universite Pierre et Marie Curie (Paris VI)
25 * linux/fs/minix/namei.c
27 * Copyright (C) 1991, 1992 Linus Torvalds
29 * Big-endian to little-endian byte-swapping/bitmaps by
30 * David S. Miller (davem@caip.rutgers.edu), 1995
33 #include <linux/pagemap.h>
34 #include <linux/quotaops.h>
40 static inline int ext2_add_nondir(struct dentry
*dentry
, struct inode
*inode
)
42 int err
= ext2_add_link(dentry
, inode
);
44 d_instantiate(dentry
, inode
);
45 unlock_new_inode(inode
);
48 inode_dec_link_count(inode
);
49 unlock_new_inode(inode
);
58 static struct dentry
*ext2_lookup(struct inode
* dir
, struct dentry
*dentry
, struct nameidata
*nd
)
63 if (dentry
->d_name
.len
> EXT2_NAME_LEN
)
64 return ERR_PTR(-ENAMETOOLONG
);
66 ino
= ext2_inode_by_name(dir
, &dentry
->d_name
);
69 inode
= ext2_iget(dir
->i_sb
, ino
);
70 if (unlikely(IS_ERR(inode
))) {
71 if (PTR_ERR(inode
) == -ESTALE
) {
72 ext2_error(dir
->i_sb
, __func__
,
73 "deleted inode referenced: %lu",
77 return ERR_CAST(inode
);
81 return d_splice_alias(inode
, dentry
);
84 struct dentry
*ext2_get_parent(struct dentry
*child
)
86 struct qstr dotdot
= {.name
= "..", .len
= 2};
87 unsigned long ino
= ext2_inode_by_name(child
->d_inode
, &dotdot
);
89 return ERR_PTR(-ENOENT
);
90 return d_obtain_alias(ext2_iget(child
->d_inode
->i_sb
, ino
));
94 * By the time this is called, we already have created
95 * the directory cache entry for the new file, but it
96 * is so far negative - it has no inode.
98 * If the create succeeds, we fill in the inode information
99 * with d_instantiate().
101 static int ext2_create (struct inode
* dir
, struct dentry
* dentry
, int mode
, struct nameidata
*nd
)
105 dquot_initialize(dir
);
107 inode
= ext2_new_inode(dir
, mode
);
109 return PTR_ERR(inode
);
111 inode
->i_op
= &ext2_file_inode_operations
;
112 if (ext2_use_xip(inode
->i_sb
)) {
113 inode
->i_mapping
->a_ops
= &ext2_aops_xip
;
114 inode
->i_fop
= &ext2_xip_file_operations
;
115 } else if (test_opt(inode
->i_sb
, NOBH
)) {
116 inode
->i_mapping
->a_ops
= &ext2_nobh_aops
;
117 inode
->i_fop
= &ext2_file_operations
;
119 inode
->i_mapping
->a_ops
= &ext2_aops
;
120 inode
->i_fop
= &ext2_file_operations
;
122 mark_inode_dirty(inode
);
123 return ext2_add_nondir(dentry
, inode
);
126 static int ext2_mknod (struct inode
* dir
, struct dentry
*dentry
, int mode
, dev_t rdev
)
128 struct inode
* inode
;
131 if (!new_valid_dev(rdev
))
134 dquot_initialize(dir
);
136 inode
= ext2_new_inode (dir
, mode
);
137 err
= PTR_ERR(inode
);
138 if (!IS_ERR(inode
)) {
139 init_special_inode(inode
, inode
->i_mode
, rdev
);
140 #ifdef CONFIG_EXT2_FS_XATTR
141 inode
->i_op
= &ext2_special_inode_operations
;
143 mark_inode_dirty(inode
);
144 err
= ext2_add_nondir(dentry
, inode
);
149 static int ext2_symlink (struct inode
* dir
, struct dentry
* dentry
,
150 const char * symname
)
152 struct super_block
* sb
= dir
->i_sb
;
153 int err
= -ENAMETOOLONG
;
154 unsigned l
= strlen(symname
)+1;
155 struct inode
* inode
;
157 if (l
> sb
->s_blocksize
)
160 dquot_initialize(dir
);
162 inode
= ext2_new_inode (dir
, S_IFLNK
| S_IRWXUGO
);
163 err
= PTR_ERR(inode
);
167 if (l
> sizeof (EXT2_I(inode
)->i_data
)) {
169 inode
->i_op
= &ext2_symlink_inode_operations
;
170 if (test_opt(inode
->i_sb
, NOBH
))
171 inode
->i_mapping
->a_ops
= &ext2_nobh_aops
;
173 inode
->i_mapping
->a_ops
= &ext2_aops
;
174 err
= page_symlink(inode
, symname
, l
);
179 inode
->i_op
= &ext2_fast_symlink_inode_operations
;
180 memcpy((char*)(EXT2_I(inode
)->i_data
),symname
,l
);
183 mark_inode_dirty(inode
);
185 err
= ext2_add_nondir(dentry
, inode
);
190 inode_dec_link_count(inode
);
191 unlock_new_inode(inode
);
196 static int ext2_link (struct dentry
* old_dentry
, struct inode
* dir
,
197 struct dentry
*dentry
)
199 struct inode
*inode
= old_dentry
->d_inode
;
202 if (inode
->i_nlink
>= EXT2_LINK_MAX
)
205 dquot_initialize(dir
);
207 inode
->i_ctime
= CURRENT_TIME_SEC
;
208 inode_inc_link_count(inode
);
211 err
= ext2_add_link(dentry
, inode
);
213 d_instantiate(dentry
, inode
);
216 inode_dec_link_count(inode
);
221 static int ext2_mkdir(struct inode
* dir
, struct dentry
* dentry
, int mode
)
223 struct inode
* inode
;
226 if (dir
->i_nlink
>= EXT2_LINK_MAX
)
229 dquot_initialize(dir
);
231 inode_inc_link_count(dir
);
233 inode
= ext2_new_inode (dir
, S_IFDIR
| mode
);
234 err
= PTR_ERR(inode
);
238 inode
->i_op
= &ext2_dir_inode_operations
;
239 inode
->i_fop
= &ext2_dir_operations
;
240 if (test_opt(inode
->i_sb
, NOBH
))
241 inode
->i_mapping
->a_ops
= &ext2_nobh_aops
;
243 inode
->i_mapping
->a_ops
= &ext2_aops
;
245 inode_inc_link_count(inode
);
247 err
= ext2_make_empty(inode
, dir
);
251 err
= ext2_add_link(dentry
, inode
);
255 d_instantiate(dentry
, inode
);
256 unlock_new_inode(inode
);
261 inode_dec_link_count(inode
);
262 inode_dec_link_count(inode
);
263 unlock_new_inode(inode
);
266 inode_dec_link_count(dir
);
270 static int ext2_unlink(struct inode
* dir
, struct dentry
*dentry
)
272 struct inode
* inode
= dentry
->d_inode
;
273 struct ext2_dir_entry_2
* de
;
277 dquot_initialize(dir
);
279 de
= ext2_find_entry (dir
, &dentry
->d_name
, &page
);
283 err
= ext2_delete_entry (de
, page
);
287 inode
->i_ctime
= dir
->i_ctime
;
288 inode_dec_link_count(inode
);
294 static int ext2_rmdir (struct inode
* dir
, struct dentry
*dentry
)
296 struct inode
* inode
= dentry
->d_inode
;
297 int err
= -ENOTEMPTY
;
299 if (ext2_empty_dir(inode
)) {
300 err
= ext2_unlink(dir
, dentry
);
303 inode_dec_link_count(inode
);
304 inode_dec_link_count(dir
);
310 static int ext2_rename (struct inode
* old_dir
, struct dentry
* old_dentry
,
311 struct inode
* new_dir
, struct dentry
* new_dentry
)
313 struct inode
* old_inode
= old_dentry
->d_inode
;
314 struct inode
* new_inode
= new_dentry
->d_inode
;
315 struct page
* dir_page
= NULL
;
316 struct ext2_dir_entry_2
* dir_de
= NULL
;
317 struct page
* old_page
;
318 struct ext2_dir_entry_2
* old_de
;
321 dquot_initialize(old_dir
);
322 dquot_initialize(new_dir
);
324 old_de
= ext2_find_entry (old_dir
, &old_dentry
->d_name
, &old_page
);
328 if (S_ISDIR(old_inode
->i_mode
)) {
330 dir_de
= ext2_dotdot(old_inode
, &dir_page
);
336 struct page
*new_page
;
337 struct ext2_dir_entry_2
*new_de
;
340 if (dir_de
&& !ext2_empty_dir (new_inode
))
344 new_de
= ext2_find_entry (new_dir
, &new_dentry
->d_name
, &new_page
);
347 inode_inc_link_count(old_inode
);
348 ext2_set_link(new_dir
, new_de
, new_page
, old_inode
, 1);
349 new_inode
->i_ctime
= CURRENT_TIME_SEC
;
351 drop_nlink(new_inode
);
352 inode_dec_link_count(new_inode
);
356 if (new_dir
->i_nlink
>= EXT2_LINK_MAX
)
359 inode_inc_link_count(old_inode
);
360 err
= ext2_add_link(new_dentry
, old_inode
);
362 inode_dec_link_count(old_inode
);
366 inode_inc_link_count(new_dir
);
370 * Like most other Unix systems, set the ctime for inodes on a
372 * inode_dec_link_count() will mark the inode dirty.
374 old_inode
->i_ctime
= CURRENT_TIME_SEC
;
376 ext2_delete_entry (old_de
, old_page
);
377 inode_dec_link_count(old_inode
);
380 if (old_dir
!= new_dir
)
381 ext2_set_link(old_inode
, dir_de
, dir_page
, new_dir
, 0);
384 page_cache_release(dir_page
);
386 inode_dec_link_count(old_dir
);
394 page_cache_release(dir_page
);
398 page_cache_release(old_page
);
403 const struct inode_operations ext2_dir_inode_operations
= {
404 .create
= ext2_create
,
405 .lookup
= ext2_lookup
,
407 .unlink
= ext2_unlink
,
408 .symlink
= ext2_symlink
,
412 .rename
= ext2_rename
,
413 #ifdef CONFIG_EXT2_FS_XATTR
414 .setxattr
= generic_setxattr
,
415 .getxattr
= generic_getxattr
,
416 .listxattr
= ext2_listxattr
,
417 .removexattr
= generic_removexattr
,
419 .setattr
= ext2_setattr
,
420 .check_acl
= ext2_check_acl
,
423 const struct inode_operations ext2_special_inode_operations
= {
424 #ifdef CONFIG_EXT2_FS_XATTR
425 .setxattr
= generic_setxattr
,
426 .getxattr
= generic_getxattr
,
427 .listxattr
= ext2_listxattr
,
428 .removexattr
= generic_removexattr
,
430 .setattr
= ext2_setattr
,
431 .check_acl
= ext2_check_acl
,