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 <linux/compat.h>
24 #include <asm/uaccess.h>
26 static inline loff_t fat_make_i_pos(struct super_block *sb,
27 struct buffer_head *bh,
28 struct msdos_dir_entry *de)
30 return ((loff_t)bh->b_blocknr << MSDOS_SB(sb)->dir_per_block_bits)
31 | (de - (struct msdos_dir_entry *)bh->b_data);
34 static inline void fat_dir_readahead(struct inode *dir, sector_t iblock,
37 struct super_block *sb = dir->i_sb;
38 struct msdos_sb_info *sbi = MSDOS_SB(sb);
39 struct buffer_head *bh;
42 /* This is not a first sector of cluster, or sec_per_clus == 1 */
43 if ((iblock & (sbi->sec_per_clus - 1)) || sbi->sec_per_clus == 1)
45 /* root dir of FAT12/FAT16 */
46 if ((sbi->fat_bits != 32) && (dir->i_ino == MSDOS_ROOT_INO))
49 bh = sb_find_get_block(sb, phys);
50 if (bh == NULL || !buffer_uptodate(bh)) {
51 for (sec = 0; sec < sbi->sec_per_clus; sec++)
52 sb_breadahead(sb, phys + sec);
57 /* Returns the inode number of the directory entry at offset pos. If bh is
58 non-NULL, it is brelse'd before. Pos is incremented. The buffer header is
60 AV. Most often we do it item-by-item. Makes sense to optimize.
61 AV. OK, there we go: if both bh and de are non-NULL we assume that we just
62 AV. want the next entry (took one explicit de=NULL in vfat/namei.c).
63 AV. It's done in fat_get_entry() (inlined), here the slow case lives.
64 AV. Additionally, when we return -1 (i.e. reached the end of directory)
67 static int fat__get_entry(struct inode *dir, loff_t *pos,
68 struct buffer_head **bh, struct msdos_dir_entry **de)
70 struct super_block *sb = dir->i_sb;
71 sector_t phys, iblock;
72 unsigned long mapped_blocks;
80 iblock = *pos >> sb->s_blocksize_bits;
81 err = fat_bmap(dir, iblock, &phys, &mapped_blocks);
83 return -1; /* beyond EOF or error */
85 fat_dir_readahead(dir, iblock, phys);
87 *bh = sb_bread(sb, phys);
89 printk(KERN_ERR "FAT: Directory bread(block %llu) failed\n",
90 (unsigned long long)phys);
92 *pos = (iblock + 1) << sb->s_blocksize_bits;
96 offset = *pos & (sb->s_blocksize - 1);
97 *pos += sizeof(struct msdos_dir_entry);
98 *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
103 static inline int fat_get_entry(struct inode *dir, loff_t *pos,
104 struct buffer_head **bh,
105 struct msdos_dir_entry **de)
107 /* Fast stuff first */
109 (*de - (struct msdos_dir_entry *)(*bh)->b_data) < MSDOS_SB(dir->i_sb)->dir_per_block - 1) {
110 *pos += sizeof(struct msdos_dir_entry);
114 return fat__get_entry(dir, pos, bh, de);
118 * Convert Unicode 16 to UTF-8, translated Unicode, or ASCII.
119 * If uni_xlate is enabled and we can't get a 1:1 conversion, use a
120 * colon as an escape character since it is normally invalid on the vfat
121 * filesystem. The following four characters are the hexadecimal digits
122 * of Unicode value. This lets us do a full dump and restore of Unicode
123 * filenames. We could get into some trouble with long Unicode names,
124 * but ignore that right now.
125 * Ahem... Stack smashing in ring 0 isn't fun. Fixed.
127 static int uni16_to_x8(unsigned char *ascii, wchar_t *uni, int len,
128 int uni_xlate, struct nls_table *nls)
131 unsigned char *op, nc;
138 while (*ip && ((len - NLS_MAX_CHARSET_SIZE) > 0)) {
140 if ( (charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE)) > 0) {
144 if (uni_xlate == 1) {
146 for (k = 4; k > 0; k--) {
148 op[k] = nc > 9 ? nc + ('a' - 10)
162 printk(KERN_WARNING "FAT: filename was truncated while "
171 fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
175 charlen = t->char2uni(c, clen, uni);
177 *uni = 0x003f; /* a question mark */
184 fat_short2lower_uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
189 charlen = t->char2uni(c, clen, &wc);
191 *uni = 0x003f; /* a question mark */
193 } else if (charlen <= 1) {
194 unsigned char nc = t->charset2lower[*c];
199 if ( (charlen = t->char2uni(&nc, 1, uni)) < 0) {
200 *uni = 0x003f; /* a question mark */
210 fat_shortname2uni(struct nls_table *nls, unsigned char *buf, int buf_size,
211 wchar_t *uni_buf, unsigned short opt, int lower)
215 if (opt & VFAT_SFN_DISPLAY_LOWER)
216 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
217 else if (opt & VFAT_SFN_DISPLAY_WIN95)
218 len = fat_short2uni(nls, buf, buf_size, uni_buf);
219 else if (opt & VFAT_SFN_DISPLAY_WINNT) {
221 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
223 len = fat_short2uni(nls, buf, buf_size, uni_buf);
225 len = fat_short2uni(nls, buf, buf_size, uni_buf);
230 enum { PARSE_INVALID = 1, PARSE_NOT_LONGNAME, PARSE_EOF, };
233 * fat_parse_long - Parse extended directory entry.
235 * This function returns zero on success, negative value on error, or one of
238 * %PARSE_INVALID - Directory entry is invalid.
239 * %PARSE_NOT_LONGNAME - Directory entry does not contain longname.
240 * %PARSE_EOF - Directory has no more entries.
242 static int fat_parse_long(struct inode *dir, loff_t *pos,
243 struct buffer_head **bh, struct msdos_dir_entry **de,
244 wchar_t **unicode, unsigned char *nr_slots)
246 struct msdos_dir_slot *ds;
247 unsigned char id, slot, slots, alias_checksum;
250 *unicode = __getname();
258 ds = (struct msdos_dir_slot *)*de;
261 return PARSE_INVALID;
263 if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */
264 return PARSE_INVALID;
266 alias_checksum = ds->alias_checksum;
274 fat16_towchar(*unicode + offset, ds->name0_4, 5);
275 fat16_towchar(*unicode + offset + 5, ds->name5_10, 6);
276 fat16_towchar(*unicode + offset + 11, ds->name11_12, 2);
279 (*unicode)[offset + 13] = 0;
280 if (fat_get_entry(dir, pos, bh, de) < 0)
284 ds = (struct msdos_dir_slot *)*de;
285 if (ds->attr != ATTR_EXT)
286 return PARSE_NOT_LONGNAME;
287 if ((ds->id & ~0x40) != slot)
289 if (ds->alias_checksum != alias_checksum)
292 if ((*de)->name[0] == DELETED_FLAG)
293 return PARSE_INVALID;
294 if ((*de)->attr == ATTR_EXT)
296 if (IS_FREE((*de)->name) || ((*de)->attr & ATTR_VOLUME))
297 return PARSE_INVALID;
298 if (fat_checksum((*de)->name) != alias_checksum)
305 * Return values: negative -> error, 0 -> not found, positive -> found,
306 * value is the total amount of slots, including the shortname entry.
308 int fat_search_long(struct inode *inode, const unsigned char *name,
309 int name_len, struct fat_slot_info *sinfo)
311 struct super_block *sb = inode->i_sb;
312 struct msdos_sb_info *sbi = MSDOS_SB(sb);
313 struct buffer_head *bh = NULL;
314 struct msdos_dir_entry *de;
315 struct nls_table *nls_io = sbi->nls_io;
316 struct nls_table *nls_disk = sbi->nls_disk;
317 wchar_t bufuname[14];
318 unsigned char nr_slots;
320 wchar_t *unicode = NULL;
321 unsigned char work[MSDOS_NAME];
322 unsigned char *bufname = NULL;
323 int uni_xlate = sbi->options.unicode_xlate;
324 int utf8 = sbi->options.utf8;
325 int anycase = (sbi->options.name_check != 's');
326 unsigned short opt_shortname = sbi->options.shortname;
328 int chl, i, j, last_u, err;
330 bufname = __getname();
336 if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
340 if (de->name[0] == DELETED_FLAG)
342 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
344 if (de->attr != ATTR_EXT && IS_FREE(de->name))
346 if (de->attr == ATTR_EXT) {
347 int status = fat_parse_long(inode, &cpos, &bh, &de,
348 &unicode, &nr_slots);
351 else if (status == PARSE_INVALID)
353 else if (status == PARSE_NOT_LONGNAME)
355 else if (status == PARSE_EOF)
359 memcpy(work, de->name, sizeof(de->name));
360 /* see namei.c, msdos_format_name */
363 for (i = 0, j = 0, last_u = 0; i < 8;) {
366 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
367 &bufuname[j++], opt_shortname,
368 de->lcase & CASE_LOWER_BASE);
378 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
379 for (i = 8; i < MSDOS_NAME;) {
382 chl = fat_shortname2uni(nls_disk, &work[i],
384 &bufuname[j++], opt_shortname,
385 de->lcase & CASE_LOWER_EXT);
397 bufuname[last_u] = 0x0000;
399 ?utf8_wcstombs(bufname, bufuname, PATH_MAX)
400 :uni16_to_x8(bufname, bufuname, PATH_MAX, uni_xlate, nls_io);
401 if (xlate_len == name_len)
402 if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
403 (anycase && !nls_strnicmp(nls_io, name, bufname,
409 ?utf8_wcstombs(bufname, unicode, PATH_MAX)
410 :uni16_to_x8(bufname, unicode, PATH_MAX, uni_xlate, nls_io);
411 if (xlate_len != name_len)
413 if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
414 (anycase && !nls_strnicmp(nls_io, name, bufname,
421 nr_slots++; /* include the de */
422 sinfo->slot_off = cpos - nr_slots * sizeof(*de);
423 sinfo->nr_slots = nr_slots;
426 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
437 EXPORT_SYMBOL_GPL(fat_search_long);
439 struct fat_ioctl_filldir_callback {
443 const char *longname;
445 const char *shortname;
449 static int __fat_readdir(struct inode *inode, struct file *filp, void *dirent,
450 filldir_t filldir, int short_only, int both)
452 struct super_block *sb = inode->i_sb;
453 struct msdos_sb_info *sbi = MSDOS_SB(sb);
454 struct buffer_head *bh;
455 struct msdos_dir_entry *de;
456 struct nls_table *nls_io = sbi->nls_io;
457 struct nls_table *nls_disk = sbi->nls_disk;
458 unsigned char long_slots;
459 const char *fill_name;
461 wchar_t bufuname[14];
462 wchar_t *unicode = NULL;
463 unsigned char c, work[MSDOS_NAME], bufname[56], *ptname = bufname;
464 unsigned long lpos, dummy, *furrfu = &lpos;
465 int uni_xlate = sbi->options.unicode_xlate;
466 int isvfat = sbi->options.isvfat;
467 int utf8 = sbi->options.utf8;
468 int nocase = sbi->options.nocase;
469 unsigned short opt_shortname = sbi->options.shortname;
471 int chi, chl, i, i2, j, last, last_u, dotoffset = 0;
478 /* Fake . and .. for the root directory. */
479 if (inode->i_ino == MSDOS_ROOT_INO) {
481 if (filldir(dirent, "..", cpos+1, cpos, MSDOS_ROOT_INO, DT_DIR) < 0)
492 if (cpos & (sizeof(struct msdos_dir_entry)-1)) {
499 if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
503 /* Check for long filename entry */
505 if (de->name[0] == DELETED_FLAG)
507 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
509 if (de->attr != ATTR_EXT && IS_FREE(de->name))
512 if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
516 if (isvfat && de->attr == ATTR_EXT) {
517 int status = fat_parse_long(inode, &cpos, &bh, &de,
518 &unicode, &long_slots);
523 } else if (status == PARSE_INVALID)
525 else if (status == PARSE_NOT_LONGNAME)
527 else if (status == PARSE_EOF)
531 if (sbi->options.dotsOK) {
534 if (de->attr & ATTR_HIDDEN) {
540 memcpy(work, de->name, sizeof(de->name));
541 /* see namei.c, msdos_format_name */
544 for (i = 0, j = 0, last = 0, last_u = 0; i < 8;) {
547 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
548 &bufuname[j++], opt_shortname,
549 de->lcase & CASE_LOWER_BASE);
551 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
558 for (chi = 0; chi < chl && i < 8; chi++) {
566 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
568 for (i2 = 8; i2 < MSDOS_NAME;) {
571 chl = fat_shortname2uni(nls_disk, &work[i2], MSDOS_NAME - i2,
572 &bufuname[j++], opt_shortname,
573 de->lcase & CASE_LOWER_EXT);
576 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
583 for (chi = 0; chi < chl && i2 < MSDOS_NAME; chi++) {
584 ptname[i++] = work[i2++];
592 i = last + dotoffset;
595 lpos = cpos - (long_slots+1)*sizeof(struct msdos_dir_entry);
596 if (!memcmp(de->name, MSDOS_DOT, MSDOS_NAME))
598 else if (!memcmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
599 inum = parent_ino(filp->f_path.dentry);
601 loff_t i_pos = fat_make_i_pos(sb, bh, de);
602 struct inode *tmp = fat_iget(sb, i_pos);
607 inum = iunique(sb, MSDOS_ROOT_INO);
611 bufuname[j] = 0x0000;
612 i = utf8 ? utf8_wcstombs(bufname, bufuname, sizeof(bufname))
613 : uni16_to_x8(bufname, bufuname, sizeof(bufname), uni_xlate, nls_io);
618 if (!short_only && long_slots) {
619 /* convert the unicode long name. 261 is maximum size
620 * of unicode buffer. (13 * slots + nul) */
621 void *longname = unicode + 261;
622 int buf_size = PATH_MAX - (261 * sizeof(unicode[0]));
624 ? utf8_wcstombs(longname, unicode, buf_size)
625 : uni16_to_x8(longname, unicode, buf_size, uni_xlate, nls_io);
628 fill_name = longname;
631 /* hack for fat_ioctl_filldir() */
632 struct fat_ioctl_filldir_callback *p = dirent;
634 p->longname = longname;
635 p->long_len = long_len;
636 p->shortname = bufname;
642 if (filldir(dirent, fill_name, fill_len, *furrfu, inum,
643 (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
661 static int fat_readdir(struct file *filp, void *dirent, filldir_t filldir)
663 struct inode *inode = filp->f_path.dentry->d_inode;
664 return __fat_readdir(inode, filp, dirent, filldir, 0, 0);
667 #define FAT_IOCTL_FILLDIR_FUNC(func, dirent_type) \
668 static int func(void *__buf, const char *name, int name_len, \
669 loff_t offset, u64 ino, unsigned int d_type) \
671 struct fat_ioctl_filldir_callback *buf = __buf; \
672 struct dirent_type __user *d1 = buf->dirent; \
673 struct dirent_type __user *d2 = d1 + 1; \
679 if (name != NULL) { \
680 /* dirent has only short name */ \
681 if (name_len >= sizeof(d1->d_name)) \
682 name_len = sizeof(d1->d_name) - 1; \
684 if (put_user(0, d2->d_name) || \
685 put_user(0, &d2->d_reclen) || \
686 copy_to_user(d1->d_name, name, name_len) || \
687 put_user(0, d1->d_name + name_len) || \
688 put_user(name_len, &d1->d_reclen)) \
691 /* dirent has short and long name */ \
692 const char *longname = buf->longname; \
693 int long_len = buf->long_len; \
694 const char *shortname = buf->shortname; \
695 int short_len = buf->short_len; \
697 if (long_len >= sizeof(d1->d_name)) \
698 long_len = sizeof(d1->d_name) - 1; \
699 if (short_len >= sizeof(d1->d_name)) \
700 short_len = sizeof(d1->d_name) - 1; \
702 if (copy_to_user(d2->d_name, longname, long_len) || \
703 put_user(0, d2->d_name + long_len) || \
704 put_user(long_len, &d2->d_reclen) || \
705 put_user(ino, &d2->d_ino) || \
706 put_user(offset, &d2->d_off) || \
707 copy_to_user(d1->d_name, shortname, short_len) || \
708 put_user(0, d1->d_name + short_len) || \
709 put_user(short_len, &d1->d_reclen)) \
714 buf->result = -EFAULT; \
718 FAT_IOCTL_FILLDIR_FUNC(fat_ioctl_filldir, dirent)
720 static int fat_ioctl_readdir(struct inode *inode, struct file *filp,
721 void __user *dirent, filldir_t filldir,
722 int short_only, int both)
724 struct fat_ioctl_filldir_callback buf;
729 mutex_lock(&inode->i_mutex);
731 if (!IS_DEADDIR(inode)) {
732 ret = __fat_readdir(inode, filp, &buf, filldir,
735 mutex_unlock(&inode->i_mutex);
741 static int fat_dir_ioctl(struct inode *inode, struct file *filp,
742 unsigned int cmd, unsigned long arg)
744 struct dirent __user *d1 = (struct dirent __user *)arg;
745 int short_only, both;
748 case VFAT_IOCTL_READDIR_SHORT:
752 case VFAT_IOCTL_READDIR_BOTH:
757 return fat_generic_ioctl(inode, filp, cmd, arg);
760 if (!access_ok(VERIFY_WRITE, d1, sizeof(struct dirent[2])))
763 * Yes, we don't need this put_user() absolutely. However old
764 * code didn't return the right value. So, app use this value,
765 * in order to check whether it is EOF.
767 if (put_user(0, &d1->d_reclen))
770 return fat_ioctl_readdir(inode, filp, d1, fat_ioctl_filldir,
775 #define VFAT_IOCTL_READDIR_BOTH32 _IOR('r', 1, struct compat_dirent[2])
776 #define VFAT_IOCTL_READDIR_SHORT32 _IOR('r', 2, struct compat_dirent[2])
778 FAT_IOCTL_FILLDIR_FUNC(fat_compat_ioctl_filldir, compat_dirent)
780 static long fat_compat_dir_ioctl(struct file *filp, unsigned cmd,
783 struct inode *inode = filp->f_path.dentry->d_inode;
784 struct compat_dirent __user *d1 = compat_ptr(arg);
785 int short_only, both;
788 case VFAT_IOCTL_READDIR_SHORT32:
792 case VFAT_IOCTL_READDIR_BOTH32:
800 if (!access_ok(VERIFY_WRITE, d1, sizeof(struct compat_dirent[2])))
803 * Yes, we don't need this put_user() absolutely. However old
804 * code didn't return the right value. So, app use this value,
805 * in order to check whether it is EOF.
807 if (put_user(0, &d1->d_reclen))
810 return fat_ioctl_readdir(inode, filp, d1, fat_compat_ioctl_filldir,
813 #endif /* CONFIG_COMPAT */
815 const struct file_operations fat_dir_operations = {
816 .read = generic_read_dir,
817 .readdir = fat_readdir,
818 .ioctl = fat_dir_ioctl,
820 .compat_ioctl = fat_compat_dir_ioctl,
825 static int fat_get_short_entry(struct inode *dir, loff_t *pos,
826 struct buffer_head **bh,
827 struct msdos_dir_entry **de)
829 while (fat_get_entry(dir, pos, bh, de) >= 0) {
830 /* free entry or long name entry or volume label */
831 if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME))
838 * The ".." entry can not provide the "struct fat_slot_info" informations
839 * for inode. So, this function provide the some informations only.
841 int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh,
842 struct msdos_dir_entry **de, loff_t *i_pos)
848 while (fat_get_short_entry(dir, &offset, bh, de) >= 0) {
849 if (!strncmp((*de)->name, MSDOS_DOTDOT, MSDOS_NAME)) {
850 *i_pos = fat_make_i_pos(dir->i_sb, *bh, *de);
857 EXPORT_SYMBOL_GPL(fat_get_dotdot_entry);
859 /* See if directory is empty */
860 int fat_dir_empty(struct inode *dir)
862 struct buffer_head *bh;
863 struct msdos_dir_entry *de;
869 while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
870 if (strncmp(de->name, MSDOS_DOT , MSDOS_NAME) &&
871 strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
880 EXPORT_SYMBOL_GPL(fat_dir_empty);
883 * fat_subdirs counts the number of sub-directories of dir. It can be run
884 * on directories being created.
886 int fat_subdirs(struct inode *dir)
888 struct buffer_head *bh;
889 struct msdos_dir_entry *de;
895 while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
896 if (de->attr & ATTR_DIR)
904 * Scans a directory for a given file (name points to its formatted name).
905 * Returns an error code or zero.
907 int fat_scan(struct inode *dir, const unsigned char *name,
908 struct fat_slot_info *sinfo)
910 struct super_block *sb = dir->i_sb;
914 while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh,
916 if (!strncmp(sinfo->de->name, name, MSDOS_NAME)) {
917 sinfo->slot_off -= sizeof(*sinfo->de);
919 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
926 EXPORT_SYMBOL_GPL(fat_scan);
928 static int __fat_remove_entries(struct inode *dir, loff_t pos, int nr_slots)
930 struct super_block *sb = dir->i_sb;
931 struct buffer_head *bh;
932 struct msdos_dir_entry *de, *endp;
933 int err = 0, orig_slots;
937 if (fat_get_entry(dir, &pos, &bh, &de) < 0) {
942 orig_slots = nr_slots;
943 endp = (struct msdos_dir_entry *)(bh->b_data + sb->s_blocksize);
944 while (nr_slots && de < endp) {
945 de->name[0] = DELETED_FLAG;
949 mark_buffer_dirty(bh);
951 err = sync_dirty_buffer(bh);
956 /* pos is *next* de's position, so this does `- sizeof(de)' */
957 pos += ((orig_slots - nr_slots) * sizeof(*de)) - sizeof(*de);
963 int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo)
965 struct msdos_dir_entry *de;
966 struct buffer_head *bh;
967 int err = 0, nr_slots;
970 * First stage: Remove the shortname. By this, the directory
973 nr_slots = sinfo->nr_slots;
978 while (nr_slots && de >= (struct msdos_dir_entry *)bh->b_data) {
979 de->name[0] = DELETED_FLAG;
983 mark_buffer_dirty(bh);
985 err = sync_dirty_buffer(bh);
993 * Second stage: remove the remaining longname slots.
994 * (This directory entry is already removed, and so return
997 err = __fat_remove_entries(dir, sinfo->slot_off, nr_slots);
1000 "FAT: Couldn't remove the long name slots\n");
1004 dir->i_mtime = dir->i_atime = CURRENT_TIME_SEC;
1005 if (IS_DIRSYNC(dir))
1006 (void)fat_sync_inode(dir);
1008 mark_inode_dirty(dir);
1013 EXPORT_SYMBOL_GPL(fat_remove_entries);
1015 static int fat_zeroed_cluster(struct inode *dir, sector_t blknr, int nr_used,
1016 struct buffer_head **bhs, int nr_bhs)
1018 struct super_block *sb = dir->i_sb;
1019 sector_t last_blknr = blknr + MSDOS_SB(sb)->sec_per_clus;
1022 /* Zeroing the unused blocks on this cluster */
1025 while (blknr < last_blknr) {
1026 bhs[n] = sb_getblk(sb, blknr);
1031 memset(bhs[n]->b_data, 0, sb->s_blocksize);
1032 set_buffer_uptodate(bhs[n]);
1033 mark_buffer_dirty(bhs[n]);
1038 if (IS_DIRSYNC(dir)) {
1039 err = fat_sync_bhs(bhs, n);
1043 for (i = 0; i < n; i++)
1048 if (IS_DIRSYNC(dir)) {
1049 err = fat_sync_bhs(bhs, n);
1053 for (i = 0; i < n; i++)
1059 for (i = 0; i < n; i++)
1064 int fat_alloc_new_dir(struct inode *dir, struct timespec *ts)
1066 struct super_block *sb = dir->i_sb;
1067 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1068 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
1069 struct msdos_dir_entry *de;
1074 err = fat_alloc_clusters(dir, &cluster, 1);
1078 blknr = fat_clus_to_blknr(sbi, cluster);
1079 bhs[0] = sb_getblk(sb, blknr);
1085 fat_date_unix2dos(ts->tv_sec, &time, &date);
1087 de = (struct msdos_dir_entry *)bhs[0]->b_data;
1088 /* filling the new directory slots ("." and ".." entries) */
1089 memcpy(de[0].name, MSDOS_DOT, MSDOS_NAME);
1090 memcpy(de[1].name, MSDOS_DOTDOT, MSDOS_NAME);
1091 de->attr = de[1].attr = ATTR_DIR;
1092 de[0].lcase = de[1].lcase = 0;
1093 de[0].time = de[1].time = time;
1094 de[0].date = de[1].date = date;
1095 de[0].ctime_cs = de[1].ctime_cs = 0;
1096 if (sbi->options.isvfat) {
1097 /* extra timestamps */
1098 de[0].ctime = de[1].ctime = time;
1099 de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = date;
1101 de[0].ctime = de[1].ctime = 0;
1102 de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = 0;
1104 de[0].start = cpu_to_le16(cluster);
1105 de[0].starthi = cpu_to_le16(cluster >> 16);
1106 de[1].start = cpu_to_le16(MSDOS_I(dir)->i_logstart);
1107 de[1].starthi = cpu_to_le16(MSDOS_I(dir)->i_logstart >> 16);
1108 de[0].size = de[1].size = 0;
1109 memset(de + 2, 0, sb->s_blocksize - 2 * sizeof(*de));
1110 set_buffer_uptodate(bhs[0]);
1111 mark_buffer_dirty(bhs[0]);
1113 err = fat_zeroed_cluster(dir, blknr, 1, bhs, MAX_BUF_PER_PAGE);
1120 fat_free_clusters(dir, cluster);
1125 EXPORT_SYMBOL_GPL(fat_alloc_new_dir);
1127 static int fat_add_new_entries(struct inode *dir, void *slots, int nr_slots,
1128 int *nr_cluster, struct msdos_dir_entry **de,
1129 struct buffer_head **bh, loff_t *i_pos)
1131 struct super_block *sb = dir->i_sb;
1132 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1133 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
1134 sector_t blknr, start_blknr, last_blknr;
1135 unsigned long size, copy;
1136 int err, i, n, offset, cluster[2];
1139 * The minimum cluster size is 512bytes, and maximum entry
1140 * size is 32*slots (672bytes). So, iff the cluster size is
1141 * 512bytes, we may need two clusters.
1143 size = nr_slots * sizeof(struct msdos_dir_entry);
1144 *nr_cluster = (size + (sbi->cluster_size - 1)) >> sbi->cluster_bits;
1145 BUG_ON(*nr_cluster > 2);
1147 err = fat_alloc_clusters(dir, cluster, *nr_cluster);
1152 * First stage: Fill the directory entry. NOTE: This cluster
1153 * is not referenced from any inode yet, so updates order is
1158 start_blknr = blknr = fat_clus_to_blknr(sbi, cluster[i]);
1159 last_blknr = start_blknr + sbi->sec_per_clus;
1160 while (blknr < last_blknr) {
1161 bhs[n] = sb_getblk(sb, blknr);
1167 /* fill the directory entry */
1168 copy = min(size, sb->s_blocksize);
1169 memcpy(bhs[n]->b_data, slots, copy);
1172 set_buffer_uptodate(bhs[n]);
1173 mark_buffer_dirty(bhs[n]);
1179 } while (++i < *nr_cluster);
1181 memset(bhs[n]->b_data + copy, 0, sb->s_blocksize - copy);
1182 offset = copy - sizeof(struct msdos_dir_entry);
1185 *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
1186 *i_pos = fat_make_i_pos(sb, *bh, *de);
1188 /* Second stage: clear the rest of cluster, and write outs */
1189 err = fat_zeroed_cluster(dir, start_blknr, ++n, bhs, MAX_BUF_PER_PAGE);
1200 for (i = 0; i < n; i++)
1202 fat_free_clusters(dir, cluster[0]);
1207 int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
1208 struct fat_slot_info *sinfo)
1210 struct super_block *sb = dir->i_sb;
1211 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1212 struct buffer_head *bh, *prev, *bhs[3]; /* 32*slots (672bytes) */
1213 struct msdos_dir_entry *de;
1214 int err, free_slots, i, nr_bhs;
1217 sinfo->nr_slots = nr_slots;
1219 /* First stage: search free direcotry entries */
1220 free_slots = nr_bhs = 0;
1224 while (fat_get_entry(dir, &pos, &bh, &de) > -1) {
1225 /* check the maximum size of directory */
1226 if (pos >= FAT_MAX_DIR_SIZE)
1229 if (IS_FREE(de->name)) {
1232 bhs[nr_bhs] = prev = bh;
1236 if (free_slots == nr_slots)
1239 for (i = 0; i < nr_bhs; i++)
1242 free_slots = nr_bhs = 0;
1245 if (dir->i_ino == MSDOS_ROOT_INO) {
1246 if (sbi->fat_bits != 32)
1248 } else if (MSDOS_I(dir)->i_start == 0) {
1249 printk(KERN_ERR "FAT: Corrupted directory (i_pos %lld)\n",
1250 MSDOS_I(dir)->i_pos);
1257 pos -= free_slots * sizeof(*de);
1258 nr_slots -= free_slots;
1261 * Second stage: filling the free entries with new entries.
1262 * NOTE: If this slots has shortname, first, we write
1263 * the long name slots, then write the short name.
1265 int size = free_slots * sizeof(*de);
1266 int offset = pos & (sb->s_blocksize - 1);
1267 int long_bhs = nr_bhs - (nr_slots == 0);
1269 /* Fill the long name slots. */
1270 for (i = 0; i < long_bhs; i++) {
1271 int copy = min_t(int, sb->s_blocksize - offset, size);
1272 memcpy(bhs[i]->b_data + offset, slots, copy);
1273 mark_buffer_dirty(bhs[i]);
1278 if (long_bhs && IS_DIRSYNC(dir))
1279 err = fat_sync_bhs(bhs, long_bhs);
1280 if (!err && i < nr_bhs) {
1281 /* Fill the short name slot. */
1282 int copy = min_t(int, sb->s_blocksize - offset, size);
1283 memcpy(bhs[i]->b_data + offset, slots, copy);
1284 mark_buffer_dirty(bhs[i]);
1285 if (IS_DIRSYNC(dir))
1286 err = sync_dirty_buffer(bhs[i]);
1288 for (i = 0; i < nr_bhs; i++)
1295 int cluster, nr_cluster;
1298 * Third stage: allocate the cluster for new entries.
1299 * And initialize the cluster with new entries, then
1300 * add the cluster to dir.
1302 cluster = fat_add_new_entries(dir, slots, nr_slots, &nr_cluster,
1308 err = fat_chain_add(dir, cluster, nr_cluster);
1310 fat_free_clusters(dir, cluster);
1313 if (dir->i_size & (sbi->cluster_size - 1)) {
1314 fat_fs_panic(sb, "Odd directory size");
1315 dir->i_size = (dir->i_size + sbi->cluster_size - 1)
1316 & ~((loff_t)sbi->cluster_size - 1);
1318 dir->i_size += nr_cluster << sbi->cluster_bits;
1319 MSDOS_I(dir)->mmu_private += nr_cluster << sbi->cluster_bits;
1321 sinfo->slot_off = pos;
1324 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
1330 for (i = 0; i < nr_bhs; i++)
1337 __fat_remove_entries(dir, pos, free_slots);
1341 EXPORT_SYMBOL_GPL(fat_add_entries);