blob: a1b328ab1e55d52877fb814db852d74f019533e7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/ext2/namei.c
3 *
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
7 *
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.
13 *
14 * The only non-static object here is ext2_dir_inode_operations.
15 *
16 * TODO: get rid of kmap() use, add readahead.
17 *
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)
22 *
23 * from
24 *
25 * linux/fs/minix/namei.c
26 *
27 * Copyright (C) 1991, 1992 Linus Torvalds
28 *
29 * Big-endian to little-endian byte-swapping/bitmaps by
30 * David S. Miller (davem@caip.rutgers.edu), 1995
31 */
32
33#include <linux/pagemap.h>
34#include "ext2.h"
35#include "xattr.h"
36#include "acl.h"
Carsten Otte6d791252005-06-23 22:05:26 -070037#include "xip.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode)
40{
41 int err = ext2_add_link(dentry, inode);
42 if (!err) {
43 d_instantiate(dentry, inode);
44 return 0;
45 }
Alexey Dobriyana513b032006-03-23 03:00:53 -080046 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 iput(inode);
48 return err;
49}
50
51/*
52 * Methods themselves.
53 */
54
55static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd)
56{
57 struct inode * inode;
58 ino_t ino;
59
60 if (dentry->d_name.len > EXT2_NAME_LEN)
61 return ERR_PTR(-ENAMETOOLONG);
62
63 ino = ext2_inode_by_name(dir, dentry);
64 inode = NULL;
65 if (ino) {
David Howells52fcf702008-02-07 00:15:35 -080066 inode = ext2_iget(dir->i_sb, ino);
67 if (IS_ERR(inode))
68 return ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 }
Pekka Enberg082a05c2006-01-14 13:21:07 -080070 return d_splice_alias(inode, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
73struct dentry *ext2_get_parent(struct dentry *child)
74{
75 unsigned long ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 struct dentry dotdot;
77
78 dotdot.d_name.name = "..";
79 dotdot.d_name.len = 2;
80
81 ino = ext2_inode_by_name(child->d_inode, &dotdot);
82 if (!ino)
83 return ERR_PTR(-ENOENT);
Christoph Hellwig44003722008-08-11 15:49:04 +020084 return d_obtain_alias(ext2_iget(child->d_inode->i_sb, ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87/*
88 * By the time this is called, we already have created
89 * the directory cache entry for the new file, but it
90 * is so far negative - it has no inode.
91 *
92 * If the create succeeds, we fill in the inode information
93 * with d_instantiate().
94 */
95static int ext2_create (struct inode * dir, struct dentry * dentry, int mode, struct nameidata *nd)
96{
97 struct inode * inode = ext2_new_inode (dir, mode);
98 int err = PTR_ERR(inode);
99 if (!IS_ERR(inode)) {
100 inode->i_op = &ext2_file_inode_operations;
Carsten Otte6d791252005-06-23 22:05:26 -0700101 if (ext2_use_xip(inode->i_sb)) {
102 inode->i_mapping->a_ops = &ext2_aops_xip;
103 inode->i_fop = &ext2_xip_file_operations;
104 } else if (test_opt(inode->i_sb, NOBH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 inode->i_mapping->a_ops = &ext2_nobh_aops;
Carsten Otte6d791252005-06-23 22:05:26 -0700106 inode->i_fop = &ext2_file_operations;
107 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 inode->i_mapping->a_ops = &ext2_aops;
Carsten Otte6d791252005-06-23 22:05:26 -0700109 inode->i_fop = &ext2_file_operations;
110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 mark_inode_dirty(inode);
112 err = ext2_add_nondir(dentry, inode);
113 }
114 return err;
115}
116
117static int ext2_mknod (struct inode * dir, struct dentry *dentry, int mode, dev_t rdev)
118{
119 struct inode * inode;
120 int err;
121
122 if (!new_valid_dev(rdev))
123 return -EINVAL;
124
125 inode = ext2_new_inode (dir, mode);
126 err = PTR_ERR(inode);
127 if (!IS_ERR(inode)) {
128 init_special_inode(inode, inode->i_mode, rdev);
129#ifdef CONFIG_EXT2_FS_XATTR
130 inode->i_op = &ext2_special_inode_operations;
131#endif
132 mark_inode_dirty(inode);
133 err = ext2_add_nondir(dentry, inode);
134 }
135 return err;
136}
137
138static int ext2_symlink (struct inode * dir, struct dentry * dentry,
139 const char * symname)
140{
141 struct super_block * sb = dir->i_sb;
142 int err = -ENAMETOOLONG;
143 unsigned l = strlen(symname)+1;
144 struct inode * inode;
145
146 if (l > sb->s_blocksize)
147 goto out;
148
149 inode = ext2_new_inode (dir, S_IFLNK | S_IRWXUGO);
150 err = PTR_ERR(inode);
151 if (IS_ERR(inode))
152 goto out;
153
154 if (l > sizeof (EXT2_I(inode)->i_data)) {
155 /* slow symlink */
156 inode->i_op = &ext2_symlink_inode_operations;
157 if (test_opt(inode->i_sb, NOBH))
158 inode->i_mapping->a_ops = &ext2_nobh_aops;
159 else
160 inode->i_mapping->a_ops = &ext2_aops;
161 err = page_symlink(inode, symname, l);
162 if (err)
163 goto out_fail;
164 } else {
165 /* fast symlink */
166 inode->i_op = &ext2_fast_symlink_inode_operations;
167 memcpy((char*)(EXT2_I(inode)->i_data),symname,l);
168 inode->i_size = l-1;
169 }
170 mark_inode_dirty(inode);
171
172 err = ext2_add_nondir(dentry, inode);
173out:
174 return err;
175
176out_fail:
Alexey Dobriyana513b032006-03-23 03:00:53 -0800177 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 iput (inode);
179 goto out;
180}
181
182static int ext2_link (struct dentry * old_dentry, struct inode * dir,
183 struct dentry *dentry)
184{
185 struct inode *inode = old_dentry->d_inode;
186
187 if (inode->i_nlink >= EXT2_LINK_MAX)
188 return -EMLINK;
189
190 inode->i_ctime = CURRENT_TIME_SEC;
Alexey Dobriyana513b032006-03-23 03:00:53 -0800191 inode_inc_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 atomic_inc(&inode->i_count);
193
194 return ext2_add_nondir(dentry, inode);
195}
196
197static int ext2_mkdir(struct inode * dir, struct dentry * dentry, int mode)
198{
199 struct inode * inode;
200 int err = -EMLINK;
201
202 if (dir->i_nlink >= EXT2_LINK_MAX)
203 goto out;
204
Alexey Dobriyana513b032006-03-23 03:00:53 -0800205 inode_inc_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 inode = ext2_new_inode (dir, S_IFDIR | mode);
208 err = PTR_ERR(inode);
209 if (IS_ERR(inode))
210 goto out_dir;
211
212 inode->i_op = &ext2_dir_inode_operations;
213 inode->i_fop = &ext2_dir_operations;
214 if (test_opt(inode->i_sb, NOBH))
215 inode->i_mapping->a_ops = &ext2_nobh_aops;
216 else
217 inode->i_mapping->a_ops = &ext2_aops;
218
Alexey Dobriyana513b032006-03-23 03:00:53 -0800219 inode_inc_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 err = ext2_make_empty(inode, dir);
222 if (err)
223 goto out_fail;
224
225 err = ext2_add_link(dentry, inode);
226 if (err)
227 goto out_fail;
228
229 d_instantiate(dentry, inode);
230out:
231 return err;
232
233out_fail:
Alexey Dobriyana513b032006-03-23 03:00:53 -0800234 inode_dec_link_count(inode);
235 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 iput(inode);
237out_dir:
Alexey Dobriyana513b032006-03-23 03:00:53 -0800238 inode_dec_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 goto out;
240}
241
242static int ext2_unlink(struct inode * dir, struct dentry *dentry)
243{
244 struct inode * inode = dentry->d_inode;
245 struct ext2_dir_entry_2 * de;
246 struct page * page;
247 int err = -ENOENT;
248
249 de = ext2_find_entry (dir, dentry, &page);
250 if (!de)
251 goto out;
252
253 err = ext2_delete_entry (de, page);
254 if (err)
255 goto out;
256
257 inode->i_ctime = dir->i_ctime;
Alexey Dobriyana513b032006-03-23 03:00:53 -0800258 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 err = 0;
260out:
261 return err;
262}
263
264static int ext2_rmdir (struct inode * dir, struct dentry *dentry)
265{
266 struct inode * inode = dentry->d_inode;
267 int err = -ENOTEMPTY;
268
269 if (ext2_empty_dir(inode)) {
270 err = ext2_unlink(dir, dentry);
271 if (!err) {
272 inode->i_size = 0;
Alexey Dobriyana513b032006-03-23 03:00:53 -0800273 inode_dec_link_count(inode);
274 inode_dec_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276 }
277 return err;
278}
279
280static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry,
281 struct inode * new_dir, struct dentry * new_dentry )
282{
283 struct inode * old_inode = old_dentry->d_inode;
284 struct inode * new_inode = new_dentry->d_inode;
285 struct page * dir_page = NULL;
286 struct ext2_dir_entry_2 * dir_de = NULL;
287 struct page * old_page;
288 struct ext2_dir_entry_2 * old_de;
289 int err = -ENOENT;
290
291 old_de = ext2_find_entry (old_dir, old_dentry, &old_page);
292 if (!old_de)
293 goto out;
294
295 if (S_ISDIR(old_inode->i_mode)) {
296 err = -EIO;
297 dir_de = ext2_dotdot(old_inode, &dir_page);
298 if (!dir_de)
299 goto out_old;
300 }
301
302 if (new_inode) {
303 struct page *new_page;
304 struct ext2_dir_entry_2 *new_de;
305
306 err = -ENOTEMPTY;
307 if (dir_de && !ext2_empty_dir (new_inode))
308 goto out_dir;
309
310 err = -ENOENT;
311 new_de = ext2_find_entry (new_dir, new_dentry, &new_page);
312 if (!new_de)
313 goto out_dir;
Alexey Dobriyana513b032006-03-23 03:00:53 -0800314 inode_inc_link_count(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 ext2_set_link(new_dir, new_de, new_page, old_inode);
316 new_inode->i_ctime = CURRENT_TIME_SEC;
317 if (dir_de)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700318 drop_nlink(new_inode);
Alexey Dobriyana513b032006-03-23 03:00:53 -0800319 inode_dec_link_count(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 } else {
321 if (dir_de) {
322 err = -EMLINK;
323 if (new_dir->i_nlink >= EXT2_LINK_MAX)
324 goto out_dir;
325 }
Alexey Dobriyana513b032006-03-23 03:00:53 -0800326 inode_inc_link_count(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 err = ext2_add_link(new_dentry, old_inode);
328 if (err) {
Alexey Dobriyana513b032006-03-23 03:00:53 -0800329 inode_dec_link_count(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 goto out_dir;
331 }
332 if (dir_de)
Alexey Dobriyana513b032006-03-23 03:00:53 -0800333 inode_inc_link_count(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335
336 /*
337 * Like most other Unix systems, set the ctime for inodes on a
338 * rename.
Alexey Dobriyana513b032006-03-23 03:00:53 -0800339 * inode_dec_link_count() will mark the inode dirty.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 */
341 old_inode->i_ctime = CURRENT_TIME_SEC;
342
343 ext2_delete_entry (old_de, old_page);
Alexey Dobriyana513b032006-03-23 03:00:53 -0800344 inode_dec_link_count(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 if (dir_de) {
347 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
Alexey Dobriyana513b032006-03-23 03:00:53 -0800348 inode_dec_link_count(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350 return 0;
351
352
353out_dir:
354 if (dir_de) {
355 kunmap(dir_page);
356 page_cache_release(dir_page);
357 }
358out_old:
359 kunmap(old_page);
360 page_cache_release(old_page);
361out:
362 return err;
363}
364
Arjan van de Ven754661f2007-02-12 00:55:38 -0800365const struct inode_operations ext2_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 .create = ext2_create,
367 .lookup = ext2_lookup,
368 .link = ext2_link,
369 .unlink = ext2_unlink,
370 .symlink = ext2_symlink,
371 .mkdir = ext2_mkdir,
372 .rmdir = ext2_rmdir,
373 .mknod = ext2_mknod,
374 .rename = ext2_rename,
375#ifdef CONFIG_EXT2_FS_XATTR
376 .setxattr = generic_setxattr,
377 .getxattr = generic_getxattr,
378 .listxattr = ext2_listxattr,
379 .removexattr = generic_removexattr,
380#endif
381 .setattr = ext2_setattr,
382 .permission = ext2_permission,
383};
384
Arjan van de Ven754661f2007-02-12 00:55:38 -0800385const struct inode_operations ext2_special_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386#ifdef CONFIG_EXT2_FS_XATTR
387 .setxattr = generic_setxattr,
388 .getxattr = generic_getxattr,
389 .listxattr = ext2_listxattr,
390 .removexattr = generic_removexattr,
391#endif
392 .setattr = ext2_setattr,
393 .permission = ext2_permission,
394};