x86: add PAGE_KERNEL_EXEC_NOCACHE
[wrt350n-kernel.git] / fs / fat / dir.c
blob72cbcd61bd951268fa49eb18faf42d516606b6dd
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/dirent.h>
21 #include <linux/smp_lock.h>
22 #include <linux/buffer_head.h>
23 #include <linux/compat.h>
24 #include <asm/uaccess.h>
26 static inline loff_t fat_make_i_pos(struct super_block *sb,
27 struct buffer_head *bh,
28 struct msdos_dir_entry *de)
30 return ((loff_t)bh->b_blocknr << MSDOS_SB(sb)->dir_per_block_bits)
31 | (de - (struct msdos_dir_entry *)bh->b_data);
34 static inline void fat_dir_readahead(struct inode *dir, sector_t iblock,
35 sector_t phys)
37 struct super_block *sb = dir->i_sb;
38 struct msdos_sb_info *sbi = MSDOS_SB(sb);
39 struct buffer_head *bh;
40 int sec;
42 /* This is not a first sector of cluster, or sec_per_clus == 1 */
43 if ((iblock & (sbi->sec_per_clus - 1)) || sbi->sec_per_clus == 1)
44 return;
45 /* root dir of FAT12/FAT16 */
46 if ((sbi->fat_bits != 32) && (dir->i_ino == MSDOS_ROOT_INO))
47 return;
49 bh = sb_find_get_block(sb, phys);
50 if (bh == NULL || !buffer_uptodate(bh)) {
51 for (sec = 0; sec < sbi->sec_per_clus; sec++)
52 sb_breadahead(sb, phys + sec);
54 brelse(bh);
57 /* Returns the inode number of the directory entry at offset pos. If bh is
58 non-NULL, it is brelse'd before. Pos is incremented. The buffer header is
59 returned in bh.
60 AV. Most often we do it item-by-item. Makes sense to optimize.
61 AV. OK, there we go: if both bh and de are non-NULL we assume that we just
62 AV. want the next entry (took one explicit de=NULL in vfat/namei.c).
63 AV. It's done in fat_get_entry() (inlined), here the slow case lives.
64 AV. Additionally, when we return -1 (i.e. reached the end of directory)
65 AV. we make bh NULL.
67 static int fat__get_entry(struct inode *dir, loff_t *pos,
68 struct buffer_head **bh, struct msdos_dir_entry **de)
70 struct super_block *sb = dir->i_sb;
71 sector_t phys, iblock;
72 unsigned long mapped_blocks;
73 int err, offset;
75 next:
76 if (*bh)
77 brelse(*bh);
79 *bh = NULL;
80 iblock = *pos >> sb->s_blocksize_bits;
81 err = fat_bmap(dir, iblock, &phys, &mapped_blocks);
82 if (err || !phys)
83 return -1; /* beyond EOF or error */
85 fat_dir_readahead(dir, iblock, phys);
87 *bh = sb_bread(sb, phys);
88 if (*bh == NULL) {
89 printk(KERN_ERR "FAT: Directory bread(block %llu) failed\n",
90 (unsigned long long)phys);
91 /* skip this block */
92 *pos = (iblock + 1) << sb->s_blocksize_bits;
93 goto next;
96 offset = *pos & (sb->s_blocksize - 1);
97 *pos += sizeof(struct msdos_dir_entry);
98 *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
100 return 0;
103 static inline int fat_get_entry(struct inode *dir, loff_t *pos,
104 struct buffer_head **bh,
105 struct msdos_dir_entry **de)
107 /* Fast stuff first */
108 if (*bh && *de &&
109 (*de - (struct msdos_dir_entry *)(*bh)->b_data) < MSDOS_SB(dir->i_sb)->dir_per_block - 1) {
110 *pos += sizeof(struct msdos_dir_entry);
111 (*de)++;
112 return 0;
114 return fat__get_entry(dir, pos, bh, de);
118 * Convert Unicode 16 to UTF-8, translated Unicode, or ASCII.
119 * If uni_xlate is enabled and we can't get a 1:1 conversion, use a
120 * colon as an escape character since it is normally invalid on the vfat
121 * filesystem. The following four characters are the hexadecimal digits
122 * of Unicode value. This lets us do a full dump and restore of Unicode
123 * filenames. We could get into some trouble with long Unicode names,
124 * but ignore that right now.
125 * Ahem... Stack smashing in ring 0 isn't fun. Fixed.
127 static int uni16_to_x8(unsigned char *ascii, wchar_t *uni, int uni_xlate,
128 struct nls_table *nls)
130 wchar_t *ip, ec;
131 unsigned char *op, nc;
132 int charlen;
133 int k;
135 ip = uni;
136 op = ascii;
138 while (*ip) {
139 ec = *ip++;
140 if ( (charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE)) > 0) {
141 op += charlen;
142 } else {
143 if (uni_xlate == 1) {
144 *op = ':';
145 for (k = 4; k > 0; k--) {
146 nc = ec & 0xF;
147 op[k] = nc > 9 ? nc + ('a' - 10)
148 : nc + '0';
149 ec >>= 4;
151 op += 5;
152 } else {
153 *op++ = '?';
156 /* We have some slack there, so it's OK */
157 if (op>ascii+256) {
158 op = ascii + 256;
159 break;
162 *op = 0;
163 return (op - ascii);
166 static inline int
167 fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
169 int charlen;
171 charlen = t->char2uni(c, clen, uni);
172 if (charlen < 0) {
173 *uni = 0x003f; /* a question mark */
174 charlen = 1;
176 return charlen;
179 static inline int
180 fat_short2lower_uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
182 int charlen;
183 wchar_t wc;
185 charlen = t->char2uni(c, clen, &wc);
186 if (charlen < 0) {
187 *uni = 0x003f; /* a question mark */
188 charlen = 1;
189 } else if (charlen <= 1) {
190 unsigned char nc = t->charset2lower[*c];
192 if (!nc)
193 nc = *c;
195 if ( (charlen = t->char2uni(&nc, 1, uni)) < 0) {
196 *uni = 0x003f; /* a question mark */
197 charlen = 1;
199 } else
200 *uni = wc;
202 return charlen;
205 static inline int
206 fat_shortname2uni(struct nls_table *nls, unsigned char *buf, int buf_size,
207 wchar_t *uni_buf, unsigned short opt, int lower)
209 int len = 0;
211 if (opt & VFAT_SFN_DISPLAY_LOWER)
212 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
213 else if (opt & VFAT_SFN_DISPLAY_WIN95)
214 len = fat_short2uni(nls, buf, buf_size, uni_buf);
215 else if (opt & VFAT_SFN_DISPLAY_WINNT) {
216 if (lower)
217 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
218 else
219 len = fat_short2uni(nls, buf, buf_size, uni_buf);
220 } else
221 len = fat_short2uni(nls, buf, buf_size, uni_buf);
223 return len;
226 enum { PARSE_INVALID = 1, PARSE_NOT_LONGNAME, PARSE_EOF, };
229 * fat_parse_long - Parse extended directory entry.
231 * This function returns zero on success, negative value on error, or one of
232 * the following:
234 * %PARSE_INVALID - Directory entry is invalid.
235 * %PARSE_NOT_LONGNAME - Directory entry does not contain longname.
236 * %PARSE_EOF - Directory has no more entries.
238 static int fat_parse_long(struct inode *dir, loff_t *pos,
239 struct buffer_head **bh, struct msdos_dir_entry **de,
240 wchar_t **unicode, unsigned char *nr_slots)
242 struct msdos_dir_slot *ds;
243 unsigned char id, slot, slots, alias_checksum;
245 if (!*unicode) {
246 *unicode = (wchar_t *)__get_free_page(GFP_KERNEL);
247 if (!*unicode) {
248 brelse(*bh);
249 return -ENOMEM;
252 parse_long:
253 slots = 0;
254 ds = (struct msdos_dir_slot *)*de;
255 id = ds->id;
256 if (!(id & 0x40))
257 return PARSE_INVALID;
258 slots = id & ~0x40;
259 if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */
260 return PARSE_INVALID;
261 *nr_slots = slots;
262 alias_checksum = ds->alias_checksum;
264 slot = slots;
265 while (1) {
266 int offset;
268 slot--;
269 offset = slot * 13;
270 fat16_towchar(*unicode + offset, ds->name0_4, 5);
271 fat16_towchar(*unicode + offset + 5, ds->name5_10, 6);
272 fat16_towchar(*unicode + offset + 11, ds->name11_12, 2);
274 if (ds->id & 0x40)
275 (*unicode)[offset + 13] = 0;
276 if (fat_get_entry(dir, pos, bh, de) < 0)
277 return PARSE_EOF;
278 if (slot == 0)
279 break;
280 ds = (struct msdos_dir_slot *)*de;
281 if (ds->attr != ATTR_EXT)
282 return PARSE_NOT_LONGNAME;
283 if ((ds->id & ~0x40) != slot)
284 goto parse_long;
285 if (ds->alias_checksum != alias_checksum)
286 goto parse_long;
288 if ((*de)->name[0] == DELETED_FLAG)
289 return PARSE_INVALID;
290 if ((*de)->attr == ATTR_EXT)
291 goto parse_long;
292 if (IS_FREE((*de)->name) || ((*de)->attr & ATTR_VOLUME))
293 return PARSE_INVALID;
294 if (fat_checksum((*de)->name) != alias_checksum)
295 *nr_slots = 0;
297 return 0;
301 * Return values: negative -> error, 0 -> not found, positive -> found,
302 * value is the total amount of slots, including the shortname entry.
304 int fat_search_long(struct inode *inode, const unsigned char *name,
305 int name_len, struct fat_slot_info *sinfo)
307 struct super_block *sb = inode->i_sb;
308 struct msdos_sb_info *sbi = MSDOS_SB(sb);
309 struct buffer_head *bh = NULL;
310 struct msdos_dir_entry *de;
311 struct nls_table *nls_io = sbi->nls_io;
312 struct nls_table *nls_disk = sbi->nls_disk;
313 wchar_t bufuname[14];
314 unsigned char xlate_len, nr_slots;
315 wchar_t *unicode = NULL;
316 unsigned char work[MSDOS_NAME], bufname[260]; /* 256 + 4 */
317 int uni_xlate = sbi->options.unicode_xlate;
318 int utf8 = sbi->options.utf8;
319 int anycase = (sbi->options.name_check != 's');
320 unsigned short opt_shortname = sbi->options.shortname;
321 loff_t cpos = 0;
322 int chl, i, j, last_u, err;
324 err = -ENOENT;
325 while(1) {
326 if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
327 goto EODir;
328 parse_record:
329 nr_slots = 0;
330 if (de->name[0] == DELETED_FLAG)
331 continue;
332 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
333 continue;
334 if (de->attr != ATTR_EXT && IS_FREE(de->name))
335 continue;
336 if (de->attr == ATTR_EXT) {
337 int status = fat_parse_long(inode, &cpos, &bh, &de,
338 &unicode, &nr_slots);
339 if (status < 0)
340 return status;
341 else if (status == PARSE_INVALID)
342 continue;
343 else if (status == PARSE_NOT_LONGNAME)
344 goto parse_record;
345 else if (status == PARSE_EOF)
346 goto EODir;
349 memcpy(work, de->name, sizeof(de->name));
350 /* see namei.c, msdos_format_name */
351 if (work[0] == 0x05)
352 work[0] = 0xE5;
353 for (i = 0, j = 0, last_u = 0; i < 8;) {
354 if (!work[i])
355 break;
356 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
357 &bufuname[j++], opt_shortname,
358 de->lcase & CASE_LOWER_BASE);
359 if (chl <= 1) {
360 if (work[i] != ' ')
361 last_u = j;
362 } else {
363 last_u = j;
365 i += chl;
367 j = last_u;
368 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
369 for (i = 8; i < MSDOS_NAME;) {
370 if (!work[i])
371 break;
372 chl = fat_shortname2uni(nls_disk, &work[i],
373 MSDOS_NAME - i,
374 &bufuname[j++], opt_shortname,
375 de->lcase & CASE_LOWER_EXT);
376 if (chl <= 1) {
377 if (work[i] != ' ')
378 last_u = j;
379 } else {
380 last_u = j;
382 i += chl;
384 if (!last_u)
385 continue;
387 bufuname[last_u] = 0x0000;
388 xlate_len = utf8
389 ?utf8_wcstombs(bufname, bufuname, sizeof(bufname))
390 :uni16_to_x8(bufname, bufuname, uni_xlate, nls_io);
391 if (xlate_len == name_len)
392 if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
393 (anycase && !nls_strnicmp(nls_io, name, bufname,
394 xlate_len)))
395 goto Found;
397 if (nr_slots) {
398 xlate_len = utf8
399 ?utf8_wcstombs(bufname, unicode, sizeof(bufname))
400 :uni16_to_x8(bufname, unicode, uni_xlate, nls_io);
401 if (xlate_len != name_len)
402 continue;
403 if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
404 (anycase && !nls_strnicmp(nls_io, name, bufname,
405 xlate_len)))
406 goto Found;
410 Found:
411 nr_slots++; /* include the de */
412 sinfo->slot_off = cpos - nr_slots * sizeof(*de);
413 sinfo->nr_slots = nr_slots;
414 sinfo->de = de;
415 sinfo->bh = bh;
416 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
417 err = 0;
418 EODir:
419 if (unicode)
420 free_page((unsigned long)unicode);
422 return err;
425 EXPORT_SYMBOL_GPL(fat_search_long);
427 struct fat_ioctl_filldir_callback {
428 void __user *dirent;
429 int result;
430 /* for dir ioctl */
431 const char *longname;
432 int long_len;
433 const char *shortname;
434 int short_len;
437 static int __fat_readdir(struct inode *inode, struct file *filp, void *dirent,
438 filldir_t filldir, int short_only, int both)
440 struct super_block *sb = inode->i_sb;
441 struct msdos_sb_info *sbi = MSDOS_SB(sb);
442 struct buffer_head *bh;
443 struct msdos_dir_entry *de;
444 struct nls_table *nls_io = sbi->nls_io;
445 struct nls_table *nls_disk = sbi->nls_disk;
446 unsigned char long_slots;
447 const char *fill_name;
448 int fill_len;
449 wchar_t bufuname[14];
450 wchar_t *unicode = NULL;
451 unsigned char c, work[MSDOS_NAME], bufname[56], *ptname = bufname;
452 unsigned long lpos, dummy, *furrfu = &lpos;
453 int uni_xlate = sbi->options.unicode_xlate;
454 int isvfat = sbi->options.isvfat;
455 int utf8 = sbi->options.utf8;
456 int nocase = sbi->options.nocase;
457 unsigned short opt_shortname = sbi->options.shortname;
458 unsigned long inum;
459 int chi, chl, i, i2, j, last, last_u, dotoffset = 0;
460 loff_t cpos;
461 int ret = 0;
463 lock_kernel();
465 cpos = filp->f_pos;
466 /* Fake . and .. for the root directory. */
467 if (inode->i_ino == MSDOS_ROOT_INO) {
468 while (cpos < 2) {
469 if (filldir(dirent, "..", cpos+1, cpos, MSDOS_ROOT_INO, DT_DIR) < 0)
470 goto out;
471 cpos++;
472 filp->f_pos++;
474 if (cpos == 2) {
475 dummy = 2;
476 furrfu = &dummy;
477 cpos = 0;
480 if (cpos & (sizeof(struct msdos_dir_entry)-1)) {
481 ret = -ENOENT;
482 goto out;
485 bh = NULL;
486 GetNew:
487 if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
488 goto EODir;
489 parse_record:
490 long_slots = 0;
491 /* Check for long filename entry */
492 if (isvfat) {
493 if (de->name[0] == DELETED_FLAG)
494 goto RecEnd;
495 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
496 goto RecEnd;
497 if (de->attr != ATTR_EXT && IS_FREE(de->name))
498 goto RecEnd;
499 } else {
500 if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
501 goto RecEnd;
504 if (isvfat && de->attr == ATTR_EXT) {
505 int status = fat_parse_long(inode, &cpos, &bh, &de,
506 &unicode, &long_slots);
507 if (status < 0) {
508 filp->f_pos = cpos;
509 ret = status;
510 goto out;
511 } else if (status == PARSE_INVALID)
512 goto RecEnd;
513 else if (status == PARSE_NOT_LONGNAME)
514 goto parse_record;
515 else if (status == PARSE_EOF)
516 goto EODir;
519 if (sbi->options.dotsOK) {
520 ptname = bufname;
521 dotoffset = 0;
522 if (de->attr & ATTR_HIDDEN) {
523 *ptname++ = '.';
524 dotoffset = 1;
528 memcpy(work, de->name, sizeof(de->name));
529 /* see namei.c, msdos_format_name */
530 if (work[0] == 0x05)
531 work[0] = 0xE5;
532 for (i = 0, j = 0, last = 0, last_u = 0; i < 8;) {
533 if (!(c = work[i]))
534 break;
535 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
536 &bufuname[j++], opt_shortname,
537 de->lcase & CASE_LOWER_BASE);
538 if (chl <= 1) {
539 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
540 if (c != ' ') {
541 last = i;
542 last_u = j;
544 } else {
545 last_u = j;
546 for (chi = 0; chi < chl && i < 8; chi++) {
547 ptname[i] = work[i];
548 i++; last = i;
552 i = last;
553 j = last_u;
554 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
555 ptname[i++] = '.';
556 for (i2 = 8; i2 < MSDOS_NAME;) {
557 if (!(c = work[i2]))
558 break;
559 chl = fat_shortname2uni(nls_disk, &work[i2], MSDOS_NAME - i2,
560 &bufuname[j++], opt_shortname,
561 de->lcase & CASE_LOWER_EXT);
562 if (chl <= 1) {
563 i2++;
564 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
565 if (c != ' ') {
566 last = i;
567 last_u = j;
569 } else {
570 last_u = j;
571 for (chi = 0; chi < chl && i2 < MSDOS_NAME; chi++) {
572 ptname[i++] = work[i2++];
573 last = i;
577 if (!last)
578 goto RecEnd;
580 i = last + dotoffset;
581 j = last_u;
583 lpos = cpos - (long_slots+1)*sizeof(struct msdos_dir_entry);
584 if (!memcmp(de->name, MSDOS_DOT, MSDOS_NAME))
585 inum = inode->i_ino;
586 else if (!memcmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
587 inum = parent_ino(filp->f_path.dentry);
588 } else {
589 loff_t i_pos = fat_make_i_pos(sb, bh, de);
590 struct inode *tmp = fat_iget(sb, i_pos);
591 if (tmp) {
592 inum = tmp->i_ino;
593 iput(tmp);
594 } else
595 inum = iunique(sb, MSDOS_ROOT_INO);
598 if (isvfat) {
599 bufuname[j] = 0x0000;
600 i = utf8 ? utf8_wcstombs(bufname, bufuname, sizeof(bufname))
601 : uni16_to_x8(bufname, bufuname, uni_xlate, nls_io);
604 fill_name = bufname;
605 fill_len = i;
606 if (!short_only && long_slots) {
607 /* convert the unicode long name. 261 is maximum size
608 * of unicode buffer. (13 * slots + nul) */
609 void *longname = unicode + 261;
610 int buf_size = PAGE_SIZE - (261 * sizeof(unicode[0]));
611 int long_len = utf8
612 ? utf8_wcstombs(longname, unicode, buf_size)
613 : uni16_to_x8(longname, unicode, uni_xlate, nls_io);
615 if (!both) {
616 fill_name = longname;
617 fill_len = long_len;
618 } else {
619 /* hack for fat_ioctl_filldir() */
620 struct fat_ioctl_filldir_callback *p = dirent;
622 p->longname = longname;
623 p->long_len = long_len;
624 p->shortname = bufname;
625 p->short_len = i;
626 fill_name = NULL;
627 fill_len = 0;
630 if (filldir(dirent, fill_name, fill_len, *furrfu, inum,
631 (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
632 goto FillFailed;
634 RecEnd:
635 furrfu = &lpos;
636 filp->f_pos = cpos;
637 goto GetNew;
638 EODir:
639 filp->f_pos = cpos;
640 FillFailed:
641 brelse(bh);
642 if (unicode)
643 free_page((unsigned long)unicode);
644 out:
645 unlock_kernel();
646 return ret;
649 static int fat_readdir(struct file *filp, void *dirent, filldir_t filldir)
651 struct inode *inode = filp->f_path.dentry->d_inode;
652 return __fat_readdir(inode, filp, dirent, filldir, 0, 0);
655 #define FAT_IOCTL_FILLDIR_FUNC(func, dirent_type) \
656 static int func(void *__buf, const char *name, int name_len, \
657 loff_t offset, u64 ino, unsigned int d_type) \
659 struct fat_ioctl_filldir_callback *buf = __buf; \
660 struct dirent_type __user *d1 = buf->dirent; \
661 struct dirent_type __user *d2 = d1 + 1; \
663 if (buf->result) \
664 return -EINVAL; \
665 buf->result++; \
667 if (name != NULL) { \
668 /* dirent has only short name */ \
669 if (name_len >= sizeof(d1->d_name)) \
670 name_len = sizeof(d1->d_name) - 1; \
672 if (put_user(0, d2->d_name) || \
673 put_user(0, &d2->d_reclen) || \
674 copy_to_user(d1->d_name, name, name_len) || \
675 put_user(0, d1->d_name + name_len) || \
676 put_user(name_len, &d1->d_reclen)) \
677 goto efault; \
678 } else { \
679 /* dirent has short and long name */ \
680 const char *longname = buf->longname; \
681 int long_len = buf->long_len; \
682 const char *shortname = buf->shortname; \
683 int short_len = buf->short_len; \
685 if (long_len >= sizeof(d1->d_name)) \
686 long_len = sizeof(d1->d_name) - 1; \
687 if (short_len >= sizeof(d1->d_name)) \
688 short_len = sizeof(d1->d_name) - 1; \
690 if (copy_to_user(d2->d_name, longname, long_len) || \
691 put_user(0, d2->d_name + long_len) || \
692 put_user(long_len, &d2->d_reclen) || \
693 put_user(ino, &d2->d_ino) || \
694 put_user(offset, &d2->d_off) || \
695 copy_to_user(d1->d_name, shortname, short_len) || \
696 put_user(0, d1->d_name + short_len) || \
697 put_user(short_len, &d1->d_reclen)) \
698 goto efault; \
700 return 0; \
701 efault: \
702 buf->result = -EFAULT; \
703 return -EFAULT; \
706 FAT_IOCTL_FILLDIR_FUNC(fat_ioctl_filldir, dirent)
708 static int fat_ioctl_readdir(struct inode *inode, struct file *filp,
709 void __user *dirent, filldir_t filldir,
710 int short_only, int both)
712 struct fat_ioctl_filldir_callback buf;
713 int ret;
715 buf.dirent = dirent;
716 buf.result = 0;
717 mutex_lock(&inode->i_mutex);
718 ret = -ENOENT;
719 if (!IS_DEADDIR(inode)) {
720 ret = __fat_readdir(inode, filp, &buf, filldir,
721 short_only, both);
723 mutex_unlock(&inode->i_mutex);
724 if (ret >= 0)
725 ret = buf.result;
726 return ret;
729 static int fat_dir_ioctl(struct inode *inode, struct file *filp,
730 unsigned int cmd, unsigned long arg)
732 struct dirent __user *d1 = (struct dirent __user *)arg;
733 int short_only, both;
735 switch (cmd) {
736 case VFAT_IOCTL_READDIR_SHORT:
737 short_only = 1;
738 both = 0;
739 break;
740 case VFAT_IOCTL_READDIR_BOTH:
741 short_only = 0;
742 both = 1;
743 break;
744 default:
745 return fat_generic_ioctl(inode, filp, cmd, arg);
748 if (!access_ok(VERIFY_WRITE, d1, sizeof(struct dirent[2])))
749 return -EFAULT;
751 * Yes, we don't need this put_user() absolutely. However old
752 * code didn't return the right value. So, app use this value,
753 * in order to check whether it is EOF.
755 if (put_user(0, &d1->d_reclen))
756 return -EFAULT;
758 return fat_ioctl_readdir(inode, filp, d1, fat_ioctl_filldir,
759 short_only, both);
762 #ifdef CONFIG_COMPAT
763 #define VFAT_IOCTL_READDIR_BOTH32 _IOR('r', 1, struct compat_dirent[2])
764 #define VFAT_IOCTL_READDIR_SHORT32 _IOR('r', 2, struct compat_dirent[2])
766 FAT_IOCTL_FILLDIR_FUNC(fat_compat_ioctl_filldir, compat_dirent)
768 static long fat_compat_dir_ioctl(struct file *filp, unsigned cmd,
769 unsigned long arg)
771 struct inode *inode = filp->f_path.dentry->d_inode;
772 struct compat_dirent __user *d1 = compat_ptr(arg);
773 int short_only, both;
775 switch (cmd) {
776 case VFAT_IOCTL_READDIR_SHORT32:
777 short_only = 1;
778 both = 0;
779 break;
780 case VFAT_IOCTL_READDIR_BOTH32:
781 short_only = 0;
782 both = 1;
783 break;
784 default:
785 return -ENOIOCTLCMD;
788 if (!access_ok(VERIFY_WRITE, d1, sizeof(struct compat_dirent[2])))
789 return -EFAULT;
791 * Yes, we don't need this put_user() absolutely. However old
792 * code didn't return the right value. So, app use this value,
793 * in order to check whether it is EOF.
795 if (put_user(0, &d1->d_reclen))
796 return -EFAULT;
798 return fat_ioctl_readdir(inode, filp, d1, fat_compat_ioctl_filldir,
799 short_only, both);
801 #endif /* CONFIG_COMPAT */
803 const struct file_operations fat_dir_operations = {
804 .read = generic_read_dir,
805 .readdir = fat_readdir,
806 .ioctl = fat_dir_ioctl,
807 #ifdef CONFIG_COMPAT
808 .compat_ioctl = fat_compat_dir_ioctl,
809 #endif
810 .fsync = file_fsync,
813 static int fat_get_short_entry(struct inode *dir, loff_t *pos,
814 struct buffer_head **bh,
815 struct msdos_dir_entry **de)
817 while (fat_get_entry(dir, pos, bh, de) >= 0) {
818 /* free entry or long name entry or volume label */
819 if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME))
820 return 0;
822 return -ENOENT;
826 * The ".." entry can not provide the "struct fat_slot_info" informations
827 * for inode. So, this function provide the some informations only.
829 int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh,
830 struct msdos_dir_entry **de, loff_t *i_pos)
832 loff_t offset;
834 offset = 0;
835 *bh = NULL;
836 while (fat_get_short_entry(dir, &offset, bh, de) >= 0) {
837 if (!strncmp((*de)->name, MSDOS_DOTDOT, MSDOS_NAME)) {
838 *i_pos = fat_make_i_pos(dir->i_sb, *bh, *de);
839 return 0;
842 return -ENOENT;
845 EXPORT_SYMBOL_GPL(fat_get_dotdot_entry);
847 /* See if directory is empty */
848 int fat_dir_empty(struct inode *dir)
850 struct buffer_head *bh;
851 struct msdos_dir_entry *de;
852 loff_t cpos;
853 int result = 0;
855 bh = NULL;
856 cpos = 0;
857 while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
858 if (strncmp(de->name, MSDOS_DOT , MSDOS_NAME) &&
859 strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
860 result = -ENOTEMPTY;
861 break;
864 brelse(bh);
865 return result;
868 EXPORT_SYMBOL_GPL(fat_dir_empty);
871 * fat_subdirs counts the number of sub-directories of dir. It can be run
872 * on directories being created.
874 int fat_subdirs(struct inode *dir)
876 struct buffer_head *bh;
877 struct msdos_dir_entry *de;
878 loff_t cpos;
879 int count = 0;
881 bh = NULL;
882 cpos = 0;
883 while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
884 if (de->attr & ATTR_DIR)
885 count++;
887 brelse(bh);
888 return count;
892 * Scans a directory for a given file (name points to its formatted name).
893 * Returns an error code or zero.
895 int fat_scan(struct inode *dir, const unsigned char *name,
896 struct fat_slot_info *sinfo)
898 struct super_block *sb = dir->i_sb;
900 sinfo->slot_off = 0;
901 sinfo->bh = NULL;
902 while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh,
903 &sinfo->de) >= 0) {
904 if (!strncmp(sinfo->de->name, name, MSDOS_NAME)) {
905 sinfo->slot_off -= sizeof(*sinfo->de);
906 sinfo->nr_slots = 1;
907 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
908 return 0;
911 return -ENOENT;
914 EXPORT_SYMBOL_GPL(fat_scan);
916 static int __fat_remove_entries(struct inode *dir, loff_t pos, int nr_slots)
918 struct super_block *sb = dir->i_sb;
919 struct buffer_head *bh;
920 struct msdos_dir_entry *de, *endp;
921 int err = 0, orig_slots;
923 while (nr_slots) {
924 bh = NULL;
925 if (fat_get_entry(dir, &pos, &bh, &de) < 0) {
926 err = -EIO;
927 break;
930 orig_slots = nr_slots;
931 endp = (struct msdos_dir_entry *)(bh->b_data + sb->s_blocksize);
932 while (nr_slots && de < endp) {
933 de->name[0] = DELETED_FLAG;
934 de++;
935 nr_slots--;
937 mark_buffer_dirty(bh);
938 if (IS_DIRSYNC(dir))
939 err = sync_dirty_buffer(bh);
940 brelse(bh);
941 if (err)
942 break;
944 /* pos is *next* de's position, so this does `- sizeof(de)' */
945 pos += ((orig_slots - nr_slots) * sizeof(*de)) - sizeof(*de);
948 return err;
951 int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo)
953 struct msdos_dir_entry *de;
954 struct buffer_head *bh;
955 int err = 0, nr_slots;
958 * First stage: Remove the shortname. By this, the directory
959 * entry is removed.
961 nr_slots = sinfo->nr_slots;
962 de = sinfo->de;
963 sinfo->de = NULL;
964 bh = sinfo->bh;
965 sinfo->bh = NULL;
966 while (nr_slots && de >= (struct msdos_dir_entry *)bh->b_data) {
967 de->name[0] = DELETED_FLAG;
968 de--;
969 nr_slots--;
971 mark_buffer_dirty(bh);
972 if (IS_DIRSYNC(dir))
973 err = sync_dirty_buffer(bh);
974 brelse(bh);
975 if (err)
976 return err;
977 dir->i_version++;
979 if (nr_slots) {
981 * Second stage: remove the remaining longname slots.
982 * (This directory entry is already removed, and so return
983 * the success)
985 err = __fat_remove_entries(dir, sinfo->slot_off, nr_slots);
986 if (err) {
987 printk(KERN_WARNING
988 "FAT: Couldn't remove the long name slots\n");
992 dir->i_mtime = dir->i_atime = CURRENT_TIME_SEC;
993 if (IS_DIRSYNC(dir))
994 (void)fat_sync_inode(dir);
995 else
996 mark_inode_dirty(dir);
998 return 0;
1001 EXPORT_SYMBOL_GPL(fat_remove_entries);
1003 static int fat_zeroed_cluster(struct inode *dir, sector_t blknr, int nr_used,
1004 struct buffer_head **bhs, int nr_bhs)
1006 struct super_block *sb = dir->i_sb;
1007 sector_t last_blknr = blknr + MSDOS_SB(sb)->sec_per_clus;
1008 int err, i, n;
1010 /* Zeroing the unused blocks on this cluster */
1011 blknr += nr_used;
1012 n = nr_used;
1013 while (blknr < last_blknr) {
1014 bhs[n] = sb_getblk(sb, blknr);
1015 if (!bhs[n]) {
1016 err = -ENOMEM;
1017 goto error;
1019 memset(bhs[n]->b_data, 0, sb->s_blocksize);
1020 set_buffer_uptodate(bhs[n]);
1021 mark_buffer_dirty(bhs[n]);
1023 n++;
1024 blknr++;
1025 if (n == nr_bhs) {
1026 if (IS_DIRSYNC(dir)) {
1027 err = fat_sync_bhs(bhs, n);
1028 if (err)
1029 goto error;
1031 for (i = 0; i < n; i++)
1032 brelse(bhs[i]);
1033 n = 0;
1036 if (IS_DIRSYNC(dir)) {
1037 err = fat_sync_bhs(bhs, n);
1038 if (err)
1039 goto error;
1041 for (i = 0; i < n; i++)
1042 brelse(bhs[i]);
1044 return 0;
1046 error:
1047 for (i = 0; i < n; i++)
1048 bforget(bhs[i]);
1049 return err;
1052 int fat_alloc_new_dir(struct inode *dir, struct timespec *ts)
1054 struct super_block *sb = dir->i_sb;
1055 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1056 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
1057 struct msdos_dir_entry *de;
1058 sector_t blknr;
1059 __le16 date, time;
1060 int err, cluster;
1062 err = fat_alloc_clusters(dir, &cluster, 1);
1063 if (err)
1064 goto error;
1066 blknr = fat_clus_to_blknr(sbi, cluster);
1067 bhs[0] = sb_getblk(sb, blknr);
1068 if (!bhs[0]) {
1069 err = -ENOMEM;
1070 goto error_free;
1073 fat_date_unix2dos(ts->tv_sec, &time, &date);
1075 de = (struct msdos_dir_entry *)bhs[0]->b_data;
1076 /* filling the new directory slots ("." and ".." entries) */
1077 memcpy(de[0].name, MSDOS_DOT, MSDOS_NAME);
1078 memcpy(de[1].name, MSDOS_DOTDOT, MSDOS_NAME);
1079 de->attr = de[1].attr = ATTR_DIR;
1080 de[0].lcase = de[1].lcase = 0;
1081 de[0].time = de[1].time = time;
1082 de[0].date = de[1].date = date;
1083 de[0].ctime_cs = de[1].ctime_cs = 0;
1084 if (sbi->options.isvfat) {
1085 /* extra timestamps */
1086 de[0].ctime = de[1].ctime = time;
1087 de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = date;
1088 } else {
1089 de[0].ctime = de[1].ctime = 0;
1090 de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = 0;
1092 de[0].start = cpu_to_le16(cluster);
1093 de[0].starthi = cpu_to_le16(cluster >> 16);
1094 de[1].start = cpu_to_le16(MSDOS_I(dir)->i_logstart);
1095 de[1].starthi = cpu_to_le16(MSDOS_I(dir)->i_logstart >> 16);
1096 de[0].size = de[1].size = 0;
1097 memset(de + 2, 0, sb->s_blocksize - 2 * sizeof(*de));
1098 set_buffer_uptodate(bhs[0]);
1099 mark_buffer_dirty(bhs[0]);
1101 err = fat_zeroed_cluster(dir, blknr, 1, bhs, MAX_BUF_PER_PAGE);
1102 if (err)
1103 goto error_free;
1105 return cluster;
1107 error_free:
1108 fat_free_clusters(dir, cluster);
1109 error:
1110 return err;
1113 EXPORT_SYMBOL_GPL(fat_alloc_new_dir);
1115 static int fat_add_new_entries(struct inode *dir, void *slots, int nr_slots,
1116 int *nr_cluster, struct msdos_dir_entry **de,
1117 struct buffer_head **bh, loff_t *i_pos)
1119 struct super_block *sb = dir->i_sb;
1120 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1121 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
1122 sector_t blknr, start_blknr, last_blknr;
1123 unsigned long size, copy;
1124 int err, i, n, offset, cluster[2];
1127 * The minimum cluster size is 512bytes, and maximum entry
1128 * size is 32*slots (672bytes). So, iff the cluster size is
1129 * 512bytes, we may need two clusters.
1131 size = nr_slots * sizeof(struct msdos_dir_entry);
1132 *nr_cluster = (size + (sbi->cluster_size - 1)) >> sbi->cluster_bits;
1133 BUG_ON(*nr_cluster > 2);
1135 err = fat_alloc_clusters(dir, cluster, *nr_cluster);
1136 if (err)
1137 goto error;
1140 * First stage: Fill the directory entry. NOTE: This cluster
1141 * is not referenced from any inode yet, so updates order is
1142 * not important.
1144 i = n = copy = 0;
1145 do {
1146 start_blknr = blknr = fat_clus_to_blknr(sbi, cluster[i]);
1147 last_blknr = start_blknr + sbi->sec_per_clus;
1148 while (blknr < last_blknr) {
1149 bhs[n] = sb_getblk(sb, blknr);
1150 if (!bhs[n]) {
1151 err = -ENOMEM;
1152 goto error_nomem;
1155 /* fill the directory entry */
1156 copy = min(size, sb->s_blocksize);
1157 memcpy(bhs[n]->b_data, slots, copy);
1158 slots += copy;
1159 size -= copy;
1160 set_buffer_uptodate(bhs[n]);
1161 mark_buffer_dirty(bhs[n]);
1162 if (!size)
1163 break;
1164 n++;
1165 blknr++;
1167 } while (++i < *nr_cluster);
1169 memset(bhs[n]->b_data + copy, 0, sb->s_blocksize - copy);
1170 offset = copy - sizeof(struct msdos_dir_entry);
1171 get_bh(bhs[n]);
1172 *bh = bhs[n];
1173 *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
1174 *i_pos = fat_make_i_pos(sb, *bh, *de);
1176 /* Second stage: clear the rest of cluster, and write outs */
1177 err = fat_zeroed_cluster(dir, start_blknr, ++n, bhs, MAX_BUF_PER_PAGE);
1178 if (err)
1179 goto error_free;
1181 return cluster[0];
1183 error_free:
1184 brelse(*bh);
1185 *bh = NULL;
1186 n = 0;
1187 error_nomem:
1188 for (i = 0; i < n; i++)
1189 bforget(bhs[i]);
1190 fat_free_clusters(dir, cluster[0]);
1191 error:
1192 return err;
1195 int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
1196 struct fat_slot_info *sinfo)
1198 struct super_block *sb = dir->i_sb;
1199 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1200 struct buffer_head *bh, *prev, *bhs[3]; /* 32*slots (672bytes) */
1201 struct msdos_dir_entry *de;
1202 int err, free_slots, i, nr_bhs;
1203 loff_t pos, i_pos;
1205 sinfo->nr_slots = nr_slots;
1207 /* First stage: search free direcotry entries */
1208 free_slots = nr_bhs = 0;
1209 bh = prev = NULL;
1210 pos = 0;
1211 err = -ENOSPC;
1212 while (fat_get_entry(dir, &pos, &bh, &de) > -1) {
1213 /* check the maximum size of directory */
1214 if (pos >= FAT_MAX_DIR_SIZE)
1215 goto error;
1217 if (IS_FREE(de->name)) {
1218 if (prev != bh) {
1219 get_bh(bh);
1220 bhs[nr_bhs] = prev = bh;
1221 nr_bhs++;
1223 free_slots++;
1224 if (free_slots == nr_slots)
1225 goto found;
1226 } else {
1227 for (i = 0; i < nr_bhs; i++)
1228 brelse(bhs[i]);
1229 prev = NULL;
1230 free_slots = nr_bhs = 0;
1233 if (dir->i_ino == MSDOS_ROOT_INO) {
1234 if (sbi->fat_bits != 32)
1235 goto error;
1236 } else if (MSDOS_I(dir)->i_start == 0) {
1237 printk(KERN_ERR "FAT: Corrupted directory (i_pos %lld)\n",
1238 MSDOS_I(dir)->i_pos);
1239 err = -EIO;
1240 goto error;
1243 found:
1244 err = 0;
1245 pos -= free_slots * sizeof(*de);
1246 nr_slots -= free_slots;
1247 if (free_slots) {
1249 * Second stage: filling the free entries with new entries.
1250 * NOTE: If this slots has shortname, first, we write
1251 * the long name slots, then write the short name.
1253 int size = free_slots * sizeof(*de);
1254 int offset = pos & (sb->s_blocksize - 1);
1255 int long_bhs = nr_bhs - (nr_slots == 0);
1257 /* Fill the long name slots. */
1258 for (i = 0; i < long_bhs; i++) {
1259 int copy = min_t(int, sb->s_blocksize - offset, size);
1260 memcpy(bhs[i]->b_data + offset, slots, copy);
1261 mark_buffer_dirty(bhs[i]);
1262 offset = 0;
1263 slots += copy;
1264 size -= copy;
1266 if (long_bhs && IS_DIRSYNC(dir))
1267 err = fat_sync_bhs(bhs, long_bhs);
1268 if (!err && i < nr_bhs) {
1269 /* Fill the short name slot. */
1270 int copy = min_t(int, sb->s_blocksize - offset, size);
1271 memcpy(bhs[i]->b_data + offset, slots, copy);
1272 mark_buffer_dirty(bhs[i]);
1273 if (IS_DIRSYNC(dir))
1274 err = sync_dirty_buffer(bhs[i]);
1276 for (i = 0; i < nr_bhs; i++)
1277 brelse(bhs[i]);
1278 if (err)
1279 goto error_remove;
1282 if (nr_slots) {
1283 int cluster, nr_cluster;
1286 * Third stage: allocate the cluster for new entries.
1287 * And initialize the cluster with new entries, then
1288 * add the cluster to dir.
1290 cluster = fat_add_new_entries(dir, slots, nr_slots, &nr_cluster,
1291 &de, &bh, &i_pos);
1292 if (cluster < 0) {
1293 err = cluster;
1294 goto error_remove;
1296 err = fat_chain_add(dir, cluster, nr_cluster);
1297 if (err) {
1298 fat_free_clusters(dir, cluster);
1299 goto error_remove;
1301 if (dir->i_size & (sbi->cluster_size - 1)) {
1302 fat_fs_panic(sb, "Odd directory size");
1303 dir->i_size = (dir->i_size + sbi->cluster_size - 1)
1304 & ~((loff_t)sbi->cluster_size - 1);
1306 dir->i_size += nr_cluster << sbi->cluster_bits;
1307 MSDOS_I(dir)->mmu_private += nr_cluster << sbi->cluster_bits;
1309 sinfo->slot_off = pos;
1310 sinfo->de = de;
1311 sinfo->bh = bh;
1312 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
1314 return 0;
1316 error:
1317 brelse(bh);
1318 for (i = 0; i < nr_bhs; i++)
1319 brelse(bhs[i]);
1320 return err;
1322 error_remove:
1323 brelse(bh);
1324 if (free_slots)
1325 __fat_remove_entries(dir, pos, free_slots);
1326 return err;
1329 EXPORT_SYMBOL_GPL(fat_add_entries);