blob: 2a747252ec1204776d7ba99d9b7147872c060743 [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
Al Viroa9885442008-08-24 07:28:39 -040063 ino = ext2_inode_by_name(dir, &dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 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{
Al Viroa9885442008-08-24 07:28:39 -040075 struct qstr dotdot = {.name = "..", .len = 2};
76 unsigned long ino = ext2_inode_by_name(child->d_inode, &dotdot);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 if (!ino)
78 return ERR_PTR(-ENOENT);
Christoph Hellwig44003722008-08-11 15:49:04 +020079 return d_obtain_alias(ext2_iget(child->d_inode->i_sb, ino));
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
82/*
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.
86 *
87 * If the create succeeds, we fill in the inode information
88 * with d_instantiate().
89 */
90static int ext2_create (struct inode * dir, struct dentry * dentry, int mode, struct nameidata *nd)
91{
92 struct inode * inode = ext2_new_inode (dir, mode);
93 int err = PTR_ERR(inode);
94 if (!IS_ERR(inode)) {
95 inode->i_op = &ext2_file_inode_operations;
Carsten Otte6d791252005-06-23 22:05:26 -070096 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)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 inode->i_mapping->a_ops = &ext2_nobh_aops;
Carsten Otte6d791252005-06-23 22:05:26 -0700101 inode->i_fop = &ext2_file_operations;
102 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 inode->i_mapping->a_ops = &ext2_aops;
Carsten Otte6d791252005-06-23 22:05:26 -0700104 inode->i_fop = &ext2_file_operations;
105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 mark_inode_dirty(inode);
107 err = ext2_add_nondir(dentry, inode);
108 }
109 return err;
110}
111
112static int ext2_mknod (struct inode * dir, struct dentry *dentry, int mode, dev_t rdev)
113{
114 struct inode * inode;
115 int err;
116
117 if (!new_valid_dev(rdev))
118 return -EINVAL;
119
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;
126#endif
127 mark_inode_dirty(inode);
128 err = ext2_add_nondir(dentry, inode);
129 }
130 return err;
131}
132
133static int ext2_symlink (struct inode * dir, struct dentry * dentry,
134 const char * symname)
135{
136 struct super_block * sb = dir->i_sb;
137 int err = -ENAMETOOLONG;
138 unsigned l = strlen(symname)+1;
139 struct inode * inode;
140
141 if (l > sb->s_blocksize)
142 goto out;
143
144 inode = ext2_new_inode (dir, S_IFLNK | S_IRWXUGO);
145 err = PTR_ERR(inode);
146 if (IS_ERR(inode))
147 goto out;
148
149 if (l > sizeof (EXT2_I(inode)->i_data)) {
150 /* slow symlink */
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;
154 else
155 inode->i_mapping->a_ops = &ext2_aops;
156 err = page_symlink(inode, symname, l);
157 if (err)
158 goto out_fail;
159 } else {
160 /* fast symlink */
161 inode->i_op = &ext2_fast_symlink_inode_operations;
162 memcpy((char*)(EXT2_I(inode)->i_data),symname,l);
163 inode->i_size = l-1;
164 }
165 mark_inode_dirty(inode);
166
167 err = ext2_add_nondir(dentry, inode);
168out:
169 return err;
170
171out_fail:
Alexey Dobriyana513b032006-03-23 03:00:53 -0800172 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 iput (inode);
174 goto out;
175}
176
177static int ext2_link (struct dentry * old_dentry, struct inode * dir,
178 struct dentry *dentry)
179{
180 struct inode *inode = old_dentry->d_inode;
181
182 if (inode->i_nlink >= EXT2_LINK_MAX)
183 return -EMLINK;
184
185 inode->i_ctime = CURRENT_TIME_SEC;
Alexey Dobriyana513b032006-03-23 03:00:53 -0800186 inode_inc_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 atomic_inc(&inode->i_count);
188
189 return ext2_add_nondir(dentry, inode);
190}
191
192static int ext2_mkdir(struct inode * dir, struct dentry * dentry, int mode)
193{
194 struct inode * inode;
195 int err = -EMLINK;
196
197 if (dir->i_nlink >= EXT2_LINK_MAX)
198 goto out;
199
Alexey Dobriyana513b032006-03-23 03:00:53 -0800200 inode_inc_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 inode = ext2_new_inode (dir, S_IFDIR | mode);
203 err = PTR_ERR(inode);
204 if (IS_ERR(inode))
205 goto out_dir;
206
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;
211 else
212 inode->i_mapping->a_ops = &ext2_aops;
213
Alexey Dobriyana513b032006-03-23 03:00:53 -0800214 inode_inc_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 err = ext2_make_empty(inode, dir);
217 if (err)
218 goto out_fail;
219
220 err = ext2_add_link(dentry, inode);
221 if (err)
222 goto out_fail;
223
224 d_instantiate(dentry, inode);
225out:
226 return err;
227
228out_fail:
Alexey Dobriyana513b032006-03-23 03:00:53 -0800229 inode_dec_link_count(inode);
230 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 iput(inode);
232out_dir:
Alexey Dobriyana513b032006-03-23 03:00:53 -0800233 inode_dec_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 goto out;
235}
236
237static int ext2_unlink(struct inode * dir, struct dentry *dentry)
238{
239 struct inode * inode = dentry->d_inode;
240 struct ext2_dir_entry_2 * de;
241 struct page * page;
242 int err = -ENOENT;
243
Al Viroa9885442008-08-24 07:28:39 -0400244 de = ext2_find_entry (dir, &dentry->d_name, &page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (!de)
246 goto out;
247
248 err = ext2_delete_entry (de, page);
249 if (err)
250 goto out;
251
252 inode->i_ctime = dir->i_ctime;
Alexey Dobriyana513b032006-03-23 03:00:53 -0800253 inode_dec_link_count(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 err = 0;
255out:
256 return err;
257}
258
259static int ext2_rmdir (struct inode * dir, struct dentry *dentry)
260{
261 struct inode * inode = dentry->d_inode;
262 int err = -ENOTEMPTY;
263
264 if (ext2_empty_dir(inode)) {
265 err = ext2_unlink(dir, dentry);
266 if (!err) {
267 inode->i_size = 0;
Alexey Dobriyana513b032006-03-23 03:00:53 -0800268 inode_dec_link_count(inode);
269 inode_dec_link_count(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
271 }
272 return err;
273}
274
275static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry,
276 struct inode * new_dir, struct dentry * new_dentry )
277{
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;
284 int err = -ENOENT;
285
Al Viroa9885442008-08-24 07:28:39 -0400286 old_de = ext2_find_entry (old_dir, &old_dentry->d_name, &old_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (!old_de)
288 goto out;
289
290 if (S_ISDIR(old_inode->i_mode)) {
291 err = -EIO;
292 dir_de = ext2_dotdot(old_inode, &dir_page);
293 if (!dir_de)
294 goto out_old;
295 }
296
297 if (new_inode) {
298 struct page *new_page;
299 struct ext2_dir_entry_2 *new_de;
300
301 err = -ENOTEMPTY;
302 if (dir_de && !ext2_empty_dir (new_inode))
303 goto out_dir;
304
305 err = -ENOENT;
Al Viroa9885442008-08-24 07:28:39 -0400306 new_de = ext2_find_entry (new_dir, &new_dentry->d_name, &new_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (!new_de)
308 goto out_dir;
Alexey Dobriyana513b032006-03-23 03:00:53 -0800309 inode_inc_link_count(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 ext2_set_link(new_dir, new_de, new_page, old_inode);
311 new_inode->i_ctime = CURRENT_TIME_SEC;
312 if (dir_de)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700313 drop_nlink(new_inode);
Alexey Dobriyana513b032006-03-23 03:00:53 -0800314 inode_dec_link_count(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 } else {
316 if (dir_de) {
317 err = -EMLINK;
318 if (new_dir->i_nlink >= EXT2_LINK_MAX)
319 goto out_dir;
320 }
Alexey Dobriyana513b032006-03-23 03:00:53 -0800321 inode_inc_link_count(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 err = ext2_add_link(new_dentry, old_inode);
323 if (err) {
Alexey Dobriyana513b032006-03-23 03:00:53 -0800324 inode_dec_link_count(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 goto out_dir;
326 }
327 if (dir_de)
Alexey Dobriyana513b032006-03-23 03:00:53 -0800328 inode_inc_link_count(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
330
331 /*
332 * Like most other Unix systems, set the ctime for inodes on a
333 * rename.
Alexey Dobriyana513b032006-03-23 03:00:53 -0800334 * inode_dec_link_count() will mark the inode dirty.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 */
336 old_inode->i_ctime = CURRENT_TIME_SEC;
337
338 ext2_delete_entry (old_de, old_page);
Alexey Dobriyana513b032006-03-23 03:00:53 -0800339 inode_dec_link_count(old_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 if (dir_de) {
342 ext2_set_link(old_inode, dir_de, dir_page, new_dir);
Alexey Dobriyana513b032006-03-23 03:00:53 -0800343 inode_dec_link_count(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345 return 0;
346
347
348out_dir:
349 if (dir_de) {
350 kunmap(dir_page);
351 page_cache_release(dir_page);
352 }
353out_old:
354 kunmap(old_page);
355 page_cache_release(old_page);
356out:
357 return err;
358}
359
Arjan van de Ven754661f2007-02-12 00:55:38 -0800360const struct inode_operations ext2_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 .create = ext2_create,
362 .lookup = ext2_lookup,
363 .link = ext2_link,
364 .unlink = ext2_unlink,
365 .symlink = ext2_symlink,
366 .mkdir = ext2_mkdir,
367 .rmdir = ext2_rmdir,
368 .mknod = ext2_mknod,
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,
375#endif
376 .setattr = ext2_setattr,
377 .permission = ext2_permission,
378};
379
Arjan van de Ven754661f2007-02-12 00:55:38 -0800380const struct inode_operations ext2_special_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381#ifdef CONFIG_EXT2_FS_XATTR
382 .setxattr = generic_setxattr,
383 .getxattr = generic_getxattr,
384 .listxattr = ext2_listxattr,
385 .removexattr = generic_removexattr,
386#endif
387 .setattr = ext2_setattr,
388 .permission = ext2_permission,
389};