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