mac80211_hwsim: fix-up build damage from removal of skb->dst
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / fat / dir.c
blob38ff75a0fe22d3b8764e9be001dd57857c5abe66
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/smp_lock.h>
20 #include <linux/buffer_head.h>
21 #include <linux/compat.h>
22 #include <asm/uaccess.h>
23 #include "fat.h"
26 * Maximum buffer size of short name.
27 * [(MSDOS_NAME + '.') * max one char + nul]
28 * For msdos style, ['.' (hidden) + MSDOS_NAME + '.' + nul]
30 #define FAT_MAX_SHORT_SIZE ((MSDOS_NAME + 1) * NLS_MAX_CHARSET_SIZE + 1)
32 * Maximum buffer size of unicode chars from slots.
33 * [(max longname slots * 13 (size in a slot) + nul) * sizeof(wchar_t)]
35 #define FAT_MAX_UNI_CHARS ((MSDOS_SLOTS - 1) * 13 + 1)
36 #define FAT_MAX_UNI_SIZE (FAT_MAX_UNI_CHARS * sizeof(wchar_t))
38 static inline loff_t fat_make_i_pos(struct super_block *sb,
39 struct buffer_head *bh,
40 struct msdos_dir_entry *de)
42 return ((loff_t)bh->b_blocknr << MSDOS_SB(sb)->dir_per_block_bits)
43 | (de - (struct msdos_dir_entry *)bh->b_data);
46 static inline void fat_dir_readahead(struct inode *dir, sector_t iblock,
47 sector_t phys)
49 struct super_block *sb = dir->i_sb;
50 struct msdos_sb_info *sbi = MSDOS_SB(sb);
51 struct buffer_head *bh;
52 int sec;
54 /* This is not a first sector of cluster, or sec_per_clus == 1 */
55 if ((iblock & (sbi->sec_per_clus - 1)) || sbi->sec_per_clus == 1)
56 return;
57 /* root dir of FAT12/FAT16 */
58 if ((sbi->fat_bits != 32) && (dir->i_ino == MSDOS_ROOT_INO))
59 return;
61 bh = sb_find_get_block(sb, phys);
62 if (bh == NULL || !buffer_uptodate(bh)) {
63 for (sec = 0; sec < sbi->sec_per_clus; sec++)
64 sb_breadahead(sb, phys + sec);
66 brelse(bh);
69 /* Returns the inode number of the directory entry at offset pos. If bh is
70 non-NULL, it is brelse'd before. Pos is incremented. The buffer header is
71 returned in bh.
72 AV. Most often we do it item-by-item. Makes sense to optimize.
73 AV. OK, there we go: if both bh and de are non-NULL we assume that we just
74 AV. want the next entry (took one explicit de=NULL in vfat/namei.c).
75 AV. It's done in fat_get_entry() (inlined), here the slow case lives.
76 AV. Additionally, when we return -1 (i.e. reached the end of directory)
77 AV. we make bh NULL.
79 static int fat__get_entry(struct inode *dir, loff_t *pos,
80 struct buffer_head **bh, struct msdos_dir_entry **de)
82 struct super_block *sb = dir->i_sb;
83 sector_t phys, iblock;
84 unsigned long mapped_blocks;
85 int err, offset;
87 next:
88 if (*bh)
89 brelse(*bh);
91 *bh = NULL;
92 iblock = *pos >> sb->s_blocksize_bits;
93 err = fat_bmap(dir, iblock, &phys, &mapped_blocks, 0);
94 if (err || !phys)
95 return -1; /* beyond EOF or error */
97 fat_dir_readahead(dir, iblock, phys);
99 *bh = sb_bread(sb, phys);
100 if (*bh == NULL) {
101 printk(KERN_ERR "FAT: Directory bread(block %llu) failed\n",
102 (llu)phys);
103 /* skip this block */
104 *pos = (iblock + 1) << sb->s_blocksize_bits;
105 goto next;
108 offset = *pos & (sb->s_blocksize - 1);
109 *pos += sizeof(struct msdos_dir_entry);
110 *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
112 return 0;
115 static inline int fat_get_entry(struct inode *dir, loff_t *pos,
116 struct buffer_head **bh,
117 struct msdos_dir_entry **de)
119 /* Fast stuff first */
120 if (*bh && *de &&
121 (*de - (struct msdos_dir_entry *)(*bh)->b_data) < MSDOS_SB(dir->i_sb)->dir_per_block - 1) {
122 *pos += sizeof(struct msdos_dir_entry);
123 (*de)++;
124 return 0;
126 return fat__get_entry(dir, pos, bh, de);
130 * Convert Unicode 16 to UTF-8, translated Unicode, or ASCII.
131 * If uni_xlate is enabled and we can't get a 1:1 conversion, use a
132 * colon as an escape character since it is normally invalid on the vfat
133 * filesystem. The following four characters are the hexadecimal digits
134 * of Unicode value. This lets us do a full dump and restore of Unicode
135 * filenames. We could get into some trouble with long Unicode names,
136 * but ignore that right now.
137 * Ahem... Stack smashing in ring 0 isn't fun. Fixed.
139 static int uni16_to_x8(unsigned char *ascii, const wchar_t *uni, int len,
140 int uni_xlate, struct nls_table *nls)
142 const wchar_t *ip;
143 wchar_t ec;
144 unsigned char *op, nc;
145 int charlen;
146 int k;
148 ip = uni;
149 op = ascii;
151 while (*ip && ((len - NLS_MAX_CHARSET_SIZE) > 0)) {
152 ec = *ip++;
153 if ( (charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE)) > 0) {
154 op += charlen;
155 len -= charlen;
156 } else {
157 if (uni_xlate == 1) {
158 *op = ':';
159 for (k = 4; k > 0; k--) {
160 nc = ec & 0xF;
161 op[k] = nc > 9 ? nc + ('a' - 10)
162 : nc + '0';
163 ec >>= 4;
165 op += 5;
166 len -= 5;
167 } else {
168 *op++ = '?';
169 len--;
174 if (unlikely(*ip)) {
175 printk(KERN_WARNING "FAT: filename was truncated while "
176 "converting.");
179 *op = 0;
180 return (op - ascii);
183 static inline int fat_uni_to_x8(struct msdos_sb_info *sbi, const wchar_t *uni,
184 unsigned char *buf, int size)
186 if (sbi->options.utf8)
187 return utf16s_to_utf8s(uni, FAT_MAX_UNI_CHARS,
188 UTF16_HOST_ENDIAN, buf, size);
189 else
190 return uni16_to_x8(buf, uni, size, sbi->options.unicode_xlate,
191 sbi->nls_io);
194 static inline int
195 fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
197 int charlen;
199 charlen = t->char2uni(c, clen, uni);
200 if (charlen < 0) {
201 *uni = 0x003f; /* a question mark */
202 charlen = 1;
204 return charlen;
207 static inline int
208 fat_short2lower_uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
210 int charlen;
211 wchar_t wc;
213 charlen = t->char2uni(c, clen, &wc);
214 if (charlen < 0) {
215 *uni = 0x003f; /* a question mark */
216 charlen = 1;
217 } else if (charlen <= 1) {
218 unsigned char nc = t->charset2lower[*c];
220 if (!nc)
221 nc = *c;
223 if ( (charlen = t->char2uni(&nc, 1, uni)) < 0) {
224 *uni = 0x003f; /* a question mark */
225 charlen = 1;
227 } else
228 *uni = wc;
230 return charlen;
233 static inline int
234 fat_shortname2uni(struct nls_table *nls, unsigned char *buf, int buf_size,
235 wchar_t *uni_buf, unsigned short opt, int lower)
237 int len = 0;
239 if (opt & VFAT_SFN_DISPLAY_LOWER)
240 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
241 else if (opt & VFAT_SFN_DISPLAY_WIN95)
242 len = fat_short2uni(nls, buf, buf_size, uni_buf);
243 else if (opt & VFAT_SFN_DISPLAY_WINNT) {
244 if (lower)
245 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
246 else
247 len = fat_short2uni(nls, buf, buf_size, uni_buf);
248 } else
249 len = fat_short2uni(nls, buf, buf_size, uni_buf);
251 return len;
254 static inline int fat_name_match(struct msdos_sb_info *sbi,
255 const unsigned char *a, int a_len,
256 const unsigned char *b, int b_len)
258 if (a_len != b_len)
259 return 0;
261 if (sbi->options.name_check != 's')
262 return !nls_strnicmp(sbi->nls_io, a, b, a_len);
263 else
264 return !memcmp(a, b, a_len);
267 enum { PARSE_INVALID = 1, PARSE_NOT_LONGNAME, PARSE_EOF, };
270 * fat_parse_long - Parse extended directory entry.
272 * This function returns zero on success, negative value on error, or one of
273 * the following:
275 * %PARSE_INVALID - Directory entry is invalid.
276 * %PARSE_NOT_LONGNAME - Directory entry does not contain longname.
277 * %PARSE_EOF - Directory has no more entries.
279 static int fat_parse_long(struct inode *dir, loff_t *pos,
280 struct buffer_head **bh, struct msdos_dir_entry **de,
281 wchar_t **unicode, unsigned char *nr_slots)
283 struct msdos_dir_slot *ds;
284 unsigned char id, slot, slots, alias_checksum;
286 if (!*unicode) {
287 *unicode = __getname();
288 if (!*unicode) {
289 brelse(*bh);
290 return -ENOMEM;
293 parse_long:
294 slots = 0;
295 ds = (struct msdos_dir_slot *)*de;
296 id = ds->id;
297 if (!(id & 0x40))
298 return PARSE_INVALID;
299 slots = id & ~0x40;
300 if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */
301 return PARSE_INVALID;
302 *nr_slots = slots;
303 alias_checksum = ds->alias_checksum;
305 slot = slots;
306 while (1) {
307 int offset;
309 slot--;
310 offset = slot * 13;
311 fat16_towchar(*unicode + offset, ds->name0_4, 5);
312 fat16_towchar(*unicode + offset + 5, ds->name5_10, 6);
313 fat16_towchar(*unicode + offset + 11, ds->name11_12, 2);
315 if (ds->id & 0x40)
316 (*unicode)[offset + 13] = 0;
317 if (fat_get_entry(dir, pos, bh, de) < 0)
318 return PARSE_EOF;
319 if (slot == 0)
320 break;
321 ds = (struct msdos_dir_slot *)*de;
322 if (ds->attr != ATTR_EXT)
323 return PARSE_NOT_LONGNAME;
324 if ((ds->id & ~0x40) != slot)
325 goto parse_long;
326 if (ds->alias_checksum != alias_checksum)
327 goto parse_long;
329 if ((*de)->name[0] == DELETED_FLAG)
330 return PARSE_INVALID;
331 if ((*de)->attr == ATTR_EXT)
332 goto parse_long;
333 if (IS_FREE((*de)->name) || ((*de)->attr & ATTR_VOLUME))
334 return PARSE_INVALID;
335 if (fat_checksum((*de)->name) != alias_checksum)
336 *nr_slots = 0;
338 return 0;
342 * Return values: negative -> error, 0 -> not found, positive -> found,
343 * value is the total amount of slots, including the shortname entry.
345 int fat_search_long(struct inode *inode, const unsigned char *name,
346 int name_len, struct fat_slot_info *sinfo)
348 struct super_block *sb = inode->i_sb;
349 struct msdos_sb_info *sbi = MSDOS_SB(sb);
350 struct buffer_head *bh = NULL;
351 struct msdos_dir_entry *de;
352 struct nls_table *nls_disk = sbi->nls_disk;
353 unsigned char nr_slots;
354 wchar_t bufuname[14];
355 wchar_t *unicode = NULL;
356 unsigned char work[MSDOS_NAME];
357 unsigned char bufname[FAT_MAX_SHORT_SIZE];
358 unsigned short opt_shortname = sbi->options.shortname;
359 loff_t cpos = 0;
360 int chl, i, j, last_u, err, len;
362 err = -ENOENT;
363 while (1) {
364 if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
365 goto end_of_dir;
366 parse_record:
367 nr_slots = 0;
368 if (de->name[0] == DELETED_FLAG)
369 continue;
370 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
371 continue;
372 if (de->attr != ATTR_EXT && IS_FREE(de->name))
373 continue;
374 if (de->attr == ATTR_EXT) {
375 int status = fat_parse_long(inode, &cpos, &bh, &de,
376 &unicode, &nr_slots);
377 if (status < 0) {
378 err = status;
379 goto end_of_dir;
380 } else if (status == PARSE_INVALID)
381 continue;
382 else if (status == PARSE_NOT_LONGNAME)
383 goto parse_record;
384 else if (status == PARSE_EOF)
385 goto end_of_dir;
388 memcpy(work, de->name, sizeof(de->name));
389 /* see namei.c, msdos_format_name */
390 if (work[0] == 0x05)
391 work[0] = 0xE5;
392 for (i = 0, j = 0, last_u = 0; i < 8;) {
393 if (!work[i])
394 break;
395 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
396 &bufuname[j++], opt_shortname,
397 de->lcase & CASE_LOWER_BASE);
398 if (chl <= 1) {
399 if (work[i] != ' ')
400 last_u = j;
401 } else {
402 last_u = j;
404 i += chl;
406 j = last_u;
407 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
408 for (i = 8; i < MSDOS_NAME;) {
409 if (!work[i])
410 break;
411 chl = fat_shortname2uni(nls_disk, &work[i],
412 MSDOS_NAME - i,
413 &bufuname[j++], opt_shortname,
414 de->lcase & CASE_LOWER_EXT);
415 if (chl <= 1) {
416 if (work[i] != ' ')
417 last_u = j;
418 } else {
419 last_u = j;
421 i += chl;
423 if (!last_u)
424 continue;
426 /* Compare shortname */
427 bufuname[last_u] = 0x0000;
428 len = fat_uni_to_x8(sbi, bufuname, bufname, sizeof(bufname));
429 if (fat_name_match(sbi, name, name_len, bufname, len))
430 goto found;
432 if (nr_slots) {
433 void *longname = unicode + FAT_MAX_UNI_CHARS;
434 int size = PATH_MAX - FAT_MAX_UNI_SIZE;
436 /* Compare longname */
437 len = fat_uni_to_x8(sbi, unicode, longname, size);
438 if (fat_name_match(sbi, name, name_len, longname, len))
439 goto found;
443 found:
444 nr_slots++; /* include the de */
445 sinfo->slot_off = cpos - nr_slots * sizeof(*de);
446 sinfo->nr_slots = nr_slots;
447 sinfo->de = de;
448 sinfo->bh = bh;
449 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
450 err = 0;
451 end_of_dir:
452 if (unicode)
453 __putname(unicode);
455 return err;
458 EXPORT_SYMBOL_GPL(fat_search_long);
460 struct fat_ioctl_filldir_callback {
461 void __user *dirent;
462 int result;
463 /* for dir ioctl */
464 const char *longname;
465 int long_len;
466 const char *shortname;
467 int short_len;
470 static int __fat_readdir(struct inode *inode, struct file *filp, void *dirent,
471 filldir_t filldir, int short_only, int both)
473 struct super_block *sb = inode->i_sb;
474 struct msdos_sb_info *sbi = MSDOS_SB(sb);
475 struct buffer_head *bh;
476 struct msdos_dir_entry *de;
477 struct nls_table *nls_disk = sbi->nls_disk;
478 unsigned char nr_slots;
479 wchar_t bufuname[14];
480 wchar_t *unicode = NULL;
481 unsigned char c, work[MSDOS_NAME];
482 unsigned char bufname[FAT_MAX_SHORT_SIZE], *ptname = bufname;
483 unsigned short opt_shortname = sbi->options.shortname;
484 int isvfat = sbi->options.isvfat;
485 int nocase = sbi->options.nocase;
486 const char *fill_name = NULL;
487 unsigned long inum;
488 unsigned long lpos, dummy, *furrfu = &lpos;
489 loff_t cpos;
490 int chi, chl, i, i2, j, last, last_u, dotoffset = 0, fill_len = 0;
491 int ret = 0;
493 lock_super(sb);
495 cpos = filp->f_pos;
496 /* Fake . and .. for the root directory. */
497 if (inode->i_ino == MSDOS_ROOT_INO) {
498 while (cpos < 2) {
499 if (filldir(dirent, "..", cpos+1, cpos, MSDOS_ROOT_INO, DT_DIR) < 0)
500 goto out;
501 cpos++;
502 filp->f_pos++;
504 if (cpos == 2) {
505 dummy = 2;
506 furrfu = &dummy;
507 cpos = 0;
510 if (cpos & (sizeof(struct msdos_dir_entry) - 1)) {
511 ret = -ENOENT;
512 goto out;
515 bh = NULL;
516 get_new:
517 if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
518 goto end_of_dir;
519 parse_record:
520 nr_slots = 0;
522 * Check for long filename entry, but if short_only, we don't
523 * need to parse long filename.
525 if (isvfat && !short_only) {
526 if (de->name[0] == DELETED_FLAG)
527 goto record_end;
528 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
529 goto record_end;
530 if (de->attr != ATTR_EXT && IS_FREE(de->name))
531 goto record_end;
532 } else {
533 if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
534 goto record_end;
537 if (isvfat && de->attr == ATTR_EXT) {
538 int status = fat_parse_long(inode, &cpos, &bh, &de,
539 &unicode, &nr_slots);
540 if (status < 0) {
541 filp->f_pos = cpos;
542 ret = status;
543 goto out;
544 } else if (status == PARSE_INVALID)
545 goto record_end;
546 else if (status == PARSE_NOT_LONGNAME)
547 goto parse_record;
548 else if (status == PARSE_EOF)
549 goto end_of_dir;
551 if (nr_slots) {
552 void *longname = unicode + FAT_MAX_UNI_CHARS;
553 int size = PATH_MAX - FAT_MAX_UNI_SIZE;
554 int len = fat_uni_to_x8(sbi, unicode, longname, size);
556 fill_name = longname;
557 fill_len = len;
558 /* !both && !short_only, so we don't need shortname. */
559 if (!both)
560 goto start_filldir;
564 if (sbi->options.dotsOK) {
565 ptname = bufname;
566 dotoffset = 0;
567 if (de->attr & ATTR_HIDDEN) {
568 *ptname++ = '.';
569 dotoffset = 1;
573 memcpy(work, de->name, sizeof(de->name));
574 /* see namei.c, msdos_format_name */
575 if (work[0] == 0x05)
576 work[0] = 0xE5;
577 for (i = 0, j = 0, last = 0, last_u = 0; i < 8;) {
578 if (!(c = work[i]))
579 break;
580 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
581 &bufuname[j++], opt_shortname,
582 de->lcase & CASE_LOWER_BASE);
583 if (chl <= 1) {
584 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
585 if (c != ' ') {
586 last = i;
587 last_u = j;
589 } else {
590 last_u = j;
591 for (chi = 0; chi < chl && i < 8; chi++) {
592 ptname[i] = work[i];
593 i++; last = i;
597 i = last;
598 j = last_u;
599 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
600 ptname[i++] = '.';
601 for (i2 = 8; i2 < MSDOS_NAME;) {
602 if (!(c = work[i2]))
603 break;
604 chl = fat_shortname2uni(nls_disk, &work[i2], MSDOS_NAME - i2,
605 &bufuname[j++], opt_shortname,
606 de->lcase & CASE_LOWER_EXT);
607 if (chl <= 1) {
608 i2++;
609 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
610 if (c != ' ') {
611 last = i;
612 last_u = j;
614 } else {
615 last_u = j;
616 for (chi = 0; chi < chl && i2 < MSDOS_NAME; chi++) {
617 ptname[i++] = work[i2++];
618 last = i;
622 if (!last)
623 goto record_end;
625 i = last + dotoffset;
626 j = last_u;
628 if (isvfat) {
629 bufuname[j] = 0x0000;
630 i = fat_uni_to_x8(sbi, bufuname, bufname, sizeof(bufname));
632 if (nr_slots) {
633 /* hack for fat_ioctl_filldir() */
634 struct fat_ioctl_filldir_callback *p = dirent;
636 p->longname = fill_name;
637 p->long_len = fill_len;
638 p->shortname = bufname;
639 p->short_len = i;
640 fill_name = NULL;
641 fill_len = 0;
642 } else {
643 fill_name = bufname;
644 fill_len = i;
647 start_filldir:
648 lpos = cpos - (nr_slots + 1) * sizeof(struct msdos_dir_entry);
649 if (!memcmp(de->name, MSDOS_DOT, MSDOS_NAME))
650 inum = inode->i_ino;
651 else if (!memcmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
652 inum = parent_ino(filp->f_path.dentry);
653 } else {
654 loff_t i_pos = fat_make_i_pos(sb, bh, de);
655 struct inode *tmp = fat_iget(sb, i_pos);
656 if (tmp) {
657 inum = tmp->i_ino;
658 iput(tmp);
659 } else
660 inum = iunique(sb, MSDOS_ROOT_INO);
663 if (filldir(dirent, fill_name, fill_len, *furrfu, inum,
664 (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
665 goto fill_failed;
667 record_end:
668 furrfu = &lpos;
669 filp->f_pos = cpos;
670 goto get_new;
671 end_of_dir:
672 filp->f_pos = cpos;
673 fill_failed:
674 brelse(bh);
675 if (unicode)
676 __putname(unicode);
677 out:
678 unlock_super(sb);
679 return ret;
682 static int fat_readdir(struct file *filp, void *dirent, filldir_t filldir)
684 struct inode *inode = filp->f_path.dentry->d_inode;
685 return __fat_readdir(inode, filp, dirent, filldir, 0, 0);
688 #define FAT_IOCTL_FILLDIR_FUNC(func, dirent_type) \
689 static int func(void *__buf, const char *name, int name_len, \
690 loff_t offset, u64 ino, unsigned int d_type) \
692 struct fat_ioctl_filldir_callback *buf = __buf; \
693 struct dirent_type __user *d1 = buf->dirent; \
694 struct dirent_type __user *d2 = d1 + 1; \
696 if (buf->result) \
697 return -EINVAL; \
698 buf->result++; \
700 if (name != NULL) { \
701 /* dirent has only short name */ \
702 if (name_len >= sizeof(d1->d_name)) \
703 name_len = sizeof(d1->d_name) - 1; \
705 if (put_user(0, d2->d_name) || \
706 put_user(0, &d2->d_reclen) || \
707 copy_to_user(d1->d_name, name, name_len) || \
708 put_user(0, d1->d_name + name_len) || \
709 put_user(name_len, &d1->d_reclen)) \
710 goto efault; \
711 } else { \
712 /* dirent has short and long name */ \
713 const char *longname = buf->longname; \
714 int long_len = buf->long_len; \
715 const char *shortname = buf->shortname; \
716 int short_len = buf->short_len; \
718 if (long_len >= sizeof(d1->d_name)) \
719 long_len = sizeof(d1->d_name) - 1; \
720 if (short_len >= sizeof(d1->d_name)) \
721 short_len = sizeof(d1->d_name) - 1; \
723 if (copy_to_user(d2->d_name, longname, long_len) || \
724 put_user(0, d2->d_name + long_len) || \
725 put_user(long_len, &d2->d_reclen) || \
726 put_user(ino, &d2->d_ino) || \
727 put_user(offset, &d2->d_off) || \
728 copy_to_user(d1->d_name, shortname, short_len) || \
729 put_user(0, d1->d_name + short_len) || \
730 put_user(short_len, &d1->d_reclen)) \
731 goto efault; \
733 return 0; \
734 efault: \
735 buf->result = -EFAULT; \
736 return -EFAULT; \
739 FAT_IOCTL_FILLDIR_FUNC(fat_ioctl_filldir, __fat_dirent)
741 static int fat_ioctl_readdir(struct inode *inode, struct file *filp,
742 void __user *dirent, filldir_t filldir,
743 int short_only, int both)
745 struct fat_ioctl_filldir_callback buf;
746 int ret;
748 buf.dirent = dirent;
749 buf.result = 0;
750 mutex_lock(&inode->i_mutex);
751 ret = -ENOENT;
752 if (!IS_DEADDIR(inode)) {
753 ret = __fat_readdir(inode, filp, &buf, filldir,
754 short_only, both);
756 mutex_unlock(&inode->i_mutex);
757 if (ret >= 0)
758 ret = buf.result;
759 return ret;
762 static int fat_dir_ioctl(struct inode *inode, struct file *filp,
763 unsigned int cmd, unsigned long arg)
765 struct __fat_dirent __user *d1 = (struct __fat_dirent __user *)arg;
766 int short_only, both;
768 switch (cmd) {
769 case VFAT_IOCTL_READDIR_SHORT:
770 short_only = 1;
771 both = 0;
772 break;
773 case VFAT_IOCTL_READDIR_BOTH:
774 short_only = 0;
775 both = 1;
776 break;
777 default:
778 return fat_generic_ioctl(inode, filp, cmd, arg);
781 if (!access_ok(VERIFY_WRITE, d1, sizeof(struct __fat_dirent[2])))
782 return -EFAULT;
784 * Yes, we don't need this put_user() absolutely. However old
785 * code didn't return the right value. So, app use this value,
786 * in order to check whether it is EOF.
788 if (put_user(0, &d1->d_reclen))
789 return -EFAULT;
791 return fat_ioctl_readdir(inode, filp, d1, fat_ioctl_filldir,
792 short_only, both);
795 #ifdef CONFIG_COMPAT
796 #define VFAT_IOCTL_READDIR_BOTH32 _IOR('r', 1, struct compat_dirent[2])
797 #define VFAT_IOCTL_READDIR_SHORT32 _IOR('r', 2, struct compat_dirent[2])
799 FAT_IOCTL_FILLDIR_FUNC(fat_compat_ioctl_filldir, compat_dirent)
801 static long fat_compat_dir_ioctl(struct file *filp, unsigned cmd,
802 unsigned long arg)
804 struct inode *inode = filp->f_path.dentry->d_inode;
805 struct compat_dirent __user *d1 = compat_ptr(arg);
806 int short_only, both;
808 switch (cmd) {
809 case VFAT_IOCTL_READDIR_SHORT32:
810 short_only = 1;
811 both = 0;
812 break;
813 case VFAT_IOCTL_READDIR_BOTH32:
814 short_only = 0;
815 both = 1;
816 break;
817 default:
818 return -ENOIOCTLCMD;
821 if (!access_ok(VERIFY_WRITE, d1, sizeof(struct compat_dirent[2])))
822 return -EFAULT;
824 * Yes, we don't need this put_user() absolutely. However old
825 * code didn't return the right value. So, app use this value,
826 * in order to check whether it is EOF.
828 if (put_user(0, &d1->d_reclen))
829 return -EFAULT;
831 return fat_ioctl_readdir(inode, filp, d1, fat_compat_ioctl_filldir,
832 short_only, both);
834 #endif /* CONFIG_COMPAT */
836 const struct file_operations fat_dir_operations = {
837 .llseek = generic_file_llseek,
838 .read = generic_read_dir,
839 .readdir = fat_readdir,
840 .ioctl = fat_dir_ioctl,
841 #ifdef CONFIG_COMPAT
842 .compat_ioctl = fat_compat_dir_ioctl,
843 #endif
844 .fsync = fat_file_fsync,
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))
854 return 0;
856 return -ENOENT;
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)
866 loff_t offset;
868 offset = 0;
869 *bh = NULL;
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);
873 return 0;
876 return -ENOENT;
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;
886 loff_t cpos;
887 int result = 0;
889 bh = NULL;
890 cpos = 0;
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)) {
894 result = -ENOTEMPTY;
895 break;
898 brelse(bh);
899 return result;
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;
912 loff_t cpos;
913 int count = 0;
915 bh = NULL;
916 cpos = 0;
917 while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
918 if (de->attr & ATTR_DIR)
919 count++;
921 brelse(bh);
922 return count;
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;
934 sinfo->slot_off = 0;
935 sinfo->bh = NULL;
936 while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh,
937 &sinfo->de) >= 0) {
938 if (!strncmp(sinfo->de->name, name, MSDOS_NAME)) {
939 sinfo->slot_off -= sizeof(*sinfo->de);
940 sinfo->nr_slots = 1;
941 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
942 return 0;
945 return -ENOENT;
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;
957 while (nr_slots) {
958 bh = NULL;
959 if (fat_get_entry(dir, &pos, &bh, &de) < 0) {
960 err = -EIO;
961 break;
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;
968 de++;
969 nr_slots--;
971 mark_buffer_dirty_inode(bh, dir);
972 if (IS_DIRSYNC(dir))
973 err = sync_dirty_buffer(bh);
974 brelse(bh);
975 if (err)
976 break;
978 /* pos is *next* de's position, so this does `- sizeof(de)' */
979 pos += ((orig_slots - nr_slots) * sizeof(*de)) - sizeof(*de);
982 return err;
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
993 * entry is removed.
995 nr_slots = sinfo->nr_slots;
996 de = sinfo->de;
997 sinfo->de = NULL;
998 bh = sinfo->bh;
999 sinfo->bh = NULL;
1000 while (nr_slots && de >= (struct msdos_dir_entry *)bh->b_data) {
1001 de->name[0] = DELETED_FLAG;
1002 de--;
1003 nr_slots--;
1005 mark_buffer_dirty_inode(bh, dir);
1006 if (IS_DIRSYNC(dir))
1007 err = sync_dirty_buffer(bh);
1008 brelse(bh);
1009 if (err)
1010 return err;
1011 dir->i_version++;
1013 if (nr_slots) {
1015 * Second stage: remove the remaining longname slots.
1016 * (This directory entry is already removed, and so return
1017 * the success)
1019 err = __fat_remove_entries(dir, sinfo->slot_off, nr_slots);
1020 if (err) {
1021 printk(KERN_WARNING
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);
1029 else
1030 mark_inode_dirty(dir);
1032 return 0;
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;
1042 int err, i, n;
1044 /* Zeroing the unused blocks on this cluster */
1045 blknr += nr_used;
1046 n = nr_used;
1047 while (blknr < last_blknr) {
1048 bhs[n] = sb_getblk(sb, blknr);
1049 if (!bhs[n]) {
1050 err = -ENOMEM;
1051 goto error;
1053 memset(bhs[n]->b_data, 0, sb->s_blocksize);
1054 set_buffer_uptodate(bhs[n]);
1055 mark_buffer_dirty_inode(bhs[n], dir);
1057 n++;
1058 blknr++;
1059 if (n == nr_bhs) {
1060 if (IS_DIRSYNC(dir)) {
1061 err = fat_sync_bhs(bhs, n);
1062 if (err)
1063 goto error;
1065 for (i = 0; i < n; i++)
1066 brelse(bhs[i]);
1067 n = 0;
1070 if (IS_DIRSYNC(dir)) {
1071 err = fat_sync_bhs(bhs, n);
1072 if (err)
1073 goto error;
1075 for (i = 0; i < n; i++)
1076 brelse(bhs[i]);
1078 return 0;
1080 error:
1081 for (i = 0; i < n; i++)
1082 bforget(bhs[i]);
1083 return err;
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;
1092 sector_t blknr;
1093 __le16 date, time;
1094 u8 time_cs;
1095 int err, cluster;
1097 err = fat_alloc_clusters(dir, &cluster, 1);
1098 if (err)
1099 goto error;
1101 blknr = fat_clus_to_blknr(sbi, cluster);
1102 bhs[0] = sb_getblk(sb, blknr);
1103 if (!bhs[0]) {
1104 err = -ENOMEM;
1105 goto error_free;
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;
1123 } else {
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_inode(bhs[0], dir);
1137 err = fat_zeroed_cluster(dir, blknr, 1, bhs, MAX_BUF_PER_PAGE);
1138 if (err)
1139 goto error_free;
1141 return cluster;
1143 error_free:
1144 fat_free_clusters(dir, cluster);
1145 error:
1146 return err;
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);
1172 if (err)
1173 goto error;
1176 * First stage: Fill the directory entry. NOTE: This cluster
1177 * is not referenced from any inode yet, so updates order is
1178 * not important.
1180 i = n = copy = 0;
1181 do {
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);
1186 if (!bhs[n]) {
1187 err = -ENOMEM;
1188 goto error_nomem;
1191 /* fill the directory entry */
1192 copy = min(size, sb->s_blocksize);
1193 memcpy(bhs[n]->b_data, slots, copy);
1194 slots += copy;
1195 size -= copy;
1196 set_buffer_uptodate(bhs[n]);
1197 mark_buffer_dirty_inode(bhs[n], dir);
1198 if (!size)
1199 break;
1200 n++;
1201 blknr++;
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);
1207 get_bh(bhs[n]);
1208 *bh = bhs[n];
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);
1214 if (err)
1215 goto error_free;
1217 return cluster[0];
1219 error_free:
1220 brelse(*bh);
1221 *bh = NULL;
1222 n = 0;
1223 error_nomem:
1224 for (i = 0; i < n; i++)
1225 bforget(bhs[i]);
1226 fat_free_clusters(dir, cluster[0]);
1227 error:
1228 return err;
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;
1239 loff_t pos, i_pos;
1241 sinfo->nr_slots = nr_slots;
1243 /* First stage: search free direcotry entries */
1244 free_slots = nr_bhs = 0;
1245 bh = prev = NULL;
1246 pos = 0;
1247 err = -ENOSPC;
1248 while (fat_get_entry(dir, &pos, &bh, &de) > -1) {
1249 /* check the maximum size of directory */
1250 if (pos >= FAT_MAX_DIR_SIZE)
1251 goto error;
1253 if (IS_FREE(de->name)) {
1254 if (prev != bh) {
1255 get_bh(bh);
1256 bhs[nr_bhs] = prev = bh;
1257 nr_bhs++;
1259 free_slots++;
1260 if (free_slots == nr_slots)
1261 goto found;
1262 } else {
1263 for (i = 0; i < nr_bhs; i++)
1264 brelse(bhs[i]);
1265 prev = NULL;
1266 free_slots = nr_bhs = 0;
1269 if (dir->i_ino == MSDOS_ROOT_INO) {
1270 if (sbi->fat_bits != 32)
1271 goto error;
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);
1275 err = -EIO;
1276 goto error;
1279 found:
1280 err = 0;
1281 pos -= free_slots * sizeof(*de);
1282 nr_slots -= free_slots;
1283 if (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_inode(bhs[i], dir);
1298 offset = 0;
1299 slots += copy;
1300 size -= copy;
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_inode(bhs[i], dir);
1309 if (IS_DIRSYNC(dir))
1310 err = sync_dirty_buffer(bhs[i]);
1312 for (i = 0; i < nr_bhs; i++)
1313 brelse(bhs[i]);
1314 if (err)
1315 goto error_remove;
1318 if (nr_slots) {
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,
1327 &de, &bh, &i_pos);
1328 if (cluster < 0) {
1329 err = cluster;
1330 goto error_remove;
1332 err = fat_chain_add(dir, cluster, nr_cluster);
1333 if (err) {
1334 fat_free_clusters(dir, cluster);
1335 goto error_remove;
1337 if (dir->i_size & (sbi->cluster_size - 1)) {
1338 fat_fs_error(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;
1346 sinfo->de = de;
1347 sinfo->bh = bh;
1348 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
1350 return 0;
1352 error:
1353 brelse(bh);
1354 for (i = 0; i < nr_bhs; i++)
1355 brelse(bhs[i]);
1356 return err;
1358 error_remove:
1359 brelse(bh);
1360 if (free_slots)
1361 __fat_remove_entries(dir, pos, free_slots);
1362 return err;
1365 EXPORT_SYMBOL_GPL(fat_add_entries);