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>
25 static inline loff_t fat_make_i_pos(struct super_block *sb,
26 struct buffer_head *bh,
27 struct msdos_dir_entry *de)
29 return ((loff_t)bh->b_blocknr << MSDOS_SB(sb)->dir_per_block_bits)
30 | (de - (struct msdos_dir_entry *)bh->b_data);
33 static inline void fat_dir_readahead(struct inode *dir, sector_t iblock,
36 struct super_block *sb = dir->i_sb;
37 struct msdos_sb_info *sbi = MSDOS_SB(sb);
38 struct buffer_head *bh;
41 /* This is not a first sector of cluster, or sec_per_clus == 1 */
42 if ((iblock & (sbi->sec_per_clus - 1)) || sbi->sec_per_clus == 1)
44 /* root dir of FAT12/FAT16 */
45 if ((sbi->fat_bits != 32) && (dir->i_ino == MSDOS_ROOT_INO))
48 bh = sb_getblk(sb, phys);
49 if (bh && !buffer_uptodate(bh)) {
50 for (sec = 0; sec < sbi->sec_per_clus; sec++)
51 sb_breadahead(sb, phys + sec);
56 /* Returns the inode number of the directory entry at offset pos. If bh is
57 non-NULL, it is brelse'd before. Pos is incremented. The buffer header is
59 AV. Most often we do it item-by-item. Makes sense to optimize.
60 AV. OK, there we go: if both bh and de are non-NULL we assume that we just
61 AV. want the next entry (took one explicit de=NULL in vfat/namei.c).
62 AV. It's done in fat_get_entry() (inlined), here the slow case lives.
63 AV. Additionally, when we return -1 (i.e. reached the end of directory)
66 static int fat__get_entry(struct inode *dir, loff_t *pos,
67 struct buffer_head **bh, struct msdos_dir_entry **de)
69 struct super_block *sb = dir->i_sb;
70 sector_t phys, iblock;
79 iblock = *pos >> sb->s_blocksize_bits;
80 err = fat_bmap(dir, iblock, &phys);
82 return -1; /* beyond EOF or error */
84 fat_dir_readahead(dir, iblock, phys);
86 *bh = sb_bread(sb, phys);
88 printk(KERN_ERR "FAT: Directory bread(block %llu) failed\n",
89 (unsigned long long)phys);
91 *pos = (iblock + 1) << sb->s_blocksize_bits;
95 offset = *pos & (sb->s_blocksize - 1);
96 *pos += sizeof(struct msdos_dir_entry);
97 *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
102 static inline int fat_get_entry(struct inode *dir, loff_t *pos,
103 struct buffer_head **bh,
104 struct msdos_dir_entry **de)
106 /* Fast stuff first */
108 (*de - (struct msdos_dir_entry *)(*bh)->b_data) < MSDOS_SB(dir->i_sb)->dir_per_block - 1) {
109 *pos += sizeof(struct msdos_dir_entry);
113 return fat__get_entry(dir, pos, bh, de);
117 * Convert Unicode 16 to UTF8, translated Unicode, or ASCII.
118 * If uni_xlate is enabled and we can't get a 1:1 conversion, use a
119 * colon as an escape character since it is normally invalid on the vfat
120 * filesystem. The following four characters are the hexadecimal digits
121 * of Unicode value. This lets us do a full dump and restore of Unicode
122 * filenames. We could get into some trouble with long Unicode names,
123 * but ignore that right now.
124 * Ahem... Stack smashing in ring 0 isn't fun. Fixed.
126 static int uni16_to_x8(unsigned char *ascii, wchar_t *uni, int uni_xlate,
127 struct nls_table *nls)
130 unsigned char *op, nc;
139 if ( (charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE)) > 0) {
142 if (uni_xlate == 1) {
144 for (k = 4; k > 0; k--) {
146 op[k] = nc > 9 ? nc + ('a' - 10)
155 /* We have some slack there, so it's OK */
166 fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
170 charlen = t->char2uni(c, clen, uni);
172 *uni = 0x003f; /* a question mark */
179 fat_short2lower_uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
184 charlen = t->char2uni(c, clen, &wc);
186 *uni = 0x003f; /* a question mark */
188 } else if (charlen <= 1) {
189 unsigned char nc = t->charset2lower[*c];
194 if ( (charlen = t->char2uni(&nc, 1, uni)) < 0) {
195 *uni = 0x003f; /* a question mark */
205 fat_shortname2uni(struct nls_table *nls, unsigned char *buf, int buf_size,
206 wchar_t *uni_buf, unsigned short opt, int lower)
210 if (opt & VFAT_SFN_DISPLAY_LOWER)
211 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
212 else if (opt & VFAT_SFN_DISPLAY_WIN95)
213 len = fat_short2uni(nls, buf, buf_size, uni_buf);
214 else if (opt & VFAT_SFN_DISPLAY_WINNT) {
216 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
218 len = fat_short2uni(nls, buf, buf_size, uni_buf);
220 len = fat_short2uni(nls, buf, buf_size, uni_buf);
226 * Return values: negative -> error, 0 -> not found, positive -> found,
227 * value is the total amount of slots, including the shortname entry.
229 int fat_search_long(struct inode *inode, const unsigned char *name,
230 int name_len, struct fat_slot_info *sinfo)
232 struct super_block *sb = inode->i_sb;
233 struct msdos_sb_info *sbi = MSDOS_SB(sb);
234 struct buffer_head *bh = NULL;
235 struct msdos_dir_entry *de;
236 struct nls_table *nls_io = sbi->nls_io;
237 struct nls_table *nls_disk = sbi->nls_disk;
238 wchar_t bufuname[14];
239 unsigned char xlate_len, nr_slots;
240 wchar_t *unicode = NULL;
241 unsigned char work[8], bufname[260]; /* 256 + 4 */
242 int uni_xlate = sbi->options.unicode_xlate;
243 int utf8 = sbi->options.utf8;
244 int anycase = (sbi->options.name_check != 's');
245 unsigned short opt_shortname = sbi->options.shortname;
247 int chl, i, j, last_u, err;
251 if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
255 if (de->name[0] == DELETED_FLAG)
257 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
259 if (de->attr != ATTR_EXT && IS_FREE(de->name))
261 if (de->attr == ATTR_EXT) {
262 struct msdos_dir_slot *ds;
266 unsigned char alias_checksum;
269 unicode = (wchar_t *)
270 __get_free_page(GFP_KERNEL);
278 ds = (struct msdos_dir_slot *) de;
283 if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */
286 alias_checksum = ds->alias_checksum;
294 fat16_towchar(unicode + offset, ds->name0_4, 5);
295 fat16_towchar(unicode + offset + 5, ds->name5_10, 6);
296 fat16_towchar(unicode + offset + 11, ds->name11_12, 2);
299 unicode[offset + 13] = 0;
301 if (fat_get_entry(inode, &cpos, &bh, &de) < 0)
305 ds = (struct msdos_dir_slot *) de;
306 if (ds->attr != ATTR_EXT)
308 if ((ds->id & ~0x40) != slot)
310 if (ds->alias_checksum != alias_checksum)
313 if (de->name[0] == DELETED_FLAG)
315 if (de->attr == ATTR_EXT)
317 if (IS_FREE(de->name) || (de->attr & ATTR_VOLUME))
319 if (fat_checksum(de->name) != alias_checksum)
323 memcpy(work, de->name, sizeof(de->name));
324 /* see namei.c, msdos_format_name */
327 for (i = 0, j = 0, last_u = 0; i < 8;) {
329 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
330 &bufuname[j++], opt_shortname,
331 de->lcase & CASE_LOWER_BASE);
341 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
342 for (i = 0; i < 3;) {
343 if (!de->ext[i]) break;
344 chl = fat_shortname2uni(nls_disk, &de->ext[i], 3 - i,
345 &bufuname[j++], opt_shortname,
346 de->lcase & CASE_LOWER_EXT);
348 if (de->ext[i] != ' ')
358 bufuname[last_u] = 0x0000;
360 ?utf8_wcstombs(bufname, bufuname, sizeof(bufname))
361 :uni16_to_x8(bufname, bufuname, uni_xlate, nls_io);
362 if (xlate_len == name_len)
363 if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
364 (anycase && !nls_strnicmp(nls_io, name, bufname,
370 ?utf8_wcstombs(bufname, unicode, sizeof(bufname))
371 :uni16_to_x8(bufname, unicode, uni_xlate, nls_io);
372 if (xlate_len != name_len)
374 if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
375 (anycase && !nls_strnicmp(nls_io, name, bufname,
382 nr_slots++; /* include the de */
383 sinfo->slot_off = cpos - nr_slots * sizeof(*de);
384 sinfo->nr_slots = nr_slots;
387 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
391 free_page((unsigned long)unicode);
396 EXPORT_SYMBOL(fat_search_long);
398 struct fat_ioctl_filldir_callback {
399 struct dirent __user *dirent;
402 const char *longname;
404 const char *shortname;
408 static int fat_readdirx(struct inode *inode, struct file *filp, void *dirent,
409 filldir_t filldir, int short_only, int both)
411 struct super_block *sb = inode->i_sb;
412 struct msdos_sb_info *sbi = MSDOS_SB(sb);
413 struct buffer_head *bh;
414 struct msdos_dir_entry *de;
415 struct nls_table *nls_io = sbi->nls_io;
416 struct nls_table *nls_disk = sbi->nls_disk;
417 unsigned char long_slots;
418 const char *fill_name;
420 wchar_t bufuname[14];
421 wchar_t *unicode = NULL;
422 unsigned char c, work[8], bufname[56], *ptname = bufname;
423 unsigned long lpos, dummy, *furrfu = &lpos;
424 int uni_xlate = sbi->options.unicode_xlate;
425 int isvfat = sbi->options.isvfat;
426 int utf8 = sbi->options.utf8;
427 int nocase = sbi->options.nocase;
428 unsigned short opt_shortname = sbi->options.shortname;
430 int chi, chl, i, i2, j, last, last_u, dotoffset = 0;
437 /* Fake . and .. for the root directory. */
438 if (inode->i_ino == MSDOS_ROOT_INO) {
440 if (filldir(dirent, "..", cpos+1, cpos, MSDOS_ROOT_INO, DT_DIR) < 0)
451 if (cpos & (sizeof(struct msdos_dir_entry)-1)) {
459 if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
461 /* Check for long filename entry */
463 if (de->name[0] == DELETED_FLAG)
465 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
467 if (de->attr != ATTR_EXT && IS_FREE(de->name))
470 if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
474 if (isvfat && de->attr == ATTR_EXT) {
475 struct msdos_dir_slot *ds;
479 unsigned char alias_checksum;
482 unicode = (wchar_t *)__get_free_page(GFP_KERNEL);
492 ds = (struct msdos_dir_slot *) de;
497 if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */
500 alias_checksum = ds->alias_checksum;
508 fat16_towchar(unicode + offset, ds->name0_4, 5);
509 fat16_towchar(unicode + offset + 5, ds->name5_10, 6);
510 fat16_towchar(unicode + offset + 11, ds->name11_12, 2);
513 unicode[offset + 13] = 0;
515 if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
519 ds = (struct msdos_dir_slot *) de;
520 if (ds->attr != ATTR_EXT)
521 goto RecEnd; /* XXX */
522 if ((ds->id & ~0x40) != slot)
524 if (ds->alias_checksum != alias_checksum)
527 if (de->name[0] == DELETED_FLAG)
529 if (de->attr == ATTR_EXT)
531 if (IS_FREE(de->name) || (de->attr & ATTR_VOLUME))
533 if (fat_checksum(de->name) != alias_checksum)
537 if (sbi->options.dotsOK) {
540 if (de->attr & ATTR_HIDDEN) {
546 memcpy(work, de->name, sizeof(de->name));
547 /* see namei.c, msdos_format_name */
550 for (i = 0, j = 0, last = 0, last_u = 0; i < 8;) {
551 if (!(c = work[i])) break;
552 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
553 &bufuname[j++], opt_shortname,
554 de->lcase & CASE_LOWER_BASE);
556 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
563 for (chi = 0; chi < chl && i < 8; chi++) {
571 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
573 for (i2 = 0; i2 < 3;) {
574 if (!(c = de->ext[i2])) break;
575 chl = fat_shortname2uni(nls_disk, &de->ext[i2], 3 - i2,
576 &bufuname[j++], opt_shortname,
577 de->lcase & CASE_LOWER_EXT);
580 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
587 for (chi = 0; chi < chl && i2 < 3; chi++) {
588 ptname[i++] = de->ext[i2++];
596 i = last + dotoffset;
599 lpos = cpos - (long_slots+1)*sizeof(struct msdos_dir_entry);
600 if (!memcmp(de->name, MSDOS_DOT, MSDOS_NAME))
602 else if (!memcmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
603 inum = parent_ino(filp->f_dentry);
605 loff_t i_pos = fat_make_i_pos(sb, bh, de);
606 struct inode *tmp = fat_iget(sb, i_pos);
611 inum = iunique(sb, MSDOS_ROOT_INO);
615 bufuname[j] = 0x0000;
616 i = utf8 ? utf8_wcstombs(bufname, bufuname, sizeof(bufname))
617 : uni16_to_x8(bufname, bufuname, uni_xlate, nls_io);
622 if (!short_only && long_slots) {
623 /* convert the unicode long name. 261 is maximum size
624 * of unicode buffer. (13 * slots + nul) */
625 void *longname = unicode + 261;
626 int buf_size = PAGE_SIZE - (261 * sizeof(unicode[0]));
628 ? utf8_wcstombs(longname, unicode, buf_size)
629 : uni16_to_x8(longname, unicode, uni_xlate, nls_io);
632 fill_name = longname;
635 /* hack for fat_ioctl_filldir() */
636 struct fat_ioctl_filldir_callback *p = dirent;
638 p->longname = longname;
639 p->long_len = long_len;
640 p->shortname = bufname;
646 if (filldir(dirent, fill_name, fill_len, *furrfu, inum,
647 (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
659 free_page((unsigned long)unicode);
665 static int fat_readdir(struct file *filp, void *dirent, filldir_t filldir)
667 struct inode *inode = filp->f_dentry->d_inode;
668 return fat_readdirx(inode, filp, dirent, filldir, 0, 0);
671 static int fat_ioctl_filldir(void *__buf, const char *name, int name_len,
672 loff_t offset, ino_t ino, unsigned int d_type)
674 struct fat_ioctl_filldir_callback *buf = __buf;
675 struct dirent __user *d1 = buf->dirent;
676 struct dirent __user *d2 = d1 + 1;
683 /* dirent has only short name */
684 if (name_len >= sizeof(d1->d_name))
685 name_len = sizeof(d1->d_name) - 1;
687 if (put_user(0, d2->d_name) ||
688 put_user(0, &d2->d_reclen) ||
689 copy_to_user(d1->d_name, name, name_len) ||
690 put_user(0, d1->d_name + name_len) ||
691 put_user(name_len, &d1->d_reclen))
694 /* dirent has short and long name */
695 const char *longname = buf->longname;
696 int long_len = buf->long_len;
697 const char *shortname = buf->shortname;
698 int short_len = buf->short_len;
700 if (long_len >= sizeof(d1->d_name))
701 long_len = sizeof(d1->d_name) - 1;
702 if (short_len >= sizeof(d1->d_name))
703 short_len = sizeof(d1->d_name) - 1;
705 if (copy_to_user(d2->d_name, longname, long_len) ||
706 put_user(0, d2->d_name + long_len) ||
707 put_user(long_len, &d2->d_reclen) ||
708 put_user(ino, &d2->d_ino) ||
709 put_user(offset, &d2->d_off) ||
710 copy_to_user(d1->d_name, shortname, short_len) ||
711 put_user(0, d1->d_name + short_len) ||
712 put_user(short_len, &d1->d_reclen))
717 buf->result = -EFAULT;
721 static int fat_dir_ioctl(struct inode * inode, struct file * filp,
722 unsigned int cmd, unsigned long arg)
724 struct fat_ioctl_filldir_callback buf;
725 struct dirent __user *d1;
726 int ret, short_only, both;
729 case VFAT_IOCTL_READDIR_SHORT:
733 case VFAT_IOCTL_READDIR_BOTH:
738 return fat_generic_ioctl(inode, filp, cmd, arg);
741 d1 = (struct dirent __user *)arg;
742 if (!access_ok(VERIFY_WRITE, d1, sizeof(struct dirent[2])))
745 * Yes, we don't need this put_user() absolutely. However old
746 * code didn't return the right value. So, app use this value,
747 * in order to check whether it is EOF.
749 if (put_user(0, &d1->d_reclen))
756 if (!IS_DEADDIR(inode)) {
757 ret = fat_readdirx(inode, filp, &buf, fat_ioctl_filldir,
766 struct file_operations fat_dir_operations = {
767 .read = generic_read_dir,
768 .readdir = fat_readdir,
769 .ioctl = fat_dir_ioctl,
773 static int fat_get_short_entry(struct inode *dir, loff_t *pos,
774 struct buffer_head **bh,
775 struct msdos_dir_entry **de)
777 while (fat_get_entry(dir, pos, bh, de) >= 0) {
778 /* free entry or long name entry or volume label */
779 if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME))
786 * The ".." entry can not provide the "struct fat_slot_info" informations
787 * for inode. So, this function provide the some informations only.
789 int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh,
790 struct msdos_dir_entry **de, loff_t *i_pos)
796 while (fat_get_short_entry(dir, &offset, bh, de) >= 0) {
797 if (!strncmp((*de)->name, MSDOS_DOTDOT, MSDOS_NAME)) {
798 *i_pos = fat_make_i_pos(dir->i_sb, *bh, *de);
805 EXPORT_SYMBOL(fat_get_dotdot_entry);
807 /* See if directory is empty */
808 int fat_dir_empty(struct inode *dir)
810 struct buffer_head *bh;
811 struct msdos_dir_entry *de;
817 while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
818 if (strncmp(de->name, MSDOS_DOT , MSDOS_NAME) &&
819 strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
828 EXPORT_SYMBOL(fat_dir_empty);
831 * fat_subdirs counts the number of sub-directories of dir. It can be run
832 * on directories being created.
834 int fat_subdirs(struct inode *dir)
836 struct buffer_head *bh;
837 struct msdos_dir_entry *de;
843 while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
844 if (de->attr & ATTR_DIR)
852 * Scans a directory for a given file (name points to its formatted name).
853 * Returns an error code or zero.
855 int fat_scan(struct inode *dir, const unsigned char *name,
856 struct fat_slot_info *sinfo)
858 struct super_block *sb = dir->i_sb;
862 while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh,
864 if (!strncmp(sinfo->de->name, name, MSDOS_NAME)) {
865 sinfo->slot_off -= sizeof(*sinfo->de);
867 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
874 EXPORT_SYMBOL(fat_scan);
876 static int __fat_remove_entries(struct inode *dir, loff_t pos, int nr_slots)
878 struct super_block *sb = dir->i_sb;
879 struct buffer_head *bh;
880 struct msdos_dir_entry *de, *endp;
881 int err = 0, orig_slots;
885 if (fat_get_entry(dir, &pos, &bh, &de) < 0) {
890 orig_slots = nr_slots;
891 endp = (struct msdos_dir_entry *)(bh->b_data + sb->s_blocksize);
892 while (nr_slots && de < endp) {
893 de->name[0] = DELETED_FLAG;
897 mark_buffer_dirty(bh);
899 err = sync_dirty_buffer(bh);
904 /* pos is *next* de's position, so this does `- sizeof(de)' */
905 pos += ((orig_slots - nr_slots) * sizeof(*de)) - sizeof(*de);
911 int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo)
913 struct msdos_dir_entry *de;
914 struct buffer_head *bh;
915 int err = 0, nr_slots;
918 * First stage: Remove the shortname. By this, the directory
921 nr_slots = sinfo->nr_slots;
926 while (nr_slots && de >= (struct msdos_dir_entry *)bh->b_data) {
927 de->name[0] = DELETED_FLAG;
931 mark_buffer_dirty(bh);
933 err = sync_dirty_buffer(bh);
941 * Second stage: remove the remaining longname slots.
942 * (This directory entry is already removed, and so return
945 err = __fat_remove_entries(dir, sinfo->slot_off, nr_slots);
948 "FAT: Couldn't remove the long name slots\n");
952 dir->i_mtime = dir->i_atime = CURRENT_TIME_SEC;
954 (void)fat_sync_inode(dir);
956 mark_inode_dirty(dir);
961 EXPORT_SYMBOL(fat_remove_entries);
963 static int fat_zeroed_cluster(struct inode *dir, sector_t blknr, int nr_used,
964 struct buffer_head **bhs, int nr_bhs)
966 struct super_block *sb = dir->i_sb;
967 sector_t last_blknr = blknr + MSDOS_SB(sb)->sec_per_clus;
970 /* Zeroing the unused blocks on this cluster */
973 while (blknr < last_blknr) {
974 bhs[n] = sb_getblk(sb, blknr);
979 memset(bhs[n]->b_data, 0, sb->s_blocksize);
980 set_buffer_uptodate(bhs[n]);
981 mark_buffer_dirty(bhs[n]);
986 if (IS_DIRSYNC(dir)) {
987 err = fat_sync_bhs(bhs, n);
991 for (i = 0; i < n; i++)
996 if (IS_DIRSYNC(dir)) {
997 err = fat_sync_bhs(bhs, n);
1001 for (i = 0; i < n; i++)
1007 for (i = 0; i < n; i++)
1012 int fat_alloc_new_dir(struct inode *dir, struct timespec *ts)
1014 struct super_block *sb = dir->i_sb;
1015 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1016 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
1017 struct msdos_dir_entry *de;
1022 err = fat_alloc_clusters(dir, &cluster, 1);
1026 blknr = fat_clus_to_blknr(sbi, cluster);
1027 bhs[0] = sb_getblk(sb, blknr);
1033 fat_date_unix2dos(ts->tv_sec, &time, &date);
1035 de = (struct msdos_dir_entry *)bhs[0]->b_data;
1036 /* filling the new directory slots ("." and ".." entries) */
1037 memcpy(de[0].name, MSDOS_DOT, MSDOS_NAME);
1038 memcpy(de[1].name, MSDOS_DOTDOT, MSDOS_NAME);
1039 de->attr = de[1].attr = ATTR_DIR;
1040 de[0].lcase = de[1].lcase = 0;
1041 de[0].time = de[1].time = time;
1042 de[0].date = de[1].date = date;
1043 de[0].ctime_cs = de[1].ctime_cs = 0;
1044 if (sbi->options.isvfat) {
1045 /* extra timestamps */
1046 de[0].ctime = de[1].ctime = time;
1047 de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = date;
1049 de[0].ctime = de[1].ctime = 0;
1050 de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = 0;
1052 de[0].start = cpu_to_le16(cluster);
1053 de[0].starthi = cpu_to_le16(cluster >> 16);
1054 de[1].start = cpu_to_le16(MSDOS_I(dir)->i_logstart);
1055 de[1].starthi = cpu_to_le16(MSDOS_I(dir)->i_logstart >> 16);
1056 de[0].size = de[1].size = 0;
1057 memset(de + 2, 0, sb->s_blocksize - 2 * sizeof(*de));
1058 set_buffer_uptodate(bhs[0]);
1059 mark_buffer_dirty(bhs[0]);
1061 err = fat_zeroed_cluster(dir, blknr, 1, bhs, MAX_BUF_PER_PAGE);
1068 fat_free_clusters(dir, cluster);
1073 EXPORT_SYMBOL(fat_alloc_new_dir);
1075 static int fat_add_new_entries(struct inode *dir, void *slots, int nr_slots,
1076 int *nr_cluster, struct msdos_dir_entry **de,
1077 struct buffer_head **bh, loff_t *i_pos)
1079 struct super_block *sb = dir->i_sb;
1080 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1081 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
1082 sector_t blknr, start_blknr, last_blknr;
1083 unsigned long size, copy;
1084 int err, i, n, offset, cluster[2];
1087 * The minimum cluster size is 512bytes, and maximum entry
1088 * size is 32*slots (672bytes). So, iff the cluster size is
1089 * 512bytes, we may need two clusters.
1091 size = nr_slots * sizeof(struct msdos_dir_entry);
1092 *nr_cluster = (size + (sbi->cluster_size - 1)) >> sbi->cluster_bits;
1093 BUG_ON(*nr_cluster > 2);
1095 err = fat_alloc_clusters(dir, cluster, *nr_cluster);
1100 * First stage: Fill the directory entry. NOTE: This cluster
1101 * is not referenced from any inode yet, so updates order is
1106 start_blknr = blknr = fat_clus_to_blknr(sbi, cluster[i]);
1107 last_blknr = start_blknr + sbi->sec_per_clus;
1108 while (blknr < last_blknr) {
1109 bhs[n] = sb_getblk(sb, blknr);
1115 /* fill the directory entry */
1116 copy = min(size, sb->s_blocksize);
1117 memcpy(bhs[n]->b_data, slots, copy);
1120 set_buffer_uptodate(bhs[n]);
1121 mark_buffer_dirty(bhs[n]);
1127 } while (++i < *nr_cluster);
1129 memset(bhs[n]->b_data + copy, 0, sb->s_blocksize - copy);
1130 offset = copy - sizeof(struct msdos_dir_entry);
1133 *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
1134 *i_pos = fat_make_i_pos(sb, *bh, *de);
1136 /* Second stage: clear the rest of cluster, and write outs */
1137 err = fat_zeroed_cluster(dir, start_blknr, ++n, bhs, MAX_BUF_PER_PAGE);
1148 for (i = 0; i < n; i++)
1150 fat_free_clusters(dir, cluster[0]);
1155 int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
1156 struct fat_slot_info *sinfo)
1158 struct super_block *sb = dir->i_sb;
1159 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1160 struct buffer_head *bh, *prev, *bhs[3]; /* 32*slots (672bytes) */
1161 struct msdos_dir_entry *de;
1162 int err, free_slots, i, nr_bhs;
1165 sinfo->nr_slots = nr_slots;
1167 /* First stage: search free direcotry entries */
1168 free_slots = nr_bhs = 0;
1172 while (fat_get_entry(dir, &pos, &bh, &de) > -1) {
1173 /* check the maximum size of directory */
1174 if (pos >= FAT_MAX_DIR_SIZE)
1177 if (IS_FREE(de->name)) {
1180 bhs[nr_bhs] = prev = bh;
1184 if (free_slots == nr_slots)
1187 for (i = 0; i < nr_bhs; i++)
1190 free_slots = nr_bhs = 0;
1193 if (dir->i_ino == MSDOS_ROOT_INO) {
1194 if (sbi->fat_bits != 32)
1196 } else if (MSDOS_I(dir)->i_start == 0) {
1197 printk(KERN_ERR "FAT: Corrupted directory (i_pos %lld)\n",
1198 MSDOS_I(dir)->i_pos);
1205 pos -= free_slots * sizeof(*de);
1206 nr_slots -= free_slots;
1209 * Second stage: filling the free entries with new entries.
1210 * NOTE: If this slots has shortname, first, we write
1211 * the long name slots, then write the short name.
1213 int size = free_slots * sizeof(*de);
1214 int offset = pos & (sb->s_blocksize - 1);
1215 int long_bhs = nr_bhs - (nr_slots == 0);
1217 /* Fill the long name slots. */
1218 for (i = 0; i < long_bhs; i++) {
1219 int copy = min_t(int, sb->s_blocksize - offset, size);
1220 memcpy(bhs[i]->b_data + offset, slots, copy);
1221 mark_buffer_dirty(bhs[i]);
1226 if (long_bhs && IS_DIRSYNC(dir))
1227 err = fat_sync_bhs(bhs, long_bhs);
1228 if (!err && i < nr_bhs) {
1229 /* Fill the short name slot. */
1230 int copy = min_t(int, sb->s_blocksize - offset, size);
1231 memcpy(bhs[i]->b_data + offset, slots, copy);
1232 mark_buffer_dirty(bhs[i]);
1233 if (IS_DIRSYNC(dir))
1234 err = sync_dirty_buffer(bhs[i]);
1236 for (i = 0; i < nr_bhs; i++)
1243 int cluster, nr_cluster;
1246 * Third stage: allocate the cluster for new entries.
1247 * And initialize the cluster with new entries, then
1248 * add the cluster to dir.
1250 cluster = fat_add_new_entries(dir, slots, nr_slots, &nr_cluster,
1256 err = fat_chain_add(dir, cluster, nr_cluster);
1258 fat_free_clusters(dir, cluster);
1261 if (dir->i_size & (sbi->cluster_size - 1)) {
1262 fat_fs_panic(sb, "Odd directory size");
1263 dir->i_size = (dir->i_size + sbi->cluster_size - 1)
1264 & ~((loff_t)sbi->cluster_size - 1);
1266 dir->i_size += nr_cluster << sbi->cluster_bits;
1267 MSDOS_I(dir)->mmu_private += nr_cluster << sbi->cluster_bits;
1269 sinfo->slot_off = pos;
1272 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
1278 for (i = 0; i < nr_bhs; i++)
1285 __fat_remove_entries(dir, pos, free_slots);
1289 EXPORT_SYMBOL(fat_add_entries);