1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd. 4 */ 5 6 #include <linux/iversion.h> 7 #include <linux/namei.h> 8 #include <linux/slab.h> 9 #include <linux/buffer_head.h> 10 #include <linux/nls.h> 11 12 #include "exfat_raw.h" 13 #include "exfat_fs.h" 14 15 static inline unsigned long exfat_d_version(struct dentry *dentry) 16 { 17 return (unsigned long) dentry->d_fsdata; 18 } 19 20 static inline void exfat_d_version_set(struct dentry *dentry, 21 unsigned long version) 22 { 23 dentry->d_fsdata = (void *) version; 24 } 25 26 /* 27 * If new entry was created in the parent, it could create the 8.3 alias (the 28 * shortname of logname). So, the parent may have the negative-dentry which 29 * matches the created 8.3 alias. 30 * 31 * If it happened, the negative dentry isn't actually negative anymore. So, 32 * drop it. 33 */ 34 static int exfat_d_revalidate(struct dentry *dentry, unsigned int flags) 35 { 36 int ret; 37 38 if (flags & LOOKUP_RCU) 39 return -ECHILD; 40 41 /* 42 * This is not negative dentry. Always valid. 43 * 44 * Note, rename() to existing directory entry will have ->d_inode, and 45 * will use existing name which isn't specified name by user. 46 * 47 * We may be able to drop this positive dentry here. But dropping 48 * positive dentry isn't good idea. So it's unsupported like 49 * rename("filename", "FILENAME") for now. 50 */ 51 if (d_really_is_positive(dentry)) 52 return 1; 53 54 /* 55 * Drop the negative dentry, in order to make sure to use the case 56 * sensitive name which is specified by user if this is for creation. 57 */ 58 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) 59 return 0; 60 61 spin_lock(&dentry->d_lock); 62 ret = inode_eq_iversion(d_inode(dentry->d_parent), 63 exfat_d_version(dentry)); 64 spin_unlock(&dentry->d_lock); 65 return ret; 66 } 67 68 /* returns the length of a struct qstr, ignoring trailing dots if necessary */ 69 static unsigned int exfat_striptail_len(unsigned int len, const char *name, 70 bool keep_last_dots) 71 { 72 if (!keep_last_dots) { 73 while (len && name[len - 1] == '.') 74 len--; 75 } 76 return len; 77 } 78 79 /* 80 * Compute the hash for the exfat name corresponding to the dentry. If the name 81 * is invalid, we leave the hash code unchanged so that the existing dentry can 82 * be used. The exfat fs routines will return ENOENT or EINVAL as appropriate. 83 */ 84 static int exfat_d_hash(const struct dentry *dentry, struct qstr *qstr) 85 { 86 struct super_block *sb = dentry->d_sb; 87 struct nls_table *t = EXFAT_SB(sb)->nls_io; 88 const unsigned char *name = qstr->name; 89 unsigned int len = exfat_striptail_len(qstr->len, qstr->name, 90 EXFAT_SB(sb)->options.keep_last_dots); 91 unsigned long hash = init_name_hash(dentry); 92 int i, charlen; 93 wchar_t c; 94 95 for (i = 0; i < len; i += charlen) { 96 charlen = t->char2uni(&name[i], len - i, &c); 97 if (charlen < 0) 98 return charlen; 99 hash = partial_name_hash(exfat_toupper(sb, c), hash); 100 } 101 102 qstr->hash = end_name_hash(hash); 103 return 0; 104 } 105 106 static int exfat_d_cmp(const struct dentry *dentry, unsigned int len, 107 const char *str, const struct qstr *name) 108 { 109 struct super_block *sb = dentry->d_sb; 110 struct nls_table *t = EXFAT_SB(sb)->nls_io; 111 unsigned int alen = exfat_striptail_len(name->len, name->name, 112 EXFAT_SB(sb)->options.keep_last_dots); 113 unsigned int blen = exfat_striptail_len(len, str, 114 EXFAT_SB(sb)->options.keep_last_dots); 115 wchar_t c1, c2; 116 int charlen, i; 117 118 if (alen != blen) 119 return 1; 120 121 for (i = 0; i < len; i += charlen) { 122 charlen = t->char2uni(&name->name[i], alen - i, &c1); 123 if (charlen < 0) 124 return 1; 125 if (charlen != t->char2uni(&str[i], blen - i, &c2)) 126 return 1; 127 128 if (exfat_toupper(sb, c1) != exfat_toupper(sb, c2)) 129 return 1; 130 } 131 132 return 0; 133 } 134 135 const struct dentry_operations exfat_dentry_ops = { 136 .d_revalidate = exfat_d_revalidate, 137 .d_hash = exfat_d_hash, 138 .d_compare = exfat_d_cmp, 139 }; 140 141 static int exfat_utf8_d_hash(const struct dentry *dentry, struct qstr *qstr) 142 { 143 struct super_block *sb = dentry->d_sb; 144 const unsigned char *name = qstr->name; 145 unsigned int len = exfat_striptail_len(qstr->len, qstr->name, 146 EXFAT_SB(sb)->options.keep_last_dots); 147 unsigned long hash = init_name_hash(dentry); 148 int i, charlen; 149 unicode_t u; 150 151 for (i = 0; i < len; i += charlen) { 152 charlen = utf8_to_utf32(&name[i], len - i, &u); 153 if (charlen < 0) 154 return charlen; 155 156 /* 157 * exfat_toupper() works only for code points up to the U+FFFF. 158 */ 159 hash = partial_name_hash(u <= 0xFFFF ? exfat_toupper(sb, u) : u, 160 hash); 161 } 162 163 qstr->hash = end_name_hash(hash); 164 return 0; 165 } 166 167 static int exfat_utf8_d_cmp(const struct dentry *dentry, unsigned int len, 168 const char *str, const struct qstr *name) 169 { 170 struct super_block *sb = dentry->d_sb; 171 unsigned int alen = exfat_striptail_len(name->len, name->name, 172 EXFAT_SB(sb)->options.keep_last_dots); 173 unsigned int blen = exfat_striptail_len(len, str, 174 EXFAT_SB(sb)->options.keep_last_dots); 175 176 unicode_t u_a, u_b; 177 int charlen, i; 178 179 if (alen != blen) 180 return 1; 181 182 for (i = 0; i < alen; i += charlen) { 183 charlen = utf8_to_utf32(&name->name[i], alen - i, &u_a); 184 if (charlen < 0) 185 return 1; 186 if (charlen != utf8_to_utf32(&str[i], blen - i, &u_b)) 187 return 1; 188 189 if (u_a <= 0xFFFF && u_b <= 0xFFFF) { 190 if (exfat_toupper(sb, u_a) != exfat_toupper(sb, u_b)) 191 return 1; 192 } else { 193 if (u_a != u_b) 194 return 1; 195 } 196 } 197 198 return 0; 199 } 200 201 const struct dentry_operations exfat_utf8_dentry_ops = { 202 .d_revalidate = exfat_d_revalidate, 203 .d_hash = exfat_utf8_d_hash, 204 .d_compare = exfat_utf8_d_cmp, 205 }; 206 207 /* used only in search empty_slot() */ 208 #define CNT_UNUSED_NOHIT (-1) 209 #define CNT_UNUSED_HIT (-2) 210 /* search EMPTY CONTINUOUS "num_entries" entries */ 211 static int exfat_search_empty_slot(struct super_block *sb, 212 struct exfat_hint_femp *hint_femp, struct exfat_chain *p_dir, 213 int num_entries) 214 { 215 int i, dentry, num_empty = 0; 216 int dentries_per_clu; 217 unsigned int type; 218 struct exfat_chain clu; 219 struct exfat_dentry *ep; 220 struct exfat_sb_info *sbi = EXFAT_SB(sb); 221 struct buffer_head *bh; 222 223 dentries_per_clu = sbi->dentries_per_clu; 224 225 if (hint_femp->eidx != EXFAT_HINT_NONE) { 226 dentry = hint_femp->eidx; 227 228 /* 229 * If hint_femp->count is enough, it is needed to check if 230 * there are actual empty entries. 231 * Otherwise, and if "dentry + hint_famp->count" is also equal 232 * to "p_dir->size * dentries_per_clu", it means ENOSPC. 233 */ 234 if (dentry + hint_femp->count == p_dir->size * dentries_per_clu && 235 num_entries > hint_femp->count) 236 return -ENOSPC; 237 238 hint_femp->eidx = EXFAT_HINT_NONE; 239 exfat_chain_dup(&clu, &hint_femp->cur); 240 } else { 241 exfat_chain_dup(&clu, p_dir); 242 dentry = 0; 243 } 244 245 while (clu.dir != EXFAT_EOF_CLUSTER) { 246 i = dentry & (dentries_per_clu - 1); 247 248 for (; i < dentries_per_clu; i++, dentry++) { 249 ep = exfat_get_dentry(sb, &clu, i, &bh); 250 if (!ep) 251 return -EIO; 252 type = exfat_get_entry_type(ep); 253 brelse(bh); 254 255 if (type == TYPE_UNUSED || type == TYPE_DELETED) { 256 num_empty++; 257 if (hint_femp->eidx == EXFAT_HINT_NONE) { 258 hint_femp->eidx = dentry; 259 hint_femp->count = CNT_UNUSED_NOHIT; 260 exfat_chain_set(&hint_femp->cur, 261 clu.dir, clu.size, clu.flags); 262 } 263 264 if (type == TYPE_UNUSED && 265 hint_femp->count != CNT_UNUSED_HIT) 266 hint_femp->count = CNT_UNUSED_HIT; 267 } else { 268 if (hint_femp->eidx != EXFAT_HINT_NONE && 269 hint_femp->count == CNT_UNUSED_HIT) { 270 /* unused empty group means 271 * an empty group which includes 272 * unused dentry 273 */ 274 exfat_fs_error(sb, 275 "found bogus dentry(%d) beyond unused empty group(%d) (start_clu : %u, cur_clu : %u)", 276 dentry, hint_femp->eidx, 277 p_dir->dir, clu.dir); 278 return -EIO; 279 } 280 281 num_empty = 0; 282 hint_femp->eidx = EXFAT_HINT_NONE; 283 } 284 285 if (num_empty >= num_entries) { 286 /* found and invalidate hint_femp */ 287 hint_femp->eidx = EXFAT_HINT_NONE; 288 return (dentry - (num_entries - 1)); 289 } 290 } 291 292 if (clu.flags == ALLOC_NO_FAT_CHAIN) { 293 if (--clu.size > 0) 294 clu.dir++; 295 else 296 clu.dir = EXFAT_EOF_CLUSTER; 297 } else { 298 if (exfat_get_next_cluster(sb, &clu.dir)) 299 return -EIO; 300 } 301 } 302 303 hint_femp->eidx = p_dir->size * dentries_per_clu - num_empty; 304 hint_femp->count = num_empty; 305 if (num_empty == 0) 306 exfat_chain_set(&hint_femp->cur, EXFAT_EOF_CLUSTER, 0, 307 clu.flags); 308 309 return -ENOSPC; 310 } 311 312 static int exfat_check_max_dentries(struct inode *inode) 313 { 314 if (EXFAT_B_TO_DEN(i_size_read(inode)) >= MAX_EXFAT_DENTRIES) { 315 /* 316 * exFAT spec allows a dir to grow up to 8388608(256MB) 317 * dentries 318 */ 319 return -ENOSPC; 320 } 321 return 0; 322 } 323 324 /* find empty directory entry. 325 * if there isn't any empty slot, expand cluster chain. 326 */ 327 static int exfat_find_empty_entry(struct inode *inode, 328 struct exfat_chain *p_dir, int num_entries) 329 { 330 int dentry; 331 unsigned int ret, last_clu; 332 loff_t size = 0; 333 struct exfat_chain clu; 334 struct super_block *sb = inode->i_sb; 335 struct exfat_sb_info *sbi = EXFAT_SB(sb); 336 struct exfat_inode_info *ei = EXFAT_I(inode); 337 struct exfat_hint_femp hint_femp; 338 339 hint_femp.eidx = EXFAT_HINT_NONE; 340 341 if (ei->hint_femp.eidx != EXFAT_HINT_NONE) { 342 hint_femp = ei->hint_femp; 343 ei->hint_femp.eidx = EXFAT_HINT_NONE; 344 } 345 346 while ((dentry = exfat_search_empty_slot(sb, &hint_femp, p_dir, 347 num_entries)) < 0) { 348 if (dentry == -EIO) 349 break; 350 351 if (exfat_check_max_dentries(inode)) 352 return -ENOSPC; 353 354 /* 355 * Allocate new cluster to this directory 356 */ 357 if (ei->start_clu != EXFAT_EOF_CLUSTER) { 358 /* we trust p_dir->size regardless of FAT type */ 359 if (exfat_find_last_cluster(sb, p_dir, &last_clu)) 360 return -EIO; 361 362 exfat_chain_set(&clu, last_clu + 1, 0, p_dir->flags); 363 } else { 364 /* This directory is empty */ 365 exfat_chain_set(&clu, EXFAT_EOF_CLUSTER, 0, 366 ALLOC_NO_FAT_CHAIN); 367 } 368 369 /* allocate a cluster */ 370 ret = exfat_alloc_cluster(inode, 1, &clu, IS_DIRSYNC(inode)); 371 if (ret) 372 return ret; 373 374 if (exfat_zeroed_cluster(inode, clu.dir)) 375 return -EIO; 376 377 if (ei->start_clu == EXFAT_EOF_CLUSTER) { 378 ei->start_clu = clu.dir; 379 p_dir->dir = clu.dir; 380 hint_femp.eidx = 0; 381 } 382 383 /* append to the FAT chain */ 384 if (clu.flags != p_dir->flags) { 385 /* no-fat-chain bit is disabled, 386 * so fat-chain should be synced with alloc-bitmap 387 */ 388 exfat_chain_cont_cluster(sb, p_dir->dir, p_dir->size); 389 p_dir->flags = ALLOC_FAT_CHAIN; 390 hint_femp.cur.flags = ALLOC_FAT_CHAIN; 391 } 392 393 if (clu.flags == ALLOC_FAT_CHAIN) 394 if (exfat_ent_set(sb, last_clu, clu.dir)) 395 return -EIO; 396 397 if (hint_femp.cur.dir == EXFAT_EOF_CLUSTER) 398 exfat_chain_set(&hint_femp.cur, clu.dir, 0, clu.flags); 399 400 hint_femp.count += sbi->dentries_per_clu; 401 402 hint_femp.cur.size++; 403 p_dir->size++; 404 size = EXFAT_CLU_TO_B(p_dir->size, sbi); 405 406 /* directory inode should be updated in here */ 407 i_size_write(inode, size); 408 ei->i_size_ondisk += sbi->cluster_size; 409 ei->i_size_aligned += sbi->cluster_size; 410 ei->flags = p_dir->flags; 411 inode->i_blocks += sbi->cluster_size >> 9; 412 } 413 414 return dentry; 415 } 416 417 /* 418 * Name Resolution Functions : 419 * Zero if it was successful; otherwise nonzero. 420 */ 421 static int __exfat_resolve_path(struct inode *inode, const unsigned char *path, 422 struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname, 423 int lookup) 424 { 425 int namelen; 426 int lossy = NLS_NAME_NO_LOSSY; 427 struct super_block *sb = inode->i_sb; 428 struct exfat_sb_info *sbi = EXFAT_SB(sb); 429 struct exfat_inode_info *ei = EXFAT_I(inode); 430 int pathlen = strlen(path); 431 432 /* 433 * get the length of the pathname excluding 434 * trailing periods, if any. 435 */ 436 namelen = exfat_striptail_len(pathlen, path, false); 437 if (EXFAT_SB(sb)->options.keep_last_dots) { 438 /* 439 * Do not allow the creation of files with names 440 * ending with period(s). 441 */ 442 if (!lookup && (namelen < pathlen)) 443 return -EINVAL; 444 namelen = pathlen; 445 } 446 if (!namelen) 447 return -ENOENT; 448 if (pathlen > (MAX_NAME_LENGTH * MAX_CHARSET_SIZE)) 449 return -ENAMETOOLONG; 450 451 /* 452 * strip all leading spaces : 453 * "MS windows 7" supports leading spaces. 454 * So we should skip this preprocessing for compatibility. 455 */ 456 457 /* file name conversion : 458 * If lookup case, we allow bad-name for compatibility. 459 */ 460 namelen = exfat_nls_to_utf16(sb, path, namelen, p_uniname, 461 &lossy); 462 if (namelen < 0) 463 return namelen; /* return error value */ 464 465 if ((lossy && !lookup) || !namelen) 466 return (lossy & NLS_NAME_OVERLEN) ? -ENAMETOOLONG : -EINVAL; 467 468 exfat_chain_set(p_dir, ei->start_clu, 469 EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags); 470 471 return 0; 472 } 473 474 static inline int exfat_resolve_path(struct inode *inode, 475 const unsigned char *path, struct exfat_chain *dir, 476 struct exfat_uni_name *uni) 477 { 478 return __exfat_resolve_path(inode, path, dir, uni, 0); 479 } 480 481 static inline int exfat_resolve_path_for_lookup(struct inode *inode, 482 const unsigned char *path, struct exfat_chain *dir, 483 struct exfat_uni_name *uni) 484 { 485 return __exfat_resolve_path(inode, path, dir, uni, 1); 486 } 487 488 static inline loff_t exfat_make_i_pos(struct exfat_dir_entry *info) 489 { 490 return ((loff_t) info->dir.dir << 32) | (info->entry & 0xffffffff); 491 } 492 493 static int exfat_add_entry(struct inode *inode, const char *path, 494 struct exfat_chain *p_dir, unsigned int type, 495 struct exfat_dir_entry *info) 496 { 497 int ret, dentry, num_entries; 498 struct super_block *sb = inode->i_sb; 499 struct exfat_sb_info *sbi = EXFAT_SB(sb); 500 struct exfat_uni_name uniname; 501 struct exfat_chain clu; 502 int clu_size = 0; 503 unsigned int start_clu = EXFAT_FREE_CLUSTER; 504 505 ret = exfat_resolve_path(inode, path, p_dir, &uniname); 506 if (ret) 507 goto out; 508 509 num_entries = exfat_calc_num_entries(&uniname); 510 if (num_entries < 0) { 511 ret = num_entries; 512 goto out; 513 } 514 515 /* exfat_find_empty_entry must be called before alloc_cluster() */ 516 dentry = exfat_find_empty_entry(inode, p_dir, num_entries); 517 if (dentry < 0) { 518 ret = dentry; /* -EIO or -ENOSPC */ 519 goto out; 520 } 521 522 if (type == TYPE_DIR) { 523 ret = exfat_alloc_new_dir(inode, &clu); 524 if (ret) 525 goto out; 526 start_clu = clu.dir; 527 clu_size = sbi->cluster_size; 528 } 529 530 /* update the directory entry */ 531 /* fill the dos name directory entry information of the created file. 532 * the first cluster is not determined yet. (0) 533 */ 534 ret = exfat_init_dir_entry(inode, p_dir, dentry, type, 535 start_clu, clu_size); 536 if (ret) 537 goto out; 538 539 ret = exfat_init_ext_entry(inode, p_dir, dentry, num_entries, &uniname); 540 if (ret) 541 goto out; 542 543 info->dir = *p_dir; 544 info->entry = dentry; 545 info->flags = ALLOC_NO_FAT_CHAIN; 546 info->type = type; 547 548 if (type == TYPE_FILE) { 549 info->attr = ATTR_ARCHIVE; 550 info->start_clu = EXFAT_EOF_CLUSTER; 551 info->size = 0; 552 info->num_subdirs = 0; 553 } else { 554 info->attr = ATTR_SUBDIR; 555 info->start_clu = start_clu; 556 info->size = clu_size; 557 info->num_subdirs = EXFAT_MIN_SUBDIR; 558 } 559 memset(&info->crtime, 0, sizeof(info->crtime)); 560 memset(&info->mtime, 0, sizeof(info->mtime)); 561 memset(&info->atime, 0, sizeof(info->atime)); 562 out: 563 return ret; 564 } 565 566 static int exfat_create(struct mnt_idmap *idmap, struct inode *dir, 567 struct dentry *dentry, umode_t mode, bool excl) 568 { 569 struct super_block *sb = dir->i_sb; 570 struct inode *inode; 571 struct exfat_chain cdir; 572 struct exfat_dir_entry info; 573 loff_t i_pos; 574 int err; 575 576 mutex_lock(&EXFAT_SB(sb)->s_lock); 577 exfat_set_volume_dirty(sb); 578 err = exfat_add_entry(dir, dentry->d_name.name, &cdir, TYPE_FILE, 579 &info); 580 if (err) 581 goto unlock; 582 583 inode_inc_iversion(dir); 584 dir->i_mtime = inode_set_ctime_current(dir); 585 if (IS_DIRSYNC(dir)) 586 exfat_sync_inode(dir); 587 else 588 mark_inode_dirty(dir); 589 590 i_pos = exfat_make_i_pos(&info); 591 inode = exfat_build_inode(sb, &info, i_pos); 592 err = PTR_ERR_OR_ZERO(inode); 593 if (err) 594 goto unlock; 595 596 inode_inc_iversion(inode); 597 inode->i_mtime = inode->i_atime = EXFAT_I(inode)->i_crtime = inode_set_ctime_current(inode); 598 exfat_truncate_atime(&inode->i_atime); 599 /* timestamp is already written, so mark_inode_dirty() is unneeded. */ 600 601 d_instantiate(dentry, inode); 602 unlock: 603 mutex_unlock(&EXFAT_SB(sb)->s_lock); 604 return err; 605 } 606 607 /* lookup a file */ 608 static int exfat_find(struct inode *dir, struct qstr *qname, 609 struct exfat_dir_entry *info) 610 { 611 int ret, dentry, count; 612 struct exfat_chain cdir; 613 struct exfat_uni_name uni_name; 614 struct super_block *sb = dir->i_sb; 615 struct exfat_sb_info *sbi = EXFAT_SB(sb); 616 struct exfat_inode_info *ei = EXFAT_I(dir); 617 struct exfat_dentry *ep, *ep2; 618 struct exfat_entry_set_cache es; 619 /* for optimized dir & entry to prevent long traverse of cluster chain */ 620 struct exfat_hint hint_opt; 621 622 if (qname->len == 0) 623 return -ENOENT; 624 625 /* check the validity of directory name in the given pathname */ 626 ret = exfat_resolve_path_for_lookup(dir, qname->name, &cdir, &uni_name); 627 if (ret) 628 return ret; 629 630 /* check the validation of hint_stat and initialize it if required */ 631 if (ei->version != (inode_peek_iversion_raw(dir) & 0xffffffff)) { 632 ei->hint_stat.clu = cdir.dir; 633 ei->hint_stat.eidx = 0; 634 ei->version = (inode_peek_iversion_raw(dir) & 0xffffffff); 635 ei->hint_femp.eidx = EXFAT_HINT_NONE; 636 } 637 638 /* search the file name for directories */ 639 dentry = exfat_find_dir_entry(sb, ei, &cdir, &uni_name, &hint_opt); 640 if (dentry < 0) 641 return dentry; /* -error value */ 642 643 info->dir = cdir; 644 info->entry = dentry; 645 info->num_subdirs = 0; 646 647 /* adjust cdir to the optimized value */ 648 cdir.dir = hint_opt.clu; 649 if (cdir.flags & ALLOC_NO_FAT_CHAIN) 650 cdir.size -= dentry / sbi->dentries_per_clu; 651 dentry = hint_opt.eidx; 652 if (exfat_get_dentry_set(&es, sb, &cdir, dentry, ES_2_ENTRIES)) 653 return -EIO; 654 ep = exfat_get_dentry_cached(&es, ES_IDX_FILE); 655 ep2 = exfat_get_dentry_cached(&es, ES_IDX_STREAM); 656 657 info->type = exfat_get_entry_type(ep); 658 info->attr = le16_to_cpu(ep->dentry.file.attr); 659 info->size = le64_to_cpu(ep2->dentry.stream.valid_size); 660 if (info->size == 0) { 661 info->flags = ALLOC_NO_FAT_CHAIN; 662 info->start_clu = EXFAT_EOF_CLUSTER; 663 } else { 664 info->flags = ep2->dentry.stream.flags; 665 info->start_clu = 666 le32_to_cpu(ep2->dentry.stream.start_clu); 667 } 668 669 exfat_get_entry_time(sbi, &info->crtime, 670 ep->dentry.file.create_tz, 671 ep->dentry.file.create_time, 672 ep->dentry.file.create_date, 673 ep->dentry.file.create_time_cs); 674 exfat_get_entry_time(sbi, &info->mtime, 675 ep->dentry.file.modify_tz, 676 ep->dentry.file.modify_time, 677 ep->dentry.file.modify_date, 678 ep->dentry.file.modify_time_cs); 679 exfat_get_entry_time(sbi, &info->atime, 680 ep->dentry.file.access_tz, 681 ep->dentry.file.access_time, 682 ep->dentry.file.access_date, 683 0); 684 exfat_put_dentry_set(&es, false); 685 686 if (ei->start_clu == EXFAT_FREE_CLUSTER) { 687 exfat_fs_error(sb, 688 "non-zero size file starts with zero cluster (size : %llu, p_dir : %u, entry : 0x%08x)", 689 i_size_read(dir), ei->dir.dir, ei->entry); 690 return -EIO; 691 } 692 693 if (info->type == TYPE_DIR) { 694 exfat_chain_set(&cdir, info->start_clu, 695 EXFAT_B_TO_CLU(info->size, sbi), info->flags); 696 count = exfat_count_dir_entries(sb, &cdir); 697 if (count < 0) 698 return -EIO; 699 700 info->num_subdirs = count + EXFAT_MIN_SUBDIR; 701 } 702 return 0; 703 } 704 705 static int exfat_d_anon_disconn(struct dentry *dentry) 706 { 707 return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED); 708 } 709 710 static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry, 711 unsigned int flags) 712 { 713 struct super_block *sb = dir->i_sb; 714 struct inode *inode; 715 struct dentry *alias; 716 struct exfat_dir_entry info; 717 int err; 718 loff_t i_pos; 719 mode_t i_mode; 720 721 mutex_lock(&EXFAT_SB(sb)->s_lock); 722 err = exfat_find(dir, &dentry->d_name, &info); 723 if (err) { 724 if (err == -ENOENT) { 725 inode = NULL; 726 goto out; 727 } 728 goto unlock; 729 } 730 731 i_pos = exfat_make_i_pos(&info); 732 inode = exfat_build_inode(sb, &info, i_pos); 733 err = PTR_ERR_OR_ZERO(inode); 734 if (err) 735 goto unlock; 736 737 i_mode = inode->i_mode; 738 alias = d_find_alias(inode); 739 740 /* 741 * Checking "alias->d_parent == dentry->d_parent" to make sure 742 * FS is not corrupted (especially double linked dir). 743 */ 744 if (alias && alias->d_parent == dentry->d_parent && 745 !exfat_d_anon_disconn(alias)) { 746 747 /* 748 * Unhashed alias is able to exist because of revalidate() 749 * called by lookup_fast. You can easily make this status 750 * by calling create and lookup concurrently 751 * In such case, we reuse an alias instead of new dentry 752 */ 753 if (d_unhashed(alias)) { 754 WARN_ON(alias->d_name.hash_len != 755 dentry->d_name.hash_len); 756 exfat_info(sb, "rehashed a dentry(%p) in read lookup", 757 alias); 758 d_drop(dentry); 759 d_rehash(alias); 760 } else if (!S_ISDIR(i_mode)) { 761 /* 762 * This inode has non anonymous-DCACHE_DISCONNECTED 763 * dentry. This means, the user did ->lookup() by an 764 * another name (longname vs 8.3 alias of it) in past. 765 * 766 * Switch to new one for reason of locality if possible. 767 */ 768 d_move(alias, dentry); 769 } 770 iput(inode); 771 mutex_unlock(&EXFAT_SB(sb)->s_lock); 772 return alias; 773 } 774 dput(alias); 775 out: 776 mutex_unlock(&EXFAT_SB(sb)->s_lock); 777 if (!inode) 778 exfat_d_version_set(dentry, inode_query_iversion(dir)); 779 780 return d_splice_alias(inode, dentry); 781 unlock: 782 mutex_unlock(&EXFAT_SB(sb)->s_lock); 783 return ERR_PTR(err); 784 } 785 786 /* remove an entry, BUT don't truncate */ 787 static int exfat_unlink(struct inode *dir, struct dentry *dentry) 788 { 789 struct exfat_chain cdir; 790 struct exfat_dentry *ep; 791 struct super_block *sb = dir->i_sb; 792 struct inode *inode = dentry->d_inode; 793 struct exfat_inode_info *ei = EXFAT_I(inode); 794 struct buffer_head *bh; 795 int num_entries, entry, err = 0; 796 797 mutex_lock(&EXFAT_SB(sb)->s_lock); 798 exfat_chain_dup(&cdir, &ei->dir); 799 entry = ei->entry; 800 if (ei->dir.dir == DIR_DELETED) { 801 exfat_err(sb, "abnormal access to deleted dentry"); 802 err = -ENOENT; 803 goto unlock; 804 } 805 806 ep = exfat_get_dentry(sb, &cdir, entry, &bh); 807 if (!ep) { 808 err = -EIO; 809 goto unlock; 810 } 811 num_entries = exfat_count_ext_entries(sb, &cdir, entry, ep); 812 if (num_entries < 0) { 813 err = -EIO; 814 brelse(bh); 815 goto unlock; 816 } 817 num_entries++; 818 brelse(bh); 819 820 exfat_set_volume_dirty(sb); 821 /* update the directory entry */ 822 if (exfat_remove_entries(dir, &cdir, entry, 0, num_entries)) { 823 err = -EIO; 824 goto unlock; 825 } 826 827 /* This doesn't modify ei */ 828 ei->dir.dir = DIR_DELETED; 829 830 inode_inc_iversion(dir); 831 dir->i_mtime = dir->i_atime = inode_set_ctime_current(dir); 832 exfat_truncate_atime(&dir->i_atime); 833 if (IS_DIRSYNC(dir)) 834 exfat_sync_inode(dir); 835 else 836 mark_inode_dirty(dir); 837 838 clear_nlink(inode); 839 inode->i_mtime = inode->i_atime = inode_set_ctime_current(inode); 840 exfat_truncate_atime(&inode->i_atime); 841 exfat_unhash_inode(inode); 842 exfat_d_version_set(dentry, inode_query_iversion(dir)); 843 unlock: 844 mutex_unlock(&EXFAT_SB(sb)->s_lock); 845 return err; 846 } 847 848 static int exfat_mkdir(struct mnt_idmap *idmap, struct inode *dir, 849 struct dentry *dentry, umode_t mode) 850 { 851 struct super_block *sb = dir->i_sb; 852 struct inode *inode; 853 struct exfat_dir_entry info; 854 struct exfat_chain cdir; 855 loff_t i_pos; 856 int err; 857 858 mutex_lock(&EXFAT_SB(sb)->s_lock); 859 exfat_set_volume_dirty(sb); 860 err = exfat_add_entry(dir, dentry->d_name.name, &cdir, TYPE_DIR, 861 &info); 862 if (err) 863 goto unlock; 864 865 inode_inc_iversion(dir); 866 dir->i_mtime = inode_set_ctime_current(dir); 867 if (IS_DIRSYNC(dir)) 868 exfat_sync_inode(dir); 869 else 870 mark_inode_dirty(dir); 871 inc_nlink(dir); 872 873 i_pos = exfat_make_i_pos(&info); 874 inode = exfat_build_inode(sb, &info, i_pos); 875 err = PTR_ERR_OR_ZERO(inode); 876 if (err) 877 goto unlock; 878 879 inode_inc_iversion(inode); 880 inode->i_mtime = inode->i_atime = EXFAT_I(inode)->i_crtime = inode_set_ctime_current(inode); 881 exfat_truncate_atime(&inode->i_atime); 882 /* timestamp is already written, so mark_inode_dirty() is unneeded. */ 883 884 d_instantiate(dentry, inode); 885 886 unlock: 887 mutex_unlock(&EXFAT_SB(sb)->s_lock); 888 return err; 889 } 890 891 static int exfat_check_dir_empty(struct super_block *sb, 892 struct exfat_chain *p_dir) 893 { 894 int i, dentries_per_clu; 895 unsigned int type; 896 struct exfat_chain clu; 897 struct exfat_dentry *ep; 898 struct exfat_sb_info *sbi = EXFAT_SB(sb); 899 struct buffer_head *bh; 900 901 dentries_per_clu = sbi->dentries_per_clu; 902 903 if (p_dir->dir == EXFAT_EOF_CLUSTER) 904 return 0; 905 906 exfat_chain_dup(&clu, p_dir); 907 908 while (clu.dir != EXFAT_EOF_CLUSTER) { 909 for (i = 0; i < dentries_per_clu; i++) { 910 ep = exfat_get_dentry(sb, &clu, i, &bh); 911 if (!ep) 912 return -EIO; 913 type = exfat_get_entry_type(ep); 914 brelse(bh); 915 if (type == TYPE_UNUSED) 916 return 0; 917 918 if (type != TYPE_FILE && type != TYPE_DIR) 919 continue; 920 921 return -ENOTEMPTY; 922 } 923 924 if (clu.flags == ALLOC_NO_FAT_CHAIN) { 925 if (--clu.size > 0) 926 clu.dir++; 927 else 928 clu.dir = EXFAT_EOF_CLUSTER; 929 } else { 930 if (exfat_get_next_cluster(sb, &(clu.dir))) 931 return -EIO; 932 } 933 } 934 935 return 0; 936 } 937 938 static int exfat_rmdir(struct inode *dir, struct dentry *dentry) 939 { 940 struct inode *inode = dentry->d_inode; 941 struct exfat_dentry *ep; 942 struct exfat_chain cdir, clu_to_free; 943 struct super_block *sb = inode->i_sb; 944 struct exfat_sb_info *sbi = EXFAT_SB(sb); 945 struct exfat_inode_info *ei = EXFAT_I(inode); 946 struct buffer_head *bh; 947 int num_entries, entry, err; 948 949 mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock); 950 951 exfat_chain_dup(&cdir, &ei->dir); 952 entry = ei->entry; 953 954 if (ei->dir.dir == DIR_DELETED) { 955 exfat_err(sb, "abnormal access to deleted dentry"); 956 err = -ENOENT; 957 goto unlock; 958 } 959 960 exfat_chain_set(&clu_to_free, ei->start_clu, 961 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi), ei->flags); 962 963 err = exfat_check_dir_empty(sb, &clu_to_free); 964 if (err) { 965 if (err == -EIO) 966 exfat_err(sb, "failed to exfat_check_dir_empty : err(%d)", 967 err); 968 goto unlock; 969 } 970 971 ep = exfat_get_dentry(sb, &cdir, entry, &bh); 972 if (!ep) { 973 err = -EIO; 974 goto unlock; 975 } 976 977 num_entries = exfat_count_ext_entries(sb, &cdir, entry, ep); 978 if (num_entries < 0) { 979 err = -EIO; 980 brelse(bh); 981 goto unlock; 982 } 983 num_entries++; 984 brelse(bh); 985 986 exfat_set_volume_dirty(sb); 987 err = exfat_remove_entries(dir, &cdir, entry, 0, num_entries); 988 if (err) { 989 exfat_err(sb, "failed to exfat_remove_entries : err(%d)", err); 990 goto unlock; 991 } 992 ei->dir.dir = DIR_DELETED; 993 994 inode_inc_iversion(dir); 995 dir->i_mtime = dir->i_atime = inode_set_ctime_current(dir); 996 exfat_truncate_atime(&dir->i_atime); 997 if (IS_DIRSYNC(dir)) 998 exfat_sync_inode(dir); 999 else 1000 mark_inode_dirty(dir); 1001 drop_nlink(dir); 1002 1003 clear_nlink(inode); 1004 inode->i_mtime = inode->i_atime = inode_set_ctime_current(inode); 1005 exfat_truncate_atime(&inode->i_atime); 1006 exfat_unhash_inode(inode); 1007 exfat_d_version_set(dentry, inode_query_iversion(dir)); 1008 unlock: 1009 mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock); 1010 return err; 1011 } 1012 1013 static int exfat_rename_file(struct inode *inode, struct exfat_chain *p_dir, 1014 int oldentry, struct exfat_uni_name *p_uniname, 1015 struct exfat_inode_info *ei) 1016 { 1017 int ret, num_old_entries, num_new_entries; 1018 struct exfat_dentry *epold, *epnew; 1019 struct super_block *sb = inode->i_sb; 1020 struct buffer_head *new_bh, *old_bh; 1021 int sync = IS_DIRSYNC(inode); 1022 1023 epold = exfat_get_dentry(sb, p_dir, oldentry, &old_bh); 1024 if (!epold) 1025 return -EIO; 1026 1027 num_old_entries = exfat_count_ext_entries(sb, p_dir, oldentry, epold); 1028 if (num_old_entries < 0) 1029 return -EIO; 1030 num_old_entries++; 1031 1032 num_new_entries = exfat_calc_num_entries(p_uniname); 1033 if (num_new_entries < 0) 1034 return num_new_entries; 1035 1036 if (num_old_entries < num_new_entries) { 1037 int newentry; 1038 1039 newentry = 1040 exfat_find_empty_entry(inode, p_dir, num_new_entries); 1041 if (newentry < 0) 1042 return newentry; /* -EIO or -ENOSPC */ 1043 1044 epnew = exfat_get_dentry(sb, p_dir, newentry, &new_bh); 1045 if (!epnew) 1046 return -EIO; 1047 1048 *epnew = *epold; 1049 if (exfat_get_entry_type(epnew) == TYPE_FILE) { 1050 epnew->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE); 1051 ei->attr |= ATTR_ARCHIVE; 1052 } 1053 exfat_update_bh(new_bh, sync); 1054 brelse(old_bh); 1055 brelse(new_bh); 1056 1057 epold = exfat_get_dentry(sb, p_dir, oldentry + 1, &old_bh); 1058 if (!epold) 1059 return -EIO; 1060 epnew = exfat_get_dentry(sb, p_dir, newentry + 1, &new_bh); 1061 if (!epnew) { 1062 brelse(old_bh); 1063 return -EIO; 1064 } 1065 1066 *epnew = *epold; 1067 exfat_update_bh(new_bh, sync); 1068 brelse(old_bh); 1069 brelse(new_bh); 1070 1071 ret = exfat_init_ext_entry(inode, p_dir, newentry, 1072 num_new_entries, p_uniname); 1073 if (ret) 1074 return ret; 1075 1076 exfat_remove_entries(inode, p_dir, oldentry, 0, 1077 num_old_entries); 1078 ei->dir = *p_dir; 1079 ei->entry = newentry; 1080 } else { 1081 if (exfat_get_entry_type(epold) == TYPE_FILE) { 1082 epold->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE); 1083 ei->attr |= ATTR_ARCHIVE; 1084 } 1085 exfat_update_bh(old_bh, sync); 1086 brelse(old_bh); 1087 ret = exfat_init_ext_entry(inode, p_dir, oldentry, 1088 num_new_entries, p_uniname); 1089 if (ret) 1090 return ret; 1091 1092 exfat_remove_entries(inode, p_dir, oldentry, num_new_entries, 1093 num_old_entries); 1094 } 1095 return 0; 1096 } 1097 1098 static int exfat_move_file(struct inode *inode, struct exfat_chain *p_olddir, 1099 int oldentry, struct exfat_chain *p_newdir, 1100 struct exfat_uni_name *p_uniname, struct exfat_inode_info *ei) 1101 { 1102 int ret, newentry, num_new_entries, num_old_entries; 1103 struct exfat_dentry *epmov, *epnew; 1104 struct super_block *sb = inode->i_sb; 1105 struct buffer_head *mov_bh, *new_bh; 1106 1107 epmov = exfat_get_dentry(sb, p_olddir, oldentry, &mov_bh); 1108 if (!epmov) 1109 return -EIO; 1110 1111 num_old_entries = exfat_count_ext_entries(sb, p_olddir, oldentry, 1112 epmov); 1113 if (num_old_entries < 0) 1114 return -EIO; 1115 num_old_entries++; 1116 1117 num_new_entries = exfat_calc_num_entries(p_uniname); 1118 if (num_new_entries < 0) 1119 return num_new_entries; 1120 1121 newentry = exfat_find_empty_entry(inode, p_newdir, num_new_entries); 1122 if (newentry < 0) 1123 return newentry; /* -EIO or -ENOSPC */ 1124 1125 epnew = exfat_get_dentry(sb, p_newdir, newentry, &new_bh); 1126 if (!epnew) 1127 return -EIO; 1128 1129 *epnew = *epmov; 1130 if (exfat_get_entry_type(epnew) == TYPE_FILE) { 1131 epnew->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE); 1132 ei->attr |= ATTR_ARCHIVE; 1133 } 1134 exfat_update_bh(new_bh, IS_DIRSYNC(inode)); 1135 brelse(mov_bh); 1136 brelse(new_bh); 1137 1138 epmov = exfat_get_dentry(sb, p_olddir, oldentry + 1, &mov_bh); 1139 if (!epmov) 1140 return -EIO; 1141 epnew = exfat_get_dentry(sb, p_newdir, newentry + 1, &new_bh); 1142 if (!epnew) { 1143 brelse(mov_bh); 1144 return -EIO; 1145 } 1146 1147 *epnew = *epmov; 1148 exfat_update_bh(new_bh, IS_DIRSYNC(inode)); 1149 brelse(mov_bh); 1150 brelse(new_bh); 1151 1152 ret = exfat_init_ext_entry(inode, p_newdir, newentry, num_new_entries, 1153 p_uniname); 1154 if (ret) 1155 return ret; 1156 1157 exfat_remove_entries(inode, p_olddir, oldentry, 0, num_old_entries); 1158 1159 exfat_chain_set(&ei->dir, p_newdir->dir, p_newdir->size, 1160 p_newdir->flags); 1161 1162 ei->entry = newentry; 1163 return 0; 1164 } 1165 1166 /* rename or move a old file into a new file */ 1167 static int __exfat_rename(struct inode *old_parent_inode, 1168 struct exfat_inode_info *ei, struct inode *new_parent_inode, 1169 struct dentry *new_dentry) 1170 { 1171 int ret; 1172 int dentry; 1173 struct exfat_chain olddir, newdir; 1174 struct exfat_chain *p_dir = NULL; 1175 struct exfat_uni_name uni_name; 1176 struct exfat_dentry *ep; 1177 struct super_block *sb = old_parent_inode->i_sb; 1178 struct exfat_sb_info *sbi = EXFAT_SB(sb); 1179 const unsigned char *new_path = new_dentry->d_name.name; 1180 struct inode *new_inode = new_dentry->d_inode; 1181 int num_entries; 1182 struct exfat_inode_info *new_ei = NULL; 1183 unsigned int new_entry_type = TYPE_UNUSED; 1184 int new_entry = 0; 1185 struct buffer_head *new_bh = NULL; 1186 1187 /* check the validity of pointer parameters */ 1188 if (new_path == NULL || strlen(new_path) == 0) 1189 return -EINVAL; 1190 1191 if (ei->dir.dir == DIR_DELETED) { 1192 exfat_err(sb, "abnormal access to deleted source dentry"); 1193 return -ENOENT; 1194 } 1195 1196 exfat_chain_set(&olddir, EXFAT_I(old_parent_inode)->start_clu, 1197 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(old_parent_inode), sbi), 1198 EXFAT_I(old_parent_inode)->flags); 1199 dentry = ei->entry; 1200 1201 /* check whether new dir is existing directory and empty */ 1202 if (new_inode) { 1203 ret = -EIO; 1204 new_ei = EXFAT_I(new_inode); 1205 1206 if (new_ei->dir.dir == DIR_DELETED) { 1207 exfat_err(sb, "abnormal access to deleted target dentry"); 1208 goto out; 1209 } 1210 1211 p_dir = &(new_ei->dir); 1212 new_entry = new_ei->entry; 1213 ep = exfat_get_dentry(sb, p_dir, new_entry, &new_bh); 1214 if (!ep) 1215 goto out; 1216 1217 new_entry_type = exfat_get_entry_type(ep); 1218 brelse(new_bh); 1219 1220 /* if new_inode exists, update ei */ 1221 if (new_entry_type == TYPE_DIR) { 1222 struct exfat_chain new_clu; 1223 1224 new_clu.dir = new_ei->start_clu; 1225 new_clu.size = 1226 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode), 1227 sbi); 1228 new_clu.flags = new_ei->flags; 1229 1230 ret = exfat_check_dir_empty(sb, &new_clu); 1231 if (ret) 1232 goto out; 1233 } 1234 } 1235 1236 /* check the validity of directory name in the given new pathname */ 1237 ret = exfat_resolve_path(new_parent_inode, new_path, &newdir, 1238 &uni_name); 1239 if (ret) 1240 goto out; 1241 1242 exfat_set_volume_dirty(sb); 1243 1244 if (olddir.dir == newdir.dir) 1245 ret = exfat_rename_file(new_parent_inode, &olddir, dentry, 1246 &uni_name, ei); 1247 else 1248 ret = exfat_move_file(new_parent_inode, &olddir, dentry, 1249 &newdir, &uni_name, ei); 1250 1251 if (!ret && new_inode) { 1252 /* delete entries of new_dir */ 1253 ep = exfat_get_dentry(sb, p_dir, new_entry, &new_bh); 1254 if (!ep) { 1255 ret = -EIO; 1256 goto del_out; 1257 } 1258 1259 num_entries = exfat_count_ext_entries(sb, p_dir, new_entry, ep); 1260 if (num_entries < 0) { 1261 ret = -EIO; 1262 goto del_out; 1263 } 1264 brelse(new_bh); 1265 1266 if (exfat_remove_entries(new_inode, p_dir, new_entry, 0, 1267 num_entries + 1)) { 1268 ret = -EIO; 1269 goto del_out; 1270 } 1271 1272 /* Free the clusters if new_inode is a dir(as if exfat_rmdir) */ 1273 if (new_entry_type == TYPE_DIR && 1274 new_ei->start_clu != EXFAT_EOF_CLUSTER) { 1275 /* new_ei, new_clu_to_free */ 1276 struct exfat_chain new_clu_to_free; 1277 1278 exfat_chain_set(&new_clu_to_free, new_ei->start_clu, 1279 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode), 1280 sbi), new_ei->flags); 1281 1282 if (exfat_free_cluster(new_inode, &new_clu_to_free)) { 1283 /* just set I/O error only */ 1284 ret = -EIO; 1285 } 1286 1287 i_size_write(new_inode, 0); 1288 new_ei->start_clu = EXFAT_EOF_CLUSTER; 1289 new_ei->flags = ALLOC_NO_FAT_CHAIN; 1290 } 1291 del_out: 1292 /* Update new_inode ei 1293 * Prevent syncing removed new_inode 1294 * (new_ei is already initialized above code ("if (new_inode)") 1295 */ 1296 new_ei->dir.dir = DIR_DELETED; 1297 } 1298 out: 1299 return ret; 1300 } 1301 1302 static int exfat_rename(struct mnt_idmap *idmap, 1303 struct inode *old_dir, struct dentry *old_dentry, 1304 struct inode *new_dir, struct dentry *new_dentry, 1305 unsigned int flags) 1306 { 1307 struct inode *old_inode, *new_inode; 1308 struct super_block *sb = old_dir->i_sb; 1309 loff_t i_pos; 1310 int err; 1311 1312 /* 1313 * The VFS already checks for existence, so for local filesystems 1314 * the RENAME_NOREPLACE implementation is equivalent to plain rename. 1315 * Don't support any other flags 1316 */ 1317 if (flags & ~RENAME_NOREPLACE) 1318 return -EINVAL; 1319 1320 mutex_lock(&EXFAT_SB(sb)->s_lock); 1321 old_inode = old_dentry->d_inode; 1322 new_inode = new_dentry->d_inode; 1323 1324 err = __exfat_rename(old_dir, EXFAT_I(old_inode), new_dir, new_dentry); 1325 if (err) 1326 goto unlock; 1327 1328 inode_inc_iversion(new_dir); 1329 simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry); 1330 EXFAT_I(new_dir)->i_crtime = current_time(new_dir); 1331 exfat_truncate_atime(&new_dir->i_atime); 1332 if (IS_DIRSYNC(new_dir)) 1333 exfat_sync_inode(new_dir); 1334 else 1335 mark_inode_dirty(new_dir); 1336 1337 i_pos = ((loff_t)EXFAT_I(old_inode)->dir.dir << 32) | 1338 (EXFAT_I(old_inode)->entry & 0xffffffff); 1339 exfat_unhash_inode(old_inode); 1340 exfat_hash_inode(old_inode, i_pos); 1341 if (IS_DIRSYNC(new_dir)) 1342 exfat_sync_inode(old_inode); 1343 else 1344 mark_inode_dirty(old_inode); 1345 1346 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) { 1347 drop_nlink(old_dir); 1348 if (!new_inode) 1349 inc_nlink(new_dir); 1350 } 1351 1352 inode_inc_iversion(old_dir); 1353 if (IS_DIRSYNC(old_dir)) 1354 exfat_sync_inode(old_dir); 1355 else 1356 mark_inode_dirty(old_dir); 1357 1358 if (new_inode) { 1359 exfat_unhash_inode(new_inode); 1360 1361 /* skip drop_nlink if new_inode already has been dropped */ 1362 if (new_inode->i_nlink) { 1363 drop_nlink(new_inode); 1364 if (S_ISDIR(new_inode->i_mode)) 1365 drop_nlink(new_inode); 1366 } else { 1367 exfat_warn(sb, "abnormal access to an inode dropped"); 1368 WARN_ON(new_inode->i_nlink == 0); 1369 } 1370 EXFAT_I(new_inode)->i_crtime = current_time(new_inode); 1371 } 1372 1373 unlock: 1374 mutex_unlock(&EXFAT_SB(sb)->s_lock); 1375 return err; 1376 } 1377 1378 const struct inode_operations exfat_dir_inode_operations = { 1379 .create = exfat_create, 1380 .lookup = exfat_lookup, 1381 .unlink = exfat_unlink, 1382 .mkdir = exfat_mkdir, 1383 .rmdir = exfat_rmdir, 1384 .rename = exfat_rename, 1385 .setattr = exfat_setattr, 1386 .getattr = exfat_getattr, 1387 }; 1388