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>
39 static inline int ext2_add_nondir(struct dentry
*dentry
, struct inode
*inode
)
41 int err
= ext2_add_link(dentry
, inode
);
43 d_instantiate(dentry
, inode
);
46 inode_dec_link_count(inode
);
55 static struct dentry
*ext2_lookup(struct inode
* dir
, struct dentry
*dentry
, struct nameidata
*nd
)
60 if (dentry
->d_name
.len
> EXT2_NAME_LEN
)
61 return ERR_PTR(-ENAMETOOLONG
);
63 ino
= ext2_inode_by_name(dir
, &dentry
->d_name
);
66 inode
= ext2_iget(dir
->i_sb
, ino
);
68 return ERR_CAST(inode
);
70 return d_splice_alias(inode
, dentry
);
73 struct dentry
*ext2_get_parent(struct dentry
*child
)
75 struct qstr dotdot
= {.name
= "..", .len
= 2};
76 unsigned long ino
= ext2_inode_by_name(child
->d_inode
, &dotdot
);
78 return ERR_PTR(-ENOENT
);
79 return d_obtain_alias(ext2_iget(child
->d_inode
->i_sb
, ino
));
83 * By the time this is called, we already have created
84 * the directory cache entry for the new file, but it
85 * is so far negative - it has no inode.
87 * If the create succeeds, we fill in the inode information
88 * with d_instantiate().
90 static int ext2_create (struct inode
* dir
, struct dentry
* dentry
, int mode
, struct nameidata
*nd
)
92 struct inode
* inode
= ext2_new_inode (dir
, mode
);
93 int err
= PTR_ERR(inode
);
95 inode
->i_op
= &ext2_file_inode_operations
;
96 if (ext2_use_xip(inode
->i_sb
)) {
97 inode
->i_mapping
->a_ops
= &ext2_aops_xip
;
98 inode
->i_fop
= &ext2_xip_file_operations
;
99 } else if (test_opt(inode
->i_sb
, NOBH
)) {
100 inode
->i_mapping
->a_ops
= &ext2_nobh_aops
;
101 inode
->i_fop
= &ext2_file_operations
;
103 inode
->i_mapping
->a_ops
= &ext2_aops
;
104 inode
->i_fop
= &ext2_file_operations
;
106 mark_inode_dirty(inode
);
107 err
= ext2_add_nondir(dentry
, inode
);
112 static int ext2_mknod (struct inode
* dir
, struct dentry
*dentry
, int mode
, dev_t rdev
)
114 struct inode
* inode
;
117 if (!new_valid_dev(rdev
))
120 inode
= ext2_new_inode (dir
, mode
);
121 err
= PTR_ERR(inode
);
122 if (!IS_ERR(inode
)) {
123 init_special_inode(inode
, inode
->i_mode
, rdev
);
124 #ifdef CONFIG_EXT2_FS_XATTR
125 inode
->i_op
= &ext2_special_inode_operations
;
127 mark_inode_dirty(inode
);
128 err
= ext2_add_nondir(dentry
, inode
);
133 static int ext2_symlink (struct inode
* dir
, struct dentry
* dentry
,
134 const char * symname
)
136 struct super_block
* sb
= dir
->i_sb
;
137 int err
= -ENAMETOOLONG
;
138 unsigned l
= strlen(symname
)+1;
139 struct inode
* inode
;
141 if (l
> sb
->s_blocksize
)
144 inode
= ext2_new_inode (dir
, S_IFLNK
| S_IRWXUGO
);
145 err
= PTR_ERR(inode
);
149 if (l
> sizeof (EXT2_I(inode
)->i_data
)) {
151 inode
->i_op
= &ext2_symlink_inode_operations
;
152 if (test_opt(inode
->i_sb
, NOBH
))
153 inode
->i_mapping
->a_ops
= &ext2_nobh_aops
;
155 inode
->i_mapping
->a_ops
= &ext2_aops
;
156 err
= page_symlink(inode
, symname
, l
);
161 inode
->i_op
= &ext2_fast_symlink_inode_operations
;
162 memcpy((char*)(EXT2_I(inode
)->i_data
),symname
,l
);
165 mark_inode_dirty(inode
);
167 err
= ext2_add_nondir(dentry
, inode
);
172 inode_dec_link_count(inode
);
177 static int ext2_link (struct dentry
* old_dentry
, struct inode
* dir
,
178 struct dentry
*dentry
)
180 struct inode
*inode
= old_dentry
->d_inode
;
182 if (inode
->i_nlink
>= EXT2_LINK_MAX
)
185 inode
->i_ctime
= CURRENT_TIME_SEC
;
186 inode_inc_link_count(inode
);
187 atomic_inc(&inode
->i_count
);
189 return ext2_add_nondir(dentry
, inode
);
192 static int ext2_mkdir(struct inode
* dir
, struct dentry
* dentry
, int mode
)
194 struct inode
* inode
;
197 if (dir
->i_nlink
>= EXT2_LINK_MAX
)
200 inode_inc_link_count(dir
);
202 inode
= ext2_new_inode (dir
, S_IFDIR
| mode
);
203 err
= PTR_ERR(inode
);
207 inode
->i_op
= &ext2_dir_inode_operations
;
208 inode
->i_fop
= &ext2_dir_operations
;
209 if (test_opt(inode
->i_sb
, NOBH
))
210 inode
->i_mapping
->a_ops
= &ext2_nobh_aops
;
212 inode
->i_mapping
->a_ops
= &ext2_aops
;
214 inode_inc_link_count(inode
);
216 err
= ext2_make_empty(inode
, dir
);
220 err
= ext2_add_link(dentry
, inode
);
224 d_instantiate(dentry
, inode
);
229 inode_dec_link_count(inode
);
230 inode_dec_link_count(inode
);
233 inode_dec_link_count(dir
);
237 static int ext2_unlink(struct inode
* dir
, struct dentry
*dentry
)
239 struct inode
* inode
= dentry
->d_inode
;
240 struct ext2_dir_entry_2
* de
;
244 de
= ext2_find_entry (dir
, &dentry
->d_name
, &page
);
248 err
= ext2_delete_entry (de
, page
);
252 inode
->i_ctime
= dir
->i_ctime
;
253 inode_dec_link_count(inode
);
259 static int ext2_rmdir (struct inode
* dir
, struct dentry
*dentry
)
261 struct inode
* inode
= dentry
->d_inode
;
262 int err
= -ENOTEMPTY
;
264 if (ext2_empty_dir(inode
)) {
265 err
= ext2_unlink(dir
, dentry
);
268 inode_dec_link_count(inode
);
269 inode_dec_link_count(dir
);
275 static int ext2_rename (struct inode
* old_dir
, struct dentry
* old_dentry
,
276 struct inode
* new_dir
, struct dentry
* new_dentry
)
278 struct inode
* old_inode
= old_dentry
->d_inode
;
279 struct inode
* new_inode
= new_dentry
->d_inode
;
280 struct page
* dir_page
= NULL
;
281 struct ext2_dir_entry_2
* dir_de
= NULL
;
282 struct page
* old_page
;
283 struct ext2_dir_entry_2
* old_de
;
286 old_de
= ext2_find_entry (old_dir
, &old_dentry
->d_name
, &old_page
);
290 if (S_ISDIR(old_inode
->i_mode
)) {
292 dir_de
= ext2_dotdot(old_inode
, &dir_page
);
298 struct page
*new_page
;
299 struct ext2_dir_entry_2
*new_de
;
302 if (dir_de
&& !ext2_empty_dir (new_inode
))
306 new_de
= ext2_find_entry (new_dir
, &new_dentry
->d_name
, &new_page
);
309 inode_inc_link_count(old_inode
);
310 ext2_set_link(new_dir
, new_de
, new_page
, old_inode
);
311 new_inode
->i_ctime
= CURRENT_TIME_SEC
;
313 drop_nlink(new_inode
);
314 inode_dec_link_count(new_inode
);
318 if (new_dir
->i_nlink
>= EXT2_LINK_MAX
)
321 inode_inc_link_count(old_inode
);
322 err
= ext2_add_link(new_dentry
, old_inode
);
324 inode_dec_link_count(old_inode
);
328 inode_inc_link_count(new_dir
);
332 * Like most other Unix systems, set the ctime for inodes on a
334 * inode_dec_link_count() will mark the inode dirty.
336 old_inode
->i_ctime
= CURRENT_TIME_SEC
;
338 ext2_delete_entry (old_de
, old_page
);
339 inode_dec_link_count(old_inode
);
342 ext2_set_link(old_inode
, dir_de
, dir_page
, new_dir
);
343 inode_dec_link_count(old_dir
);
351 page_cache_release(dir_page
);
355 page_cache_release(old_page
);
360 const struct inode_operations ext2_dir_inode_operations
= {
361 .create
= ext2_create
,
362 .lookup
= ext2_lookup
,
364 .unlink
= ext2_unlink
,
365 .symlink
= ext2_symlink
,
369 .rename
= ext2_rename
,
370 #ifdef CONFIG_EXT2_FS_XATTR
371 .setxattr
= generic_setxattr
,
372 .getxattr
= generic_getxattr
,
373 .listxattr
= ext2_listxattr
,
374 .removexattr
= generic_removexattr
,
376 .setattr
= ext2_setattr
,
377 .permission
= ext2_permission
,
380 const struct inode_operations ext2_special_inode_operations
= {
381 #ifdef CONFIG_EXT2_FS_XATTR
382 .setxattr
= generic_setxattr
,
383 .getxattr
= generic_getxattr
,
384 .listxattr
= ext2_listxattr
,
385 .removexattr
= generic_removexattr
,
387 .setattr
= ext2_setattr
,
388 .permission
= ext2_permission
,