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