1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * NILFS pathname lookup operations.
4 *
5 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
6 *
7 * Modified for NILFS by Amagai Yoshiji and Ryusuke Konishi.
8 */
9 /*
10 * linux/fs/ext2/namei.c
11 *
12 * Copyright (C) 1992, 1993, 1994, 1995
13 * Remy Card (card@masi.ibp.fr)
14 * Laboratoire MASI - Institut Blaise Pascal
15 * Universite Pierre et Marie Curie (Paris VI)
16 *
17 * from
18 *
19 * linux/fs/minix/namei.c
20 *
21 * Copyright (C) 1991, 1992 Linus Torvalds
22 *
23 * Big-endian to little-endian byte-swapping/bitmaps by
24 * David S. Miller (davem@caip.rutgers.edu), 1995
25 */
26
27 #include <linux/pagemap.h>
28 #include "nilfs.h"
29 #include "export.h"
30
31 #define NILFS_FID_SIZE_NON_CONNECTABLE \
32 (offsetof(struct nilfs_fid, parent_gen) / 4)
33 #define NILFS_FID_SIZE_CONNECTABLE (sizeof(struct nilfs_fid) / 4)
34
nilfs_add_nondir(struct dentry * dentry,struct inode * inode)35 static inline int nilfs_add_nondir(struct dentry *dentry, struct inode *inode)
36 {
37 int err = nilfs_add_link(dentry, inode);
38
39 if (!err) {
40 d_instantiate_new(dentry, inode);
41 return 0;
42 }
43 inode_dec_link_count(inode);
44 unlock_new_inode(inode);
45 iput(inode);
46 return err;
47 }
48
49 /*
50 * Methods themselves.
51 */
52
53 static struct dentry *
nilfs_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)54 nilfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
55 {
56 struct inode *inode;
57 ino_t ino;
58 int res;
59
60 if (dentry->d_name.len > NILFS_NAME_LEN)
61 return ERR_PTR(-ENAMETOOLONG);
62
63 res = nilfs_inode_by_name(dir, &dentry->d_name, &ino);
64 if (res) {
65 if (res != -ENOENT)
66 return ERR_PTR(res);
67 inode = NULL;
68 } else {
69 inode = nilfs_iget(dir->i_sb, NILFS_I(dir)->i_root, ino);
70 if (inode == ERR_PTR(-ESTALE)) {
71 nilfs_error(dir->i_sb,
72 "deleted inode referenced: %lu", ino);
73 return ERR_PTR(-EIO);
74 }
75 }
76
77 return d_splice_alias(inode, dentry);
78 }
79
80 /*
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.
84 *
85 * If the create succeeds, we fill in the inode information
86 * with d_instantiate().
87 */
nilfs_create(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,bool excl)88 static int nilfs_create(struct mnt_idmap *idmap, struct inode *dir,
89 struct dentry *dentry, umode_t mode, bool excl)
90 {
91 struct inode *inode;
92 struct nilfs_transaction_info ti;
93 int err;
94
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);
106 }
107 if (!err)
108 err = nilfs_transaction_commit(dir->i_sb);
109 else
110 nilfs_transaction_abort(dir->i_sb);
111
112 return err;
113 }
114
115 static int
nilfs_mknod(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,dev_t rdev)116 nilfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
117 struct dentry *dentry, umode_t mode, dev_t rdev)
118 {
119 struct inode *inode;
120 struct nilfs_transaction_info ti;
121 int err;
122
123 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
124 if (err)
125 return err;
126 inode = nilfs_new_inode(dir, mode);
127 err = PTR_ERR(inode);
128 if (!IS_ERR(inode)) {
129 init_special_inode(inode, inode->i_mode, rdev);
130 nilfs_mark_inode_dirty(inode);
131 err = nilfs_add_nondir(dentry, inode);
132 }
133 if (!err)
134 err = nilfs_transaction_commit(dir->i_sb);
135 else
136 nilfs_transaction_abort(dir->i_sb);
137
138 return err;
139 }
140
nilfs_symlink(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,const char * symname)141 static int nilfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
142 struct dentry *dentry, const char *symname)
143 {
144 struct nilfs_transaction_info ti;
145 struct super_block *sb = dir->i_sb;
146 unsigned int l = strlen(symname) + 1;
147 struct inode *inode;
148 int err;
149
150 if (l > sb->s_blocksize)
151 return -ENAMETOOLONG;
152
153 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
154 if (err)
155 return err;
156
157 inode = nilfs_new_inode(dir, S_IFLNK | 0777);
158 err = PTR_ERR(inode);
159 if (IS_ERR(inode))
160 goto out;
161
162 /* slow symlink */
163 inode->i_op = &nilfs_symlink_inode_operations;
164 inode_nohighmem(inode);
165 mapping_set_gfp_mask(inode->i_mapping,
166 mapping_gfp_constraint(inode->i_mapping,
167 ~__GFP_FS));
168 inode->i_mapping->a_ops = &nilfs_aops;
169 err = page_symlink(inode, symname, l);
170 if (err)
171 goto out_fail;
172
173 /* mark_inode_dirty(inode); */
174 /* page_symlink() do this */
175
176 err = nilfs_add_nondir(dentry, inode);
177 out:
178 if (!err)
179 err = nilfs_transaction_commit(dir->i_sb);
180 else
181 nilfs_transaction_abort(dir->i_sb);
182
183 return err;
184
185 out_fail:
186 drop_nlink(inode);
187 nilfs_mark_inode_dirty(inode);
188 unlock_new_inode(inode);
189 iput(inode);
190 goto out;
191 }
192
nilfs_link(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)193 static int nilfs_link(struct dentry *old_dentry, struct inode *dir,
194 struct dentry *dentry)
195 {
196 struct inode *inode = d_inode(old_dentry);
197 struct nilfs_transaction_info ti;
198 int err;
199
200 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
201 if (err)
202 return err;
203
204 inode_set_ctime_current(inode);
205 inode_inc_link_count(inode);
206 ihold(inode);
207
208 err = nilfs_add_link(dentry, inode);
209 if (!err) {
210 d_instantiate(dentry, inode);
211 err = nilfs_transaction_commit(dir->i_sb);
212 } else {
213 inode_dec_link_count(inode);
214 iput(inode);
215 nilfs_transaction_abort(dir->i_sb);
216 }
217
218 return err;
219 }
220
nilfs_mkdir(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode)221 static int nilfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
222 struct dentry *dentry, umode_t mode)
223 {
224 struct inode *inode;
225 struct nilfs_transaction_info ti;
226 int err;
227
228 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
229 if (err)
230 return err;
231
232 inc_nlink(dir);
233
234 inode = nilfs_new_inode(dir, S_IFDIR | mode);
235 err = PTR_ERR(inode);
236 if (IS_ERR(inode))
237 goto out_dir;
238
239 inode->i_op = &nilfs_dir_inode_operations;
240 inode->i_fop = &nilfs_dir_operations;
241 inode->i_mapping->a_ops = &nilfs_aops;
242
243 inc_nlink(inode);
244
245 err = nilfs_make_empty(inode, dir);
246 if (err)
247 goto out_fail;
248
249 err = nilfs_add_link(dentry, inode);
250 if (err)
251 goto out_fail;
252
253 nilfs_mark_inode_dirty(inode);
254 d_instantiate_new(dentry, inode);
255 out:
256 if (!err)
257 err = nilfs_transaction_commit(dir->i_sb);
258 else
259 nilfs_transaction_abort(dir->i_sb);
260
261 return err;
262
263 out_fail:
264 drop_nlink(inode);
265 drop_nlink(inode);
266 nilfs_mark_inode_dirty(inode);
267 unlock_new_inode(inode);
268 iput(inode);
269 out_dir:
270 drop_nlink(dir);
271 nilfs_mark_inode_dirty(dir);
272 goto out;
273 }
274
nilfs_do_unlink(struct inode * dir,struct dentry * dentry)275 static int nilfs_do_unlink(struct inode *dir, struct dentry *dentry)
276 {
277 struct inode *inode;
278 struct nilfs_dir_entry *de;
279 struct page *page;
280 int err;
281
282 de = nilfs_find_entry(dir, &dentry->d_name, &page);
283 if (IS_ERR(de)) {
284 err = PTR_ERR(de);
285 goto out;
286 }
287
288 inode = d_inode(dentry);
289 err = -EIO;
290 if (le64_to_cpu(de->inode) != inode->i_ino)
291 goto out;
292
293 if (!inode->i_nlink) {
294 nilfs_warn(inode->i_sb,
295 "deleting nonexistent file (ino=%lu), %d",
296 inode->i_ino, inode->i_nlink);
297 set_nlink(inode, 1);
298 }
299 err = nilfs_delete_entry(de, page);
300 if (err)
301 goto out;
302
303 inode_set_ctime_to_ts(inode, inode_get_ctime(dir));
304 drop_nlink(inode);
305 err = 0;
306 out:
307 return err;
308 }
309
nilfs_unlink(struct inode * dir,struct dentry * dentry)310 static int nilfs_unlink(struct inode *dir, struct dentry *dentry)
311 {
312 struct nilfs_transaction_info ti;
313 int err;
314
315 err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
316 if (err)
317 return err;
318
319 err = nilfs_do_unlink(dir, dentry);
320
321 if (!err) {
322 nilfs_mark_inode_dirty(dir);
323 nilfs_mark_inode_dirty(d_inode(dentry));
324 err = nilfs_transaction_commit(dir->i_sb);
325 } else
326 nilfs_transaction_abort(dir->i_sb);
327
328 return err;
329 }
330
nilfs_rmdir(struct inode * dir,struct dentry * dentry)331 static int nilfs_rmdir(struct inode *dir, struct dentry *dentry)
332 {
333 struct inode *inode = d_inode(dentry);
334 struct nilfs_transaction_info ti;
335 int err;
336
337 err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
338 if (err)
339 return err;
340
341 err = -ENOTEMPTY;
342 if (nilfs_empty_dir(inode)) {
343 err = nilfs_do_unlink(dir, dentry);
344 if (!err) {
345 inode->i_size = 0;
346 drop_nlink(inode);
347 nilfs_mark_inode_dirty(inode);
348 drop_nlink(dir);
349 nilfs_mark_inode_dirty(dir);
350 }
351 }
352 if (!err)
353 err = nilfs_transaction_commit(dir->i_sb);
354 else
355 nilfs_transaction_abort(dir->i_sb);
356
357 return err;
358 }
359
nilfs_rename(struct mnt_idmap * idmap,struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)360 static int nilfs_rename(struct mnt_idmap *idmap,
361 struct inode *old_dir, struct dentry *old_dentry,
362 struct inode *new_dir, struct dentry *new_dentry,
363 unsigned int flags)
364 {
365 struct inode *old_inode = d_inode(old_dentry);
366 struct inode *new_inode = d_inode(new_dentry);
367 struct page *dir_page = NULL;
368 struct nilfs_dir_entry *dir_de = NULL;
369 struct page *old_page;
370 struct nilfs_dir_entry *old_de;
371 struct nilfs_transaction_info ti;
372 int err;
373
374 if (flags & ~RENAME_NOREPLACE)
375 return -EINVAL;
376
377 err = nilfs_transaction_begin(old_dir->i_sb, &ti, 1);
378 if (unlikely(err))
379 return err;
380
381 old_de = nilfs_find_entry(old_dir, &old_dentry->d_name, &old_page);
382 if (IS_ERR(old_de)) {
383 err = PTR_ERR(old_de);
384 goto out;
385 }
386
387 if (S_ISDIR(old_inode->i_mode)) {
388 err = -EIO;
389 dir_de = nilfs_dotdot(old_inode, &dir_page);
390 if (!dir_de)
391 goto out_old;
392 }
393
394 if (new_inode) {
395 struct page *new_page;
396 struct nilfs_dir_entry *new_de;
397
398 err = -ENOTEMPTY;
399 if (dir_de && !nilfs_empty_dir(new_inode))
400 goto out_dir;
401
402 new_de = nilfs_find_entry(new_dir, &new_dentry->d_name,
403 &new_page);
404 if (IS_ERR(new_de)) {
405 err = PTR_ERR(new_de);
406 goto out_dir;
407 }
408 nilfs_set_link(new_dir, new_de, new_page, old_inode);
409 nilfs_mark_inode_dirty(new_dir);
410 inode_set_ctime_current(new_inode);
411 if (dir_de)
412 drop_nlink(new_inode);
413 drop_nlink(new_inode);
414 nilfs_mark_inode_dirty(new_inode);
415 } else {
416 err = nilfs_add_link(new_dentry, old_inode);
417 if (err)
418 goto out_dir;
419 if (dir_de) {
420 inc_nlink(new_dir);
421 nilfs_mark_inode_dirty(new_dir);
422 }
423 }
424
425 /*
426 * Like most other Unix systems, set the ctime for inodes on a
427 * rename.
428 */
429 inode_set_ctime_current(old_inode);
430
431 nilfs_delete_entry(old_de, old_page);
432
433 if (dir_de) {
434 nilfs_set_link(old_inode, dir_de, dir_page, new_dir);
435 drop_nlink(old_dir);
436 }
437 nilfs_mark_inode_dirty(old_dir);
438 nilfs_mark_inode_dirty(old_inode);
439
440 err = nilfs_transaction_commit(old_dir->i_sb);
441 return err;
442
443 out_dir:
444 if (dir_de) {
445 kunmap(dir_page);
446 put_page(dir_page);
447 }
448 out_old:
449 kunmap(old_page);
450 put_page(old_page);
451 out:
452 nilfs_transaction_abort(old_dir->i_sb);
453 return err;
454 }
455
456 /*
457 * Export operations
458 */
nilfs_get_parent(struct dentry * child)459 static struct dentry *nilfs_get_parent(struct dentry *child)
460 {
461 ino_t ino;
462 int res;
463 struct inode *inode;
464 struct nilfs_root *root;
465
466 res = nilfs_inode_by_name(d_inode(child), &dotdot_name, &ino);
467 if (res)
468 return ERR_PTR(res);
469
470 root = NILFS_I(d_inode(child))->i_root;
471
472 inode = nilfs_iget(child->d_sb, root, ino);
473 if (IS_ERR(inode))
474 return ERR_CAST(inode);
475
476 return d_obtain_alias(inode);
477 }
478
nilfs_get_dentry(struct super_block * sb,u64 cno,u64 ino,u32 gen)479 static struct dentry *nilfs_get_dentry(struct super_block *sb, u64 cno,
480 u64 ino, u32 gen)
481 {
482 struct nilfs_root *root;
483 struct inode *inode;
484
485 if (ino < NILFS_FIRST_INO(sb) && ino != NILFS_ROOT_INO)
486 return ERR_PTR(-ESTALE);
487
488 root = nilfs_lookup_root(sb->s_fs_info, cno);
489 if (!root)
490 return ERR_PTR(-ESTALE);
491
492 inode = nilfs_iget(sb, root, ino);
493 nilfs_put_root(root);
494
495 if (IS_ERR(inode))
496 return ERR_CAST(inode);
497 if (gen && inode->i_generation != gen) {
498 iput(inode);
499 return ERR_PTR(-ESTALE);
500 }
501 return d_obtain_alias(inode);
502 }
503
nilfs_fh_to_dentry(struct super_block * sb,struct fid * fh,int fh_len,int fh_type)504 static struct dentry *nilfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
505 int fh_len, int fh_type)
506 {
507 struct nilfs_fid *fid = (struct nilfs_fid *)fh;
508
509 if (fh_len < NILFS_FID_SIZE_NON_CONNECTABLE ||
510 (fh_type != FILEID_NILFS_WITH_PARENT &&
511 fh_type != FILEID_NILFS_WITHOUT_PARENT))
512 return NULL;
513
514 return nilfs_get_dentry(sb, fid->cno, fid->ino, fid->gen);
515 }
516
nilfs_fh_to_parent(struct super_block * sb,struct fid * fh,int fh_len,int fh_type)517 static struct dentry *nilfs_fh_to_parent(struct super_block *sb, struct fid *fh,
518 int fh_len, int fh_type)
519 {
520 struct nilfs_fid *fid = (struct nilfs_fid *)fh;
521
522 if (fh_len < NILFS_FID_SIZE_CONNECTABLE ||
523 fh_type != FILEID_NILFS_WITH_PARENT)
524 return NULL;
525
526 return nilfs_get_dentry(sb, fid->cno, fid->parent_ino, fid->parent_gen);
527 }
528
nilfs_encode_fh(struct inode * inode,__u32 * fh,int * lenp,struct inode * parent)529 static int nilfs_encode_fh(struct inode *inode, __u32 *fh, int *lenp,
530 struct inode *parent)
531 {
532 struct nilfs_fid *fid = (struct nilfs_fid *)fh;
533 struct nilfs_root *root = NILFS_I(inode)->i_root;
534 int type;
535
536 if (parent && *lenp < NILFS_FID_SIZE_CONNECTABLE) {
537 *lenp = NILFS_FID_SIZE_CONNECTABLE;
538 return FILEID_INVALID;
539 }
540 if (*lenp < NILFS_FID_SIZE_NON_CONNECTABLE) {
541 *lenp = NILFS_FID_SIZE_NON_CONNECTABLE;
542 return FILEID_INVALID;
543 }
544
545 fid->cno = root->cno;
546 fid->ino = inode->i_ino;
547 fid->gen = inode->i_generation;
548
549 if (parent) {
550 fid->parent_ino = parent->i_ino;
551 fid->parent_gen = parent->i_generation;
552 type = FILEID_NILFS_WITH_PARENT;
553 *lenp = NILFS_FID_SIZE_CONNECTABLE;
554 } else {
555 type = FILEID_NILFS_WITHOUT_PARENT;
556 *lenp = NILFS_FID_SIZE_NON_CONNECTABLE;
557 }
558
559 return type;
560 }
561
562 const struct inode_operations nilfs_dir_inode_operations = {
563 .create = nilfs_create,
564 .lookup = nilfs_lookup,
565 .link = nilfs_link,
566 .unlink = nilfs_unlink,
567 .symlink = nilfs_symlink,
568 .mkdir = nilfs_mkdir,
569 .rmdir = nilfs_rmdir,
570 .mknod = nilfs_mknod,
571 .rename = nilfs_rename,
572 .setattr = nilfs_setattr,
573 .permission = nilfs_permission,
574 .fiemap = nilfs_fiemap,
575 .fileattr_get = nilfs_fileattr_get,
576 .fileattr_set = nilfs_fileattr_set,
577 };
578
579 const struct inode_operations nilfs_special_inode_operations = {
580 .setattr = nilfs_setattr,
581 .permission = nilfs_permission,
582 };
583
584 const struct inode_operations nilfs_symlink_inode_operations = {
585 .get_link = page_get_link,
586 .permission = nilfs_permission,
587 };
588
589 const struct export_operations nilfs_export_ops = {
590 .encode_fh = nilfs_encode_fh,
591 .fh_to_dentry = nilfs_fh_to_dentry,
592 .fh_to_parent = nilfs_fh_to_parent,
593 .get_parent = nilfs_get_parent,
594 };
595