HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / fs / nilfs2 / namei.c
blobcd7f5b0abe842a80021382b9bf5a5c23e2b997d7
1 /*
2 * namei.c - NILFS pathname lookup operations.
4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Modified for NILFS by Amagai Yoshiji <amagai@osrg.net>,
21 * Ryusuke Konishi <ryusuke@osrg.net>
24 * linux/fs/ext2/namei.c
26 * Copyright (C) 1992, 1993, 1994, 1995
27 * Remy Card (card@masi.ibp.fr)
28 * Laboratoire MASI - Institut Blaise Pascal
29 * Universite Pierre et Marie Curie (Paris VI)
31 * from
33 * linux/fs/minix/namei.c
35 * Copyright (C) 1991, 1992 Linus Torvalds
37 * Big-endian to little-endian byte-swapping/bitmaps by
38 * David S. Miller (davem@caip.rutgers.edu), 1995
41 #include <linux/pagemap.h>
42 #include "nilfs.h"
43 #include "export.h"
45 #define NILFS_FID_SIZE_NON_CONNECTABLE \
46 (offsetof(struct nilfs_fid, parent_gen) / 4)
47 #define NILFS_FID_SIZE_CONNECTABLE (sizeof(struct nilfs_fid) / 4)
49 static inline int nilfs_add_nondir(struct dentry *dentry, struct inode *inode)
51 int err = nilfs_add_link(dentry, inode);
52 if (!err) {
53 d_instantiate_new(dentry, inode);
54 return 0;
56 inode_dec_link_count(inode);
57 unlock_new_inode(inode);
58 iput(inode);
59 return err;
63 * Methods themselves.
66 static struct dentry *
67 nilfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
69 struct inode *inode;
70 ino_t ino;
72 if (dentry->d_name.len > NILFS_NAME_LEN)
73 return ERR_PTR(-ENAMETOOLONG);
75 ino = nilfs_inode_by_name(dir, &dentry->d_name);
76 inode = ino ? nilfs_iget(dir->i_sb, NILFS_I(dir)->i_root, ino) : NULL;
77 return d_splice_alias(inode, dentry);
81 * By the time this is called, we already have created
82 * the directory cache entry for the new file, but it
83 * is so far negative - it has no inode.
85 * If the create succeeds, we fill in the inode information
86 * with d_instantiate().
88 static int nilfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
89 bool excl)
91 struct inode *inode;
92 struct nilfs_transaction_info ti;
93 int err;
95 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
96 if (err)
97 return err;
98 inode = nilfs_new_inode(dir, mode);
99 err = PTR_ERR(inode);
100 if (!IS_ERR(inode)) {
101 inode->i_op = &nilfs_file_inode_operations;
102 inode->i_fop = &nilfs_file_operations;
103 inode->i_mapping->a_ops = &nilfs_aops;
104 nilfs_mark_inode_dirty(inode);
105 err = nilfs_add_nondir(dentry, inode);
107 if (!err)
108 err = nilfs_transaction_commit(dir->i_sb);
109 else
110 nilfs_transaction_abort(dir->i_sb);
112 return err;
115 static int
116 nilfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
118 struct inode *inode;
119 struct nilfs_transaction_info ti;
120 int err;
122 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
123 if (err)
124 return err;
125 inode = nilfs_new_inode(dir, mode);
126 err = PTR_ERR(inode);
127 if (!IS_ERR(inode)) {
128 init_special_inode(inode, inode->i_mode, rdev);
129 nilfs_mark_inode_dirty(inode);
130 err = nilfs_add_nondir(dentry, inode);
132 if (!err)
133 err = nilfs_transaction_commit(dir->i_sb);
134 else
135 nilfs_transaction_abort(dir->i_sb);
137 return err;
140 static int nilfs_symlink(struct inode *dir, struct dentry *dentry,
141 const char *symname)
143 struct nilfs_transaction_info ti;
144 struct super_block *sb = dir->i_sb;
145 unsigned l = strlen(symname)+1;
146 struct inode *inode;
147 int err;
149 if (l > sb->s_blocksize)
150 return -ENAMETOOLONG;
152 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
153 if (err)
154 return err;
156 inode = nilfs_new_inode(dir, S_IFLNK | S_IRWXUGO);
157 err = PTR_ERR(inode);
158 if (IS_ERR(inode))
159 goto out;
161 /* slow symlink */
162 inode->i_op = &nilfs_symlink_inode_operations;
163 inode->i_mapping->a_ops = &nilfs_aops;
164 err = page_symlink(inode, symname, l);
165 if (err)
166 goto out_fail;
168 /* mark_inode_dirty(inode); */
169 /* page_symlink() do this */
171 err = nilfs_add_nondir(dentry, inode);
172 out:
173 if (!err)
174 err = nilfs_transaction_commit(dir->i_sb);
175 else
176 nilfs_transaction_abort(dir->i_sb);
178 return err;
180 out_fail:
181 drop_nlink(inode);
182 nilfs_mark_inode_dirty(inode);
183 unlock_new_inode(inode);
184 iput(inode);
185 goto out;
188 static int nilfs_link(struct dentry *old_dentry, struct inode *dir,
189 struct dentry *dentry)
191 struct inode *inode = d_inode(old_dentry);
192 struct nilfs_transaction_info ti;
193 int err;
195 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
196 if (err)
197 return err;
199 inode->i_ctime = CURRENT_TIME;
200 inode_inc_link_count(inode);
201 ihold(inode);
203 err = nilfs_add_link(dentry, inode);
204 if (!err) {
205 d_instantiate(dentry, inode);
206 err = nilfs_transaction_commit(dir->i_sb);
207 } else {
208 inode_dec_link_count(inode);
209 iput(inode);
210 nilfs_transaction_abort(dir->i_sb);
213 return err;
216 static int nilfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
218 struct inode *inode;
219 struct nilfs_transaction_info ti;
220 int err;
222 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
223 if (err)
224 return err;
226 inc_nlink(dir);
228 inode = nilfs_new_inode(dir, S_IFDIR | mode);
229 err = PTR_ERR(inode);
230 if (IS_ERR(inode))
231 goto out_dir;
233 inode->i_op = &nilfs_dir_inode_operations;
234 inode->i_fop = &nilfs_dir_operations;
235 inode->i_mapping->a_ops = &nilfs_aops;
237 inc_nlink(inode);
239 err = nilfs_make_empty(inode, dir);
240 if (err)
241 goto out_fail;
243 err = nilfs_add_link(dentry, inode);
244 if (err)
245 goto out_fail;
247 nilfs_mark_inode_dirty(inode);
248 d_instantiate_new(dentry, inode);
249 out:
250 if (!err)
251 err = nilfs_transaction_commit(dir->i_sb);
252 else
253 nilfs_transaction_abort(dir->i_sb);
255 return err;
257 out_fail:
258 drop_nlink(inode);
259 drop_nlink(inode);
260 nilfs_mark_inode_dirty(inode);
261 unlock_new_inode(inode);
262 iput(inode);
263 out_dir:
264 drop_nlink(dir);
265 nilfs_mark_inode_dirty(dir);
266 goto out;
269 static int nilfs_do_unlink(struct inode *dir, struct dentry *dentry)
271 struct inode *inode;
272 struct nilfs_dir_entry *de;
273 struct page *page;
274 int err;
276 err = -ENOENT;
277 de = nilfs_find_entry(dir, &dentry->d_name, &page);
278 if (!de)
279 goto out;
281 inode = d_inode(dentry);
282 err = -EIO;
283 if (le64_to_cpu(de->inode) != inode->i_ino)
284 goto out;
286 if (!inode->i_nlink) {
287 nilfs_warning(inode->i_sb, __func__,
288 "deleting nonexistent file (%lu), %d\n",
289 inode->i_ino, inode->i_nlink);
290 set_nlink(inode, 1);
292 err = nilfs_delete_entry(de, page);
293 if (err)
294 goto out;
296 inode->i_ctime = dir->i_ctime;
297 drop_nlink(inode);
298 err = 0;
299 out:
300 return err;
303 static int nilfs_unlink(struct inode *dir, struct dentry *dentry)
305 struct nilfs_transaction_info ti;
306 int err;
308 err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
309 if (err)
310 return err;
312 err = nilfs_do_unlink(dir, dentry);
314 if (!err) {
315 nilfs_mark_inode_dirty(dir);
316 nilfs_mark_inode_dirty(d_inode(dentry));
317 err = nilfs_transaction_commit(dir->i_sb);
318 } else
319 nilfs_transaction_abort(dir->i_sb);
321 return err;
324 static int nilfs_rmdir(struct inode *dir, struct dentry *dentry)
326 struct inode *inode = d_inode(dentry);
327 struct nilfs_transaction_info ti;
328 int err;
330 err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
331 if (err)
332 return err;
334 err = -ENOTEMPTY;
335 if (nilfs_empty_dir(inode)) {
336 err = nilfs_do_unlink(dir, dentry);
337 if (!err) {
338 inode->i_size = 0;
339 drop_nlink(inode);
340 nilfs_mark_inode_dirty(inode);
341 drop_nlink(dir);
342 nilfs_mark_inode_dirty(dir);
345 if (!err)
346 err = nilfs_transaction_commit(dir->i_sb);
347 else
348 nilfs_transaction_abort(dir->i_sb);
350 return err;
353 static int nilfs_rename(struct inode *old_dir, struct dentry *old_dentry,
354 struct inode *new_dir, struct dentry *new_dentry)
356 struct inode *old_inode = d_inode(old_dentry);
357 struct inode *new_inode = d_inode(new_dentry);
358 struct page *dir_page = NULL;
359 struct nilfs_dir_entry *dir_de = NULL;
360 struct page *old_page;
361 struct nilfs_dir_entry *old_de;
362 struct nilfs_transaction_info ti;
363 int err;
365 err = nilfs_transaction_begin(old_dir->i_sb, &ti, 1);
366 if (unlikely(err))
367 return err;
369 err = -ENOENT;
370 old_de = nilfs_find_entry(old_dir, &old_dentry->d_name, &old_page);
371 if (!old_de)
372 goto out;
374 if (S_ISDIR(old_inode->i_mode)) {
375 err = -EIO;
376 dir_de = nilfs_dotdot(old_inode, &dir_page);
377 if (!dir_de)
378 goto out_old;
381 if (new_inode) {
382 struct page *new_page;
383 struct nilfs_dir_entry *new_de;
385 err = -ENOTEMPTY;
386 if (dir_de && !nilfs_empty_dir(new_inode))
387 goto out_dir;
389 err = -ENOENT;
390 new_de = nilfs_find_entry(new_dir, &new_dentry->d_name, &new_page);
391 if (!new_de)
392 goto out_dir;
393 nilfs_set_link(new_dir, new_de, new_page, old_inode);
394 nilfs_mark_inode_dirty(new_dir);
395 new_inode->i_ctime = CURRENT_TIME;
396 if (dir_de)
397 drop_nlink(new_inode);
398 drop_nlink(new_inode);
399 nilfs_mark_inode_dirty(new_inode);
400 } else {
401 err = nilfs_add_link(new_dentry, old_inode);
402 if (err)
403 goto out_dir;
404 if (dir_de) {
405 inc_nlink(new_dir);
406 nilfs_mark_inode_dirty(new_dir);
411 * Like most other Unix systems, set the ctime for inodes on a
412 * rename.
414 old_inode->i_ctime = CURRENT_TIME;
416 nilfs_delete_entry(old_de, old_page);
418 if (dir_de) {
419 nilfs_set_link(old_inode, dir_de, dir_page, new_dir);
420 drop_nlink(old_dir);
422 nilfs_mark_inode_dirty(old_dir);
423 nilfs_mark_inode_dirty(old_inode);
425 err = nilfs_transaction_commit(old_dir->i_sb);
426 return err;
428 out_dir:
429 if (dir_de) {
430 kunmap(dir_page);
431 page_cache_release(dir_page);
433 out_old:
434 kunmap(old_page);
435 page_cache_release(old_page);
436 out:
437 nilfs_transaction_abort(old_dir->i_sb);
438 return err;
442 * Export operations
444 static struct dentry *nilfs_get_parent(struct dentry *child)
446 unsigned long ino;
447 struct inode *inode;
448 struct qstr dotdot = QSTR_INIT("..", 2);
449 struct nilfs_root *root;
451 ino = nilfs_inode_by_name(d_inode(child), &dotdot);
452 if (!ino)
453 return ERR_PTR(-ENOENT);
455 root = NILFS_I(d_inode(child))->i_root;
457 inode = nilfs_iget(d_inode(child)->i_sb, root, ino);
458 if (IS_ERR(inode))
459 return ERR_CAST(inode);
461 return d_obtain_alias(inode);
464 static struct dentry *nilfs_get_dentry(struct super_block *sb, u64 cno,
465 u64 ino, u32 gen)
467 struct nilfs_root *root;
468 struct inode *inode;
470 if (ino < NILFS_FIRST_INO(sb) && ino != NILFS_ROOT_INO)
471 return ERR_PTR(-ESTALE);
473 root = nilfs_lookup_root(sb->s_fs_info, cno);
474 if (!root)
475 return ERR_PTR(-ESTALE);
477 inode = nilfs_iget(sb, root, ino);
478 nilfs_put_root(root);
480 if (IS_ERR(inode))
481 return ERR_CAST(inode);
482 if (gen && inode->i_generation != gen) {
483 iput(inode);
484 return ERR_PTR(-ESTALE);
486 return d_obtain_alias(inode);
489 static struct dentry *nilfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
490 int fh_len, int fh_type)
492 struct nilfs_fid *fid = (struct nilfs_fid *)fh;
494 if (fh_len < NILFS_FID_SIZE_NON_CONNECTABLE ||
495 (fh_type != FILEID_NILFS_WITH_PARENT &&
496 fh_type != FILEID_NILFS_WITHOUT_PARENT))
497 return NULL;
499 return nilfs_get_dentry(sb, fid->cno, fid->ino, fid->gen);
502 static struct dentry *nilfs_fh_to_parent(struct super_block *sb, struct fid *fh,
503 int fh_len, int fh_type)
505 struct nilfs_fid *fid = (struct nilfs_fid *)fh;
507 if (fh_len < NILFS_FID_SIZE_CONNECTABLE ||
508 fh_type != FILEID_NILFS_WITH_PARENT)
509 return NULL;
511 return nilfs_get_dentry(sb, fid->cno, fid->parent_ino, fid->parent_gen);
514 static int nilfs_encode_fh(struct inode *inode, __u32 *fh, int *lenp,
515 struct inode *parent)
517 struct nilfs_fid *fid = (struct nilfs_fid *)fh;
518 struct nilfs_root *root = NILFS_I(inode)->i_root;
519 int type;
521 if (parent && *lenp < NILFS_FID_SIZE_CONNECTABLE) {
522 *lenp = NILFS_FID_SIZE_CONNECTABLE;
523 return FILEID_INVALID;
525 if (*lenp < NILFS_FID_SIZE_NON_CONNECTABLE) {
526 *lenp = NILFS_FID_SIZE_NON_CONNECTABLE;
527 return FILEID_INVALID;
530 fid->cno = root->cno;
531 fid->ino = inode->i_ino;
532 fid->gen = inode->i_generation;
534 if (parent) {
535 fid->parent_ino = parent->i_ino;
536 fid->parent_gen = parent->i_generation;
537 type = FILEID_NILFS_WITH_PARENT;
538 *lenp = NILFS_FID_SIZE_CONNECTABLE;
539 } else {
540 type = FILEID_NILFS_WITHOUT_PARENT;
541 *lenp = NILFS_FID_SIZE_NON_CONNECTABLE;
544 return type;
547 const struct inode_operations nilfs_dir_inode_operations = {
548 .create = nilfs_create,
549 .lookup = nilfs_lookup,
550 .link = nilfs_link,
551 .unlink = nilfs_unlink,
552 .symlink = nilfs_symlink,
553 .mkdir = nilfs_mkdir,
554 .rmdir = nilfs_rmdir,
555 .mknod = nilfs_mknod,
556 .rename = nilfs_rename,
557 .setattr = nilfs_setattr,
558 .permission = nilfs_permission,
559 .fiemap = nilfs_fiemap,
562 const struct inode_operations nilfs_special_inode_operations = {
563 .setattr = nilfs_setattr,
564 .permission = nilfs_permission,
567 const struct inode_operations nilfs_symlink_inode_operations = {
568 .readlink = generic_readlink,
569 .follow_link = page_follow_link_light,
570 .put_link = page_put_link,
571 .permission = nilfs_permission,
574 const struct export_operations nilfs_export_ops = {
575 .encode_fh = nilfs_encode_fh,
576 .fh_to_dentry = nilfs_fh_to_dentry,
577 .fh_to_parent = nilfs_fh_to_parent,
578 .get_parent = nilfs_get_parent,