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 static int fat_readdirx(struct inode *inode, struct file *filp, void *dirent,
329 filldir_t filldir, int shortnames, int both)
331 struct super_block *sb = inode->i_sb;
332 struct buffer_head *bh;
333 struct msdos_dir_entry *de;
334 struct nls_table *nls_io = MSDOS_SB(sb)->nls_io;
335 struct nls_table *nls_disk = MSDOS_SB(sb)->nls_disk;
336 wchar_t bufuname[14];
337 unsigned char long_slots;
338 wchar_t *unicode = NULL;
339 unsigned char c, work[8], bufname[56], *ptname = bufname;
340 unsigned long lpos, dummy, *furrfu = &lpos;
341 int uni_xlate = MSDOS_SB(sb)->options.unicode_xlate;
342 int isvfat = MSDOS_SB(sb)->options.isvfat;
343 int utf8 = MSDOS_SB(sb)->options.utf8;
344 int nocase = MSDOS_SB(sb)->options.nocase;
345 unsigned short opt_shortname = MSDOS_SB(sb)->options.shortname;
347 int chi, chl, i, i2, j, last, last_u, dotoffset = 0;
354 /* Fake . and .. for the root directory. */
355 if (inode->i_ino == MSDOS_ROOT_INO) {
357 if (filldir(dirent, "..", cpos+1, cpos, MSDOS_ROOT_INO, DT_DIR) < 0)
368 if (cpos & (sizeof(struct msdos_dir_entry)-1)) {
376 if (fat_get_entry(inode,&cpos,&bh,&de,&i_pos) == -1)
378 /* Check for long filename entry */
380 if (de->name[0] == DELETED_FLAG)
382 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
384 if (de->attr != ATTR_EXT && IS_FREE(de->name))
387 if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
391 if (isvfat && de->attr == ATTR_EXT) {
392 struct msdos_dir_slot *ds;
397 unsigned char alias_checksum;
400 unicode = (wchar_t *)
401 __get_free_page(GFP_KERNEL);
411 ds = (struct msdos_dir_slot *) de;
416 if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */
419 alias_checksum = ds->alias_checksum;
427 fat16_towchar(unicode + offset, ds->name0_4, 5);
428 fat16_towchar(unicode + offset + 5, ds->name5_10, 6);
429 fat16_towchar(unicode + offset + 11, ds->name11_12, 2);
432 unicode[offset + 13] = 0;
434 if (fat_get_entry(inode,&cpos,&bh,&de,&i_pos) == -1)
438 ds = (struct msdos_dir_slot *) de;
439 if (ds->attr != ATTR_EXT)
440 goto RecEnd; /* XXX */
441 if ((ds->id & ~0x40) != slot)
443 if (ds->alias_checksum != alias_checksum)
446 if (de->name[0] == DELETED_FLAG)
448 if (de->attr == ATTR_EXT)
450 if (IS_FREE(de->name) || (de->attr & ATTR_VOLUME))
452 for (sum = 0, i = 0; i < 11; i++)
453 sum = (((sum&1)<<7)|((sum&0xfe)>>1)) + de->name[i];
454 if (sum != alias_checksum)
458 if ((de->attr & ATTR_HIDDEN) && MSDOS_SB(sb)->options.dotsOK) {
463 memcpy(work, de->name, sizeof(de->name));
464 /* see namei.c, msdos_format_name */
467 for (i = 0, j = 0, last = 0, last_u = 0; i < 8;) {
468 if (!(c = work[i])) break;
469 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
470 &bufuname[j++], opt_shortname,
471 de->lcase & CASE_LOWER_BASE);
473 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
480 for (chi = 0; chi < chl && i < 8; chi++) {
488 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
490 for (i2 = 0; i2 < 3;) {
491 if (!(c = de->ext[i2])) break;
492 chl = fat_shortname2uni(nls_disk, &de->ext[i2], 3 - i2,
493 &bufuname[j++], opt_shortname,
494 de->lcase & CASE_LOWER_EXT);
497 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
504 for (chi = 0; chi < chl && i2 < 3; chi++) {
505 ptname[i++] = de->ext[i2++];
513 i = last + dotoffset;
516 lpos = cpos - (long_slots+1)*sizeof(struct msdos_dir_entry);
517 if (!memcmp(de->name,MSDOS_DOT,11))
519 else if (!memcmp(de->name,MSDOS_DOTDOT,11)) {
520 inum = parent_ino(filp->f_dentry);
522 struct inode *tmp = fat_iget(sb, i_pos);
527 inum = iunique(sb, MSDOS_ROOT_INO);
531 bufuname[j] = 0x0000;
532 i = utf8 ? utf8_wcstombs(bufname, bufuname, sizeof(bufname))
533 : uni16_to_x8(bufname, bufuname, uni_xlate, nls_io);
536 if (!long_slots||shortnames) {
539 if (filldir(dirent, bufname, i, *furrfu, inum,
540 (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
543 unsigned char longname[275];
545 ? utf8_wcstombs(longname, unicode, sizeof(longname))
546 : uni16_to_x8(longname, unicode, uni_xlate,
549 memcpy(&longname[long_len+1], bufname, i);
552 if (filldir(dirent, longname, long_len, *furrfu, inum,
553 (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
567 free_page((unsigned long) unicode);
574 static int fat_readdir(struct file *filp, void *dirent, filldir_t filldir)
576 struct inode *inode = filp->f_dentry->d_inode;
577 return fat_readdirx(inode, filp, dirent, filldir, 0, 0);
580 struct fat_ioctl_filldir_callback {
581 struct dirent __user *dirent;
585 static int fat_ioctl_filldir(void *__buf, const char * name, int name_len,
586 loff_t offset, ino_t ino, unsigned int d_type)
588 struct fat_ioctl_filldir_callback *buf = __buf;
589 struct dirent __user *d1 = buf->dirent;
590 struct dirent __user *d2 = d1 + 1;
598 if ((name_len == 1 && name[0] == '.') ||
599 (name_len == 2 && name[0] == '.' && name[1] == '.')) {
606 if (len != name_len) {
607 slen = name_len - len;
608 if (copy_to_user(d2->d_name, name, len) ||
609 put_user(0, d2->d_name + len) ||
610 put_user(len, &d2->d_reclen) ||
611 put_user(ino, &d2->d_ino) ||
612 put_user(offset, &d2->d_off) ||
613 copy_to_user(d1->d_name, name+len+1, slen) ||
614 put_user(0, d1->d_name+slen) ||
615 put_user(slen, &d1->d_reclen))
618 if (put_user(0, d2->d_name) ||
619 put_user(0, &d2->d_reclen) ||
620 copy_to_user(d1->d_name, name, len) ||
621 put_user(0, d1->d_name+len) ||
622 put_user(len, &d1->d_reclen))
627 buf->result = -EFAULT;
631 static int fat_dir_ioctl(struct inode * inode, struct file * filp,
632 unsigned int cmd, unsigned long arg)
634 struct fat_ioctl_filldir_callback buf;
635 struct dirent __user *d1;
636 int ret, shortname, both;
639 case VFAT_IOCTL_READDIR_SHORT:
643 case VFAT_IOCTL_READDIR_BOTH:
651 d1 = (struct dirent __user *)arg;
652 if (!access_ok(VERIFY_WRITE, d1, sizeof(struct dirent[2])))
655 * Yes, we don't need this put_user() absolutely. However old
656 * code didn't return the right value. So, app use this value,
657 * in order to check whether it is EOF.
659 if (put_user(0, &d1->d_reclen))
666 if (!IS_DEADDIR(inode)) {
667 ret = fat_readdirx(inode, filp, &buf, fat_ioctl_filldir,
676 /* This assumes that size of cluster is above the 32*slots */
678 int fat_add_entries(struct inode *dir,int slots, struct buffer_head **bh,
679 struct msdos_dir_entry **de, loff_t *i_pos)
681 struct super_block *sb = dir->i_sb;
684 struct buffer_head *new_bh;
689 while (fat_get_entry(dir, &curr, bh, de, i_pos) > -1) {
690 /* check the maximum size of directory */
691 if (curr >= FAT_MAX_DIR_SIZE) {
696 if (IS_FREE((*de)->name)) {
704 if ((dir->i_ino == MSDOS_ROOT_INO) && (MSDOS_SB(sb)->fat_bits != 32))
706 new_bh = fat_extend_dir(dir);
708 return PTR_ERR(new_bh);
711 fat_get_entry(dir, &curr, bh, de, i_pos);
712 } while (++row < slots);
717 int fat_new_dir(struct inode *dir, struct inode *parent, int is_vfat)
719 struct buffer_head *bh;
720 struct msdos_dir_entry *de;
723 bh = fat_extend_dir(dir);
727 /* zeroed out, so... */
728 fat_date_unix2dos(dir->i_mtime.tv_sec,&time,&date);
729 de = (struct msdos_dir_entry*)&bh->b_data[0];
730 memcpy(de[0].name,MSDOS_DOT,MSDOS_NAME);
731 memcpy(de[1].name,MSDOS_DOTDOT,MSDOS_NAME);
732 de[0].attr = de[1].attr = ATTR_DIR;
733 de[0].time = de[1].time = time;
734 de[0].date = de[1].date = date;
735 if (is_vfat) { /* extra timestamps */
736 de[0].ctime = de[1].ctime = time;
737 de[0].adate = de[0].cdate =
738 de[1].adate = de[1].cdate = date;
740 de[0].start = CT_LE_W(MSDOS_I(dir)->i_logstart);
741 de[0].starthi = CT_LE_W(MSDOS_I(dir)->i_logstart>>16);
742 de[1].start = CT_LE_W(MSDOS_I(parent)->i_logstart);
743 de[1].starthi = CT_LE_W(MSDOS_I(parent)->i_logstart>>16);
744 mark_buffer_dirty(bh);
746 dir->i_atime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
747 mark_inode_dirty(dir);
752 static int fat_get_short_entry(struct inode *dir, loff_t *pos,
753 struct buffer_head **bh,
754 struct msdos_dir_entry **de, loff_t *i_pos)
756 while (fat_get_entry(dir, pos, bh, de, i_pos) >= 0) {
757 /* free entry or long name entry or volume label */
758 if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME))
764 /* See if directory is empty */
765 int fat_dir_empty(struct inode *dir)
767 struct buffer_head *bh;
768 struct msdos_dir_entry *de;
774 while (fat_get_short_entry(dir, &cpos, &bh, &de, &i_pos) >= 0) {
775 if (strncmp(de->name, MSDOS_DOT , MSDOS_NAME) &&
776 strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
786 * fat_subdirs counts the number of sub-directories of dir. It can be run
787 * on directories being created.
789 int fat_subdirs(struct inode *dir)
791 struct buffer_head *bh;
792 struct msdos_dir_entry *de;
798 while (fat_get_short_entry(dir, &cpos, &bh, &de, &i_pos) >= 0) {
799 if (de->attr & ATTR_DIR)
807 * Scans a directory for a given file (name points to its formatted name).
808 * Returns an error code or zero.
810 int fat_scan(struct inode *dir, const unsigned char *name,
811 struct buffer_head **bh, struct msdos_dir_entry **de,
818 while (fat_get_short_entry(dir, &cpos, bh, de, i_pos) >= 0) {
819 if (!strncmp((*de)->name, name, MSDOS_NAME))