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