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>
17 #include <linux/msdos_fs.h>
18 #include <linux/nls.h>
19 #include <linux/kernel.h>
20 #include <linux/errno.h>
21 #include <linux/stat.h>
22 #include <linux/string.h>
23 #include <linux/ioctl.h>
24 #include <linux/dirent.h>
26 #include <linux/ctype.h>
28 #include <asm/uaccess.h>
32 struct file_operations fat_dir_operations = {
33 read: generic_read_dir,
40 * Convert Unicode 16 to UTF8, translated Unicode, or ASCII.
41 * If uni_xlate is enabled and we can't get a 1:1 conversion, use a
42 * colon as an escape character since it is normally invalid on the vfat
43 * filesystem. The following four characters are the hexadecimal digits
44 * of Unicode value. This lets us do a full dump and restore of Unicode
45 * filenames. We could get into some trouble with long Unicode names,
46 * but ignore that right now.
47 * Ahem... Stack smashing in ring 0 isn't fun. Fixed.
50 uni16_to_x8(unsigned char *ascii, wchar_t *uni, int uni_xlate,
51 struct nls_table *nls)
54 unsigned char *op, nc;
63 if ( (charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE)) > 0) {
68 for (k = 4; k > 0; k--) {
70 op[k] = nc > 9 ? nc + ('a' - 10)
79 /* We have some slack there, so it's OK */
90 static void dump_de(struct msdos_dir_entry *de)
93 unsigned char *p = (unsigned char *) de;
96 for (i = 0; i < 32; i++, p++) {
103 static inline unsigned char
104 fat_tolower(struct nls_table *t, unsigned char c)
106 unsigned char nc = t->charset2lower[c];
112 fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
116 charlen = t->char2uni(c, clen, uni);
118 *uni = 0x003f; /* a question mark */
125 fat_short2lower_uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
130 charlen = t->char2uni(c, clen, &wc);
132 *uni = 0x003f; /* a question mark */
134 } else if (charlen <= 1) {
135 unsigned char nc = t->charset2lower[*c];
140 if ( (charlen = t->char2uni(&nc, 1, uni)) < 0) {
141 *uni = 0x003f; /* a question mark */
151 fat_strnicmp(struct nls_table *t, const unsigned char *s1,
152 const unsigned char *s2, int len)
155 if (fat_tolower(t, *s1++) != fat_tolower(t, *s2++))
162 fat_shortname2uni(struct nls_table *nls, char *buf, int buf_size,
163 wchar_t *uni_buf, unsigned short opt, int lower)
167 if (opt & VFAT_SFN_DISPLAY_LOWER)
168 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
169 else if (opt & VFAT_SFN_DISPLAY_WIN95)
170 len = fat_short2uni(nls, buf, buf_size, uni_buf);
171 else if (opt & VFAT_SFN_DISPLAY_WINNT) {
173 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
175 len = fat_short2uni(nls, buf, buf_size, uni_buf);
177 len = fat_short2uni(nls, buf, buf_size, uni_buf);
183 * Return values: negative -> error, 0 -> not found, positive -> found,
184 * value is the total amount of slots, including the shortname entry.
186 int fat_search_long(struct inode *inode, const char *name, int name_len,
187 int anycase, loff_t *spos, loff_t *lpos)
189 struct super_block *sb = inode->i_sb;
190 struct buffer_head *bh = NULL;
191 struct msdos_dir_entry *de;
192 struct nls_table *nls_io = MSDOS_SB(sb)->nls_io;
193 struct nls_table *nls_disk = MSDOS_SB(sb)->nls_disk;
194 wchar_t bufuname[14];
195 unsigned char xlate_len, long_slots;
196 wchar_t *unicode = NULL;
197 char work[8], bufname[260]; /* 256 + 4 */
198 int uni_xlate = MSDOS_SB(sb)->options.unicode_xlate;
199 int utf8 = MSDOS_SB(sb)->options.utf8;
200 unsigned short opt_shortname = MSDOS_SB(sb)->options.shortname;
201 int chl, i, j, last_u, res = 0;
202 loff_t i_pos, cpos = 0;
205 if (fat_get_entry(inode,&cpos,&bh,&de,&i_pos) == -1)
209 if (de->name[0] == (__s8) DELETED_FLAG)
211 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
213 if (de->attr != ATTR_EXT && IS_FREE(de->name))
215 if (de->attr == ATTR_EXT) {
216 struct msdos_dir_slot *ds;
221 unsigned char alias_checksum;
224 unicode = (wchar_t *)
225 __get_free_page(GFP_KERNEL);
233 ds = (struct msdos_dir_slot *) de;
238 if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */
241 alias_checksum = ds->alias_checksum;
249 fat16_towchar(unicode + offset, ds->name0_4, 5);
250 fat16_towchar(unicode + offset + 5, ds->name5_10, 6);
251 fat16_towchar(unicode + offset + 11, ds->name11_12, 2);
254 unicode[offset + 13] = 0;
256 if (fat_get_entry(inode,&cpos,&bh,&de,&i_pos)<0)
260 ds = (struct msdos_dir_slot *) de;
261 if (ds->attr != ATTR_EXT)
263 if ((ds->id & ~0x40) != slot)
265 if (ds->alias_checksum != alias_checksum)
268 if (de->name[0] == (__s8) DELETED_FLAG)
270 if (de->attr == ATTR_EXT)
272 if (IS_FREE(de->name) || (de->attr & ATTR_VOLUME))
274 for (sum = 0, i = 0; i < 11; i++)
275 sum = (((sum&1)<<7)|((sum&0xfe)>>1)) + de->name[i];
276 if (sum != alias_checksum)
280 for (i = 0; i < 8; i++) {
281 /* see namei.c, msdos_format_name */
282 if (de->name[i] == 0x05)
285 work[i] = de->name[i];
287 for (i = 0, j = 0, last_u = 0; i < 8;) {
289 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
290 &bufuname[j++], opt_shortname,
291 de->lcase & CASE_LOWER_BASE);
301 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
302 for (i = 0; i < 3;) {
303 if (!de->ext[i]) break;
304 chl = fat_shortname2uni(nls_disk, &de->ext[i], 3 - i,
305 &bufuname[j++], opt_shortname,
306 de->lcase & CASE_LOWER_EXT);
308 if (de->ext[i] != ' ')
318 bufuname[last_u] = 0x0000;
320 ?utf8_wcstombs(bufname, bufuname, sizeof(bufname))
321 :uni16_to_x8(bufname, bufuname, uni_xlate, nls_io);
322 if (xlate_len == name_len)
323 if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
324 (anycase && !fat_strnicmp(nls_io, name, bufname,
330 ?utf8_wcstombs(bufname, unicode, sizeof(bufname))
331 :uni16_to_x8(bufname, unicode, uni_xlate, nls_io);
332 if (xlate_len != name_len)
334 if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
335 (anycase && !fat_strnicmp(nls_io, name, bufname,
342 res = long_slots + 1;
343 *spos = cpos - sizeof(struct msdos_dir_entry);
344 *lpos = cpos - res*sizeof(struct msdos_dir_entry);
348 free_page((unsigned long) unicode);
353 static int fat_readdirx(struct inode *inode, struct file *filp, void *dirent,
354 filldir_t filldir, int shortnames, int both)
356 struct super_block *sb = inode->i_sb;
357 struct buffer_head *bh;
358 struct msdos_dir_entry *de;
359 struct nls_table *nls_io = MSDOS_SB(sb)->nls_io;
360 struct nls_table *nls_disk = MSDOS_SB(sb)->nls_disk;
361 wchar_t bufuname[14];
362 unsigned char long_slots;
363 wchar_t *unicode = NULL;
364 char c, work[8], bufname[56], *ptname = bufname;
365 unsigned long lpos, dummy, *furrfu = &lpos;
366 int uni_xlate = MSDOS_SB(sb)->options.unicode_xlate;
367 int isvfat = MSDOS_SB(sb)->options.isvfat;
368 int utf8 = MSDOS_SB(sb)->options.utf8;
369 int nocase = MSDOS_SB(sb)->options.nocase;
370 unsigned short opt_shortname = MSDOS_SB(sb)->options.shortname;
372 int chi, chl, i, i2, j, last, last_u, dotoffset = 0;
376 /* Fake . and .. for the root directory. */
377 if (inode->i_ino == MSDOS_ROOT_INO) {
379 if (filldir(dirent, "..", cpos+1, cpos, MSDOS_ROOT_INO, DT_DIR) < 0)
390 if (cpos & (sizeof(struct msdos_dir_entry)-1))
396 if (fat_get_entry(inode,&cpos,&bh,&de,&i_pos) == -1)
398 /* Check for long filename entry */
400 if (de->name[0] == (__s8) DELETED_FLAG)
402 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
404 if (de->attr != ATTR_EXT && IS_FREE(de->name))
407 if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
411 if (isvfat && de->attr == ATTR_EXT) {
412 struct msdos_dir_slot *ds;
417 unsigned char alias_checksum;
420 unicode = (wchar_t *)
421 __get_free_page(GFP_KERNEL);
430 ds = (struct msdos_dir_slot *) de;
435 if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */
438 alias_checksum = ds->alias_checksum;
446 fat16_towchar(unicode + offset, ds->name0_4, 5);
447 fat16_towchar(unicode + offset + 5, ds->name5_10, 6);
448 fat16_towchar(unicode + offset + 11, ds->name11_12, 2);
451 unicode[offset + 13] = 0;
453 if (fat_get_entry(inode,&cpos,&bh,&de,&i_pos) == -1)
457 ds = (struct msdos_dir_slot *) de;
458 if (ds->attr != ATTR_EXT)
459 goto RecEnd; /* XXX */
460 if ((ds->id & ~0x40) != slot)
462 if (ds->alias_checksum != alias_checksum)
465 if (de->name[0] == (__s8) DELETED_FLAG)
467 if (de->attr == ATTR_EXT)
469 if (IS_FREE(de->name) || (de->attr & ATTR_VOLUME))
471 for (sum = 0, i = 0; i < 11; i++)
472 sum = (((sum&1)<<7)|((sum&0xfe)>>1)) + de->name[i];
473 if (sum != alias_checksum)
477 if ((de->attr & ATTR_HIDDEN) && MSDOS_SB(sb)->options.dotsOK) {
482 for (i = 0; i < 8; i++) {
483 /* see namei.c, msdos_format_name */
484 if (de->name[i] == 0x05)
487 work[i] = de->name[i];
489 for (i = 0, j = 0, last = 0, last_u = 0; i < 8;) {
490 if (!(c = work[i])) break;
491 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
492 &bufuname[j++], opt_shortname,
493 de->lcase & CASE_LOWER_BASE);
495 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
502 for (chi = 0; chi < chl && i < 8; chi++) {
510 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
512 for (i2 = 0; i2 < 3;) {
513 if (!(c = de->ext[i2])) break;
514 chl = fat_shortname2uni(nls_disk, &de->ext[i2], 3 - i2,
515 &bufuname[j++], opt_shortname,
516 de->lcase & CASE_LOWER_EXT);
519 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
526 for (chi = 0; chi < chl && i2 < 3; chi++) {
527 ptname[i++] = de->ext[i2++];
535 i = last + dotoffset;
538 lpos = cpos - (long_slots+1)*sizeof(struct msdos_dir_entry);
539 if (!memcmp(de->name,MSDOS_DOT,11))
541 else if (!memcmp(de->name,MSDOS_DOTDOT,11)) {
542 /* inum = fat_parent_ino(inode,0); */
543 inum = filp->f_dentry->d_parent->d_inode->i_ino;
545 struct inode *tmp = fat_iget(sb, i_pos);
550 inum = iunique(sb, MSDOS_ROOT_INO);
554 bufuname[j] = 0x0000;
555 i = utf8 ? utf8_wcstombs(bufname, bufuname, sizeof(bufname))
556 : uni16_to_x8(bufname, bufuname, uni_xlate, nls_io);
559 if (!long_slots||shortnames) {
562 if (filldir(dirent, bufname, i, *furrfu, inum,
563 (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
568 ? utf8_wcstombs(longname, unicode, sizeof(longname))
569 : uni16_to_x8(longname, unicode, uni_xlate,
572 memcpy(&longname[long_len+1], bufname, i);
575 if (filldir(dirent, longname, long_len, *furrfu, inum,
576 (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
590 free_page((unsigned long) unicode);
595 int fat_readdir(struct file *filp, void *dirent, filldir_t filldir)
597 struct inode *inode = filp->f_dentry->d_inode;
598 return fat_readdirx(inode, filp, dirent, filldir, 0, 0);
601 static int vfat_ioctl_fill(
609 struct dirent *d1 = (struct dirent *)buf;
610 struct dirent *d2 = d1 + 1;
614 get_user(len, &d1->d_reclen);
619 if ((name_len == 1 && name[0] == '.') ||
620 (name_len == 2 && name[0] == '.' && name[1] == '.')) {
627 if (len != name_len) {
628 copy_to_user(d2->d_name, name, len);
629 put_user(0, d2->d_name + len);
630 put_user(len, &d2->d_reclen);
631 put_user(ino, &d2->d_ino);
632 put_user(offset, &d2->d_off);
633 slen = name_len - len;
634 copy_to_user(d1->d_name, name+len+1, slen);
635 put_user(0, d1->d_name+slen);
636 put_user(slen, &d1->d_reclen);
638 put_user(0, d2->d_name);
639 put_user(0, &d2->d_reclen);
640 copy_to_user(d1->d_name, name, len);
641 put_user(0, d1->d_name+len);
642 put_user(len, &d1->d_reclen);
644 PRINTK(("FAT d1=%p d2=%p len=%d, name_len=%d\n",
645 d1, d2, len, name_len));
650 int fat_dir_ioctl(struct inode * inode, struct file * filp,
651 unsigned int cmd, unsigned long arg)
655 * We want to provide an interface for Samba to be able
656 * to get the short filename for a given long filename.
657 * Samba should use this ioctl instead of readdir() to
658 * get the information it needs.
661 case VFAT_IOCTL_READDIR_BOTH: {
662 struct dirent *d1 = (struct dirent *)arg;
663 err = verify_area(VERIFY_WRITE, d1, sizeof(struct dirent[2]));
666 put_user(0, &d1->d_reclen);
667 return fat_readdirx(inode,filp,(void *)arg,
668 vfat_ioctl_fill, 0, 1);
670 case VFAT_IOCTL_READDIR_SHORT: {
671 struct dirent *d1 = (struct dirent *)arg;
672 put_user(0, &d1->d_reclen);
673 err = verify_area(VERIFY_WRITE, d1, sizeof(struct dirent[2]));
676 return fat_readdirx(inode,filp,(void *)arg,
677 vfat_ioctl_fill, 1, 1);
680 /* forward ioctl to CVF extension */
681 if (MSDOS_SB(inode->i_sb)->cvf_format &&
682 MSDOS_SB(inode->i_sb)->cvf_format->cvf_dir_ioctl)
683 return MSDOS_SB(inode->i_sb)->cvf_format
684 ->cvf_dir_ioctl(inode,filp,cmd,arg);
691 /***** See if directory is empty */
692 int fat_dir_empty(struct inode *dir)
695 struct buffer_head *bh;
696 struct msdos_dir_entry *de;
701 while (fat_get_entry(dir,&pos,&bh,&de,&i_pos) > -1) {
702 /* Ignore vfat longname entries */
703 if (de->attr == ATTR_EXT)
705 if (!IS_FREE(de->name) &&
706 strncmp(de->name,MSDOS_DOT , MSDOS_NAME) &&
707 strncmp(de->name,MSDOS_DOTDOT, MSDOS_NAME)) {
713 fat_brelse(dir->i_sb, bh);
718 /* This assumes that size of cluster is above the 32*slots */
720 int fat_add_entries(struct inode *dir,int slots, struct buffer_head **bh,
721 struct msdos_dir_entry **de, loff_t *i_pos)
723 struct super_block *sb = dir->i_sb;
726 struct buffer_head *new_bh;
731 while (fat_get_entry(dir,&curr,bh,de,i_pos) > -1) {
732 if (IS_FREE((*de)->name)) {
740 if ((dir->i_ino == MSDOS_ROOT_INO) && (MSDOS_SB(sb)->fat_bits != 32))
742 new_bh = fat_extend_dir(dir);
745 fat_brelse(sb, new_bh);
746 do fat_get_entry(dir,&curr,bh,de,i_pos); while (++row<slots);
750 int fat_new_dir(struct inode *dir, struct inode *parent, int is_vfat)
752 struct super_block *sb = dir->i_sb;
753 struct buffer_head *bh;
754 struct msdos_dir_entry *de;
757 if ((bh = fat_extend_dir(dir)) == NULL) return -ENOSPC;
758 /* zeroed out, so... */
759 fat_date_unix2dos(dir->i_mtime,&time,&date);
760 de = (struct msdos_dir_entry*)&bh->b_data[0];
761 memcpy(de[0].name,MSDOS_DOT,MSDOS_NAME);
762 memcpy(de[1].name,MSDOS_DOTDOT,MSDOS_NAME);
763 de[0].attr = de[1].attr = ATTR_DIR;
764 de[0].time = de[1].time = CT_LE_W(time);
765 de[0].date = de[1].date = CT_LE_W(date);
766 if (is_vfat) { /* extra timestamps */
767 de[0].ctime = de[1].ctime = CT_LE_W(time);
768 de[0].adate = de[0].cdate =
769 de[1].adate = de[1].cdate = CT_LE_W(date);
771 de[0].start = CT_LE_W(MSDOS_I(dir)->i_logstart);
772 de[0].starthi = CT_LE_W(MSDOS_I(dir)->i_logstart>>16);
773 de[1].start = CT_LE_W(MSDOS_I(parent)->i_logstart);
774 de[1].starthi = CT_LE_W(MSDOS_I(parent)->i_logstart>>16);
775 fat_mark_buffer_dirty(sb, bh);
777 dir->i_atime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
778 mark_inode_dirty(dir);
784 * Overrides for Emacs so that we follow Linus's tabbing style.
785 * Emacs will notice this stuff at the end of the file and automatically
786 * adjust the settings for this buffer only. This must remain at the end
788 * ---------------------------------------------------------------------------
791 * c-brace-imaginary-offset: 0
793 * c-argdecl-indent: 8
795 * c-continued-statement-offset: 8
796 * c-continued-brace-offset: 0