4 * directory handling functions for fat-based filesystems
6 * Written 1992,1993 by Werner Almesberger
8 * Hidden files 1995 by Albert Cahalan <albert@ccs.neu.edu> <adc@coe.neu.edu>
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>
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>
26 * Convert Unicode 16 to UTF8, translated Unicode, or ASCII.
27 * If uni_xlate is enabled and we can't get a 1:1 conversion, use a
28 * colon as an escape character since it is normally invalid on the vfat
29 * filesystem. The following four characters are the hexadecimal digits
30 * of Unicode value. This lets us do a full dump and restore of Unicode
31 * filenames. We could get into some trouble with long Unicode names,
32 * but ignore that right now.
33 * Ahem... Stack smashing in ring 0 isn't fun. Fixed.
35 static int uni16_to_x8(unsigned char *ascii, wchar_t *uni, int uni_xlate,
36 struct nls_table *nls)
39 unsigned char *op, nc;
48 if ( (charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE)) > 0) {
53 for (k = 4; k > 0; k--) {
55 op[k] = nc > 9 ? nc + ('a' - 10)
64 /* We have some slack there, so it's OK */
75 fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
79 charlen = t->char2uni(c, clen, uni);
81 *uni = 0x003f; /* a question mark */
88 fat_short2lower_uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
93 charlen = t->char2uni(c, clen, &wc);
95 *uni = 0x003f; /* a question mark */
97 } else if (charlen <= 1) {
98 unsigned char nc = t->charset2lower[*c];
103 if ( (charlen = t->char2uni(&nc, 1, uni)) < 0) {
104 *uni = 0x003f; /* a question mark */
114 fat_shortname2uni(struct nls_table *nls, unsigned char *buf, int buf_size,
115 wchar_t *uni_buf, unsigned short opt, int lower)
119 if (opt & VFAT_SFN_DISPLAY_LOWER)
120 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
121 else if (opt & VFAT_SFN_DISPLAY_WIN95)
122 len = fat_short2uni(nls, buf, buf_size, uni_buf);
123 else if (opt & VFAT_SFN_DISPLAY_WINNT) {
125 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
127 len = fat_short2uni(nls, buf, buf_size, uni_buf);
129 len = fat_short2uni(nls, buf, buf_size, uni_buf);
135 * Return values: negative -> error, 0 -> not found, positive -> found,
136 * value is the total amount of slots, including the shortname entry.
138 int fat_search_long(struct inode *inode, const unsigned char *name,
139 int name_len, int anycase, loff_t *spos, loff_t *lpos)
141 struct super_block *sb = inode->i_sb;
142 struct buffer_head *bh = NULL;
143 struct msdos_dir_entry *de;
144 struct nls_table *nls_io = MSDOS_SB(sb)->nls_io;
145 struct nls_table *nls_disk = MSDOS_SB(sb)->nls_disk;
146 wchar_t bufuname[14];
147 unsigned char xlate_len, long_slots;
148 wchar_t *unicode = NULL;
149 unsigned char work[8], bufname[260]; /* 256 + 4 */
150 int uni_xlate = MSDOS_SB(sb)->options.unicode_xlate;
151 int utf8 = MSDOS_SB(sb)->options.utf8;
152 unsigned short opt_shortname = MSDOS_SB(sb)->options.shortname;
153 int chl, i, j, last_u, res = 0;
154 loff_t i_pos, cpos = 0;
157 if (fat_get_entry(inode,&cpos,&bh,&de,&i_pos) == -1)
161 if (de->name[0] == DELETED_FLAG)
163 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
165 if (de->attr != ATTR_EXT && IS_FREE(de->name))
167 if (de->attr == ATTR_EXT) {
168 struct msdos_dir_slot *ds;
173 unsigned char alias_checksum;
176 unicode = (wchar_t *)
177 __get_free_page(GFP_KERNEL);
185 ds = (struct msdos_dir_slot *) de;
190 if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */
193 alias_checksum = ds->alias_checksum;
201 fat16_towchar(unicode + offset, ds->name0_4, 5);
202 fat16_towchar(unicode + offset + 5, ds->name5_10, 6);
203 fat16_towchar(unicode + offset + 11, ds->name11_12, 2);
206 unicode[offset + 13] = 0;
208 if (fat_get_entry(inode,&cpos,&bh,&de,&i_pos)<0)
212 ds = (struct msdos_dir_slot *) de;
213 if (ds->attr != ATTR_EXT)
215 if ((ds->id & ~0x40) != slot)
217 if (ds->alias_checksum != alias_checksum)
220 if (de->name[0] == DELETED_FLAG)
222 if (de->attr == ATTR_EXT)
224 if (IS_FREE(de->name) || (de->attr & ATTR_VOLUME))
226 for (sum = 0, i = 0; i < 11; i++)
227 sum = (((sum&1)<<7)|((sum&0xfe)>>1)) + de->name[i];
228 if (sum != alias_checksum)
232 memcpy(work, de->name, sizeof(de->name));
233 /* see namei.c, msdos_format_name */
236 for (i = 0, j = 0, last_u = 0; i < 8;) {
238 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
239 &bufuname[j++], opt_shortname,
240 de->lcase & CASE_LOWER_BASE);
250 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
251 for (i = 0; i < 3;) {
252 if (!de->ext[i]) break;
253 chl = fat_shortname2uni(nls_disk, &de->ext[i], 3 - i,
254 &bufuname[j++], opt_shortname,
255 de->lcase & CASE_LOWER_EXT);
257 if (de->ext[i] != ' ')
267 bufuname[last_u] = 0x0000;
269 ?utf8_wcstombs(bufname, bufuname, sizeof(bufname))
270 :uni16_to_x8(bufname, bufuname, uni_xlate, nls_io);
271 if (xlate_len == name_len)
272 if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
273 (anycase && !nls_strnicmp(nls_io, name, bufname,
279 ?utf8_wcstombs(bufname, unicode, sizeof(bufname))
280 :uni16_to_x8(bufname, unicode, uni_xlate, nls_io);
281 if (xlate_len != name_len)
283 if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
284 (anycase && !nls_strnicmp(nls_io, name, bufname,
291 res = long_slots + 1;
292 *spos = cpos - sizeof(struct msdos_dir_entry);
293 *lpos = cpos - res*sizeof(struct msdos_dir_entry);
297 free_page((unsigned long) unicode);
302 EXPORT_SYMBOL(fat_search_long);
304 struct fat_ioctl_filldir_callback {
305 struct dirent __user *dirent;
308 const char *longname;
310 const char *shortname;
314 static int fat_readdirx(struct inode *inode, struct file *filp, void *dirent,
315 filldir_t filldir, int short_only, int both)
317 struct super_block *sb = inode->i_sb;
318 struct buffer_head *bh;
319 struct msdos_dir_entry *de;
320 struct nls_table *nls_io = MSDOS_SB(sb)->nls_io;
321 struct nls_table *nls_disk = MSDOS_SB(sb)->nls_disk;
322 unsigned char long_slots;
323 const char *fill_name;
325 wchar_t bufuname[14];
326 wchar_t *unicode = NULL;
327 unsigned char c, work[8], bufname[56], *ptname = bufname;
328 unsigned long lpos, dummy, *furrfu = &lpos;
329 int uni_xlate = MSDOS_SB(sb)->options.unicode_xlate;
330 int isvfat = MSDOS_SB(sb)->options.isvfat;
331 int utf8 = MSDOS_SB(sb)->options.utf8;
332 int nocase = MSDOS_SB(sb)->options.nocase;
333 unsigned short opt_shortname = MSDOS_SB(sb)->options.shortname;
335 int chi, chl, i, i2, j, last, last_u, dotoffset = 0;
342 /* Fake . and .. for the root directory. */
343 if (inode->i_ino == MSDOS_ROOT_INO) {
345 if (filldir(dirent, "..", cpos+1, cpos, MSDOS_ROOT_INO, DT_DIR) < 0)
356 if (cpos & (sizeof(struct msdos_dir_entry)-1)) {
364 if (fat_get_entry(inode,&cpos,&bh,&de,&i_pos) == -1)
366 /* Check for long filename entry */
368 if (de->name[0] == DELETED_FLAG)
370 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
372 if (de->attr != ATTR_EXT && IS_FREE(de->name))
375 if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
379 if (isvfat && de->attr == ATTR_EXT) {
380 struct msdos_dir_slot *ds;
385 unsigned char alias_checksum;
388 unicode = (wchar_t *)__get_free_page(GFP_KERNEL);
398 ds = (struct msdos_dir_slot *) de;
403 if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */
406 alias_checksum = ds->alias_checksum;
414 fat16_towchar(unicode + offset, ds->name0_4, 5);
415 fat16_towchar(unicode + offset + 5, ds->name5_10, 6);
416 fat16_towchar(unicode + offset + 11, ds->name11_12, 2);
419 unicode[offset + 13] = 0;
421 if (fat_get_entry(inode,&cpos,&bh,&de,&i_pos) == -1)
425 ds = (struct msdos_dir_slot *) de;
426 if (ds->attr != ATTR_EXT)
427 goto RecEnd; /* XXX */
428 if ((ds->id & ~0x40) != slot)
430 if (ds->alias_checksum != alias_checksum)
433 if (de->name[0] == DELETED_FLAG)
435 if (de->attr == ATTR_EXT)
437 if (IS_FREE(de->name) || (de->attr & ATTR_VOLUME))
439 for (sum = 0, i = 0; i < 11; i++)
440 sum = (((sum&1)<<7)|((sum&0xfe)>>1)) + de->name[i];
441 if (sum != alias_checksum)
445 if ((de->attr & ATTR_HIDDEN) && MSDOS_SB(sb)->options.dotsOK) {
450 memcpy(work, de->name, sizeof(de->name));
451 /* see namei.c, msdos_format_name */
454 for (i = 0, j = 0, last = 0, last_u = 0; i < 8;) {
455 if (!(c = work[i])) break;
456 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
457 &bufuname[j++], opt_shortname,
458 de->lcase & CASE_LOWER_BASE);
460 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
467 for (chi = 0; chi < chl && i < 8; chi++) {
475 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
477 for (i2 = 0; i2 < 3;) {
478 if (!(c = de->ext[i2])) break;
479 chl = fat_shortname2uni(nls_disk, &de->ext[i2], 3 - i2,
480 &bufuname[j++], opt_shortname,
481 de->lcase & CASE_LOWER_EXT);
484 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
491 for (chi = 0; chi < chl && i2 < 3; chi++) {
492 ptname[i++] = de->ext[i2++];
500 i = last + dotoffset;
503 lpos = cpos - (long_slots+1)*sizeof(struct msdos_dir_entry);
504 if (!memcmp(de->name,MSDOS_DOT,11))
506 else if (!memcmp(de->name,MSDOS_DOTDOT,11)) {
507 inum = parent_ino(filp->f_dentry);
509 struct inode *tmp = fat_iget(sb, i_pos);
514 inum = iunique(sb, MSDOS_ROOT_INO);
518 bufuname[j] = 0x0000;
519 i = utf8 ? utf8_wcstombs(bufname, bufuname, sizeof(bufname))
520 : uni16_to_x8(bufname, bufuname, uni_xlate, nls_io);
525 if (!short_only && long_slots) {
526 /* convert the unicode long name. 261 is maximum size
527 * of unicode buffer. (13 * slots + nul) */
528 void *longname = unicode + 261;
529 int buf_size = PAGE_SIZE - (261 * sizeof(unicode[0]));
531 ? utf8_wcstombs(longname, unicode, buf_size)
532 : uni16_to_x8(longname, unicode, uni_xlate, nls_io);
535 fill_name = longname;
538 /* hack for fat_ioctl_filldir() */
539 struct fat_ioctl_filldir_callback *p = dirent;
541 p->longname = longname;
542 p->long_len = long_len;
543 p->shortname = bufname;
549 if (filldir(dirent, fill_name, fill_len, *furrfu, inum,
550 (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
563 free_page((unsigned long)unicode);
569 static int fat_readdir(struct file *filp, void *dirent, filldir_t filldir)
571 struct inode *inode = filp->f_dentry->d_inode;
572 return fat_readdirx(inode, filp, dirent, filldir, 0, 0);
575 static int fat_ioctl_filldir(void *__buf, const char *name, int name_len,
576 loff_t offset, ino_t ino, unsigned int d_type)
578 struct fat_ioctl_filldir_callback *buf = __buf;
579 struct dirent __user *d1 = buf->dirent;
580 struct dirent __user *d2 = d1 + 1;
587 /* dirent has only short name */
588 if (name_len >= sizeof(d1->d_name))
589 name_len = sizeof(d1->d_name) - 1;
591 if (put_user(0, d2->d_name) ||
592 put_user(0, &d2->d_reclen) ||
593 copy_to_user(d1->d_name, name, name_len) ||
594 put_user(0, d1->d_name + name_len) ||
595 put_user(name_len, &d1->d_reclen))
598 /* dirent has short and long name */
599 const char *longname = buf->longname;
600 int long_len = buf->long_len;
601 const char *shortname = buf->shortname;
602 int short_len = buf->short_len;
604 if (long_len >= sizeof(d1->d_name))
605 long_len = sizeof(d1->d_name) - 1;
606 if (short_len >= sizeof(d1->d_name))
607 short_len = sizeof(d1->d_name) - 1;
609 if (copy_to_user(d2->d_name, longname, long_len) ||
610 put_user(0, d2->d_name + long_len) ||
611 put_user(long_len, &d2->d_reclen) ||
612 put_user(ino, &d2->d_ino) ||
613 put_user(offset, &d2->d_off) ||
614 copy_to_user(d1->d_name, shortname, short_len) ||
615 put_user(0, d1->d_name + short_len) ||
616 put_user(short_len, &d1->d_reclen))
621 buf->result = -EFAULT;
625 static int fat_dir_ioctl(struct inode * inode, struct file * filp,
626 unsigned int cmd, unsigned long arg)
628 struct fat_ioctl_filldir_callback buf;
629 struct dirent __user *d1;
630 int ret, short_only, both;
633 case VFAT_IOCTL_READDIR_SHORT:
637 case VFAT_IOCTL_READDIR_BOTH:
645 d1 = (struct dirent __user *)arg;
646 if (!access_ok(VERIFY_WRITE, d1, sizeof(struct dirent[2])))
649 * Yes, we don't need this put_user() absolutely. However old
650 * code didn't return the right value. So, app use this value,
651 * in order to check whether it is EOF.
653 if (put_user(0, &d1->d_reclen))
660 if (!IS_DEADDIR(inode)) {
661 ret = fat_readdirx(inode, filp, &buf, fat_ioctl_filldir,
670 struct file_operations fat_dir_operations = {
671 .read = generic_read_dir,
672 .readdir = fat_readdir,
673 .ioctl = fat_dir_ioctl,
677 static int fat_get_short_entry(struct inode *dir, loff_t *pos,
678 struct buffer_head **bh,
679 struct msdos_dir_entry **de, loff_t *i_pos)
681 while (fat_get_entry(dir, pos, bh, de, i_pos) >= 0) {
682 /* free entry or long name entry or volume label */
683 if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME))
689 /* See if directory is empty */
690 int fat_dir_empty(struct inode *dir)
692 struct buffer_head *bh;
693 struct msdos_dir_entry *de;
699 while (fat_get_short_entry(dir, &cpos, &bh, &de, &i_pos) >= 0) {
700 if (strncmp(de->name, MSDOS_DOT , MSDOS_NAME) &&
701 strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
710 EXPORT_SYMBOL(fat_dir_empty);
713 * fat_subdirs counts the number of sub-directories of dir. It can be run
714 * on directories being created.
716 int fat_subdirs(struct inode *dir)
718 struct buffer_head *bh;
719 struct msdos_dir_entry *de;
725 while (fat_get_short_entry(dir, &cpos, &bh, &de, &i_pos) >= 0) {
726 if (de->attr & ATTR_DIR)
734 * Scans a directory for a given file (name points to its formatted name).
735 * Returns an error code or zero.
737 int fat_scan(struct inode *dir, const unsigned char *name,
738 struct buffer_head **bh, struct msdos_dir_entry **de,
745 while (fat_get_short_entry(dir, &cpos, bh, de, i_pos) >= 0) {
746 if (!strncmp((*de)->name, name, MSDOS_NAME))
752 EXPORT_SYMBOL(fat_scan);
754 static struct buffer_head *fat_extend_dir(struct inode *inode)
756 struct super_block *sb = inode->i_sb;
757 struct buffer_head *bh, *res = NULL;
758 int nr, sec_per_clus = MSDOS_SB(sb)->sec_per_clus;
759 sector_t sector, last_sector;
761 if (MSDOS_SB(sb)->fat_bits != 32) {
762 if (inode->i_ino == MSDOS_ROOT_INO)
763 return ERR_PTR(-ENOSPC);
766 nr = fat_add_cluster(inode);
770 sector = fat_clus_to_blknr(MSDOS_SB(sb), nr);
771 last_sector = sector + sec_per_clus;
772 for ( ; sector < last_sector; sector++) {
773 if ((bh = sb_getblk(sb, sector))) {
774 memset(bh->b_data, 0, sb->s_blocksize);
775 set_buffer_uptodate(bh);
776 mark_buffer_dirty(bh);
785 if (inode->i_size & (sb->s_blocksize - 1)) {
786 fat_fs_panic(sb, "Odd directory size");
787 inode->i_size = (inode->i_size + sb->s_blocksize)
788 & ~((loff_t)sb->s_blocksize - 1);
790 inode->i_size += MSDOS_SB(sb)->cluster_size;
791 MSDOS_I(inode)->mmu_private += MSDOS_SB(sb)->cluster_size;
796 /* This assumes that size of cluster is above the 32*slots */
798 int fat_add_entries(struct inode *dir,int slots, struct buffer_head **bh,
799 struct msdos_dir_entry **de, loff_t *i_pos)
801 struct super_block *sb = dir->i_sb;
804 struct buffer_head *new_bh;
809 while (fat_get_entry(dir, &curr, bh, de, i_pos) > -1) {
810 /* check the maximum size of directory */
811 if (curr >= FAT_MAX_DIR_SIZE) {
816 if (IS_FREE((*de)->name)) {
824 if ((dir->i_ino == MSDOS_ROOT_INO) && (MSDOS_SB(sb)->fat_bits != 32))
826 new_bh = fat_extend_dir(dir);
828 return PTR_ERR(new_bh);
831 fat_get_entry(dir, &curr, bh, de, i_pos);
832 } while (++row < slots);
837 EXPORT_SYMBOL(fat_add_entries);
839 int fat_new_dir(struct inode *dir, struct inode *parent, int is_vfat)
841 struct buffer_head *bh;
842 struct msdos_dir_entry *de;
845 bh = fat_extend_dir(dir);
849 /* zeroed out, so... */
850 fat_date_unix2dos(dir->i_mtime.tv_sec,&time,&date);
851 de = (struct msdos_dir_entry*)&bh->b_data[0];
852 memcpy(de[0].name,MSDOS_DOT,MSDOS_NAME);
853 memcpy(de[1].name,MSDOS_DOTDOT,MSDOS_NAME);
854 de[0].attr = de[1].attr = ATTR_DIR;
855 de[0].time = de[1].time = time;
856 de[0].date = de[1].date = date;
857 if (is_vfat) { /* extra timestamps */
858 de[0].ctime = de[1].ctime = time;
859 de[0].adate = de[0].cdate =
860 de[1].adate = de[1].cdate = date;
862 de[0].start = cpu_to_le16(MSDOS_I(dir)->i_logstart);
863 de[0].starthi = cpu_to_le16(MSDOS_I(dir)->i_logstart>>16);
864 de[1].start = cpu_to_le16(MSDOS_I(parent)->i_logstart);
865 de[1].starthi = cpu_to_le16(MSDOS_I(parent)->i_logstart>>16);
866 mark_buffer_dirty(bh);
868 dir->i_atime = dir->i_ctime = dir->i_mtime = CURRENT_TIME_SEC;
869 mark_inode_dirty(dir);
874 EXPORT_SYMBOL(fat_new_dir);