File Index
Files and directories
Sign in
FILE

namei.c

File preview with syntax highlighting.

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