1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/fs/fat/dir.c 4 * 5 * directory handling functions for fat-based filesystems 6 * 7 * Written 1992,1993 by Werner Almesberger 8 * 9 * Hidden files 1995 by Albert Cahalan <albert@ccs.neu.edu> <adc@coe.neu.edu> 10 * 11 * VFAT extensions by Gordon Chaffee <chaffee@plateau.cs.berkeley.edu> 12 * Merged with msdos fs by Henrik Storner <storner@osiris.ping.dk> 13 * Rewritten for constant inumbers. Plugged buffer overrun in readdir(). AV 14 * Short name translation 1999, 2001 by Wolfram Pienkoss <wp@bszh.de> 15 */ 16 17 #include <linux/slab.h> 18 #include <linux/compat.h> 19 #include <linux/filelock.h> 20 #include <linux/hex.h> 21 #include <linux/uaccess.h> 22 #include <linux/iversion.h> 23 #include "fat.h" 24 25 /* 26 * Maximum buffer size of short name. 27 * [(MSDOS_NAME + '.') * max one char + nul] 28 * For msdos style, ['.' (hidden) + MSDOS_NAME + '.' + nul] 29 */ 30 #define FAT_MAX_SHORT_SIZE ((MSDOS_NAME + 1) * NLS_MAX_CHARSET_SIZE + 1) 31 /* 32 * Maximum buffer size of unicode chars from slots. 33 * [(max longname slots * 13 (size in a slot) + nul) * sizeof(wchar_t)] 34 */ 35 #define FAT_MAX_UNI_CHARS ((MSDOS_SLOTS - 1) * 13 + 1) 36 #define FAT_MAX_UNI_SIZE (FAT_MAX_UNI_CHARS * sizeof(wchar_t)) 37 38 static inline unsigned char fat_tolower(unsigned char c) 39 { 40 return ((c >= 'A') && (c <= 'Z')) ? c+32 : c; 41 } 42 43 static inline loff_t fat_make_i_pos(struct super_block *sb, 44 struct buffer_head *bh, 45 struct msdos_dir_entry *de) 46 { 47 return ((loff_t)bh->b_blocknr << MSDOS_SB(sb)->dir_per_block_bits) 48 | (de - (struct msdos_dir_entry *)bh->b_data); 49 } 50 51 static inline void fat_dir_readahead(struct inode *dir, sector_t iblock, 52 sector_t phys) 53 { 54 struct super_block *sb = dir->i_sb; 55 struct msdos_sb_info *sbi = MSDOS_SB(sb); 56 struct buffer_head *bh; 57 int sec; 58 59 /* This is not a first sector of cluster, or sec_per_clus == 1 */ 60 if ((iblock & (sbi->sec_per_clus - 1)) || sbi->sec_per_clus == 1) 61 return; 62 /* root dir of FAT12/FAT16 */ 63 if (!is_fat32(sbi) && (dir->i_ino == MSDOS_ROOT_INO)) 64 return; 65 66 bh = sb_find_get_block(sb, phys); 67 if (bh == NULL || !buffer_uptodate(bh)) { 68 for (sec = 0; sec < sbi->sec_per_clus; sec++) 69 sb_breadahead(sb, phys + sec); 70 } 71 brelse(bh); 72 } 73 74 /* Returns the inode number of the directory entry at offset pos. If bh is 75 non-NULL, it is brelse'd before. Pos is incremented. The buffer header is 76 returned in bh. 77 AV. Most often we do it item-by-item. Makes sense to optimize. 78 AV. OK, there we go: if both bh and de are non-NULL we assume that we just 79 AV. want the next entry (took one explicit de=NULL in vfat/namei.c). 80 AV. It's done in fat_get_entry() (inlined), here the slow case lives. 81 AV. Additionally, when we return -1 (i.e. reached the end of directory) 82 AV. we make bh NULL. 83 */ 84 static int fat__get_entry(struct inode *dir, loff_t *pos, 85 struct buffer_head **bh, struct msdos_dir_entry **de) 86 { 87 struct super_block *sb = dir->i_sb; 88 sector_t phys, iblock; 89 unsigned long mapped_blocks; 90 int err, offset; 91 92 next: 93 brelse(*bh); 94 *bh = NULL; 95 iblock = *pos >> sb->s_blocksize_bits; 96 err = fat_bmap(dir, iblock, &phys, &mapped_blocks, 0, false); 97 if (err || !phys) 98 return -1; /* beyond EOF or error */ 99 100 fat_dir_readahead(dir, iblock, phys); 101 102 *bh = sb_bread(sb, phys); 103 if (*bh == NULL) { 104 fat_msg_ratelimit(sb, KERN_ERR, 105 "Directory bread(block %llu) failed", (llu)phys); 106 /* skip this block */ 107 *pos = (iblock + 1) << sb->s_blocksize_bits; 108 goto next; 109 } 110 111 offset = *pos & (sb->s_blocksize - 1); 112 *pos += sizeof(struct msdos_dir_entry); 113 *de = (struct msdos_dir_entry *)((*bh)->b_data + offset); 114 115 return 0; 116 } 117 118 static inline int fat_get_entry(struct inode *dir, loff_t *pos, 119 struct buffer_head **bh, 120 struct msdos_dir_entry **de) 121 { 122 /* Fast stuff first */ 123 if (*bh && *de && 124 (*de - (struct msdos_dir_entry *)(*bh)->b_data) < 125 MSDOS_SB(dir->i_sb)->dir_per_block - 1) { 126 *pos += sizeof(struct msdos_dir_entry); 127 (*de)++; 128 return 0; 129 } 130 return fat__get_entry(dir, pos, bh, de); 131 } 132 133 /* 134 * Convert Unicode 16 to UTF-8, translated Unicode, or ASCII. 135 * If uni_xlate is enabled and we can't get a 1:1 conversion, use a 136 * colon as an escape character since it is normally invalid on the vfat 137 * filesystem. The following four characters are the hexadecimal digits 138 * of Unicode value. This lets us do a full dump and restore of Unicode 139 * filenames. We could get into some trouble with long Unicode names, 140 * but ignore that right now. 141 * Ahem... Stack smashing in ring 0 isn't fun. Fixed. 142 */ 143 static int uni16_to_x8(struct super_block *sb, unsigned char *ascii, 144 const wchar_t *uni, int len, struct nls_table *nls) 145 { 146 int uni_xlate = MSDOS_SB(sb)->options.unicode_xlate; 147 const wchar_t *ip; 148 wchar_t ec; 149 unsigned char *op; 150 int charlen; 151 152 ip = uni; 153 op = ascii; 154 155 while (*ip && ((len - NLS_MAX_CHARSET_SIZE) > 0)) { 156 ec = *ip++; 157 charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE); 158 if (charlen > 0) { 159 op += charlen; 160 len -= charlen; 161 } else { 162 if (uni_xlate == 1) { 163 *op++ = ':'; 164 op = hex_byte_pack(op, ec >> 8); 165 op = hex_byte_pack(op, ec); 166 len -= 5; 167 } else { 168 *op++ = '?'; 169 len--; 170 } 171 } 172 } 173 174 if (unlikely(*ip)) { 175 fat_msg(sb, KERN_WARNING, 176 "filename was truncated while converting."); 177 } 178 179 *op = 0; 180 return op - ascii; 181 } 182 183 static inline int fat_uni_to_x8(struct super_block *sb, const wchar_t *uni, 184 unsigned char *buf, int size) 185 { 186 struct msdos_sb_info *sbi = MSDOS_SB(sb); 187 if (sbi->options.utf8) 188 return utf16s_to_utf8s(uni, FAT_MAX_UNI_CHARS, 189 UTF16_HOST_ENDIAN, buf, size); 190 else 191 return uni16_to_x8(sb, buf, uni, size, sbi->nls_io); 192 } 193 194 static inline int 195 fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni) 196 { 197 int charlen; 198 199 charlen = t->char2uni(c, clen, uni); 200 if (charlen < 0) { 201 *uni = 0x003f; /* a question mark */ 202 charlen = 1; 203 } 204 return charlen; 205 } 206 207 static inline int 208 fat_short2lower_uni(struct nls_table *t, unsigned char *c, 209 int clen, wchar_t *uni) 210 { 211 int charlen; 212 wchar_t wc; 213 214 charlen = t->char2uni(c, clen, &wc); 215 if (charlen < 0) { 216 *uni = 0x003f; /* a question mark */ 217 charlen = 1; 218 } else if (charlen <= 1) { 219 unsigned char nc = t->charset2lower[*c]; 220 221 if (!nc) 222 nc = *c; 223 224 charlen = t->char2uni(&nc, 1, uni); 225 if (charlen < 0) { 226 *uni = 0x003f; /* a question mark */ 227 charlen = 1; 228 } 229 } else 230 *uni = wc; 231 232 return charlen; 233 } 234 235 static inline int 236 fat_shortname2uni(struct nls_table *nls, unsigned char *buf, int buf_size, 237 wchar_t *uni_buf, unsigned short opt, int lower) 238 { 239 int len = 0; 240 241 if (opt & VFAT_SFN_DISPLAY_LOWER) 242 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf); 243 else if (opt & VFAT_SFN_DISPLAY_WIN95) 244 len = fat_short2uni(nls, buf, buf_size, uni_buf); 245 else if (opt & VFAT_SFN_DISPLAY_WINNT) { 246 if (lower) 247 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf); 248 else 249 len = fat_short2uni(nls, buf, buf_size, uni_buf); 250 } else 251 len = fat_short2uni(nls, buf, buf_size, uni_buf); 252 253 return len; 254 } 255 256 static inline int fat_name_match(struct msdos_sb_info *sbi, 257 const unsigned char *a, int a_len, 258 const unsigned char *b, int b_len) 259 { 260 if (a_len != b_len) 261 return 0; 262 263 if (sbi->options.name_check != 's') 264 return !nls_strnicmp(sbi->nls_io, a, b, a_len); 265 else 266 return !memcmp(a, b, a_len); 267 } 268 269 enum { PARSE_INVALID = 1, PARSE_NOT_LONGNAME, PARSE_EOF, }; 270 271 /** 272 * fat_parse_long - Parse extended directory entry. 273 * 274 * @dir: Pointer to the inode that represents the directory. 275 * @pos: On input, contains the starting position to read from. 276 * On output, updated with the new position. 277 * @bh: Pointer to the buffer head that may be used for reading directory 278 * entries. May be updated. 279 * @de: On input, points to the current directory entry. 280 * On output, points to the next directory entry. 281 * @unicode: Pointer to a buffer where the parsed Unicode long filename will be 282 * stored. 283 * @nr_slots: Pointer to a variable that will store the number of longname 284 * slots found. 285 * 286 * This function returns zero on success, negative value on error, or one of 287 * the following: 288 * 289 * %PARSE_INVALID - Directory entry is invalid. 290 * %PARSE_NOT_LONGNAME - Directory entry does not contain longname. 291 * %PARSE_EOF - Directory has no more entries. 292 */ 293 static int fat_parse_long(struct inode *dir, loff_t *pos, 294 struct buffer_head **bh, struct msdos_dir_entry **de, 295 wchar_t **unicode, unsigned char *nr_slots) 296 { 297 struct msdos_dir_slot *ds; 298 unsigned char id, slot, slots, alias_checksum; 299 300 if (!*unicode) { 301 *unicode = __getname(); 302 if (!*unicode) { 303 brelse(*bh); 304 return -ENOMEM; 305 } 306 } 307 parse_long: 308 ds = (struct msdos_dir_slot *)*de; 309 id = ds->id; 310 if (!(id & 0x40)) 311 return PARSE_INVALID; 312 slots = id & ~0x40; 313 if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */ 314 return PARSE_INVALID; 315 *nr_slots = slots; 316 alias_checksum = ds->alias_checksum; 317 318 slot = slots; 319 while (1) { 320 int offset; 321 322 slot--; 323 offset = slot * 13; 324 fat16_towchar(*unicode + offset, ds->name0_4, 5); 325 fat16_towchar(*unicode + offset + 5, ds->name5_10, 6); 326 fat16_towchar(*unicode + offset + 11, ds->name11_12, 2); 327 328 if (ds->id & 0x40) 329 (*unicode)[offset + 13] = 0; 330 if (fat_get_entry(dir, pos, bh, de) < 0) 331 return PARSE_EOF; 332 if (slot == 0) 333 break; 334 ds = (struct msdos_dir_slot *)*de; 335 if (ds->attr != ATTR_EXT) 336 return PARSE_NOT_LONGNAME; 337 if ((ds->id & ~0x40) != slot) 338 goto parse_long; 339 if (ds->alias_checksum != alias_checksum) 340 goto parse_long; 341 } 342 if ((*de)->name[0] == DELETED_FLAG) 343 return PARSE_INVALID; 344 if ((*de)->attr == ATTR_EXT) 345 goto parse_long; 346 if (IS_FREE((*de)->name) || ((*de)->attr & ATTR_VOLUME)) 347 return PARSE_INVALID; 348 if (fat_checksum((*de)->name) != alias_checksum) 349 *nr_slots = 0; 350 351 return 0; 352 } 353 354 /** 355 * fat_parse_short - Parse MS-DOS (short) directory entry. 356 * @sb: superblock 357 * @de: directory entry to parse 358 * @name: FAT_MAX_SHORT_SIZE array in which to place extracted name 359 * @dot_hidden: Nonzero == prepend '.' to names with ATTR_HIDDEN 360 * 361 * Returns the number of characters extracted into 'name'. 362 */ 363 static int fat_parse_short(struct super_block *sb, 364 const struct msdos_dir_entry *de, 365 unsigned char *name, int dot_hidden) 366 { 367 const struct msdos_sb_info *sbi = MSDOS_SB(sb); 368 int isvfat = sbi->options.isvfat; 369 int nocase = sbi->options.nocase; 370 unsigned short opt_shortname = sbi->options.shortname; 371 struct nls_table *nls_disk = sbi->nls_disk; 372 wchar_t uni_name[14]; 373 unsigned char c, work[MSDOS_NAME]; 374 unsigned char *ptname = name; 375 int chi, chl, i, j, k; 376 int dotoffset = 0; 377 int name_len = 0, uni_len = 0; 378 379 if (!isvfat && dot_hidden && (de->attr & ATTR_HIDDEN)) { 380 *ptname++ = '.'; 381 dotoffset = 1; 382 } 383 384 memcpy(work, de->name, sizeof(work)); 385 /* For an explanation of the special treatment of 0x05 in 386 * filenames, see msdos_format_name in namei_msdos.c 387 */ 388 if (work[0] == 0x05) 389 work[0] = 0xE5; 390 391 /* Filename */ 392 for (i = 0, j = 0; i < 8;) { 393 c = work[i]; 394 if (!c) 395 break; 396 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i, 397 &uni_name[j++], opt_shortname, 398 de->lcase & CASE_LOWER_BASE); 399 if (chl <= 1) { 400 if (!isvfat) 401 ptname[i] = nocase ? c : fat_tolower(c); 402 i++; 403 if (c != ' ') { 404 name_len = i; 405 uni_len = j; 406 } 407 } else { 408 uni_len = j; 409 if (isvfat) 410 i += min(chl, 8-i); 411 else { 412 for (chi = 0; chi < chl && i < 8; chi++, i++) 413 ptname[i] = work[i]; 414 } 415 if (chl) 416 name_len = i; 417 } 418 } 419 420 i = name_len; 421 j = uni_len; 422 fat_short2uni(nls_disk, ".", 1, &uni_name[j++]); 423 if (!isvfat) 424 ptname[i] = '.'; 425 i++; 426 427 /* Extension */ 428 for (k = 8; k < MSDOS_NAME;) { 429 c = work[k]; 430 if (!c) 431 break; 432 chl = fat_shortname2uni(nls_disk, &work[k], MSDOS_NAME - k, 433 &uni_name[j++], opt_shortname, 434 de->lcase & CASE_LOWER_EXT); 435 if (chl <= 1) { 436 k++; 437 if (!isvfat) 438 ptname[i] = nocase ? c : fat_tolower(c); 439 i++; 440 if (c != ' ') { 441 name_len = i; 442 uni_len = j; 443 } 444 } else { 445 uni_len = j; 446 if (isvfat) { 447 int offset = min(chl, MSDOS_NAME-k); 448 k += offset; 449 i += offset; 450 } else { 451 for (chi = 0; chi < chl && k < MSDOS_NAME; 452 chi++, i++, k++) { 453 ptname[i] = work[k]; 454 } 455 } 456 if (chl) 457 name_len = i; 458 } 459 } 460 461 if (name_len > 0) { 462 name_len += dotoffset; 463 464 if (sbi->options.isvfat) { 465 uni_name[uni_len] = 0x0000; 466 name_len = fat_uni_to_x8(sb, uni_name, name, 467 FAT_MAX_SHORT_SIZE); 468 } 469 } 470 471 return name_len; 472 } 473 474 /* 475 * Return values: negative -> error/not found, 0 -> found. 476 */ 477 int fat_search_long(struct inode *inode, const unsigned char *name, 478 int name_len, struct fat_slot_info *sinfo) 479 { 480 struct super_block *sb = inode->i_sb; 481 struct msdos_sb_info *sbi = MSDOS_SB(sb); 482 struct buffer_head *bh = NULL; 483 struct msdos_dir_entry *de; 484 unsigned char nr_slots; 485 wchar_t *unicode = NULL; 486 unsigned char bufname[FAT_MAX_SHORT_SIZE]; 487 loff_t cpos = 0; 488 int err, len; 489 490 err = -ENOENT; 491 while (1) { 492 if (fat_get_entry(inode, &cpos, &bh, &de) == -1) 493 goto end_of_dir; 494 parse_record: 495 nr_slots = 0; 496 if (de->name[0] == DELETED_FLAG) 497 continue; 498 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME)) 499 continue; 500 if (de->attr != ATTR_EXT && IS_FREE(de->name)) 501 continue; 502 if (de->attr == ATTR_EXT) { 503 int status = fat_parse_long(inode, &cpos, &bh, &de, 504 &unicode, &nr_slots); 505 if (status < 0) { 506 err = status; 507 goto end_of_dir; 508 } else if (status == PARSE_INVALID) 509 continue; 510 else if (status == PARSE_NOT_LONGNAME) 511 goto parse_record; 512 else if (status == PARSE_EOF) 513 goto end_of_dir; 514 } 515 516 /* Never prepend '.' to hidden files here. 517 * That is done only for msdos mounts (and only when 518 * 'dotsOK=yes'); if we are executing here, it is in the 519 * context of a vfat mount. 520 */ 521 len = fat_parse_short(sb, de, bufname, 0); 522 if (len == 0) 523 continue; 524 525 /* Compare shortname */ 526 if (fat_name_match(sbi, name, name_len, bufname, len)) 527 goto found; 528 529 if (nr_slots) { 530 void *longname = unicode + FAT_MAX_UNI_CHARS; 531 int size = PATH_MAX - FAT_MAX_UNI_SIZE; 532 533 /* Compare longname */ 534 len = fat_uni_to_x8(sb, unicode, longname, size); 535 if (fat_name_match(sbi, name, name_len, longname, len)) 536 goto found; 537 } 538 } 539 540 found: 541 nr_slots++; /* include the de */ 542 sinfo->slot_off = cpos - nr_slots * sizeof(*de); 543 sinfo->nr_slots = nr_slots; 544 sinfo->de = de; 545 sinfo->bh = bh; 546 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de); 547 err = 0; 548 end_of_dir: 549 if (unicode) 550 __putname(unicode); 551 552 return err; 553 } 554 EXPORT_SYMBOL_GPL(fat_search_long); 555 556 struct fat_ioctl_filldir_callback { 557 struct dir_context ctx; 558 void __user *dirent; 559 int result; 560 /* for dir ioctl */ 561 const char *longname; 562 int long_len; 563 const char *shortname; 564 int short_len; 565 }; 566 567 static int __fat_readdir(struct inode *inode, struct file *file, 568 struct dir_context *ctx, int short_only, 569 struct fat_ioctl_filldir_callback *both) 570 { 571 struct super_block *sb = inode->i_sb; 572 struct msdos_sb_info *sbi = MSDOS_SB(sb); 573 struct buffer_head *bh; 574 struct msdos_dir_entry *de; 575 unsigned char nr_slots; 576 wchar_t *unicode = NULL; 577 unsigned char bufname[FAT_MAX_SHORT_SIZE]; 578 int isvfat = sbi->options.isvfat; 579 const char *fill_name = NULL; 580 int fake_offset = 0; 581 loff_t cpos; 582 int short_len = 0, fill_len = 0; 583 int ret = 0; 584 585 mutex_lock(&sbi->s_lock); 586 587 cpos = ctx->pos; 588 /* Fake . and .. for the root directory. */ 589 if (inode->i_ino == MSDOS_ROOT_INO) { 590 if (!dir_emit_dots(file, ctx)) 591 goto out; 592 if (ctx->pos == 2) { 593 fake_offset = 1; 594 cpos = 0; 595 } 596 } 597 if (cpos & (sizeof(struct msdos_dir_entry) - 1)) { 598 ret = -ENOENT; 599 goto out; 600 } 601 602 bh = NULL; 603 get_new: 604 if (fat_get_entry(inode, &cpos, &bh, &de) == -1) 605 goto end_of_dir; 606 parse_record: 607 nr_slots = 0; 608 /* 609 * Check for long filename entry, but if short_only, we don't 610 * need to parse long filename. 611 */ 612 if (isvfat && !short_only) { 613 if (de->name[0] == DELETED_FLAG) 614 goto record_end; 615 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME)) 616 goto record_end; 617 if (de->attr != ATTR_EXT && IS_FREE(de->name)) 618 goto record_end; 619 } else { 620 if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name)) 621 goto record_end; 622 } 623 624 if (isvfat && de->attr == ATTR_EXT) { 625 int status = fat_parse_long(inode, &cpos, &bh, &de, 626 &unicode, &nr_slots); 627 if (status < 0) { 628 bh = NULL; 629 ret = status; 630 goto end_of_dir; 631 } else if (status == PARSE_INVALID) 632 goto record_end; 633 else if (status == PARSE_NOT_LONGNAME) 634 goto parse_record; 635 else if (status == PARSE_EOF) 636 goto end_of_dir; 637 638 if (nr_slots) { 639 void *longname = unicode + FAT_MAX_UNI_CHARS; 640 int size = PATH_MAX - FAT_MAX_UNI_SIZE; 641 int len = fat_uni_to_x8(sb, unicode, longname, size); 642 643 fill_name = longname; 644 fill_len = len; 645 /* !both && !short_only, so we don't need shortname. */ 646 if (!both) 647 goto start_filldir; 648 649 short_len = fat_parse_short(sb, de, bufname, 650 sbi->options.dotsOK); 651 if (short_len == 0) 652 goto record_end; 653 /* hack for fat_ioctl_filldir() */ 654 both->longname = fill_name; 655 both->long_len = fill_len; 656 both->shortname = bufname; 657 both->short_len = short_len; 658 fill_name = NULL; 659 fill_len = 0; 660 goto start_filldir; 661 } 662 } 663 664 short_len = fat_parse_short(sb, de, bufname, sbi->options.dotsOK); 665 if (short_len == 0) 666 goto record_end; 667 668 fill_name = bufname; 669 fill_len = short_len; 670 671 start_filldir: 672 ctx->pos = cpos - (nr_slots + 1) * sizeof(struct msdos_dir_entry); 673 if (fake_offset && ctx->pos < 2) 674 ctx->pos = 2; 675 676 if (!memcmp(de->name, MSDOS_DOT, MSDOS_NAME)) { 677 if (!dir_emit_dot(file, ctx)) 678 goto fill_failed; 679 } else if (!memcmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) { 680 if (!dir_emit_dotdot(file, ctx)) 681 goto fill_failed; 682 } else { 683 unsigned long inum; 684 loff_t i_pos = fat_make_i_pos(sb, bh, de); 685 struct inode *tmp = fat_iget(sb, i_pos); 686 if (tmp) { 687 inum = tmp->i_ino; 688 iput(tmp); 689 } else 690 inum = iunique(sb, MSDOS_ROOT_INO); 691 if (!dir_emit(ctx, fill_name, fill_len, inum, 692 (de->attr & ATTR_DIR) ? DT_DIR : DT_REG)) 693 goto fill_failed; 694 } 695 696 record_end: 697 fake_offset = 0; 698 ctx->pos = cpos; 699 goto get_new; 700 701 end_of_dir: 702 if (fake_offset && cpos < 2) 703 ctx->pos = 2; 704 else 705 ctx->pos = cpos; 706 fill_failed: 707 brelse(bh); 708 if (unicode) 709 __putname(unicode); 710 out: 711 mutex_unlock(&sbi->s_lock); 712 713 return ret; 714 } 715 716 static int fat_readdir(struct file *file, struct dir_context *ctx) 717 { 718 return __fat_readdir(file_inode(file), file, ctx, 0, NULL); 719 } 720 721 #define FAT_IOCTL_FILLDIR_FUNC(func, dirent_type) \ 722 static bool func(struct dir_context *ctx, const char *name, int name_len, \ 723 loff_t offset, u64 ino, unsigned int d_type) \ 724 { \ 725 struct fat_ioctl_filldir_callback *buf = \ 726 container_of(ctx, struct fat_ioctl_filldir_callback, ctx); \ 727 struct dirent_type __user *d1 = buf->dirent; \ 728 struct dirent_type __user *d2 = d1 + 1; \ 729 \ 730 if (buf->result) \ 731 return false; \ 732 buf->result++; \ 733 \ 734 if (name != NULL) { \ 735 /* dirent has only short name */ \ 736 if (name_len >= sizeof(d1->d_name)) \ 737 name_len = sizeof(d1->d_name) - 1; \ 738 \ 739 if (put_user(0, &d2->d_name[0]) || \ 740 put_user(0, &d2->d_reclen) || \ 741 copy_to_user(d1->d_name, name, name_len) || \ 742 put_user(0, d1->d_name + name_len) || \ 743 put_user(name_len, &d1->d_reclen)) \ 744 goto efault; \ 745 } else { \ 746 /* dirent has short and long name */ \ 747 const char *longname = buf->longname; \ 748 int long_len = buf->long_len; \ 749 const char *shortname = buf->shortname; \ 750 int short_len = buf->short_len; \ 751 \ 752 if (long_len >= sizeof(d1->d_name)) \ 753 long_len = sizeof(d1->d_name) - 1; \ 754 if (short_len >= sizeof(d1->d_name)) \ 755 short_len = sizeof(d1->d_name) - 1; \ 756 \ 757 if (copy_to_user(d2->d_name, longname, long_len) || \ 758 put_user(0, d2->d_name + long_len) || \ 759 put_user(long_len, &d2->d_reclen) || \ 760 put_user(ino, &d2->d_ino) || \ 761 put_user(offset, &d2->d_off) || \ 762 copy_to_user(d1->d_name, shortname, short_len) || \ 763 put_user(0, d1->d_name + short_len) || \ 764 put_user(short_len, &d1->d_reclen)) \ 765 goto efault; \ 766 } \ 767 return true; \ 768 efault: \ 769 buf->result = -EFAULT; \ 770 return false; \ 771 } 772 773 FAT_IOCTL_FILLDIR_FUNC(fat_ioctl_filldir, __fat_dirent) 774 775 static int fat_ioctl_readdir(struct inode *inode, struct file *file, 776 void __user *dirent, filldir_t filldir, 777 int short_only, int both) 778 { 779 struct fat_ioctl_filldir_callback buf = { 780 .ctx.actor = filldir, 781 .dirent = dirent 782 }; 783 int ret; 784 785 buf.dirent = dirent; 786 buf.result = 0; 787 inode_lock_shared(inode); 788 buf.ctx.pos = file->f_pos; 789 ret = -ENOENT; 790 if (!IS_DEADDIR(inode)) { 791 ret = __fat_readdir(inode, file, &buf.ctx, 792 short_only, both ? &buf : NULL); 793 file->f_pos = buf.ctx.pos; 794 } 795 inode_unlock_shared(inode); 796 if (ret >= 0) 797 ret = buf.result; 798 return ret; 799 } 800 801 static long fat_dir_ioctl(struct file *filp, unsigned int cmd, 802 unsigned long arg) 803 { 804 struct inode *inode = file_inode(filp); 805 struct __fat_dirent __user *d1 = (struct __fat_dirent __user *)arg; 806 int short_only, both; 807 808 switch (cmd) { 809 case VFAT_IOCTL_READDIR_SHORT: 810 short_only = 1; 811 both = 0; 812 break; 813 case VFAT_IOCTL_READDIR_BOTH: 814 short_only = 0; 815 both = 1; 816 break; 817 default: 818 return fat_generic_ioctl(filp, cmd, arg); 819 } 820 821 /* 822 * Yes, we don't need this put_user() absolutely. However old 823 * code didn't return the right value. So, app use this value, 824 * in order to check whether it is EOF. 825 */ 826 if (put_user(0, &d1->d_reclen)) 827 return -EFAULT; 828 829 return fat_ioctl_readdir(inode, filp, d1, fat_ioctl_filldir, 830 short_only, both); 831 } 832 833 #ifdef CONFIG_COMPAT 834 #define VFAT_IOCTL_READDIR_BOTH32 _IOR('r', 1, struct compat_dirent[2]) 835 #define VFAT_IOCTL_READDIR_SHORT32 _IOR('r', 2, struct compat_dirent[2]) 836 837 FAT_IOCTL_FILLDIR_FUNC(fat_compat_ioctl_filldir, compat_dirent) 838 839 static long fat_compat_dir_ioctl(struct file *filp, unsigned cmd, 840 unsigned long arg) 841 { 842 struct inode *inode = file_inode(filp); 843 struct compat_dirent __user *d1 = compat_ptr(arg); 844 int short_only, both; 845 846 switch (cmd) { 847 case VFAT_IOCTL_READDIR_SHORT32: 848 short_only = 1; 849 both = 0; 850 break; 851 case VFAT_IOCTL_READDIR_BOTH32: 852 short_only = 0; 853 both = 1; 854 break; 855 default: 856 return fat_generic_ioctl(filp, cmd, (unsigned long)arg); 857 } 858 859 /* 860 * Yes, we don't need this put_user() absolutely. However old 861 * code didn't return the right value. So, app use this value, 862 * in order to check whether it is EOF. 863 */ 864 if (put_user(0, &d1->d_reclen)) 865 return -EFAULT; 866 867 return fat_ioctl_readdir(inode, filp, d1, fat_compat_ioctl_filldir, 868 short_only, both); 869 } 870 #endif /* CONFIG_COMPAT */ 871 872 const struct file_operations fat_dir_operations = { 873 .llseek = generic_file_llseek, 874 .read = generic_read_dir, 875 .iterate_shared = fat_readdir, 876 .unlocked_ioctl = fat_dir_ioctl, 877 #ifdef CONFIG_COMPAT 878 .compat_ioctl = fat_compat_dir_ioctl, 879 #endif 880 .fsync = fat_file_fsync, 881 .setlease = generic_setlease, 882 }; 883 884 static int fat_get_short_entry(struct inode *dir, loff_t *pos, 885 struct buffer_head **bh, 886 struct msdos_dir_entry **de) 887 { 888 while (fat_get_entry(dir, pos, bh, de) >= 0) { 889 /* free entry or long name entry or volume label */ 890 if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME)) 891 return 0; 892 } 893 return -ENOENT; 894 } 895 896 /* 897 * The ".." entry can not provide the "struct fat_slot_info" information 898 * for inode, nor a usable i_pos. So, this function provides some information 899 * only. 900 * 901 * Since this function walks through the on-disk inodes within a directory, 902 * callers are responsible for taking any locks necessary to prevent the 903 * directory from changing. 904 */ 905 int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh, 906 struct msdos_dir_entry **de) 907 { 908 loff_t offset = 0; 909 910 *de = NULL; 911 while (fat_get_short_entry(dir, &offset, bh, de) >= 0) { 912 if (!strncmp((*de)->name, MSDOS_DOTDOT, MSDOS_NAME)) 913 return 0; 914 } 915 return -ENOENT; 916 } 917 EXPORT_SYMBOL_GPL(fat_get_dotdot_entry); 918 919 /* See if directory is empty */ 920 int fat_dir_empty(struct inode *dir) 921 { 922 struct buffer_head *bh; 923 struct msdos_dir_entry *de; 924 loff_t cpos; 925 int result = 0; 926 927 bh = NULL; 928 cpos = 0; 929 while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) { 930 if (strncmp(de->name, MSDOS_DOT , MSDOS_NAME) && 931 strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) { 932 result = -ENOTEMPTY; 933 break; 934 } 935 } 936 brelse(bh); 937 return result; 938 } 939 EXPORT_SYMBOL_GPL(fat_dir_empty); 940 941 /* 942 * fat_subdirs counts the number of sub-directories of dir. It can be run 943 * on directories being created. 944 */ 945 int fat_subdirs(struct inode *dir) 946 { 947 struct buffer_head *bh; 948 struct msdos_dir_entry *de; 949 loff_t cpos; 950 int count = 0; 951 952 bh = NULL; 953 cpos = 0; 954 while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) { 955 if (de->attr & ATTR_DIR) 956 count++; 957 } 958 brelse(bh); 959 return count; 960 } 961 962 /* 963 * Scans a directory for a given file (name points to its formatted name). 964 * Returns an error code or zero. 965 */ 966 int fat_scan(struct inode *dir, const unsigned char *name, 967 struct fat_slot_info *sinfo) 968 { 969 struct super_block *sb = dir->i_sb; 970 971 sinfo->slot_off = 0; 972 sinfo->bh = NULL; 973 while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh, 974 &sinfo->de) >= 0) { 975 if (!strncmp(sinfo->de->name, name, MSDOS_NAME)) { 976 sinfo->slot_off -= sizeof(*sinfo->de); 977 sinfo->nr_slots = 1; 978 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de); 979 return 0; 980 } 981 } 982 return -ENOENT; 983 } 984 EXPORT_SYMBOL_GPL(fat_scan); 985 986 /* 987 * Scans a directory for a given logstart. 988 * Returns an error code or zero. 989 */ 990 int fat_scan_logstart(struct inode *dir, int i_logstart, 991 struct fat_slot_info *sinfo) 992 { 993 struct super_block *sb = dir->i_sb; 994 995 sinfo->slot_off = 0; 996 sinfo->bh = NULL; 997 while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh, 998 &sinfo->de) >= 0) { 999 if (fat_get_start(MSDOS_SB(sb), sinfo->de) == i_logstart) { 1000 sinfo->slot_off -= sizeof(*sinfo->de); 1001 sinfo->nr_slots = 1; 1002 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de); 1003 return 0; 1004 } 1005 } 1006 return -ENOENT; 1007 } 1008 1009 static int __fat_remove_entries(struct inode *dir, loff_t pos, int nr_slots) 1010 { 1011 struct super_block *sb = dir->i_sb; 1012 struct buffer_head *bh; 1013 struct msdos_dir_entry *de, *endp; 1014 int err = 0, orig_slots; 1015 1016 while (nr_slots) { 1017 bh = NULL; 1018 if (fat_get_entry(dir, &pos, &bh, &de) < 0) { 1019 err = -EIO; 1020 break; 1021 } 1022 1023 orig_slots = nr_slots; 1024 endp = (struct msdos_dir_entry *)(bh->b_data + sb->s_blocksize); 1025 while (nr_slots && de < endp) { 1026 de->name[0] = DELETED_FLAG; 1027 de++; 1028 nr_slots--; 1029 } 1030 mark_buffer_dirty_inode(bh, dir); 1031 if (IS_DIRSYNC(dir)) 1032 err = sync_dirty_buffer(bh); 1033 brelse(bh); 1034 if (err) 1035 break; 1036 1037 /* pos is *next* de's position, so this does `- sizeof(de)' */ 1038 pos += ((orig_slots - nr_slots) * sizeof(*de)) - sizeof(*de); 1039 } 1040 1041 return err; 1042 } 1043 1044 int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo) 1045 { 1046 struct super_block *sb = dir->i_sb; 1047 struct msdos_dir_entry *de; 1048 struct buffer_head *bh; 1049 int err = 0, nr_slots; 1050 1051 /* 1052 * First stage: Remove the shortname. By this, the directory 1053 * entry is removed. 1054 */ 1055 nr_slots = sinfo->nr_slots; 1056 de = sinfo->de; 1057 sinfo->de = NULL; 1058 bh = sinfo->bh; 1059 sinfo->bh = NULL; 1060 while (nr_slots && de >= (struct msdos_dir_entry *)bh->b_data) { 1061 de->name[0] = DELETED_FLAG; 1062 de--; 1063 nr_slots--; 1064 } 1065 mark_buffer_dirty_inode(bh, dir); 1066 if (IS_DIRSYNC(dir)) 1067 err = sync_dirty_buffer(bh); 1068 brelse(bh); 1069 if (err) 1070 return err; 1071 inode_inc_iversion(dir); 1072 1073 if (nr_slots) { 1074 /* 1075 * Second stage: remove the remaining longname slots. 1076 * (This directory entry is already removed, and so return 1077 * the success) 1078 */ 1079 err = __fat_remove_entries(dir, sinfo->slot_off, nr_slots); 1080 if (err) { 1081 fat_msg(sb, KERN_WARNING, 1082 "Couldn't remove the long name slots"); 1083 } 1084 } 1085 1086 fat_truncate_time(dir, NULL, FAT_UPDATE_ATIME | FAT_UPDATE_CMTIME); 1087 if (IS_DIRSYNC(dir)) 1088 (void)fat_sync_inode(dir); 1089 else 1090 mark_inode_dirty(dir); 1091 1092 return 0; 1093 } 1094 EXPORT_SYMBOL_GPL(fat_remove_entries); 1095 1096 static int fat_zeroed_cluster(struct inode *dir, sector_t blknr, int nr_used, 1097 struct buffer_head **bhs, int nr_bhs) 1098 { 1099 struct super_block *sb = dir->i_sb; 1100 sector_t last_blknr = blknr + MSDOS_SB(sb)->sec_per_clus; 1101 int err, i, n; 1102 1103 /* Zeroing the unused blocks on this cluster */ 1104 blknr += nr_used; 1105 n = nr_used; 1106 while (blknr < last_blknr) { 1107 bhs[n] = sb_getblk(sb, blknr); 1108 if (!bhs[n]) { 1109 err = -ENOMEM; 1110 goto error; 1111 } 1112 /* Avoid race with userspace read via bdev */ 1113 lock_buffer(bhs[n]); 1114 memset(bhs[n]->b_data, 0, sb->s_blocksize); 1115 set_buffer_uptodate(bhs[n]); 1116 unlock_buffer(bhs[n]); 1117 mark_buffer_dirty_inode(bhs[n], dir); 1118 1119 n++; 1120 blknr++; 1121 if (n == nr_bhs) { 1122 if (IS_DIRSYNC(dir)) { 1123 err = fat_sync_bhs(bhs, n); 1124 if (err) 1125 goto error; 1126 } 1127 for (i = 0; i < n; i++) 1128 brelse(bhs[i]); 1129 n = 0; 1130 } 1131 } 1132 if (IS_DIRSYNC(dir)) { 1133 err = fat_sync_bhs(bhs, n); 1134 if (err) 1135 goto error; 1136 } 1137 for (i = 0; i < n; i++) 1138 brelse(bhs[i]); 1139 1140 return 0; 1141 1142 error: 1143 for (i = 0; i < n; i++) 1144 bforget(bhs[i]); 1145 return err; 1146 } 1147 1148 int fat_alloc_new_dir(struct inode *dir, struct timespec64 *ts) 1149 { 1150 struct super_block *sb = dir->i_sb; 1151 struct msdos_sb_info *sbi = MSDOS_SB(sb); 1152 struct buffer_head *bhs[MAX_BUF_PER_PAGE]; 1153 struct msdos_dir_entry *de; 1154 sector_t blknr; 1155 __le16 date, time; 1156 u8 time_cs; 1157 int err, cluster; 1158 1159 err = fat_alloc_clusters(dir, &cluster, 1); 1160 if (err) 1161 goto error; 1162 1163 blknr = fat_clus_to_blknr(sbi, cluster); 1164 bhs[0] = sb_getblk(sb, blknr); 1165 if (!bhs[0]) { 1166 err = -ENOMEM; 1167 goto error_free; 1168 } 1169 1170 fat_time_unix2fat(sbi, ts, &time, &date, &time_cs); 1171 1172 de = (struct msdos_dir_entry *)bhs[0]->b_data; 1173 /* Avoid race with userspace read via bdev */ 1174 lock_buffer(bhs[0]); 1175 /* filling the new directory slots ("." and ".." entries) */ 1176 memcpy(de[0].name, MSDOS_DOT, MSDOS_NAME); 1177 memcpy(de[1].name, MSDOS_DOTDOT, MSDOS_NAME); 1178 de->attr = de[1].attr = ATTR_DIR; 1179 de[0].lcase = de[1].lcase = 0; 1180 de[0].time = de[1].time = time; 1181 de[0].date = de[1].date = date; 1182 if (sbi->options.isvfat) { 1183 /* extra timestamps */ 1184 de[0].ctime = de[1].ctime = time; 1185 de[0].ctime_cs = de[1].ctime_cs = time_cs; 1186 de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = date; 1187 } else { 1188 de[0].ctime = de[1].ctime = 0; 1189 de[0].ctime_cs = de[1].ctime_cs = 0; 1190 de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = 0; 1191 } 1192 fat_set_start(&de[0], cluster); 1193 fat_set_start(&de[1], MSDOS_I(dir)->i_logstart); 1194 de[0].size = de[1].size = 0; 1195 memset(de + 2, 0, sb->s_blocksize - 2 * sizeof(*de)); 1196 set_buffer_uptodate(bhs[0]); 1197 unlock_buffer(bhs[0]); 1198 mark_buffer_dirty_inode(bhs[0], dir); 1199 1200 err = fat_zeroed_cluster(dir, blknr, 1, bhs, MAX_BUF_PER_PAGE); 1201 if (err) 1202 goto error_free; 1203 1204 return cluster; 1205 1206 error_free: 1207 fat_free_clusters(dir, cluster); 1208 error: 1209 return err; 1210 } 1211 EXPORT_SYMBOL_GPL(fat_alloc_new_dir); 1212 1213 static int fat_add_new_entries(struct inode *dir, void *slots, int nr_slots, 1214 int *nr_cluster, struct msdos_dir_entry **de, 1215 struct buffer_head **bh) 1216 { 1217 struct super_block *sb = dir->i_sb; 1218 struct msdos_sb_info *sbi = MSDOS_SB(sb); 1219 struct buffer_head *bhs[MAX_BUF_PER_PAGE]; 1220 sector_t blknr, start_blknr, last_blknr; 1221 unsigned long size, copy; 1222 int err, i, n, offset, cluster[2]; 1223 1224 /* 1225 * The minimum cluster size is 512bytes, and maximum entry 1226 * size is 32*slots (672bytes). So, iff the cluster size is 1227 * 512bytes, we may need two clusters. 1228 */ 1229 size = nr_slots * sizeof(struct msdos_dir_entry); 1230 *nr_cluster = (size + (sbi->cluster_size - 1)) >> sbi->cluster_bits; 1231 BUG_ON(*nr_cluster > 2); 1232 1233 err = fat_alloc_clusters(dir, cluster, *nr_cluster); 1234 if (err) 1235 goto error; 1236 1237 /* 1238 * First stage: Fill the directory entry. NOTE: This cluster 1239 * is not referenced from any inode yet, so updates order is 1240 * not important. 1241 */ 1242 i = n = copy = 0; 1243 do { 1244 start_blknr = blknr = fat_clus_to_blknr(sbi, cluster[i]); 1245 last_blknr = start_blknr + sbi->sec_per_clus; 1246 while (blknr < last_blknr) { 1247 bhs[n] = sb_getblk(sb, blknr); 1248 if (!bhs[n]) { 1249 err = -ENOMEM; 1250 goto error_nomem; 1251 } 1252 1253 /* fill the directory entry */ 1254 copy = min(size, sb->s_blocksize); 1255 /* Avoid race with userspace read via bdev */ 1256 lock_buffer(bhs[n]); 1257 memcpy(bhs[n]->b_data, slots, copy); 1258 set_buffer_uptodate(bhs[n]); 1259 unlock_buffer(bhs[n]); 1260 mark_buffer_dirty_inode(bhs[n], dir); 1261 slots += copy; 1262 size -= copy; 1263 if (!size) 1264 break; 1265 n++; 1266 blknr++; 1267 } 1268 } while (++i < *nr_cluster); 1269 1270 memset(bhs[n]->b_data + copy, 0, sb->s_blocksize - copy); 1271 offset = copy - sizeof(struct msdos_dir_entry); 1272 get_bh(bhs[n]); 1273 *bh = bhs[n]; 1274 *de = (struct msdos_dir_entry *)((*bh)->b_data + offset); 1275 1276 /* Second stage: clear the rest of cluster, and write outs */ 1277 err = fat_zeroed_cluster(dir, start_blknr, ++n, bhs, MAX_BUF_PER_PAGE); 1278 if (err) 1279 goto error_free; 1280 1281 return cluster[0]; 1282 1283 error_free: 1284 brelse(*bh); 1285 *bh = NULL; 1286 n = 0; 1287 error_nomem: 1288 for (i = 0; i < n; i++) 1289 bforget(bhs[i]); 1290 fat_free_clusters(dir, cluster[0]); 1291 error: 1292 return err; 1293 } 1294 1295 int fat_add_entries(struct inode *dir, void *slots, int nr_slots, 1296 struct fat_slot_info *sinfo) 1297 { 1298 struct super_block *sb = dir->i_sb; 1299 struct msdos_sb_info *sbi = MSDOS_SB(sb); 1300 struct buffer_head *bh, *prev, *bhs[3]; /* 32*slots (672bytes) */ 1301 struct msdos_dir_entry *de; 1302 int err, free_slots, i, nr_bhs; 1303 loff_t pos; 1304 1305 sinfo->nr_slots = nr_slots; 1306 1307 /* First stage: search free directory entries */ 1308 free_slots = nr_bhs = 0; 1309 bh = prev = NULL; 1310 pos = 0; 1311 err = -ENOSPC; 1312 while (fat_get_entry(dir, &pos, &bh, &de) > -1) { 1313 /* check the maximum size of directory */ 1314 if (pos >= FAT_MAX_DIR_SIZE) 1315 goto error; 1316 1317 if (IS_FREE(de->name)) { 1318 if (prev != bh) { 1319 get_bh(bh); 1320 bhs[nr_bhs] = prev = bh; 1321 nr_bhs++; 1322 } 1323 free_slots++; 1324 if (free_slots == nr_slots) 1325 goto found; 1326 } else { 1327 for (i = 0; i < nr_bhs; i++) 1328 brelse(bhs[i]); 1329 prev = NULL; 1330 free_slots = nr_bhs = 0; 1331 } 1332 } 1333 if (dir->i_ino == MSDOS_ROOT_INO) { 1334 if (!is_fat32(sbi)) 1335 goto error; 1336 } else if (MSDOS_I(dir)->i_start == 0) { 1337 fat_msg(sb, KERN_ERR, "Corrupted directory (i_pos %lld)", 1338 MSDOS_I(dir)->i_pos); 1339 err = -EIO; 1340 goto error; 1341 } 1342 1343 found: 1344 err = 0; 1345 pos -= free_slots * sizeof(*de); 1346 nr_slots -= free_slots; 1347 if (free_slots) { 1348 /* 1349 * Second stage: filling the free entries with new entries. 1350 * NOTE: If this slots has shortname, first, we write 1351 * the long name slots, then write the short name. 1352 */ 1353 int size = free_slots * sizeof(*de); 1354 int offset = pos & (sb->s_blocksize - 1); 1355 int long_bhs = nr_bhs - (nr_slots == 0); 1356 1357 /* Fill the long name slots. */ 1358 for (i = 0; i < long_bhs; i++) { 1359 int copy = umin(sb->s_blocksize - offset, size); 1360 memcpy(bhs[i]->b_data + offset, slots, copy); 1361 mark_buffer_dirty_inode(bhs[i], dir); 1362 offset = 0; 1363 slots += copy; 1364 size -= copy; 1365 } 1366 if (long_bhs && IS_DIRSYNC(dir)) 1367 err = fat_sync_bhs(bhs, long_bhs); 1368 if (!err && i < nr_bhs) { 1369 /* Fill the short name slot. */ 1370 int copy = umin(sb->s_blocksize - offset, size); 1371 memcpy(bhs[i]->b_data + offset, slots, copy); 1372 mark_buffer_dirty_inode(bhs[i], dir); 1373 if (IS_DIRSYNC(dir)) 1374 err = sync_dirty_buffer(bhs[i]); 1375 } 1376 for (i = 0; i < nr_bhs; i++) 1377 brelse(bhs[i]); 1378 if (err) 1379 goto error_remove; 1380 } 1381 1382 if (nr_slots) { 1383 int cluster, nr_cluster; 1384 1385 /* 1386 * Third stage: allocate the cluster for new entries. 1387 * And initialize the cluster with new entries, then 1388 * add the cluster to dir. 1389 */ 1390 cluster = fat_add_new_entries(dir, slots, nr_slots, &nr_cluster, 1391 &de, &bh); 1392 if (cluster < 0) { 1393 err = cluster; 1394 goto error_remove; 1395 } 1396 err = fat_chain_add(dir, cluster, nr_cluster); 1397 if (err) { 1398 fat_free_clusters(dir, cluster); 1399 goto error_remove; 1400 } 1401 if (dir->i_size & (sbi->cluster_size - 1)) { 1402 fat_fs_error(sb, "Odd directory size"); 1403 dir->i_size = (dir->i_size + sbi->cluster_size - 1) 1404 & ~((loff_t)sbi->cluster_size - 1); 1405 } 1406 dir->i_size += nr_cluster << sbi->cluster_bits; 1407 MSDOS_I(dir)->mmu_private += nr_cluster << sbi->cluster_bits; 1408 } 1409 sinfo->slot_off = pos; 1410 sinfo->de = de; 1411 sinfo->bh = bh; 1412 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de); 1413 1414 return 0; 1415 1416 error: 1417 brelse(bh); 1418 for (i = 0; i < nr_bhs; i++) 1419 brelse(bhs[i]); 1420 return err; 1421 1422 error_remove: 1423 brelse(bh); 1424 if (free_slots) 1425 __fat_remove_entries(dir, pos, free_slots); 1426 return err; 1427 } 1428 EXPORT_SYMBOL_GPL(fat_add_entries); 1429