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 /* Returns the inode number of the directory entry at offset pos. If bh is
34 non-NULL, it is brelse'd before. Pos is incremented. The buffer header is
36 AV. Most often we do it item-by-item. Makes sense to optimize.
37 AV. OK, there we go: if both bh and de are non-NULL we assume that we just
38 AV. want the next entry (took one explicit de=NULL in vfat/namei.c).
39 AV. It's done in fat_get_entry() (inlined), here the slow case lives.
40 AV. Additionally, when we return -1 (i.e. reached the end of directory)
43 static int fat__get_entry(struct inode
*dir
, loff_t
*pos
,
44 struct buffer_head
**bh
, struct msdos_dir_entry
**de
)
46 struct super_block
*sb
= dir
->i_sb
;
47 sector_t phys
, iblock
;
56 iblock
= *pos
>> sb
->s_blocksize_bits
;
57 err
= fat_bmap(dir
, iblock
, &phys
);
59 return -1; /* beyond EOF or error */
61 *bh
= sb_bread(sb
, phys
);
63 printk(KERN_ERR
"FAT: Directory bread(block %llu) failed\n",
64 (unsigned long long)phys
);
66 *pos
= (iblock
+ 1) << sb
->s_blocksize_bits
;
70 offset
= *pos
& (sb
->s_blocksize
- 1);
71 *pos
+= sizeof(struct msdos_dir_entry
);
72 *de
= (struct msdos_dir_entry
*)((*bh
)->b_data
+ offset
);
77 static inline int fat_get_entry(struct inode
*dir
, loff_t
*pos
,
78 struct buffer_head
**bh
,
79 struct msdos_dir_entry
**de
)
81 /* Fast stuff first */
83 (*de
- (struct msdos_dir_entry
*)(*bh
)->b_data
) < MSDOS_SB(dir
->i_sb
)->dir_per_block
- 1) {
84 *pos
+= sizeof(struct msdos_dir_entry
);
88 return fat__get_entry(dir
, pos
, bh
, de
);
92 * Convert Unicode 16 to UTF8, translated Unicode, or ASCII.
93 * If uni_xlate is enabled and we can't get a 1:1 conversion, use a
94 * colon as an escape character since it is normally invalid on the vfat
95 * filesystem. The following four characters are the hexadecimal digits
96 * of Unicode value. This lets us do a full dump and restore of Unicode
97 * filenames. We could get into some trouble with long Unicode names,
98 * but ignore that right now.
99 * Ahem... Stack smashing in ring 0 isn't fun. Fixed.
101 static int uni16_to_x8(unsigned char *ascii
, wchar_t *uni
, int uni_xlate
,
102 struct nls_table
*nls
)
105 unsigned char *op
, nc
;
114 if ( (charlen
= nls
->uni2char(ec
, op
, NLS_MAX_CHARSET_SIZE
)) > 0) {
117 if (uni_xlate
== 1) {
119 for (k
= 4; k
> 0; k
--) {
121 op
[k
] = nc
> 9 ? nc
+ ('a' - 10)
130 /* We have some slack there, so it's OK */
141 fat_short2uni(struct nls_table
*t
, unsigned char *c
, int clen
, wchar_t *uni
)
145 charlen
= t
->char2uni(c
, clen
, uni
);
147 *uni
= 0x003f; /* a question mark */
154 fat_short2lower_uni(struct nls_table
*t
, unsigned char *c
, int clen
, wchar_t *uni
)
159 charlen
= t
->char2uni(c
, clen
, &wc
);
161 *uni
= 0x003f; /* a question mark */
163 } else if (charlen
<= 1) {
164 unsigned char nc
= t
->charset2lower
[*c
];
169 if ( (charlen
= t
->char2uni(&nc
, 1, uni
)) < 0) {
170 *uni
= 0x003f; /* a question mark */
180 fat_shortname2uni(struct nls_table
*nls
, unsigned char *buf
, int buf_size
,
181 wchar_t *uni_buf
, unsigned short opt
, int lower
)
185 if (opt
& VFAT_SFN_DISPLAY_LOWER
)
186 len
= fat_short2lower_uni(nls
, buf
, buf_size
, uni_buf
);
187 else if (opt
& VFAT_SFN_DISPLAY_WIN95
)
188 len
= fat_short2uni(nls
, buf
, buf_size
, uni_buf
);
189 else if (opt
& VFAT_SFN_DISPLAY_WINNT
) {
191 len
= fat_short2lower_uni(nls
, buf
, buf_size
, uni_buf
);
193 len
= fat_short2uni(nls
, buf
, buf_size
, uni_buf
);
195 len
= fat_short2uni(nls
, buf
, buf_size
, uni_buf
);
201 * Return values: negative -> error, 0 -> not found, positive -> found,
202 * value is the total amount of slots, including the shortname entry.
204 int fat_search_long(struct inode
*inode
, const unsigned char *name
,
205 int name_len
, struct fat_slot_info
*sinfo
)
207 struct super_block
*sb
= inode
->i_sb
;
208 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
209 struct buffer_head
*bh
= NULL
;
210 struct msdos_dir_entry
*de
;
211 struct nls_table
*nls_io
= sbi
->nls_io
;
212 struct nls_table
*nls_disk
= sbi
->nls_disk
;
213 wchar_t bufuname
[14];
214 unsigned char xlate_len
, nr_slots
;
215 wchar_t *unicode
= NULL
;
216 unsigned char work
[8], bufname
[260]; /* 256 + 4 */
217 int uni_xlate
= sbi
->options
.unicode_xlate
;
218 int utf8
= sbi
->options
.utf8
;
219 int anycase
= (sbi
->options
.name_check
!= 's');
220 unsigned short opt_shortname
= sbi
->options
.shortname
;
222 int chl
, i
, j
, last_u
, err
;
226 if (fat_get_entry(inode
, &cpos
, &bh
, &de
) == -1)
230 if (de
->name
[0] == DELETED_FLAG
)
232 if (de
->attr
!= ATTR_EXT
&& (de
->attr
& ATTR_VOLUME
))
234 if (de
->attr
!= ATTR_EXT
&& IS_FREE(de
->name
))
236 if (de
->attr
== ATTR_EXT
) {
237 struct msdos_dir_slot
*ds
;
242 unsigned char alias_checksum
;
245 unicode
= (wchar_t *)
246 __get_free_page(GFP_KERNEL
);
254 ds
= (struct msdos_dir_slot
*) de
;
259 if (slots
> 20 || !slots
) /* ceil(256 * 2 / 26) */
262 alias_checksum
= ds
->alias_checksum
;
270 fat16_towchar(unicode
+ offset
, ds
->name0_4
, 5);
271 fat16_towchar(unicode
+ offset
+ 5, ds
->name5_10
, 6);
272 fat16_towchar(unicode
+ offset
+ 11, ds
->name11_12
, 2);
275 unicode
[offset
+ 13] = 0;
277 if (fat_get_entry(inode
, &cpos
, &bh
, &de
) < 0)
281 ds
= (struct msdos_dir_slot
*) de
;
282 if (ds
->attr
!= ATTR_EXT
)
284 if ((ds
->id
& ~0x40) != slot
)
286 if (ds
->alias_checksum
!= alias_checksum
)
289 if (de
->name
[0] == DELETED_FLAG
)
291 if (de
->attr
== ATTR_EXT
)
293 if (IS_FREE(de
->name
) || (de
->attr
& ATTR_VOLUME
))
295 for (sum
= 0, i
= 0; i
< 11; i
++)
296 sum
= (((sum
&1)<<7)|((sum
&0xfe)>>1)) + de
->name
[i
];
297 if (sum
!= alias_checksum
)
301 memcpy(work
, de
->name
, sizeof(de
->name
));
302 /* see namei.c, msdos_format_name */
305 for (i
= 0, j
= 0, last_u
= 0; i
< 8;) {
307 chl
= fat_shortname2uni(nls_disk
, &work
[i
], 8 - i
,
308 &bufuname
[j
++], opt_shortname
,
309 de
->lcase
& CASE_LOWER_BASE
);
319 fat_short2uni(nls_disk
, ".", 1, &bufuname
[j
++]);
320 for (i
= 0; i
< 3;) {
321 if (!de
->ext
[i
]) break;
322 chl
= fat_shortname2uni(nls_disk
, &de
->ext
[i
], 3 - i
,
323 &bufuname
[j
++], opt_shortname
,
324 de
->lcase
& CASE_LOWER_EXT
);
326 if (de
->ext
[i
] != ' ')
336 bufuname
[last_u
] = 0x0000;
338 ?utf8_wcstombs(bufname
, bufuname
, sizeof(bufname
))
339 :uni16_to_x8(bufname
, bufuname
, uni_xlate
, nls_io
);
340 if (xlate_len
== name_len
)
341 if ((!anycase
&& !memcmp(name
, bufname
, xlate_len
)) ||
342 (anycase
&& !nls_strnicmp(nls_io
, name
, bufname
,
348 ?utf8_wcstombs(bufname
, unicode
, sizeof(bufname
))
349 :uni16_to_x8(bufname
, unicode
, uni_xlate
, nls_io
);
350 if (xlate_len
!= name_len
)
352 if ((!anycase
&& !memcmp(name
, bufname
, xlate_len
)) ||
353 (anycase
&& !nls_strnicmp(nls_io
, name
, bufname
,
360 nr_slots
++; /* include the de */
361 sinfo
->slot_off
= cpos
- nr_slots
* sizeof(*de
);
362 sinfo
->nr_slots
= nr_slots
;
365 sinfo
->i_pos
= fat_make_i_pos(sb
, sinfo
->bh
, sinfo
->de
);
369 free_page((unsigned long)unicode
);
374 EXPORT_SYMBOL(fat_search_long
);
376 struct fat_ioctl_filldir_callback
{
377 struct dirent __user
*dirent
;
380 const char *longname
;
382 const char *shortname
;
386 static int fat_readdirx(struct inode
*inode
, struct file
*filp
, void *dirent
,
387 filldir_t filldir
, int short_only
, int both
)
389 struct super_block
*sb
= inode
->i_sb
;
390 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
391 struct buffer_head
*bh
;
392 struct msdos_dir_entry
*de
;
393 struct nls_table
*nls_io
= sbi
->nls_io
;
394 struct nls_table
*nls_disk
= sbi
->nls_disk
;
395 unsigned char long_slots
;
396 const char *fill_name
;
398 wchar_t bufuname
[14];
399 wchar_t *unicode
= NULL
;
400 unsigned char c
, work
[8], bufname
[56], *ptname
= bufname
;
401 unsigned long lpos
, dummy
, *furrfu
= &lpos
;
402 int uni_xlate
= sbi
->options
.unicode_xlate
;
403 int isvfat
= sbi
->options
.isvfat
;
404 int utf8
= sbi
->options
.utf8
;
405 int nocase
= sbi
->options
.nocase
;
406 unsigned short opt_shortname
= sbi
->options
.shortname
;
408 int chi
, chl
, i
, i2
, j
, last
, last_u
, dotoffset
= 0;
415 /* Fake . and .. for the root directory. */
416 if (inode
->i_ino
== MSDOS_ROOT_INO
) {
418 if (filldir(dirent
, "..", cpos
+1, cpos
, MSDOS_ROOT_INO
, DT_DIR
) < 0)
429 if (cpos
& (sizeof(struct msdos_dir_entry
)-1)) {
437 if (fat_get_entry(inode
, &cpos
, &bh
, &de
) == -1)
439 /* Check for long filename entry */
441 if (de
->name
[0] == DELETED_FLAG
)
443 if (de
->attr
!= ATTR_EXT
&& (de
->attr
& ATTR_VOLUME
))
445 if (de
->attr
!= ATTR_EXT
&& IS_FREE(de
->name
))
448 if ((de
->attr
& ATTR_VOLUME
) || IS_FREE(de
->name
))
452 if (isvfat
&& de
->attr
== ATTR_EXT
) {
453 struct msdos_dir_slot
*ds
;
458 unsigned char alias_checksum
;
461 unicode
= (wchar_t *)__get_free_page(GFP_KERNEL
);
471 ds
= (struct msdos_dir_slot
*) de
;
476 if (slots
> 20 || !slots
) /* ceil(256 * 2 / 26) */
479 alias_checksum
= ds
->alias_checksum
;
487 fat16_towchar(unicode
+ offset
, ds
->name0_4
, 5);
488 fat16_towchar(unicode
+ offset
+ 5, ds
->name5_10
, 6);
489 fat16_towchar(unicode
+ offset
+ 11, ds
->name11_12
, 2);
492 unicode
[offset
+ 13] = 0;
494 if (fat_get_entry(inode
, &cpos
, &bh
, &de
) == -1)
498 ds
= (struct msdos_dir_slot
*) de
;
499 if (ds
->attr
!= ATTR_EXT
)
500 goto RecEnd
; /* XXX */
501 if ((ds
->id
& ~0x40) != slot
)
503 if (ds
->alias_checksum
!= alias_checksum
)
506 if (de
->name
[0] == DELETED_FLAG
)
508 if (de
->attr
== ATTR_EXT
)
510 if (IS_FREE(de
->name
) || (de
->attr
& ATTR_VOLUME
))
512 for (sum
= 0, i
= 0; i
< 11; i
++)
513 sum
= (((sum
&1)<<7)|((sum
&0xfe)>>1)) + de
->name
[i
];
514 if (sum
!= alias_checksum
)
518 if (sbi
->options
.dotsOK
) {
521 if (de
->attr
& ATTR_HIDDEN
) {
527 memcpy(work
, de
->name
, sizeof(de
->name
));
528 /* see namei.c, msdos_format_name */
531 for (i
= 0, j
= 0, last
= 0, last_u
= 0; i
< 8;) {
532 if (!(c
= work
[i
])) break;
533 chl
= fat_shortname2uni(nls_disk
, &work
[i
], 8 - i
,
534 &bufuname
[j
++], opt_shortname
,
535 de
->lcase
& CASE_LOWER_BASE
);
537 ptname
[i
++] = (!nocase
&& c
>='A' && c
<='Z') ? c
+32 : c
;
544 for (chi
= 0; chi
< chl
&& i
< 8; chi
++) {
552 fat_short2uni(nls_disk
, ".", 1, &bufuname
[j
++]);
554 for (i2
= 0; i2
< 3;) {
555 if (!(c
= de
->ext
[i2
])) break;
556 chl
= fat_shortname2uni(nls_disk
, &de
->ext
[i2
], 3 - i2
,
557 &bufuname
[j
++], opt_shortname
,
558 de
->lcase
& CASE_LOWER_EXT
);
561 ptname
[i
++] = (!nocase
&& c
>='A' && c
<='Z') ? c
+32 : c
;
568 for (chi
= 0; chi
< chl
&& i2
< 3; chi
++) {
569 ptname
[i
++] = de
->ext
[i2
++];
577 i
= last
+ dotoffset
;
580 lpos
= cpos
- (long_slots
+1)*sizeof(struct msdos_dir_entry
);
581 if (!memcmp(de
->name
, MSDOS_DOT
, MSDOS_NAME
))
583 else if (!memcmp(de
->name
, MSDOS_DOTDOT
, MSDOS_NAME
)) {
584 inum
= parent_ino(filp
->f_dentry
);
586 loff_t i_pos
= fat_make_i_pos(sb
, bh
, de
);
587 struct inode
*tmp
= fat_iget(sb
, i_pos
);
592 inum
= iunique(sb
, MSDOS_ROOT_INO
);
596 bufuname
[j
] = 0x0000;
597 i
= utf8
? utf8_wcstombs(bufname
, bufuname
, sizeof(bufname
))
598 : uni16_to_x8(bufname
, bufuname
, uni_xlate
, nls_io
);
603 if (!short_only
&& long_slots
) {
604 /* convert the unicode long name. 261 is maximum size
605 * of unicode buffer. (13 * slots + nul) */
606 void *longname
= unicode
+ 261;
607 int buf_size
= PAGE_SIZE
- (261 * sizeof(unicode
[0]));
609 ? utf8_wcstombs(longname
, unicode
, buf_size
)
610 : uni16_to_x8(longname
, unicode
, uni_xlate
, nls_io
);
613 fill_name
= longname
;
616 /* hack for fat_ioctl_filldir() */
617 struct fat_ioctl_filldir_callback
*p
= dirent
;
619 p
->longname
= longname
;
620 p
->long_len
= long_len
;
621 p
->shortname
= bufname
;
627 if (filldir(dirent
, fill_name
, fill_len
, *furrfu
, inum
,
628 (de
->attr
& ATTR_DIR
) ? DT_DIR
: DT_REG
) < 0)
641 free_page((unsigned long)unicode
);
647 static int fat_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
649 struct inode
*inode
= filp
->f_dentry
->d_inode
;
650 return fat_readdirx(inode
, filp
, dirent
, filldir
, 0, 0);
653 static int fat_ioctl_filldir(void *__buf
, const char *name
, int name_len
,
654 loff_t offset
, ino_t ino
, unsigned int d_type
)
656 struct fat_ioctl_filldir_callback
*buf
= __buf
;
657 struct dirent __user
*d1
= buf
->dirent
;
658 struct dirent __user
*d2
= d1
+ 1;
665 /* dirent has only short name */
666 if (name_len
>= sizeof(d1
->d_name
))
667 name_len
= sizeof(d1
->d_name
) - 1;
669 if (put_user(0, d2
->d_name
) ||
670 put_user(0, &d2
->d_reclen
) ||
671 copy_to_user(d1
->d_name
, name
, name_len
) ||
672 put_user(0, d1
->d_name
+ name_len
) ||
673 put_user(name_len
, &d1
->d_reclen
))
676 /* dirent has short and long name */
677 const char *longname
= buf
->longname
;
678 int long_len
= buf
->long_len
;
679 const char *shortname
= buf
->shortname
;
680 int short_len
= buf
->short_len
;
682 if (long_len
>= sizeof(d1
->d_name
))
683 long_len
= sizeof(d1
->d_name
) - 1;
684 if (short_len
>= sizeof(d1
->d_name
))
685 short_len
= sizeof(d1
->d_name
) - 1;
687 if (copy_to_user(d2
->d_name
, longname
, long_len
) ||
688 put_user(0, d2
->d_name
+ long_len
) ||
689 put_user(long_len
, &d2
->d_reclen
) ||
690 put_user(ino
, &d2
->d_ino
) ||
691 put_user(offset
, &d2
->d_off
) ||
692 copy_to_user(d1
->d_name
, shortname
, short_len
) ||
693 put_user(0, d1
->d_name
+ short_len
) ||
694 put_user(short_len
, &d1
->d_reclen
))
699 buf
->result
= -EFAULT
;
703 static int fat_dir_ioctl(struct inode
* inode
, struct file
* filp
,
704 unsigned int cmd
, unsigned long arg
)
706 struct fat_ioctl_filldir_callback buf
;
707 struct dirent __user
*d1
;
708 int ret
, short_only
, both
;
711 case VFAT_IOCTL_READDIR_SHORT
:
715 case VFAT_IOCTL_READDIR_BOTH
:
720 return fat_generic_ioctl(inode
, filp
, cmd
, arg
);
723 d1
= (struct dirent __user
*)arg
;
724 if (!access_ok(VERIFY_WRITE
, d1
, sizeof(struct dirent
[2])))
727 * Yes, we don't need this put_user() absolutely. However old
728 * code didn't return the right value. So, app use this value,
729 * in order to check whether it is EOF.
731 if (put_user(0, &d1
->d_reclen
))
738 if (!IS_DEADDIR(inode
)) {
739 ret
= fat_readdirx(inode
, filp
, &buf
, fat_ioctl_filldir
,
748 struct file_operations fat_dir_operations
= {
749 .read
= generic_read_dir
,
750 .readdir
= fat_readdir
,
751 .ioctl
= fat_dir_ioctl
,
755 static int fat_get_short_entry(struct inode
*dir
, loff_t
*pos
,
756 struct buffer_head
**bh
,
757 struct msdos_dir_entry
**de
)
759 while (fat_get_entry(dir
, pos
, bh
, de
) >= 0) {
760 /* free entry or long name entry or volume label */
761 if (!IS_FREE((*de
)->name
) && !((*de
)->attr
& ATTR_VOLUME
))
768 * The ".." entry can not provide the "struct fat_slot_info" informations
769 * for inode. So, this function provide the some informations only.
771 int fat_get_dotdot_entry(struct inode
*dir
, struct buffer_head
**bh
,
772 struct msdos_dir_entry
**de
, loff_t
*i_pos
)
778 while (fat_get_short_entry(dir
, &offset
, bh
, de
) >= 0) {
779 if (!strncmp((*de
)->name
, MSDOS_DOTDOT
, MSDOS_NAME
)) {
780 *i_pos
= fat_make_i_pos(dir
->i_sb
, *bh
, *de
);
787 EXPORT_SYMBOL(fat_get_dotdot_entry
);
789 /* See if directory is empty */
790 int fat_dir_empty(struct inode
*dir
)
792 struct buffer_head
*bh
;
793 struct msdos_dir_entry
*de
;
799 while (fat_get_short_entry(dir
, &cpos
, &bh
, &de
) >= 0) {
800 if (strncmp(de
->name
, MSDOS_DOT
, MSDOS_NAME
) &&
801 strncmp(de
->name
, MSDOS_DOTDOT
, MSDOS_NAME
)) {
810 EXPORT_SYMBOL(fat_dir_empty
);
813 * fat_subdirs counts the number of sub-directories of dir. It can be run
814 * on directories being created.
816 int fat_subdirs(struct inode
*dir
)
818 struct buffer_head
*bh
;
819 struct msdos_dir_entry
*de
;
825 while (fat_get_short_entry(dir
, &cpos
, &bh
, &de
) >= 0) {
826 if (de
->attr
& ATTR_DIR
)
834 * Scans a directory for a given file (name points to its formatted name).
835 * Returns an error code or zero.
837 int fat_scan(struct inode
*dir
, const unsigned char *name
,
838 struct fat_slot_info
*sinfo
)
840 struct super_block
*sb
= dir
->i_sb
;
844 while (fat_get_short_entry(dir
, &sinfo
->slot_off
, &sinfo
->bh
,
846 if (!strncmp(sinfo
->de
->name
, name
, MSDOS_NAME
)) {
847 sinfo
->slot_off
-= sizeof(*sinfo
->de
);
849 sinfo
->i_pos
= fat_make_i_pos(sb
, sinfo
->bh
, sinfo
->de
);
856 EXPORT_SYMBOL(fat_scan
);
858 static int __fat_remove_entries(struct inode
*dir
, loff_t pos
, int nr_slots
)
860 struct super_block
*sb
= dir
->i_sb
;
861 struct buffer_head
*bh
;
862 struct msdos_dir_entry
*de
, *endp
;
863 int err
= 0, orig_slots
;
867 if (fat_get_entry(dir
, &pos
, &bh
, &de
) < 0) {
872 orig_slots
= nr_slots
;
873 endp
= (struct msdos_dir_entry
*)(bh
->b_data
+ sb
->s_blocksize
);
874 while (nr_slots
&& de
< endp
) {
875 de
->name
[0] = DELETED_FLAG
;
879 mark_buffer_dirty(bh
);
881 err
= sync_dirty_buffer(bh
);
886 /* pos is *next* de's position, so this does `- sizeof(de)' */
887 pos
+= ((orig_slots
- nr_slots
) * sizeof(*de
)) - sizeof(*de
);
893 int fat_remove_entries(struct inode
*dir
, struct fat_slot_info
*sinfo
)
895 struct msdos_dir_entry
*de
;
896 struct buffer_head
*bh
;
897 int err
= 0, nr_slots
;
900 * First stage: Remove the shortname. By this, the directory
903 nr_slots
= sinfo
->nr_slots
;
908 while (nr_slots
&& de
>= (struct msdos_dir_entry
*)bh
->b_data
) {
909 de
->name
[0] = DELETED_FLAG
;
913 mark_buffer_dirty(bh
);
915 err
= sync_dirty_buffer(bh
);
923 * Second stage: remove the remaining longname slots.
924 * (This directory entry is already removed, and so return
927 err
= __fat_remove_entries(dir
, sinfo
->slot_off
, nr_slots
);
930 "FAT: Couldn't remove the long name slots\n");
934 dir
->i_mtime
= dir
->i_atime
= CURRENT_TIME_SEC
;
936 (void)fat_sync_inode(dir
);
938 mark_inode_dirty(dir
);
943 EXPORT_SYMBOL(fat_remove_entries
);
945 static int fat_zeroed_cluster(struct inode
*dir
, sector_t blknr
, int nr_used
,
946 struct buffer_head
**bhs
, int nr_bhs
)
948 struct super_block
*sb
= dir
->i_sb
;
949 sector_t last_blknr
= blknr
+ MSDOS_SB(sb
)->sec_per_clus
;
952 /* Zeroing the unused blocks on this cluster */
955 while (blknr
< last_blknr
) {
956 bhs
[n
] = sb_getblk(sb
, blknr
);
961 memset(bhs
[n
]->b_data
, 0, sb
->s_blocksize
);
962 set_buffer_uptodate(bhs
[n
]);
963 mark_buffer_dirty(bhs
[n
]);
968 if (IS_DIRSYNC(dir
)) {
969 err
= fat_sync_bhs(bhs
, n
);
973 for (i
= 0; i
< n
; i
++)
978 if (IS_DIRSYNC(dir
)) {
979 err
= fat_sync_bhs(bhs
, n
);
983 for (i
= 0; i
< n
; i
++)
989 for (i
= 0; i
< n
; i
++)
994 int fat_alloc_new_dir(struct inode
*dir
, struct timespec
*ts
)
996 struct super_block
*sb
= dir
->i_sb
;
997 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
998 struct buffer_head
*bhs
[MAX_BUF_PER_PAGE
];
999 struct msdos_dir_entry
*de
;
1004 err
= fat_alloc_clusters(dir
, &cluster
, 1);
1008 blknr
= fat_clus_to_blknr(sbi
, cluster
);
1009 bhs
[0] = sb_getblk(sb
, blknr
);
1015 fat_date_unix2dos(ts
->tv_sec
, &time
, &date
);
1017 de
= (struct msdos_dir_entry
*)bhs
[0]->b_data
;
1018 /* filling the new directory slots ("." and ".." entries) */
1019 memcpy(de
[0].name
, MSDOS_DOT
, MSDOS_NAME
);
1020 memcpy(de
[1].name
, MSDOS_DOTDOT
, MSDOS_NAME
);
1021 de
->attr
= de
[1].attr
= ATTR_DIR
;
1022 de
[0].lcase
= de
[1].lcase
= 0;
1023 de
[0].time
= de
[1].time
= time
;
1024 de
[0].date
= de
[1].date
= date
;
1025 de
[0].ctime_cs
= de
[1].ctime_cs
= 0;
1026 if (sbi
->options
.isvfat
) {
1027 /* extra timestamps */
1028 de
[0].ctime
= de
[1].ctime
= time
;
1029 de
[0].adate
= de
[0].cdate
= de
[1].adate
= de
[1].cdate
= date
;
1031 de
[0].ctime
= de
[1].ctime
= 0;
1032 de
[0].adate
= de
[0].cdate
= de
[1].adate
= de
[1].cdate
= 0;
1034 de
[0].start
= cpu_to_le16(cluster
);
1035 de
[0].starthi
= cpu_to_le16(cluster
>> 16);
1036 de
[1].start
= cpu_to_le16(MSDOS_I(dir
)->i_logstart
);
1037 de
[1].starthi
= cpu_to_le16(MSDOS_I(dir
)->i_logstart
>> 16);
1038 de
[0].size
= de
[1].size
= 0;
1039 memset(de
+ 2, 0, sb
->s_blocksize
- 2 * sizeof(*de
));
1040 set_buffer_uptodate(bhs
[0]);
1041 mark_buffer_dirty(bhs
[0]);
1043 err
= fat_zeroed_cluster(dir
, blknr
, 1, bhs
, MAX_BUF_PER_PAGE
);
1050 fat_free_clusters(dir
, cluster
);
1055 EXPORT_SYMBOL(fat_alloc_new_dir
);
1057 static int fat_add_new_entries(struct inode
*dir
, void *slots
, int nr_slots
,
1058 int *nr_cluster
, struct msdos_dir_entry
**de
,
1059 struct buffer_head
**bh
, loff_t
*i_pos
)
1061 struct super_block
*sb
= dir
->i_sb
;
1062 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
1063 struct buffer_head
*bhs
[MAX_BUF_PER_PAGE
];
1064 sector_t blknr
, start_blknr
, last_blknr
;
1065 unsigned long size
, copy
;
1066 int err
, i
, n
, offset
, cluster
[2];
1069 * The minimum cluster size is 512bytes, and maximum entry
1070 * size is 32*slots (672bytes). So, iff the cluster size is
1071 * 512bytes, we may need two clusters.
1073 size
= nr_slots
* sizeof(struct msdos_dir_entry
);
1074 *nr_cluster
= (size
+ (sbi
->cluster_size
- 1)) >> sbi
->cluster_bits
;
1075 BUG_ON(*nr_cluster
> 2);
1077 err
= fat_alloc_clusters(dir
, cluster
, *nr_cluster
);
1082 * First stage: Fill the directory entry. NOTE: This cluster
1083 * is not referenced from any inode yet, so updates order is
1088 start_blknr
= blknr
= fat_clus_to_blknr(sbi
, cluster
[i
]);
1089 last_blknr
= start_blknr
+ sbi
->sec_per_clus
;
1090 while (blknr
< last_blknr
) {
1091 bhs
[n
] = sb_getblk(sb
, blknr
);
1097 /* fill the directory entry */
1098 copy
= min(size
, sb
->s_blocksize
);
1099 memcpy(bhs
[n
]->b_data
, slots
, copy
);
1102 set_buffer_uptodate(bhs
[n
]);
1103 mark_buffer_dirty(bhs
[n
]);
1109 } while (++i
< *nr_cluster
);
1111 memset(bhs
[n
]->b_data
+ copy
, 0, sb
->s_blocksize
- copy
);
1112 offset
= copy
- sizeof(struct msdos_dir_entry
);
1115 *de
= (struct msdos_dir_entry
*)((*bh
)->b_data
+ offset
);
1116 *i_pos
= fat_make_i_pos(sb
, *bh
, *de
);
1118 /* Second stage: clear the rest of cluster, and write outs */
1119 err
= fat_zeroed_cluster(dir
, start_blknr
, ++n
, bhs
, MAX_BUF_PER_PAGE
);
1130 for (i
= 0; i
< n
; i
++)
1132 fat_free_clusters(dir
, cluster
[0]);
1137 int fat_add_entries(struct inode
*dir
, void *slots
, int nr_slots
,
1138 struct fat_slot_info
*sinfo
)
1140 struct super_block
*sb
= dir
->i_sb
;
1141 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
1142 struct buffer_head
*bh
, *prev
, *bhs
[3]; /* 32*slots (672bytes) */
1143 struct msdos_dir_entry
*de
;
1144 int err
, free_slots
, i
, nr_bhs
;
1147 sinfo
->nr_slots
= nr_slots
;
1149 /* First stage: search free direcotry entries */
1150 free_slots
= nr_bhs
= 0;
1154 while (fat_get_entry(dir
, &pos
, &bh
, &de
) > -1) {
1155 /* check the maximum size of directory */
1156 if (pos
>= FAT_MAX_DIR_SIZE
)
1159 if (IS_FREE(de
->name
)) {
1162 bhs
[nr_bhs
] = prev
= bh
;
1166 if (free_slots
== nr_slots
)
1169 for (i
= 0; i
< nr_bhs
; i
++)
1172 free_slots
= nr_bhs
= 0;
1175 if (dir
->i_ino
== MSDOS_ROOT_INO
) {
1176 if (sbi
->fat_bits
!= 32)
1178 } else if (MSDOS_I(dir
)->i_start
== 0) {
1179 printk(KERN_ERR
"FAT: Corrupted directory (i_pos %lld)\n",
1180 MSDOS_I(dir
)->i_pos
);
1187 pos
-= free_slots
* sizeof(*de
);
1188 nr_slots
-= free_slots
;
1191 * Second stage: filling the free entries with new entries.
1192 * NOTE: If this slots has shortname, first, we write
1193 * the long name slots, then write the short name.
1195 int size
= free_slots
* sizeof(*de
);
1196 int offset
= pos
& (sb
->s_blocksize
- 1);
1197 int long_bhs
= nr_bhs
- (nr_slots
== 0);
1199 /* Fill the long name slots. */
1200 for (i
= 0; i
< long_bhs
; i
++) {
1201 int copy
= min_t(int, sb
->s_blocksize
- offset
, size
);
1202 memcpy(bhs
[i
]->b_data
+ offset
, slots
, copy
);
1203 mark_buffer_dirty(bhs
[i
]);
1208 if (long_bhs
&& IS_DIRSYNC(dir
))
1209 err
= fat_sync_bhs(bhs
, long_bhs
);
1210 if (!err
&& i
< nr_bhs
) {
1211 /* Fill the short name slot. */
1212 int copy
= min_t(int, sb
->s_blocksize
- offset
, size
);
1213 memcpy(bhs
[i
]->b_data
+ offset
, slots
, copy
);
1214 mark_buffer_dirty(bhs
[i
]);
1215 if (IS_DIRSYNC(dir
))
1216 err
= sync_dirty_buffer(bhs
[i
]);
1218 for (i
= 0; i
< nr_bhs
; i
++)
1225 int cluster
, nr_cluster
;
1228 * Third stage: allocate the cluster for new entries.
1229 * And initialize the cluster with new entries, then
1230 * add the cluster to dir.
1232 cluster
= fat_add_new_entries(dir
, slots
, nr_slots
, &nr_cluster
,
1238 err
= fat_chain_add(dir
, cluster
, nr_cluster
);
1240 fat_free_clusters(dir
, cluster
);
1243 if (dir
->i_size
& (sbi
->cluster_size
- 1)) {
1244 fat_fs_panic(sb
, "Odd directory size");
1245 dir
->i_size
= (dir
->i_size
+ sbi
->cluster_size
- 1)
1246 & ~((loff_t
)sbi
->cluster_size
- 1);
1248 dir
->i_size
+= nr_cluster
<< sbi
->cluster_bits
;
1249 MSDOS_I(dir
)->mmu_private
+= nr_cluster
<< sbi
->cluster_bits
;
1251 sinfo
->slot_off
= pos
;
1254 sinfo
->i_pos
= fat_make_i_pos(sb
, sinfo
->bh
, sinfo
->de
);
1260 for (i
= 0; i
< nr_bhs
; i
++)
1267 __fat_remove_entries(dir
, pos
, free_slots
);
1271 EXPORT_SYMBOL(fat_add_entries
);