0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #include <linux/slab.h>
0018 #include <linux/compat.h>
0019 #include <linux/uaccess.h>
0020 #include <linux/iversion.h>
0021 #include "fat.h"
0022
0023
0024
0025
0026
0027
0028 #define FAT_MAX_SHORT_SIZE ((MSDOS_NAME + 1) * NLS_MAX_CHARSET_SIZE + 1)
0029
0030
0031
0032
0033 #define FAT_MAX_UNI_CHARS ((MSDOS_SLOTS - 1) * 13 + 1)
0034 #define FAT_MAX_UNI_SIZE (FAT_MAX_UNI_CHARS * sizeof(wchar_t))
0035
0036 static inline unsigned char fat_tolower(unsigned char c)
0037 {
0038 return ((c >= 'A') && (c <= 'Z')) ? c+32 : c;
0039 }
0040
0041 static inline loff_t fat_make_i_pos(struct super_block *sb,
0042 struct buffer_head *bh,
0043 struct msdos_dir_entry *de)
0044 {
0045 return ((loff_t)bh->b_blocknr << MSDOS_SB(sb)->dir_per_block_bits)
0046 | (de - (struct msdos_dir_entry *)bh->b_data);
0047 }
0048
0049 static inline void fat_dir_readahead(struct inode *dir, sector_t iblock,
0050 sector_t phys)
0051 {
0052 struct super_block *sb = dir->i_sb;
0053 struct msdos_sb_info *sbi = MSDOS_SB(sb);
0054 struct buffer_head *bh;
0055 int sec;
0056
0057
0058 if ((iblock & (sbi->sec_per_clus - 1)) || sbi->sec_per_clus == 1)
0059 return;
0060
0061 if (!is_fat32(sbi) && (dir->i_ino == MSDOS_ROOT_INO))
0062 return;
0063
0064 bh = sb_find_get_block(sb, phys);
0065 if (bh == NULL || !buffer_uptodate(bh)) {
0066 for (sec = 0; sec < sbi->sec_per_clus; sec++)
0067 sb_breadahead(sb, phys + sec);
0068 }
0069 brelse(bh);
0070 }
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082 static int fat__get_entry(struct inode *dir, loff_t *pos,
0083 struct buffer_head **bh, struct msdos_dir_entry **de)
0084 {
0085 struct super_block *sb = dir->i_sb;
0086 sector_t phys, iblock;
0087 unsigned long mapped_blocks;
0088 int err, offset;
0089
0090 next:
0091 brelse(*bh);
0092 *bh = NULL;
0093 iblock = *pos >> sb->s_blocksize_bits;
0094 err = fat_bmap(dir, iblock, &phys, &mapped_blocks, 0, false);
0095 if (err || !phys)
0096 return -1;
0097
0098 fat_dir_readahead(dir, iblock, phys);
0099
0100 *bh = sb_bread(sb, phys);
0101 if (*bh == NULL) {
0102 fat_msg_ratelimit(sb, KERN_ERR,
0103 "Directory bread(block %llu) failed", (llu)phys);
0104
0105 *pos = (iblock + 1) << sb->s_blocksize_bits;
0106 goto next;
0107 }
0108
0109 offset = *pos & (sb->s_blocksize - 1);
0110 *pos += sizeof(struct msdos_dir_entry);
0111 *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
0112
0113 return 0;
0114 }
0115
0116 static inline int fat_get_entry(struct inode *dir, loff_t *pos,
0117 struct buffer_head **bh,
0118 struct msdos_dir_entry **de)
0119 {
0120
0121 if (*bh && *de &&
0122 (*de - (struct msdos_dir_entry *)(*bh)->b_data) <
0123 MSDOS_SB(dir->i_sb)->dir_per_block - 1) {
0124 *pos += sizeof(struct msdos_dir_entry);
0125 (*de)++;
0126 return 0;
0127 }
0128 return fat__get_entry(dir, pos, bh, de);
0129 }
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141 static int uni16_to_x8(struct super_block *sb, unsigned char *ascii,
0142 const wchar_t *uni, int len, struct nls_table *nls)
0143 {
0144 int uni_xlate = MSDOS_SB(sb)->options.unicode_xlate;
0145 const wchar_t *ip;
0146 wchar_t ec;
0147 unsigned char *op;
0148 int charlen;
0149
0150 ip = uni;
0151 op = ascii;
0152
0153 while (*ip && ((len - NLS_MAX_CHARSET_SIZE) > 0)) {
0154 ec = *ip++;
0155 charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE);
0156 if (charlen > 0) {
0157 op += charlen;
0158 len -= charlen;
0159 } else {
0160 if (uni_xlate == 1) {
0161 *op++ = ':';
0162 op = hex_byte_pack(op, ec >> 8);
0163 op = hex_byte_pack(op, ec);
0164 len -= 5;
0165 } else {
0166 *op++ = '?';
0167 len--;
0168 }
0169 }
0170 }
0171
0172 if (unlikely(*ip)) {
0173 fat_msg(sb, KERN_WARNING,
0174 "filename was truncated while converting.");
0175 }
0176
0177 *op = 0;
0178 return op - ascii;
0179 }
0180
0181 static inline int fat_uni_to_x8(struct super_block *sb, const wchar_t *uni,
0182 unsigned char *buf, int size)
0183 {
0184 struct msdos_sb_info *sbi = MSDOS_SB(sb);
0185 if (sbi->options.utf8)
0186 return utf16s_to_utf8s(uni, FAT_MAX_UNI_CHARS,
0187 UTF16_HOST_ENDIAN, buf, size);
0188 else
0189 return uni16_to_x8(sb, buf, uni, size, sbi->nls_io);
0190 }
0191
0192 static inline int
0193 fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
0194 {
0195 int charlen;
0196
0197 charlen = t->char2uni(c, clen, uni);
0198 if (charlen < 0) {
0199 *uni = 0x003f;
0200 charlen = 1;
0201 }
0202 return charlen;
0203 }
0204
0205 static inline int
0206 fat_short2lower_uni(struct nls_table *t, unsigned char *c,
0207 int clen, wchar_t *uni)
0208 {
0209 int charlen;
0210 wchar_t wc;
0211
0212 charlen = t->char2uni(c, clen, &wc);
0213 if (charlen < 0) {
0214 *uni = 0x003f;
0215 charlen = 1;
0216 } else if (charlen <= 1) {
0217 unsigned char nc = t->charset2lower[*c];
0218
0219 if (!nc)
0220 nc = *c;
0221
0222 charlen = t->char2uni(&nc, 1, uni);
0223 if (charlen < 0) {
0224 *uni = 0x003f;
0225 charlen = 1;
0226 }
0227 } else
0228 *uni = wc;
0229
0230 return charlen;
0231 }
0232
0233 static inline int
0234 fat_shortname2uni(struct nls_table *nls, unsigned char *buf, int buf_size,
0235 wchar_t *uni_buf, unsigned short opt, int lower)
0236 {
0237 int len = 0;
0238
0239 if (opt & VFAT_SFN_DISPLAY_LOWER)
0240 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
0241 else if (opt & VFAT_SFN_DISPLAY_WIN95)
0242 len = fat_short2uni(nls, buf, buf_size, uni_buf);
0243 else if (opt & VFAT_SFN_DISPLAY_WINNT) {
0244 if (lower)
0245 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
0246 else
0247 len = fat_short2uni(nls, buf, buf_size, uni_buf);
0248 } else
0249 len = fat_short2uni(nls, buf, buf_size, uni_buf);
0250
0251 return len;
0252 }
0253
0254 static inline int fat_name_match(struct msdos_sb_info *sbi,
0255 const unsigned char *a, int a_len,
0256 const unsigned char *b, int b_len)
0257 {
0258 if (a_len != b_len)
0259 return 0;
0260
0261 if (sbi->options.name_check != 's')
0262 return !nls_strnicmp(sbi->nls_io, a, b, a_len);
0263 else
0264 return !memcmp(a, b, a_len);
0265 }
0266
0267 enum { PARSE_INVALID = 1, PARSE_NOT_LONGNAME, PARSE_EOF, };
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279 static int fat_parse_long(struct inode *dir, loff_t *pos,
0280 struct buffer_head **bh, struct msdos_dir_entry **de,
0281 wchar_t **unicode, unsigned char *nr_slots)
0282 {
0283 struct msdos_dir_slot *ds;
0284 unsigned char id, slot, slots, alias_checksum;
0285
0286 if (!*unicode) {
0287 *unicode = __getname();
0288 if (!*unicode) {
0289 brelse(*bh);
0290 return -ENOMEM;
0291 }
0292 }
0293 parse_long:
0294 ds = (struct msdos_dir_slot *)*de;
0295 id = ds->id;
0296 if (!(id & 0x40))
0297 return PARSE_INVALID;
0298 slots = id & ~0x40;
0299 if (slots > 20 || !slots)
0300 return PARSE_INVALID;
0301 *nr_slots = slots;
0302 alias_checksum = ds->alias_checksum;
0303
0304 slot = slots;
0305 while (1) {
0306 int offset;
0307
0308 slot--;
0309 offset = slot * 13;
0310 fat16_towchar(*unicode + offset, ds->name0_4, 5);
0311 fat16_towchar(*unicode + offset + 5, ds->name5_10, 6);
0312 fat16_towchar(*unicode + offset + 11, ds->name11_12, 2);
0313
0314 if (ds->id & 0x40)
0315 (*unicode)[offset + 13] = 0;
0316 if (fat_get_entry(dir, pos, bh, de) < 0)
0317 return PARSE_EOF;
0318 if (slot == 0)
0319 break;
0320 ds = (struct msdos_dir_slot *)*de;
0321 if (ds->attr != ATTR_EXT)
0322 return PARSE_NOT_LONGNAME;
0323 if ((ds->id & ~0x40) != slot)
0324 goto parse_long;
0325 if (ds->alias_checksum != alias_checksum)
0326 goto parse_long;
0327 }
0328 if ((*de)->name[0] == DELETED_FLAG)
0329 return PARSE_INVALID;
0330 if ((*de)->attr == ATTR_EXT)
0331 goto parse_long;
0332 if (IS_FREE((*de)->name) || ((*de)->attr & ATTR_VOLUME))
0333 return PARSE_INVALID;
0334 if (fat_checksum((*de)->name) != alias_checksum)
0335 *nr_slots = 0;
0336
0337 return 0;
0338 }
0339
0340
0341
0342
0343
0344
0345
0346
0347
0348
0349 static int fat_parse_short(struct super_block *sb,
0350 const struct msdos_dir_entry *de,
0351 unsigned char *name, int dot_hidden)
0352 {
0353 const struct msdos_sb_info *sbi = MSDOS_SB(sb);
0354 int isvfat = sbi->options.isvfat;
0355 int nocase = sbi->options.nocase;
0356 unsigned short opt_shortname = sbi->options.shortname;
0357 struct nls_table *nls_disk = sbi->nls_disk;
0358 wchar_t uni_name[14];
0359 unsigned char c, work[MSDOS_NAME];
0360 unsigned char *ptname = name;
0361 int chi, chl, i, j, k;
0362 int dotoffset = 0;
0363 int name_len = 0, uni_len = 0;
0364
0365 if (!isvfat && dot_hidden && (de->attr & ATTR_HIDDEN)) {
0366 *ptname++ = '.';
0367 dotoffset = 1;
0368 }
0369
0370 memcpy(work, de->name, sizeof(work));
0371
0372
0373
0374 if (work[0] == 0x05)
0375 work[0] = 0xE5;
0376
0377
0378 for (i = 0, j = 0; i < 8;) {
0379 c = work[i];
0380 if (!c)
0381 break;
0382 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
0383 &uni_name[j++], opt_shortname,
0384 de->lcase & CASE_LOWER_BASE);
0385 if (chl <= 1) {
0386 if (!isvfat)
0387 ptname[i] = nocase ? c : fat_tolower(c);
0388 i++;
0389 if (c != ' ') {
0390 name_len = i;
0391 uni_len = j;
0392 }
0393 } else {
0394 uni_len = j;
0395 if (isvfat)
0396 i += min(chl, 8-i);
0397 else {
0398 for (chi = 0; chi < chl && i < 8; chi++, i++)
0399 ptname[i] = work[i];
0400 }
0401 if (chl)
0402 name_len = i;
0403 }
0404 }
0405
0406 i = name_len;
0407 j = uni_len;
0408 fat_short2uni(nls_disk, ".", 1, &uni_name[j++]);
0409 if (!isvfat)
0410 ptname[i] = '.';
0411 i++;
0412
0413
0414 for (k = 8; k < MSDOS_NAME;) {
0415 c = work[k];
0416 if (!c)
0417 break;
0418 chl = fat_shortname2uni(nls_disk, &work[k], MSDOS_NAME - k,
0419 &uni_name[j++], opt_shortname,
0420 de->lcase & CASE_LOWER_EXT);
0421 if (chl <= 1) {
0422 k++;
0423 if (!isvfat)
0424 ptname[i] = nocase ? c : fat_tolower(c);
0425 i++;
0426 if (c != ' ') {
0427 name_len = i;
0428 uni_len = j;
0429 }
0430 } else {
0431 uni_len = j;
0432 if (isvfat) {
0433 int offset = min(chl, MSDOS_NAME-k);
0434 k += offset;
0435 i += offset;
0436 } else {
0437 for (chi = 0; chi < chl && k < MSDOS_NAME;
0438 chi++, i++, k++) {
0439 ptname[i] = work[k];
0440 }
0441 }
0442 if (chl)
0443 name_len = i;
0444 }
0445 }
0446
0447 if (name_len > 0) {
0448 name_len += dotoffset;
0449
0450 if (sbi->options.isvfat) {
0451 uni_name[uni_len] = 0x0000;
0452 name_len = fat_uni_to_x8(sb, uni_name, name,
0453 FAT_MAX_SHORT_SIZE);
0454 }
0455 }
0456
0457 return name_len;
0458 }
0459
0460
0461
0462
0463 int fat_search_long(struct inode *inode, const unsigned char *name,
0464 int name_len, struct fat_slot_info *sinfo)
0465 {
0466 struct super_block *sb = inode->i_sb;
0467 struct msdos_sb_info *sbi = MSDOS_SB(sb);
0468 struct buffer_head *bh = NULL;
0469 struct msdos_dir_entry *de;
0470 unsigned char nr_slots;
0471 wchar_t *unicode = NULL;
0472 unsigned char bufname[FAT_MAX_SHORT_SIZE];
0473 loff_t cpos = 0;
0474 int err, len;
0475
0476 err = -ENOENT;
0477 while (1) {
0478 if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
0479 goto end_of_dir;
0480 parse_record:
0481 nr_slots = 0;
0482 if (de->name[0] == DELETED_FLAG)
0483 continue;
0484 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
0485 continue;
0486 if (de->attr != ATTR_EXT && IS_FREE(de->name))
0487 continue;
0488 if (de->attr == ATTR_EXT) {
0489 int status = fat_parse_long(inode, &cpos, &bh, &de,
0490 &unicode, &nr_slots);
0491 if (status < 0) {
0492 err = status;
0493 goto end_of_dir;
0494 } else if (status == PARSE_INVALID)
0495 continue;
0496 else if (status == PARSE_NOT_LONGNAME)
0497 goto parse_record;
0498 else if (status == PARSE_EOF)
0499 goto end_of_dir;
0500 }
0501
0502
0503
0504
0505
0506
0507 len = fat_parse_short(sb, de, bufname, 0);
0508 if (len == 0)
0509 continue;
0510
0511
0512 if (fat_name_match(sbi, name, name_len, bufname, len))
0513 goto found;
0514
0515 if (nr_slots) {
0516 void *longname = unicode + FAT_MAX_UNI_CHARS;
0517 int size = PATH_MAX - FAT_MAX_UNI_SIZE;
0518
0519
0520 len = fat_uni_to_x8(sb, unicode, longname, size);
0521 if (fat_name_match(sbi, name, name_len, longname, len))
0522 goto found;
0523 }
0524 }
0525
0526 found:
0527 nr_slots++;
0528 sinfo->slot_off = cpos - nr_slots * sizeof(*de);
0529 sinfo->nr_slots = nr_slots;
0530 sinfo->de = de;
0531 sinfo->bh = bh;
0532 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
0533 err = 0;
0534 end_of_dir:
0535 if (unicode)
0536 __putname(unicode);
0537
0538 return err;
0539 }
0540 EXPORT_SYMBOL_GPL(fat_search_long);
0541
0542 struct fat_ioctl_filldir_callback {
0543 struct dir_context ctx;
0544 void __user *dirent;
0545 int result;
0546
0547 const char *longname;
0548 int long_len;
0549 const char *shortname;
0550 int short_len;
0551 };
0552
0553 static int __fat_readdir(struct inode *inode, struct file *file,
0554 struct dir_context *ctx, int short_only,
0555 struct fat_ioctl_filldir_callback *both)
0556 {
0557 struct super_block *sb = inode->i_sb;
0558 struct msdos_sb_info *sbi = MSDOS_SB(sb);
0559 struct buffer_head *bh;
0560 struct msdos_dir_entry *de;
0561 unsigned char nr_slots;
0562 wchar_t *unicode = NULL;
0563 unsigned char bufname[FAT_MAX_SHORT_SIZE];
0564 int isvfat = sbi->options.isvfat;
0565 const char *fill_name = NULL;
0566 int fake_offset = 0;
0567 loff_t cpos;
0568 int short_len = 0, fill_len = 0;
0569 int ret = 0;
0570
0571 mutex_lock(&sbi->s_lock);
0572
0573 cpos = ctx->pos;
0574
0575 if (inode->i_ino == MSDOS_ROOT_INO) {
0576 if (!dir_emit_dots(file, ctx))
0577 goto out;
0578 if (ctx->pos == 2) {
0579 fake_offset = 1;
0580 cpos = 0;
0581 }
0582 }
0583 if (cpos & (sizeof(struct msdos_dir_entry) - 1)) {
0584 ret = -ENOENT;
0585 goto out;
0586 }
0587
0588 bh = NULL;
0589 get_new:
0590 if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
0591 goto end_of_dir;
0592 parse_record:
0593 nr_slots = 0;
0594
0595
0596
0597
0598 if (isvfat && !short_only) {
0599 if (de->name[0] == DELETED_FLAG)
0600 goto record_end;
0601 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
0602 goto record_end;
0603 if (de->attr != ATTR_EXT && IS_FREE(de->name))
0604 goto record_end;
0605 } else {
0606 if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
0607 goto record_end;
0608 }
0609
0610 if (isvfat && de->attr == ATTR_EXT) {
0611 int status = fat_parse_long(inode, &cpos, &bh, &de,
0612 &unicode, &nr_slots);
0613 if (status < 0) {
0614 bh = NULL;
0615 ret = status;
0616 goto end_of_dir;
0617 } else if (status == PARSE_INVALID)
0618 goto record_end;
0619 else if (status == PARSE_NOT_LONGNAME)
0620 goto parse_record;
0621 else if (status == PARSE_EOF)
0622 goto end_of_dir;
0623
0624 if (nr_slots) {
0625 void *longname = unicode + FAT_MAX_UNI_CHARS;
0626 int size = PATH_MAX - FAT_MAX_UNI_SIZE;
0627 int len = fat_uni_to_x8(sb, unicode, longname, size);
0628
0629 fill_name = longname;
0630 fill_len = len;
0631
0632 if (!both)
0633 goto start_filldir;
0634
0635 short_len = fat_parse_short(sb, de, bufname,
0636 sbi->options.dotsOK);
0637 if (short_len == 0)
0638 goto record_end;
0639
0640 both->longname = fill_name;
0641 both->long_len = fill_len;
0642 both->shortname = bufname;
0643 both->short_len = short_len;
0644 fill_name = NULL;
0645 fill_len = 0;
0646 goto start_filldir;
0647 }
0648 }
0649
0650 short_len = fat_parse_short(sb, de, bufname, sbi->options.dotsOK);
0651 if (short_len == 0)
0652 goto record_end;
0653
0654 fill_name = bufname;
0655 fill_len = short_len;
0656
0657 start_filldir:
0658 ctx->pos = cpos - (nr_slots + 1) * sizeof(struct msdos_dir_entry);
0659 if (fake_offset && ctx->pos < 2)
0660 ctx->pos = 2;
0661
0662 if (!memcmp(de->name, MSDOS_DOT, MSDOS_NAME)) {
0663 if (!dir_emit_dot(file, ctx))
0664 goto fill_failed;
0665 } else if (!memcmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
0666 if (!dir_emit_dotdot(file, ctx))
0667 goto fill_failed;
0668 } else {
0669 unsigned long inum;
0670 loff_t i_pos = fat_make_i_pos(sb, bh, de);
0671 struct inode *tmp = fat_iget(sb, i_pos);
0672 if (tmp) {
0673 inum = tmp->i_ino;
0674 iput(tmp);
0675 } else
0676 inum = iunique(sb, MSDOS_ROOT_INO);
0677 if (!dir_emit(ctx, fill_name, fill_len, inum,
0678 (de->attr & ATTR_DIR) ? DT_DIR : DT_REG))
0679 goto fill_failed;
0680 }
0681
0682 record_end:
0683 fake_offset = 0;
0684 ctx->pos = cpos;
0685 goto get_new;
0686
0687 end_of_dir:
0688 if (fake_offset && cpos < 2)
0689 ctx->pos = 2;
0690 else
0691 ctx->pos = cpos;
0692 fill_failed:
0693 brelse(bh);
0694 if (unicode)
0695 __putname(unicode);
0696 out:
0697 mutex_unlock(&sbi->s_lock);
0698
0699 return ret;
0700 }
0701
0702 static int fat_readdir(struct file *file, struct dir_context *ctx)
0703 {
0704 return __fat_readdir(file_inode(file), file, ctx, 0, NULL);
0705 }
0706
0707 #define FAT_IOCTL_FILLDIR_FUNC(func, dirent_type) \
0708 static int func(struct dir_context *ctx, const char *name, int name_len, \
0709 loff_t offset, u64 ino, unsigned int d_type) \
0710 { \
0711 struct fat_ioctl_filldir_callback *buf = \
0712 container_of(ctx, struct fat_ioctl_filldir_callback, ctx); \
0713 struct dirent_type __user *d1 = buf->dirent; \
0714 struct dirent_type __user *d2 = d1 + 1; \
0715 \
0716 if (buf->result) \
0717 return -EINVAL; \
0718 buf->result++; \
0719 \
0720 if (name != NULL) { \
0721 \
0722 if (name_len >= sizeof(d1->d_name)) \
0723 name_len = sizeof(d1->d_name) - 1; \
0724 \
0725 if (put_user(0, &d2->d_name[0]) || \
0726 put_user(0, &d2->d_reclen) || \
0727 copy_to_user(d1->d_name, name, name_len) || \
0728 put_user(0, d1->d_name + name_len) || \
0729 put_user(name_len, &d1->d_reclen)) \
0730 goto efault; \
0731 } else { \
0732 \
0733 const char *longname = buf->longname; \
0734 int long_len = buf->long_len; \
0735 const char *shortname = buf->shortname; \
0736 int short_len = buf->short_len; \
0737 \
0738 if (long_len >= sizeof(d1->d_name)) \
0739 long_len = sizeof(d1->d_name) - 1; \
0740 if (short_len >= sizeof(d1->d_name)) \
0741 short_len = sizeof(d1->d_name) - 1; \
0742 \
0743 if (copy_to_user(d2->d_name, longname, long_len) || \
0744 put_user(0, d2->d_name + long_len) || \
0745 put_user(long_len, &d2->d_reclen) || \
0746 put_user(ino, &d2->d_ino) || \
0747 put_user(offset, &d2->d_off) || \
0748 copy_to_user(d1->d_name, shortname, short_len) || \
0749 put_user(0, d1->d_name + short_len) || \
0750 put_user(short_len, &d1->d_reclen)) \
0751 goto efault; \
0752 } \
0753 return 0; \
0754 efault: \
0755 buf->result = -EFAULT; \
0756 return -EFAULT; \
0757 }
0758
0759 FAT_IOCTL_FILLDIR_FUNC(fat_ioctl_filldir, __fat_dirent)
0760
0761 static int fat_ioctl_readdir(struct inode *inode, struct file *file,
0762 void __user *dirent, filldir_t filldir,
0763 int short_only, int both)
0764 {
0765 struct fat_ioctl_filldir_callback buf = {
0766 .ctx.actor = filldir,
0767 .dirent = dirent
0768 };
0769 int ret;
0770
0771 buf.dirent = dirent;
0772 buf.result = 0;
0773 inode_lock_shared(inode);
0774 buf.ctx.pos = file->f_pos;
0775 ret = -ENOENT;
0776 if (!IS_DEADDIR(inode)) {
0777 ret = __fat_readdir(inode, file, &buf.ctx,
0778 short_only, both ? &buf : NULL);
0779 file->f_pos = buf.ctx.pos;
0780 }
0781 inode_unlock_shared(inode);
0782 if (ret >= 0)
0783 ret = buf.result;
0784 return ret;
0785 }
0786
0787 static long fat_dir_ioctl(struct file *filp, unsigned int cmd,
0788 unsigned long arg)
0789 {
0790 struct inode *inode = file_inode(filp);
0791 struct __fat_dirent __user *d1 = (struct __fat_dirent __user *)arg;
0792 int short_only, both;
0793
0794 switch (cmd) {
0795 case VFAT_IOCTL_READDIR_SHORT:
0796 short_only = 1;
0797 both = 0;
0798 break;
0799 case VFAT_IOCTL_READDIR_BOTH:
0800 short_only = 0;
0801 both = 1;
0802 break;
0803 default:
0804 return fat_generic_ioctl(filp, cmd, arg);
0805 }
0806
0807
0808
0809
0810
0811
0812 if (put_user(0, &d1->d_reclen))
0813 return -EFAULT;
0814
0815 return fat_ioctl_readdir(inode, filp, d1, fat_ioctl_filldir,
0816 short_only, both);
0817 }
0818
0819 #ifdef CONFIG_COMPAT
0820 #define VFAT_IOCTL_READDIR_BOTH32 _IOR('r', 1, struct compat_dirent[2])
0821 #define VFAT_IOCTL_READDIR_SHORT32 _IOR('r', 2, struct compat_dirent[2])
0822
0823 FAT_IOCTL_FILLDIR_FUNC(fat_compat_ioctl_filldir, compat_dirent)
0824
0825 static long fat_compat_dir_ioctl(struct file *filp, unsigned cmd,
0826 unsigned long arg)
0827 {
0828 struct inode *inode = file_inode(filp);
0829 struct compat_dirent __user *d1 = compat_ptr(arg);
0830 int short_only, both;
0831
0832 switch (cmd) {
0833 case VFAT_IOCTL_READDIR_SHORT32:
0834 short_only = 1;
0835 both = 0;
0836 break;
0837 case VFAT_IOCTL_READDIR_BOTH32:
0838 short_only = 0;
0839 both = 1;
0840 break;
0841 default:
0842 return fat_generic_ioctl(filp, cmd, (unsigned long)arg);
0843 }
0844
0845
0846
0847
0848
0849
0850 if (put_user(0, &d1->d_reclen))
0851 return -EFAULT;
0852
0853 return fat_ioctl_readdir(inode, filp, d1, fat_compat_ioctl_filldir,
0854 short_only, both);
0855 }
0856 #endif
0857
0858 const struct file_operations fat_dir_operations = {
0859 .llseek = generic_file_llseek,
0860 .read = generic_read_dir,
0861 .iterate_shared = fat_readdir,
0862 .unlocked_ioctl = fat_dir_ioctl,
0863 #ifdef CONFIG_COMPAT
0864 .compat_ioctl = fat_compat_dir_ioctl,
0865 #endif
0866 .fsync = fat_file_fsync,
0867 };
0868
0869 static int fat_get_short_entry(struct inode *dir, loff_t *pos,
0870 struct buffer_head **bh,
0871 struct msdos_dir_entry **de)
0872 {
0873 while (fat_get_entry(dir, pos, bh, de) >= 0) {
0874
0875 if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME))
0876 return 0;
0877 }
0878 return -ENOENT;
0879 }
0880
0881
0882
0883
0884
0885
0886
0887
0888
0889
0890 int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh,
0891 struct msdos_dir_entry **de)
0892 {
0893 loff_t offset = 0;
0894
0895 *de = NULL;
0896 while (fat_get_short_entry(dir, &offset, bh, de) >= 0) {
0897 if (!strncmp((*de)->name, MSDOS_DOTDOT, MSDOS_NAME))
0898 return 0;
0899 }
0900 return -ENOENT;
0901 }
0902 EXPORT_SYMBOL_GPL(fat_get_dotdot_entry);
0903
0904
0905 int fat_dir_empty(struct inode *dir)
0906 {
0907 struct buffer_head *bh;
0908 struct msdos_dir_entry *de;
0909 loff_t cpos;
0910 int result = 0;
0911
0912 bh = NULL;
0913 cpos = 0;
0914 while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
0915 if (strncmp(de->name, MSDOS_DOT , MSDOS_NAME) &&
0916 strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
0917 result = -ENOTEMPTY;
0918 break;
0919 }
0920 }
0921 brelse(bh);
0922 return result;
0923 }
0924 EXPORT_SYMBOL_GPL(fat_dir_empty);
0925
0926
0927
0928
0929
0930 int fat_subdirs(struct inode *dir)
0931 {
0932 struct buffer_head *bh;
0933 struct msdos_dir_entry *de;
0934 loff_t cpos;
0935 int count = 0;
0936
0937 bh = NULL;
0938 cpos = 0;
0939 while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
0940 if (de->attr & ATTR_DIR)
0941 count++;
0942 }
0943 brelse(bh);
0944 return count;
0945 }
0946
0947
0948
0949
0950
0951 int fat_scan(struct inode *dir, const unsigned char *name,
0952 struct fat_slot_info *sinfo)
0953 {
0954 struct super_block *sb = dir->i_sb;
0955
0956 sinfo->slot_off = 0;
0957 sinfo->bh = NULL;
0958 while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh,
0959 &sinfo->de) >= 0) {
0960 if (!strncmp(sinfo->de->name, name, MSDOS_NAME)) {
0961 sinfo->slot_off -= sizeof(*sinfo->de);
0962 sinfo->nr_slots = 1;
0963 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
0964 return 0;
0965 }
0966 }
0967 return -ENOENT;
0968 }
0969 EXPORT_SYMBOL_GPL(fat_scan);
0970
0971
0972
0973
0974
0975 int fat_scan_logstart(struct inode *dir, int i_logstart,
0976 struct fat_slot_info *sinfo)
0977 {
0978 struct super_block *sb = dir->i_sb;
0979
0980 sinfo->slot_off = 0;
0981 sinfo->bh = NULL;
0982 while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh,
0983 &sinfo->de) >= 0) {
0984 if (fat_get_start(MSDOS_SB(sb), sinfo->de) == i_logstart) {
0985 sinfo->slot_off -= sizeof(*sinfo->de);
0986 sinfo->nr_slots = 1;
0987 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
0988 return 0;
0989 }
0990 }
0991 return -ENOENT;
0992 }
0993
0994 static int __fat_remove_entries(struct inode *dir, loff_t pos, int nr_slots)
0995 {
0996 struct super_block *sb = dir->i_sb;
0997 struct buffer_head *bh;
0998 struct msdos_dir_entry *de, *endp;
0999 int err = 0, orig_slots;
1000
1001 while (nr_slots) {
1002 bh = NULL;
1003 if (fat_get_entry(dir, &pos, &bh, &de) < 0) {
1004 err = -EIO;
1005 break;
1006 }
1007
1008 orig_slots = nr_slots;
1009 endp = (struct msdos_dir_entry *)(bh->b_data + sb->s_blocksize);
1010 while (nr_slots && de < endp) {
1011 de->name[0] = DELETED_FLAG;
1012 de++;
1013 nr_slots--;
1014 }
1015 mark_buffer_dirty_inode(bh, dir);
1016 if (IS_DIRSYNC(dir))
1017 err = sync_dirty_buffer(bh);
1018 brelse(bh);
1019 if (err)
1020 break;
1021
1022
1023 pos += ((orig_slots - nr_slots) * sizeof(*de)) - sizeof(*de);
1024 }
1025
1026 return err;
1027 }
1028
1029 int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo)
1030 {
1031 struct super_block *sb = dir->i_sb;
1032 struct msdos_dir_entry *de;
1033 struct buffer_head *bh;
1034 int err = 0, nr_slots;
1035
1036
1037
1038
1039
1040 nr_slots = sinfo->nr_slots;
1041 de = sinfo->de;
1042 sinfo->de = NULL;
1043 bh = sinfo->bh;
1044 sinfo->bh = NULL;
1045 while (nr_slots && de >= (struct msdos_dir_entry *)bh->b_data) {
1046 de->name[0] = DELETED_FLAG;
1047 de--;
1048 nr_slots--;
1049 }
1050 mark_buffer_dirty_inode(bh, dir);
1051 if (IS_DIRSYNC(dir))
1052 err = sync_dirty_buffer(bh);
1053 brelse(bh);
1054 if (err)
1055 return err;
1056 inode_inc_iversion(dir);
1057
1058 if (nr_slots) {
1059
1060
1061
1062
1063
1064 err = __fat_remove_entries(dir, sinfo->slot_off, nr_slots);
1065 if (err) {
1066 fat_msg(sb, KERN_WARNING,
1067 "Couldn't remove the long name slots");
1068 }
1069 }
1070
1071 fat_truncate_time(dir, NULL, S_ATIME|S_MTIME);
1072 if (IS_DIRSYNC(dir))
1073 (void)fat_sync_inode(dir);
1074 else
1075 mark_inode_dirty(dir);
1076
1077 return 0;
1078 }
1079 EXPORT_SYMBOL_GPL(fat_remove_entries);
1080
1081 static int fat_zeroed_cluster(struct inode *dir, sector_t blknr, int nr_used,
1082 struct buffer_head **bhs, int nr_bhs)
1083 {
1084 struct super_block *sb = dir->i_sb;
1085 sector_t last_blknr = blknr + MSDOS_SB(sb)->sec_per_clus;
1086 int err, i, n;
1087
1088
1089 blknr += nr_used;
1090 n = nr_used;
1091 while (blknr < last_blknr) {
1092 bhs[n] = sb_getblk(sb, blknr);
1093 if (!bhs[n]) {
1094 err = -ENOMEM;
1095 goto error;
1096 }
1097
1098 lock_buffer(bhs[n]);
1099 memset(bhs[n]->b_data, 0, sb->s_blocksize);
1100 set_buffer_uptodate(bhs[n]);
1101 unlock_buffer(bhs[n]);
1102 mark_buffer_dirty_inode(bhs[n], dir);
1103
1104 n++;
1105 blknr++;
1106 if (n == nr_bhs) {
1107 if (IS_DIRSYNC(dir)) {
1108 err = fat_sync_bhs(bhs, n);
1109 if (err)
1110 goto error;
1111 }
1112 for (i = 0; i < n; i++)
1113 brelse(bhs[i]);
1114 n = 0;
1115 }
1116 }
1117 if (IS_DIRSYNC(dir)) {
1118 err = fat_sync_bhs(bhs, n);
1119 if (err)
1120 goto error;
1121 }
1122 for (i = 0; i < n; i++)
1123 brelse(bhs[i]);
1124
1125 return 0;
1126
1127 error:
1128 for (i = 0; i < n; i++)
1129 bforget(bhs[i]);
1130 return err;
1131 }
1132
1133 int fat_alloc_new_dir(struct inode *dir, struct timespec64 *ts)
1134 {
1135 struct super_block *sb = dir->i_sb;
1136 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1137 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
1138 struct msdos_dir_entry *de;
1139 sector_t blknr;
1140 __le16 date, time;
1141 u8 time_cs;
1142 int err, cluster;
1143
1144 err = fat_alloc_clusters(dir, &cluster, 1);
1145 if (err)
1146 goto error;
1147
1148 blknr = fat_clus_to_blknr(sbi, cluster);
1149 bhs[0] = sb_getblk(sb, blknr);
1150 if (!bhs[0]) {
1151 err = -ENOMEM;
1152 goto error_free;
1153 }
1154
1155 fat_time_unix2fat(sbi, ts, &time, &date, &time_cs);
1156
1157 de = (struct msdos_dir_entry *)bhs[0]->b_data;
1158
1159 lock_buffer(bhs[0]);
1160
1161 memcpy(de[0].name, MSDOS_DOT, MSDOS_NAME);
1162 memcpy(de[1].name, MSDOS_DOTDOT, MSDOS_NAME);
1163 de->attr = de[1].attr = ATTR_DIR;
1164 de[0].lcase = de[1].lcase = 0;
1165 de[0].time = de[1].time = time;
1166 de[0].date = de[1].date = date;
1167 if (sbi->options.isvfat) {
1168
1169 de[0].ctime = de[1].ctime = time;
1170 de[0].ctime_cs = de[1].ctime_cs = time_cs;
1171 de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = date;
1172 } else {
1173 de[0].ctime = de[1].ctime = 0;
1174 de[0].ctime_cs = de[1].ctime_cs = 0;
1175 de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = 0;
1176 }
1177 fat_set_start(&de[0], cluster);
1178 fat_set_start(&de[1], MSDOS_I(dir)->i_logstart);
1179 de[0].size = de[1].size = 0;
1180 memset(de + 2, 0, sb->s_blocksize - 2 * sizeof(*de));
1181 set_buffer_uptodate(bhs[0]);
1182 unlock_buffer(bhs[0]);
1183 mark_buffer_dirty_inode(bhs[0], dir);
1184
1185 err = fat_zeroed_cluster(dir, blknr, 1, bhs, MAX_BUF_PER_PAGE);
1186 if (err)
1187 goto error_free;
1188
1189 return cluster;
1190
1191 error_free:
1192 fat_free_clusters(dir, cluster);
1193 error:
1194 return err;
1195 }
1196 EXPORT_SYMBOL_GPL(fat_alloc_new_dir);
1197
1198 static int fat_add_new_entries(struct inode *dir, void *slots, int nr_slots,
1199 int *nr_cluster, struct msdos_dir_entry **de,
1200 struct buffer_head **bh, loff_t *i_pos)
1201 {
1202 struct super_block *sb = dir->i_sb;
1203 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1204 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
1205 sector_t blknr, start_blknr, last_blknr;
1206 unsigned long size, copy;
1207 int err, i, n, offset, cluster[2];
1208
1209
1210
1211
1212
1213
1214 size = nr_slots * sizeof(struct msdos_dir_entry);
1215 *nr_cluster = (size + (sbi->cluster_size - 1)) >> sbi->cluster_bits;
1216 BUG_ON(*nr_cluster > 2);
1217
1218 err = fat_alloc_clusters(dir, cluster, *nr_cluster);
1219 if (err)
1220 goto error;
1221
1222
1223
1224
1225
1226
1227 i = n = copy = 0;
1228 do {
1229 start_blknr = blknr = fat_clus_to_blknr(sbi, cluster[i]);
1230 last_blknr = start_blknr + sbi->sec_per_clus;
1231 while (blknr < last_blknr) {
1232 bhs[n] = sb_getblk(sb, blknr);
1233 if (!bhs[n]) {
1234 err = -ENOMEM;
1235 goto error_nomem;
1236 }
1237
1238
1239 copy = min(size, sb->s_blocksize);
1240
1241 lock_buffer(bhs[n]);
1242 memcpy(bhs[n]->b_data, slots, copy);
1243 set_buffer_uptodate(bhs[n]);
1244 unlock_buffer(bhs[n]);
1245 mark_buffer_dirty_inode(bhs[n], dir);
1246 slots += copy;
1247 size -= copy;
1248 if (!size)
1249 break;
1250 n++;
1251 blknr++;
1252 }
1253 } while (++i < *nr_cluster);
1254
1255 memset(bhs[n]->b_data + copy, 0, sb->s_blocksize - copy);
1256 offset = copy - sizeof(struct msdos_dir_entry);
1257 get_bh(bhs[n]);
1258 *bh = bhs[n];
1259 *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
1260 *i_pos = fat_make_i_pos(sb, *bh, *de);
1261
1262
1263 err = fat_zeroed_cluster(dir, start_blknr, ++n, bhs, MAX_BUF_PER_PAGE);
1264 if (err)
1265 goto error_free;
1266
1267 return cluster[0];
1268
1269 error_free:
1270 brelse(*bh);
1271 *bh = NULL;
1272 n = 0;
1273 error_nomem:
1274 for (i = 0; i < n; i++)
1275 bforget(bhs[i]);
1276 fat_free_clusters(dir, cluster[0]);
1277 error:
1278 return err;
1279 }
1280
1281 int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
1282 struct fat_slot_info *sinfo)
1283 {
1284 struct super_block *sb = dir->i_sb;
1285 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1286 struct buffer_head *bh, *prev, *bhs[3];
1287 struct msdos_dir_entry *de;
1288 int err, free_slots, i, nr_bhs;
1289 loff_t pos, i_pos;
1290
1291 sinfo->nr_slots = nr_slots;
1292
1293
1294 free_slots = nr_bhs = 0;
1295 bh = prev = NULL;
1296 pos = 0;
1297 err = -ENOSPC;
1298 while (fat_get_entry(dir, &pos, &bh, &de) > -1) {
1299
1300 if (pos >= FAT_MAX_DIR_SIZE)
1301 goto error;
1302
1303 if (IS_FREE(de->name)) {
1304 if (prev != bh) {
1305 get_bh(bh);
1306 bhs[nr_bhs] = prev = bh;
1307 nr_bhs++;
1308 }
1309 free_slots++;
1310 if (free_slots == nr_slots)
1311 goto found;
1312 } else {
1313 for (i = 0; i < nr_bhs; i++)
1314 brelse(bhs[i]);
1315 prev = NULL;
1316 free_slots = nr_bhs = 0;
1317 }
1318 }
1319 if (dir->i_ino == MSDOS_ROOT_INO) {
1320 if (!is_fat32(sbi))
1321 goto error;
1322 } else if (MSDOS_I(dir)->i_start == 0) {
1323 fat_msg(sb, KERN_ERR, "Corrupted directory (i_pos %lld)",
1324 MSDOS_I(dir)->i_pos);
1325 err = -EIO;
1326 goto error;
1327 }
1328
1329 found:
1330 err = 0;
1331 pos -= free_slots * sizeof(*de);
1332 nr_slots -= free_slots;
1333 if (free_slots) {
1334
1335
1336
1337
1338
1339 int size = free_slots * sizeof(*de);
1340 int offset = pos & (sb->s_blocksize - 1);
1341 int long_bhs = nr_bhs - (nr_slots == 0);
1342
1343
1344 for (i = 0; i < long_bhs; i++) {
1345 int copy = min_t(int, sb->s_blocksize - offset, size);
1346 memcpy(bhs[i]->b_data + offset, slots, copy);
1347 mark_buffer_dirty_inode(bhs[i], dir);
1348 offset = 0;
1349 slots += copy;
1350 size -= copy;
1351 }
1352 if (long_bhs && IS_DIRSYNC(dir))
1353 err = fat_sync_bhs(bhs, long_bhs);
1354 if (!err && i < nr_bhs) {
1355
1356 int copy = min_t(int, sb->s_blocksize - offset, size);
1357 memcpy(bhs[i]->b_data + offset, slots, copy);
1358 mark_buffer_dirty_inode(bhs[i], dir);
1359 if (IS_DIRSYNC(dir))
1360 err = sync_dirty_buffer(bhs[i]);
1361 }
1362 for (i = 0; i < nr_bhs; i++)
1363 brelse(bhs[i]);
1364 if (err)
1365 goto error_remove;
1366 }
1367
1368 if (nr_slots) {
1369 int cluster, nr_cluster;
1370
1371
1372
1373
1374
1375
1376 cluster = fat_add_new_entries(dir, slots, nr_slots, &nr_cluster,
1377 &de, &bh, &i_pos);
1378 if (cluster < 0) {
1379 err = cluster;
1380 goto error_remove;
1381 }
1382 err = fat_chain_add(dir, cluster, nr_cluster);
1383 if (err) {
1384 fat_free_clusters(dir, cluster);
1385 goto error_remove;
1386 }
1387 if (dir->i_size & (sbi->cluster_size - 1)) {
1388 fat_fs_error(sb, "Odd directory size");
1389 dir->i_size = (dir->i_size + sbi->cluster_size - 1)
1390 & ~((loff_t)sbi->cluster_size - 1);
1391 }
1392 dir->i_size += nr_cluster << sbi->cluster_bits;
1393 MSDOS_I(dir)->mmu_private += nr_cluster << sbi->cluster_bits;
1394 }
1395 sinfo->slot_off = pos;
1396 sinfo->de = de;
1397 sinfo->bh = bh;
1398 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
1399
1400 return 0;
1401
1402 error:
1403 brelse(bh);
1404 for (i = 0; i < nr_bhs; i++)
1405 brelse(bhs[i]);
1406 return err;
1407
1408 error_remove:
1409 brelse(bh);
1410 if (free_slots)
1411 __fat_remove_entries(dir, pos, free_slots);
1412 return err;
1413 }
1414 EXPORT_SYMBOL_GPL(fat_add_entries);