xref: /linux/fs/nilfs2/namei.c (revision 4aa748dd1abf337426b4c941ae1b606ed0e2a5aa)
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 folio *folio;
280 	int err;
281 
282 	de = nilfs_find_entry(dir, &dentry->d_name, &folio);
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, folio);
300 	folio_release_kmap(folio, de);
301 	if (err)
302 		goto out;
303 
304 	inode_set_ctime_to_ts(inode, inode_get_ctime(dir));
305 	drop_nlink(inode);
306 	err = 0;
307 out:
308 	return err;
309 }
310 
nilfs_unlink(struct inode * dir,struct dentry * dentry)311 static int nilfs_unlink(struct inode *dir, struct dentry *dentry)
312 {
313 	struct nilfs_transaction_info ti;
314 	int err;
315 
316 	err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
317 	if (err)
318 		return err;
319 
320 	err = nilfs_do_unlink(dir, dentry);
321 
322 	if (!err) {
323 		nilfs_mark_inode_dirty(dir);
324 		nilfs_mark_inode_dirty(d_inode(dentry));
325 		err = nilfs_transaction_commit(dir->i_sb);
326 	} else
327 		nilfs_transaction_abort(dir->i_sb);
328 
329 	return err;
330 }
331 
nilfs_rmdir(struct inode * dir,struct dentry * dentry)332 static int nilfs_rmdir(struct inode *dir, struct dentry *dentry)
333 {
334 	struct inode *inode = d_inode(dentry);
335 	struct nilfs_transaction_info ti;
336 	int err;
337 
338 	err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
339 	if (err)
340 		return err;
341 
342 	err = -ENOTEMPTY;
343 	if (nilfs_empty_dir(inode)) {
344 		err = nilfs_do_unlink(dir, dentry);
345 		if (!err) {
346 			inode->i_size = 0;
347 			drop_nlink(inode);
348 			nilfs_mark_inode_dirty(inode);
349 			drop_nlink(dir);
350 			nilfs_mark_inode_dirty(dir);
351 		}
352 	}
353 	if (!err)
354 		err = nilfs_transaction_commit(dir->i_sb);
355 	else
356 		nilfs_transaction_abort(dir->i_sb);
357 
358 	return err;
359 }
360 
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)361 static int nilfs_rename(struct mnt_idmap *idmap,
362 			struct inode *old_dir, struct dentry *old_dentry,
363 			struct inode *new_dir, struct dentry *new_dentry,
364 			unsigned int flags)
365 {
366 	struct inode *old_inode = d_inode(old_dentry);
367 	struct inode *new_inode = d_inode(new_dentry);
368 	struct folio *dir_folio = NULL;
369 	struct nilfs_dir_entry *dir_de = NULL;
370 	struct folio *old_folio;
371 	struct nilfs_dir_entry *old_de;
372 	struct nilfs_transaction_info ti;
373 	int err;
374 
375 	if (flags & ~RENAME_NOREPLACE)
376 		return -EINVAL;
377 
378 	err = nilfs_transaction_begin(old_dir->i_sb, &ti, 1);
379 	if (unlikely(err))
380 		return err;
381 
382 	old_de = nilfs_find_entry(old_dir, &old_dentry->d_name, &old_folio);
383 	if (IS_ERR(old_de)) {
384 		err = PTR_ERR(old_de);
385 		goto out;
386 	}
387 
388 	if (S_ISDIR(old_inode->i_mode)) {
389 		err = -EIO;
390 		dir_de = nilfs_dotdot(old_inode, &dir_folio);
391 		if (!dir_de)
392 			goto out_old;
393 	}
394 
395 	if (new_inode) {
396 		struct folio *new_folio;
397 		struct nilfs_dir_entry *new_de;
398 
399 		err = -ENOTEMPTY;
400 		if (dir_de && !nilfs_empty_dir(new_inode))
401 			goto out_dir;
402 
403 		new_de = nilfs_find_entry(new_dir, &new_dentry->d_name,
404 					  &new_folio);
405 		if (IS_ERR(new_de)) {
406 			err = PTR_ERR(new_de);
407 			goto out_dir;
408 		}
409 		nilfs_set_link(new_dir, new_de, new_folio, old_inode);
410 		folio_release_kmap(new_folio, new_de);
411 		nilfs_mark_inode_dirty(new_dir);
412 		inode_set_ctime_current(new_inode);
413 		if (dir_de)
414 			drop_nlink(new_inode);
415 		drop_nlink(new_inode);
416 		nilfs_mark_inode_dirty(new_inode);
417 	} else {
418 		err = nilfs_add_link(new_dentry, old_inode);
419 		if (err)
420 			goto out_dir;
421 		if (dir_de) {
422 			inc_nlink(new_dir);
423 			nilfs_mark_inode_dirty(new_dir);
424 		}
425 	}
426 
427 	/*
428 	 * Like most other Unix systems, set the ctime for inodes on a
429 	 * rename.
430 	 */
431 	inode_set_ctime_current(old_inode);
432 
433 	nilfs_delete_entry(old_de, old_folio);
434 
435 	if (dir_de) {
436 		nilfs_set_link(old_inode, dir_de, dir_folio, new_dir);
437 		folio_release_kmap(dir_folio, dir_de);
438 		drop_nlink(old_dir);
439 	}
440 	folio_release_kmap(old_folio, old_de);
441 
442 	nilfs_mark_inode_dirty(old_dir);
443 	nilfs_mark_inode_dirty(old_inode);
444 
445 	err = nilfs_transaction_commit(old_dir->i_sb);
446 	return err;
447 
448 out_dir:
449 	if (dir_de)
450 		folio_release_kmap(dir_folio, dir_de);
451 out_old:
452 	folio_release_kmap(old_folio, old_de);
453 out:
454 	nilfs_transaction_abort(old_dir->i_sb);
455 	return err;
456 }
457 
458 /*
459  * Export operations
460  */
nilfs_get_parent(struct dentry * child)461 static struct dentry *nilfs_get_parent(struct dentry *child)
462 {
463 	ino_t ino;
464 	int res;
465 	struct nilfs_root *root;
466 
467 	res = nilfs_inode_by_name(d_inode(child), &dotdot_name, &ino);
468 	if (res)
469 		return ERR_PTR(res);
470 
471 	root = NILFS_I(d_inode(child))->i_root;
472 
473 	return d_obtain_alias(nilfs_iget(child->d_sb, root, ino));
474 }
475 
nilfs_get_dentry(struct super_block * sb,u64 cno,u64 ino,u32 gen)476 static struct dentry *nilfs_get_dentry(struct super_block *sb, u64 cno,
477 				       u64 ino, u32 gen)
478 {
479 	struct nilfs_root *root;
480 	struct inode *inode;
481 
482 	if (ino < NILFS_FIRST_INO(sb) && ino != NILFS_ROOT_INO)
483 		return ERR_PTR(-ESTALE);
484 
485 	root = nilfs_lookup_root(sb->s_fs_info, cno);
486 	if (!root)
487 		return ERR_PTR(-ESTALE);
488 
489 	inode = nilfs_iget(sb, root, ino);
490 	nilfs_put_root(root);
491 
492 	if (IS_ERR(inode))
493 		return ERR_CAST(inode);
494 	if (gen && inode->i_generation != gen) {
495 		iput(inode);
496 		return ERR_PTR(-ESTALE);
497 	}
498 	return d_obtain_alias(inode);
499 }
500 
nilfs_fh_to_dentry(struct super_block * sb,struct fid * fh,int fh_len,int fh_type)501 static struct dentry *nilfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
502 					 int fh_len, int fh_type)
503 {
504 	struct nilfs_fid *fid = (struct nilfs_fid *)fh;
505 
506 	if (fh_len < NILFS_FID_SIZE_NON_CONNECTABLE ||
507 	    (fh_type != FILEID_NILFS_WITH_PARENT &&
508 	     fh_type != FILEID_NILFS_WITHOUT_PARENT))
509 		return NULL;
510 
511 	return nilfs_get_dentry(sb, fid->cno, fid->ino, fid->gen);
512 }
513 
nilfs_fh_to_parent(struct super_block * sb,struct fid * fh,int fh_len,int fh_type)514 static struct dentry *nilfs_fh_to_parent(struct super_block *sb, struct fid *fh,
515 					 int fh_len, int fh_type)
516 {
517 	struct nilfs_fid *fid = (struct nilfs_fid *)fh;
518 
519 	if (fh_len < NILFS_FID_SIZE_CONNECTABLE ||
520 	    fh_type != FILEID_NILFS_WITH_PARENT)
521 		return NULL;
522 
523 	return nilfs_get_dentry(sb, fid->cno, fid->parent_ino, fid->parent_gen);
524 }
525 
nilfs_encode_fh(struct inode * inode,__u32 * fh,int * lenp,struct inode * parent)526 static int nilfs_encode_fh(struct inode *inode, __u32 *fh, int *lenp,
527 			   struct inode *parent)
528 {
529 	struct nilfs_fid *fid = (struct nilfs_fid *)fh;
530 	struct nilfs_root *root = NILFS_I(inode)->i_root;
531 	int type;
532 
533 	if (parent && *lenp < NILFS_FID_SIZE_CONNECTABLE) {
534 		*lenp = NILFS_FID_SIZE_CONNECTABLE;
535 		return FILEID_INVALID;
536 	}
537 	if (*lenp < NILFS_FID_SIZE_NON_CONNECTABLE) {
538 		*lenp = NILFS_FID_SIZE_NON_CONNECTABLE;
539 		return FILEID_INVALID;
540 	}
541 
542 	fid->cno = root->cno;
543 	fid->ino = inode->i_ino;
544 	fid->gen = inode->i_generation;
545 
546 	if (parent) {
547 		fid->parent_ino = parent->i_ino;
548 		fid->parent_gen = parent->i_generation;
549 		type = FILEID_NILFS_WITH_PARENT;
550 		*lenp = NILFS_FID_SIZE_CONNECTABLE;
551 	} else {
552 		type = FILEID_NILFS_WITHOUT_PARENT;
553 		*lenp = NILFS_FID_SIZE_NON_CONNECTABLE;
554 	}
555 
556 	return type;
557 }
558 
559 const struct inode_operations nilfs_dir_inode_operations = {
560 	.create		= nilfs_create,
561 	.lookup		= nilfs_lookup,
562 	.link		= nilfs_link,
563 	.unlink		= nilfs_unlink,
564 	.symlink	= nilfs_symlink,
565 	.mkdir		= nilfs_mkdir,
566 	.rmdir		= nilfs_rmdir,
567 	.mknod		= nilfs_mknod,
568 	.rename		= nilfs_rename,
569 	.setattr	= nilfs_setattr,
570 	.permission	= nilfs_permission,
571 	.fiemap		= nilfs_fiemap,
572 	.fileattr_get	= nilfs_fileattr_get,
573 	.fileattr_set	= nilfs_fileattr_set,
574 };
575 
576 const struct inode_operations nilfs_special_inode_operations = {
577 	.setattr	= nilfs_setattr,
578 	.permission	= nilfs_permission,
579 };
580 
581 const struct inode_operations nilfs_symlink_inode_operations = {
582 	.get_link	= page_get_link,
583 	.permission     = nilfs_permission,
584 };
585 
586 const struct export_operations nilfs_export_ops = {
587 	.encode_fh = nilfs_encode_fh,
588 	.fh_to_dentry = nilfs_fh_to_dentry,
589 	.fh_to_parent = nilfs_fh_to_parent,
590 	.get_parent = nilfs_get_parent,
591 };
592