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/smp_lock.h>
20 #include <linux/buffer_head.h>
21 #include <linux/compat.h>
22 #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_find_get_block(sb
, phys
);
49 if (bh
== NULL
|| !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
;
71 unsigned long mapped_blocks
;
79 iblock
= *pos
>> sb
->s_blocksize_bits
;
80 err
= fat_bmap(dir
, iblock
, &phys
, &mapped_blocks
, 0);
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",
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 UTF-8, 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
, const wchar_t *uni
, int len
,
127 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 "
170 static inline int fat_uni_to_x8(struct msdos_sb_info
*sbi
, const wchar_t *uni
,
171 unsigned char *buf
, int size
)
173 if (sbi
->options
.utf8
)
174 return utf8_wcstombs(buf
, uni
, size
);
176 return uni16_to_x8(buf
, uni
, size
, sbi
->options
.unicode_xlate
,
181 fat_short2uni(struct nls_table
*t
, unsigned char *c
, int clen
, wchar_t *uni
)
185 charlen
= t
->char2uni(c
, clen
, uni
);
187 *uni
= 0x003f; /* a question mark */
194 fat_short2lower_uni(struct nls_table
*t
, unsigned char *c
, int clen
, wchar_t *uni
)
199 charlen
= t
->char2uni(c
, clen
, &wc
);
201 *uni
= 0x003f; /* a question mark */
203 } else if (charlen
<= 1) {
204 unsigned char nc
= t
->charset2lower
[*c
];
209 if ( (charlen
= t
->char2uni(&nc
, 1, uni
)) < 0) {
210 *uni
= 0x003f; /* a question mark */
220 fat_shortname2uni(struct nls_table
*nls
, unsigned char *buf
, int buf_size
,
221 wchar_t *uni_buf
, unsigned short opt
, int lower
)
225 if (opt
& VFAT_SFN_DISPLAY_LOWER
)
226 len
= fat_short2lower_uni(nls
, buf
, buf_size
, uni_buf
);
227 else if (opt
& VFAT_SFN_DISPLAY_WIN95
)
228 len
= fat_short2uni(nls
, buf
, buf_size
, uni_buf
);
229 else if (opt
& VFAT_SFN_DISPLAY_WINNT
) {
231 len
= fat_short2lower_uni(nls
, buf
, buf_size
, uni_buf
);
233 len
= fat_short2uni(nls
, buf
, buf_size
, uni_buf
);
235 len
= fat_short2uni(nls
, buf
, buf_size
, uni_buf
);
240 static inline int fat_name_match(struct msdos_sb_info
*sbi
,
241 const unsigned char *a
, int a_len
,
242 const unsigned char *b
, int b_len
)
247 if (sbi
->options
.name_check
!= 's')
248 return !nls_strnicmp(sbi
->nls_io
, a
, b
, a_len
);
250 return !memcmp(a
, b
, a_len
);
253 enum { PARSE_INVALID
= 1, PARSE_NOT_LONGNAME
, PARSE_EOF
, };
256 * fat_parse_long - Parse extended directory entry.
258 * This function returns zero on success, negative value on error, or one of
261 * %PARSE_INVALID - Directory entry is invalid.
262 * %PARSE_NOT_LONGNAME - Directory entry does not contain longname.
263 * %PARSE_EOF - Directory has no more entries.
265 static int fat_parse_long(struct inode
*dir
, loff_t
*pos
,
266 struct buffer_head
**bh
, struct msdos_dir_entry
**de
,
267 wchar_t **unicode
, unsigned char *nr_slots
)
269 struct msdos_dir_slot
*ds
;
270 unsigned char id
, slot
, slots
, alias_checksum
;
273 *unicode
= __getname();
281 ds
= (struct msdos_dir_slot
*)*de
;
284 return PARSE_INVALID
;
286 if (slots
> 20 || !slots
) /* ceil(256 * 2 / 26) */
287 return PARSE_INVALID
;
289 alias_checksum
= ds
->alias_checksum
;
297 fat16_towchar(*unicode
+ offset
, ds
->name0_4
, 5);
298 fat16_towchar(*unicode
+ offset
+ 5, ds
->name5_10
, 6);
299 fat16_towchar(*unicode
+ offset
+ 11, ds
->name11_12
, 2);
302 (*unicode
)[offset
+ 13] = 0;
303 if (fat_get_entry(dir
, pos
, bh
, de
) < 0)
307 ds
= (struct msdos_dir_slot
*)*de
;
308 if (ds
->attr
!= ATTR_EXT
)
309 return PARSE_NOT_LONGNAME
;
310 if ((ds
->id
& ~0x40) != slot
)
312 if (ds
->alias_checksum
!= alias_checksum
)
315 if ((*de
)->name
[0] == DELETED_FLAG
)
316 return PARSE_INVALID
;
317 if ((*de
)->attr
== ATTR_EXT
)
319 if (IS_FREE((*de
)->name
) || ((*de
)->attr
& ATTR_VOLUME
))
320 return PARSE_INVALID
;
321 if (fat_checksum((*de
)->name
) != alias_checksum
)
328 * Maximum buffer size of short name.
329 * [(MSDOS_NAME + '.') * max one char + nul]
330 * For msdos style, ['.' (hidden) + MSDOS_NAME + '.' + nul]
332 #define FAT_MAX_SHORT_SIZE ((MSDOS_NAME + 1) * NLS_MAX_CHARSET_SIZE + 1)
334 * Maximum buffer size of unicode chars from slots.
335 * [(max longname slots * 13 (size in a slot) + nul) * sizeof(wchar_t)]
337 #define FAT_MAX_UNI_CHARS ((MSDOS_SLOTS - 1) * 13 + 1)
338 #define FAT_MAX_UNI_SIZE (FAT_MAX_UNI_CHARS * sizeof(wchar_t))
341 * Return values: negative -> error, 0 -> not found, positive -> found,
342 * value is the total amount of slots, including the shortname entry.
344 int fat_search_long(struct inode
*inode
, const unsigned char *name
,
345 int name_len
, struct fat_slot_info
*sinfo
)
347 struct super_block
*sb
= inode
->i_sb
;
348 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
349 struct buffer_head
*bh
= NULL
;
350 struct msdos_dir_entry
*de
;
351 struct nls_table
*nls_disk
= sbi
->nls_disk
;
352 unsigned char nr_slots
;
353 wchar_t bufuname
[14];
354 wchar_t *unicode
= NULL
;
355 unsigned char work
[MSDOS_NAME
];
356 unsigned char bufname
[FAT_MAX_SHORT_SIZE
];
357 unsigned short opt_shortname
= sbi
->options
.shortname
;
359 int chl
, i
, j
, last_u
, err
, len
;
363 if (fat_get_entry(inode
, &cpos
, &bh
, &de
) == -1)
367 if (de
->name
[0] == DELETED_FLAG
)
369 if (de
->attr
!= ATTR_EXT
&& (de
->attr
& ATTR_VOLUME
))
371 if (de
->attr
!= ATTR_EXT
&& IS_FREE(de
->name
))
373 if (de
->attr
== ATTR_EXT
) {
374 int status
= fat_parse_long(inode
, &cpos
, &bh
, &de
,
375 &unicode
, &nr_slots
);
379 } else if (status
== PARSE_INVALID
)
381 else if (status
== PARSE_NOT_LONGNAME
)
383 else if (status
== PARSE_EOF
)
387 memcpy(work
, de
->name
, sizeof(de
->name
));
388 /* see namei.c, msdos_format_name */
391 for (i
= 0, j
= 0, last_u
= 0; i
< 8;) {
394 chl
= fat_shortname2uni(nls_disk
, &work
[i
], 8 - i
,
395 &bufuname
[j
++], opt_shortname
,
396 de
->lcase
& CASE_LOWER_BASE
);
406 fat_short2uni(nls_disk
, ".", 1, &bufuname
[j
++]);
407 for (i
= 8; i
< MSDOS_NAME
;) {
410 chl
= fat_shortname2uni(nls_disk
, &work
[i
],
412 &bufuname
[j
++], opt_shortname
,
413 de
->lcase
& CASE_LOWER_EXT
);
425 /* Compare shortname */
426 bufuname
[last_u
] = 0x0000;
427 len
= fat_uni_to_x8(sbi
, bufuname
, bufname
, sizeof(bufname
));
428 if (fat_name_match(sbi
, name
, name_len
, bufname
, len
))
432 void *longname
= unicode
+ FAT_MAX_UNI_CHARS
;
433 int size
= PATH_MAX
- FAT_MAX_UNI_SIZE
;
435 /* Compare longname */
436 len
= fat_uni_to_x8(sbi
, unicode
, longname
, size
);
437 if (fat_name_match(sbi
, name
, name_len
, longname
, len
))
443 nr_slots
++; /* include the de */
444 sinfo
->slot_off
= cpos
- nr_slots
* sizeof(*de
);
445 sinfo
->nr_slots
= nr_slots
;
448 sinfo
->i_pos
= fat_make_i_pos(sb
, sinfo
->bh
, sinfo
->de
);
457 EXPORT_SYMBOL_GPL(fat_search_long
);
459 struct fat_ioctl_filldir_callback
{
463 const char *longname
;
465 const char *shortname
;
469 static int __fat_readdir(struct inode
*inode
, struct file
*filp
, void *dirent
,
470 filldir_t filldir
, int short_only
, int both
)
472 struct super_block
*sb
= inode
->i_sb
;
473 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
474 struct buffer_head
*bh
;
475 struct msdos_dir_entry
*de
;
476 struct nls_table
*nls_disk
= sbi
->nls_disk
;
477 unsigned char nr_slots
;
478 wchar_t bufuname
[14];
479 wchar_t *unicode
= NULL
;
480 unsigned char c
, work
[MSDOS_NAME
];
481 unsigned char bufname
[FAT_MAX_SHORT_SIZE
], *ptname
= bufname
;
482 unsigned short opt_shortname
= sbi
->options
.shortname
;
483 int isvfat
= sbi
->options
.isvfat
;
484 int nocase
= sbi
->options
.nocase
;
485 const char *fill_name
= NULL
;
487 unsigned long lpos
, dummy
, *furrfu
= &lpos
;
489 int chi
, chl
, i
, i2
, j
, last
, last_u
, dotoffset
= 0, fill_len
= 0;
495 /* Fake . and .. for the root directory. */
496 if (inode
->i_ino
== MSDOS_ROOT_INO
) {
498 if (filldir(dirent
, "..", cpos
+1, cpos
, MSDOS_ROOT_INO
, DT_DIR
) < 0)
509 if (cpos
& (sizeof(struct msdos_dir_entry
) - 1)) {
516 if (fat_get_entry(inode
, &cpos
, &bh
, &de
) == -1)
521 * Check for long filename entry, but if short_only, we don't
522 * need to parse long filename.
524 if (isvfat
&& !short_only
) {
525 if (de
->name
[0] == DELETED_FLAG
)
527 if (de
->attr
!= ATTR_EXT
&& (de
->attr
& ATTR_VOLUME
))
529 if (de
->attr
!= ATTR_EXT
&& IS_FREE(de
->name
))
532 if ((de
->attr
& ATTR_VOLUME
) || IS_FREE(de
->name
))
536 if (isvfat
&& de
->attr
== ATTR_EXT
) {
537 int status
= fat_parse_long(inode
, &cpos
, &bh
, &de
,
538 &unicode
, &nr_slots
);
543 } else if (status
== PARSE_INVALID
)
545 else if (status
== PARSE_NOT_LONGNAME
)
547 else if (status
== PARSE_EOF
)
551 void *longname
= unicode
+ FAT_MAX_UNI_CHARS
;
552 int size
= PATH_MAX
- FAT_MAX_UNI_SIZE
;
553 int len
= fat_uni_to_x8(sbi
, unicode
, longname
, size
);
555 fill_name
= longname
;
557 /* !both && !short_only, so we don't need shortname. */
563 if (sbi
->options
.dotsOK
) {
566 if (de
->attr
& ATTR_HIDDEN
) {
572 memcpy(work
, de
->name
, sizeof(de
->name
));
573 /* see namei.c, msdos_format_name */
576 for (i
= 0, j
= 0, last
= 0, last_u
= 0; i
< 8;) {
579 chl
= fat_shortname2uni(nls_disk
, &work
[i
], 8 - i
,
580 &bufuname
[j
++], opt_shortname
,
581 de
->lcase
& CASE_LOWER_BASE
);
583 ptname
[i
++] = (!nocase
&& c
>='A' && c
<='Z') ? c
+32 : c
;
590 for (chi
= 0; chi
< chl
&& i
< 8; chi
++) {
598 fat_short2uni(nls_disk
, ".", 1, &bufuname
[j
++]);
600 for (i2
= 8; i2
< MSDOS_NAME
;) {
603 chl
= fat_shortname2uni(nls_disk
, &work
[i2
], MSDOS_NAME
- i2
,
604 &bufuname
[j
++], opt_shortname
,
605 de
->lcase
& CASE_LOWER_EXT
);
608 ptname
[i
++] = (!nocase
&& c
>='A' && c
<='Z') ? c
+32 : c
;
615 for (chi
= 0; chi
< chl
&& i2
< MSDOS_NAME
; chi
++) {
616 ptname
[i
++] = work
[i2
++];
624 i
= last
+ dotoffset
;
628 bufuname
[j
] = 0x0000;
629 i
= fat_uni_to_x8(sbi
, bufuname
, bufname
, sizeof(bufname
));
632 /* hack for fat_ioctl_filldir() */
633 struct fat_ioctl_filldir_callback
*p
= dirent
;
635 p
->longname
= fill_name
;
636 p
->long_len
= fill_len
;
637 p
->shortname
= bufname
;
647 lpos
= cpos
- (nr_slots
+ 1) * sizeof(struct msdos_dir_entry
);
648 if (!memcmp(de
->name
, MSDOS_DOT
, MSDOS_NAME
))
650 else if (!memcmp(de
->name
, MSDOS_DOTDOT
, MSDOS_NAME
)) {
651 inum
= parent_ino(filp
->f_path
.dentry
);
653 loff_t i_pos
= fat_make_i_pos(sb
, bh
, de
);
654 struct inode
*tmp
= fat_iget(sb
, i_pos
);
659 inum
= iunique(sb
, MSDOS_ROOT_INO
);
662 if (filldir(dirent
, fill_name
, fill_len
, *furrfu
, inum
,
663 (de
->attr
& ATTR_DIR
) ? DT_DIR
: DT_REG
) < 0)
681 static int fat_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
683 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
684 return __fat_readdir(inode
, filp
, dirent
, filldir
, 0, 0);
687 #define FAT_IOCTL_FILLDIR_FUNC(func, dirent_type) \
688 static int func(void *__buf, const char *name, int name_len, \
689 loff_t offset, u64 ino, unsigned int d_type) \
691 struct fat_ioctl_filldir_callback *buf = __buf; \
692 struct dirent_type __user *d1 = buf->dirent; \
693 struct dirent_type __user *d2 = d1 + 1; \
699 if (name != NULL) { \
700 /* dirent has only short name */ \
701 if (name_len >= sizeof(d1->d_name)) \
702 name_len = sizeof(d1->d_name) - 1; \
704 if (put_user(0, d2->d_name) || \
705 put_user(0, &d2->d_reclen) || \
706 copy_to_user(d1->d_name, name, name_len) || \
707 put_user(0, d1->d_name + name_len) || \
708 put_user(name_len, &d1->d_reclen)) \
711 /* dirent has short and long name */ \
712 const char *longname = buf->longname; \
713 int long_len = buf->long_len; \
714 const char *shortname = buf->shortname; \
715 int short_len = buf->short_len; \
717 if (long_len >= sizeof(d1->d_name)) \
718 long_len = sizeof(d1->d_name) - 1; \
719 if (short_len >= sizeof(d1->d_name)) \
720 short_len = sizeof(d1->d_name) - 1; \
722 if (copy_to_user(d2->d_name, longname, long_len) || \
723 put_user(0, d2->d_name + long_len) || \
724 put_user(long_len, &d2->d_reclen) || \
725 put_user(ino, &d2->d_ino) || \
726 put_user(offset, &d2->d_off) || \
727 copy_to_user(d1->d_name, shortname, short_len) || \
728 put_user(0, d1->d_name + short_len) || \
729 put_user(short_len, &d1->d_reclen)) \
734 buf->result = -EFAULT; \
738 FAT_IOCTL_FILLDIR_FUNC(fat_ioctl_filldir
, __fat_dirent
)
740 static int fat_ioctl_readdir(struct inode
*inode
, struct file
*filp
,
741 void __user
*dirent
, filldir_t filldir
,
742 int short_only
, int both
)
744 struct fat_ioctl_filldir_callback buf
;
749 mutex_lock(&inode
->i_mutex
);
751 if (!IS_DEADDIR(inode
)) {
752 ret
= __fat_readdir(inode
, filp
, &buf
, filldir
,
755 mutex_unlock(&inode
->i_mutex
);
761 static int fat_dir_ioctl(struct inode
*inode
, struct file
*filp
,
762 unsigned int cmd
, unsigned long arg
)
764 struct __fat_dirent __user
*d1
= (struct __fat_dirent __user
*)arg
;
765 int short_only
, both
;
768 case VFAT_IOCTL_READDIR_SHORT
:
772 case VFAT_IOCTL_READDIR_BOTH
:
777 return fat_generic_ioctl(inode
, filp
, cmd
, arg
);
780 if (!access_ok(VERIFY_WRITE
, d1
, sizeof(struct __fat_dirent
[2])))
783 * Yes, we don't need this put_user() absolutely. However old
784 * code didn't return the right value. So, app use this value,
785 * in order to check whether it is EOF.
787 if (put_user(0, &d1
->d_reclen
))
790 return fat_ioctl_readdir(inode
, filp
, d1
, fat_ioctl_filldir
,
795 #define VFAT_IOCTL_READDIR_BOTH32 _IOR('r', 1, struct compat_dirent[2])
796 #define VFAT_IOCTL_READDIR_SHORT32 _IOR('r', 2, struct compat_dirent[2])
798 FAT_IOCTL_FILLDIR_FUNC(fat_compat_ioctl_filldir
, compat_dirent
)
800 static long fat_compat_dir_ioctl(struct file
*filp
, unsigned cmd
,
803 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
804 struct compat_dirent __user
*d1
= compat_ptr(arg
);
805 int short_only
, both
;
808 case VFAT_IOCTL_READDIR_SHORT32
:
812 case VFAT_IOCTL_READDIR_BOTH32
:
820 if (!access_ok(VERIFY_WRITE
, d1
, sizeof(struct compat_dirent
[2])))
823 * Yes, we don't need this put_user() absolutely. However old
824 * code didn't return the right value. So, app use this value,
825 * in order to check whether it is EOF.
827 if (put_user(0, &d1
->d_reclen
))
830 return fat_ioctl_readdir(inode
, filp
, d1
, fat_compat_ioctl_filldir
,
833 #endif /* CONFIG_COMPAT */
835 const struct file_operations fat_dir_operations
= {
836 .llseek
= generic_file_llseek
,
837 .read
= generic_read_dir
,
838 .readdir
= fat_readdir
,
839 .ioctl
= fat_dir_ioctl
,
841 .compat_ioctl
= fat_compat_dir_ioctl
,
844 .llseek
= generic_file_llseek
,
847 static int fat_get_short_entry(struct inode
*dir
, loff_t
*pos
,
848 struct buffer_head
**bh
,
849 struct msdos_dir_entry
**de
)
851 while (fat_get_entry(dir
, pos
, bh
, de
) >= 0) {
852 /* free entry or long name entry or volume label */
853 if (!IS_FREE((*de
)->name
) && !((*de
)->attr
& ATTR_VOLUME
))
860 * The ".." entry can not provide the "struct fat_slot_info" informations
861 * for inode. So, this function provide the some informations only.
863 int fat_get_dotdot_entry(struct inode
*dir
, struct buffer_head
**bh
,
864 struct msdos_dir_entry
**de
, loff_t
*i_pos
)
870 while (fat_get_short_entry(dir
, &offset
, bh
, de
) >= 0) {
871 if (!strncmp((*de
)->name
, MSDOS_DOTDOT
, MSDOS_NAME
)) {
872 *i_pos
= fat_make_i_pos(dir
->i_sb
, *bh
, *de
);
879 EXPORT_SYMBOL_GPL(fat_get_dotdot_entry
);
881 /* See if directory is empty */
882 int fat_dir_empty(struct inode
*dir
)
884 struct buffer_head
*bh
;
885 struct msdos_dir_entry
*de
;
891 while (fat_get_short_entry(dir
, &cpos
, &bh
, &de
) >= 0) {
892 if (strncmp(de
->name
, MSDOS_DOT
, MSDOS_NAME
) &&
893 strncmp(de
->name
, MSDOS_DOTDOT
, MSDOS_NAME
)) {
902 EXPORT_SYMBOL_GPL(fat_dir_empty
);
905 * fat_subdirs counts the number of sub-directories of dir. It can be run
906 * on directories being created.
908 int fat_subdirs(struct inode
*dir
)
910 struct buffer_head
*bh
;
911 struct msdos_dir_entry
*de
;
917 while (fat_get_short_entry(dir
, &cpos
, &bh
, &de
) >= 0) {
918 if (de
->attr
& ATTR_DIR
)
926 * Scans a directory for a given file (name points to its formatted name).
927 * Returns an error code or zero.
929 int fat_scan(struct inode
*dir
, const unsigned char *name
,
930 struct fat_slot_info
*sinfo
)
932 struct super_block
*sb
= dir
->i_sb
;
936 while (fat_get_short_entry(dir
, &sinfo
->slot_off
, &sinfo
->bh
,
938 if (!strncmp(sinfo
->de
->name
, name
, MSDOS_NAME
)) {
939 sinfo
->slot_off
-= sizeof(*sinfo
->de
);
941 sinfo
->i_pos
= fat_make_i_pos(sb
, sinfo
->bh
, sinfo
->de
);
948 EXPORT_SYMBOL_GPL(fat_scan
);
950 static int __fat_remove_entries(struct inode
*dir
, loff_t pos
, int nr_slots
)
952 struct super_block
*sb
= dir
->i_sb
;
953 struct buffer_head
*bh
;
954 struct msdos_dir_entry
*de
, *endp
;
955 int err
= 0, orig_slots
;
959 if (fat_get_entry(dir
, &pos
, &bh
, &de
) < 0) {
964 orig_slots
= nr_slots
;
965 endp
= (struct msdos_dir_entry
*)(bh
->b_data
+ sb
->s_blocksize
);
966 while (nr_slots
&& de
< endp
) {
967 de
->name
[0] = DELETED_FLAG
;
971 mark_buffer_dirty(bh
);
973 err
= sync_dirty_buffer(bh
);
978 /* pos is *next* de's position, so this does `- sizeof(de)' */
979 pos
+= ((orig_slots
- nr_slots
) * sizeof(*de
)) - sizeof(*de
);
985 int fat_remove_entries(struct inode
*dir
, struct fat_slot_info
*sinfo
)
987 struct msdos_dir_entry
*de
;
988 struct buffer_head
*bh
;
989 int err
= 0, nr_slots
;
992 * First stage: Remove the shortname. By this, the directory
995 nr_slots
= sinfo
->nr_slots
;
1000 while (nr_slots
&& de
>= (struct msdos_dir_entry
*)bh
->b_data
) {
1001 de
->name
[0] = DELETED_FLAG
;
1005 mark_buffer_dirty(bh
);
1006 if (IS_DIRSYNC(dir
))
1007 err
= sync_dirty_buffer(bh
);
1015 * Second stage: remove the remaining longname slots.
1016 * (This directory entry is already removed, and so return
1019 err
= __fat_remove_entries(dir
, sinfo
->slot_off
, nr_slots
);
1022 "FAT: Couldn't remove the long name slots\n");
1026 dir
->i_mtime
= dir
->i_atime
= CURRENT_TIME_SEC
;
1027 if (IS_DIRSYNC(dir
))
1028 (void)fat_sync_inode(dir
);
1030 mark_inode_dirty(dir
);
1035 EXPORT_SYMBOL_GPL(fat_remove_entries
);
1037 static int fat_zeroed_cluster(struct inode
*dir
, sector_t blknr
, int nr_used
,
1038 struct buffer_head
**bhs
, int nr_bhs
)
1040 struct super_block
*sb
= dir
->i_sb
;
1041 sector_t last_blknr
= blknr
+ MSDOS_SB(sb
)->sec_per_clus
;
1044 /* Zeroing the unused blocks on this cluster */
1047 while (blknr
< last_blknr
) {
1048 bhs
[n
] = sb_getblk(sb
, blknr
);
1053 memset(bhs
[n
]->b_data
, 0, sb
->s_blocksize
);
1054 set_buffer_uptodate(bhs
[n
]);
1055 mark_buffer_dirty(bhs
[n
]);
1060 if (IS_DIRSYNC(dir
)) {
1061 err
= fat_sync_bhs(bhs
, n
);
1065 for (i
= 0; i
< n
; i
++)
1070 if (IS_DIRSYNC(dir
)) {
1071 err
= fat_sync_bhs(bhs
, n
);
1075 for (i
= 0; i
< n
; i
++)
1081 for (i
= 0; i
< n
; i
++)
1086 int fat_alloc_new_dir(struct inode
*dir
, struct timespec
*ts
)
1088 struct super_block
*sb
= dir
->i_sb
;
1089 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
1090 struct buffer_head
*bhs
[MAX_BUF_PER_PAGE
];
1091 struct msdos_dir_entry
*de
;
1097 err
= fat_alloc_clusters(dir
, &cluster
, 1);
1101 blknr
= fat_clus_to_blknr(sbi
, cluster
);
1102 bhs
[0] = sb_getblk(sb
, blknr
);
1108 fat_time_unix2fat(sbi
, ts
, &time
, &date
, &time_cs
);
1110 de
= (struct msdos_dir_entry
*)bhs
[0]->b_data
;
1111 /* filling the new directory slots ("." and ".." entries) */
1112 memcpy(de
[0].name
, MSDOS_DOT
, MSDOS_NAME
);
1113 memcpy(de
[1].name
, MSDOS_DOTDOT
, MSDOS_NAME
);
1114 de
->attr
= de
[1].attr
= ATTR_DIR
;
1115 de
[0].lcase
= de
[1].lcase
= 0;
1116 de
[0].time
= de
[1].time
= time
;
1117 de
[0].date
= de
[1].date
= date
;
1118 if (sbi
->options
.isvfat
) {
1119 /* extra timestamps */
1120 de
[0].ctime
= de
[1].ctime
= time
;
1121 de
[0].ctime_cs
= de
[1].ctime_cs
= time_cs
;
1122 de
[0].adate
= de
[0].cdate
= de
[1].adate
= de
[1].cdate
= date
;
1124 de
[0].ctime
= de
[1].ctime
= 0;
1125 de
[0].ctime_cs
= de
[1].ctime_cs
= 0;
1126 de
[0].adate
= de
[0].cdate
= de
[1].adate
= de
[1].cdate
= 0;
1128 de
[0].start
= cpu_to_le16(cluster
);
1129 de
[0].starthi
= cpu_to_le16(cluster
>> 16);
1130 de
[1].start
= cpu_to_le16(MSDOS_I(dir
)->i_logstart
);
1131 de
[1].starthi
= cpu_to_le16(MSDOS_I(dir
)->i_logstart
>> 16);
1132 de
[0].size
= de
[1].size
= 0;
1133 memset(de
+ 2, 0, sb
->s_blocksize
- 2 * sizeof(*de
));
1134 set_buffer_uptodate(bhs
[0]);
1135 mark_buffer_dirty(bhs
[0]);
1137 err
= fat_zeroed_cluster(dir
, blknr
, 1, bhs
, MAX_BUF_PER_PAGE
);
1144 fat_free_clusters(dir
, cluster
);
1149 EXPORT_SYMBOL_GPL(fat_alloc_new_dir
);
1151 static int fat_add_new_entries(struct inode
*dir
, void *slots
, int nr_slots
,
1152 int *nr_cluster
, struct msdos_dir_entry
**de
,
1153 struct buffer_head
**bh
, loff_t
*i_pos
)
1155 struct super_block
*sb
= dir
->i_sb
;
1156 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
1157 struct buffer_head
*bhs
[MAX_BUF_PER_PAGE
];
1158 sector_t blknr
, start_blknr
, last_blknr
;
1159 unsigned long size
, copy
;
1160 int err
, i
, n
, offset
, cluster
[2];
1163 * The minimum cluster size is 512bytes, and maximum entry
1164 * size is 32*slots (672bytes). So, iff the cluster size is
1165 * 512bytes, we may need two clusters.
1167 size
= nr_slots
* sizeof(struct msdos_dir_entry
);
1168 *nr_cluster
= (size
+ (sbi
->cluster_size
- 1)) >> sbi
->cluster_bits
;
1169 BUG_ON(*nr_cluster
> 2);
1171 err
= fat_alloc_clusters(dir
, cluster
, *nr_cluster
);
1176 * First stage: Fill the directory entry. NOTE: This cluster
1177 * is not referenced from any inode yet, so updates order is
1182 start_blknr
= blknr
= fat_clus_to_blknr(sbi
, cluster
[i
]);
1183 last_blknr
= start_blknr
+ sbi
->sec_per_clus
;
1184 while (blknr
< last_blknr
) {
1185 bhs
[n
] = sb_getblk(sb
, blknr
);
1191 /* fill the directory entry */
1192 copy
= min(size
, sb
->s_blocksize
);
1193 memcpy(bhs
[n
]->b_data
, slots
, copy
);
1196 set_buffer_uptodate(bhs
[n
]);
1197 mark_buffer_dirty(bhs
[n
]);
1203 } while (++i
< *nr_cluster
);
1205 memset(bhs
[n
]->b_data
+ copy
, 0, sb
->s_blocksize
- copy
);
1206 offset
= copy
- sizeof(struct msdos_dir_entry
);
1209 *de
= (struct msdos_dir_entry
*)((*bh
)->b_data
+ offset
);
1210 *i_pos
= fat_make_i_pos(sb
, *bh
, *de
);
1212 /* Second stage: clear the rest of cluster, and write outs */
1213 err
= fat_zeroed_cluster(dir
, start_blknr
, ++n
, bhs
, MAX_BUF_PER_PAGE
);
1224 for (i
= 0; i
< n
; i
++)
1226 fat_free_clusters(dir
, cluster
[0]);
1231 int fat_add_entries(struct inode
*dir
, void *slots
, int nr_slots
,
1232 struct fat_slot_info
*sinfo
)
1234 struct super_block
*sb
= dir
->i_sb
;
1235 struct msdos_sb_info
*sbi
= MSDOS_SB(sb
);
1236 struct buffer_head
*bh
, *prev
, *bhs
[3]; /* 32*slots (672bytes) */
1237 struct msdos_dir_entry
*de
;
1238 int err
, free_slots
, i
, nr_bhs
;
1241 sinfo
->nr_slots
= nr_slots
;
1243 /* First stage: search free direcotry entries */
1244 free_slots
= nr_bhs
= 0;
1248 while (fat_get_entry(dir
, &pos
, &bh
, &de
) > -1) {
1249 /* check the maximum size of directory */
1250 if (pos
>= FAT_MAX_DIR_SIZE
)
1253 if (IS_FREE(de
->name
)) {
1256 bhs
[nr_bhs
] = prev
= bh
;
1260 if (free_slots
== nr_slots
)
1263 for (i
= 0; i
< nr_bhs
; i
++)
1266 free_slots
= nr_bhs
= 0;
1269 if (dir
->i_ino
== MSDOS_ROOT_INO
) {
1270 if (sbi
->fat_bits
!= 32)
1272 } else if (MSDOS_I(dir
)->i_start
== 0) {
1273 printk(KERN_ERR
"FAT: Corrupted directory (i_pos %lld)\n",
1274 MSDOS_I(dir
)->i_pos
);
1281 pos
-= free_slots
* sizeof(*de
);
1282 nr_slots
-= free_slots
;
1285 * Second stage: filling the free entries with new entries.
1286 * NOTE: If this slots has shortname, first, we write
1287 * the long name slots, then write the short name.
1289 int size
= free_slots
* sizeof(*de
);
1290 int offset
= pos
& (sb
->s_blocksize
- 1);
1291 int long_bhs
= nr_bhs
- (nr_slots
== 0);
1293 /* Fill the long name slots. */
1294 for (i
= 0; i
< long_bhs
; i
++) {
1295 int copy
= min_t(int, sb
->s_blocksize
- offset
, size
);
1296 memcpy(bhs
[i
]->b_data
+ offset
, slots
, copy
);
1297 mark_buffer_dirty(bhs
[i
]);
1302 if (long_bhs
&& IS_DIRSYNC(dir
))
1303 err
= fat_sync_bhs(bhs
, long_bhs
);
1304 if (!err
&& i
< nr_bhs
) {
1305 /* Fill the short name slot. */
1306 int copy
= min_t(int, sb
->s_blocksize
- offset
, size
);
1307 memcpy(bhs
[i
]->b_data
+ offset
, slots
, copy
);
1308 mark_buffer_dirty(bhs
[i
]);
1309 if (IS_DIRSYNC(dir
))
1310 err
= sync_dirty_buffer(bhs
[i
]);
1312 for (i
= 0; i
< nr_bhs
; i
++)
1319 int cluster
, nr_cluster
;
1322 * Third stage: allocate the cluster for new entries.
1323 * And initialize the cluster with new entries, then
1324 * add the cluster to dir.
1326 cluster
= fat_add_new_entries(dir
, slots
, nr_slots
, &nr_cluster
,
1332 err
= fat_chain_add(dir
, cluster
, nr_cluster
);
1334 fat_free_clusters(dir
, cluster
);
1337 if (dir
->i_size
& (sbi
->cluster_size
- 1)) {
1338 fat_fs_panic(sb
, "Odd directory size");
1339 dir
->i_size
= (dir
->i_size
+ sbi
->cluster_size
- 1)
1340 & ~((loff_t
)sbi
->cluster_size
- 1);
1342 dir
->i_size
+= nr_cluster
<< sbi
->cluster_bits
;
1343 MSDOS_I(dir
)->mmu_private
+= nr_cluster
<< sbi
->cluster_bits
;
1345 sinfo
->slot_off
= pos
;
1348 sinfo
->i_pos
= fat_make_i_pos(sb
, sinfo
->bh
, sinfo
->de
);
1354 for (i
= 0; i
< nr_bhs
; i
++)
1361 __fat_remove_entries(dir
, pos
, free_slots
);
1365 EXPORT_SYMBOL_GPL(fat_add_entries
);