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