1351 lines · c
1// SPDX-License-Identifier: GPL-2.02/*3 * fs/f2fs/namei.c4 *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 else114 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 separation195 */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 else216 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 else265 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 if (nid_free)345 set_inode_flag(inode, FI_FREE_NID);346 clear_nlink(inode);347 unlock_new_inode(inode);348 iput(inode);349 return ERR_PTR(err);350}351 352static int f2fs_create(struct mnt_idmap *idmap, struct inode *dir,353 struct dentry *dentry, umode_t mode, bool excl)354{355 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);356 struct inode *inode;357 nid_t ino = 0;358 int err;359 360 if (unlikely(f2fs_cp_error(sbi)))361 return -EIO;362 if (!f2fs_is_checkpoint_ready(sbi))363 return -ENOSPC;364 365 err = f2fs_dquot_initialize(dir);366 if (err)367 return err;368 369 inode = f2fs_new_inode(idmap, dir, mode, dentry->d_name.name);370 if (IS_ERR(inode))371 return PTR_ERR(inode);372 373 inode->i_op = &f2fs_file_inode_operations;374 inode->i_fop = &f2fs_file_operations;375 inode->i_mapping->a_ops = &f2fs_dblock_aops;376 ino = inode->i_ino;377 378 f2fs_lock_op(sbi);379 err = f2fs_add_link(dentry, inode);380 if (err)381 goto out;382 f2fs_unlock_op(sbi);383 384 f2fs_alloc_nid_done(sbi, ino);385 386 d_instantiate_new(dentry, inode);387 388 if (IS_DIRSYNC(dir))389 f2fs_sync_fs(sbi->sb, 1);390 391 f2fs_balance_fs(sbi, true);392 return 0;393out:394 f2fs_handle_failed_inode(inode);395 return err;396}397 398static int f2fs_link(struct dentry *old_dentry, struct inode *dir,399 struct dentry *dentry)400{401 struct inode *inode = d_inode(old_dentry);402 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);403 int err;404 405 if (unlikely(f2fs_cp_error(sbi)))406 return -EIO;407 if (!f2fs_is_checkpoint_ready(sbi))408 return -ENOSPC;409 410 err = fscrypt_prepare_link(old_dentry, dir, dentry);411 if (err)412 return err;413 414 if (is_inode_flag_set(dir, FI_PROJ_INHERIT) &&415 (!projid_eq(F2FS_I(dir)->i_projid,416 F2FS_I(old_dentry->d_inode)->i_projid)))417 return -EXDEV;418 419 err = f2fs_dquot_initialize(dir);420 if (err)421 return err;422 423 f2fs_balance_fs(sbi, true);424 425 inode_set_ctime_current(inode);426 ihold(inode);427 428 set_inode_flag(inode, FI_INC_LINK);429 f2fs_lock_op(sbi);430 err = f2fs_add_link(dentry, inode);431 if (err)432 goto out;433 f2fs_unlock_op(sbi);434 435 d_instantiate(dentry, inode);436 437 if (IS_DIRSYNC(dir))438 f2fs_sync_fs(sbi->sb, 1);439 return 0;440out:441 clear_inode_flag(inode, FI_INC_LINK);442 iput(inode);443 f2fs_unlock_op(sbi);444 return err;445}446 447struct dentry *f2fs_get_parent(struct dentry *child)448{449 struct page *page;450 unsigned long ino = f2fs_inode_by_name(d_inode(child), &dotdot_name, &page);451 452 if (!ino) {453 if (IS_ERR(page))454 return ERR_CAST(page);455 return ERR_PTR(-ENOENT);456 }457 return d_obtain_alias(f2fs_iget(child->d_sb, ino));458}459 460static struct dentry *f2fs_lookup(struct inode *dir, struct dentry *dentry,461 unsigned int flags)462{463 struct inode *inode = NULL;464 struct f2fs_dir_entry *de;465 struct page *page;466 struct dentry *new;467 nid_t ino = -1;468 int err = 0;469 struct f2fs_filename fname;470 471 trace_f2fs_lookup_start(dir, dentry, flags);472 473 if (dentry->d_name.len > F2FS_NAME_LEN) {474 err = -ENAMETOOLONG;475 goto out;476 }477 478 err = f2fs_prepare_lookup(dir, dentry, &fname);479 if (err == -ENOENT)480 goto out_splice;481 if (err)482 goto out;483 de = __f2fs_find_entry(dir, &fname, &page);484 f2fs_free_filename(&fname);485 486 if (!de) {487 if (IS_ERR(page)) {488 err = PTR_ERR(page);489 goto out;490 }491 err = -ENOENT;492 goto out_splice;493 }494 495 ino = le32_to_cpu(de->ino);496 f2fs_put_page(page, 0);497 498 inode = f2fs_iget(dir->i_sb, ino);499 if (IS_ERR(inode)) {500 err = PTR_ERR(inode);501 goto out;502 }503 504 if (IS_ENCRYPTED(dir) &&505 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&506 !fscrypt_has_permitted_context(dir, inode)) {507 f2fs_warn(F2FS_I_SB(inode), "Inconsistent encryption contexts: %lu/%lu",508 dir->i_ino, inode->i_ino);509 err = -EPERM;510 goto out_iput;511 }512out_splice:513 if (IS_ENABLED(CONFIG_UNICODE) && !inode && IS_CASEFOLDED(dir)) {514 /* Eventually we want to call d_add_ci(dentry, NULL)515 * for negative dentries in the encoding case as516 * well. For now, prevent the negative dentry517 * from being cached.518 */519 trace_f2fs_lookup_end(dir, dentry, ino, err);520 return NULL;521 }522 523 new = d_splice_alias(inode, dentry);524 trace_f2fs_lookup_end(dir, !IS_ERR_OR_NULL(new) ? new : dentry,525 ino, IS_ERR(new) ? PTR_ERR(new) : err);526 return new;527out_iput:528 iput(inode);529out:530 trace_f2fs_lookup_end(dir, dentry, ino, err);531 return ERR_PTR(err);532}533 534static int f2fs_unlink(struct inode *dir, struct dentry *dentry)535{536 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);537 struct inode *inode = d_inode(dentry);538 struct f2fs_dir_entry *de;539 struct page *page;540 int err;541 542 trace_f2fs_unlink_enter(dir, dentry);543 544 if (unlikely(f2fs_cp_error(sbi))) {545 err = -EIO;546 goto fail;547 }548 549 err = f2fs_dquot_initialize(dir);550 if (err)551 goto fail;552 err = f2fs_dquot_initialize(inode);553 if (err)554 goto fail;555 556 de = f2fs_find_entry(dir, &dentry->d_name, &page);557 if (!de) {558 if (IS_ERR(page))559 err = PTR_ERR(page);560 goto fail;561 }562 563 f2fs_balance_fs(sbi, true);564 565 f2fs_lock_op(sbi);566 err = f2fs_acquire_orphan_inode(sbi);567 if (err) {568 f2fs_unlock_op(sbi);569 f2fs_put_page(page, 0);570 goto fail;571 }572 f2fs_delete_entry(de, page, dir, inode);573 f2fs_unlock_op(sbi);574 575 /* VFS negative dentries are incompatible with Encoding and576 * Case-insensitiveness. Eventually we'll want avoid577 * invalidating the dentries here, alongside with returning the578 * negative dentries at f2fs_lookup(), when it is better579 * supported by the VFS for the CI case.580 */581 if (IS_ENABLED(CONFIG_UNICODE) && IS_CASEFOLDED(dir))582 d_invalidate(dentry);583 584 if (IS_DIRSYNC(dir))585 f2fs_sync_fs(sbi->sb, 1);586fail:587 trace_f2fs_unlink_exit(inode, err);588 return err;589}590 591static const char *f2fs_get_link(struct dentry *dentry,592 struct inode *inode,593 struct delayed_call *done)594{595 const char *link = page_get_link(dentry, inode, done);596 597 if (!IS_ERR(link) && !*link) {598 /* this is broken symlink case */599 do_delayed_call(done);600 clear_delayed_call(done);601 link = ERR_PTR(-ENOENT);602 }603 return link;604}605 606static int f2fs_symlink(struct mnt_idmap *idmap, struct inode *dir,607 struct dentry *dentry, const char *symname)608{609 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);610 struct inode *inode;611 size_t len = strlen(symname);612 struct fscrypt_str disk_link;613 int err;614 615 if (unlikely(f2fs_cp_error(sbi)))616 return -EIO;617 if (!f2fs_is_checkpoint_ready(sbi))618 return -ENOSPC;619 620 err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize,621 &disk_link);622 if (err)623 return err;624 625 err = f2fs_dquot_initialize(dir);626 if (err)627 return err;628 629 inode = f2fs_new_inode(idmap, dir, S_IFLNK | S_IRWXUGO, NULL);630 if (IS_ERR(inode))631 return PTR_ERR(inode);632 633 if (IS_ENCRYPTED(inode))634 inode->i_op = &f2fs_encrypted_symlink_inode_operations;635 else636 inode->i_op = &f2fs_symlink_inode_operations;637 inode_nohighmem(inode);638 inode->i_mapping->a_ops = &f2fs_dblock_aops;639 640 f2fs_lock_op(sbi);641 err = f2fs_add_link(dentry, inode);642 if (err)643 goto out_f2fs_handle_failed_inode;644 f2fs_unlock_op(sbi);645 f2fs_alloc_nid_done(sbi, inode->i_ino);646 647 err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link);648 if (err)649 goto err_out;650 651 err = page_symlink(inode, disk_link.name, disk_link.len);652 653err_out:654 d_instantiate_new(dentry, inode);655 656 /*657 * Let's flush symlink data in order to avoid broken symlink as much as658 * possible. Nevertheless, fsyncing is the best way, but there is no659 * way to get a file descriptor in order to flush that.660 *661 * Note that, it needs to do dir->fsync to make this recoverable.662 * If the symlink path is stored into inline_data, there is no663 * performance regression.664 */665 if (!err) {666 filemap_write_and_wait_range(inode->i_mapping, 0,667 disk_link.len - 1);668 669 if (IS_DIRSYNC(dir))670 f2fs_sync_fs(sbi->sb, 1);671 } else {672 f2fs_unlink(dir, dentry);673 }674 675 f2fs_balance_fs(sbi, true);676 goto out_free_encrypted_link;677 678out_f2fs_handle_failed_inode:679 f2fs_handle_failed_inode(inode);680out_free_encrypted_link:681 if (disk_link.name != (unsigned char *)symname)682 kfree(disk_link.name);683 return err;684}685 686static int f2fs_mkdir(struct mnt_idmap *idmap, struct inode *dir,687 struct dentry *dentry, umode_t mode)688{689 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);690 struct inode *inode;691 int err;692 693 if (unlikely(f2fs_cp_error(sbi)))694 return -EIO;695 696 err = f2fs_dquot_initialize(dir);697 if (err)698 return err;699 700 inode = f2fs_new_inode(idmap, dir, S_IFDIR | mode, NULL);701 if (IS_ERR(inode))702 return PTR_ERR(inode);703 704 inode->i_op = &f2fs_dir_inode_operations;705 inode->i_fop = &f2fs_dir_operations;706 inode->i_mapping->a_ops = &f2fs_dblock_aops;707 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);708 709 set_inode_flag(inode, FI_INC_LINK);710 f2fs_lock_op(sbi);711 err = f2fs_add_link(dentry, inode);712 if (err)713 goto out_fail;714 f2fs_unlock_op(sbi);715 716 f2fs_alloc_nid_done(sbi, inode->i_ino);717 718 d_instantiate_new(dentry, inode);719 720 if (IS_DIRSYNC(dir))721 f2fs_sync_fs(sbi->sb, 1);722 723 f2fs_balance_fs(sbi, true);724 return 0;725 726out_fail:727 clear_inode_flag(inode, FI_INC_LINK);728 f2fs_handle_failed_inode(inode);729 return err;730}731 732static int f2fs_rmdir(struct inode *dir, struct dentry *dentry)733{734 struct inode *inode = d_inode(dentry);735 736 if (f2fs_empty_dir(inode))737 return f2fs_unlink(dir, dentry);738 return -ENOTEMPTY;739}740 741static int f2fs_mknod(struct mnt_idmap *idmap, struct inode *dir,742 struct dentry *dentry, umode_t mode, dev_t rdev)743{744 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);745 struct inode *inode;746 int err = 0;747 748 if (unlikely(f2fs_cp_error(sbi)))749 return -EIO;750 if (!f2fs_is_checkpoint_ready(sbi))751 return -ENOSPC;752 753 err = f2fs_dquot_initialize(dir);754 if (err)755 return err;756 757 inode = f2fs_new_inode(idmap, dir, mode, NULL);758 if (IS_ERR(inode))759 return PTR_ERR(inode);760 761 init_special_inode(inode, inode->i_mode, rdev);762 inode->i_op = &f2fs_special_inode_operations;763 764 f2fs_lock_op(sbi);765 err = f2fs_add_link(dentry, inode);766 if (err)767 goto out;768 f2fs_unlock_op(sbi);769 770 f2fs_alloc_nid_done(sbi, inode->i_ino);771 772 d_instantiate_new(dentry, inode);773 774 if (IS_DIRSYNC(dir))775 f2fs_sync_fs(sbi->sb, 1);776 777 f2fs_balance_fs(sbi, true);778 return 0;779out:780 f2fs_handle_failed_inode(inode);781 return err;782}783 784static int __f2fs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,785 struct file *file, umode_t mode, bool is_whiteout,786 struct inode **new_inode, struct f2fs_filename *fname)787{788 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);789 struct inode *inode;790 int err;791 792 err = f2fs_dquot_initialize(dir);793 if (err)794 return err;795 796 inode = f2fs_new_inode(idmap, dir, mode, NULL);797 if (IS_ERR(inode))798 return PTR_ERR(inode);799 800 if (is_whiteout) {801 init_special_inode(inode, inode->i_mode, WHITEOUT_DEV);802 inode->i_op = &f2fs_special_inode_operations;803 } else {804 inode->i_op = &f2fs_file_inode_operations;805 inode->i_fop = &f2fs_file_operations;806 inode->i_mapping->a_ops = &f2fs_dblock_aops;807 }808 809 f2fs_lock_op(sbi);810 err = f2fs_acquire_orphan_inode(sbi);811 if (err)812 goto out;813 814 err = f2fs_do_tmpfile(inode, dir, fname);815 if (err)816 goto release_out;817 818 /*819 * add this non-linked tmpfile to orphan list, in this way we could820 * remove all unused data of tmpfile after abnormal power-off.821 */822 f2fs_add_orphan_inode(inode);823 f2fs_alloc_nid_done(sbi, inode->i_ino);824 825 if (is_whiteout) {826 f2fs_i_links_write(inode, false);827 828 spin_lock(&inode->i_lock);829 inode->i_state |= I_LINKABLE;830 spin_unlock(&inode->i_lock);831 } else {832 if (file)833 d_tmpfile(file, inode);834 else835 f2fs_i_links_write(inode, false);836 }837 /* link_count was changed by d_tmpfile as well. */838 f2fs_unlock_op(sbi);839 unlock_new_inode(inode);840 841 if (new_inode)842 *new_inode = inode;843 844 f2fs_balance_fs(sbi, true);845 return 0;846 847release_out:848 f2fs_release_orphan_inode(sbi);849out:850 f2fs_handle_failed_inode(inode);851 return err;852}853 854static int f2fs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,855 struct file *file, umode_t mode)856{857 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);858 int err;859 860 if (unlikely(f2fs_cp_error(sbi)))861 return -EIO;862 if (!f2fs_is_checkpoint_ready(sbi))863 return -ENOSPC;864 865 err = __f2fs_tmpfile(idmap, dir, file, mode, false, NULL, NULL);866 867 return finish_open_simple(file, err);868}869 870static int f2fs_create_whiteout(struct mnt_idmap *idmap,871 struct inode *dir, struct inode **whiteout,872 struct f2fs_filename *fname)873{874 return __f2fs_tmpfile(idmap, dir, NULL, S_IFCHR | WHITEOUT_MODE,875 true, whiteout, fname);876}877 878int f2fs_get_tmpfile(struct mnt_idmap *idmap, struct inode *dir,879 struct inode **new_inode)880{881 return __f2fs_tmpfile(idmap, dir, NULL, S_IFREG,882 false, new_inode, NULL);883}884 885static int f2fs_rename(struct mnt_idmap *idmap, struct inode *old_dir,886 struct dentry *old_dentry, struct inode *new_dir,887 struct dentry *new_dentry, unsigned int flags)888{889 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);890 struct inode *old_inode = d_inode(old_dentry);891 struct inode *new_inode = d_inode(new_dentry);892 struct inode *whiteout = NULL;893 struct page *old_dir_page = NULL;894 struct page *old_page, *new_page = NULL;895 struct f2fs_dir_entry *old_dir_entry = NULL;896 struct f2fs_dir_entry *old_entry;897 struct f2fs_dir_entry *new_entry;898 bool old_is_dir = S_ISDIR(old_inode->i_mode);899 int err;900 901 if (unlikely(f2fs_cp_error(sbi)))902 return -EIO;903 if (!f2fs_is_checkpoint_ready(sbi))904 return -ENOSPC;905 906 if (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&907 (!projid_eq(F2FS_I(new_dir)->i_projid,908 F2FS_I(old_dentry->d_inode)->i_projid)))909 return -EXDEV;910 911 /*912 * If new_inode is null, the below renaming flow will913 * add a link in old_dir which can convert inline_dir.914 * After then, if we failed to get the entry due to other915 * reasons like ENOMEM, we had to remove the new entry.916 * Instead of adding such the error handling routine, let's917 * simply convert first here.918 */919 if (old_dir == new_dir && !new_inode) {920 err = f2fs_try_convert_inline_dir(old_dir, new_dentry);921 if (err)922 return err;923 }924 925 if (flags & RENAME_WHITEOUT) {926 struct f2fs_filename fname;927 928 err = f2fs_setup_filename(old_dir, &old_dentry->d_name,929 0, &fname);930 if (err)931 return err;932 933 err = f2fs_create_whiteout(idmap, old_dir, &whiteout, &fname);934 if (err)935 return err;936 }937 938 err = f2fs_dquot_initialize(old_dir);939 if (err)940 goto out;941 942 err = f2fs_dquot_initialize(new_dir);943 if (err)944 goto out;945 946 if (new_inode) {947 err = f2fs_dquot_initialize(new_inode);948 if (err)949 goto out;950 }951 952 err = -ENOENT;953 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);954 if (!old_entry) {955 if (IS_ERR(old_page))956 err = PTR_ERR(old_page);957 goto out;958 }959 960 if (old_is_dir && old_dir != new_dir) {961 old_dir_entry = f2fs_parent_dir(old_inode, &old_dir_page);962 if (!old_dir_entry) {963 if (IS_ERR(old_dir_page))964 err = PTR_ERR(old_dir_page);965 goto out_old;966 }967 }968 969 if (new_inode) {970 971 err = -ENOTEMPTY;972 if (old_is_dir && !f2fs_empty_dir(new_inode))973 goto out_dir;974 975 err = -ENOENT;976 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,977 &new_page);978 if (!new_entry) {979 if (IS_ERR(new_page))980 err = PTR_ERR(new_page);981 goto out_dir;982 }983 984 f2fs_balance_fs(sbi, true);985 986 f2fs_lock_op(sbi);987 988 err = f2fs_acquire_orphan_inode(sbi);989 if (err)990 goto put_out_dir;991 992 f2fs_set_link(new_dir, new_entry, new_page, old_inode);993 new_page = NULL;994 995 inode_set_ctime_current(new_inode);996 f2fs_down_write(&F2FS_I(new_inode)->i_sem);997 if (old_is_dir)998 f2fs_i_links_write(new_inode, false);999 f2fs_i_links_write(new_inode, false);1000 f2fs_up_write(&F2FS_I(new_inode)->i_sem);1001 1002 if (!new_inode->i_nlink)1003 f2fs_add_orphan_inode(new_inode);1004 else1005 f2fs_release_orphan_inode(sbi);1006 } else {1007 f2fs_balance_fs(sbi, true);1008 1009 f2fs_lock_op(sbi);1010 1011 err = f2fs_add_link(new_dentry, old_inode);1012 if (err) {1013 f2fs_unlock_op(sbi);1014 goto out_dir;1015 }1016 1017 if (old_is_dir)1018 f2fs_i_links_write(new_dir, true);1019 }1020 1021 f2fs_down_write(&F2FS_I(old_inode)->i_sem);1022 if (!old_is_dir || whiteout)1023 file_lost_pino(old_inode);1024 else1025 /* adjust dir's i_pino to pass fsck check */1026 f2fs_i_pino_write(old_inode, new_dir->i_ino);1027 f2fs_up_write(&F2FS_I(old_inode)->i_sem);1028 1029 inode_set_ctime_current(old_inode);1030 f2fs_mark_inode_dirty_sync(old_inode, false);1031 1032 f2fs_delete_entry(old_entry, old_page, old_dir, NULL);1033 old_page = NULL;1034 1035 if (whiteout) {1036 set_inode_flag(whiteout, FI_INC_LINK);1037 err = f2fs_add_link(old_dentry, whiteout);1038 if (err)1039 goto put_out_dir;1040 1041 spin_lock(&whiteout->i_lock);1042 whiteout->i_state &= ~I_LINKABLE;1043 spin_unlock(&whiteout->i_lock);1044 1045 iput(whiteout);1046 }1047 1048 if (old_dir_entry)1049 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);1050 if (old_is_dir)1051 f2fs_i_links_write(old_dir, false);1052 1053 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {1054 f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);1055 if (S_ISDIR(old_inode->i_mode))1056 f2fs_add_ino_entry(sbi, old_inode->i_ino,1057 TRANS_DIR_INO);1058 }1059 1060 f2fs_unlock_op(sbi);1061 1062 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))1063 f2fs_sync_fs(sbi->sb, 1);1064 1065 f2fs_update_time(sbi, REQ_TIME);1066 return 0;1067 1068put_out_dir:1069 f2fs_unlock_op(sbi);1070 f2fs_put_page(new_page, 0);1071out_dir:1072 if (old_dir_entry)1073 f2fs_put_page(old_dir_page, 0);1074out_old:1075 f2fs_put_page(old_page, 0);1076out:1077 iput(whiteout);1078 return err;1079}1080 1081static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,1082 struct inode *new_dir, struct dentry *new_dentry)1083{1084 struct f2fs_sb_info *sbi = F2FS_I_SB(old_dir);1085 struct inode *old_inode = d_inode(old_dentry);1086 struct inode *new_inode = d_inode(new_dentry);1087 struct page *old_dir_page, *new_dir_page;1088 struct page *old_page, *new_page;1089 struct f2fs_dir_entry *old_dir_entry = NULL, *new_dir_entry = NULL;1090 struct f2fs_dir_entry *old_entry, *new_entry;1091 int old_nlink = 0, new_nlink = 0;1092 int err;1093 1094 if (unlikely(f2fs_cp_error(sbi)))1095 return -EIO;1096 if (!f2fs_is_checkpoint_ready(sbi))1097 return -ENOSPC;1098 1099 if ((is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&1100 !projid_eq(F2FS_I(new_dir)->i_projid,1101 F2FS_I(old_dentry->d_inode)->i_projid)) ||1102 (is_inode_flag_set(new_dir, FI_PROJ_INHERIT) &&1103 !projid_eq(F2FS_I(old_dir)->i_projid,1104 F2FS_I(new_dentry->d_inode)->i_projid)))1105 return -EXDEV;1106 1107 err = f2fs_dquot_initialize(old_dir);1108 if (err)1109 goto out;1110 1111 err = f2fs_dquot_initialize(new_dir);1112 if (err)1113 goto out;1114 1115 err = -ENOENT;1116 old_entry = f2fs_find_entry(old_dir, &old_dentry->d_name, &old_page);1117 if (!old_entry) {1118 if (IS_ERR(old_page))1119 err = PTR_ERR(old_page);1120 goto out;1121 }1122 1123 new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name, &new_page);1124 if (!new_entry) {1125 if (IS_ERR(new_page))1126 err = PTR_ERR(new_page);1127 goto out_old;1128 }1129 1130 /* prepare for updating ".." directory entry info later */1131 if (old_dir != new_dir) {1132 if (S_ISDIR(old_inode->i_mode)) {1133 old_dir_entry = f2fs_parent_dir(old_inode,1134 &old_dir_page);1135 if (!old_dir_entry) {1136 if (IS_ERR(old_dir_page))1137 err = PTR_ERR(old_dir_page);1138 goto out_new;1139 }1140 }1141 1142 if (S_ISDIR(new_inode->i_mode)) {1143 new_dir_entry = f2fs_parent_dir(new_inode,1144 &new_dir_page);1145 if (!new_dir_entry) {1146 if (IS_ERR(new_dir_page))1147 err = PTR_ERR(new_dir_page);1148 goto out_old_dir;1149 }1150 }1151 }1152 1153 /*1154 * If cross rename between file and directory those are not1155 * in the same directory, we will inc nlink of file's parent1156 * later, so we should check upper boundary of its nlink.1157 */1158 if ((!old_dir_entry || !new_dir_entry) &&1159 old_dir_entry != new_dir_entry) {1160 old_nlink = old_dir_entry ? -1 : 1;1161 new_nlink = -old_nlink;1162 err = -EMLINK;1163 if ((old_nlink > 0 && old_dir->i_nlink >= F2FS_LINK_MAX) ||1164 (new_nlink > 0 && new_dir->i_nlink >= F2FS_LINK_MAX))1165 goto out_new_dir;1166 }1167 1168 f2fs_balance_fs(sbi, true);1169 1170 f2fs_lock_op(sbi);1171 1172 /* update ".." directory entry info of old dentry */1173 if (old_dir_entry)1174 f2fs_set_link(old_inode, old_dir_entry, old_dir_page, new_dir);1175 1176 /* update ".." directory entry info of new dentry */1177 if (new_dir_entry)1178 f2fs_set_link(new_inode, new_dir_entry, new_dir_page, old_dir);1179 1180 /* update directory entry info of old dir inode */1181 f2fs_set_link(old_dir, old_entry, old_page, new_inode);1182 1183 f2fs_down_write(&F2FS_I(old_inode)->i_sem);1184 if (!old_dir_entry)1185 file_lost_pino(old_inode);1186 else1187 /* adjust dir's i_pino to pass fsck check */1188 f2fs_i_pino_write(old_inode, new_dir->i_ino);1189 f2fs_up_write(&F2FS_I(old_inode)->i_sem);1190 1191 inode_set_ctime_current(old_dir);1192 if (old_nlink) {1193 f2fs_down_write(&F2FS_I(old_dir)->i_sem);1194 f2fs_i_links_write(old_dir, old_nlink > 0);1195 f2fs_up_write(&F2FS_I(old_dir)->i_sem);1196 }1197 f2fs_mark_inode_dirty_sync(old_dir, false);1198 1199 /* update directory entry info of new dir inode */1200 f2fs_set_link(new_dir, new_entry, new_page, old_inode);1201 1202 f2fs_down_write(&F2FS_I(new_inode)->i_sem);1203 if (!new_dir_entry)1204 file_lost_pino(new_inode);1205 else1206 /* adjust dir's i_pino to pass fsck check */1207 f2fs_i_pino_write(new_inode, old_dir->i_ino);1208 f2fs_up_write(&F2FS_I(new_inode)->i_sem);1209 1210 inode_set_ctime_current(new_dir);1211 if (new_nlink) {1212 f2fs_down_write(&F2FS_I(new_dir)->i_sem);1213 f2fs_i_links_write(new_dir, new_nlink > 0);1214 f2fs_up_write(&F2FS_I(new_dir)->i_sem);1215 }1216 f2fs_mark_inode_dirty_sync(new_dir, false);1217 1218 if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT) {1219 f2fs_add_ino_entry(sbi, old_dir->i_ino, TRANS_DIR_INO);1220 f2fs_add_ino_entry(sbi, new_dir->i_ino, TRANS_DIR_INO);1221 }1222 1223 f2fs_unlock_op(sbi);1224 1225 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))1226 f2fs_sync_fs(sbi->sb, 1);1227 1228 f2fs_update_time(sbi, REQ_TIME);1229 return 0;1230out_new_dir:1231 if (new_dir_entry) {1232 f2fs_put_page(new_dir_page, 0);1233 }1234out_old_dir:1235 if (old_dir_entry) {1236 f2fs_put_page(old_dir_page, 0);1237 }1238out_new:1239 f2fs_put_page(new_page, 0);1240out_old:1241 f2fs_put_page(old_page, 0);1242out:1243 return err;1244}1245 1246static int f2fs_rename2(struct mnt_idmap *idmap,1247 struct inode *old_dir, struct dentry *old_dentry,1248 struct inode *new_dir, struct dentry *new_dentry,1249 unsigned int flags)1250{1251 int err;1252 1253 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))1254 return -EINVAL;1255 1256 trace_f2fs_rename_start(old_dir, old_dentry, new_dir, new_dentry,1257 flags);1258 1259 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry,1260 flags);1261 if (err)1262 return err;1263 1264 if (flags & RENAME_EXCHANGE)1265 err = f2fs_cross_rename(old_dir, old_dentry,1266 new_dir, new_dentry);1267 else1268 /*1269 * VFS has already handled the new dentry existence case,1270 * here, we just deal with "RENAME_NOREPLACE" as regular rename.1271 */1272 err = f2fs_rename(idmap, old_dir, old_dentry,1273 new_dir, new_dentry, flags);1274 1275 trace_f2fs_rename_end(old_dentry, new_dentry, flags, err);1276 return err;1277}1278 1279static const char *f2fs_encrypted_get_link(struct dentry *dentry,1280 struct inode *inode,1281 struct delayed_call *done)1282{1283 struct page *page;1284 const char *target;1285 1286 if (!dentry)1287 return ERR_PTR(-ECHILD);1288 1289 page = read_mapping_page(inode->i_mapping, 0, NULL);1290 if (IS_ERR(page))1291 return ERR_CAST(page);1292 1293 target = fscrypt_get_symlink(inode, page_address(page),1294 inode->i_sb->s_blocksize, done);1295 put_page(page);1296 return target;1297}1298 1299static int f2fs_encrypted_symlink_getattr(struct mnt_idmap *idmap,1300 const struct path *path,1301 struct kstat *stat, u32 request_mask,1302 unsigned int query_flags)1303{1304 f2fs_getattr(idmap, path, stat, request_mask, query_flags);1305 1306 return fscrypt_symlink_getattr(path, stat);1307}1308 1309const struct inode_operations f2fs_encrypted_symlink_inode_operations = {1310 .get_link = f2fs_encrypted_get_link,1311 .getattr = f2fs_encrypted_symlink_getattr,1312 .setattr = f2fs_setattr,1313 .listxattr = f2fs_listxattr,1314};1315 1316const struct inode_operations f2fs_dir_inode_operations = {1317 .create = f2fs_create,1318 .lookup = f2fs_lookup,1319 .link = f2fs_link,1320 .unlink = f2fs_unlink,1321 .symlink = f2fs_symlink,1322 .mkdir = f2fs_mkdir,1323 .rmdir = f2fs_rmdir,1324 .mknod = f2fs_mknod,1325 .rename = f2fs_rename2,1326 .tmpfile = f2fs_tmpfile,1327 .getattr = f2fs_getattr,1328 .setattr = f2fs_setattr,1329 .get_inode_acl = f2fs_get_acl,1330 .set_acl = f2fs_set_acl,1331 .listxattr = f2fs_listxattr,1332 .fiemap = f2fs_fiemap,1333 .fileattr_get = f2fs_fileattr_get,1334 .fileattr_set = f2fs_fileattr_set,1335};1336 1337const struct inode_operations f2fs_symlink_inode_operations = {1338 .get_link = f2fs_get_link,1339 .getattr = f2fs_getattr,1340 .setattr = f2fs_setattr,1341 .listxattr = f2fs_listxattr,1342};1343 1344const struct inode_operations f2fs_special_inode_operations = {1345 .getattr = f2fs_getattr,1346 .setattr = f2fs_setattr,1347 .get_inode_acl = f2fs_get_acl,1348 .set_acl = f2fs_set_acl,1349 .listxattr = f2fs_listxattr,1350};1351