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/slab.h>
17 #include <linux/time.h>
18 #include <linux/msdos_fs.h>
19 #include <linux/dirent.h>
20 #include <linux/smp_lock.h>
21 #include <linux/buffer_head.h>
23 #include <asm/uaccess.h>
25 static int fat_dir_ioctl(struct inode * inode, struct file * filp,
26 unsigned int cmd, unsigned long arg);
27 static int fat_readdir(struct file *filp, void *dirent, filldir_t filldir);
29 struct file_operations fat_dir_operations = {
30 .read = generic_read_dir,
31 .readdir = fat_readdir,
32 .ioctl = fat_dir_ioctl,
37 * Convert Unicode 16 to UTF8, translated Unicode, or ASCII.
38 * If uni_xlate is enabled and we can't get a 1:1 conversion, use a
39 * colon as an escape character since it is normally invalid on the vfat
40 * filesystem. The following four characters are the hexadecimal digits
41 * of Unicode value. This lets us do a full dump and restore of Unicode
42 * filenames. We could get into some trouble with long Unicode names,
43 * but ignore that right now.
44 * Ahem... Stack smashing in ring 0 isn't fun. Fixed.
47 uni16_to_x8(unsigned char *ascii, wchar_t *uni, int uni_xlate,
48 struct nls_table *nls)
51 unsigned char *op, nc;
60 if ( (charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE)) > 0) {
65 for (k = 4; k > 0; k--) {
67 op[k] = nc > 9 ? nc + ('a' - 10)
76 /* We have some slack there, so it's OK */
87 static void dump_de(struct msdos_dir_entry *de)
90 unsigned char *p = (unsigned char *) de;
93 for (i = 0; i < 32; i++, p++) {
101 fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
105 charlen = t->char2uni(c, clen, uni);
107 *uni = 0x003f; /* a question mark */
114 fat_short2lower_uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
119 charlen = t->char2uni(c, clen, &wc);
121 *uni = 0x003f; /* a question mark */
123 } else if (charlen <= 1) {
124 unsigned char nc = t->charset2lower[*c];
129 if ( (charlen = t->char2uni(&nc, 1, uni)) < 0) {
130 *uni = 0x003f; /* a question mark */
140 fat_shortname2uni(struct nls_table *nls, unsigned char *buf, int buf_size,
141 wchar_t *uni_buf, unsigned short opt, int lower)
145 if (opt & VFAT_SFN_DISPLAY_LOWER)
146 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
147 else if (opt & VFAT_SFN_DISPLAY_WIN95)
148 len = fat_short2uni(nls, buf, buf_size, uni_buf);
149 else if (opt & VFAT_SFN_DISPLAY_WINNT) {
151 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
153 len = fat_short2uni(nls, buf, buf_size, uni_buf);
155 len = fat_short2uni(nls, buf, buf_size, uni_buf);
161 * Return values: negative -> error, 0 -> not found, positive -> found,
162 * value is the total amount of slots, including the shortname entry.
164 int fat_search_long(struct inode *inode, const unsigned char *name,
165 int name_len, int anycase, loff_t *spos, loff_t *lpos)
167 struct super_block *sb = inode->i_sb;
168 struct buffer_head *bh = NULL;
169 struct msdos_dir_entry *de;
170 struct nls_table *nls_io = MSDOS_SB(sb)->nls_io;
171 struct nls_table *nls_disk = MSDOS_SB(sb)->nls_disk;
172 wchar_t bufuname[14];
173 unsigned char xlate_len, long_slots;
174 wchar_t *unicode = NULL;
175 unsigned char work[8], bufname[260]; /* 256 + 4 */
176 int uni_xlate = MSDOS_SB(sb)->options.unicode_xlate;
177 int utf8 = MSDOS_SB(sb)->options.utf8;
178 unsigned short opt_shortname = MSDOS_SB(sb)->options.shortname;
179 int chl, i, j, last_u, res = 0;
180 loff_t i_pos, cpos = 0;
183 if (fat_get_entry(inode,&cpos,&bh,&de,&i_pos) == -1)
187 if (de->name[0] == DELETED_FLAG)
189 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
191 if (de->attr != ATTR_EXT && IS_FREE(de->name))
193 if (de->attr == ATTR_EXT) {
194 struct msdos_dir_slot *ds;
199 unsigned char alias_checksum;
202 unicode = (wchar_t *)
203 __get_free_page(GFP_KERNEL);
211 ds = (struct msdos_dir_slot *) de;
216 if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */
219 alias_checksum = ds->alias_checksum;
227 fat16_towchar(unicode + offset, ds->name0_4, 5);
228 fat16_towchar(unicode + offset + 5, ds->name5_10, 6);
229 fat16_towchar(unicode + offset + 11, ds->name11_12, 2);
232 unicode[offset + 13] = 0;
234 if (fat_get_entry(inode,&cpos,&bh,&de,&i_pos)<0)
238 ds = (struct msdos_dir_slot *) de;
239 if (ds->attr != ATTR_EXT)
241 if ((ds->id & ~0x40) != slot)
243 if (ds->alias_checksum != alias_checksum)
246 if (de->name[0] == DELETED_FLAG)
248 if (de->attr == ATTR_EXT)
250 if (IS_FREE(de->name) || (de->attr & ATTR_VOLUME))
252 for (sum = 0, i = 0; i < 11; i++)
253 sum = (((sum&1)<<7)|((sum&0xfe)>>1)) + de->name[i];
254 if (sum != alias_checksum)
258 memcpy(work, de->name, sizeof(de->name));
259 /* see namei.c, msdos_format_name */
262 for (i = 0, j = 0, last_u = 0; i < 8;) {
264 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
265 &bufuname[j++], opt_shortname,
266 de->lcase & CASE_LOWER_BASE);
276 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
277 for (i = 0; i < 3;) {
278 if (!de->ext[i]) break;
279 chl = fat_shortname2uni(nls_disk, &de->ext[i], 3 - i,
280 &bufuname[j++], opt_shortname,
281 de->lcase & CASE_LOWER_EXT);
283 if (de->ext[i] != ' ')
293 bufuname[last_u] = 0x0000;
295 ?utf8_wcstombs(bufname, bufuname, sizeof(bufname))
296 :uni16_to_x8(bufname, bufuname, uni_xlate, nls_io);
297 if (xlate_len == name_len)
298 if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
299 (anycase && !nls_strnicmp(nls_io, name, bufname,
305 ?utf8_wcstombs(bufname, unicode, sizeof(bufname))
306 :uni16_to_x8(bufname, unicode, uni_xlate, nls_io);
307 if (xlate_len != name_len)
309 if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
310 (anycase && !nls_strnicmp(nls_io, name, bufname,
317 res = long_slots + 1;
318 *spos = cpos - sizeof(struct msdos_dir_entry);
319 *lpos = cpos - res*sizeof(struct msdos_dir_entry);
323 free_page((unsigned long) unicode);
328 struct fat_ioctl_filldir_callback {
329 struct dirent __user *dirent;
332 const char *longname;
334 const char *shortname;
338 static int fat_readdirx(struct inode *inode, struct file *filp, void *dirent,
339 filldir_t filldir, int short_only, int both)
341 struct super_block *sb = inode->i_sb;
342 struct buffer_head *bh;
343 struct msdos_dir_entry *de;
344 struct nls_table *nls_io = MSDOS_SB(sb)->nls_io;
345 struct nls_table *nls_disk = MSDOS_SB(sb)->nls_disk;
346 unsigned char long_slots;
347 const char *fill_name;
349 wchar_t bufuname[14];
350 wchar_t *unicode = NULL;
351 unsigned char c, work[8], bufname[56], *ptname = bufname;
352 unsigned long lpos, dummy, *furrfu = &lpos;
353 int uni_xlate = MSDOS_SB(sb)->options.unicode_xlate;
354 int isvfat = MSDOS_SB(sb)->options.isvfat;
355 int utf8 = MSDOS_SB(sb)->options.utf8;
356 int nocase = MSDOS_SB(sb)->options.nocase;
357 unsigned short opt_shortname = MSDOS_SB(sb)->options.shortname;
359 int chi, chl, i, i2, j, last, last_u, dotoffset = 0;
366 /* Fake . and .. for the root directory. */
367 if (inode->i_ino == MSDOS_ROOT_INO) {
369 if (filldir(dirent, "..", cpos+1, cpos, MSDOS_ROOT_INO, DT_DIR) < 0)
380 if (cpos & (sizeof(struct msdos_dir_entry)-1)) {
388 if (fat_get_entry(inode,&cpos,&bh,&de,&i_pos) == -1)
390 /* Check for long filename entry */
392 if (de->name[0] == DELETED_FLAG)
394 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
396 if (de->attr != ATTR_EXT && IS_FREE(de->name))
399 if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
403 if (isvfat && de->attr == ATTR_EXT) {
404 struct msdos_dir_slot *ds;
409 unsigned char alias_checksum;
412 unicode = (wchar_t *)__get_free_page(GFP_KERNEL);
422 ds = (struct msdos_dir_slot *) de;
427 if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */
430 alias_checksum = ds->alias_checksum;
438 fat16_towchar(unicode + offset, ds->name0_4, 5);
439 fat16_towchar(unicode + offset + 5, ds->name5_10, 6);
440 fat16_towchar(unicode + offset + 11, ds->name11_12, 2);
443 unicode[offset + 13] = 0;
445 if (fat_get_entry(inode,&cpos,&bh,&de,&i_pos) == -1)
449 ds = (struct msdos_dir_slot *) de;
450 if (ds->attr != ATTR_EXT)
451 goto RecEnd; /* XXX */
452 if ((ds->id & ~0x40) != slot)
454 if (ds->alias_checksum != alias_checksum)
457 if (de->name[0] == DELETED_FLAG)
459 if (de->attr == ATTR_EXT)
461 if (IS_FREE(de->name) || (de->attr & ATTR_VOLUME))
463 for (sum = 0, i = 0; i < 11; i++)
464 sum = (((sum&1)<<7)|((sum&0xfe)>>1)) + de->name[i];
465 if (sum != alias_checksum)
469 if ((de->attr & ATTR_HIDDEN) && MSDOS_SB(sb)->options.dotsOK) {
474 memcpy(work, de->name, sizeof(de->name));
475 /* see namei.c, msdos_format_name */
478 for (i = 0, j = 0, last = 0, last_u = 0; i < 8;) {
479 if (!(c = work[i])) break;
480 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
481 &bufuname[j++], opt_shortname,
482 de->lcase & CASE_LOWER_BASE);
484 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
491 for (chi = 0; chi < chl && i < 8; chi++) {
499 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
501 for (i2 = 0; i2 < 3;) {
502 if (!(c = de->ext[i2])) break;
503 chl = fat_shortname2uni(nls_disk, &de->ext[i2], 3 - i2,
504 &bufuname[j++], opt_shortname,
505 de->lcase & CASE_LOWER_EXT);
508 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
515 for (chi = 0; chi < chl && i2 < 3; chi++) {
516 ptname[i++] = de->ext[i2++];
524 i = last + dotoffset;
527 lpos = cpos - (long_slots+1)*sizeof(struct msdos_dir_entry);
528 if (!memcmp(de->name,MSDOS_DOT,11))
530 else if (!memcmp(de->name,MSDOS_DOTDOT,11)) {
531 inum = parent_ino(filp->f_dentry);
533 struct inode *tmp = fat_iget(sb, i_pos);
538 inum = iunique(sb, MSDOS_ROOT_INO);
542 bufuname[j] = 0x0000;
543 i = utf8 ? utf8_wcstombs(bufname, bufuname, sizeof(bufname))
544 : uni16_to_x8(bufname, bufuname, uni_xlate, nls_io);
549 if (!short_only && long_slots) {
550 /* convert the unicode long name. 261 is maximum size
551 * of unicode buffer. (13 * slots + nul) */
552 void *longname = unicode + 261;
553 int buf_size = PAGE_SIZE - (261 * sizeof(unicode[0]));
555 ? utf8_wcstombs(longname, unicode, buf_size)
556 : uni16_to_x8(longname, unicode, uni_xlate, nls_io);
559 fill_name = longname;
562 /* hack for fat_ioctl_filldir() */
563 struct fat_ioctl_filldir_callback *p = dirent;
565 p->longname = longname;
566 p->long_len = long_len;
567 p->shortname = bufname;
573 if (filldir(dirent, fill_name, fill_len, *furrfu, inum,
574 (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
587 free_page((unsigned long)unicode);
593 static int fat_readdir(struct file *filp, void *dirent, filldir_t filldir)
595 struct inode *inode = filp->f_dentry->d_inode;
596 return fat_readdirx(inode, filp, dirent, filldir, 0, 0);
599 static int fat_ioctl_filldir(void *__buf, const char *name, int name_len,
600 loff_t offset, ino_t ino, unsigned int d_type)
602 struct fat_ioctl_filldir_callback *buf = __buf;
603 struct dirent __user *d1 = buf->dirent;
604 struct dirent __user *d2 = d1 + 1;
611 /* dirent has only short name */
612 if (name_len >= sizeof(d1->d_name))
613 name_len = sizeof(d1->d_name) - 1;
615 if (put_user(0, d2->d_name) ||
616 put_user(0, &d2->d_reclen) ||
617 copy_to_user(d1->d_name, name, name_len) ||
618 put_user(0, d1->d_name + name_len) ||
619 put_user(name_len, &d1->d_reclen))
622 /* dirent has short and long name */
623 const char *longname = buf->longname;
624 int long_len = buf->long_len;
625 const char *shortname = buf->shortname;
626 int short_len = buf->short_len;
628 if (long_len >= sizeof(d1->d_name))
629 long_len = sizeof(d1->d_name) - 1;
630 if (short_len >= sizeof(d1->d_name))
631 short_len = sizeof(d1->d_name) - 1;
633 if (copy_to_user(d2->d_name, longname, long_len) ||
634 put_user(0, d2->d_name + long_len) ||
635 put_user(long_len, &d2->d_reclen) ||
636 put_user(ino, &d2->d_ino) ||
637 put_user(offset, &d2->d_off) ||
638 copy_to_user(d1->d_name, shortname, short_len) ||
639 put_user(0, d1->d_name + short_len) ||
640 put_user(short_len, &d1->d_reclen))
645 buf->result = -EFAULT;
649 static int fat_dir_ioctl(struct inode * inode, struct file * filp,
650 unsigned int cmd, unsigned long arg)
652 struct fat_ioctl_filldir_callback buf;
653 struct dirent __user *d1;
654 int ret, short_only, both;
657 case VFAT_IOCTL_READDIR_SHORT:
661 case VFAT_IOCTL_READDIR_BOTH:
669 d1 = (struct dirent __user *)arg;
670 if (!access_ok(VERIFY_WRITE, d1, sizeof(struct dirent[2])))
673 * Yes, we don't need this put_user() absolutely. However old
674 * code didn't return the right value. So, app use this value,
675 * in order to check whether it is EOF.
677 if (put_user(0, &d1->d_reclen))
684 if (!IS_DEADDIR(inode)) {
685 ret = fat_readdirx(inode, filp, &buf, fat_ioctl_filldir,
694 /* This assumes that size of cluster is above the 32*slots */
696 int fat_add_entries(struct inode *dir,int slots, struct buffer_head **bh,
697 struct msdos_dir_entry **de, loff_t *i_pos)
699 struct super_block *sb = dir->i_sb;
702 struct buffer_head *new_bh;
707 while (fat_get_entry(dir, &curr, bh, de, i_pos) > -1) {
708 /* check the maximum size of directory */
709 if (curr >= FAT_MAX_DIR_SIZE) {
714 if (IS_FREE((*de)->name)) {
722 if ((dir->i_ino == MSDOS_ROOT_INO) && (MSDOS_SB(sb)->fat_bits != 32))
724 new_bh = fat_extend_dir(dir);
726 return PTR_ERR(new_bh);
729 fat_get_entry(dir, &curr, bh, de, i_pos);
730 } while (++row < slots);
735 int fat_new_dir(struct inode *dir, struct inode *parent, int is_vfat)
737 struct buffer_head *bh;
738 struct msdos_dir_entry *de;
741 bh = fat_extend_dir(dir);
745 /* zeroed out, so... */
746 fat_date_unix2dos(dir->i_mtime.tv_sec,&time,&date);
747 de = (struct msdos_dir_entry*)&bh->b_data[0];
748 memcpy(de[0].name,MSDOS_DOT,MSDOS_NAME);
749 memcpy(de[1].name,MSDOS_DOTDOT,MSDOS_NAME);
750 de[0].attr = de[1].attr = ATTR_DIR;
751 de[0].time = de[1].time = time;
752 de[0].date = de[1].date = date;
753 if (is_vfat) { /* extra timestamps */
754 de[0].ctime = de[1].ctime = time;
755 de[0].adate = de[0].cdate =
756 de[1].adate = de[1].cdate = date;
758 de[0].start = cpu_to_le16(MSDOS_I(dir)->i_logstart);
759 de[0].starthi = cpu_to_le16(MSDOS_I(dir)->i_logstart>>16);
760 de[1].start = cpu_to_le16(MSDOS_I(parent)->i_logstart);
761 de[1].starthi = cpu_to_le16(MSDOS_I(parent)->i_logstart>>16);
762 mark_buffer_dirty(bh);
764 dir->i_atime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
765 mark_inode_dirty(dir);
770 static int fat_get_short_entry(struct inode *dir, loff_t *pos,
771 struct buffer_head **bh,
772 struct msdos_dir_entry **de, loff_t *i_pos)
774 while (fat_get_entry(dir, pos, bh, de, i_pos) >= 0) {
775 /* free entry or long name entry or volume label */
776 if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME))
782 /* See if directory is empty */
783 int fat_dir_empty(struct inode *dir)
785 struct buffer_head *bh;
786 struct msdos_dir_entry *de;
792 while (fat_get_short_entry(dir, &cpos, &bh, &de, &i_pos) >= 0) {
793 if (strncmp(de->name, MSDOS_DOT , MSDOS_NAME) &&
794 strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
804 * fat_subdirs counts the number of sub-directories of dir. It can be run
805 * on directories being created.
807 int fat_subdirs(struct inode *dir)
809 struct buffer_head *bh;
810 struct msdos_dir_entry *de;
816 while (fat_get_short_entry(dir, &cpos, &bh, &de, &i_pos) >= 0) {
817 if (de->attr & ATTR_DIR)
825 * Scans a directory for a given file (name points to its formatted name).
826 * Returns an error code or zero.
828 int fat_scan(struct inode *dir, const unsigned char *name,
829 struct buffer_head **bh, struct msdos_dir_entry **de,
836 while (fat_get_short_entry(dir, &cpos, bh, de, i_pos) >= 0) {
837 if (!strncmp((*de)->name, name, MSDOS_NAME))