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 }
71
72 return d_splice_alias(inode, dentry);
73 }
74
75 /*
76 * By the time this is called, we already have created
77 * the directory cache entry for the new file, but it
78 * is so far negative - it has no inode.
79 *
80 * If the create succeeds, we fill in the inode information
81 * with d_instantiate().
82 */
nilfs_create(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,bool excl)83 static int nilfs_create(struct mnt_idmap *idmap, struct inode *dir,
84 struct dentry *dentry, umode_t mode, bool excl)
85 {
86 struct inode *inode;
87 struct nilfs_transaction_info ti;
88 int err;
89
90 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
91 if (err)
92 return err;
93 inode = nilfs_new_inode(dir, mode);
94 err = PTR_ERR(inode);
95 if (!IS_ERR(inode)) {
96 inode->i_op = &nilfs_file_inode_operations;
97 inode->i_fop = &nilfs_file_operations;
98 inode->i_mapping->a_ops = &nilfs_aops;
99 nilfs_mark_inode_dirty(inode);
100 err = nilfs_add_nondir(dentry, inode);
101 }
102 if (!err)
103 err = nilfs_transaction_commit(dir->i_sb);
104 else
105 nilfs_transaction_abort(dir->i_sb);
106
107 return err;
108 }
109
110 static int
nilfs_mknod(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,dev_t rdev)111 nilfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
112 struct dentry *dentry, umode_t mode, dev_t rdev)
113 {
114 struct inode *inode;
115 struct nilfs_transaction_info ti;
116 int err;
117
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);
127 }
128 if (!err)
129 err = nilfs_transaction_commit(dir->i_sb);
130 else
131 nilfs_transaction_abort(dir->i_sb);
132
133 return err;
134 }
135
nilfs_symlink(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,const char * symname)136 static int nilfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
137 struct dentry *dentry, const char *symname)
138 {
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;
144
145 if (l > sb->s_blocksize)
146 return -ENAMETOOLONG;
147
148 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
149 if (err)
150 return err;
151
152 inode = nilfs_new_inode(dir, S_IFLNK | 0777);
153 err = PTR_ERR(inode);
154 if (IS_ERR(inode))
155 goto out;
156
157 /* slow symlink */
158 inode->i_op = &nilfs_symlink_inode_operations;
159 inode_nohighmem(inode);
160 mapping_set_gfp_mask(inode->i_mapping,
161 mapping_gfp_constraint(inode->i_mapping,
162 ~__GFP_FS));
163 inode->i_mapping->a_ops = &nilfs_aops;
164 err = page_symlink(inode, symname, l);
165 if (err)
166 goto out_fail;
167
168 /* mark_inode_dirty(inode); */
169 /* page_symlink() do this */
170
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);
177
178 return err;
179
180 out_fail:
181 drop_nlink(inode);
182 nilfs_mark_inode_dirty(inode);
183 unlock_new_inode(inode);
184 iput(inode);
185 goto out;
186 }
187
nilfs_link(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)188 static int nilfs_link(struct dentry *old_dentry, struct inode *dir,
189 struct dentry *dentry)
190 {
191 struct inode *inode = d_inode(old_dentry);
192 struct nilfs_transaction_info ti;
193 int err;
194
195 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
196 if (err)
197 return err;
198
199 inode_set_ctime_current(inode);
200 inode_inc_link_count(inode);
201 ihold(inode);
202
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);
211 }
212
213 return err;
214 }
215
nilfs_mkdir(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode)216 static int nilfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
217 struct dentry *dentry, umode_t mode)
218 {
219 struct inode *inode;
220 struct nilfs_transaction_info ti;
221 int err;
222
223 err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
224 if (err)
225 return err;
226
227 inc_nlink(dir);
228
229 inode = nilfs_new_inode(dir, S_IFDIR | mode);
230 err = PTR_ERR(inode);
231 if (IS_ERR(inode))
232 goto out_dir;
233
234 inode->i_op = &nilfs_dir_inode_operations;
235 inode->i_fop = &nilfs_dir_operations;
236 inode->i_mapping->a_ops = &nilfs_aops;
237
238 inc_nlink(inode);
239
240 err = nilfs_make_empty(inode, dir);
241 if (err)
242 goto out_fail;
243
244 err = nilfs_add_link(dentry, inode);
245 if (err)
246 goto out_fail;
247
248 nilfs_mark_inode_dirty(inode);
249 d_instantiate_new(dentry, inode);
250 out:
251 if (!err)
252 err = nilfs_transaction_commit(dir->i_sb);
253 else
254 nilfs_transaction_abort(dir->i_sb);
255
256 return err;
257
258 out_fail:
259 drop_nlink(inode);
260 drop_nlink(inode);
261 nilfs_mark_inode_dirty(inode);
262 unlock_new_inode(inode);
263 iput(inode);
264 out_dir:
265 drop_nlink(dir);
266 nilfs_mark_inode_dirty(dir);
267 goto out;
268 }
269
nilfs_do_unlink(struct inode * dir,struct dentry * dentry)270 static int nilfs_do_unlink(struct inode *dir, struct dentry *dentry)
271 {
272 struct inode *inode;
273 struct nilfs_dir_entry *de;
274 struct page *page;
275 int err;
276
277 de = nilfs_find_entry(dir, &dentry->d_name, &page);
278 if (IS_ERR(de)) {
279 err = PTR_ERR(de);
280 goto out;
281 }
282
283 inode = d_inode(dentry);
284 err = -EIO;
285 if (le64_to_cpu(de->inode) != inode->i_ino)
286 goto out;
287
288 if (!inode->i_nlink) {
289 nilfs_warn(inode->i_sb,
290 "deleting nonexistent file (ino=%lu), %d",
291 inode->i_ino, inode->i_nlink);
292 set_nlink(inode, 1);
293 }
294 err = nilfs_delete_entry(de, page);
295 if (err)
296 goto out;
297
298 inode_set_ctime_to_ts(inode, inode_get_ctime(dir));
299 drop_nlink(inode);
300 err = 0;
301 out:
302 return err;
303 }
304
nilfs_unlink(struct inode * dir,struct dentry * dentry)305 static int nilfs_unlink(struct inode *dir, struct dentry *dentry)
306 {
307 struct nilfs_transaction_info ti;
308 int err;
309
310 err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
311 if (err)
312 return err;
313
314 err = nilfs_do_unlink(dir, dentry);
315
316 if (!err) {
317 nilfs_mark_inode_dirty(dir);
318 nilfs_mark_inode_dirty(d_inode(dentry));
319 err = nilfs_transaction_commit(dir->i_sb);
320 } else
321 nilfs_transaction_abort(dir->i_sb);
322
323 return err;
324 }
325
nilfs_rmdir(struct inode * dir,struct dentry * dentry)326 static int nilfs_rmdir(struct inode *dir, struct dentry *dentry)
327 {
328 struct inode *inode = d_inode(dentry);
329 struct nilfs_transaction_info ti;
330 int err;
331
332 err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
333 if (err)
334 return err;
335
336 err = -ENOTEMPTY;
337 if (nilfs_empty_dir(inode)) {
338 err = nilfs_do_unlink(dir, dentry);
339 if (!err) {
340 inode->i_size = 0;
341 drop_nlink(inode);
342 nilfs_mark_inode_dirty(inode);
343 drop_nlink(dir);
344 nilfs_mark_inode_dirty(dir);
345 }
346 }
347 if (!err)
348 err = nilfs_transaction_commit(dir->i_sb);
349 else
350 nilfs_transaction_abort(dir->i_sb);
351
352 return err;
353 }
354
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)355 static int nilfs_rename(struct mnt_idmap *idmap,
356 struct inode *old_dir, struct dentry *old_dentry,
357 struct inode *new_dir, struct dentry *new_dentry,
358 unsigned int flags)
359 {
360 struct inode *old_inode = d_inode(old_dentry);
361 struct inode *new_inode = d_inode(new_dentry);
362 struct page *dir_page = NULL;
363 struct nilfs_dir_entry *dir_de = NULL;
364 struct page *old_page;
365 struct nilfs_dir_entry *old_de;
366 struct nilfs_transaction_info ti;
367 int err;
368
369 if (flags & ~RENAME_NOREPLACE)
370 return -EINVAL;
371
372 err = nilfs_transaction_begin(old_dir->i_sb, &ti, 1);
373 if (unlikely(err))
374 return err;
375
376 old_de = nilfs_find_entry(old_dir, &old_dentry->d_name, &old_page);
377 if (IS_ERR(old_de)) {
378 err = PTR_ERR(old_de);
379 goto out;
380 }
381
382 if (S_ISDIR(old_inode->i_mode)) {
383 err = -EIO;
384 dir_de = nilfs_dotdot(old_inode, &dir_page);
385 if (!dir_de)
386 goto out_old;
387 }
388
389 if (new_inode) {
390 struct page *new_page;
391 struct nilfs_dir_entry *new_de;
392
393 err = -ENOTEMPTY;
394 if (dir_de && !nilfs_empty_dir(new_inode))
395 goto out_dir;
396
397 new_de = nilfs_find_entry(new_dir, &new_dentry->d_name,
398 &new_page);
399 if (IS_ERR(new_de)) {
400 err = PTR_ERR(new_de);
401 goto out_dir;
402 }
403 nilfs_set_link(new_dir, new_de, new_page, old_inode);
404 nilfs_mark_inode_dirty(new_dir);
405 inode_set_ctime_current(new_inode);
406 if (dir_de)
407 drop_nlink(new_inode);
408 drop_nlink(new_inode);
409 nilfs_mark_inode_dirty(new_inode);
410 } else {
411 err = nilfs_add_link(new_dentry, old_inode);
412 if (err)
413 goto out_dir;
414 if (dir_de) {
415 inc_nlink(new_dir);
416 nilfs_mark_inode_dirty(new_dir);
417 }
418 }
419
420 /*
421 * Like most other Unix systems, set the ctime for inodes on a
422 * rename.
423 */
424 inode_set_ctime_current(old_inode);
425
426 nilfs_delete_entry(old_de, old_page);
427
428 if (dir_de) {
429 nilfs_set_link(old_inode, dir_de, dir_page, new_dir);
430 drop_nlink(old_dir);
431 }
432 nilfs_mark_inode_dirty(old_dir);
433 nilfs_mark_inode_dirty(old_inode);
434
435 err = nilfs_transaction_commit(old_dir->i_sb);
436 return err;
437
438 out_dir:
439 if (dir_de) {
440 kunmap(dir_page);
441 put_page(dir_page);
442 }
443 out_old:
444 kunmap(old_page);
445 put_page(old_page);
446 out:
447 nilfs_transaction_abort(old_dir->i_sb);
448 return err;
449 }
450
451 /*
452 * Export operations
453 */
nilfs_get_parent(struct dentry * child)454 static struct dentry *nilfs_get_parent(struct dentry *child)
455 {
456 ino_t ino;
457 int res;
458 struct inode *inode;
459 struct nilfs_root *root;
460
461 res = nilfs_inode_by_name(d_inode(child), &dotdot_name, &ino);
462 if (res)
463 return ERR_PTR(res);
464
465 root = NILFS_I(d_inode(child))->i_root;
466
467 inode = nilfs_iget(child->d_sb, root, ino);
468 if (IS_ERR(inode))
469 return ERR_CAST(inode);
470
471 return d_obtain_alias(inode);
472 }
473
nilfs_get_dentry(struct super_block * sb,u64 cno,u64 ino,u32 gen)474 static struct dentry *nilfs_get_dentry(struct super_block *sb, u64 cno,
475 u64 ino, u32 gen)
476 {
477 struct nilfs_root *root;
478 struct inode *inode;
479
480 if (ino < NILFS_FIRST_INO(sb) && ino != NILFS_ROOT_INO)
481 return ERR_PTR(-ESTALE);
482
483 root = nilfs_lookup_root(sb->s_fs_info, cno);
484 if (!root)
485 return ERR_PTR(-ESTALE);
486
487 inode = nilfs_iget(sb, root, ino);
488 nilfs_put_root(root);
489
490 if (IS_ERR(inode))
491 return ERR_CAST(inode);
492 if (gen && inode->i_generation != gen) {
493 iput(inode);
494 return ERR_PTR(-ESTALE);
495 }
496 return d_obtain_alias(inode);
497 }
498
nilfs_fh_to_dentry(struct super_block * sb,struct fid * fh,int fh_len,int fh_type)499 static struct dentry *nilfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
500 int fh_len, int fh_type)
501 {
502 struct nilfs_fid *fid = (struct nilfs_fid *)fh;
503
504 if (fh_len < NILFS_FID_SIZE_NON_CONNECTABLE ||
505 (fh_type != FILEID_NILFS_WITH_PARENT &&
506 fh_type != FILEID_NILFS_WITHOUT_PARENT))
507 return NULL;
508
509 return nilfs_get_dentry(sb, fid->cno, fid->ino, fid->gen);
510 }
511
nilfs_fh_to_parent(struct super_block * sb,struct fid * fh,int fh_len,int fh_type)512 static struct dentry *nilfs_fh_to_parent(struct super_block *sb, struct fid *fh,
513 int fh_len, int fh_type)
514 {
515 struct nilfs_fid *fid = (struct nilfs_fid *)fh;
516
517 if (fh_len < NILFS_FID_SIZE_CONNECTABLE ||
518 fh_type != FILEID_NILFS_WITH_PARENT)
519 return NULL;
520
521 return nilfs_get_dentry(sb, fid->cno, fid->parent_ino, fid->parent_gen);
522 }
523
nilfs_encode_fh(struct inode * inode,__u32 * fh,int * lenp,struct inode * parent)524 static int nilfs_encode_fh(struct inode *inode, __u32 *fh, int *lenp,
525 struct inode *parent)
526 {
527 struct nilfs_fid *fid = (struct nilfs_fid *)fh;
528 struct nilfs_root *root = NILFS_I(inode)->i_root;
529 int type;
530
531 if (parent && *lenp < NILFS_FID_SIZE_CONNECTABLE) {
532 *lenp = NILFS_FID_SIZE_CONNECTABLE;
533 return FILEID_INVALID;
534 }
535 if (*lenp < NILFS_FID_SIZE_NON_CONNECTABLE) {
536 *lenp = NILFS_FID_SIZE_NON_CONNECTABLE;
537 return FILEID_INVALID;
538 }
539
540 fid->cno = root->cno;
541 fid->ino = inode->i_ino;
542 fid->gen = inode->i_generation;
543
544 if (parent) {
545 fid->parent_ino = parent->i_ino;
546 fid->parent_gen = parent->i_generation;
547 type = FILEID_NILFS_WITH_PARENT;
548 *lenp = NILFS_FID_SIZE_CONNECTABLE;
549 } else {
550 type = FILEID_NILFS_WITHOUT_PARENT;
551 *lenp = NILFS_FID_SIZE_NON_CONNECTABLE;
552 }
553
554 return type;
555 }
556
557 const struct inode_operations nilfs_dir_inode_operations = {
558 .create = nilfs_create,
559 .lookup = nilfs_lookup,
560 .link = nilfs_link,
561 .unlink = nilfs_unlink,
562 .symlink = nilfs_symlink,
563 .mkdir = nilfs_mkdir,
564 .rmdir = nilfs_rmdir,
565 .mknod = nilfs_mknod,
566 .rename = nilfs_rename,
567 .setattr = nilfs_setattr,
568 .permission = nilfs_permission,
569 .fiemap = nilfs_fiemap,
570 .fileattr_get = nilfs_fileattr_get,
571 .fileattr_set = nilfs_fileattr_set,
572 };
573
574 const struct inode_operations nilfs_special_inode_operations = {
575 .setattr = nilfs_setattr,
576 .permission = nilfs_permission,
577 };
578
579 const struct inode_operations nilfs_symlink_inode_operations = {
580 .get_link = page_get_link,
581 .permission = nilfs_permission,
582 };
583
584 const struct export_operations nilfs_export_ops = {
585 .encode_fh = nilfs_encode_fh,
586 .fh_to_dentry = nilfs_fh_to_dentry,
587 .fh_to_parent = nilfs_fh_to_parent,
588 .get_parent = nilfs_get_parent,
589 };
590