[PATCH] fat: cleanup and optimization of checksum
[linux-2.6/btrfs-unstable.git] / fs / fat / dir.c
blob204d8614406f9cd18efe2f660ebb1142e66d01f5
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 <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_getblk(sb, phys);
49 if (bh && !buffer_uptodate(bh)) {
50 for (sec = 0; sec < sbi->sec_per_clus; sec++)
51 sb_breadahead(sb, phys + sec);
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 int offset;
72 int err;
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);
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 UTF8, translated Unicode, or ASCII.
118 * If uni_xlate is enabled and we can't get a 1:1 conversion, use a
119 * colon as an escape character since it is normally invalid on the vfat
120 * filesystem. The following four characters are the hexadecimal digits
121 * of Unicode value. This lets us do a full dump and restore of Unicode
122 * filenames. We could get into some trouble with long Unicode names,
123 * but ignore that right now.
124 * Ahem... Stack smashing in ring 0 isn't fun. Fixed.
126 static int uni16_to_x8(unsigned char *ascii, wchar_t *uni, int uni_xlate,
127 struct nls_table *nls)
129 wchar_t *ip, ec;
130 unsigned char *op, nc;
131 int charlen;
132 int k;
134 ip = uni;
135 op = ascii;
137 while (*ip) {
138 ec = *ip++;
139 if ( (charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE)) > 0) {
140 op += charlen;
141 } else {
142 if (uni_xlate == 1) {
143 *op = ':';
144 for (k = 4; k > 0; k--) {
145 nc = ec & 0xF;
146 op[k] = nc > 9 ? nc + ('a' - 10)
147 : nc + '0';
148 ec >>= 4;
150 op += 5;
151 } else {
152 *op++ = '?';
155 /* We have some slack there, so it's OK */
156 if (op>ascii+256) {
157 op = ascii + 256;
158 break;
161 *op = 0;
162 return (op - ascii);
165 static inline int
166 fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
168 int charlen;
170 charlen = t->char2uni(c, clen, uni);
171 if (charlen < 0) {
172 *uni = 0x003f; /* a question mark */
173 charlen = 1;
175 return charlen;
178 static inline int
179 fat_short2lower_uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
181 int charlen;
182 wchar_t wc;
184 charlen = t->char2uni(c, clen, &wc);
185 if (charlen < 0) {
186 *uni = 0x003f; /* a question mark */
187 charlen = 1;
188 } else if (charlen <= 1) {
189 unsigned char nc = t->charset2lower[*c];
191 if (!nc)
192 nc = *c;
194 if ( (charlen = t->char2uni(&nc, 1, uni)) < 0) {
195 *uni = 0x003f; /* a question mark */
196 charlen = 1;
198 } else
199 *uni = wc;
201 return charlen;
204 static inline int
205 fat_shortname2uni(struct nls_table *nls, unsigned char *buf, int buf_size,
206 wchar_t *uni_buf, unsigned short opt, int lower)
208 int len = 0;
210 if (opt & VFAT_SFN_DISPLAY_LOWER)
211 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
212 else if (opt & VFAT_SFN_DISPLAY_WIN95)
213 len = fat_short2uni(nls, buf, buf_size, uni_buf);
214 else if (opt & VFAT_SFN_DISPLAY_WINNT) {
215 if (lower)
216 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
217 else
218 len = fat_short2uni(nls, buf, buf_size, uni_buf);
219 } else
220 len = fat_short2uni(nls, buf, buf_size, uni_buf);
222 return len;
226 * Return values: negative -> error, 0 -> not found, positive -> found,
227 * value is the total amount of slots, including the shortname entry.
229 int fat_search_long(struct inode *inode, const unsigned char *name,
230 int name_len, struct fat_slot_info *sinfo)
232 struct super_block *sb = inode->i_sb;
233 struct msdos_sb_info *sbi = MSDOS_SB(sb);
234 struct buffer_head *bh = NULL;
235 struct msdos_dir_entry *de;
236 struct nls_table *nls_io = sbi->nls_io;
237 struct nls_table *nls_disk = sbi->nls_disk;
238 wchar_t bufuname[14];
239 unsigned char xlate_len, nr_slots;
240 wchar_t *unicode = NULL;
241 unsigned char work[8], bufname[260]; /* 256 + 4 */
242 int uni_xlate = sbi->options.unicode_xlate;
243 int utf8 = sbi->options.utf8;
244 int anycase = (sbi->options.name_check != 's');
245 unsigned short opt_shortname = sbi->options.shortname;
246 loff_t cpos = 0;
247 int chl, i, j, last_u, err;
249 err = -ENOENT;
250 while(1) {
251 if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
252 goto EODir;
253 parse_record:
254 nr_slots = 0;
255 if (de->name[0] == DELETED_FLAG)
256 continue;
257 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
258 continue;
259 if (de->attr != ATTR_EXT && IS_FREE(de->name))
260 continue;
261 if (de->attr == ATTR_EXT) {
262 struct msdos_dir_slot *ds;
263 unsigned char id;
264 unsigned char slot;
265 unsigned char slots;
266 unsigned char alias_checksum;
268 if (!unicode) {
269 unicode = (wchar_t *)
270 __get_free_page(GFP_KERNEL);
271 if (!unicode) {
272 brelse(bh);
273 return -ENOMEM;
276 parse_long:
277 slots = 0;
278 ds = (struct msdos_dir_slot *) de;
279 id = ds->id;
280 if (!(id & 0x40))
281 continue;
282 slots = id & ~0x40;
283 if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */
284 continue;
285 nr_slots = slots;
286 alias_checksum = ds->alias_checksum;
288 slot = slots;
289 while (1) {
290 int offset;
292 slot--;
293 offset = slot * 13;
294 fat16_towchar(unicode + offset, ds->name0_4, 5);
295 fat16_towchar(unicode + offset + 5, ds->name5_10, 6);
296 fat16_towchar(unicode + offset + 11, ds->name11_12, 2);
298 if (ds->id & 0x40) {
299 unicode[offset + 13] = 0;
301 if (fat_get_entry(inode, &cpos, &bh, &de) < 0)
302 goto EODir;
303 if (slot == 0)
304 break;
305 ds = (struct msdos_dir_slot *) de;
306 if (ds->attr != ATTR_EXT)
307 goto parse_record;
308 if ((ds->id & ~0x40) != slot)
309 goto parse_long;
310 if (ds->alias_checksum != alias_checksum)
311 goto parse_long;
313 if (de->name[0] == DELETED_FLAG)
314 continue;
315 if (de->attr == ATTR_EXT)
316 goto parse_long;
317 if (IS_FREE(de->name) || (de->attr & ATTR_VOLUME))
318 continue;
319 if (fat_checksum(de->name) != alias_checksum)
320 nr_slots = 0;
323 memcpy(work, de->name, sizeof(de->name));
324 /* see namei.c, msdos_format_name */
325 if (work[0] == 0x05)
326 work[0] = 0xE5;
327 for (i = 0, j = 0, last_u = 0; i < 8;) {
328 if (!work[i]) break;
329 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
330 &bufuname[j++], opt_shortname,
331 de->lcase & CASE_LOWER_BASE);
332 if (chl <= 1) {
333 if (work[i] != ' ')
334 last_u = j;
335 } else {
336 last_u = j;
338 i += chl;
340 j = last_u;
341 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
342 for (i = 0; i < 3;) {
343 if (!de->ext[i]) break;
344 chl = fat_shortname2uni(nls_disk, &de->ext[i], 3 - i,
345 &bufuname[j++], opt_shortname,
346 de->lcase & CASE_LOWER_EXT);
347 if (chl <= 1) {
348 if (de->ext[i] != ' ')
349 last_u = j;
350 } else {
351 last_u = j;
353 i += chl;
355 if (!last_u)
356 continue;
358 bufuname[last_u] = 0x0000;
359 xlate_len = utf8
360 ?utf8_wcstombs(bufname, bufuname, sizeof(bufname))
361 :uni16_to_x8(bufname, bufuname, uni_xlate, nls_io);
362 if (xlate_len == name_len)
363 if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
364 (anycase && !nls_strnicmp(nls_io, name, bufname,
365 xlate_len)))
366 goto Found;
368 if (nr_slots) {
369 xlate_len = utf8
370 ?utf8_wcstombs(bufname, unicode, sizeof(bufname))
371 :uni16_to_x8(bufname, unicode, uni_xlate, nls_io);
372 if (xlate_len != name_len)
373 continue;
374 if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
375 (anycase && !nls_strnicmp(nls_io, name, bufname,
376 xlate_len)))
377 goto Found;
381 Found:
382 nr_slots++; /* include the de */
383 sinfo->slot_off = cpos - nr_slots * sizeof(*de);
384 sinfo->nr_slots = nr_slots;
385 sinfo->de = de;
386 sinfo->bh = bh;
387 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
388 err = 0;
389 EODir:
390 if (unicode)
391 free_page((unsigned long)unicode);
393 return err;
396 EXPORT_SYMBOL(fat_search_long);
398 struct fat_ioctl_filldir_callback {
399 struct dirent __user *dirent;
400 int result;
401 /* for dir ioctl */
402 const char *longname;
403 int long_len;
404 const char *shortname;
405 int short_len;
408 static int fat_readdirx(struct inode *inode, struct file *filp, void *dirent,
409 filldir_t filldir, int short_only, int both)
411 struct super_block *sb = inode->i_sb;
412 struct msdos_sb_info *sbi = MSDOS_SB(sb);
413 struct buffer_head *bh;
414 struct msdos_dir_entry *de;
415 struct nls_table *nls_io = sbi->nls_io;
416 struct nls_table *nls_disk = sbi->nls_disk;
417 unsigned char long_slots;
418 const char *fill_name;
419 int fill_len;
420 wchar_t bufuname[14];
421 wchar_t *unicode = NULL;
422 unsigned char c, work[8], bufname[56], *ptname = bufname;
423 unsigned long lpos, dummy, *furrfu = &lpos;
424 int uni_xlate = sbi->options.unicode_xlate;
425 int isvfat = sbi->options.isvfat;
426 int utf8 = sbi->options.utf8;
427 int nocase = sbi->options.nocase;
428 unsigned short opt_shortname = sbi->options.shortname;
429 unsigned long inum;
430 int chi, chl, i, i2, j, last, last_u, dotoffset = 0;
431 loff_t cpos;
432 int ret = 0;
434 lock_kernel();
436 cpos = filp->f_pos;
437 /* Fake . and .. for the root directory. */
438 if (inode->i_ino == MSDOS_ROOT_INO) {
439 while (cpos < 2) {
440 if (filldir(dirent, "..", cpos+1, cpos, MSDOS_ROOT_INO, DT_DIR) < 0)
441 goto out;
442 cpos++;
443 filp->f_pos++;
445 if (cpos == 2) {
446 dummy = 2;
447 furrfu = &dummy;
448 cpos = 0;
451 if (cpos & (sizeof(struct msdos_dir_entry)-1)) {
452 ret = -ENOENT;
453 goto out;
456 bh = NULL;
457 GetNew:
458 long_slots = 0;
459 if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
460 goto EODir;
461 /* Check for long filename entry */
462 if (isvfat) {
463 if (de->name[0] == DELETED_FLAG)
464 goto RecEnd;
465 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
466 goto RecEnd;
467 if (de->attr != ATTR_EXT && IS_FREE(de->name))
468 goto RecEnd;
469 } else {
470 if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
471 goto RecEnd;
474 if (isvfat && de->attr == ATTR_EXT) {
475 struct msdos_dir_slot *ds;
476 unsigned char id;
477 unsigned char slot;
478 unsigned char slots;
479 unsigned char alias_checksum;
481 if (!unicode) {
482 unicode = (wchar_t *)__get_free_page(GFP_KERNEL);
483 if (!unicode) {
484 filp->f_pos = cpos;
485 brelse(bh);
486 ret = -ENOMEM;
487 goto out;
490 ParseLong:
491 slots = 0;
492 ds = (struct msdos_dir_slot *) de;
493 id = ds->id;
494 if (!(id & 0x40))
495 goto RecEnd;
496 slots = id & ~0x40;
497 if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */
498 goto RecEnd;
499 long_slots = slots;
500 alias_checksum = ds->alias_checksum;
502 slot = slots;
503 while (1) {
504 int offset;
506 slot--;
507 offset = slot * 13;
508 fat16_towchar(unicode + offset, ds->name0_4, 5);
509 fat16_towchar(unicode + offset + 5, ds->name5_10, 6);
510 fat16_towchar(unicode + offset + 11, ds->name11_12, 2);
512 if (ds->id & 0x40) {
513 unicode[offset + 13] = 0;
515 if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
516 goto EODir;
517 if (slot == 0)
518 break;
519 ds = (struct msdos_dir_slot *) de;
520 if (ds->attr != ATTR_EXT)
521 goto RecEnd; /* XXX */
522 if ((ds->id & ~0x40) != slot)
523 goto ParseLong;
524 if (ds->alias_checksum != alias_checksum)
525 goto ParseLong;
527 if (de->name[0] == DELETED_FLAG)
528 goto RecEnd;
529 if (de->attr == ATTR_EXT)
530 goto ParseLong;
531 if (IS_FREE(de->name) || (de->attr & ATTR_VOLUME))
532 goto RecEnd;
533 if (fat_checksum(de->name) != alias_checksum)
534 long_slots = 0;
537 if (sbi->options.dotsOK) {
538 ptname = bufname;
539 dotoffset = 0;
540 if (de->attr & ATTR_HIDDEN) {
541 *ptname++ = '.';
542 dotoffset = 1;
546 memcpy(work, de->name, sizeof(de->name));
547 /* see namei.c, msdos_format_name */
548 if (work[0] == 0x05)
549 work[0] = 0xE5;
550 for (i = 0, j = 0, last = 0, last_u = 0; i < 8;) {
551 if (!(c = work[i])) 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 = 0; i2 < 3;) {
574 if (!(c = de->ext[i2])) break;
575 chl = fat_shortname2uni(nls_disk, &de->ext[i2], 3 - i2,
576 &bufuname[j++], opt_shortname,
577 de->lcase & CASE_LOWER_EXT);
578 if (chl <= 1) {
579 i2++;
580 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
581 if (c != ' ') {
582 last = i;
583 last_u = j;
585 } else {
586 last_u = j;
587 for (chi = 0; chi < chl && i2 < 3; chi++) {
588 ptname[i++] = de->ext[i2++];
589 last = i;
593 if (!last)
594 goto RecEnd;
596 i = last + dotoffset;
597 j = last_u;
599 lpos = cpos - (long_slots+1)*sizeof(struct msdos_dir_entry);
600 if (!memcmp(de->name, MSDOS_DOT, MSDOS_NAME))
601 inum = inode->i_ino;
602 else if (!memcmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
603 inum = parent_ino(filp->f_dentry);
604 } else {
605 loff_t i_pos = fat_make_i_pos(sb, bh, de);
606 struct inode *tmp = fat_iget(sb, i_pos);
607 if (tmp) {
608 inum = tmp->i_ino;
609 iput(tmp);
610 } else
611 inum = iunique(sb, MSDOS_ROOT_INO);
614 if (isvfat) {
615 bufuname[j] = 0x0000;
616 i = utf8 ? utf8_wcstombs(bufname, bufuname, sizeof(bufname))
617 : uni16_to_x8(bufname, bufuname, uni_xlate, nls_io);
620 fill_name = bufname;
621 fill_len = i;
622 if (!short_only && long_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 = PAGE_SIZE - (261 * sizeof(unicode[0]));
627 int long_len = utf8
628 ? utf8_wcstombs(longname, unicode, buf_size)
629 : uni16_to_x8(longname, unicode, uni_xlate, nls_io);
631 if (!both) {
632 fill_name = longname;
633 fill_len = long_len;
634 } else {
635 /* hack for fat_ioctl_filldir() */
636 struct fat_ioctl_filldir_callback *p = dirent;
638 p->longname = longname;
639 p->long_len = long_len;
640 p->shortname = bufname;
641 p->short_len = i;
642 fill_name = NULL;
643 fill_len = 0;
646 if (filldir(dirent, fill_name, fill_len, *furrfu, inum,
647 (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
648 goto FillFailed;
650 RecEnd:
651 furrfu = &lpos;
652 filp->f_pos = cpos;
653 goto GetNew;
654 EODir:
655 filp->f_pos = cpos;
656 FillFailed:
657 brelse(bh);
658 if (unicode)
659 free_page((unsigned long)unicode);
660 out:
661 unlock_kernel();
662 return ret;
665 static int fat_readdir(struct file *filp, void *dirent, filldir_t filldir)
667 struct inode *inode = filp->f_dentry->d_inode;
668 return fat_readdirx(inode, filp, dirent, filldir, 0, 0);
671 static int fat_ioctl_filldir(void *__buf, const char *name, int name_len,
672 loff_t offset, ino_t ino, unsigned int d_type)
674 struct fat_ioctl_filldir_callback *buf = __buf;
675 struct dirent __user *d1 = buf->dirent;
676 struct dirent __user *d2 = d1 + 1;
678 if (buf->result)
679 return -EINVAL;
680 buf->result++;
682 if (name != NULL) {
683 /* dirent has only short name */
684 if (name_len >= sizeof(d1->d_name))
685 name_len = sizeof(d1->d_name) - 1;
687 if (put_user(0, d2->d_name) ||
688 put_user(0, &d2->d_reclen) ||
689 copy_to_user(d1->d_name, name, name_len) ||
690 put_user(0, d1->d_name + name_len) ||
691 put_user(name_len, &d1->d_reclen))
692 goto efault;
693 } else {
694 /* dirent has short and long name */
695 const char *longname = buf->longname;
696 int long_len = buf->long_len;
697 const char *shortname = buf->shortname;
698 int short_len = buf->short_len;
700 if (long_len >= sizeof(d1->d_name))
701 long_len = sizeof(d1->d_name) - 1;
702 if (short_len >= sizeof(d1->d_name))
703 short_len = sizeof(d1->d_name) - 1;
705 if (copy_to_user(d2->d_name, longname, long_len) ||
706 put_user(0, d2->d_name + long_len) ||
707 put_user(long_len, &d2->d_reclen) ||
708 put_user(ino, &d2->d_ino) ||
709 put_user(offset, &d2->d_off) ||
710 copy_to_user(d1->d_name, shortname, short_len) ||
711 put_user(0, d1->d_name + short_len) ||
712 put_user(short_len, &d1->d_reclen))
713 goto efault;
715 return 0;
716 efault:
717 buf->result = -EFAULT;
718 return -EFAULT;
721 static int fat_dir_ioctl(struct inode * inode, struct file * filp,
722 unsigned int cmd, unsigned long arg)
724 struct fat_ioctl_filldir_callback buf;
725 struct dirent __user *d1;
726 int ret, short_only, both;
728 switch (cmd) {
729 case VFAT_IOCTL_READDIR_SHORT:
730 short_only = 1;
731 both = 0;
732 break;
733 case VFAT_IOCTL_READDIR_BOTH:
734 short_only = 0;
735 both = 1;
736 break;
737 default:
738 return fat_generic_ioctl(inode, filp, cmd, arg);
741 d1 = (struct dirent __user *)arg;
742 if (!access_ok(VERIFY_WRITE, d1, sizeof(struct dirent[2])))
743 return -EFAULT;
745 * Yes, we don't need this put_user() absolutely. However old
746 * code didn't return the right value. So, app use this value,
747 * in order to check whether it is EOF.
749 if (put_user(0, &d1->d_reclen))
750 return -EFAULT;
752 buf.dirent = d1;
753 buf.result = 0;
754 down(&inode->i_sem);
755 ret = -ENOENT;
756 if (!IS_DEADDIR(inode)) {
757 ret = fat_readdirx(inode, filp, &buf, fat_ioctl_filldir,
758 short_only, both);
760 up(&inode->i_sem);
761 if (ret >= 0)
762 ret = buf.result;
763 return ret;
766 struct file_operations fat_dir_operations = {
767 .read = generic_read_dir,
768 .readdir = fat_readdir,
769 .ioctl = fat_dir_ioctl,
770 .fsync = file_fsync,
773 static int fat_get_short_entry(struct inode *dir, loff_t *pos,
774 struct buffer_head **bh,
775 struct msdos_dir_entry **de)
777 while (fat_get_entry(dir, pos, bh, de) >= 0) {
778 /* free entry or long name entry or volume label */
779 if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME))
780 return 0;
782 return -ENOENT;
786 * The ".." entry can not provide the "struct fat_slot_info" informations
787 * for inode. So, this function provide the some informations only.
789 int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh,
790 struct msdos_dir_entry **de, loff_t *i_pos)
792 loff_t offset;
794 offset = 0;
795 *bh = NULL;
796 while (fat_get_short_entry(dir, &offset, bh, de) >= 0) {
797 if (!strncmp((*de)->name, MSDOS_DOTDOT, MSDOS_NAME)) {
798 *i_pos = fat_make_i_pos(dir->i_sb, *bh, *de);
799 return 0;
802 return -ENOENT;
805 EXPORT_SYMBOL(fat_get_dotdot_entry);
807 /* See if directory is empty */
808 int fat_dir_empty(struct inode *dir)
810 struct buffer_head *bh;
811 struct msdos_dir_entry *de;
812 loff_t cpos;
813 int result = 0;
815 bh = NULL;
816 cpos = 0;
817 while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
818 if (strncmp(de->name, MSDOS_DOT , MSDOS_NAME) &&
819 strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
820 result = -ENOTEMPTY;
821 break;
824 brelse(bh);
825 return result;
828 EXPORT_SYMBOL(fat_dir_empty);
831 * fat_subdirs counts the number of sub-directories of dir. It can be run
832 * on directories being created.
834 int fat_subdirs(struct inode *dir)
836 struct buffer_head *bh;
837 struct msdos_dir_entry *de;
838 loff_t cpos;
839 int count = 0;
841 bh = NULL;
842 cpos = 0;
843 while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
844 if (de->attr & ATTR_DIR)
845 count++;
847 brelse(bh);
848 return count;
852 * Scans a directory for a given file (name points to its formatted name).
853 * Returns an error code or zero.
855 int fat_scan(struct inode *dir, const unsigned char *name,
856 struct fat_slot_info *sinfo)
858 struct super_block *sb = dir->i_sb;
860 sinfo->slot_off = 0;
861 sinfo->bh = NULL;
862 while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh,
863 &sinfo->de) >= 0) {
864 if (!strncmp(sinfo->de->name, name, MSDOS_NAME)) {
865 sinfo->slot_off -= sizeof(*sinfo->de);
866 sinfo->nr_slots = 1;
867 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
868 return 0;
871 return -ENOENT;
874 EXPORT_SYMBOL(fat_scan);
876 static int __fat_remove_entries(struct inode *dir, loff_t pos, int nr_slots)
878 struct super_block *sb = dir->i_sb;
879 struct buffer_head *bh;
880 struct msdos_dir_entry *de, *endp;
881 int err = 0, orig_slots;
883 while (nr_slots) {
884 bh = NULL;
885 if (fat_get_entry(dir, &pos, &bh, &de) < 0) {
886 err = -EIO;
887 break;
890 orig_slots = nr_slots;
891 endp = (struct msdos_dir_entry *)(bh->b_data + sb->s_blocksize);
892 while (nr_slots && de < endp) {
893 de->name[0] = DELETED_FLAG;
894 de++;
895 nr_slots--;
897 mark_buffer_dirty(bh);
898 if (IS_DIRSYNC(dir))
899 err = sync_dirty_buffer(bh);
900 brelse(bh);
901 if (err)
902 break;
904 /* pos is *next* de's position, so this does `- sizeof(de)' */
905 pos += ((orig_slots - nr_slots) * sizeof(*de)) - sizeof(*de);
908 return err;
911 int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo)
913 struct msdos_dir_entry *de;
914 struct buffer_head *bh;
915 int err = 0, nr_slots;
918 * First stage: Remove the shortname. By this, the directory
919 * entry is removed.
921 nr_slots = sinfo->nr_slots;
922 de = sinfo->de;
923 sinfo->de = NULL;
924 bh = sinfo->bh;
925 sinfo->bh = NULL;
926 while (nr_slots && de >= (struct msdos_dir_entry *)bh->b_data) {
927 de->name[0] = DELETED_FLAG;
928 de--;
929 nr_slots--;
931 mark_buffer_dirty(bh);
932 if (IS_DIRSYNC(dir))
933 err = sync_dirty_buffer(bh);
934 brelse(bh);
935 if (err)
936 return err;
937 dir->i_version++;
939 if (nr_slots) {
941 * Second stage: remove the remaining longname slots.
942 * (This directory entry is already removed, and so return
943 * the success)
945 err = __fat_remove_entries(dir, sinfo->slot_off, nr_slots);
946 if (err) {
947 printk(KERN_WARNING
948 "FAT: Couldn't remove the long name slots\n");
952 dir->i_mtime = dir->i_atime = CURRENT_TIME_SEC;
953 if (IS_DIRSYNC(dir))
954 (void)fat_sync_inode(dir);
955 else
956 mark_inode_dirty(dir);
958 return 0;
961 EXPORT_SYMBOL(fat_remove_entries);
963 static int fat_zeroed_cluster(struct inode *dir, sector_t blknr, int nr_used,
964 struct buffer_head **bhs, int nr_bhs)
966 struct super_block *sb = dir->i_sb;
967 sector_t last_blknr = blknr + MSDOS_SB(sb)->sec_per_clus;
968 int err, i, n;
970 /* Zeroing the unused blocks on this cluster */
971 blknr += nr_used;
972 n = nr_used;
973 while (blknr < last_blknr) {
974 bhs[n] = sb_getblk(sb, blknr);
975 if (!bhs[n]) {
976 err = -ENOMEM;
977 goto error;
979 memset(bhs[n]->b_data, 0, sb->s_blocksize);
980 set_buffer_uptodate(bhs[n]);
981 mark_buffer_dirty(bhs[n]);
983 n++;
984 blknr++;
985 if (n == nr_bhs) {
986 if (IS_DIRSYNC(dir)) {
987 err = fat_sync_bhs(bhs, n);
988 if (err)
989 goto error;
991 for (i = 0; i < n; i++)
992 brelse(bhs[i]);
993 n = 0;
996 if (IS_DIRSYNC(dir)) {
997 err = fat_sync_bhs(bhs, n);
998 if (err)
999 goto error;
1001 for (i = 0; i < n; i++)
1002 brelse(bhs[i]);
1004 return 0;
1006 error:
1007 for (i = 0; i < n; i++)
1008 bforget(bhs[i]);
1009 return err;
1012 int fat_alloc_new_dir(struct inode *dir, struct timespec *ts)
1014 struct super_block *sb = dir->i_sb;
1015 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1016 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
1017 struct msdos_dir_entry *de;
1018 sector_t blknr;
1019 __le16 date, time;
1020 int err, cluster;
1022 err = fat_alloc_clusters(dir, &cluster, 1);
1023 if (err)
1024 goto error;
1026 blknr = fat_clus_to_blknr(sbi, cluster);
1027 bhs[0] = sb_getblk(sb, blknr);
1028 if (!bhs[0]) {
1029 err = -ENOMEM;
1030 goto error_free;
1033 fat_date_unix2dos(ts->tv_sec, &time, &date);
1035 de = (struct msdos_dir_entry *)bhs[0]->b_data;
1036 /* filling the new directory slots ("." and ".." entries) */
1037 memcpy(de[0].name, MSDOS_DOT, MSDOS_NAME);
1038 memcpy(de[1].name, MSDOS_DOTDOT, MSDOS_NAME);
1039 de->attr = de[1].attr = ATTR_DIR;
1040 de[0].lcase = de[1].lcase = 0;
1041 de[0].time = de[1].time = time;
1042 de[0].date = de[1].date = date;
1043 de[0].ctime_cs = de[1].ctime_cs = 0;
1044 if (sbi->options.isvfat) {
1045 /* extra timestamps */
1046 de[0].ctime = de[1].ctime = time;
1047 de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = date;
1048 } else {
1049 de[0].ctime = de[1].ctime = 0;
1050 de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = 0;
1052 de[0].start = cpu_to_le16(cluster);
1053 de[0].starthi = cpu_to_le16(cluster >> 16);
1054 de[1].start = cpu_to_le16(MSDOS_I(dir)->i_logstart);
1055 de[1].starthi = cpu_to_le16(MSDOS_I(dir)->i_logstart >> 16);
1056 de[0].size = de[1].size = 0;
1057 memset(de + 2, 0, sb->s_blocksize - 2 * sizeof(*de));
1058 set_buffer_uptodate(bhs[0]);
1059 mark_buffer_dirty(bhs[0]);
1061 err = fat_zeroed_cluster(dir, blknr, 1, bhs, MAX_BUF_PER_PAGE);
1062 if (err)
1063 goto error_free;
1065 return cluster;
1067 error_free:
1068 fat_free_clusters(dir, cluster);
1069 error:
1070 return err;
1073 EXPORT_SYMBOL(fat_alloc_new_dir);
1075 static int fat_add_new_entries(struct inode *dir, void *slots, int nr_slots,
1076 int *nr_cluster, struct msdos_dir_entry **de,
1077 struct buffer_head **bh, loff_t *i_pos)
1079 struct super_block *sb = dir->i_sb;
1080 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1081 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
1082 sector_t blknr, start_blknr, last_blknr;
1083 unsigned long size, copy;
1084 int err, i, n, offset, cluster[2];
1087 * The minimum cluster size is 512bytes, and maximum entry
1088 * size is 32*slots (672bytes). So, iff the cluster size is
1089 * 512bytes, we may need two clusters.
1091 size = nr_slots * sizeof(struct msdos_dir_entry);
1092 *nr_cluster = (size + (sbi->cluster_size - 1)) >> sbi->cluster_bits;
1093 BUG_ON(*nr_cluster > 2);
1095 err = fat_alloc_clusters(dir, cluster, *nr_cluster);
1096 if (err)
1097 goto error;
1100 * First stage: Fill the directory entry. NOTE: This cluster
1101 * is not referenced from any inode yet, so updates order is
1102 * not important.
1104 i = n = copy = 0;
1105 do {
1106 start_blknr = blknr = fat_clus_to_blknr(sbi, cluster[i]);
1107 last_blknr = start_blknr + sbi->sec_per_clus;
1108 while (blknr < last_blknr) {
1109 bhs[n] = sb_getblk(sb, blknr);
1110 if (!bhs[n]) {
1111 err = -ENOMEM;
1112 goto error_nomem;
1115 /* fill the directory entry */
1116 copy = min(size, sb->s_blocksize);
1117 memcpy(bhs[n]->b_data, slots, copy);
1118 slots += copy;
1119 size -= copy;
1120 set_buffer_uptodate(bhs[n]);
1121 mark_buffer_dirty(bhs[n]);
1122 if (!size)
1123 break;
1124 n++;
1125 blknr++;
1127 } while (++i < *nr_cluster);
1129 memset(bhs[n]->b_data + copy, 0, sb->s_blocksize - copy);
1130 offset = copy - sizeof(struct msdos_dir_entry);
1131 get_bh(bhs[n]);
1132 *bh = bhs[n];
1133 *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
1134 *i_pos = fat_make_i_pos(sb, *bh, *de);
1136 /* Second stage: clear the rest of cluster, and write outs */
1137 err = fat_zeroed_cluster(dir, start_blknr, ++n, bhs, MAX_BUF_PER_PAGE);
1138 if (err)
1139 goto error_free;
1141 return cluster[0];
1143 error_free:
1144 brelse(*bh);
1145 *bh = NULL;
1146 n = 0;
1147 error_nomem:
1148 for (i = 0; i < n; i++)
1149 bforget(bhs[i]);
1150 fat_free_clusters(dir, cluster[0]);
1151 error:
1152 return err;
1155 int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
1156 struct fat_slot_info *sinfo)
1158 struct super_block *sb = dir->i_sb;
1159 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1160 struct buffer_head *bh, *prev, *bhs[3]; /* 32*slots (672bytes) */
1161 struct msdos_dir_entry *de;
1162 int err, free_slots, i, nr_bhs;
1163 loff_t pos, i_pos;
1165 sinfo->nr_slots = nr_slots;
1167 /* First stage: search free direcotry entries */
1168 free_slots = nr_bhs = 0;
1169 bh = prev = NULL;
1170 pos = 0;
1171 err = -ENOSPC;
1172 while (fat_get_entry(dir, &pos, &bh, &de) > -1) {
1173 /* check the maximum size of directory */
1174 if (pos >= FAT_MAX_DIR_SIZE)
1175 goto error;
1177 if (IS_FREE(de->name)) {
1178 if (prev != bh) {
1179 get_bh(bh);
1180 bhs[nr_bhs] = prev = bh;
1181 nr_bhs++;
1183 free_slots++;
1184 if (free_slots == nr_slots)
1185 goto found;
1186 } else {
1187 for (i = 0; i < nr_bhs; i++)
1188 brelse(bhs[i]);
1189 prev = NULL;
1190 free_slots = nr_bhs = 0;
1193 if (dir->i_ino == MSDOS_ROOT_INO) {
1194 if (sbi->fat_bits != 32)
1195 goto error;
1196 } else if (MSDOS_I(dir)->i_start == 0) {
1197 printk(KERN_ERR "FAT: Corrupted directory (i_pos %lld)\n",
1198 MSDOS_I(dir)->i_pos);
1199 err = -EIO;
1200 goto error;
1203 found:
1204 err = 0;
1205 pos -= free_slots * sizeof(*de);
1206 nr_slots -= free_slots;
1207 if (free_slots) {
1209 * Second stage: filling the free entries with new entries.
1210 * NOTE: If this slots has shortname, first, we write
1211 * the long name slots, then write the short name.
1213 int size = free_slots * sizeof(*de);
1214 int offset = pos & (sb->s_blocksize - 1);
1215 int long_bhs = nr_bhs - (nr_slots == 0);
1217 /* Fill the long name slots. */
1218 for (i = 0; i < long_bhs; i++) {
1219 int copy = min_t(int, sb->s_blocksize - offset, size);
1220 memcpy(bhs[i]->b_data + offset, slots, copy);
1221 mark_buffer_dirty(bhs[i]);
1222 offset = 0;
1223 slots += copy;
1224 size -= copy;
1226 if (long_bhs && IS_DIRSYNC(dir))
1227 err = fat_sync_bhs(bhs, long_bhs);
1228 if (!err && i < nr_bhs) {
1229 /* Fill the short name slot. */
1230 int copy = min_t(int, sb->s_blocksize - offset, size);
1231 memcpy(bhs[i]->b_data + offset, slots, copy);
1232 mark_buffer_dirty(bhs[i]);
1233 if (IS_DIRSYNC(dir))
1234 err = sync_dirty_buffer(bhs[i]);
1236 for (i = 0; i < nr_bhs; i++)
1237 brelse(bhs[i]);
1238 if (err)
1239 goto error_remove;
1242 if (nr_slots) {
1243 int cluster, nr_cluster;
1246 * Third stage: allocate the cluster for new entries.
1247 * And initialize the cluster with new entries, then
1248 * add the cluster to dir.
1250 cluster = fat_add_new_entries(dir, slots, nr_slots, &nr_cluster,
1251 &de, &bh, &i_pos);
1252 if (cluster < 0) {
1253 err = cluster;
1254 goto error_remove;
1256 err = fat_chain_add(dir, cluster, nr_cluster);
1257 if (err) {
1258 fat_free_clusters(dir, cluster);
1259 goto error_remove;
1261 if (dir->i_size & (sbi->cluster_size - 1)) {
1262 fat_fs_panic(sb, "Odd directory size");
1263 dir->i_size = (dir->i_size + sbi->cluster_size - 1)
1264 & ~((loff_t)sbi->cluster_size - 1);
1266 dir->i_size += nr_cluster << sbi->cluster_bits;
1267 MSDOS_I(dir)->mmu_private += nr_cluster << sbi->cluster_bits;
1269 sinfo->slot_off = pos;
1270 sinfo->de = de;
1271 sinfo->bh = bh;
1272 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
1274 return 0;
1276 error:
1277 brelse(bh);
1278 for (i = 0; i < nr_bhs; i++)
1279 brelse(bhs[i]);
1280 return err;
1282 error_remove:
1283 brelse(bh);
1284 if (free_slots)
1285 __fat_remove_entries(dir, pos, free_slots);
1286 return err;
1289 EXPORT_SYMBOL(fat_add_entries);