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