Linux 4.9.169
[linux/fpc-iii.git] / fs / nilfs2 / namei.c
blob163131809e361b284dee809bd84ff6b9410ec88d
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 * Modified for NILFS by Amagai Yoshiji and Ryusuke Konishi.
19 * linux/fs/ext2/namei.c
21 * Copyright (C) 1992, 1993, 1994, 1995
22 * Remy Card (card@masi.ibp.fr)
23 * Laboratoire MASI - Institut Blaise Pascal
24 * Universite Pierre et Marie Curie (Paris VI)
26 * from
28 * linux/fs/minix/namei.c
30 * Copyright (C) 1991, 1992 Linus Torvalds
32 * Big-endian to little-endian byte-swapping/bitmaps by
33 * David S. Miller (davem@caip.rutgers.edu), 1995
36 #include <linux/pagemap.h>
37 #include "nilfs.h"
38 #include "export.h"
40 #define NILFS_FID_SIZE_NON_CONNECTABLE \
41 (offsetof(struct nilfs_fid, parent_gen) / 4)
42 #define NILFS_FID_SIZE_CONNECTABLE (sizeof(struct nilfs_fid) / 4)
44 static inline int nilfs_add_nondir(struct dentry *dentry, struct inode *inode)
46 int err = nilfs_add_link(dentry, inode);
48 if (!err) {
49 d_instantiate_new(dentry, inode);
50 return 0;
52 inode_dec_link_count(inode);
53 unlock_new_inode(inode);
54 iput(inode);
55 return err;
59 * Methods themselves.
62 static struct dentry *
63 nilfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
65 struct inode *inode;
66 ino_t ino;
68 if (dentry->d_name.len > NILFS_NAME_LEN)
69 return ERR_PTR(-ENAMETOOLONG);
71 ino = nilfs_inode_by_name(dir, &dentry->d_name);
72 inode = ino ? nilfs_iget(dir->i_sb, NILFS_I(dir)->i_root, ino) : NULL;
73 return d_splice_alias(inode, dentry);
77 * By the time this is called, we already have created
78 * the directory cache entry for the new file, but it
79 * is so far negative - it has no inode.
81 * If the create succeeds, we fill in the inode information
82 * with d_instantiate().
84 static int nilfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
85 bool excl)
87 struct inode *inode;
88 struct nilfs_transaction_info ti;
89 int err;
91 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
92 if (err)
93 return err;
94 inode = nilfs_new_inode(dir, mode);
95 err = PTR_ERR(inode);
96 if (!IS_ERR(inode)) {
97 inode->i_op = &nilfs_file_inode_operations;
98 inode->i_fop = &nilfs_file_operations;
99 inode->i_mapping->a_ops = &nilfs_aops;
100 nilfs_mark_inode_dirty(inode);
101 err = nilfs_add_nondir(dentry, inode);
103 if (!err)
104 err = nilfs_transaction_commit(dir->i_sb);
105 else
106 nilfs_transaction_abort(dir->i_sb);
108 return err;
111 static int
112 nilfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
114 struct inode *inode;
115 struct nilfs_transaction_info ti;
116 int err;
118 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
119 if (err)
120 return err;
121 inode = nilfs_new_inode(dir, mode);
122 err = PTR_ERR(inode);
123 if (!IS_ERR(inode)) {
124 init_special_inode(inode, inode->i_mode, rdev);
125 nilfs_mark_inode_dirty(inode);
126 err = nilfs_add_nondir(dentry, inode);
128 if (!err)
129 err = nilfs_transaction_commit(dir->i_sb);
130 else
131 nilfs_transaction_abort(dir->i_sb);
133 return err;
136 static int nilfs_symlink(struct inode *dir, struct dentry *dentry,
137 const char *symname)
139 struct nilfs_transaction_info ti;
140 struct super_block *sb = dir->i_sb;
141 unsigned int l = strlen(symname) + 1;
142 struct inode *inode;
143 int err;
145 if (l > sb->s_blocksize)
146 return -ENAMETOOLONG;
148 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
149 if (err)
150 return err;
152 inode = nilfs_new_inode(dir, S_IFLNK | S_IRWXUGO);
153 err = PTR_ERR(inode);
154 if (IS_ERR(inode))
155 goto out;
157 /* slow symlink */
158 inode->i_op = &nilfs_symlink_inode_operations;
159 inode_nohighmem(inode);
160 inode->i_mapping->a_ops = &nilfs_aops;
161 err = page_symlink(inode, symname, l);
162 if (err)
163 goto out_fail;
165 /* mark_inode_dirty(inode); */
166 /* page_symlink() do this */
168 err = nilfs_add_nondir(dentry, inode);
169 out:
170 if (!err)
171 err = nilfs_transaction_commit(dir->i_sb);
172 else
173 nilfs_transaction_abort(dir->i_sb);
175 return err;
177 out_fail:
178 drop_nlink(inode);
179 nilfs_mark_inode_dirty(inode);
180 unlock_new_inode(inode);
181 iput(inode);
182 goto out;
185 static int nilfs_link(struct dentry *old_dentry, struct inode *dir,
186 struct dentry *dentry)
188 struct inode *inode = d_inode(old_dentry);
189 struct nilfs_transaction_info ti;
190 int err;
192 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
193 if (err)
194 return err;
196 inode->i_ctime = current_time(inode);
197 inode_inc_link_count(inode);
198 ihold(inode);
200 err = nilfs_add_link(dentry, inode);
201 if (!err) {
202 d_instantiate(dentry, inode);
203 err = nilfs_transaction_commit(dir->i_sb);
204 } else {
205 inode_dec_link_count(inode);
206 iput(inode);
207 nilfs_transaction_abort(dir->i_sb);
210 return err;
213 static int nilfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
215 struct inode *inode;
216 struct nilfs_transaction_info ti;
217 int err;
219 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
220 if (err)
221 return err;
223 inc_nlink(dir);
225 inode = nilfs_new_inode(dir, S_IFDIR | mode);
226 err = PTR_ERR(inode);
227 if (IS_ERR(inode))
228 goto out_dir;
230 inode->i_op = &nilfs_dir_inode_operations;
231 inode->i_fop = &nilfs_dir_operations;
232 inode->i_mapping->a_ops = &nilfs_aops;
234 inc_nlink(inode);
236 err = nilfs_make_empty(inode, dir);
237 if (err)
238 goto out_fail;
240 err = nilfs_add_link(dentry, inode);
241 if (err)
242 goto out_fail;
244 nilfs_mark_inode_dirty(inode);
245 d_instantiate_new(dentry, inode);
246 out:
247 if (!err)
248 err = nilfs_transaction_commit(dir->i_sb);
249 else
250 nilfs_transaction_abort(dir->i_sb);
252 return err;
254 out_fail:
255 drop_nlink(inode);
256 drop_nlink(inode);
257 nilfs_mark_inode_dirty(inode);
258 unlock_new_inode(inode);
259 iput(inode);
260 out_dir:
261 drop_nlink(dir);
262 nilfs_mark_inode_dirty(dir);
263 goto out;
266 static int nilfs_do_unlink(struct inode *dir, struct dentry *dentry)
268 struct inode *inode;
269 struct nilfs_dir_entry *de;
270 struct page *page;
271 int err;
273 err = -ENOENT;
274 de = nilfs_find_entry(dir, &dentry->d_name, &page);
275 if (!de)
276 goto out;
278 inode = d_inode(dentry);
279 err = -EIO;
280 if (le64_to_cpu(de->inode) != inode->i_ino)
281 goto out;
283 if (!inode->i_nlink) {
284 nilfs_msg(inode->i_sb, KERN_WARNING,
285 "deleting nonexistent file (ino=%lu), %d",
286 inode->i_ino, inode->i_nlink);
287 set_nlink(inode, 1);
289 err = nilfs_delete_entry(de, page);
290 if (err)
291 goto out;
293 inode->i_ctime = dir->i_ctime;
294 drop_nlink(inode);
295 err = 0;
296 out:
297 return err;
300 static int nilfs_unlink(struct inode *dir, struct dentry *dentry)
302 struct nilfs_transaction_info ti;
303 int err;
305 err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
306 if (err)
307 return err;
309 err = nilfs_do_unlink(dir, dentry);
311 if (!err) {
312 nilfs_mark_inode_dirty(dir);
313 nilfs_mark_inode_dirty(d_inode(dentry));
314 err = nilfs_transaction_commit(dir->i_sb);
315 } else
316 nilfs_transaction_abort(dir->i_sb);
318 return err;
321 static int nilfs_rmdir(struct inode *dir, struct dentry *dentry)
323 struct inode *inode = d_inode(dentry);
324 struct nilfs_transaction_info ti;
325 int err;
327 err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
328 if (err)
329 return err;
331 err = -ENOTEMPTY;
332 if (nilfs_empty_dir(inode)) {
333 err = nilfs_do_unlink(dir, dentry);
334 if (!err) {
335 inode->i_size = 0;
336 drop_nlink(inode);
337 nilfs_mark_inode_dirty(inode);
338 drop_nlink(dir);
339 nilfs_mark_inode_dirty(dir);
342 if (!err)
343 err = nilfs_transaction_commit(dir->i_sb);
344 else
345 nilfs_transaction_abort(dir->i_sb);
347 return err;
350 static int nilfs_rename(struct inode *old_dir, struct dentry *old_dentry,
351 struct inode *new_dir, struct dentry *new_dentry,
352 unsigned int flags)
354 struct inode *old_inode = d_inode(old_dentry);
355 struct inode *new_inode = d_inode(new_dentry);
356 struct page *dir_page = NULL;
357 struct nilfs_dir_entry *dir_de = NULL;
358 struct page *old_page;
359 struct nilfs_dir_entry *old_de;
360 struct nilfs_transaction_info ti;
361 int err;
363 if (flags & ~RENAME_NOREPLACE)
364 return -EINVAL;
366 err = nilfs_transaction_begin(old_dir->i_sb, &ti, 1);
367 if (unlikely(err))
368 return err;
370 err = -ENOENT;
371 old_de = nilfs_find_entry(old_dir, &old_dentry->d_name, &old_page);
372 if (!old_de)
373 goto out;
375 if (S_ISDIR(old_inode->i_mode)) {
376 err = -EIO;
377 dir_de = nilfs_dotdot(old_inode, &dir_page);
378 if (!dir_de)
379 goto out_old;
382 if (new_inode) {
383 struct page *new_page;
384 struct nilfs_dir_entry *new_de;
386 err = -ENOTEMPTY;
387 if (dir_de && !nilfs_empty_dir(new_inode))
388 goto out_dir;
390 err = -ENOENT;
391 new_de = nilfs_find_entry(new_dir, &new_dentry->d_name, &new_page);
392 if (!new_de)
393 goto out_dir;
394 nilfs_set_link(new_dir, new_de, new_page, old_inode);
395 nilfs_mark_inode_dirty(new_dir);
396 new_inode->i_ctime = current_time(new_inode);
397 if (dir_de)
398 drop_nlink(new_inode);
399 drop_nlink(new_inode);
400 nilfs_mark_inode_dirty(new_inode);
401 } else {
402 err = nilfs_add_link(new_dentry, old_inode);
403 if (err)
404 goto out_dir;
405 if (dir_de) {
406 inc_nlink(new_dir);
407 nilfs_mark_inode_dirty(new_dir);
412 * Like most other Unix systems, set the ctime for inodes on a
413 * rename.
415 old_inode->i_ctime = current_time(old_inode);
417 nilfs_delete_entry(old_de, old_page);
419 if (dir_de) {
420 nilfs_set_link(old_inode, dir_de, dir_page, new_dir);
421 drop_nlink(old_dir);
423 nilfs_mark_inode_dirty(old_dir);
424 nilfs_mark_inode_dirty(old_inode);
426 err = nilfs_transaction_commit(old_dir->i_sb);
427 return err;
429 out_dir:
430 if (dir_de) {
431 kunmap(dir_page);
432 put_page(dir_page);
434 out_old:
435 kunmap(old_page);
436 put_page(old_page);
437 out:
438 nilfs_transaction_abort(old_dir->i_sb);
439 return err;
443 * Export operations
445 static struct dentry *nilfs_get_parent(struct dentry *child)
447 unsigned long ino;
448 struct inode *inode;
449 struct qstr dotdot = QSTR_INIT("..", 2);
450 struct nilfs_root *root;
452 ino = nilfs_inode_by_name(d_inode(child), &dotdot);
453 if (!ino)
454 return ERR_PTR(-ENOENT);
456 root = NILFS_I(d_inode(child))->i_root;
458 inode = nilfs_iget(child->d_sb, root, ino);
459 if (IS_ERR(inode))
460 return ERR_CAST(inode);
462 return d_obtain_alias(inode);
465 static struct dentry *nilfs_get_dentry(struct super_block *sb, u64 cno,
466 u64 ino, u32 gen)
468 struct nilfs_root *root;
469 struct inode *inode;
471 if (ino < NILFS_FIRST_INO(sb) && ino != NILFS_ROOT_INO)
472 return ERR_PTR(-ESTALE);
474 root = nilfs_lookup_root(sb->s_fs_info, cno);
475 if (!root)
476 return ERR_PTR(-ESTALE);
478 inode = nilfs_iget(sb, root, ino);
479 nilfs_put_root(root);
481 if (IS_ERR(inode))
482 return ERR_CAST(inode);
483 if (gen && inode->i_generation != gen) {
484 iput(inode);
485 return ERR_PTR(-ESTALE);
487 return d_obtain_alias(inode);
490 static struct dentry *nilfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
491 int fh_len, int fh_type)
493 struct nilfs_fid *fid = (struct nilfs_fid *)fh;
495 if (fh_len < NILFS_FID_SIZE_NON_CONNECTABLE ||
496 (fh_type != FILEID_NILFS_WITH_PARENT &&
497 fh_type != FILEID_NILFS_WITHOUT_PARENT))
498 return NULL;
500 return nilfs_get_dentry(sb, fid->cno, fid->ino, fid->gen);
503 static struct dentry *nilfs_fh_to_parent(struct super_block *sb, struct fid *fh,
504 int fh_len, int fh_type)
506 struct nilfs_fid *fid = (struct nilfs_fid *)fh;
508 if (fh_len < NILFS_FID_SIZE_CONNECTABLE ||
509 fh_type != FILEID_NILFS_WITH_PARENT)
510 return NULL;
512 return nilfs_get_dentry(sb, fid->cno, fid->parent_ino, fid->parent_gen);
515 static int nilfs_encode_fh(struct inode *inode, __u32 *fh, int *lenp,
516 struct inode *parent)
518 struct nilfs_fid *fid = (struct nilfs_fid *)fh;
519 struct nilfs_root *root = NILFS_I(inode)->i_root;
520 int type;
522 if (parent && *lenp < NILFS_FID_SIZE_CONNECTABLE) {
523 *lenp = NILFS_FID_SIZE_CONNECTABLE;
524 return FILEID_INVALID;
526 if (*lenp < NILFS_FID_SIZE_NON_CONNECTABLE) {
527 *lenp = NILFS_FID_SIZE_NON_CONNECTABLE;
528 return FILEID_INVALID;
531 fid->cno = root->cno;
532 fid->ino = inode->i_ino;
533 fid->gen = inode->i_generation;
535 if (parent) {
536 fid->parent_ino = parent->i_ino;
537 fid->parent_gen = parent->i_generation;
538 type = FILEID_NILFS_WITH_PARENT;
539 *lenp = NILFS_FID_SIZE_CONNECTABLE;
540 } else {
541 type = FILEID_NILFS_WITHOUT_PARENT;
542 *lenp = NILFS_FID_SIZE_NON_CONNECTABLE;
545 return type;
548 const struct inode_operations nilfs_dir_inode_operations = {
549 .create = nilfs_create,
550 .lookup = nilfs_lookup,
551 .link = nilfs_link,
552 .unlink = nilfs_unlink,
553 .symlink = nilfs_symlink,
554 .mkdir = nilfs_mkdir,
555 .rmdir = nilfs_rmdir,
556 .mknod = nilfs_mknod,
557 .rename = nilfs_rename,
558 .setattr = nilfs_setattr,
559 .permission = nilfs_permission,
560 .fiemap = nilfs_fiemap,
563 const struct inode_operations nilfs_special_inode_operations = {
564 .setattr = nilfs_setattr,
565 .permission = nilfs_permission,
568 const struct inode_operations nilfs_symlink_inode_operations = {
569 .readlink = generic_readlink,
570 .get_link = page_get_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,