HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / fs / ext2 / namei.c
blobda3d40ef1668553719be7d2618b3393a251cf455
1 /*
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)
23 * from
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>
35 #include "ext2.h"
36 #include "xattr.h"
37 #include "acl.h"
39 static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode)
41 int err = ext2_add_link(dentry, inode);
42 if (!err) {
43 d_instantiate_new(dentry, inode);
44 return 0;
46 inode_dec_link_count(inode);
47 unlock_new_inode(inode);
48 iput(inode);
49 return err;
53 * Methods themselves.
56 static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, unsigned int flags)
58 struct inode * inode;
59 ino_t ino;
61 if (dentry->d_name.len > EXT2_NAME_LEN)
62 return ERR_PTR(-ENAMETOOLONG);
64 ino = ext2_inode_by_name(dir, &dentry->d_name);
65 inode = NULL;
66 if (ino) {
67 inode = ext2_iget(dir->i_sb, ino);
68 if (inode == ERR_PTR(-ESTALE)) {
69 ext2_error(dir->i_sb, __func__,
70 "deleted inode referenced: %lu",
71 (unsigned long) ino);
72 return ERR_PTR(-EIO);
75 return d_splice_alias(inode, dentry);
78 struct dentry *ext2_get_parent(struct dentry *child)
80 struct qstr dotdot = QSTR_INIT("..", 2);
81 unsigned long ino = ext2_inode_by_name(d_inode(child), &dotdot);
82 if (!ino)
83 return ERR_PTR(-ENOENT);
84 return d_obtain_alias(ext2_iget(d_inode(child)->i_sb, ino));
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.
92 * If the create succeeds, we fill in the inode information
93 * with d_instantiate().
95 static int ext2_create (struct inode * dir, struct dentry * dentry, umode_t mode, bool excl)
97 struct inode *inode;
98 int err;
100 err = dquot_initialize(dir);
101 if (err)
102 return err;
104 inode = ext2_new_inode(dir, mode, &dentry->d_name);
105 if (IS_ERR(inode))
106 return PTR_ERR(inode);
108 inode->i_op = &ext2_file_inode_operations;
109 if (test_opt(inode->i_sb, NOBH)) {
110 inode->i_mapping->a_ops = &ext2_nobh_aops;
111 inode->i_fop = &ext2_file_operations;
112 } else {
113 inode->i_mapping->a_ops = &ext2_aops;
114 inode->i_fop = &ext2_file_operations;
116 mark_inode_dirty(inode);
117 return ext2_add_nondir(dentry, inode);
120 static int ext2_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
122 struct inode *inode = ext2_new_inode(dir, mode, NULL);
123 if (IS_ERR(inode))
124 return PTR_ERR(inode);
126 inode->i_op = &ext2_file_inode_operations;
127 if (test_opt(inode->i_sb, NOBH)) {
128 inode->i_mapping->a_ops = &ext2_nobh_aops;
129 inode->i_fop = &ext2_file_operations;
130 } else {
131 inode->i_mapping->a_ops = &ext2_aops;
132 inode->i_fop = &ext2_file_operations;
134 mark_inode_dirty(inode);
135 d_tmpfile(dentry, inode);
136 unlock_new_inode(inode);
137 return 0;
140 static int ext2_mknod (struct inode * dir, struct dentry *dentry, umode_t mode, dev_t rdev)
142 struct inode * inode;
143 int err;
145 err = dquot_initialize(dir);
146 if (err)
147 return err;
149 inode = ext2_new_inode (dir, mode, &dentry->d_name);
150 err = PTR_ERR(inode);
151 if (!IS_ERR(inode)) {
152 init_special_inode(inode, inode->i_mode, rdev);
153 #ifdef CONFIG_EXT2_FS_XATTR
154 inode->i_op = &ext2_special_inode_operations;
155 #endif
156 mark_inode_dirty(inode);
157 err = ext2_add_nondir(dentry, inode);
159 return err;
162 static int ext2_symlink (struct inode * dir, struct dentry * dentry,
163 const char * symname)
165 struct super_block * sb = dir->i_sb;
166 int err = -ENAMETOOLONG;
167 unsigned l = strlen(symname)+1;
168 struct inode * inode;
170 if (l > sb->s_blocksize)
171 goto out;
173 err = dquot_initialize(dir);
174 if (err)
175 goto out;
177 inode = ext2_new_inode (dir, S_IFLNK | S_IRWXUGO, &dentry->d_name);
178 err = PTR_ERR(inode);
179 if (IS_ERR(inode))
180 goto out;
182 if (l > sizeof (EXT2_I(inode)->i_data)) {
183 /* slow symlink */
184 inode->i_op = &ext2_symlink_inode_operations;
185 if (test_opt(inode->i_sb, NOBH))
186 inode->i_mapping->a_ops = &ext2_nobh_aops;
187 else
188 inode->i_mapping->a_ops = &ext2_aops;
189 err = page_symlink(inode, symname, l);
190 if (err)
191 goto out_fail;
192 } else {
193 /* fast symlink */
194 inode->i_op = &ext2_fast_symlink_inode_operations;
195 inode->i_link = (char*)EXT2_I(inode)->i_data;
196 memcpy(inode->i_link, symname, l);
197 inode->i_size = l-1;
199 mark_inode_dirty(inode);
201 err = ext2_add_nondir(dentry, inode);
202 out:
203 return err;
205 out_fail:
206 inode_dec_link_count(inode);
207 unlock_new_inode(inode);
208 iput (inode);
209 goto out;
212 static int ext2_link (struct dentry * old_dentry, struct inode * dir,
213 struct dentry *dentry)
215 struct inode *inode = d_inode(old_dentry);
216 int err;
218 err = dquot_initialize(dir);
219 if (err)
220 return err;
222 inode->i_ctime = CURRENT_TIME_SEC;
223 inode_inc_link_count(inode);
224 ihold(inode);
226 err = ext2_add_link(dentry, inode);
227 if (!err) {
228 d_instantiate(dentry, inode);
229 return 0;
231 inode_dec_link_count(inode);
232 iput(inode);
233 return err;
236 static int ext2_mkdir(struct inode * dir, struct dentry * dentry, umode_t mode)
238 struct inode * inode;
239 int err;
241 err = dquot_initialize(dir);
242 if (err)
243 return err;
245 inode_inc_link_count(dir);
247 inode = ext2_new_inode(dir, S_IFDIR | mode, &dentry->d_name);
248 err = PTR_ERR(inode);
249 if (IS_ERR(inode))
250 goto out_dir;
252 inode->i_op = &ext2_dir_inode_operations;
253 inode->i_fop = &ext2_dir_operations;
254 if (test_opt(inode->i_sb, NOBH))
255 inode->i_mapping->a_ops = &ext2_nobh_aops;
256 else
257 inode->i_mapping->a_ops = &ext2_aops;
259 inode_inc_link_count(inode);
261 err = ext2_make_empty(inode, dir);
262 if (err)
263 goto out_fail;
265 err = ext2_add_link(dentry, inode);
266 if (err)
267 goto out_fail;
269 d_instantiate_new(dentry, inode);
270 out:
271 return err;
273 out_fail:
274 inode_dec_link_count(inode);
275 inode_dec_link_count(inode);
276 unlock_new_inode(inode);
277 iput(inode);
278 out_dir:
279 inode_dec_link_count(dir);
280 goto out;
283 static int ext2_unlink(struct inode * dir, struct dentry *dentry)
285 struct inode * inode = d_inode(dentry);
286 struct ext2_dir_entry_2 * de;
287 struct page * page;
288 int err;
290 err = dquot_initialize(dir);
291 if (err)
292 goto out;
294 de = ext2_find_entry (dir, &dentry->d_name, &page);
295 if (!de) {
296 err = -ENOENT;
297 goto out;
300 err = ext2_delete_entry (de, page);
301 if (err)
302 goto out;
304 inode->i_ctime = dir->i_ctime;
305 inode_dec_link_count(inode);
306 err = 0;
307 out:
308 return err;
311 static int ext2_rmdir (struct inode * dir, struct dentry *dentry)
313 struct inode * inode = d_inode(dentry);
314 int err = -ENOTEMPTY;
316 if (ext2_empty_dir(inode)) {
317 err = ext2_unlink(dir, dentry);
318 if (!err) {
319 inode->i_size = 0;
320 inode_dec_link_count(inode);
321 inode_dec_link_count(dir);
324 return err;
327 static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry,
328 struct inode * new_dir, struct dentry * new_dentry )
330 struct inode * old_inode = d_inode(old_dentry);
331 struct inode * new_inode = d_inode(new_dentry);
332 struct page * dir_page = NULL;
333 struct ext2_dir_entry_2 * dir_de = NULL;
334 struct page * old_page;
335 struct ext2_dir_entry_2 * old_de;
336 int err;
338 err = dquot_initialize(old_dir);
339 if (err)
340 goto out;
342 err = dquot_initialize(new_dir);
343 if (err)
344 goto out;
346 old_de = ext2_find_entry (old_dir, &old_dentry->d_name, &old_page);
347 if (!old_de) {
348 err = -ENOENT;
349 goto out;
352 if (S_ISDIR(old_inode->i_mode)) {
353 err = -EIO;
354 dir_de = ext2_dotdot(old_inode, &dir_page);
355 if (!dir_de)
356 goto out_old;
359 if (new_inode) {
360 struct page *new_page;
361 struct ext2_dir_entry_2 *new_de;
363 err = -ENOTEMPTY;
364 if (dir_de && !ext2_empty_dir (new_inode))
365 goto out_dir;
367 err = -ENOENT;
368 new_de = ext2_find_entry (new_dir, &new_dentry->d_name, &new_page);
369 if (!new_de)
370 goto out_dir;
371 ext2_set_link(new_dir, new_de, new_page, old_inode, 1);
372 new_inode->i_ctime = CURRENT_TIME_SEC;
373 if (dir_de)
374 drop_nlink(new_inode);
375 inode_dec_link_count(new_inode);
376 } else {
377 err = ext2_add_link(new_dentry, old_inode);
378 if (err)
379 goto out_dir;
380 if (dir_de)
381 inode_inc_link_count(new_dir);
385 * Like most other Unix systems, set the ctime for inodes on a
386 * rename.
388 old_inode->i_ctime = CURRENT_TIME_SEC;
389 mark_inode_dirty(old_inode);
391 ext2_delete_entry (old_de, old_page);
393 if (dir_de) {
394 if (old_dir != new_dir)
395 ext2_set_link(old_inode, dir_de, dir_page, new_dir, 0);
396 else {
397 kunmap(dir_page);
398 page_cache_release(dir_page);
400 inode_dec_link_count(old_dir);
402 return 0;
405 out_dir:
406 if (dir_de) {
407 kunmap(dir_page);
408 page_cache_release(dir_page);
410 out_old:
411 kunmap(old_page);
412 page_cache_release(old_page);
413 out:
414 return err;
417 const struct inode_operations ext2_dir_inode_operations = {
418 .create = ext2_create,
419 .lookup = ext2_lookup,
420 .link = ext2_link,
421 .unlink = ext2_unlink,
422 .symlink = ext2_symlink,
423 .mkdir = ext2_mkdir,
424 .rmdir = ext2_rmdir,
425 .mknod = ext2_mknod,
426 .rename = ext2_rename,
427 #ifdef CONFIG_EXT2_FS_XATTR
428 .setxattr = generic_setxattr,
429 .getxattr = generic_getxattr,
430 .listxattr = ext2_listxattr,
431 .removexattr = generic_removexattr,
432 #endif
433 .setattr = ext2_setattr,
434 .get_acl = ext2_get_acl,
435 .set_acl = ext2_set_acl,
436 .tmpfile = ext2_tmpfile,
439 const struct inode_operations ext2_special_inode_operations = {
440 #ifdef CONFIG_EXT2_FS_XATTR
441 .setxattr = generic_setxattr,
442 .getxattr = generic_getxattr,
443 .listxattr = ext2_listxattr,
444 .removexattr = generic_removexattr,
445 #endif
446 .setattr = ext2_setattr,
447 .get_acl = ext2_get_acl,
448 .set_acl = ext2_set_acl,