Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 | /* * linux/fs/fat/dir.c * * directory handling functions for fat-based filesystems * * Written 1992,1993 by Werner Almesberger * * Hidden files 1995 by Albert Cahalan <albert@ccs.neu.edu> <adc@coe.neu.edu> * * VFAT extensions by Gordon Chaffee <chaffee@plateau.cs.berkeley.edu> * Merged with msdos fs by Henrik Storner <storner@osiris.ping.dk> * Rewritten for constant inumbers. Plugged buffer overrun in readdir(). AV * Short name translation 1999 by Wolfram Pienkoss <wp@bsz.shk.th.schule.de> */ #define ASC_LINUX_VERSION(V, P, S) (((V) * 65536) + ((P) * 256) + (S)) #include <linux/version.h> #include <linux/fs.h> #include <linux/msdos_fs.h> #include <linux/nls.h> #include <linux/kernel.h> #include <linux/errno.h> #include <linux/stat.h> #include <linux/string.h> #include <linux/ioctl.h> #include <linux/dirent.h> #include <linux/mm.h> #include <linux/ctype.h> #include <asm/uaccess.h> #include "msbuffer.h" #define PRINTK(X) struct file_operations fat_dir_operations = { read: generic_read_dir, readdir: fat_readdir, ioctl: fat_dir_ioctl, fsync: file_fsync, }; /* * Convert Unicode 16 to UTF8, translated Unicode, or ASCII. * If uni_xlate is enabled and we can't get a 1:1 conversion, use a * colon as an escape character since it is normally invalid on the vfat * filesystem. The following four characters are the hexadecimal digits * of Unicode value. This lets us do a full dump and restore of Unicode * filenames. We could get into some trouble with long Unicode names, * but ignore that right now. * Ahem... Stack smashing in ring 0 isn't fun. Fixed. */ static int uni16_to_x8(unsigned char *ascii, unsigned char *uni, int uni_xlate, struct nls_table *nls) { unsigned char *ip, *op, *uni_page, ch, cl, nc; unsigned int ec; int k; ip = uni; op = ascii; while (*ip || ip[1]) { cl = *ip++; ch = *ip++; uni_page = nls->page_uni2charset[ch]; if (uni_page && uni_page[cl]) { *op++ = uni_page[cl]; } else { if (uni_xlate == 1) { *op = ':'; ec = (ch << 8) + cl; for (k = 4; k > 0; k--) { nc = ec & 0xF; op[k] = nc > 9 ? nc + ('a' - 10) : nc + '0'; ec >>= 4; } op += 5; } else { *op++ = '?'; } } /* We have some slack there, so it's OK */ if (op>ascii+256) { op = ascii + 256; break; } } *op = 0; return (op - ascii); } #if 0 static void dump_de(struct msdos_dir_entry *de) { int i; unsigned char *p = (unsigned char *) de; printk("["); for (i = 0; i < 32; i++, p++) { printk("%02x ", *p); } printk("]\n"); } #endif static inline unsigned char fat_tolower(struct nls_table *t, unsigned char c) { unsigned char nc = t->charset2lower[c]; return nc ? nc : c; } static inline struct nls_unicode fat_short2lower_uni(struct nls_table *t, unsigned char c) { unsigned char nc = t->charset2lower[c]; return nc ? t->charset2uni[nc] : t->charset2uni[c]; } static int fat_strnicmp(struct nls_table *t, const unsigned char *s1, const unsigned char *s2, int len) { while(len--) if (fat_tolower(t, *s1++) != fat_tolower(t, *s2++)) return 1; return 0; } /* * Return values: negative -> error, 0 -> not found, positive -> found, * value is the total amount of slots, including the shortname entry. */ int fat_search_long(struct inode *inode, const char *name, int name_len, int anycase, loff_t *spos, loff_t *lpos) { struct super_block *sb = inode->i_sb; struct buffer_head *bh = NULL; struct msdos_dir_entry *de; struct nls_table *nls_io = MSDOS_SB(sb)->nls_io; struct nls_table *nls_disk = MSDOS_SB(sb)->nls_disk; struct nls_unicode bufuname[14]; unsigned char xlate_len, long_slots, *unicode = NULL; char c, bufname[260]; /* 256 + 4 */ int uni_xlate = MSDOS_SB(sb)->options.unicode_xlate; int utf8 = MSDOS_SB(sb)->options.utf8; int ino, i, i2, last, res = 0; loff_t cpos = 0; while(1) { if (fat_get_entry(inode,&cpos,&bh,&de,&ino) == -1) goto EODir; parse_record: long_slots = 0; if (de->name[0] == (__s8) DELETED_FLAG) continue; if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME)) continue; if (de->attr != ATTR_EXT && IS_FREE(de->name)) continue; if (de->attr == ATTR_EXT) { struct msdos_dir_slot *ds; int offset; unsigned char id; unsigned char slot; unsigned char slots; unsigned char sum; unsigned char alias_checksum; if (!unicode) { unicode = (unsigned char *) __get_free_page(GFP_KERNEL); if (!unicode) { fat_brelse(sb, bh); return -ENOMEM; } } parse_long: slots = 0; offset = 0; ds = (struct msdos_dir_slot *) de; id = ds->id; if (!(id & 0x40)) continue; slots = id & ~0x40; if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */ continue; long_slots = slots; alias_checksum = ds->alias_checksum; slot = slots; while (1) { slot--; offset = slot * 26; memcpy(&unicode[offset], ds->name0_4, 10); memcpy(&unicode[offset+10], ds->name5_10, 12); memcpy(&unicode[offset+22], ds->name11_12, 4); offset += 26; if (ds->id & 0x40) { unicode[offset] = 0; unicode[offset+1] = 0; } if (fat_get_entry(inode,&cpos,&bh,&de,&ino)<0) goto EODir; if (slot == 0) break; ds = (struct msdos_dir_slot *) de; if (ds->attr != ATTR_EXT) goto parse_record; if ((ds->id & ~0x40) != slot) goto parse_long; if (ds->alias_checksum != alias_checksum) goto parse_long; } if (de->name[0] == (__s8) DELETED_FLAG) continue; if (de->attr == ATTR_EXT) goto parse_long; if (IS_FREE(de->name) || (de->attr & ATTR_VOLUME)) continue; for (sum = 0, i = 0; i < 11; i++) sum = (((sum&1)<<7)|((sum&0xfe)>>1)) + de->name[i]; if (sum != alias_checksum) long_slots = 0; } for (i = 0, last = 0; i < 8;) { if (!(c = de->name[i])) break; if (c == 0x05) c = 0xE5; bufuname[i++] = fat_short2lower_uni(nls_disk, c); if (c != ' ') last = i; } i = last; bufuname[i++] = fat_short2lower_uni(nls_disk, '.'); for (i2 = 0; i2 < 3; i2++) { if (!(c = de->ext[i2])) break; bufuname[i++] = fat_short2lower_uni(nls_disk, c); if (c != ' ') last = i; } if (!last) continue; memset(&bufuname[last], 0, sizeof(struct nls_unicode)); xlate_len = utf8 ?utf8_wcstombs(bufname, (__u16 *) &bufuname, 260) :uni16_to_x8(bufname, (unsigned char *) &bufuname, uni_xlate, nls_io); if (xlate_len == name_len) if ((!anycase && !memcmp(name, bufname, xlate_len)) || (anycase && !fat_strnicmp(nls_io, name, bufname, xlate_len))) goto Found; if (long_slots) { xlate_len = utf8 ?utf8_wcstombs(bufname, (__u16 *) unicode, 260) :uni16_to_x8(bufname, unicode, uni_xlate, nls_io); if (xlate_len != name_len) continue; if ((!anycase && !memcmp(name, bufname, xlate_len)) || (anycase && !fat_strnicmp(nls_io, name, bufname, xlate_len))) goto Found; } } Found: res = long_slots + 1; *spos = cpos - sizeof(struct msdos_dir_entry); *lpos = cpos - res*sizeof(struct msdos_dir_entry); EODir: fat_brelse(sb, bh); if (unicode) { free_page((unsigned long) unicode); } return res; } static int fat_readdirx(struct inode *inode, struct file *filp, void *dirent, filldir_t filldir, int shortnames, int both) { struct super_block *sb = inode->i_sb; struct buffer_head *bh; struct msdos_dir_entry *de; struct nls_table *nls_io = MSDOS_SB(sb)->nls_io; struct nls_table *nls_disk = MSDOS_SB(sb)->nls_disk; struct nls_unicode bufuname[14], *ptuname = &bufuname[0]; unsigned char long_slots, *unicode = NULL; char c, bufname[56], *ptname = bufname; unsigned long lpos, dummy, *furrfu = &lpos; int uni_xlate = MSDOS_SB(sb)->options.unicode_xlate; int isvfat = MSDOS_SB(sb)->options.isvfat; int utf8 = MSDOS_SB(sb)->options.utf8; int ino,inum,i,i2,last, dotoffset = 0; loff_t cpos; cpos = filp->f_pos; /* Fake . and .. for the root directory. */ if (inode->i_ino == MSDOS_ROOT_INO) { while (cpos < 2) { if (filldir(dirent, "..", cpos+1, cpos, MSDOS_ROOT_INO) < 0) return 0; cpos++; filp->f_pos++; } if (cpos == 2) { dummy = 2; furrfu = &dummy; cpos = 0; } } if (cpos & (sizeof(struct msdos_dir_entry)-1)) return -ENOENT; bh = NULL; GetNew: long_slots = 0; if (fat_get_entry(inode,&cpos,&bh,&de,&ino) == -1) goto EODir; /* Check for long filename entry */ if (isvfat) { if (de->name[0] == (__s8) DELETED_FLAG) goto RecEnd; if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME)) goto RecEnd; if (de->attr != ATTR_EXT && IS_FREE(de->name)) goto RecEnd; } else { if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name)) goto RecEnd; } if (isvfat && de->attr == ATTR_EXT) { struct msdos_dir_slot *ds; int offset; unsigned char id; unsigned char slot; unsigned char slots; unsigned char sum; unsigned char alias_checksum; if (!unicode) { unicode = (unsigned char *) __get_free_page(GFP_KERNEL); if (!unicode) { filp->f_pos = cpos; fat_brelse(sb, bh); return -ENOMEM; } } ParseLong: slots = 0; offset = 0; ds = (struct msdos_dir_slot *) de; id = ds->id; if (!(id & 0x40)) goto RecEnd; slots = id & ~0x40; if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */ goto RecEnd; long_slots = slots; alias_checksum = ds->alias_checksum; slot = slots; while (1) { slot--; offset = slot * 26; memcpy(&unicode[offset], ds->name0_4, 10); memcpy(&unicode[offset+10], ds->name5_10, 12); memcpy(&unicode[offset+22], ds->name11_12, 4); offset += 26; if (ds->id & 0x40) { unicode[offset] = 0; unicode[offset+1] = 0; } if (fat_get_entry(inode,&cpos,&bh,&de,&ino) == -1) goto EODir; if (slot == 0) break; ds = (struct msdos_dir_slot *) de; if (ds->attr != ATTR_EXT) goto RecEnd; /* XXX */ if ((ds->id & ~0x40) != slot) goto ParseLong; if (ds->alias_checksum != alias_checksum) goto ParseLong; } if (de->name[0] == (__s8) DELETED_FLAG) goto RecEnd; if (de->attr == ATTR_EXT) goto ParseLong; if (IS_FREE(de->name) || (de->attr & ATTR_VOLUME)) goto RecEnd; for (sum = 0, i = 0; i < 11; i++) sum = (((sum&1)<<7)|((sum&0xfe)>>1)) + de->name[i]; if (sum != alias_checksum) long_slots = 0; } if ((de->attr & ATTR_HIDDEN) && MSDOS_SB(sb)->options.dotsOK) { *ptuname = fat_short2lower_uni(nls_disk, '.'); *ptname++ = '.'; dotoffset = 1; } for (i = 0, last = 0; i < 8;) { if (!(c = de->name[i])) break; /* see namei.c, msdos_format_name */ if (c == 0x05) c = 0xE5; ptuname[i] = fat_short2lower_uni(nls_disk, c); ptname[i++] = (c>='A' && c<='Z') ? c+32 : c; if (c != ' ') last = i; } i = last; ptuname[i] = fat_short2lower_uni(nls_disk, '.'); ptname[i++] = '.'; for (i2 = 0; i2 < 3; i2++) { if (!(c = de->ext[i2])) break; ptuname[i] = fat_short2lower_uni(nls_disk, c); ptname[i++] = (c>='A' && c<='Z') ? c+32 : c; if (c != ' ') last = i; } if (!last) goto RecEnd; i = last + dotoffset; lpos = cpos - (long_slots+1)*sizeof(struct msdos_dir_entry); if (!memcmp(de->name,MSDOS_DOT,11)) inum = inode->i_ino; else if (!memcmp(de->name,MSDOS_DOTDOT,11)) { /* inum = fat_parent_ino(inode,0); */ inum = filp->f_dentry->d_parent->d_inode->i_ino; } else { struct inode *tmp = fat_iget(sb, ino); if (tmp) { inum = tmp->i_ino; iput(tmp); } else inum = iunique(sb, MSDOS_ROOT_INO); } if (isvfat) { memset(&bufuname[i], 0, sizeof(struct nls_unicode)); i = utf8 ? utf8_wcstombs(bufname, (__u16 *) &bufuname, 56) : uni16_to_x8(bufname, (unsigned char *) &bufuname, uni_xlate, nls_io); } if (!long_slots||shortnames) { if (both) bufname[i] = '\0'; if (filldir(dirent, bufname, i, *furrfu, inum) < 0) goto FillFailed; } else { char longname[275]; unsigned char long_len = utf8 ? utf8_wcstombs(longname, (__u16 *) unicode, 275) : uni16_to_x8(longname, unicode, uni_xlate, nls_io); if (both) { memcpy(&longname[long_len+1], bufname, i); long_len += i; } if (filldir(dirent, longname, long_len, *furrfu, inum) < 0) goto FillFailed; } RecEnd: furrfu = &lpos; filp->f_pos = cpos; goto GetNew; EODir: filp->f_pos = cpos; FillFailed: if (bh) fat_brelse(sb, bh); if (unicode) { free_page((unsigned long) unicode); } return 0; } int fat_readdir(struct file *filp, void *dirent, filldir_t filldir) { struct inode *inode = filp->f_dentry->d_inode; return fat_readdirx(inode, filp, dirent, filldir, 0, 0); } static int vfat_ioctl_fill( void * buf, const char * name, int name_len, off_t offset, ino_t ino) { struct dirent *d1 = (struct dirent *)buf; struct dirent *d2 = d1 + 1; int len, slen; int dotdir; get_user(len, &d1->d_reclen); if (len != 0) { return -1; } if ((name_len == 1 && name[0] == '.') || (name_len == 2 && name[0] == '.' && name[1] == '.')) { dotdir = 1; len = name_len; } else { dotdir = 0; len = strlen(name); } if (len != name_len) { copy_to_user(d2->d_name, name, len); put_user(0, d2->d_name + len); put_user(len, &d2->d_reclen); put_user(ino, &d2->d_ino); put_user(offset, &d2->d_off); slen = name_len - len; copy_to_user(d1->d_name, name+len+1, slen); put_user(0, d1->d_name+slen); put_user(slen, &d1->d_reclen); } else { put_user(0, d2->d_name); put_user(0, &d2->d_reclen); copy_to_user(d1->d_name, name, len); put_user(0, d1->d_name+len); put_user(len, &d1->d_reclen); } PRINTK(("FAT d1=%p d2=%p len=%d, name_len=%d\n", d1, d2, len, name_len)); return 0; } int fat_dir_ioctl(struct inode * inode, struct file * filp, unsigned int cmd, unsigned long arg) { int err; /* * We want to provide an interface for Samba to be able * to get the short filename for a given long filename. * Samba should use this ioctl instead of readdir() to * get the information it needs. */ switch (cmd) { case VFAT_IOCTL_READDIR_BOTH: { struct dirent *d1 = (struct dirent *)arg; err = verify_area(VERIFY_WRITE, d1, sizeof(struct dirent[2])); if (err) return err; put_user(0, &d1->d_reclen); return fat_readdirx(inode,filp,(void *)arg, vfat_ioctl_fill, 0, 1); } case VFAT_IOCTL_READDIR_SHORT: { struct dirent *d1 = (struct dirent *)arg; put_user(0, &d1->d_reclen); err = verify_area(VERIFY_WRITE, d1, sizeof(struct dirent[2])); if (err) return err; return fat_readdirx(inode,filp,(void *)arg, vfat_ioctl_fill, 1, 1); } default: /* forward ioctl to CVF extension */ if (MSDOS_SB(inode->i_sb)->cvf_format && MSDOS_SB(inode->i_sb)->cvf_format->cvf_dir_ioctl) return MSDOS_SB(inode->i_sb)->cvf_format ->cvf_dir_ioctl(inode,filp,cmd,arg); return -EINVAL; } return 0; } /***** See if directory is empty */ int fat_dir_empty(struct inode *dir) { loff_t pos; struct buffer_head *bh; struct msdos_dir_entry *de; int ino,result = 0; pos = 0; bh = NULL; while (fat_get_entry(dir,&pos,&bh,&de,&ino) > -1) { /* Ignore vfat longname entries */ if (de->attr == ATTR_EXT) continue; if (!IS_FREE(de->name) && strncmp(de->name,MSDOS_DOT , MSDOS_NAME) && strncmp(de->name,MSDOS_DOTDOT, MSDOS_NAME)) { result = -ENOTEMPTY; break; } } if (bh) fat_brelse(dir->i_sb, bh); return result; } /* This assumes that size of cluster is above the 32*slots */ int fat_add_entries(struct inode *dir,int slots, struct buffer_head **bh, struct msdos_dir_entry **de, int *ino) { struct super_block *sb = dir->i_sb; loff_t offset, curr; int row; struct buffer_head *new_bh; offset = curr = 0; *bh = NULL; row = 0; while (fat_get_entry(dir,&curr,bh,de,ino) > -1) { if (IS_FREE((*de)->name)) { if (++row == slots) return offset; } else { row = 0; offset = curr; } } if ((dir->i_ino == MSDOS_ROOT_INO) && (MSDOS_SB(sb)->fat_bits != 32)) return -ENOSPC; new_bh = fat_extend_dir(dir); if (!new_bh) return -ENOSPC; fat_brelse(sb, new_bh); do fat_get_entry(dir,&curr,bh,de,ino); while (++row<slots); return offset; } int fat_new_dir(struct inode *dir, struct inode *parent, int is_vfat) { struct super_block *sb = dir->i_sb; struct buffer_head *bh; struct msdos_dir_entry *de; __u16 date, time; if ((bh = fat_extend_dir(dir)) == NULL) return -ENOSPC; /* zeroed out, so... */ fat_date_unix2dos(dir->i_mtime,&time,&date); de = (struct msdos_dir_entry*)&bh->b_data[0]; memcpy(de[0].name,MSDOS_DOT,MSDOS_NAME); memcpy(de[1].name,MSDOS_DOTDOT,MSDOS_NAME); de[0].attr = de[1].attr = ATTR_DIR; de[0].time = de[1].time = CT_LE_W(time); de[0].date = de[1].date = CT_LE_W(date); if (is_vfat) { /* extra timestamps */ de[0].ctime = de[1].ctime = CT_LE_W(time); de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = CT_LE_W(date); } de[0].start = CT_LE_W(MSDOS_I(dir)->i_logstart); de[0].starthi = CT_LE_W(MSDOS_I(dir)->i_logstart>>16); de[1].start = CT_LE_W(MSDOS_I(parent)->i_logstart); de[1].starthi = CT_LE_W(MSDOS_I(parent)->i_logstart>>16); fat_mark_buffer_dirty(sb, bh, 1); fat_brelse(sb, bh); dir->i_atime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; mark_inode_dirty(dir); return 0; } /* * Overrides for Emacs so that we follow Linus's tabbing style. * Emacs will notice this stuff at the end of the file and automatically * adjust the settings for this buffer only. This must remain at the end * of the file. * --------------------------------------------------------------------------- * Local variables: * c-indent-level: 8 * c-brace-imaginary-offset: 0 * c-brace-offset: -8 * c-argdecl-indent: 8 * c-label-offset: -8 * c-continued-statement-offset: 8 * c-continued-brace-offset: 0 * End: */ |