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 /* search EMPTY CONTINUOUS "num_entries" entries */ 208 static int exfat_search_empty_slot(struct super_block *sb, 209 struct exfat_hint_femp *hint_femp, struct exfat_chain *p_dir, 210 int num_entries, struct exfat_entry_set_cache *es) 211 { 212 int i, dentry, ret; 213 int dentries_per_clu; 214 struct exfat_chain clu; 215 struct exfat_sb_info *sbi = EXFAT_SB(sb); 216 int total_entries = EXFAT_CLU_TO_DEN(p_dir->size, sbi); 217 218 dentries_per_clu = sbi->dentries_per_clu; 219 220 if (hint_femp->eidx != EXFAT_HINT_NONE) { 221 dentry = hint_femp->eidx; 222 223 /* 224 * If hint_femp->count is enough, it is needed to check if 225 * there are actual empty entries. 226 * Otherwise, and if "dentry + hint_famp->count" is also equal 227 * to "p_dir->size * dentries_per_clu", it means ENOSPC. 228 */ 229 if (dentry + hint_femp->count == total_entries && 230 num_entries > hint_femp->count) 231 return -ENOSPC; 232 233 hint_femp->eidx = EXFAT_HINT_NONE; 234 exfat_chain_dup(&clu, &hint_femp->cur); 235 } else { 236 exfat_chain_dup(&clu, p_dir); 237 dentry = 0; 238 } 239 240 while (dentry + num_entries < total_entries && 241 clu.dir != EXFAT_EOF_CLUSTER) { 242 i = dentry & (dentries_per_clu - 1); 243 244 ret = exfat_get_empty_dentry_set(es, sb, &clu, i, num_entries); 245 if (ret < 0) 246 return ret; 247 else if (ret == 0) 248 return dentry; 249 250 dentry += ret; 251 i += ret; 252 253 while (i >= dentries_per_clu) { 254 if (clu.flags == ALLOC_NO_FAT_CHAIN) { 255 if (--clu.size > 0) 256 clu.dir++; 257 else 258 clu.dir = EXFAT_EOF_CLUSTER; 259 } else { 260 if (exfat_get_next_cluster(sb, &clu.dir)) 261 return -EIO; 262 } 263 264 i -= dentries_per_clu; 265 } 266 } 267 268 hint_femp->eidx = dentry; 269 hint_femp->count = 0; 270 if (dentry == total_entries || clu.dir == EXFAT_EOF_CLUSTER) 271 exfat_chain_set(&hint_femp->cur, EXFAT_EOF_CLUSTER, 0, 272 clu.flags); 273 else 274 hint_femp->cur = clu; 275 276 return -ENOSPC; 277 } 278 279 static int exfat_check_max_dentries(struct inode *inode) 280 { 281 if (EXFAT_B_TO_DEN(i_size_read(inode)) >= MAX_EXFAT_DENTRIES) { 282 /* 283 * exFAT spec allows a dir to grow up to 8388608(256MB) 284 * dentries 285 */ 286 return -ENOSPC; 287 } 288 return 0; 289 } 290 291 /* 292 * Find an empty directory entry set. 293 * 294 * If there isn't any empty slot, expand cluster chain. 295 * 296 * in: 297 * inode: inode of the parent directory 298 * num_entries: specifies how many dentries in the empty directory entry set 299 * 300 * out: 301 * p_dir: the cluster where the empty directory entry set is located 302 * es: The found empty directory entry set 303 * 304 * return: 305 * the directory entry index in p_dir is returned on succeeds 306 * -error code is returned on failure 307 */ 308 static int exfat_find_empty_entry(struct inode *inode, 309 struct exfat_chain *p_dir, int num_entries, 310 struct exfat_entry_set_cache *es) 311 { 312 int dentry; 313 unsigned int ret, last_clu; 314 loff_t size = 0; 315 struct exfat_chain clu; 316 struct super_block *sb = inode->i_sb; 317 struct exfat_sb_info *sbi = EXFAT_SB(sb); 318 struct exfat_inode_info *ei = EXFAT_I(inode); 319 struct exfat_hint_femp hint_femp; 320 321 hint_femp.eidx = EXFAT_HINT_NONE; 322 323 if (ei->hint_femp.eidx != EXFAT_HINT_NONE) { 324 hint_femp = ei->hint_femp; 325 ei->hint_femp.eidx = EXFAT_HINT_NONE; 326 } 327 328 exfat_chain_set(p_dir, ei->start_clu, 329 EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags); 330 331 while ((dentry = exfat_search_empty_slot(sb, &hint_femp, p_dir, 332 num_entries, es)) < 0) { 333 if (dentry == -EIO) 334 break; 335 336 if (exfat_check_max_dentries(inode)) 337 return -ENOSPC; 338 339 /* 340 * Allocate new cluster to this directory 341 */ 342 if (ei->start_clu != EXFAT_EOF_CLUSTER) { 343 /* we trust p_dir->size regardless of FAT type */ 344 if (exfat_find_last_cluster(sb, p_dir, &last_clu)) 345 return -EIO; 346 347 exfat_chain_set(&clu, last_clu + 1, 0, p_dir->flags); 348 } else { 349 /* This directory is empty */ 350 exfat_chain_set(&clu, EXFAT_EOF_CLUSTER, 0, 351 ALLOC_NO_FAT_CHAIN); 352 } 353 354 /* allocate a cluster */ 355 ret = exfat_alloc_cluster(inode, 1, &clu, IS_DIRSYNC(inode)); 356 if (ret) 357 return ret; 358 359 if (exfat_zeroed_cluster(inode, clu.dir)) 360 return -EIO; 361 362 if (ei->start_clu == EXFAT_EOF_CLUSTER) { 363 ei->start_clu = clu.dir; 364 p_dir->dir = clu.dir; 365 hint_femp.eidx = 0; 366 } 367 368 /* append to the FAT chain */ 369 if (clu.flags != p_dir->flags) { 370 /* no-fat-chain bit is disabled, 371 * so fat-chain should be synced with alloc-bitmap 372 */ 373 exfat_chain_cont_cluster(sb, p_dir->dir, p_dir->size); 374 p_dir->flags = ALLOC_FAT_CHAIN; 375 hint_femp.cur.flags = ALLOC_FAT_CHAIN; 376 } 377 378 if (clu.flags == ALLOC_FAT_CHAIN) 379 if (exfat_ent_set(sb, last_clu, clu.dir)) 380 return -EIO; 381 382 if (hint_femp.cur.dir == EXFAT_EOF_CLUSTER) 383 exfat_chain_set(&hint_femp.cur, clu.dir, 0, clu.flags); 384 385 hint_femp.count += sbi->dentries_per_clu; 386 387 hint_femp.cur.size++; 388 p_dir->size++; 389 size = EXFAT_CLU_TO_B(p_dir->size, sbi); 390 391 /* directory inode should be updated in here */ 392 i_size_write(inode, size); 393 ei->valid_size += sbi->cluster_size; 394 ei->flags = p_dir->flags; 395 inode->i_blocks += sbi->cluster_size >> 9; 396 } 397 398 p_dir->dir = exfat_sector_to_cluster(sbi, es->bh[0]->b_blocknr); 399 p_dir->size -= dentry / sbi->dentries_per_clu; 400 401 return dentry & (sbi->dentries_per_clu - 1); 402 } 403 404 /* 405 * Name Resolution Functions : 406 * Zero if it was successful; otherwise nonzero. 407 */ 408 static int __exfat_resolve_path(struct inode *inode, const unsigned char *path, 409 struct exfat_uni_name *p_uniname, int lookup) 410 { 411 int namelen; 412 int lossy = NLS_NAME_NO_LOSSY; 413 struct super_block *sb = inode->i_sb; 414 int pathlen = strlen(path); 415 416 /* 417 * get the length of the pathname excluding 418 * trailing periods, if any. 419 */ 420 namelen = exfat_striptail_len(pathlen, path, false); 421 if (EXFAT_SB(sb)->options.keep_last_dots) { 422 /* 423 * Do not allow the creation of files with names 424 * ending with period(s). 425 */ 426 if (!lookup && (namelen < pathlen)) 427 return -EINVAL; 428 namelen = pathlen; 429 } 430 if (!namelen) 431 return -ENOENT; 432 if (pathlen > (MAX_NAME_LENGTH * MAX_CHARSET_SIZE)) 433 return -ENAMETOOLONG; 434 435 /* 436 * strip all leading spaces : 437 * "MS windows 7" supports leading spaces. 438 * So we should skip this preprocessing for compatibility. 439 */ 440 441 /* file name conversion : 442 * If lookup case, we allow bad-name for compatibility. 443 */ 444 namelen = exfat_nls_to_utf16(sb, path, namelen, p_uniname, 445 &lossy); 446 if (namelen < 0) 447 return namelen; /* return error value */ 448 449 if ((lossy && !lookup) || !namelen) 450 return (lossy & NLS_NAME_OVERLEN) ? -ENAMETOOLONG : -EINVAL; 451 452 return 0; 453 } 454 455 static inline int exfat_resolve_path(struct inode *inode, 456 const unsigned char *path, struct exfat_uni_name *uni) 457 { 458 return __exfat_resolve_path(inode, path, uni, 0); 459 } 460 461 static inline int exfat_resolve_path_for_lookup(struct inode *inode, 462 const unsigned char *path, struct exfat_uni_name *uni) 463 { 464 return __exfat_resolve_path(inode, path, uni, 1); 465 } 466 467 static inline loff_t exfat_make_i_pos(struct exfat_dir_entry *info) 468 { 469 return ((loff_t) info->dir.dir << 32) | (info->entry & 0xffffffff); 470 } 471 472 static int exfat_add_entry(struct inode *inode, const char *path, 473 unsigned int type, struct exfat_dir_entry *info) 474 { 475 int ret, dentry, num_entries; 476 struct super_block *sb = inode->i_sb; 477 struct exfat_sb_info *sbi = EXFAT_SB(sb); 478 struct exfat_uni_name uniname; 479 struct exfat_chain clu; 480 struct timespec64 ts = current_time(inode); 481 struct exfat_entry_set_cache es; 482 int clu_size = 0; 483 unsigned int start_clu = EXFAT_FREE_CLUSTER; 484 485 ret = exfat_resolve_path(inode, path, &uniname); 486 if (ret) 487 goto out; 488 489 num_entries = exfat_calc_num_entries(&uniname); 490 if (num_entries < 0) { 491 ret = num_entries; 492 goto out; 493 } 494 495 /* exfat_find_empty_entry must be called before alloc_cluster() */ 496 dentry = exfat_find_empty_entry(inode, &info->dir, num_entries, &es); 497 if (dentry < 0) { 498 ret = dentry; /* -EIO or -ENOSPC */ 499 goto out; 500 } 501 502 if (type == TYPE_DIR && !sbi->options.zero_size_dir) { 503 ret = exfat_alloc_new_dir(inode, &clu); 504 if (ret) { 505 exfat_put_dentry_set(&es, false); 506 goto out; 507 } 508 start_clu = clu.dir; 509 clu_size = sbi->cluster_size; 510 } 511 512 /* update the directory entry */ 513 /* fill the dos name directory entry information of the created file. 514 * the first cluster is not determined yet. (0) 515 */ 516 exfat_init_dir_entry(&es, type, start_clu, clu_size, &ts); 517 exfat_init_ext_entry(&es, num_entries, &uniname); 518 519 ret = exfat_put_dentry_set(&es, IS_DIRSYNC(inode)); 520 if (ret) 521 goto out; 522 523 info->entry = dentry; 524 info->flags = ALLOC_NO_FAT_CHAIN; 525 info->type = type; 526 527 if (type == TYPE_FILE) { 528 info->attr = EXFAT_ATTR_ARCHIVE; 529 info->start_clu = EXFAT_EOF_CLUSTER; 530 info->size = 0; 531 info->num_subdirs = 0; 532 } else { 533 info->attr = EXFAT_ATTR_SUBDIR; 534 if (sbi->options.zero_size_dir) 535 info->start_clu = EXFAT_EOF_CLUSTER; 536 else 537 info->start_clu = start_clu; 538 info->size = clu_size; 539 info->num_subdirs = EXFAT_MIN_SUBDIR; 540 } 541 info->valid_size = info->size; 542 543 memset(&info->crtime, 0, sizeof(info->crtime)); 544 memset(&info->mtime, 0, sizeof(info->mtime)); 545 memset(&info->atime, 0, sizeof(info->atime)); 546 out: 547 return ret; 548 } 549 550 static int exfat_create(struct mnt_idmap *idmap, struct inode *dir, 551 struct dentry *dentry, umode_t mode, bool excl) 552 { 553 struct super_block *sb = dir->i_sb; 554 struct inode *inode; 555 struct exfat_dir_entry info; 556 loff_t i_pos; 557 int err; 558 loff_t size = i_size_read(dir); 559 560 if (unlikely(exfat_forced_shutdown(sb))) 561 return -EIO; 562 563 mutex_lock(&EXFAT_SB(sb)->s_lock); 564 exfat_set_volume_dirty(sb); 565 err = exfat_add_entry(dir, dentry->d_name.name, TYPE_FILE, &info); 566 if (err) 567 goto unlock; 568 569 inode_inc_iversion(dir); 570 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir)); 571 if (IS_DIRSYNC(dir) && size != i_size_read(dir)) 572 exfat_sync_inode(dir); 573 else 574 mark_inode_dirty(dir); 575 576 i_pos = exfat_make_i_pos(&info); 577 inode = exfat_build_inode(sb, &info, i_pos); 578 err = PTR_ERR_OR_ZERO(inode); 579 if (err) 580 goto unlock; 581 582 inode_inc_iversion(inode); 583 EXFAT_I(inode)->i_crtime = simple_inode_init_ts(inode); 584 exfat_truncate_inode_atime(inode); 585 586 /* timestamp is already written, so mark_inode_dirty() is unneeded. */ 587 588 d_instantiate(dentry, inode); 589 unlock: 590 mutex_unlock(&EXFAT_SB(sb)->s_lock); 591 return err; 592 } 593 594 /* lookup a file */ 595 static int exfat_find(struct inode *dir, struct qstr *qname, 596 struct exfat_dir_entry *info) 597 { 598 int ret, dentry, count; 599 struct exfat_chain cdir; 600 struct exfat_uni_name uni_name; 601 struct super_block *sb = dir->i_sb; 602 struct exfat_sb_info *sbi = EXFAT_SB(sb); 603 struct exfat_inode_info *ei = EXFAT_I(dir); 604 struct exfat_dentry *ep, *ep2; 605 struct exfat_entry_set_cache es; 606 /* for optimized dir & entry to prevent long traverse of cluster chain */ 607 struct exfat_hint hint_opt; 608 609 if (qname->len == 0) 610 return -ENOENT; 611 612 /* check the validity of directory name in the given pathname */ 613 ret = exfat_resolve_path_for_lookup(dir, qname->name, &uni_name); 614 if (ret) 615 return ret; 616 617 exfat_chain_set(&cdir, ei->start_clu, 618 EXFAT_B_TO_CLU(i_size_read(dir), sbi), ei->flags); 619 620 /* check the validation of hint_stat and initialize it if required */ 621 if (ei->version != (inode_peek_iversion_raw(dir) & 0xffffffff)) { 622 ei->hint_stat.clu = cdir.dir; 623 ei->hint_stat.eidx = 0; 624 ei->version = (inode_peek_iversion_raw(dir) & 0xffffffff); 625 ei->hint_femp.eidx = EXFAT_HINT_NONE; 626 } 627 628 /* search the file name for directories */ 629 dentry = exfat_find_dir_entry(sb, ei, &cdir, &uni_name, &hint_opt); 630 if (dentry < 0) 631 return dentry; /* -error value */ 632 633 /* adjust cdir to the optimized value */ 634 cdir.dir = hint_opt.clu; 635 if (cdir.flags & ALLOC_NO_FAT_CHAIN) 636 cdir.size -= dentry / sbi->dentries_per_clu; 637 dentry = hint_opt.eidx; 638 639 info->dir = cdir; 640 info->entry = dentry; 641 info->num_subdirs = 0; 642 643 if (exfat_get_dentry_set(&es, sb, &cdir, dentry, ES_2_ENTRIES)) 644 return -EIO; 645 ep = exfat_get_dentry_cached(&es, ES_IDX_FILE); 646 ep2 = exfat_get_dentry_cached(&es, ES_IDX_STREAM); 647 648 info->type = exfat_get_entry_type(ep); 649 info->attr = le16_to_cpu(ep->dentry.file.attr); 650 info->size = le64_to_cpu(ep2->dentry.stream.valid_size); 651 info->valid_size = le64_to_cpu(ep2->dentry.stream.valid_size); 652 info->size = le64_to_cpu(ep2->dentry.stream.size); 653 654 info->start_clu = le32_to_cpu(ep2->dentry.stream.start_clu); 655 if (!is_valid_cluster(sbi, info->start_clu) && info->size) { 656 exfat_warn(sb, "start_clu is invalid cluster(0x%x)", 657 info->start_clu); 658 info->size = 0; 659 info->valid_size = 0; 660 } 661 662 if (info->valid_size > info->size) { 663 exfat_warn(sb, "valid_size(%lld) is greater than size(%lld)", 664 info->valid_size, info->size); 665 info->valid_size = info->size; 666 } 667 668 if (info->size == 0) { 669 info->flags = ALLOC_NO_FAT_CHAIN; 670 info->start_clu = EXFAT_EOF_CLUSTER; 671 } else 672 info->flags = ep2->dentry.stream.flags; 673 674 exfat_get_entry_time(sbi, &info->crtime, 675 ep->dentry.file.create_tz, 676 ep->dentry.file.create_time, 677 ep->dentry.file.create_date, 678 ep->dentry.file.create_time_cs); 679 exfat_get_entry_time(sbi, &info->mtime, 680 ep->dentry.file.modify_tz, 681 ep->dentry.file.modify_time, 682 ep->dentry.file.modify_date, 683 ep->dentry.file.modify_time_cs); 684 exfat_get_entry_time(sbi, &info->atime, 685 ep->dentry.file.access_tz, 686 ep->dentry.file.access_time, 687 ep->dentry.file.access_date, 688 0); 689 exfat_put_dentry_set(&es, false); 690 691 if (ei->start_clu == EXFAT_FREE_CLUSTER) { 692 exfat_fs_error(sb, 693 "non-zero size file starts with zero cluster (size : %llu, p_dir : %u, entry : 0x%08x)", 694 i_size_read(dir), ei->dir.dir, ei->entry); 695 return -EIO; 696 } 697 698 if (info->type == TYPE_DIR) { 699 exfat_chain_set(&cdir, info->start_clu, 700 EXFAT_B_TO_CLU(info->size, sbi), info->flags); 701 count = exfat_count_dir_entries(sb, &cdir); 702 if (count < 0) 703 return -EIO; 704 705 info->num_subdirs = count + EXFAT_MIN_SUBDIR; 706 } 707 return 0; 708 } 709 710 static int exfat_d_anon_disconn(struct dentry *dentry) 711 { 712 return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED); 713 } 714 715 static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry, 716 unsigned int flags) 717 { 718 struct super_block *sb = dir->i_sb; 719 struct inode *inode; 720 struct dentry *alias; 721 struct exfat_dir_entry info; 722 int err; 723 loff_t i_pos; 724 mode_t i_mode; 725 726 mutex_lock(&EXFAT_SB(sb)->s_lock); 727 err = exfat_find(dir, &dentry->d_name, &info); 728 if (err) { 729 if (err == -ENOENT) { 730 inode = NULL; 731 goto out; 732 } 733 goto unlock; 734 } 735 736 i_pos = exfat_make_i_pos(&info); 737 inode = exfat_build_inode(sb, &info, i_pos); 738 err = PTR_ERR_OR_ZERO(inode); 739 if (err) 740 goto unlock; 741 742 i_mode = inode->i_mode; 743 alias = d_find_alias(inode); 744 745 /* 746 * Checking "alias->d_parent == dentry->d_parent" to make sure 747 * FS is not corrupted (especially double linked dir). 748 */ 749 if (alias && alias->d_parent == dentry->d_parent && 750 !exfat_d_anon_disconn(alias)) { 751 752 /* 753 * Unhashed alias is able to exist because of revalidate() 754 * called by lookup_fast. You can easily make this status 755 * by calling create and lookup concurrently 756 * In such case, we reuse an alias instead of new dentry 757 */ 758 if (d_unhashed(alias)) { 759 WARN_ON(alias->d_name.hash_len != 760 dentry->d_name.hash_len); 761 exfat_info(sb, "rehashed a dentry(%p) in read lookup", 762 alias); 763 d_drop(dentry); 764 d_rehash(alias); 765 } else if (!S_ISDIR(i_mode)) { 766 /* 767 * This inode has non anonymous-DCACHE_DISCONNECTED 768 * dentry. This means, the user did ->lookup() by an 769 * another name (longname vs 8.3 alias of it) in past. 770 * 771 * Switch to new one for reason of locality if possible. 772 */ 773 d_move(alias, dentry); 774 } 775 iput(inode); 776 mutex_unlock(&EXFAT_SB(sb)->s_lock); 777 return alias; 778 } 779 dput(alias); 780 out: 781 mutex_unlock(&EXFAT_SB(sb)->s_lock); 782 if (!inode) 783 exfat_d_version_set(dentry, inode_query_iversion(dir)); 784 785 return d_splice_alias(inode, dentry); 786 unlock: 787 mutex_unlock(&EXFAT_SB(sb)->s_lock); 788 return ERR_PTR(err); 789 } 790 791 /* remove an entry, BUT don't truncate */ 792 static int exfat_unlink(struct inode *dir, struct dentry *dentry) 793 { 794 struct super_block *sb = dir->i_sb; 795 struct inode *inode = dentry->d_inode; 796 struct exfat_inode_info *ei = EXFAT_I(inode); 797 struct exfat_entry_set_cache es; 798 int err = 0; 799 800 if (unlikely(exfat_forced_shutdown(sb))) 801 return -EIO; 802 803 mutex_lock(&EXFAT_SB(sb)->s_lock); 804 if (ei->dir.dir == DIR_DELETED) { 805 exfat_err(sb, "abnormal access to deleted dentry"); 806 err = -ENOENT; 807 goto unlock; 808 } 809 810 err = exfat_get_dentry_set_by_ei(&es, sb, ei); 811 if (err) { 812 err = -EIO; 813 goto unlock; 814 } 815 816 exfat_set_volume_dirty(sb); 817 818 /* update the directory entry */ 819 exfat_remove_entries(inode, &es, ES_IDX_FILE); 820 821 err = exfat_put_dentry_set(&es, IS_DIRSYNC(inode)); 822 if (err) 823 goto unlock; 824 825 /* This doesn't modify ei */ 826 ei->dir.dir = DIR_DELETED; 827 828 inode_inc_iversion(dir); 829 simple_inode_init_ts(dir); 830 exfat_truncate_inode_atime(dir); 831 mark_inode_dirty(dir); 832 833 clear_nlink(inode); 834 simple_inode_init_ts(inode); 835 exfat_truncate_inode_atime(inode); 836 exfat_unhash_inode(inode); 837 exfat_d_version_set(dentry, inode_query_iversion(dir)); 838 unlock: 839 mutex_unlock(&EXFAT_SB(sb)->s_lock); 840 return err; 841 } 842 843 static int exfat_mkdir(struct mnt_idmap *idmap, struct inode *dir, 844 struct dentry *dentry, umode_t mode) 845 { 846 struct super_block *sb = dir->i_sb; 847 struct inode *inode; 848 struct exfat_dir_entry info; 849 loff_t i_pos; 850 int err; 851 loff_t size = i_size_read(dir); 852 853 if (unlikely(exfat_forced_shutdown(sb))) 854 return -EIO; 855 856 mutex_lock(&EXFAT_SB(sb)->s_lock); 857 exfat_set_volume_dirty(sb); 858 err = exfat_add_entry(dir, dentry->d_name.name, TYPE_DIR, &info); 859 if (err) 860 goto unlock; 861 862 inode_inc_iversion(dir); 863 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir)); 864 if (IS_DIRSYNC(dir) && size != i_size_read(dir)) 865 exfat_sync_inode(dir); 866 else 867 mark_inode_dirty(dir); 868 inc_nlink(dir); 869 870 i_pos = exfat_make_i_pos(&info); 871 inode = exfat_build_inode(sb, &info, i_pos); 872 err = PTR_ERR_OR_ZERO(inode); 873 if (err) 874 goto unlock; 875 876 inode_inc_iversion(inode); 877 EXFAT_I(inode)->i_crtime = simple_inode_init_ts(inode); 878 exfat_truncate_inode_atime(inode); 879 /* timestamp is already written, so mark_inode_dirty() is unneeded. */ 880 881 d_instantiate(dentry, inode); 882 883 unlock: 884 mutex_unlock(&EXFAT_SB(sb)->s_lock); 885 return err; 886 } 887 888 static int exfat_check_dir_empty(struct super_block *sb, 889 struct exfat_chain *p_dir) 890 { 891 int i, dentries_per_clu; 892 unsigned int type; 893 struct exfat_chain clu; 894 struct exfat_dentry *ep; 895 struct exfat_sb_info *sbi = EXFAT_SB(sb); 896 struct buffer_head *bh; 897 898 dentries_per_clu = sbi->dentries_per_clu; 899 900 if (p_dir->dir == EXFAT_EOF_CLUSTER) 901 return 0; 902 903 exfat_chain_dup(&clu, p_dir); 904 905 while (clu.dir != EXFAT_EOF_CLUSTER) { 906 for (i = 0; i < dentries_per_clu; i++) { 907 ep = exfat_get_dentry(sb, &clu, i, &bh); 908 if (!ep) 909 return -EIO; 910 type = exfat_get_entry_type(ep); 911 brelse(bh); 912 if (type == TYPE_UNUSED) 913 return 0; 914 915 if (type != TYPE_FILE && type != TYPE_DIR) 916 continue; 917 918 return -ENOTEMPTY; 919 } 920 921 if (clu.flags == ALLOC_NO_FAT_CHAIN) { 922 if (--clu.size > 0) 923 clu.dir++; 924 else 925 clu.dir = EXFAT_EOF_CLUSTER; 926 } else { 927 if (exfat_get_next_cluster(sb, &(clu.dir))) 928 return -EIO; 929 } 930 } 931 932 return 0; 933 } 934 935 static int exfat_rmdir(struct inode *dir, struct dentry *dentry) 936 { 937 struct inode *inode = dentry->d_inode; 938 struct exfat_chain clu_to_free; 939 struct super_block *sb = inode->i_sb; 940 struct exfat_sb_info *sbi = EXFAT_SB(sb); 941 struct exfat_inode_info *ei = EXFAT_I(inode); 942 struct exfat_entry_set_cache es; 943 int err; 944 945 if (unlikely(exfat_forced_shutdown(sb))) 946 return -EIO; 947 948 mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock); 949 950 if (ei->dir.dir == DIR_DELETED) { 951 exfat_err(sb, "abnormal access to deleted dentry"); 952 err = -ENOENT; 953 goto unlock; 954 } 955 956 exfat_chain_set(&clu_to_free, ei->start_clu, 957 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi), ei->flags); 958 959 err = exfat_check_dir_empty(sb, &clu_to_free); 960 if (err) { 961 if (err == -EIO) 962 exfat_err(sb, "failed to exfat_check_dir_empty : err(%d)", 963 err); 964 goto unlock; 965 } 966 967 err = exfat_get_dentry_set_by_ei(&es, sb, ei); 968 if (err) { 969 err = -EIO; 970 goto unlock; 971 } 972 973 exfat_set_volume_dirty(sb); 974 975 exfat_remove_entries(inode, &es, ES_IDX_FILE); 976 977 err = exfat_put_dentry_set(&es, IS_DIRSYNC(dir)); 978 if (err) 979 goto unlock; 980 981 ei->dir.dir = DIR_DELETED; 982 983 inode_inc_iversion(dir); 984 simple_inode_init_ts(dir); 985 exfat_truncate_inode_atime(dir); 986 if (IS_DIRSYNC(dir)) 987 exfat_sync_inode(dir); 988 else 989 mark_inode_dirty(dir); 990 drop_nlink(dir); 991 992 clear_nlink(inode); 993 simple_inode_init_ts(inode); 994 exfat_truncate_inode_atime(inode); 995 exfat_unhash_inode(inode); 996 exfat_d_version_set(dentry, inode_query_iversion(dir)); 997 unlock: 998 mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock); 999 return err; 1000 } 1001 1002 static int exfat_rename_file(struct inode *parent_inode, 1003 struct exfat_uni_name *p_uniname, struct exfat_inode_info *ei) 1004 { 1005 int ret, num_new_entries; 1006 struct exfat_dentry *epold, *epnew; 1007 struct super_block *sb = parent_inode->i_sb; 1008 struct exfat_entry_set_cache old_es, new_es; 1009 int sync = IS_DIRSYNC(parent_inode); 1010 1011 if (unlikely(exfat_forced_shutdown(sb))) 1012 return -EIO; 1013 1014 num_new_entries = exfat_calc_num_entries(p_uniname); 1015 if (num_new_entries < 0) 1016 return num_new_entries; 1017 1018 ret = exfat_get_dentry_set_by_ei(&old_es, sb, ei); 1019 if (ret) { 1020 ret = -EIO; 1021 return ret; 1022 } 1023 1024 epold = exfat_get_dentry_cached(&old_es, ES_IDX_FILE); 1025 1026 if (old_es.num_entries < num_new_entries) { 1027 int newentry; 1028 struct exfat_chain dir; 1029 1030 newentry = exfat_find_empty_entry(parent_inode, &dir, 1031 num_new_entries, &new_es); 1032 if (newentry < 0) { 1033 ret = newentry; /* -EIO or -ENOSPC */ 1034 goto put_old_es; 1035 } 1036 1037 epnew = exfat_get_dentry_cached(&new_es, ES_IDX_FILE); 1038 *epnew = *epold; 1039 if (exfat_get_entry_type(epnew) == TYPE_FILE) { 1040 epnew->dentry.file.attr |= cpu_to_le16(EXFAT_ATTR_ARCHIVE); 1041 ei->attr |= EXFAT_ATTR_ARCHIVE; 1042 } 1043 1044 epold = exfat_get_dentry_cached(&old_es, ES_IDX_STREAM); 1045 epnew = exfat_get_dentry_cached(&new_es, ES_IDX_STREAM); 1046 *epnew = *epold; 1047 1048 exfat_init_ext_entry(&new_es, num_new_entries, p_uniname); 1049 1050 ret = exfat_put_dentry_set(&new_es, sync); 1051 if (ret) 1052 goto put_old_es; 1053 1054 exfat_remove_entries(parent_inode, &old_es, ES_IDX_FILE); 1055 ei->dir = dir; 1056 ei->entry = newentry; 1057 } else { 1058 if (exfat_get_entry_type(epold) == TYPE_FILE) { 1059 epold->dentry.file.attr |= cpu_to_le16(EXFAT_ATTR_ARCHIVE); 1060 ei->attr |= EXFAT_ATTR_ARCHIVE; 1061 } 1062 1063 exfat_remove_entries(parent_inode, &old_es, ES_IDX_FIRST_FILENAME + 1); 1064 exfat_init_ext_entry(&old_es, num_new_entries, p_uniname); 1065 } 1066 return exfat_put_dentry_set(&old_es, sync); 1067 1068 put_old_es: 1069 exfat_put_dentry_set(&old_es, false); 1070 return ret; 1071 } 1072 1073 static int exfat_move_file(struct inode *parent_inode, 1074 struct exfat_uni_name *p_uniname, struct exfat_inode_info *ei) 1075 { 1076 int ret, newentry, num_new_entries; 1077 struct exfat_dentry *epmov, *epnew; 1078 struct exfat_entry_set_cache mov_es, new_es; 1079 struct exfat_chain newdir; 1080 1081 num_new_entries = exfat_calc_num_entries(p_uniname); 1082 if (num_new_entries < 0) 1083 return num_new_entries; 1084 1085 ret = exfat_get_dentry_set_by_ei(&mov_es, parent_inode->i_sb, ei); 1086 if (ret) 1087 return -EIO; 1088 1089 newentry = exfat_find_empty_entry(parent_inode, &newdir, 1090 num_new_entries, &new_es); 1091 if (newentry < 0) { 1092 ret = newentry; /* -EIO or -ENOSPC */ 1093 goto put_mov_es; 1094 } 1095 1096 epmov = exfat_get_dentry_cached(&mov_es, ES_IDX_FILE); 1097 epnew = exfat_get_dentry_cached(&new_es, ES_IDX_FILE); 1098 *epnew = *epmov; 1099 if (exfat_get_entry_type(epnew) == TYPE_FILE) { 1100 epnew->dentry.file.attr |= cpu_to_le16(EXFAT_ATTR_ARCHIVE); 1101 ei->attr |= EXFAT_ATTR_ARCHIVE; 1102 } 1103 1104 epmov = exfat_get_dentry_cached(&mov_es, ES_IDX_STREAM); 1105 epnew = exfat_get_dentry_cached(&new_es, ES_IDX_STREAM); 1106 *epnew = *epmov; 1107 1108 exfat_init_ext_entry(&new_es, num_new_entries, p_uniname); 1109 exfat_remove_entries(parent_inode, &mov_es, ES_IDX_FILE); 1110 1111 ei->dir = newdir; 1112 ei->entry = newentry; 1113 1114 ret = exfat_put_dentry_set(&new_es, IS_DIRSYNC(parent_inode)); 1115 if (ret) 1116 goto put_mov_es; 1117 1118 return exfat_put_dentry_set(&mov_es, IS_DIRSYNC(parent_inode)); 1119 1120 put_mov_es: 1121 exfat_put_dentry_set(&mov_es, false); 1122 1123 return ret; 1124 } 1125 1126 /* rename or move a old file into a new file */ 1127 static int __exfat_rename(struct inode *old_parent_inode, 1128 struct exfat_inode_info *ei, struct inode *new_parent_inode, 1129 struct dentry *new_dentry) 1130 { 1131 int ret; 1132 struct exfat_uni_name uni_name; 1133 struct super_block *sb = old_parent_inode->i_sb; 1134 struct exfat_sb_info *sbi = EXFAT_SB(sb); 1135 const unsigned char *new_path = new_dentry->d_name.name; 1136 struct inode *new_inode = new_dentry->d_inode; 1137 struct exfat_inode_info *new_ei = NULL; 1138 1139 /* check the validity of pointer parameters */ 1140 if (new_path == NULL || strlen(new_path) == 0) 1141 return -EINVAL; 1142 1143 if (ei->dir.dir == DIR_DELETED) { 1144 exfat_err(sb, "abnormal access to deleted source dentry"); 1145 return -ENOENT; 1146 } 1147 1148 /* check whether new dir is existing directory and empty */ 1149 if (new_inode) { 1150 ret = -EIO; 1151 new_ei = EXFAT_I(new_inode); 1152 1153 if (new_ei->dir.dir == DIR_DELETED) { 1154 exfat_err(sb, "abnormal access to deleted target dentry"); 1155 goto out; 1156 } 1157 1158 /* if new_inode exists, update ei */ 1159 if (S_ISDIR(new_inode->i_mode)) { 1160 struct exfat_chain new_clu; 1161 1162 new_clu.dir = new_ei->start_clu; 1163 new_clu.size = 1164 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode), 1165 sbi); 1166 new_clu.flags = new_ei->flags; 1167 1168 ret = exfat_check_dir_empty(sb, &new_clu); 1169 if (ret) 1170 goto out; 1171 } 1172 } 1173 1174 /* check the validity of directory name in the given new pathname */ 1175 ret = exfat_resolve_path(new_parent_inode, new_path, &uni_name); 1176 if (ret) 1177 goto out; 1178 1179 exfat_set_volume_dirty(sb); 1180 1181 if (new_parent_inode == old_parent_inode) 1182 ret = exfat_rename_file(new_parent_inode, &uni_name, ei); 1183 else 1184 ret = exfat_move_file(new_parent_inode, &uni_name, ei); 1185 1186 if (!ret && new_inode) { 1187 struct exfat_entry_set_cache es; 1188 1189 /* delete entries of new_dir */ 1190 ret = exfat_get_dentry_set_by_ei(&es, sb, new_ei); 1191 if (ret) { 1192 ret = -EIO; 1193 goto del_out; 1194 } 1195 1196 exfat_remove_entries(new_inode, &es, ES_IDX_FILE); 1197 1198 ret = exfat_put_dentry_set(&es, IS_DIRSYNC(new_inode)); 1199 if (ret) 1200 goto del_out; 1201 1202 /* Free the clusters if new_inode is a dir(as if exfat_rmdir) */ 1203 if (S_ISDIR(new_inode->i_mode) && 1204 new_ei->start_clu != EXFAT_EOF_CLUSTER) { 1205 /* new_ei, new_clu_to_free */ 1206 struct exfat_chain new_clu_to_free; 1207 1208 exfat_chain_set(&new_clu_to_free, new_ei->start_clu, 1209 EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode), 1210 sbi), new_ei->flags); 1211 1212 if (exfat_free_cluster(new_inode, &new_clu_to_free)) { 1213 /* just set I/O error only */ 1214 ret = -EIO; 1215 } 1216 1217 i_size_write(new_inode, 0); 1218 new_ei->valid_size = 0; 1219 new_ei->start_clu = EXFAT_EOF_CLUSTER; 1220 new_ei->flags = ALLOC_NO_FAT_CHAIN; 1221 } 1222 del_out: 1223 /* Update new_inode ei 1224 * Prevent syncing removed new_inode 1225 * (new_ei is already initialized above code ("if (new_inode)") 1226 */ 1227 new_ei->dir.dir = DIR_DELETED; 1228 } 1229 out: 1230 return ret; 1231 } 1232 1233 static int exfat_rename(struct mnt_idmap *idmap, 1234 struct inode *old_dir, struct dentry *old_dentry, 1235 struct inode *new_dir, struct dentry *new_dentry, 1236 unsigned int flags) 1237 { 1238 struct inode *old_inode, *new_inode; 1239 struct super_block *sb = old_dir->i_sb; 1240 loff_t i_pos; 1241 int err; 1242 loff_t size = i_size_read(new_dir); 1243 1244 /* 1245 * The VFS already checks for existence, so for local filesystems 1246 * the RENAME_NOREPLACE implementation is equivalent to plain rename. 1247 * Don't support any other flags 1248 */ 1249 if (flags & ~RENAME_NOREPLACE) 1250 return -EINVAL; 1251 1252 mutex_lock(&EXFAT_SB(sb)->s_lock); 1253 old_inode = old_dentry->d_inode; 1254 new_inode = new_dentry->d_inode; 1255 1256 err = __exfat_rename(old_dir, EXFAT_I(old_inode), new_dir, new_dentry); 1257 if (err) 1258 goto unlock; 1259 1260 inode_inc_iversion(new_dir); 1261 simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry); 1262 EXFAT_I(new_dir)->i_crtime = current_time(new_dir); 1263 exfat_truncate_inode_atime(new_dir); 1264 if (IS_DIRSYNC(new_dir) && size != i_size_read(new_dir)) 1265 exfat_sync_inode(new_dir); 1266 else 1267 mark_inode_dirty(new_dir); 1268 1269 i_pos = ((loff_t)EXFAT_I(old_inode)->dir.dir << 32) | 1270 (EXFAT_I(old_inode)->entry & 0xffffffff); 1271 exfat_unhash_inode(old_inode); 1272 exfat_hash_inode(old_inode, i_pos); 1273 if (IS_DIRSYNC(new_dir)) 1274 exfat_sync_inode(old_inode); 1275 else 1276 mark_inode_dirty(old_inode); 1277 1278 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) { 1279 drop_nlink(old_dir); 1280 if (!new_inode) 1281 inc_nlink(new_dir); 1282 } 1283 1284 inode_inc_iversion(old_dir); 1285 if (new_dir != old_dir) 1286 mark_inode_dirty(old_dir); 1287 1288 if (new_inode) { 1289 exfat_unhash_inode(new_inode); 1290 1291 /* skip drop_nlink if new_inode already has been dropped */ 1292 if (new_inode->i_nlink) { 1293 drop_nlink(new_inode); 1294 if (S_ISDIR(new_inode->i_mode)) 1295 drop_nlink(new_inode); 1296 } else { 1297 exfat_warn(sb, "abnormal access to an inode dropped"); 1298 WARN_ON(new_inode->i_nlink == 0); 1299 } 1300 EXFAT_I(new_inode)->i_crtime = current_time(new_inode); 1301 } 1302 1303 unlock: 1304 mutex_unlock(&EXFAT_SB(sb)->s_lock); 1305 return err; 1306 } 1307 1308 const struct inode_operations exfat_dir_inode_operations = { 1309 .create = exfat_create, 1310 .lookup = exfat_lookup, 1311 .unlink = exfat_unlink, 1312 .mkdir = exfat_mkdir, 1313 .rmdir = exfat_rmdir, 1314 .rename = exfat_rename, 1315 .setattr = exfat_setattr, 1316 .getattr = exfat_getattr, 1317 }; 1318