fat: cleanup fs/fat/dir.c
[linux-2.6/mini2440.git] / fs / fat / dir.c
bloba4410740627f6aab864057ad581e8ba25be60a6c
1 /*
2 * linux/fs/fat/dir.c
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/smp_lock.h>
21 #include <linux/buffer_head.h>
22 #include <linux/compat.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,
34 sector_t phys)
36 struct super_block *sb = dir->i_sb;
37 struct msdos_sb_info *sbi = MSDOS_SB(sb);
38 struct buffer_head *bh;
39 int sec;
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)
43 return;
44 /* root dir of FAT12/FAT16 */
45 if ((sbi->fat_bits != 32) && (dir->i_ino == MSDOS_ROOT_INO))
46 return;
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);
53 brelse(bh);
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
58 returned in bh.
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)
64 AV. we make bh NULL.
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;
72 int err, offset;
74 next:
75 if (*bh)
76 brelse(*bh);
78 *bh = NULL;
79 iblock = *pos >> sb->s_blocksize_bits;
80 err = fat_bmap(dir, iblock, &phys, &mapped_blocks);
81 if (err || !phys)
82 return -1; /* beyond EOF or error */
84 fat_dir_readahead(dir, iblock, phys);
86 *bh = sb_bread(sb, phys);
87 if (*bh == NULL) {
88 printk(KERN_ERR "FAT: Directory bread(block %llu) failed\n",
89 (unsigned long long)phys);
90 /* skip this block */
91 *pos = (iblock + 1) << sb->s_blocksize_bits;
92 goto next;
95 offset = *pos & (sb->s_blocksize - 1);
96 *pos += sizeof(struct msdos_dir_entry);
97 *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
99 return 0;
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 */
107 if (*bh && *de &&
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);
110 (*de)++;
111 return 0;
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)
129 const wchar_t *ip;
130 wchar_t ec;
131 unsigned char *op, nc;
132 int charlen;
133 int k;
135 ip = uni;
136 op = ascii;
138 while (*ip && ((len - NLS_MAX_CHARSET_SIZE) > 0)) {
139 ec = *ip++;
140 if ( (charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE)) > 0) {
141 op += charlen;
142 len -= charlen;
143 } else {
144 if (uni_xlate == 1) {
145 *op = ':';
146 for (k = 4; k > 0; k--) {
147 nc = ec & 0xF;
148 op[k] = nc > 9 ? nc + ('a' - 10)
149 : nc + '0';
150 ec >>= 4;
152 op += 5;
153 len -= 5;
154 } else {
155 *op++ = '?';
156 len--;
161 if (unlikely(*ip)) {
162 printk(KERN_WARNING "FAT: filename was truncated while "
163 "converting.");
166 *op = 0;
167 return (op - ascii);
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);
175 else
176 return uni16_to_x8(buf, uni, size, sbi->options.unicode_xlate,
177 sbi->nls_io);
180 static inline int
181 fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
183 int charlen;
185 charlen = t->char2uni(c, clen, uni);
186 if (charlen < 0) {
187 *uni = 0x003f; /* a question mark */
188 charlen = 1;
190 return charlen;
193 static inline int
194 fat_short2lower_uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
196 int charlen;
197 wchar_t wc;
199 charlen = t->char2uni(c, clen, &wc);
200 if (charlen < 0) {
201 *uni = 0x003f; /* a question mark */
202 charlen = 1;
203 } else if (charlen <= 1) {
204 unsigned char nc = t->charset2lower[*c];
206 if (!nc)
207 nc = *c;
209 if ( (charlen = t->char2uni(&nc, 1, uni)) < 0) {
210 *uni = 0x003f; /* a question mark */
211 charlen = 1;
213 } else
214 *uni = wc;
216 return charlen;
219 static inline int
220 fat_shortname2uni(struct nls_table *nls, unsigned char *buf, int buf_size,
221 wchar_t *uni_buf, unsigned short opt, int lower)
223 int len = 0;
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) {
230 if (lower)
231 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
232 else
233 len = fat_short2uni(nls, buf, buf_size, uni_buf);
234 } else
235 len = fat_short2uni(nls, buf, buf_size, uni_buf);
237 return len;
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)
244 if (a_len != b_len)
245 return 0;
247 if (sbi->options.name_check != 's')
248 return !nls_strnicmp(sbi->nls_io, a, b, a_len);
249 else
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
259 * the following:
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;
272 if (!*unicode) {
273 *unicode = __getname();
274 if (!*unicode) {
275 brelse(*bh);
276 return -ENOMEM;
279 parse_long:
280 slots = 0;
281 ds = (struct msdos_dir_slot *)*de;
282 id = ds->id;
283 if (!(id & 0x40))
284 return PARSE_INVALID;
285 slots = id & ~0x40;
286 if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */
287 return PARSE_INVALID;
288 *nr_slots = slots;
289 alias_checksum = ds->alias_checksum;
291 slot = slots;
292 while (1) {
293 int offset;
295 slot--;
296 offset = slot * 13;
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);
301 if (ds->id & 0x40)
302 (*unicode)[offset + 13] = 0;
303 if (fat_get_entry(dir, pos, bh, de) < 0)
304 return PARSE_EOF;
305 if (slot == 0)
306 break;
307 ds = (struct msdos_dir_slot *)*de;
308 if (ds->attr != ATTR_EXT)
309 return PARSE_NOT_LONGNAME;
310 if ((ds->id & ~0x40) != slot)
311 goto parse_long;
312 if (ds->alias_checksum != alias_checksum)
313 goto parse_long;
315 if ((*de)->name[0] == DELETED_FLAG)
316 return PARSE_INVALID;
317 if ((*de)->attr == ATTR_EXT)
318 goto parse_long;
319 if (IS_FREE((*de)->name) || ((*de)->attr & ATTR_VOLUME))
320 return PARSE_INVALID;
321 if (fat_checksum((*de)->name) != alias_checksum)
322 *nr_slots = 0;
324 return 0;
328 * Return values: negative -> error, 0 -> not found, positive -> found,
329 * value is the total amount of slots, including the shortname entry.
331 int fat_search_long(struct inode *inode, const unsigned char *name,
332 int name_len, struct fat_slot_info *sinfo)
334 struct super_block *sb = inode->i_sb;
335 struct msdos_sb_info *sbi = MSDOS_SB(sb);
336 struct buffer_head *bh = NULL;
337 struct msdos_dir_entry *de;
338 struct nls_table *nls_disk = sbi->nls_disk;
339 unsigned char nr_slots;
340 wchar_t bufuname[14];
341 wchar_t *unicode = NULL;
342 unsigned char work[MSDOS_NAME];
343 unsigned char *bufname = NULL;
344 unsigned short opt_shortname = sbi->options.shortname;
345 loff_t cpos = 0;
346 int chl, i, j, last_u, err, len;
348 bufname = __getname();
349 if (!bufname)
350 return -ENOMEM;
352 err = -ENOENT;
353 while (1) {
354 if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
355 goto end_of_dir;
356 parse_record:
357 nr_slots = 0;
358 if (de->name[0] == DELETED_FLAG)
359 continue;
360 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
361 continue;
362 if (de->attr != ATTR_EXT && IS_FREE(de->name))
363 continue;
364 if (de->attr == ATTR_EXT) {
365 int status = fat_parse_long(inode, &cpos, &bh, &de,
366 &unicode, &nr_slots);
367 if (status < 0)
368 return status;
369 else if (status == PARSE_INVALID)
370 continue;
371 else if (status == PARSE_NOT_LONGNAME)
372 goto parse_record;
373 else if (status == PARSE_EOF)
374 goto end_of_dir;
377 memcpy(work, de->name, sizeof(de->name));
378 /* see namei.c, msdos_format_name */
379 if (work[0] == 0x05)
380 work[0] = 0xE5;
381 for (i = 0, j = 0, last_u = 0; i < 8;) {
382 if (!work[i])
383 break;
384 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
385 &bufuname[j++], opt_shortname,
386 de->lcase & CASE_LOWER_BASE);
387 if (chl <= 1) {
388 if (work[i] != ' ')
389 last_u = j;
390 } else {
391 last_u = j;
393 i += chl;
395 j = last_u;
396 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
397 for (i = 8; i < MSDOS_NAME;) {
398 if (!work[i])
399 break;
400 chl = fat_shortname2uni(nls_disk, &work[i],
401 MSDOS_NAME - i,
402 &bufuname[j++], opt_shortname,
403 de->lcase & CASE_LOWER_EXT);
404 if (chl <= 1) {
405 if (work[i] != ' ')
406 last_u = j;
407 } else {
408 last_u = j;
410 i += chl;
412 if (!last_u)
413 continue;
415 /* Compare shortname */
416 bufuname[last_u] = 0x0000;
417 len = fat_uni_to_x8(sbi, bufuname, bufname, PATH_MAX);
418 if (fat_name_match(sbi, name, name_len, bufname, len))
419 goto found;
421 if (nr_slots) {
422 /* Compare longname */
423 len = fat_uni_to_x8(sbi, unicode, bufname, PATH_MAX);
424 if (fat_name_match(sbi, name, name_len, bufname, len))
425 goto found;
429 found:
430 nr_slots++; /* include the de */
431 sinfo->slot_off = cpos - nr_slots * sizeof(*de);
432 sinfo->nr_slots = nr_slots;
433 sinfo->de = de;
434 sinfo->bh = bh;
435 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
436 err = 0;
437 end_of_dir:
438 if (bufname)
439 __putname(bufname);
440 if (unicode)
441 __putname(unicode);
443 return err;
446 EXPORT_SYMBOL_GPL(fat_search_long);
448 struct fat_ioctl_filldir_callback {
449 void __user *dirent;
450 int result;
451 /* for dir ioctl */
452 const char *longname;
453 int long_len;
454 const char *shortname;
455 int short_len;
458 static int __fat_readdir(struct inode *inode, struct file *filp, void *dirent,
459 filldir_t filldir, int short_only, int both)
461 struct super_block *sb = inode->i_sb;
462 struct msdos_sb_info *sbi = MSDOS_SB(sb);
463 struct buffer_head *bh;
464 struct msdos_dir_entry *de;
465 struct nls_table *nls_disk = sbi->nls_disk;
466 unsigned char nr_slots;
467 wchar_t bufuname[14];
468 wchar_t *unicode = NULL;
469 unsigned char c, work[MSDOS_NAME], bufname[56], *ptname = bufname;
470 unsigned short opt_shortname = sbi->options.shortname;
471 int isvfat = sbi->options.isvfat;
472 int nocase = sbi->options.nocase;
473 const char *fill_name;
474 unsigned long inum;
475 unsigned long lpos, dummy, *furrfu = &lpos;
476 loff_t cpos;
477 int chi, chl, i, i2, j, last, last_u, dotoffset = 0, fill_len;
478 int ret = 0;
480 lock_super(sb);
482 cpos = filp->f_pos;
483 /* Fake . and .. for the root directory. */
484 if (inode->i_ino == MSDOS_ROOT_INO) {
485 while (cpos < 2) {
486 if (filldir(dirent, "..", cpos+1, cpos, MSDOS_ROOT_INO, DT_DIR) < 0)
487 goto out;
488 cpos++;
489 filp->f_pos++;
491 if (cpos == 2) {
492 dummy = 2;
493 furrfu = &dummy;
494 cpos = 0;
497 if (cpos & (sizeof(struct msdos_dir_entry) - 1)) {
498 ret = -ENOENT;
499 goto out;
502 bh = NULL;
503 get_new:
504 if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
505 goto end_of_dir;
506 parse_record:
507 nr_slots = 0;
508 /* Check for long filename entry */
509 if (isvfat) {
510 if (de->name[0] == DELETED_FLAG)
511 goto record_end;
512 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
513 goto record_end;
514 if (de->attr != ATTR_EXT && IS_FREE(de->name))
515 goto record_end;
516 } else {
517 if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
518 goto record_end;
521 if (isvfat && de->attr == ATTR_EXT) {
522 int status = fat_parse_long(inode, &cpos, &bh, &de,
523 &unicode, &nr_slots);
524 if (status < 0) {
525 filp->f_pos = cpos;
526 ret = status;
527 goto out;
528 } else if (status == PARSE_INVALID)
529 goto record_end;
530 else if (status == PARSE_NOT_LONGNAME)
531 goto parse_record;
532 else if (status == PARSE_EOF)
533 goto end_of_dir;
536 if (sbi->options.dotsOK) {
537 ptname = bufname;
538 dotoffset = 0;
539 if (de->attr & ATTR_HIDDEN) {
540 *ptname++ = '.';
541 dotoffset = 1;
545 memcpy(work, de->name, sizeof(de->name));
546 /* see namei.c, msdos_format_name */
547 if (work[0] == 0x05)
548 work[0] = 0xE5;
549 for (i = 0, j = 0, last = 0, last_u = 0; i < 8;) {
550 if (!(c = work[i]))
551 break;
552 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
553 &bufuname[j++], opt_shortname,
554 de->lcase & CASE_LOWER_BASE);
555 if (chl <= 1) {
556 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
557 if (c != ' ') {
558 last = i;
559 last_u = j;
561 } else {
562 last_u = j;
563 for (chi = 0; chi < chl && i < 8; chi++) {
564 ptname[i] = work[i];
565 i++; last = i;
569 i = last;
570 j = last_u;
571 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
572 ptname[i++] = '.';
573 for (i2 = 8; i2 < MSDOS_NAME;) {
574 if (!(c = work[i2]))
575 break;
576 chl = fat_shortname2uni(nls_disk, &work[i2], MSDOS_NAME - i2,
577 &bufuname[j++], opt_shortname,
578 de->lcase & CASE_LOWER_EXT);
579 if (chl <= 1) {
580 i2++;
581 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
582 if (c != ' ') {
583 last = i;
584 last_u = j;
586 } else {
587 last_u = j;
588 for (chi = 0; chi < chl && i2 < MSDOS_NAME; chi++) {
589 ptname[i++] = work[i2++];
590 last = i;
594 if (!last)
595 goto record_end;
597 i = last + dotoffset;
598 j = last_u;
600 lpos = cpos - (nr_slots + 1) * sizeof(struct msdos_dir_entry);
601 if (!memcmp(de->name, MSDOS_DOT, MSDOS_NAME))
602 inum = inode->i_ino;
603 else if (!memcmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
604 inum = parent_ino(filp->f_path.dentry);
605 } else {
606 loff_t i_pos = fat_make_i_pos(sb, bh, de);
607 struct inode *tmp = fat_iget(sb, i_pos);
608 if (tmp) {
609 inum = tmp->i_ino;
610 iput(tmp);
611 } else
612 inum = iunique(sb, MSDOS_ROOT_INO);
615 if (isvfat) {
616 bufuname[j] = 0x0000;
617 i = fat_uni_to_x8(sbi, bufuname, bufname, sizeof(bufname));
620 fill_name = bufname;
621 fill_len = i;
622 if (!short_only && nr_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 = PATH_MAX - (261 * sizeof(unicode[0]));
627 int long_len = fat_uni_to_x8(sbi, unicode, longname, buf_size);
629 if (!both) {
630 fill_name = longname;
631 fill_len = long_len;
632 } else {
633 /* hack for fat_ioctl_filldir() */
634 struct fat_ioctl_filldir_callback *p = dirent;
636 p->longname = longname;
637 p->long_len = long_len;
638 p->shortname = bufname;
639 p->short_len = i;
640 fill_name = NULL;
641 fill_len = 0;
644 if (filldir(dirent, fill_name, fill_len, *furrfu, inum,
645 (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
646 goto fill_failed;
648 record_end:
649 furrfu = &lpos;
650 filp->f_pos = cpos;
651 goto get_new;
652 end_of_dir:
653 filp->f_pos = cpos;
654 fill_failed:
655 brelse(bh);
656 if (unicode)
657 __putname(unicode);
658 out:
659 unlock_super(sb);
660 return ret;
663 static int fat_readdir(struct file *filp, void *dirent, filldir_t filldir)
665 struct inode *inode = filp->f_path.dentry->d_inode;
666 return __fat_readdir(inode, filp, dirent, filldir, 0, 0);
669 #define FAT_IOCTL_FILLDIR_FUNC(func, dirent_type) \
670 static int func(void *__buf, const char *name, int name_len, \
671 loff_t offset, u64 ino, unsigned int d_type) \
673 struct fat_ioctl_filldir_callback *buf = __buf; \
674 struct dirent_type __user *d1 = buf->dirent; \
675 struct dirent_type __user *d2 = d1 + 1; \
677 if (buf->result) \
678 return -EINVAL; \
679 buf->result++; \
681 if (name != NULL) { \
682 /* dirent has only short name */ \
683 if (name_len >= sizeof(d1->d_name)) \
684 name_len = sizeof(d1->d_name) - 1; \
686 if (put_user(0, d2->d_name) || \
687 put_user(0, &d2->d_reclen) || \
688 copy_to_user(d1->d_name, name, name_len) || \
689 put_user(0, d1->d_name + name_len) || \
690 put_user(name_len, &d1->d_reclen)) \
691 goto efault; \
692 } else { \
693 /* dirent has short and long name */ \
694 const char *longname = buf->longname; \
695 int long_len = buf->long_len; \
696 const char *shortname = buf->shortname; \
697 int short_len = buf->short_len; \
699 if (long_len >= sizeof(d1->d_name)) \
700 long_len = sizeof(d1->d_name) - 1; \
701 if (short_len >= sizeof(d1->d_name)) \
702 short_len = sizeof(d1->d_name) - 1; \
704 if (copy_to_user(d2->d_name, longname, long_len) || \
705 put_user(0, d2->d_name + long_len) || \
706 put_user(long_len, &d2->d_reclen) || \
707 put_user(ino, &d2->d_ino) || \
708 put_user(offset, &d2->d_off) || \
709 copy_to_user(d1->d_name, shortname, short_len) || \
710 put_user(0, d1->d_name + short_len) || \
711 put_user(short_len, &d1->d_reclen)) \
712 goto efault; \
714 return 0; \
715 efault: \
716 buf->result = -EFAULT; \
717 return -EFAULT; \
720 FAT_IOCTL_FILLDIR_FUNC(fat_ioctl_filldir, __fat_dirent)
722 static int fat_ioctl_readdir(struct inode *inode, struct file *filp,
723 void __user *dirent, filldir_t filldir,
724 int short_only, int both)
726 struct fat_ioctl_filldir_callback buf;
727 int ret;
729 buf.dirent = dirent;
730 buf.result = 0;
731 mutex_lock(&inode->i_mutex);
732 ret = -ENOENT;
733 if (!IS_DEADDIR(inode)) {
734 ret = __fat_readdir(inode, filp, &buf, filldir,
735 short_only, both);
737 mutex_unlock(&inode->i_mutex);
738 if (ret >= 0)
739 ret = buf.result;
740 return ret;
743 static int fat_dir_ioctl(struct inode *inode, struct file *filp,
744 unsigned int cmd, unsigned long arg)
746 struct __fat_dirent __user *d1 = (struct __fat_dirent __user *)arg;
747 int short_only, both;
749 switch (cmd) {
750 case VFAT_IOCTL_READDIR_SHORT:
751 short_only = 1;
752 both = 0;
753 break;
754 case VFAT_IOCTL_READDIR_BOTH:
755 short_only = 0;
756 both = 1;
757 break;
758 default:
759 return fat_generic_ioctl(inode, filp, cmd, arg);
762 if (!access_ok(VERIFY_WRITE, d1, sizeof(struct __fat_dirent[2])))
763 return -EFAULT;
765 * Yes, we don't need this put_user() absolutely. However old
766 * code didn't return the right value. So, app use this value,
767 * in order to check whether it is EOF.
769 if (put_user(0, &d1->d_reclen))
770 return -EFAULT;
772 return fat_ioctl_readdir(inode, filp, d1, fat_ioctl_filldir,
773 short_only, both);
776 #ifdef CONFIG_COMPAT
777 #define VFAT_IOCTL_READDIR_BOTH32 _IOR('r', 1, struct compat_dirent[2])
778 #define VFAT_IOCTL_READDIR_SHORT32 _IOR('r', 2, struct compat_dirent[2])
780 FAT_IOCTL_FILLDIR_FUNC(fat_compat_ioctl_filldir, compat_dirent)
782 static long fat_compat_dir_ioctl(struct file *filp, unsigned cmd,
783 unsigned long arg)
785 struct inode *inode = filp->f_path.dentry->d_inode;
786 struct compat_dirent __user *d1 = compat_ptr(arg);
787 int short_only, both;
789 switch (cmd) {
790 case VFAT_IOCTL_READDIR_SHORT32:
791 short_only = 1;
792 both = 0;
793 break;
794 case VFAT_IOCTL_READDIR_BOTH32:
795 short_only = 0;
796 both = 1;
797 break;
798 default:
799 return -ENOIOCTLCMD;
802 if (!access_ok(VERIFY_WRITE, d1, sizeof(struct compat_dirent[2])))
803 return -EFAULT;
805 * Yes, we don't need this put_user() absolutely. However old
806 * code didn't return the right value. So, app use this value,
807 * in order to check whether it is EOF.
809 if (put_user(0, &d1->d_reclen))
810 return -EFAULT;
812 return fat_ioctl_readdir(inode, filp, d1, fat_compat_ioctl_filldir,
813 short_only, both);
815 #endif /* CONFIG_COMPAT */
817 const struct file_operations fat_dir_operations = {
818 .read = generic_read_dir,
819 .readdir = fat_readdir,
820 .ioctl = fat_dir_ioctl,
821 #ifdef CONFIG_COMPAT
822 .compat_ioctl = fat_compat_dir_ioctl,
823 #endif
824 .fsync = file_fsync,
827 static int fat_get_short_entry(struct inode *dir, loff_t *pos,
828 struct buffer_head **bh,
829 struct msdos_dir_entry **de)
831 while (fat_get_entry(dir, pos, bh, de) >= 0) {
832 /* free entry or long name entry or volume label */
833 if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME))
834 return 0;
836 return -ENOENT;
840 * The ".." entry can not provide the "struct fat_slot_info" informations
841 * for inode. So, this function provide the some informations only.
843 int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh,
844 struct msdos_dir_entry **de, loff_t *i_pos)
846 loff_t offset;
848 offset = 0;
849 *bh = NULL;
850 while (fat_get_short_entry(dir, &offset, bh, de) >= 0) {
851 if (!strncmp((*de)->name, MSDOS_DOTDOT, MSDOS_NAME)) {
852 *i_pos = fat_make_i_pos(dir->i_sb, *bh, *de);
853 return 0;
856 return -ENOENT;
859 EXPORT_SYMBOL_GPL(fat_get_dotdot_entry);
861 /* See if directory is empty */
862 int fat_dir_empty(struct inode *dir)
864 struct buffer_head *bh;
865 struct msdos_dir_entry *de;
866 loff_t cpos;
867 int result = 0;
869 bh = NULL;
870 cpos = 0;
871 while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
872 if (strncmp(de->name, MSDOS_DOT , MSDOS_NAME) &&
873 strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
874 result = -ENOTEMPTY;
875 break;
878 brelse(bh);
879 return result;
882 EXPORT_SYMBOL_GPL(fat_dir_empty);
885 * fat_subdirs counts the number of sub-directories of dir. It can be run
886 * on directories being created.
888 int fat_subdirs(struct inode *dir)
890 struct buffer_head *bh;
891 struct msdos_dir_entry *de;
892 loff_t cpos;
893 int count = 0;
895 bh = NULL;
896 cpos = 0;
897 while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
898 if (de->attr & ATTR_DIR)
899 count++;
901 brelse(bh);
902 return count;
906 * Scans a directory for a given file (name points to its formatted name).
907 * Returns an error code or zero.
909 int fat_scan(struct inode *dir, const unsigned char *name,
910 struct fat_slot_info *sinfo)
912 struct super_block *sb = dir->i_sb;
914 sinfo->slot_off = 0;
915 sinfo->bh = NULL;
916 while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh,
917 &sinfo->de) >= 0) {
918 if (!strncmp(sinfo->de->name, name, MSDOS_NAME)) {
919 sinfo->slot_off -= sizeof(*sinfo->de);
920 sinfo->nr_slots = 1;
921 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
922 return 0;
925 return -ENOENT;
928 EXPORT_SYMBOL_GPL(fat_scan);
930 static int __fat_remove_entries(struct inode *dir, loff_t pos, int nr_slots)
932 struct super_block *sb = dir->i_sb;
933 struct buffer_head *bh;
934 struct msdos_dir_entry *de, *endp;
935 int err = 0, orig_slots;
937 while (nr_slots) {
938 bh = NULL;
939 if (fat_get_entry(dir, &pos, &bh, &de) < 0) {
940 err = -EIO;
941 break;
944 orig_slots = nr_slots;
945 endp = (struct msdos_dir_entry *)(bh->b_data + sb->s_blocksize);
946 while (nr_slots && de < endp) {
947 de->name[0] = DELETED_FLAG;
948 de++;
949 nr_slots--;
951 mark_buffer_dirty(bh);
952 if (IS_DIRSYNC(dir))
953 err = sync_dirty_buffer(bh);
954 brelse(bh);
955 if (err)
956 break;
958 /* pos is *next* de's position, so this does `- sizeof(de)' */
959 pos += ((orig_slots - nr_slots) * sizeof(*de)) - sizeof(*de);
962 return err;
965 int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo)
967 struct msdos_dir_entry *de;
968 struct buffer_head *bh;
969 int err = 0, nr_slots;
972 * First stage: Remove the shortname. By this, the directory
973 * entry is removed.
975 nr_slots = sinfo->nr_slots;
976 de = sinfo->de;
977 sinfo->de = NULL;
978 bh = sinfo->bh;
979 sinfo->bh = NULL;
980 while (nr_slots && de >= (struct msdos_dir_entry *)bh->b_data) {
981 de->name[0] = DELETED_FLAG;
982 de--;
983 nr_slots--;
985 mark_buffer_dirty(bh);
986 if (IS_DIRSYNC(dir))
987 err = sync_dirty_buffer(bh);
988 brelse(bh);
989 if (err)
990 return err;
991 dir->i_version++;
993 if (nr_slots) {
995 * Second stage: remove the remaining longname slots.
996 * (This directory entry is already removed, and so return
997 * the success)
999 err = __fat_remove_entries(dir, sinfo->slot_off, nr_slots);
1000 if (err) {
1001 printk(KERN_WARNING
1002 "FAT: Couldn't remove the long name slots\n");
1006 dir->i_mtime = dir->i_atime = CURRENT_TIME_SEC;
1007 if (IS_DIRSYNC(dir))
1008 (void)fat_sync_inode(dir);
1009 else
1010 mark_inode_dirty(dir);
1012 return 0;
1015 EXPORT_SYMBOL_GPL(fat_remove_entries);
1017 static int fat_zeroed_cluster(struct inode *dir, sector_t blknr, int nr_used,
1018 struct buffer_head **bhs, int nr_bhs)
1020 struct super_block *sb = dir->i_sb;
1021 sector_t last_blknr = blknr + MSDOS_SB(sb)->sec_per_clus;
1022 int err, i, n;
1024 /* Zeroing the unused blocks on this cluster */
1025 blknr += nr_used;
1026 n = nr_used;
1027 while (blknr < last_blknr) {
1028 bhs[n] = sb_getblk(sb, blknr);
1029 if (!bhs[n]) {
1030 err = -ENOMEM;
1031 goto error;
1033 memset(bhs[n]->b_data, 0, sb->s_blocksize);
1034 set_buffer_uptodate(bhs[n]);
1035 mark_buffer_dirty(bhs[n]);
1037 n++;
1038 blknr++;
1039 if (n == nr_bhs) {
1040 if (IS_DIRSYNC(dir)) {
1041 err = fat_sync_bhs(bhs, n);
1042 if (err)
1043 goto error;
1045 for (i = 0; i < n; i++)
1046 brelse(bhs[i]);
1047 n = 0;
1050 if (IS_DIRSYNC(dir)) {
1051 err = fat_sync_bhs(bhs, n);
1052 if (err)
1053 goto error;
1055 for (i = 0; i < n; i++)
1056 brelse(bhs[i]);
1058 return 0;
1060 error:
1061 for (i = 0; i < n; i++)
1062 bforget(bhs[i]);
1063 return err;
1066 int fat_alloc_new_dir(struct inode *dir, struct timespec *ts)
1068 struct super_block *sb = dir->i_sb;
1069 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1070 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
1071 struct msdos_dir_entry *de;
1072 sector_t blknr;
1073 __le16 date, time;
1074 int err, cluster;
1076 err = fat_alloc_clusters(dir, &cluster, 1);
1077 if (err)
1078 goto error;
1080 blknr = fat_clus_to_blknr(sbi, cluster);
1081 bhs[0] = sb_getblk(sb, blknr);
1082 if (!bhs[0]) {
1083 err = -ENOMEM;
1084 goto error_free;
1087 fat_date_unix2dos(ts->tv_sec, &time, &date);
1089 de = (struct msdos_dir_entry *)bhs[0]->b_data;
1090 /* filling the new directory slots ("." and ".." entries) */
1091 memcpy(de[0].name, MSDOS_DOT, MSDOS_NAME);
1092 memcpy(de[1].name, MSDOS_DOTDOT, MSDOS_NAME);
1093 de->attr = de[1].attr = ATTR_DIR;
1094 de[0].lcase = de[1].lcase = 0;
1095 de[0].time = de[1].time = time;
1096 de[0].date = de[1].date = date;
1097 de[0].ctime_cs = de[1].ctime_cs = 0;
1098 if (sbi->options.isvfat) {
1099 /* extra timestamps */
1100 de[0].ctime = de[1].ctime = time;
1101 de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = date;
1102 } else {
1103 de[0].ctime = de[1].ctime = 0;
1104 de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = 0;
1106 de[0].start = cpu_to_le16(cluster);
1107 de[0].starthi = cpu_to_le16(cluster >> 16);
1108 de[1].start = cpu_to_le16(MSDOS_I(dir)->i_logstart);
1109 de[1].starthi = cpu_to_le16(MSDOS_I(dir)->i_logstart >> 16);
1110 de[0].size = de[1].size = 0;
1111 memset(de + 2, 0, sb->s_blocksize - 2 * sizeof(*de));
1112 set_buffer_uptodate(bhs[0]);
1113 mark_buffer_dirty(bhs[0]);
1115 err = fat_zeroed_cluster(dir, blknr, 1, bhs, MAX_BUF_PER_PAGE);
1116 if (err)
1117 goto error_free;
1119 return cluster;
1121 error_free:
1122 fat_free_clusters(dir, cluster);
1123 error:
1124 return err;
1127 EXPORT_SYMBOL_GPL(fat_alloc_new_dir);
1129 static int fat_add_new_entries(struct inode *dir, void *slots, int nr_slots,
1130 int *nr_cluster, struct msdos_dir_entry **de,
1131 struct buffer_head **bh, loff_t *i_pos)
1133 struct super_block *sb = dir->i_sb;
1134 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1135 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
1136 sector_t blknr, start_blknr, last_blknr;
1137 unsigned long size, copy;
1138 int err, i, n, offset, cluster[2];
1141 * The minimum cluster size is 512bytes, and maximum entry
1142 * size is 32*slots (672bytes). So, iff the cluster size is
1143 * 512bytes, we may need two clusters.
1145 size = nr_slots * sizeof(struct msdos_dir_entry);
1146 *nr_cluster = (size + (sbi->cluster_size - 1)) >> sbi->cluster_bits;
1147 BUG_ON(*nr_cluster > 2);
1149 err = fat_alloc_clusters(dir, cluster, *nr_cluster);
1150 if (err)
1151 goto error;
1154 * First stage: Fill the directory entry. NOTE: This cluster
1155 * is not referenced from any inode yet, so updates order is
1156 * not important.
1158 i = n = copy = 0;
1159 do {
1160 start_blknr = blknr = fat_clus_to_blknr(sbi, cluster[i]);
1161 last_blknr = start_blknr + sbi->sec_per_clus;
1162 while (blknr < last_blknr) {
1163 bhs[n] = sb_getblk(sb, blknr);
1164 if (!bhs[n]) {
1165 err = -ENOMEM;
1166 goto error_nomem;
1169 /* fill the directory entry */
1170 copy = min(size, sb->s_blocksize);
1171 memcpy(bhs[n]->b_data, slots, copy);
1172 slots += copy;
1173 size -= copy;
1174 set_buffer_uptodate(bhs[n]);
1175 mark_buffer_dirty(bhs[n]);
1176 if (!size)
1177 break;
1178 n++;
1179 blknr++;
1181 } while (++i < *nr_cluster);
1183 memset(bhs[n]->b_data + copy, 0, sb->s_blocksize - copy);
1184 offset = copy - sizeof(struct msdos_dir_entry);
1185 get_bh(bhs[n]);
1186 *bh = bhs[n];
1187 *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
1188 *i_pos = fat_make_i_pos(sb, *bh, *de);
1190 /* Second stage: clear the rest of cluster, and write outs */
1191 err = fat_zeroed_cluster(dir, start_blknr, ++n, bhs, MAX_BUF_PER_PAGE);
1192 if (err)
1193 goto error_free;
1195 return cluster[0];
1197 error_free:
1198 brelse(*bh);
1199 *bh = NULL;
1200 n = 0;
1201 error_nomem:
1202 for (i = 0; i < n; i++)
1203 bforget(bhs[i]);
1204 fat_free_clusters(dir, cluster[0]);
1205 error:
1206 return err;
1209 int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
1210 struct fat_slot_info *sinfo)
1212 struct super_block *sb = dir->i_sb;
1213 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1214 struct buffer_head *bh, *prev, *bhs[3]; /* 32*slots (672bytes) */
1215 struct msdos_dir_entry *de;
1216 int err, free_slots, i, nr_bhs;
1217 loff_t pos, i_pos;
1219 sinfo->nr_slots = nr_slots;
1221 /* First stage: search free direcotry entries */
1222 free_slots = nr_bhs = 0;
1223 bh = prev = NULL;
1224 pos = 0;
1225 err = -ENOSPC;
1226 while (fat_get_entry(dir, &pos, &bh, &de) > -1) {
1227 /* check the maximum size of directory */
1228 if (pos >= FAT_MAX_DIR_SIZE)
1229 goto error;
1231 if (IS_FREE(de->name)) {
1232 if (prev != bh) {
1233 get_bh(bh);
1234 bhs[nr_bhs] = prev = bh;
1235 nr_bhs++;
1237 free_slots++;
1238 if (free_slots == nr_slots)
1239 goto found;
1240 } else {
1241 for (i = 0; i < nr_bhs; i++)
1242 brelse(bhs[i]);
1243 prev = NULL;
1244 free_slots = nr_bhs = 0;
1247 if (dir->i_ino == MSDOS_ROOT_INO) {
1248 if (sbi->fat_bits != 32)
1249 goto error;
1250 } else if (MSDOS_I(dir)->i_start == 0) {
1251 printk(KERN_ERR "FAT: Corrupted directory (i_pos %lld)\n",
1252 MSDOS_I(dir)->i_pos);
1253 err = -EIO;
1254 goto error;
1257 found:
1258 err = 0;
1259 pos -= free_slots * sizeof(*de);
1260 nr_slots -= free_slots;
1261 if (free_slots) {
1263 * Second stage: filling the free entries with new entries.
1264 * NOTE: If this slots has shortname, first, we write
1265 * the long name slots, then write the short name.
1267 int size = free_slots * sizeof(*de);
1268 int offset = pos & (sb->s_blocksize - 1);
1269 int long_bhs = nr_bhs - (nr_slots == 0);
1271 /* Fill the long name slots. */
1272 for (i = 0; i < long_bhs; i++) {
1273 int copy = min_t(int, sb->s_blocksize - offset, size);
1274 memcpy(bhs[i]->b_data + offset, slots, copy);
1275 mark_buffer_dirty(bhs[i]);
1276 offset = 0;
1277 slots += copy;
1278 size -= copy;
1280 if (long_bhs && IS_DIRSYNC(dir))
1281 err = fat_sync_bhs(bhs, long_bhs);
1282 if (!err && i < nr_bhs) {
1283 /* Fill the short name slot. */
1284 int copy = min_t(int, sb->s_blocksize - offset, size);
1285 memcpy(bhs[i]->b_data + offset, slots, copy);
1286 mark_buffer_dirty(bhs[i]);
1287 if (IS_DIRSYNC(dir))
1288 err = sync_dirty_buffer(bhs[i]);
1290 for (i = 0; i < nr_bhs; i++)
1291 brelse(bhs[i]);
1292 if (err)
1293 goto error_remove;
1296 if (nr_slots) {
1297 int cluster, nr_cluster;
1300 * Third stage: allocate the cluster for new entries.
1301 * And initialize the cluster with new entries, then
1302 * add the cluster to dir.
1304 cluster = fat_add_new_entries(dir, slots, nr_slots, &nr_cluster,
1305 &de, &bh, &i_pos);
1306 if (cluster < 0) {
1307 err = cluster;
1308 goto error_remove;
1310 err = fat_chain_add(dir, cluster, nr_cluster);
1311 if (err) {
1312 fat_free_clusters(dir, cluster);
1313 goto error_remove;
1315 if (dir->i_size & (sbi->cluster_size - 1)) {
1316 fat_fs_panic(sb, "Odd directory size");
1317 dir->i_size = (dir->i_size + sbi->cluster_size - 1)
1318 & ~((loff_t)sbi->cluster_size - 1);
1320 dir->i_size += nr_cluster << sbi->cluster_bits;
1321 MSDOS_I(dir)->mmu_private += nr_cluster << sbi->cluster_bits;
1323 sinfo->slot_off = pos;
1324 sinfo->de = de;
1325 sinfo->bh = bh;
1326 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
1328 return 0;
1330 error:
1331 brelse(bh);
1332 for (i = 0; i < nr_bhs; i++)
1333 brelse(bhs[i]);
1334 return err;
1336 error_remove:
1337 brelse(bh);
1338 if (free_slots)
1339 __fat_remove_entries(dir, pos, free_slots);
1340 return err;
1343 EXPORT_SYMBOL_GPL(fat_add_entries);