xref: /openbmc/linux/fs/f2fs/namei.c (revision 4cff79e9)
1 /*
2  * fs/f2fs/namei.c
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5  *             http://www.samsung.com/
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 #include <linux/fs.h>
12 #include <linux/f2fs_fs.h>
13 #include <linux/pagemap.h>
14 #include <linux/sched.h>
15 #include <linux/ctype.h>
16 #include <linux/dcache.h>
17 #include <linux/namei.h>
18 #include <linux/quotaops.h>
19 
20 #include "f2fs.h"
21 #include "node.h"
22 #include "xattr.h"
23 #include "acl.h"
24 #include <trace/events/f2fs.h>
25 
26 static struct inode *f2fs_new_inode(struct inode *dir, umode_t mode)
27 {
28 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
29 	nid_t ino;
30 	struct inode *inode;
31 	bool nid_free = false;
32 	int xattr_size = 0;
33 	int err;
34 
35 	inode = new_inode(dir->i_sb);
36 	if (!inode)
37 		return ERR_PTR(-ENOMEM);
38 
39 	f2fs_lock_op(sbi);
40 	if (!alloc_nid(sbi, &ino)) {
41 		f2fs_unlock_op(sbi);
42 		err = -ENOSPC;
43 		goto fail;
44 	}
45 	f2fs_unlock_op(sbi);
46 
47 	nid_free = true;
48 
49 	inode_init_owner(inode, dir, mode);
50 
51 	inode->i_ino = ino;
52 	inode->i_blocks = 0;
53 	inode->i_mtime = inode->i_atime = inode->i_ctime =
54 			F2FS_I(inode)->i_crtime = current_time(inode);
55 	inode->i_generation = sbi->s_next_generation++;
56 
57 	err = insert_inode_locked(inode);
58 	if (err) {
59 		err = -EINVAL;
60 		goto fail;
61 	}
62 
63 	if (f2fs_sb_has_project_quota(sbi->sb) &&
64 		(F2FS_I(dir)->i_flags & FS_PROJINHERIT_FL))
65 		F2FS_I(inode)->i_projid = F2FS_I(dir)->i_projid;
66 	else
67 		F2FS_I(inode)->i_projid = make_kprojid(&init_user_ns,
68 							F2FS_DEF_PROJID);
69 
70 	err = dquot_initialize(inode);
71 	if (err)
72 		goto fail_drop;
73 
74 	err = dquot_alloc_inode(inode);
75 	if (err)
76 		goto fail_drop;
77 
78 	set_inode_flag(inode, FI_NEW_INODE);
79 
80 	/* If the directory encrypted, then we should encrypt the inode. */
81 	if ((f2fs_encrypted_inode(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) &&
82 				f2fs_may_encrypt(inode))
83 		f2fs_set_encrypted_inode(inode);
84 
85 	if (f2fs_sb_has_extra_attr(sbi->sb)) {
86 		set_inode_flag(inode, FI_EXTRA_ATTR);
87 		F2FS_I(inode)->i_extra_isize = F2FS_TOTAL_EXTRA_ATTR_SIZE;
88 	}
89 
90 	if (test_opt(sbi, INLINE_XATTR))
91 		set_inode_flag(inode, FI_INLINE_XATTR);
92 
93 	if (test_opt(sbi, INLINE_DATA) && f2fs_may_inline_data(inode))
94 		set_inode_flag(inode, FI_INLINE_DATA);
95 	if (f2fs_may_inline_dentry(inode))
96 		set_inode_flag(inode, FI_INLINE_DENTRY);
97 
98 	if (f2fs_sb_has_flexible_inline_xattr(sbi->sb)) {
99 		f2fs_bug_on(sbi, !f2fs_has_extra_attr(inode));
100 		if (f2fs_has_inline_xattr(inode))
101 			xattr_size = F2FS_OPTION(sbi).inline_xattr_size;
102 		/* Otherwise, will be 0 */
103 	} else if (f2fs_has_inline_xattr(inode) ||
104 				f2fs_has_inline_dentry(inode)) {
105 		xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
106 	}
107 	F2FS_I(inode)->i_inline_xattr_size = xattr_size;
108 
109 	f2fs_init_extent_tree(inode, NULL);
110 
111 	stat_inc_inline_xattr(inode);
112 	stat_inc_inline_inode(inode);
113 	stat_inc_inline_dir(inode);
114 
115 	F2FS_I(inode)->i_flags =
116 		f2fs_mask_flags(mode, F2FS_I(dir)->i_flags & F2FS_FL_INHERITED);
117 
118 	if (S_ISDIR(inode->i_mode))
119 		F2FS_I(inode)->i_flags |= FS_INDEX_FL;
120 
121 	if (F2FS_I(inode)->i_flags & FS_PROJINHERIT_FL)
122 		set_inode_flag(inode, FI_PROJ_INHERIT);
123 
124 	trace_f2fs_new_inode(inode, 0);
125 	return inode;
126 
127 fail:
128 	trace_f2fs_new_inode(inode, err);
129 	make_bad_inode(inode);
130 	if (nid_free)
131 		set_inode_flag(inode, FI_FREE_NID);
132 	iput(inode);
133 	return ERR_PTR(err);
134 fail_drop:
135 	trace_f2fs_new_inode(inode, err);
136 	dquot_drop(inode);
137 	inode->i_flags |= S_NOQUOTA;
138 	if (nid_free)
139 		set_inode_flag(inode, FI_FREE_NID);
140 	clear_nlink(inode);
141 	unlock_new_inode(inode);
142 	iput(inode);
143 	return ERR_PTR(err);
144 }
145 
146 static int is_extension_exist(const unsigned char *s, const char *sub)
147 {
148 	size_t slen = strlen(s);
149 	size_t sublen = strlen(sub);
150 	int i;
151 
152 	/*
153 	 * filename format of multimedia file should be defined as:
154 	 * "filename + '.' + extension + (optional: '.' + temp extension)".
155 	 */
156 	if (slen < sublen + 2)
157 		return 0;
158 
159 	for (i = 1; i < slen - sublen; i++) {
160 		if (s[i] != '.')
161 			continue;
162 		if (!strncasecmp(s + i + 1, sub, sublen))
163 			return 1;
164 	}
165 
166 	return 0;
167 }
168 
169 /*
170  * Set multimedia files as cold files for hot/cold data separation
171  */
172 static inline void set_file_temperature(struct f2fs_sb_info *sbi, struct inode *inode,
173 		const unsigned char *name)
174 {
175 	__u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
176 	int i, cold_count, hot_count;
177 
178 	down_read(&sbi->sb_lock);
179 
180 	cold_count = le32_to_cpu(sbi->raw_super->extension_count);
181 	hot_count = sbi->raw_super->hot_ext_count;
182 
183 	for (i = 0; i < cold_count + hot_count; i++) {
184 		if (!is_extension_exist(name, extlist[i]))
185 			continue;
186 		if (i < cold_count)
187 			file_set_cold(inode);
188 		else
189 			file_set_hot(inode);
190 		break;
191 	}
192 
193 	up_read(&sbi->sb_lock);
194 }
195 
196 int update_extension_list(struct f2fs_sb_info *sbi, const char *name,
197 							bool hot, bool set)
198 {
199 	__u8 (*extlist)[F2FS_EXTENSION_LEN] = sbi->raw_super->extension_list;
200 	int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
201 	int hot_count = sbi->raw_super->hot_ext_count;
202 	int total_count = cold_count + hot_count;
203 	int start, count;
204 	int i;
205 
206 	if (set) {
207 		if (total_count == F2FS_MAX_EXTENSION)
208 			return -EINVAL;
209 	} else {
210 		if (!hot && !cold_count)
211 			return -EINVAL;
212 		if (hot && !hot_count)
213 			return -EINVAL;
214 	}
215 
216 	if (hot) {
217 		start = cold_count;
218 		count = total_count;
219 	} else {
220 		start = 0;
221 		count = cold_count;
222 	}
223 
224 	for (i = start; i < count; i++) {
225 		if (strcmp(name, extlist[i]))
226 			continue;
227 
228 		if (set)
229 			return -EINVAL;
230 
231 		memcpy(extlist[i], extlist[i + 1],
232 				F2FS_EXTENSION_LEN * (total_count - i - 1));
233 		memset(extlist[total_count - 1], 0, F2FS_EXTENSION_LEN);
234 		if (hot)
235 			sbi->raw_super->hot_ext_count = hot_count - 1;
236 		else
237 			sbi->raw_super->extension_count =
238 						cpu_to_le32(cold_count - 1);
239 		return 0;
240 	}
241 
242 	if (!set)
243 		return -EINVAL;
244 
245 	if (hot) {
246 		strncpy(extlist[count], name, strlen(name));
247 		sbi->raw_super->hot_ext_count = hot_count + 1;
248 	} else {
249 		char buf[F2FS_MAX_EXTENSION][F2FS_EXTENSION_LEN];
250 
251 		memcpy(buf, &extlist[cold_count],
252 				F2FS_EXTENSION_LEN * hot_count);
253 		memset(extlist[cold_count], 0, F2FS_EXTENSION_LEN);
254 		strncpy(extlist[cold_count], name, strlen(name));
255 		memcpy(&extlist[cold_count + 1], buf,
256 				F2FS_EXTENSION_LEN * hot_count);
257 		sbi->raw_super->extension_count = cpu_to_le32(cold_count + 1);
258 	}
259 	return 0;
260 }
261 
262 static int f2fs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
263 						bool excl)
264 {
265 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
266 	struct inode *inode;
267 	nid_t ino = 0;
268 	int err;
269 
270 	if (unlikely(f2fs_cp_error(sbi)))
271 		return -EIO;
272 
273 	err = dquot_initialize(dir);
274 	if (err)
275 		return err;
276 
277 	inode = f2fs_new_inode(dir, mode);
278 	if (IS_ERR(inode))
279 		return PTR_ERR(inode);
280 
281 	if (!test_opt(sbi, DISABLE_EXT_IDENTIFY))
282 		set_file_temperature(sbi, inode, dentry->d_name.name);
283 
284 	inode->i_op = &f2fs_file_inode_operations;
285 	inode->i_fop = &f2fs_file_operations;
286 	inode->i_mapping->a_ops = &f2fs_dblock_aops;
287 	ino = inode->i_ino;
288 
289 	f2fs_lock_op(sbi);
290 	err = f2fs_add_link(dentry, inode);
291 	if (err)
292 		goto out;
293 	f2fs_unlock_op(sbi);
294 
295 	alloc_nid_done(sbi, ino);
296 
297 	d_instantiate(dentry, inode);
298 	unlock_new_inode(inode);
299 
300 	if (IS_DIRSYNC(dir))
301 		f2fs_sync_fs(sbi->sb, 1);
302 
303 	f2fs_balance_fs(sbi, true);
304 	return 0;
305 out:
306 	handle_failed_inode(inode);
307 	return err;
308 }
309 
310 static int f2fs_link(struct dentry *old_dentry, struct inode *dir,
311 		struct dentry *dentry)
312 {
313 	struct inode *inode = d_inode(old_dentry);
314 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
315 	int err;
316 
317 	if (unlikely(f2fs_cp_error(sbi)))
318 		return -EIO;
319 
320 	err = fscrypt_prepare_link(old_dentry, dir, dentry);
321 	if (err)
322 		return err;
323 
324 	if (is_inode_flag_set(dir, FI_PROJ_INHERIT) &&
325 			(!projid_eq(F2FS_I(dir)->i_projid,
326 			F2FS_I(old_dentry->d_inode)->i_projid)))
327 		return -EXDEV;
328 
329 	err = dquot_initialize(dir);
330 	if (err)
331 		return err;
332 
333 	f2fs_balance_fs(sbi, true);
334 
335 	inode->i_ctime = current_time(inode);
336 	ihold(inode);
337 
338 	set_inode_flag(inode, FI_INC_LINK);
339 	f2fs_lock_op(sbi);
340 	err = f2fs_add_link(dentry, inode);
341 	if (err)
342 		goto out;
343 	f2fs_unlock_op(sbi);
344 
345 	d_instantiate(dentry, inode);
346 
347 	if (IS_DIRSYNC(dir))
348 		f2fs_sync_fs(sbi->sb, 1);
349 	return 0;
350 out:
351 	clear_inode_flag(inode, FI_INC_LINK);
352 	iput(inode);
353 	f2fs_unlock_op(sbi);
354 	return err;
355 }
356 
357 struct dentry *f2fs_get_parent(struct dentry *child)
358 {
359 	struct qstr dotdot = QSTR_INIT("..", 2);
360 	struct page *page;
361 	unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot, &page);
362 	if (!ino) {
363 		if (IS_ERR(page))
364 			return ERR_CAST(page);
365 		return ERR_PTR(-ENOENT);
366 	}
367 	return d_obtain_alias(f2fs_iget(child->d_sb, ino));
368 }
369 
370 static int __recover_dot_dentries(struct inode *dir, nid_t pino)
371 {
372 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
373 	struct qstr dot = QSTR_INIT(".", 1);
374 	struct qstr dotdot = QSTR_INIT("..", 2);
375 	struct f2fs_dir_entry *de;
376 	struct page *page;
377 	int err = 0;
378 
379 	if (f2fs_readonly(sbi->sb)) {
380 		f2fs_msg(sbi->sb, KERN_INFO,
381 			"skip recovering inline_dots inode (ino:%lu, pino:%u) "
382 			"in readonly mountpoint", dir->i_ino, pino);
383 		return 0;
384 	}
385 
386 	err = dquot_initialize(dir);
387 	if (err)
388 		return err;
389 
390 	f2fs_balance_fs(sbi, true);
391 
392 	f2fs_lock_op(sbi);
393 
394 	de = f2fs_find_entry(dir, &dot, &page);
395 	if (de) {
396 		f2fs_put_page(page, 0);
397 	} else if (IS_ERR(page)) {
398 		err = PTR_ERR(page);
399 		goto out;
400 	} else {
401 		err = __f2fs_add_link(dir, &dot, NULL, dir->i_ino, S_IFDIR);
402 		if (err)
403 			goto out;
404 	}
405 
406 	de = f2fs_find_entry(dir, &dotdot, &page);
407 	if (de)
408 		f2fs_put_page(page, 0);
409 	else if (IS_ERR(page))
410 		err = PTR_ERR(page);
411 	else
412 		err = __f2fs_add_link(dir, &dotdot, NULL, pino, S_IFDIR);
413 out:
414 	if (!err)
415 		clear_inode_flag(dir, FI_INLINE_DOTS);
416 
417 	f2fs_unlock_op(sbi);
418 	return err;
419 }
420 
421 static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,
422 		unsigned int flags)
423 {
424 	struct inode *inode = NULL;
425 	struct f2fs_dir_entry *de;
426 	struct page *page;
427 	struct dentry *new;
428 	nid_t ino = -1;
429 	int err = 0;
430 	unsigned int root_ino = F2FS_ROOT_INO(F2FS_I_SB(dir));
431 
432 	trace_f2fs_lookup_start(dir, dentry, flags);
433 
434 	err = fscrypt_prepare_lookup(dir, dentry, flags);
435 	if (err)
436 		goto out;
437 
438 	if (dentry->d_name.len > F2FS_NAME_LEN) {
439 		err = -ENAMETOOLONG;
440 		goto out;
441 	}
442 
443 	de = f2fs_find_entry(dir, &dentry->d_name, &page);
444 	if (!de) {
445 		if (IS_ERR(page)) {
446 			err = PTR_ERR(page);
447 			goto out;
448 		}
449 		goto out_splice;
450 	}
451 
452 	ino = le32_to_cpu(de->ino);
453 	f2fs_put_page(page, 0);
454 
455 	inode = f2fs_iget(dir->i_sb, ino);
456 	if (IS_ERR(inode)) {
457 		err = PTR_ERR(inode);
458 		goto out;
459 	}
460 
461 	if ((dir->i_ino == root_ino) && f2fs_has_inline_dots(dir)) {
462 		err = __recover_dot_dentries(dir, root_ino);
463 		if (err)
464 			goto out_iput;
465 	}
466 
467 	if (f2fs_has_inline_dots(inode)) {
468 		err = __recover_dot_dentries(inode, dir->i_ino);
469 		if (err)
470 			goto out_iput;
471 	}
472 	if (f2fs_encrypted_inode(dir) &&
473 	    (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
474 	    !fscrypt_has_permitted_context(dir, inode)) {
475 		f2fs_msg(inode->i_sb, KERN_WARNING,
476 			 "Inconsistent encryption contexts: %lu/%lu",
477 			 dir->i_ino, inode->i_ino);
478 		err = -EPERM;
479 		goto out_iput;
480 	}
481 out_splice:
482 	new = d_splice_alias(inode, dentry);
483 	if (IS_ERR(new))
484 		err = PTR_ERR(new);
485 	trace_f2fs_lookup_end(dir, dentry, ino, err);
486 	return new;
487 out_iput:
488 	iput(inode);
489 out:
490 	trace_f2fs_lookup_end(dir, dentry, ino, err);
491 	return ERR_PTR(err);
492 }
493 
494 static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
495 {
496 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
497 	struct inode *inode = d_inode(dentry);
498 	struct f2fs_dir_entry *de;
499 	struct page *page;
500 	int err = -ENOENT;
501 
502 	trace_f2fs_unlink_enter(dir, dentry);
503 
504 	if (unlikely(f2fs_cp_error(sbi)))
505 		return -EIO;
506 
507 	err = dquot_initialize(dir);
508 	if (err)
509 		return err;
510 	err = dquot_initialize(inode);
511 	if (err)
512 		return err;
513 
514 	de = f2fs_find_entry(dir, &dentry->d_name, &page);
515 	if (!de) {
516 		if (IS_ERR(page))
517 			err = PTR_ERR(page);
518 		goto fail;
519 	}
520 
521 	f2fs_balance_fs(sbi, true);
522 
523 	f2fs_lock_op(sbi);
524 	err = acquire_orphan_inode(sbi);
525 	if (err) {
526 		f2fs_unlock_op(sbi);
527 		f2fs_put_page(page, 0);
528 		goto fail;
529 	}
530 	f2fs_delete_entry(de, page, dir, inode);
531 	f2fs_unlock_op(sbi);
532 
533 	if (IS_DIRSYNC(dir))
534 		f2fs_sync_fs(sbi->sb, 1);
535 fail:
536 	trace_f2fs_unlink_exit(inode, err);
537 	return err;
538 }
539 
540 static const char *f2fs_get_link(struct dentry *dentry,
541 				 struct inode *inode,
542 				 struct delayed_call *done)
543 {
544 	const char *link = page_get_link(dentry, inode, done);
545 	if (!IS_ERR(link) && !*link) {
546 		/* this is broken symlink case */
547 		do_delayed_call(done);
548 		clear_delayed_call(done);
549 		link = ERR_PTR(-ENOENT);
550 	}
551 	return link;
552 }
553 
554 static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
555 					const char *symname)
556 {
557 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
558 	struct inode *inode;
559 	size_t len = strlen(symname);
560 	struct fscrypt_str disk_link;
561 	int err;
562 
563 	if (unlikely(f2fs_cp_error(sbi)))
564 		return -EIO;
565 
566 	err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,
567 				      &disk_link);
568 	if (err)
569 		return err;
570 
571 	err = dquot_initialize(dir);
572 	if (err)
573 		return err;
574 
575 	inode = f2fs_new_inode(dir, S_IFLNK | S_IRWXUGO);
576 	if (IS_ERR(inode))
577 		return PTR_ERR(inode);
578 
579 	if (IS_ENCRYPTED(inode))
580 		inode->i_op = &f2fs_encrypted_symlink_inode_operations;
581 	else
582 		inode->i_op = &f2fs_symlink_inode_operations;
583 	inode_nohighmem(inode);
584 	inode->i_mapping->a_ops = &f2fs_dblock_aops;
585 
586 	f2fs_lock_op(sbi);
587 	err = f2fs_add_link(dentry, inode);
588 	if (err)
589 		goto out_handle_failed_inode;
590 	f2fs_unlock_op(sbi);
591 	alloc_nid_done(sbi, inode->i_ino);
592 
593 	err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);
594 	if (err)
595 		goto err_out;
596 
597 	err = page_symlink(inode, disk_link.name, disk_link.len);
598 
599 err_out:
600 	d_instantiate(dentry, inode);
601 	unlock_new_inode(inode);
602 
603 	/*
604 	 * Let's flush symlink data in order to avoid broken symlink as much as
605 	 * possible. Nevertheless, fsyncing is the best way, but there is no
606 	 * way to get a file descriptor in order to flush that.
607 	 *
608 	 * Note that, it needs to do dir->fsync to make this recoverable.
609 	 * If the symlink path is stored into inline_data, there is no
610 	 * performance regression.
611 	 */
612 	if (!err) {
613 		filemap_write_and_wait_range(inode->i_mapping, 0,
614 							disk_link.len - 1);
615 
616 		if (IS_DIRSYNC(dir))
617 			f2fs_sync_fs(sbi->sb, 1);
618 	} else {
619 		f2fs_unlink(dir, dentry);
620 	}
621 
622 	f2fs_balance_fs(sbi, true);
623 	goto out_free_encrypted_link;
624 
625 out_handle_failed_inode:
626 	handle_failed_inode(inode);
627 out_free_encrypted_link:
628 	if (disk_link.name != (unsigned char *)symname)
629 		kfree(disk_link.name);
630 	return err;
631 }
632 
633 static int f2fs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
634 {
635 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
636 	struct inode *inode;
637 	int err;
638 
639 	if (unlikely(f2fs_cp_error(sbi)))
640 		return -EIO;
641 
642 	err = dquot_initialize(dir);
643 	if (err)
644 		return err;
645 
646 	inode = f2fs_new_inode(dir, S_IFDIR | mode);
647 	if (IS_ERR(inode))
648 		return PTR_ERR(inode);
649 
650 	inode->i_op = &f2fs_dir_inode_operations;
651 	inode->i_fop = &f2fs_dir_operations;
652 	inode->i_mapping->a_ops = &f2fs_dblock_aops;
653 	inode_nohighmem(inode);
654 
655 	set_inode_flag(inode, FI_INC_LINK);
656 	f2fs_lock_op(sbi);
657 	err = f2fs_add_link(dentry, inode);
658 	if (err)
659 		goto out_fail;
660 	f2fs_unlock_op(sbi);
661 
662 	alloc_nid_done(sbi, inode->i_ino);
663 
664 	d_instantiate(dentry, inode);
665 	unlock_new_inode(inode);
666 
667 	if (IS_DIRSYNC(dir))
668 		f2fs_sync_fs(sbi->sb, 1);
669 
670 	f2fs_balance_fs(sbi, true);
671 	return 0;
672 
673 out_fail:
674 	clear_inode_flag(inode, FI_INC_LINK);
675 	handle_failed_inode(inode);
676 	return err;
677 }
678 
679 static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)
680 {
681 	struct inode *inode = d_inode(dentry);
682 	if (f2fs_empty_dir(inode))
683 		return f2fs_unlink(dir, dentry);
684 	return -ENOTEMPTY;
685 }
686 
687 static int f2fs_mknod(struct inode *dir, struct dentry *dentry,
688 				umode_t mode, dev_t rdev)
689 {
690 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
691 	struct inode *inode;
692 	int err = 0;
693 
694 	if (unlikely(f2fs_cp_error(sbi)))
695 		return -EIO;
696 
697 	err = dquot_initialize(dir);
698 	if (err)
699 		return err;
700 
701 	inode = f2fs_new_inode(dir, mode);
702 	if (IS_ERR(inode))
703 		return PTR_ERR(inode);
704 
705 	init_special_inode(inode, inode->i_mode, rdev);
706 	inode->i_op = &f2fs_special_inode_operations;
707 
708 	f2fs_lock_op(sbi);
709 	err = f2fs_add_link(dentry, inode);
710 	if (err)
711 		goto out;
712 	f2fs_unlock_op(sbi);
713 
714 	alloc_nid_done(sbi, inode->i_ino);
715 
716 	d_instantiate(dentry, inode);
717 	unlock_new_inode(inode);
718 
719 	if (IS_DIRSYNC(dir))
720 		f2fs_sync_fs(sbi->sb, 1);
721 
722 	f2fs_balance_fs(sbi, true);
723 	return 0;
724 out:
725 	handle_failed_inode(inode);
726 	return err;
727 }
728 
729 static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
730 					umode_t mode, struct inode **whiteout)
731 {
732 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
733 	struct inode *inode;
734 	int err;
735 
736 	err = dquot_initialize(dir);
737 	if (err)
738 		return err;
739 
740 	inode = f2fs_new_inode(dir, mode);
741 	if (IS_ERR(inode))
742 		return PTR_ERR(inode);
743 
744 	if (whiteout) {
745 		init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);
746 		inode->i_op = &f2fs_special_inode_operations;
747 	} else {
748 		inode->i_op = &f2fs_file_inode_operations;
749 		inode->i_fop = &f2fs_file_operations;
750 		inode->i_mapping->a_ops = &f2fs_dblock_aops;
751 	}
752 
753 	f2fs_lock_op(sbi);
754 	err = acquire_orphan_inode(sbi);
755 	if (err)
756 		goto out;
757 
758 	err = f2fs_do_tmpfile(inode, dir);
759 	if (err)
760 		goto release_out;
761 
762 	/*
763 	 * add this non-linked tmpfile to orphan list, in this way we could
764 	 * remove all unused data of tmpfile after abnormal power-off.
765 	 */
766 	add_orphan_inode(inode);
767 	alloc_nid_done(sbi, inode->i_ino);
768 
769 	if (whiteout) {
770 		f2fs_i_links_write(inode, false);
771 		*whiteout = inode;
772 	} else {
773 		d_tmpfile(dentry, inode);
774 	}
775 	/* link_count was changed by d_tmpfile as well. */
776 	f2fs_unlock_op(sbi);
777 	unlock_new_inode(inode);
778 
779 	f2fs_balance_fs(sbi, true);
780 	return 0;
781 
782 release_out:
783 	release_orphan_inode(sbi);
784 out:
785 	handle_failed_inode(inode);
786 	return err;
787 }
788 
789 static int f2fs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
790 {
791 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
792 
793 	if (unlikely(f2fs_cp_error(sbi)))
794 		return -EIO;
795 
796 	if (f2fs_encrypted_inode(dir) || DUMMY_ENCRYPTION_ENABLED(sbi)) {
797 		int err = fscrypt_get_encryption_info(dir);
798 		if (err)
799 			return err;
800 	}
801 
802 	return __f2fs_tmpfile(dir, dentry, mode, NULL);
803 }
804 
805 static int f2fs_create_whiteout(struct inode *dir, struct inode **whiteout)
806 {
807 	if (unlikely(f2fs_cp_error(F2FS_I_SB(dir))))
808 		return -EIO;
809 
810 	return __f2fs_tmpfile(dir, NULL, S_IFCHR | WHITEOUT_MODE, whiteout);
811 }
812 
813 static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
814 			struct inode *new_dir, struct dentry *new_dentry,
815 			unsigned int flags)
816 {
817 	struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
818 	struct inode *old_inode = d_inode(old_dentry);
819 	struct inode *new_inode = d_inode(new_dentry);
820 	struct inode *whiteout = NULL;
821 	struct page *old_dir_page;
822 	struct page *old_page, *new_page = NULL;
823 	struct f2fs_dir_entry *old_dir_entry = NULL;
824 	struct f2fs_dir_entry *old_entry;
825 	struct f2fs_dir_entry *new_entry;
826 	bool is_old_inline = f2fs_has_inline_dentry(old_dir);
827 	int err = -ENOENT;
828 
829 	if (unlikely(f2fs_cp_error(sbi)))
830 		return -EIO;
831 
832 	if (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
833 			(!projid_eq(F2FS_I(new_dir)->i_projid,
834 			F2FS_I(old_dentry->d_inode)->i_projid)))
835 		return -EXDEV;
836 
837 	err = dquot_initialize(old_dir);
838 	if (err)
839 		goto out;
840 
841 	err = dquot_initialize(new_dir);
842 	if (err)
843 		goto out;
844 
845 	if (new_inode) {
846 		err = dquot_initialize(new_inode);
847 		if (err)
848 			goto out;
849 	}
850 
851 	old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
852 	if (!old_entry) {
853 		if (IS_ERR(old_page))
854 			err = PTR_ERR(old_page);
855 		goto out;
856 	}
857 
858 	if (S_ISDIR(old_inode->i_mode)) {
859 		old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);
860 		if (!old_dir_entry) {
861 			if (IS_ERR(old_dir_page))
862 				err = PTR_ERR(old_dir_page);
863 			goto out_old;
864 		}
865 	}
866 
867 	if (flags & RENAME_WHITEOUT) {
868 		err = f2fs_create_whiteout(old_dir, &whiteout);
869 		if (err)
870 			goto out_dir;
871 	}
872 
873 	if (new_inode) {
874 
875 		err = -ENOTEMPTY;
876 		if (old_dir_entry && !f2fs_empty_dir(new_inode))
877 			goto out_whiteout;
878 
879 		err = -ENOENT;
880 		new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
881 						&new_page);
882 		if (!new_entry) {
883 			if (IS_ERR(new_page))
884 				err = PTR_ERR(new_page);
885 			goto out_whiteout;
886 		}
887 
888 		f2fs_balance_fs(sbi, true);
889 
890 		f2fs_lock_op(sbi);
891 
892 		err = acquire_orphan_inode(sbi);
893 		if (err)
894 			goto put_out_dir;
895 
896 		f2fs_set_link(new_dir, new_entry, new_page, old_inode);
897 
898 		new_inode->i_ctime = current_time(new_inode);
899 		down_write(&F2FS_I(new_inode)->i_sem);
900 		if (old_dir_entry)
901 			f2fs_i_links_write(new_inode, false);
902 		f2fs_i_links_write(new_inode, false);
903 		up_write(&F2FS_I(new_inode)->i_sem);
904 
905 		if (!new_inode->i_nlink)
906 			add_orphan_inode(new_inode);
907 		else
908 			release_orphan_inode(sbi);
909 	} else {
910 		f2fs_balance_fs(sbi, true);
911 
912 		f2fs_lock_op(sbi);
913 
914 		err = f2fs_add_link(new_dentry, old_inode);
915 		if (err) {
916 			f2fs_unlock_op(sbi);
917 			goto out_whiteout;
918 		}
919 
920 		if (old_dir_entry)
921 			f2fs_i_links_write(new_dir, true);
922 
923 		/*
924 		 * old entry and new entry can locate in the same inline
925 		 * dentry in inode, when attaching new entry in inline dentry,
926 		 * it could force inline dentry conversion, after that,
927 		 * old_entry and old_page will point to wrong address, in
928 		 * order to avoid this, let's do the check and update here.
929 		 */
930 		if (is_old_inline && !f2fs_has_inline_dentry(old_dir)) {
931 			f2fs_put_page(old_page, 0);
932 			old_page = NULL;
933 
934 			old_entry = f2fs_find_entry(old_dir,
935 						&old_dentry->d_name, &old_page);
936 			if (!old_entry) {
937 				err = -ENOENT;
938 				if (IS_ERR(old_page))
939 					err = PTR_ERR(old_page);
940 				f2fs_unlock_op(sbi);
941 				goto out_whiteout;
942 			}
943 		}
944 	}
945 
946 	down_write(&F2FS_I(old_inode)->i_sem);
947 	if (!old_dir_entry || whiteout)
948 		file_lost_pino(old_inode);
949 	else
950 		F2FS_I(old_inode)->i_pino = new_dir->i_ino;
951 	up_write(&F2FS_I(old_inode)->i_sem);
952 
953 	old_inode->i_ctime = current_time(old_inode);
954 	f2fs_mark_inode_dirty_sync(old_inode, false);
955 
956 	f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
957 
958 	if (whiteout) {
959 		whiteout->i_state |= I_LINKABLE;
960 		set_inode_flag(whiteout, FI_INC_LINK);
961 		err = f2fs_add_link(old_dentry, whiteout);
962 		if (err)
963 			goto put_out_dir;
964 		whiteout->i_state &= ~I_LINKABLE;
965 		iput(whiteout);
966 	}
967 
968 	if (old_dir_entry) {
969 		if (old_dir != new_dir && !whiteout)
970 			f2fs_set_link(old_inode, old_dir_entry,
971 						old_dir_page, new_dir);
972 		else
973 			f2fs_put_page(old_dir_page, 0);
974 		f2fs_i_links_write(old_dir, false);
975 	}
976 	if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT)
977 		add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
978 
979 	f2fs_unlock_op(sbi);
980 
981 	if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
982 		f2fs_sync_fs(sbi->sb, 1);
983 	return 0;
984 
985 put_out_dir:
986 	f2fs_unlock_op(sbi);
987 	if (new_page)
988 		f2fs_put_page(new_page, 0);
989 out_whiteout:
990 	if (whiteout)
991 		iput(whiteout);
992 out_dir:
993 	if (old_dir_entry)
994 		f2fs_put_page(old_dir_page, 0);
995 out_old:
996 	f2fs_put_page(old_page, 0);
997 out:
998 	return err;
999 }
1000 
1001 static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
1002 			     struct inode *new_dir, struct dentry *new_dentry)
1003 {
1004 	struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);
1005 	struct inode *old_inode = d_inode(old_dentry);
1006 	struct inode *new_inode = d_inode(new_dentry);
1007 	struct page *old_dir_page, *new_dir_page;
1008 	struct page *old_page, *new_page;
1009 	struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;
1010 	struct f2fs_dir_entry *old_entry, *new_entry;
1011 	int old_nlink = 0, new_nlink = 0;
1012 	int err = -ENOENT;
1013 
1014 	if (unlikely(f2fs_cp_error(sbi)))
1015 		return -EIO;
1016 
1017 	if ((is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1018 			!projid_eq(F2FS_I(new_dir)->i_projid,
1019 			F2FS_I(old_dentry->d_inode)->i_projid)) ||
1020 	    (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&
1021 			!projid_eq(F2FS_I(old_dir)->i_projid,
1022 			F2FS_I(new_dentry->d_inode)->i_projid)))
1023 		return -EXDEV;
1024 
1025 	err = dquot_initialize(old_dir);
1026 	if (err)
1027 		goto out;
1028 
1029 	err = dquot_initialize(new_dir);
1030 	if (err)
1031 		goto out;
1032 
1033 	old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);
1034 	if (!old_entry) {
1035 		if (IS_ERR(old_page))
1036 			err = PTR_ERR(old_page);
1037 		goto out;
1038 	}
1039 
1040 	new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);
1041 	if (!new_entry) {
1042 		if (IS_ERR(new_page))
1043 			err = PTR_ERR(new_page);
1044 		goto out_old;
1045 	}
1046 
1047 	/* prepare for updating ".." directory entry info later */
1048 	if (old_dir != new_dir) {
1049 		if (S_ISDIR(old_inode->i_mode)) {
1050 			old_dir_entry = f2fs_parent_dir(old_inode,
1051 							&old_dir_page);
1052 			if (!old_dir_entry) {
1053 				if (IS_ERR(old_dir_page))
1054 					err = PTR_ERR(old_dir_page);
1055 				goto out_new;
1056 			}
1057 		}
1058 
1059 		if (S_ISDIR(new_inode->i_mode)) {
1060 			new_dir_entry = f2fs_parent_dir(new_inode,
1061 							&new_dir_page);
1062 			if (!new_dir_entry) {
1063 				if (IS_ERR(new_dir_page))
1064 					err = PTR_ERR(new_dir_page);
1065 				goto out_old_dir;
1066 			}
1067 		}
1068 	}
1069 
1070 	/*
1071 	 * If cross rename between file and directory those are not
1072 	 * in the same directory, we will inc nlink of file's parent
1073 	 * later, so we should check upper boundary of its nlink.
1074 	 */
1075 	if ((!old_dir_entry || !new_dir_entry) &&
1076 				old_dir_entry != new_dir_entry) {
1077 		old_nlink = old_dir_entry ? -1 : 1;
1078 		new_nlink = -old_nlink;
1079 		err = -EMLINK;
1080 		if ((old_nlink > 0 && old_dir->i_nlink >= F2FS_LINK_MAX) ||
1081 			(new_nlink > 0 && new_dir->i_nlink >= F2FS_LINK_MAX))
1082 			goto out_new_dir;
1083 	}
1084 
1085 	f2fs_balance_fs(sbi, true);
1086 
1087 	f2fs_lock_op(sbi);
1088 
1089 	/* update ".." directory entry info of old dentry */
1090 	if (old_dir_entry)
1091 		f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);
1092 
1093 	/* update ".." directory entry info of new dentry */
1094 	if (new_dir_entry)
1095 		f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);
1096 
1097 	/* update directory entry info of old dir inode */
1098 	f2fs_set_link(old_dir, old_entry, old_page, new_inode);
1099 
1100 	down_write(&F2FS_I(old_inode)->i_sem);
1101 	file_lost_pino(old_inode);
1102 	up_write(&F2FS_I(old_inode)->i_sem);
1103 
1104 	old_dir->i_ctime = current_time(old_dir);
1105 	if (old_nlink) {
1106 		down_write(&F2FS_I(old_dir)->i_sem);
1107 		f2fs_i_links_write(old_dir, old_nlink > 0);
1108 		up_write(&F2FS_I(old_dir)->i_sem);
1109 	}
1110 	f2fs_mark_inode_dirty_sync(old_dir, false);
1111 
1112 	/* update directory entry info of new dir inode */
1113 	f2fs_set_link(new_dir, new_entry, new_page, old_inode);
1114 
1115 	down_write(&F2FS_I(new_inode)->i_sem);
1116 	file_lost_pino(new_inode);
1117 	up_write(&F2FS_I(new_inode)->i_sem);
1118 
1119 	new_dir->i_ctime = current_time(new_dir);
1120 	if (new_nlink) {
1121 		down_write(&F2FS_I(new_dir)->i_sem);
1122 		f2fs_i_links_write(new_dir, new_nlink > 0);
1123 		up_write(&F2FS_I(new_dir)->i_sem);
1124 	}
1125 	f2fs_mark_inode_dirty_sync(new_dir, false);
1126 
1127 	if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {
1128 		add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);
1129 		add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);
1130 	}
1131 
1132 	f2fs_unlock_op(sbi);
1133 
1134 	if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
1135 		f2fs_sync_fs(sbi->sb, 1);
1136 	return 0;
1137 out_new_dir:
1138 	if (new_dir_entry) {
1139 		f2fs_put_page(new_dir_page, 0);
1140 	}
1141 out_old_dir:
1142 	if (old_dir_entry) {
1143 		f2fs_put_page(old_dir_page, 0);
1144 	}
1145 out_new:
1146 	f2fs_put_page(new_page, 0);
1147 out_old:
1148 	f2fs_put_page(old_page, 0);
1149 out:
1150 	return err;
1151 }
1152 
1153 static int f2fs_rename2(struct inode *old_dir, struct dentry *old_dentry,
1154 			struct inode *new_dir, struct dentry *new_dentry,
1155 			unsigned int flags)
1156 {
1157 	int err;
1158 
1159 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
1160 		return -EINVAL;
1161 
1162 	err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,
1163 				     flags);
1164 	if (err)
1165 		return err;
1166 
1167 	if (flags & RENAME_EXCHANGE) {
1168 		return f2fs_cross_rename(old_dir, old_dentry,
1169 					 new_dir, new_dentry);
1170 	}
1171 	/*
1172 	 * VFS has already handled the new dentry existence case,
1173 	 * here, we just deal with "RENAME_NOREPLACE" as regular rename.
1174 	 */
1175 	return f2fs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
1176 }
1177 
1178 static const char *f2fs_encrypted_get_link(struct dentry *dentry,
1179 					   struct inode *inode,
1180 					   struct delayed_call *done)
1181 {
1182 	struct page *page;
1183 	const char *target;
1184 
1185 	if (!dentry)
1186 		return ERR_PTR(-ECHILD);
1187 
1188 	page = read_mapping_page(inode->i_mapping, 0, NULL);
1189 	if (IS_ERR(page))
1190 		return ERR_CAST(page);
1191 
1192 	target = fscrypt_get_symlink(inode, page_address(page),
1193 				     inode->i_sb->s_blocksize, done);
1194 	put_page(page);
1195 	return target;
1196 }
1197 
1198 const struct inode_operations f2fs_encrypted_symlink_inode_operations = {
1199 	.get_link       = f2fs_encrypted_get_link,
1200 	.getattr	= f2fs_getattr,
1201 	.setattr	= f2fs_setattr,
1202 #ifdef CONFIG_F2FS_FS_XATTR
1203 	.listxattr	= f2fs_listxattr,
1204 #endif
1205 };
1206 
1207 const struct inode_operations f2fs_dir_inode_operations = {
1208 	.create		= f2fs_create,
1209 	.lookup		= f2fs_lookup,
1210 	.link		= f2fs_link,
1211 	.unlink		= f2fs_unlink,
1212 	.symlink	= f2fs_symlink,
1213 	.mkdir		= f2fs_mkdir,
1214 	.rmdir		= f2fs_rmdir,
1215 	.mknod		= f2fs_mknod,
1216 	.rename		= f2fs_rename2,
1217 	.tmpfile	= f2fs_tmpfile,
1218 	.getattr	= f2fs_getattr,
1219 	.setattr	= f2fs_setattr,
1220 	.get_acl	= f2fs_get_acl,
1221 	.set_acl	= f2fs_set_acl,
1222 #ifdef CONFIG_F2FS_FS_XATTR
1223 	.listxattr	= f2fs_listxattr,
1224 #endif
1225 };
1226 
1227 const struct inode_operations f2fs_symlink_inode_operations = {
1228 	.get_link       = f2fs_get_link,
1229 	.getattr	= f2fs_getattr,
1230 	.setattr	= f2fs_setattr,
1231 #ifdef CONFIG_F2FS_FS_XATTR
1232 	.listxattr	= f2fs_listxattr,
1233 #endif
1234 };
1235 
1236 const struct inode_operations f2fs_special_inode_operations = {
1237 	.getattr	= f2fs_getattr,
1238 	.setattr        = f2fs_setattr,
1239 	.get_acl	= f2fs_get_acl,
1240 	.set_acl	= f2fs_set_acl,
1241 #ifdef CONFIG_F2FS_FS_XATTR
1242 	.listxattr	= f2fs_listxattr,
1243 #endif
1244 };
1245