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
;
267 unsigned char alias_checksum
;
270 unicode
= (wchar_t *)
271 __get_free_page(GFP_KERNEL
);
279 ds
= (struct msdos_dir_slot
*) de
;
284 if (slots
> 20 || !slots
) /* ceil(256 * 2 / 26) */
287 alias_checksum
= ds
->alias_checksum
;
295 fat16_towchar(unicode
+ offset
, ds
->name0_4
, 5);
296 fat16_towchar(unicode
+ offset
+ 5, ds
->name5_10
, 6);
297 fat16_towchar(unicode
+ offset
+ 11, ds
->name11_12
, 2);
300 unicode
[offset
+ 13] = 0;
302 if (fat_get_entry(inode
, &cpos
, &bh
, &de
) < 0)
306 ds
= (struct msdos_dir_slot
*) de
;
307 if (ds
->attr
!= ATTR_EXT
)
309 if ((ds
->id
& ~0x40) != slot
)
311 if (ds
->alias_checksum
!= alias_checksum
)
314 if (de
->name
[0] == DELETED_FLAG
)
316 if (de
->attr
== ATTR_EXT
)
318 if (IS_FREE(de
->name
) || (de
->attr
& ATTR_VOLUME
))
320 for (sum
= 0, i
= 0; i
< 11; i
++)
321 sum
= (((sum
&1)<<7)|((sum
&0xfe)>>1)) + de
->name
[i
];
322 if (sum
!= alias_checksum
)
326 memcpy(work
, de
->name
, sizeof(de
->name
));
327 /* see namei.c, msdos_format_name */
330 for (i
= 0, j
= 0, last_u
= 0; i
< 8;) {
332 chl
= fat_shortname2uni(nls_disk
, &work
[i
], 8 - i
,
333 &bufuname
[j
++], opt_shortname
,
334 de
->lcase
& CASE_LOWER_BASE
);
344 fat_short2uni(nls_disk
, ".", 1, &bufuname
[j
++]);
345 for (i
= 0; i
< 3;) {
346 if (!de
->ext
[i
]) break;
347 chl
= fat_shortname2uni(nls_disk
, &de
->ext
[i
], 3 - i
,
348 &bufuname
[j
++], opt_shortname
,
349 de
->lcase
& CASE_LOWER_EXT
);
351 if (de
->ext
[i
] != ' ')
361 bufuname
[last_u
] = 0x0000;
363 ?utf8_wcstombs(bufname
, bufuname
, sizeof(bufname
))
364 :uni16_to_x8(bufname
, bufuname
, uni_xlate
, nls_io
);
365 if (xlate_len
== name_len
)
366 if ((!anycase
&& !memcmp(name
, bufname
, xlate_len
)) ||
367 (anycase
&& !nls_strnicmp(nls_io
, name
, bufname
,
373 ?utf8_wcstombs(bufname
, unicode
, sizeof(bufname
))
374 :uni16_to_x8(bufname
, unicode
, uni_xlate
, nls_io
);
375 if (xlate_len
!= name_len
)
377 if ((!anycase
&& !memcmp(name
, bufname
, xlate_len
)) ||
378 (anycase
&& !nls_strnicmp(nls_io
, name
, bufname
,
385 nr_slots
++; /* include the de */
386 sinfo
->slot_off
= cpos
- nr_slots
* sizeof(*de
);
387 sinfo
->nr_slots
= nr_slots
;
390 sinfo
->i_pos
= fat_make_i_pos(sb
, sinfo
->bh
, sinfo
->de
);
394 free_page((unsigned long)unicode
);
399 EXPORT_SYMBOL(fat_search_long
);
401 struct fat_ioctl_filldir_callback
{
402 struct dirent __user
*dirent
;
405 const char *longname
;
407 const char *shortname
;
411 static int fat_readdirx(struct inode
*inode
, struct file
*filp
, void *dirent
,
412 filldir_t filldir
, int short_only
, int both
)
414 struct super_block
*sb
= inode
->i_sb
;
415 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
416 struct buffer_head
*bh
;
417 struct msdos_dir_entry
*de
;
418 struct nls_table
*nls_io
= sbi
->nls_io
;
419 struct nls_table
*nls_disk
= sbi
->nls_disk
;
420 unsigned char long_slots
;
421 const char *fill_name
;
423 wchar_t bufuname
[14];
424 wchar_t *unicode
= NULL
;
425 unsigned char c
, work
[8], bufname
[56], *ptname
= bufname
;
426 unsigned long lpos
, dummy
, *furrfu
= &lpos
;
427 int uni_xlate
= sbi
->options
.unicode_xlate
;
428 int isvfat
= sbi
->options
.isvfat
;
429 int utf8
= sbi
->options
.utf8
;
430 int nocase
= sbi
->options
.nocase
;
431 unsigned short opt_shortname
= sbi
->options
.shortname
;
433 int chi
, chl
, i
, i2
, j
, last
, last_u
, dotoffset
= 0;
440 /* Fake . and .. for the root directory. */
441 if (inode
->i_ino
== MSDOS_ROOT_INO
) {
443 if (filldir(dirent
, "..", cpos
+1, cpos
, MSDOS_ROOT_INO
, DT_DIR
) < 0)
454 if (cpos
& (sizeof(struct msdos_dir_entry
)-1)) {
462 if (fat_get_entry(inode
, &cpos
, &bh
, &de
) == -1)
464 /* Check for long filename entry */
466 if (de
->name
[0] == DELETED_FLAG
)
468 if (de
->attr
!= ATTR_EXT
&& (de
->attr
& ATTR_VOLUME
))
470 if (de
->attr
!= ATTR_EXT
&& IS_FREE(de
->name
))
473 if ((de
->attr
& ATTR_VOLUME
) || IS_FREE(de
->name
))
477 if (isvfat
&& de
->attr
== ATTR_EXT
) {
478 struct msdos_dir_slot
*ds
;
483 unsigned char alias_checksum
;
486 unicode
= (wchar_t *)__get_free_page(GFP_KERNEL
);
496 ds
= (struct msdos_dir_slot
*) de
;
501 if (slots
> 20 || !slots
) /* ceil(256 * 2 / 26) */
504 alias_checksum
= ds
->alias_checksum
;
512 fat16_towchar(unicode
+ offset
, ds
->name0_4
, 5);
513 fat16_towchar(unicode
+ offset
+ 5, ds
->name5_10
, 6);
514 fat16_towchar(unicode
+ offset
+ 11, ds
->name11_12
, 2);
517 unicode
[offset
+ 13] = 0;
519 if (fat_get_entry(inode
, &cpos
, &bh
, &de
) == -1)
523 ds
= (struct msdos_dir_slot
*) de
;
524 if (ds
->attr
!= ATTR_EXT
)
525 goto RecEnd
; /* XXX */
526 if ((ds
->id
& ~0x40) != slot
)
528 if (ds
->alias_checksum
!= alias_checksum
)
531 if (de
->name
[0] == DELETED_FLAG
)
533 if (de
->attr
== ATTR_EXT
)
535 if (IS_FREE(de
->name
) || (de
->attr
& ATTR_VOLUME
))
537 for (sum
= 0, i
= 0; i
< 11; i
++)
538 sum
= (((sum
&1)<<7)|((sum
&0xfe)>>1)) + de
->name
[i
];
539 if (sum
!= alias_checksum
)
543 if (sbi
->options
.dotsOK
) {
546 if (de
->attr
& ATTR_HIDDEN
) {
552 memcpy(work
, de
->name
, sizeof(de
->name
));
553 /* see namei.c, msdos_format_name */
556 for (i
= 0, j
= 0, last
= 0, last_u
= 0; i
< 8;) {
557 if (!(c
= work
[i
])) break;
558 chl
= fat_shortname2uni(nls_disk
, &work
[i
], 8 - i
,
559 &bufuname
[j
++], opt_shortname
,
560 de
->lcase
& CASE_LOWER_BASE
);
562 ptname
[i
++] = (!nocase
&& c
>='A' && c
<='Z') ? c
+32 : c
;
569 for (chi
= 0; chi
< chl
&& i
< 8; chi
++) {
577 fat_short2uni(nls_disk
, ".", 1, &bufuname
[j
++]);
579 for (i2
= 0; i2
< 3;) {
580 if (!(c
= de
->ext
[i2
])) break;
581 chl
= fat_shortname2uni(nls_disk
, &de
->ext
[i2
], 3 - i2
,
582 &bufuname
[j
++], opt_shortname
,
583 de
->lcase
& CASE_LOWER_EXT
);
586 ptname
[i
++] = (!nocase
&& c
>='A' && c
<='Z') ? c
+32 : c
;
593 for (chi
= 0; chi
< chl
&& i2
< 3; chi
++) {
594 ptname
[i
++] = de
->ext
[i2
++];
602 i
= last
+ dotoffset
;
605 lpos
= cpos
- (long_slots
+1)*sizeof(struct msdos_dir_entry
);
606 if (!memcmp(de
->name
, MSDOS_DOT
, MSDOS_NAME
))
608 else if (!memcmp(de
->name
, MSDOS_DOTDOT
, MSDOS_NAME
)) {
609 inum
= parent_ino(filp
->f_dentry
);
611 loff_t i_pos
= fat_make_i_pos(sb
, bh
, de
);
612 struct inode
*tmp
= fat_iget(sb
, i_pos
);
617 inum
= iunique(sb
, MSDOS_ROOT_INO
);
621 bufuname
[j
] = 0x0000;
622 i
= utf8
? utf8_wcstombs(bufname
, bufuname
, sizeof(bufname
))
623 : uni16_to_x8(bufname
, bufuname
, uni_xlate
, nls_io
);
628 if (!short_only
&& long_slots
) {
629 /* convert the unicode long name. 261 is maximum size
630 * of unicode buffer. (13 * slots + nul) */
631 void *longname
= unicode
+ 261;
632 int buf_size
= PAGE_SIZE
- (261 * sizeof(unicode
[0]));
634 ? utf8_wcstombs(longname
, unicode
, buf_size
)
635 : uni16_to_x8(longname
, unicode
, uni_xlate
, nls_io
);
638 fill_name
= longname
;
641 /* hack for fat_ioctl_filldir() */
642 struct fat_ioctl_filldir_callback
*p
= dirent
;
644 p
->longname
= longname
;
645 p
->long_len
= long_len
;
646 p
->shortname
= bufname
;
652 if (filldir(dirent
, fill_name
, fill_len
, *furrfu
, inum
,
653 (de
->attr
& ATTR_DIR
) ? DT_DIR
: DT_REG
) < 0)
665 free_page((unsigned long)unicode
);
671 static int fat_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
673 struct inode
*inode
= filp
->f_dentry
->d_inode
;
674 return fat_readdirx(inode
, filp
, dirent
, filldir
, 0, 0);
677 static int fat_ioctl_filldir(void *__buf
, const char *name
, int name_len
,
678 loff_t offset
, ino_t ino
, unsigned int d_type
)
680 struct fat_ioctl_filldir_callback
*buf
= __buf
;
681 struct dirent __user
*d1
= buf
->dirent
;
682 struct dirent __user
*d2
= d1
+ 1;
689 /* dirent has only short name */
690 if (name_len
>= sizeof(d1
->d_name
))
691 name_len
= sizeof(d1
->d_name
) - 1;
693 if (put_user(0, d2
->d_name
) ||
694 put_user(0, &d2
->d_reclen
) ||
695 copy_to_user(d1
->d_name
, name
, name_len
) ||
696 put_user(0, d1
->d_name
+ name_len
) ||
697 put_user(name_len
, &d1
->d_reclen
))
700 /* dirent has short and long name */
701 const char *longname
= buf
->longname
;
702 int long_len
= buf
->long_len
;
703 const char *shortname
= buf
->shortname
;
704 int short_len
= buf
->short_len
;
706 if (long_len
>= sizeof(d1
->d_name
))
707 long_len
= sizeof(d1
->d_name
) - 1;
708 if (short_len
>= sizeof(d1
->d_name
))
709 short_len
= sizeof(d1
->d_name
) - 1;
711 if (copy_to_user(d2
->d_name
, longname
, long_len
) ||
712 put_user(0, d2
->d_name
+ long_len
) ||
713 put_user(long_len
, &d2
->d_reclen
) ||
714 put_user(ino
, &d2
->d_ino
) ||
715 put_user(offset
, &d2
->d_off
) ||
716 copy_to_user(d1
->d_name
, shortname
, short_len
) ||
717 put_user(0, d1
->d_name
+ short_len
) ||
718 put_user(short_len
, &d1
->d_reclen
))
723 buf
->result
= -EFAULT
;
727 static int fat_dir_ioctl(struct inode
* inode
, struct file
* filp
,
728 unsigned int cmd
, unsigned long arg
)
730 struct fat_ioctl_filldir_callback buf
;
731 struct dirent __user
*d1
;
732 int ret
, short_only
, both
;
735 case VFAT_IOCTL_READDIR_SHORT
:
739 case VFAT_IOCTL_READDIR_BOTH
:
744 return fat_generic_ioctl(inode
, filp
, cmd
, arg
);
747 d1
= (struct dirent __user
*)arg
;
748 if (!access_ok(VERIFY_WRITE
, d1
, sizeof(struct dirent
[2])))
751 * Yes, we don't need this put_user() absolutely. However old
752 * code didn't return the right value. So, app use this value,
753 * in order to check whether it is EOF.
755 if (put_user(0, &d1
->d_reclen
))
762 if (!IS_DEADDIR(inode
)) {
763 ret
= fat_readdirx(inode
, filp
, &buf
, fat_ioctl_filldir
,
772 struct file_operations fat_dir_operations
= {
773 .read
= generic_read_dir
,
774 .readdir
= fat_readdir
,
775 .ioctl
= fat_dir_ioctl
,
779 static int fat_get_short_entry(struct inode
*dir
, loff_t
*pos
,
780 struct buffer_head
**bh
,
781 struct msdos_dir_entry
**de
)
783 while (fat_get_entry(dir
, pos
, bh
, de
) >= 0) {
784 /* free entry or long name entry or volume label */
785 if (!IS_FREE((*de
)->name
) && !((*de
)->attr
& ATTR_VOLUME
))
792 * The ".." entry can not provide the "struct fat_slot_info" informations
793 * for inode. So, this function provide the some informations only.
795 int fat_get_dotdot_entry(struct inode
*dir
, struct buffer_head
**bh
,
796 struct msdos_dir_entry
**de
, loff_t
*i_pos
)
802 while (fat_get_short_entry(dir
, &offset
, bh
, de
) >= 0) {
803 if (!strncmp((*de
)->name
, MSDOS_DOTDOT
, MSDOS_NAME
)) {
804 *i_pos
= fat_make_i_pos(dir
->i_sb
, *bh
, *de
);
811 EXPORT_SYMBOL(fat_get_dotdot_entry
);
813 /* See if directory is empty */
814 int fat_dir_empty(struct inode
*dir
)
816 struct buffer_head
*bh
;
817 struct msdos_dir_entry
*de
;
823 while (fat_get_short_entry(dir
, &cpos
, &bh
, &de
) >= 0) {
824 if (strncmp(de
->name
, MSDOS_DOT
, MSDOS_NAME
) &&
825 strncmp(de
->name
, MSDOS_DOTDOT
, MSDOS_NAME
)) {
834 EXPORT_SYMBOL(fat_dir_empty
);
837 * fat_subdirs counts the number of sub-directories of dir. It can be run
838 * on directories being created.
840 int fat_subdirs(struct inode
*dir
)
842 struct buffer_head
*bh
;
843 struct msdos_dir_entry
*de
;
849 while (fat_get_short_entry(dir
, &cpos
, &bh
, &de
) >= 0) {
850 if (de
->attr
& ATTR_DIR
)
858 * Scans a directory for a given file (name points to its formatted name).
859 * Returns an error code or zero.
861 int fat_scan(struct inode
*dir
, const unsigned char *name
,
862 struct fat_slot_info
*sinfo
)
864 struct super_block
*sb
= dir
->i_sb
;
868 while (fat_get_short_entry(dir
, &sinfo
->slot_off
, &sinfo
->bh
,
870 if (!strncmp(sinfo
->de
->name
, name
, MSDOS_NAME
)) {
871 sinfo
->slot_off
-= sizeof(*sinfo
->de
);
873 sinfo
->i_pos
= fat_make_i_pos(sb
, sinfo
->bh
, sinfo
->de
);
880 EXPORT_SYMBOL(fat_scan
);
882 static int __fat_remove_entries(struct inode
*dir
, loff_t pos
, int nr_slots
)
884 struct super_block
*sb
= dir
->i_sb
;
885 struct buffer_head
*bh
;
886 struct msdos_dir_entry
*de
, *endp
;
887 int err
= 0, orig_slots
;
891 if (fat_get_entry(dir
, &pos
, &bh
, &de
) < 0) {
896 orig_slots
= nr_slots
;
897 endp
= (struct msdos_dir_entry
*)(bh
->b_data
+ sb
->s_blocksize
);
898 while (nr_slots
&& de
< endp
) {
899 de
->name
[0] = DELETED_FLAG
;
903 mark_buffer_dirty(bh
);
905 err
= sync_dirty_buffer(bh
);
910 /* pos is *next* de's position, so this does `- sizeof(de)' */
911 pos
+= ((orig_slots
- nr_slots
) * sizeof(*de
)) - sizeof(*de
);
917 int fat_remove_entries(struct inode
*dir
, struct fat_slot_info
*sinfo
)
919 struct msdos_dir_entry
*de
;
920 struct buffer_head
*bh
;
921 int err
= 0, nr_slots
;
924 * First stage: Remove the shortname. By this, the directory
927 nr_slots
= sinfo
->nr_slots
;
932 while (nr_slots
&& de
>= (struct msdos_dir_entry
*)bh
->b_data
) {
933 de
->name
[0] = DELETED_FLAG
;
937 mark_buffer_dirty(bh
);
939 err
= sync_dirty_buffer(bh
);
947 * Second stage: remove the remaining longname slots.
948 * (This directory entry is already removed, and so return
951 err
= __fat_remove_entries(dir
, sinfo
->slot_off
, nr_slots
);
954 "FAT: Couldn't remove the long name slots\n");
958 dir
->i_mtime
= dir
->i_atime
= CURRENT_TIME_SEC
;
960 (void)fat_sync_inode(dir
);
962 mark_inode_dirty(dir
);
967 EXPORT_SYMBOL(fat_remove_entries
);
969 static int fat_zeroed_cluster(struct inode
*dir
, sector_t blknr
, int nr_used
,
970 struct buffer_head
**bhs
, int nr_bhs
)
972 struct super_block
*sb
= dir
->i_sb
;
973 sector_t last_blknr
= blknr
+ MSDOS_SB(sb
)->sec_per_clus
;
976 /* Zeroing the unused blocks on this cluster */
979 while (blknr
< last_blknr
) {
980 bhs
[n
] = sb_getblk(sb
, blknr
);
985 memset(bhs
[n
]->b_data
, 0, sb
->s_blocksize
);
986 set_buffer_uptodate(bhs
[n
]);
987 mark_buffer_dirty(bhs
[n
]);
992 if (IS_DIRSYNC(dir
)) {
993 err
= fat_sync_bhs(bhs
, n
);
997 for (i
= 0; i
< n
; i
++)
1002 if (IS_DIRSYNC(dir
)) {
1003 err
= fat_sync_bhs(bhs
, n
);
1007 for (i
= 0; i
< n
; i
++)
1013 for (i
= 0; i
< n
; i
++)
1018 int fat_alloc_new_dir(struct inode
*dir
, struct timespec
*ts
)
1020 struct super_block
*sb
= dir
->i_sb
;
1021 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
1022 struct buffer_head
*bhs
[MAX_BUF_PER_PAGE
];
1023 struct msdos_dir_entry
*de
;
1028 err
= fat_alloc_clusters(dir
, &cluster
, 1);
1032 blknr
= fat_clus_to_blknr(sbi
, cluster
);
1033 bhs
[0] = sb_getblk(sb
, blknr
);
1039 fat_date_unix2dos(ts
->tv_sec
, &time
, &date
);
1041 de
= (struct msdos_dir_entry
*)bhs
[0]->b_data
;
1042 /* filling the new directory slots ("." and ".." entries) */
1043 memcpy(de
[0].name
, MSDOS_DOT
, MSDOS_NAME
);
1044 memcpy(de
[1].name
, MSDOS_DOTDOT
, MSDOS_NAME
);
1045 de
->attr
= de
[1].attr
= ATTR_DIR
;
1046 de
[0].lcase
= de
[1].lcase
= 0;
1047 de
[0].time
= de
[1].time
= time
;
1048 de
[0].date
= de
[1].date
= date
;
1049 de
[0].ctime_cs
= de
[1].ctime_cs
= 0;
1050 if (sbi
->options
.isvfat
) {
1051 /* extra timestamps */
1052 de
[0].ctime
= de
[1].ctime
= time
;
1053 de
[0].adate
= de
[0].cdate
= de
[1].adate
= de
[1].cdate
= date
;
1055 de
[0].ctime
= de
[1].ctime
= 0;
1056 de
[0].adate
= de
[0].cdate
= de
[1].adate
= de
[1].cdate
= 0;
1058 de
[0].start
= cpu_to_le16(cluster
);
1059 de
[0].starthi
= cpu_to_le16(cluster
>> 16);
1060 de
[1].start
= cpu_to_le16(MSDOS_I(dir
)->i_logstart
);
1061 de
[1].starthi
= cpu_to_le16(MSDOS_I(dir
)->i_logstart
>> 16);
1062 de
[0].size
= de
[1].size
= 0;
1063 memset(de
+ 2, 0, sb
->s_blocksize
- 2 * sizeof(*de
));
1064 set_buffer_uptodate(bhs
[0]);
1065 mark_buffer_dirty(bhs
[0]);
1067 err
= fat_zeroed_cluster(dir
, blknr
, 1, bhs
, MAX_BUF_PER_PAGE
);
1074 fat_free_clusters(dir
, cluster
);
1079 EXPORT_SYMBOL(fat_alloc_new_dir
);
1081 static int fat_add_new_entries(struct inode
*dir
, void *slots
, int nr_slots
,
1082 int *nr_cluster
, struct msdos_dir_entry
**de
,
1083 struct buffer_head
**bh
, loff_t
*i_pos
)
1085 struct super_block
*sb
= dir
->i_sb
;
1086 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
1087 struct buffer_head
*bhs
[MAX_BUF_PER_PAGE
];
1088 sector_t blknr
, start_blknr
, last_blknr
;
1089 unsigned long size
, copy
;
1090 int err
, i
, n
, offset
, cluster
[2];
1093 * The minimum cluster size is 512bytes, and maximum entry
1094 * size is 32*slots (672bytes). So, iff the cluster size is
1095 * 512bytes, we may need two clusters.
1097 size
= nr_slots
* sizeof(struct msdos_dir_entry
);
1098 *nr_cluster
= (size
+ (sbi
->cluster_size
- 1)) >> sbi
->cluster_bits
;
1099 BUG_ON(*nr_cluster
> 2);
1101 err
= fat_alloc_clusters(dir
, cluster
, *nr_cluster
);
1106 * First stage: Fill the directory entry. NOTE: This cluster
1107 * is not referenced from any inode yet, so updates order is
1112 start_blknr
= blknr
= fat_clus_to_blknr(sbi
, cluster
[i
]);
1113 last_blknr
= start_blknr
+ sbi
->sec_per_clus
;
1114 while (blknr
< last_blknr
) {
1115 bhs
[n
] = sb_getblk(sb
, blknr
);
1121 /* fill the directory entry */
1122 copy
= min(size
, sb
->s_blocksize
);
1123 memcpy(bhs
[n
]->b_data
, slots
, copy
);
1126 set_buffer_uptodate(bhs
[n
]);
1127 mark_buffer_dirty(bhs
[n
]);
1133 } while (++i
< *nr_cluster
);
1135 memset(bhs
[n
]->b_data
+ copy
, 0, sb
->s_blocksize
- copy
);
1136 offset
= copy
- sizeof(struct msdos_dir_entry
);
1139 *de
= (struct msdos_dir_entry
*)((*bh
)->b_data
+ offset
);
1140 *i_pos
= fat_make_i_pos(sb
, *bh
, *de
);
1142 /* Second stage: clear the rest of cluster, and write outs */
1143 err
= fat_zeroed_cluster(dir
, start_blknr
, ++n
, bhs
, MAX_BUF_PER_PAGE
);
1154 for (i
= 0; i
< n
; i
++)
1156 fat_free_clusters(dir
, cluster
[0]);
1161 int fat_add_entries(struct inode
*dir
, void *slots
, int nr_slots
,
1162 struct fat_slot_info
*sinfo
)
1164 struct super_block
*sb
= dir
->i_sb
;
1165 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
1166 struct buffer_head
*bh
, *prev
, *bhs
[3]; /* 32*slots (672bytes) */
1167 struct msdos_dir_entry
*de
;
1168 int err
, free_slots
, i
, nr_bhs
;
1171 sinfo
->nr_slots
= nr_slots
;
1173 /* First stage: search free direcotry entries */
1174 free_slots
= nr_bhs
= 0;
1178 while (fat_get_entry(dir
, &pos
, &bh
, &de
) > -1) {
1179 /* check the maximum size of directory */
1180 if (pos
>= FAT_MAX_DIR_SIZE
)
1183 if (IS_FREE(de
->name
)) {
1186 bhs
[nr_bhs
] = prev
= bh
;
1190 if (free_slots
== nr_slots
)
1193 for (i
= 0; i
< nr_bhs
; i
++)
1196 free_slots
= nr_bhs
= 0;
1199 if (dir
->i_ino
== MSDOS_ROOT_INO
) {
1200 if (sbi
->fat_bits
!= 32)
1202 } else if (MSDOS_I(dir
)->i_start
== 0) {
1203 printk(KERN_ERR
"FAT: Corrupted directory (i_pos %lld)\n",
1204 MSDOS_I(dir
)->i_pos
);
1211 pos
-= free_slots
* sizeof(*de
);
1212 nr_slots
-= free_slots
;
1215 * Second stage: filling the free entries with new entries.
1216 * NOTE: If this slots has shortname, first, we write
1217 * the long name slots, then write the short name.
1219 int size
= free_slots
* sizeof(*de
);
1220 int offset
= pos
& (sb
->s_blocksize
- 1);
1221 int long_bhs
= nr_bhs
- (nr_slots
== 0);
1223 /* Fill the long name slots. */
1224 for (i
= 0; i
< long_bhs
; i
++) {
1225 int copy
= min_t(int, sb
->s_blocksize
- offset
, size
);
1226 memcpy(bhs
[i
]->b_data
+ offset
, slots
, copy
);
1227 mark_buffer_dirty(bhs
[i
]);
1232 if (long_bhs
&& IS_DIRSYNC(dir
))
1233 err
= fat_sync_bhs(bhs
, long_bhs
);
1234 if (!err
&& i
< nr_bhs
) {
1235 /* Fill the short name slot. */
1236 int copy
= min_t(int, sb
->s_blocksize
- offset
, size
);
1237 memcpy(bhs
[i
]->b_data
+ offset
, slots
, copy
);
1238 mark_buffer_dirty(bhs
[i
]);
1239 if (IS_DIRSYNC(dir
))
1240 err
= sync_dirty_buffer(bhs
[i
]);
1242 for (i
= 0; i
< nr_bhs
; i
++)
1249 int cluster
, nr_cluster
;
1252 * Third stage: allocate the cluster for new entries.
1253 * And initialize the cluster with new entries, then
1254 * add the cluster to dir.
1256 cluster
= fat_add_new_entries(dir
, slots
, nr_slots
, &nr_cluster
,
1262 err
= fat_chain_add(dir
, cluster
, nr_cluster
);
1264 fat_free_clusters(dir
, cluster
);
1267 if (dir
->i_size
& (sbi
->cluster_size
- 1)) {
1268 fat_fs_panic(sb
, "Odd directory size");
1269 dir
->i_size
= (dir
->i_size
+ sbi
->cluster_size
- 1)
1270 & ~((loff_t
)sbi
->cluster_size
- 1);
1272 dir
->i_size
+= nr_cluster
<< sbi
->cluster_bits
;
1273 MSDOS_I(dir
)->mmu_private
+= nr_cluster
<< sbi
->cluster_bits
;
1275 sinfo
->slot_off
= pos
;
1278 sinfo
->i_pos
= fat_make_i_pos(sb
, sinfo
->bh
, sinfo
->de
);
1284 for (i
= 0; i
< nr_bhs
; i
++)
1291 __fat_remove_entries(dir
, pos
, free_slots
);
1295 EXPORT_SYMBOL(fat_add_entries
);