This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / fs / fat / dir.c
1 /*
2  *  linux/fs/fat/dir.c
3  *
4  *  directory handling functions for fat-based filesystems
5  *
6  *  Written 1992,1993 by Werner Almesberger
7  *
8  *  Hidden files 1995 by Albert Cahalan <albert@ccs.neu.edu> <adc@coe.neu.edu>
9  *
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>
14  */
15
16 #include <linux/slab.h>
17 #include <linux/time.h>
18 #include <linux/msdos_fs.h>
19 #include <linux/dirent.h>
20 #include <linux/smp_lock.h>
21 #include <linux/buffer_head.h>
22
23 #include <asm/uaccess.h>
24
25 static int fat_dir_ioctl(struct inode * inode, struct file * filp,
26                   unsigned int cmd, unsigned long arg);
27 static int fat_readdir(struct file *filp, void *dirent, filldir_t filldir);
28
29 struct file_operations fat_dir_operations = {
30         .read           = generic_read_dir,
31         .readdir        = fat_readdir,
32         .ioctl          = fat_dir_ioctl,
33         .fsync          = file_fsync,
34 };
35
36 /*
37  * Convert Unicode 16 to UTF8, translated Unicode, or ASCII.
38  * If uni_xlate is enabled and we can't get a 1:1 conversion, use a
39  * colon as an escape character since it is normally invalid on the vfat
40  * filesystem. The following four characters are the hexadecimal digits
41  * of Unicode value. This lets us do a full dump and restore of Unicode
42  * filenames. We could get into some trouble with long Unicode names,
43  * but ignore that right now.
44  * Ahem... Stack smashing in ring 0 isn't fun. Fixed.
45  */
46 static int
47 uni16_to_x8(unsigned char *ascii, wchar_t *uni, int uni_xlate,
48             struct nls_table *nls)
49 {
50         wchar_t *ip, ec;
51         unsigned char *op, nc;
52         int charlen;
53         int k;
54
55         ip = uni;
56         op = ascii;
57
58         while (*ip) {
59                 ec = *ip++;
60                 if ( (charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE)) > 0) {
61                         op += charlen;
62                 } else {
63                         if (uni_xlate == 1) {
64                                 *op = ':';
65                                 for (k = 4; k > 0; k--) {
66                                         nc = ec & 0xF;
67                                         op[k] = nc > 9  ? nc + ('a' - 10)
68                                                         : nc + '0';
69                                         ec >>= 4;
70                                 }
71                                 op += 5;
72                         } else {
73                                 *op++ = '?';
74                         }
75                 }
76                 /* We have some slack there, so it's OK */
77                 if (op>ascii+256) {
78                         op = ascii + 256;
79                         break;
80                 }
81         }
82         *op = 0;
83         return (op - ascii);
84 }
85
86 #if 0
87 static void dump_de(struct msdos_dir_entry *de)
88 {
89         int i;
90         unsigned char *p = (unsigned char *) de;
91         printk("[");
92
93         for (i = 0; i < 32; i++, p++) {
94                 printk("%02x ", *p);
95         }
96         printk("]\n");
97 }
98 #endif
99
100 static inline int
101 fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
102 {
103         int charlen;
104
105         charlen = t->char2uni(c, clen, uni);
106         if (charlen < 0) {
107                 *uni = 0x003f;  /* a question mark */
108                 charlen = 1;
109         }
110         return charlen;
111 }
112
113 static inline int
114 fat_short2lower_uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
115 {
116         int charlen;
117         wchar_t wc;
118
119         charlen = t->char2uni(c, clen, &wc);
120         if (charlen < 0) {
121                 *uni = 0x003f;  /* a question mark */
122                 charlen = 1;
123         } else if (charlen <= 1) {
124                 unsigned char nc = t->charset2lower[*c];
125                 
126                 if (!nc)
127                         nc = *c;
128                 
129                 if ( (charlen = t->char2uni(&nc, 1, uni)) < 0) {
130                         *uni = 0x003f;  /* a question mark */
131                         charlen = 1;
132                 }
133         } else
134                 *uni = wc;
135         
136         return charlen;
137 }
138
139 static inline int
140 fat_shortname2uni(struct nls_table *nls, unsigned char *buf, int buf_size,
141                   wchar_t *uni_buf, unsigned short opt, int lower)
142 {
143         int len = 0;
144
145         if (opt & VFAT_SFN_DISPLAY_LOWER)
146                 len =  fat_short2lower_uni(nls, buf, buf_size, uni_buf);
147         else if (opt & VFAT_SFN_DISPLAY_WIN95)
148                 len = fat_short2uni(nls, buf, buf_size, uni_buf);
149         else if (opt & VFAT_SFN_DISPLAY_WINNT) {
150                 if (lower)
151                         len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
152                 else 
153                         len = fat_short2uni(nls, buf, buf_size, uni_buf);
154         } else
155                 len = fat_short2uni(nls, buf, buf_size, uni_buf);
156
157         return len;
158 }
159
160 /*
161  * Return values: negative -> error, 0 -> not found, positive -> found,
162  * value is the total amount of slots, including the shortname entry.
163  */
164 int fat_search_long(struct inode *inode, const unsigned char *name,
165                     int name_len, int anycase, loff_t *spos, loff_t *lpos)
166 {
167         struct super_block *sb = inode->i_sb;
168         struct buffer_head *bh = NULL;
169         struct msdos_dir_entry *de;
170         struct nls_table *nls_io = MSDOS_SB(sb)->nls_io;
171         struct nls_table *nls_disk = MSDOS_SB(sb)->nls_disk;
172         wchar_t bufuname[14];
173         unsigned char xlate_len, long_slots;
174         wchar_t *unicode = NULL;
175         unsigned char work[8], bufname[260];    /* 256 + 4 */
176         int uni_xlate = MSDOS_SB(sb)->options.unicode_xlate;
177         int utf8 = MSDOS_SB(sb)->options.utf8;
178         unsigned short opt_shortname = MSDOS_SB(sb)->options.shortname;
179         int chl, i, j, last_u, res = 0;
180         loff_t i_pos, cpos = 0;
181
182         while(1) {
183                 if (fat_get_entry(inode,&cpos,&bh,&de,&i_pos) == -1)
184                         goto EODir;
185 parse_record:
186                 long_slots = 0;
187                 if (de->name[0] == DELETED_FLAG)
188                         continue;
189                 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
190                         continue;
191                 if (de->attr != ATTR_EXT && IS_FREE(de->name))
192                         continue;
193                 if (de->attr == ATTR_EXT) {
194                         struct msdos_dir_slot *ds;
195                         unsigned char id;
196                         unsigned char slot;
197                         unsigned char slots;
198                         unsigned char sum;
199                         unsigned char alias_checksum;
200
201                         if (!unicode) {
202                                 unicode = (wchar_t *)
203                                         __get_free_page(GFP_KERNEL);
204                                 if (!unicode) {
205                                         brelse(bh);
206                                         return -ENOMEM;
207                                 }
208                         }
209 parse_long:
210                         slots = 0;
211                         ds = (struct msdos_dir_slot *) de;
212                         id = ds->id;
213                         if (!(id & 0x40))
214                                 continue;
215                         slots = id & ~0x40;
216                         if (slots > 20 || !slots)       /* ceil(256 * 2 / 26) */
217                                 continue;
218                         long_slots = slots;
219                         alias_checksum = ds->alias_checksum;
220
221                         slot = slots;
222                         while (1) {
223                                 int offset;
224
225                                 slot--;
226                                 offset = slot * 13;
227                                 fat16_towchar(unicode + offset, ds->name0_4, 5);
228                                 fat16_towchar(unicode + offset + 5, ds->name5_10, 6);
229                                 fat16_towchar(unicode + offset + 11, ds->name11_12, 2);
230
231                                 if (ds->id & 0x40) {
232                                         unicode[offset + 13] = 0;
233                                 }
234                                 if (fat_get_entry(inode,&cpos,&bh,&de,&i_pos)<0)
235                                         goto EODir;
236                                 if (slot == 0)
237                                         break;
238                                 ds = (struct msdos_dir_slot *) de;
239                                 if (ds->attr !=  ATTR_EXT)
240                                         goto parse_record;
241                                 if ((ds->id & ~0x40) != slot)
242                                         goto parse_long;
243                                 if (ds->alias_checksum != alias_checksum)
244                                         goto parse_long;
245                         }
246                         if (de->name[0] == DELETED_FLAG)
247                                 continue;
248                         if (de->attr ==  ATTR_EXT)
249                                 goto parse_long;
250                         if (IS_FREE(de->name) || (de->attr & ATTR_VOLUME))
251                                 continue;
252                         for (sum = 0, i = 0; i < 11; i++)
253                                 sum = (((sum&1)<<7)|((sum&0xfe)>>1)) + de->name[i];
254                         if (sum != alias_checksum)
255                                 long_slots = 0;
256                 }
257
258                 memcpy(work, de->name, sizeof(de->name));
259                 /* see namei.c, msdos_format_name */
260                 if (work[0] == 0x05)
261                         work[0] = 0xE5;
262                 for (i = 0, j = 0, last_u = 0; i < 8;) {
263                         if (!work[i]) break;
264                         chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
265                                                 &bufuname[j++], opt_shortname,
266                                                 de->lcase & CASE_LOWER_BASE);
267                         if (chl <= 1) {
268                                 if (work[i] != ' ')
269                                         last_u = j;
270                         } else {
271                                 last_u = j;
272                         }
273                         i += chl;
274                 }
275                 j = last_u;
276                 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
277                 for (i = 0; i < 3;) {
278                         if (!de->ext[i]) break;
279                         chl = fat_shortname2uni(nls_disk, &de->ext[i], 3 - i,
280                                                 &bufuname[j++], opt_shortname,
281                                                 de->lcase & CASE_LOWER_EXT);
282                         if (chl <= 1) {
283                                 if (de->ext[i] != ' ')
284                                         last_u = j;
285                         } else {
286                                 last_u = j;
287                         }
288                         i += chl;
289                 }
290                 if (!last_u)
291                         continue;
292
293                 bufuname[last_u] = 0x0000;
294                 xlate_len = utf8
295                         ?utf8_wcstombs(bufname, bufuname, sizeof(bufname))
296                         :uni16_to_x8(bufname, bufuname, uni_xlate, nls_io);
297                 if (xlate_len == name_len)
298                         if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
299                             (anycase && !nls_strnicmp(nls_io, name, bufname,
300                                                                 xlate_len)))
301                                 goto Found;
302
303                 if (long_slots) {
304                         xlate_len = utf8
305                                 ?utf8_wcstombs(bufname, unicode, sizeof(bufname))
306                                 :uni16_to_x8(bufname, unicode, uni_xlate, nls_io);
307                         if (xlate_len != name_len)
308                                 continue;
309                         if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
310                             (anycase && !nls_strnicmp(nls_io, name, bufname,
311                                                                 xlate_len)))
312                                 goto Found;
313                 }
314         }
315
316 Found:
317         res = long_slots + 1;
318         *spos = cpos - sizeof(struct msdos_dir_entry);
319         *lpos = cpos - res*sizeof(struct msdos_dir_entry);
320 EODir:
321         brelse(bh);
322         if (unicode) {
323                 free_page((unsigned long) unicode);
324         }
325         return res;
326 }
327
328 static int fat_readdirx(struct inode *inode, struct file *filp, void *dirent,
329                         filldir_t filldir, int shortnames, int both)
330 {
331         struct super_block *sb = inode->i_sb;
332         struct buffer_head *bh;
333         struct msdos_dir_entry *de;
334         struct nls_table *nls_io = MSDOS_SB(sb)->nls_io;
335         struct nls_table *nls_disk = MSDOS_SB(sb)->nls_disk;
336         wchar_t bufuname[14];
337         unsigned char long_slots;
338         wchar_t *unicode = NULL;
339         unsigned char c, work[8], bufname[56], *ptname = bufname;
340         unsigned long lpos, dummy, *furrfu = &lpos;
341         int uni_xlate = MSDOS_SB(sb)->options.unicode_xlate;
342         int isvfat = MSDOS_SB(sb)->options.isvfat;
343         int utf8 = MSDOS_SB(sb)->options.utf8;
344         int nocase = MSDOS_SB(sb)->options.nocase;
345         unsigned short opt_shortname = MSDOS_SB(sb)->options.shortname;
346         unsigned long inum;
347         int chi, chl, i, i2, j, last, last_u, dotoffset = 0;
348         loff_t i_pos, cpos;
349         int ret = 0;
350         
351         lock_kernel();
352
353         cpos = filp->f_pos;
354         /* Fake . and .. for the root directory. */
355         if (inode->i_ino == MSDOS_ROOT_INO) {
356                 while (cpos < 2) {
357                         if (filldir(dirent, "..", cpos+1, cpos, MSDOS_ROOT_INO, DT_DIR) < 0)
358                                 goto out;
359                         cpos++;
360                         filp->f_pos++;
361                 }
362                 if (cpos == 2) {
363                         dummy = 2;
364                         furrfu = &dummy;
365                         cpos = 0;
366                 }
367         }
368         if (cpos & (sizeof(struct msdos_dir_entry)-1)) {
369                 ret = -ENOENT;
370                 goto out;
371         }
372
373         bh = NULL;
374 GetNew:
375         long_slots = 0;
376         if (fat_get_entry(inode,&cpos,&bh,&de,&i_pos) == -1)
377                 goto EODir;
378         /* Check for long filename entry */
379         if (isvfat) {
380                 if (de->name[0] == DELETED_FLAG)
381                         goto RecEnd;
382                 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
383                         goto RecEnd;
384                 if (de->attr != ATTR_EXT && IS_FREE(de->name))
385                         goto RecEnd;
386         } else {
387                 if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
388                         goto RecEnd;
389         }
390
391         if (isvfat && de->attr == ATTR_EXT) {
392                 struct msdos_dir_slot *ds;
393                 unsigned char id;
394                 unsigned char slot;
395                 unsigned char slots;
396                 unsigned char sum;
397                 unsigned char alias_checksum;
398
399                 if (!unicode) {
400                         unicode = (wchar_t *)
401                                 __get_free_page(GFP_KERNEL);
402                         if (!unicode) {
403                                 filp->f_pos = cpos;
404                                 brelse(bh);
405                                 ret = -ENOMEM;
406                                 goto out;
407                         }
408                 }
409 ParseLong:
410                 slots = 0;
411                 ds = (struct msdos_dir_slot *) de;
412                 id = ds->id;
413                 if (!(id & 0x40))
414                         goto RecEnd;
415                 slots = id & ~0x40;
416                 if (slots > 20 || !slots)       /* ceil(256 * 2 / 26) */
417                         goto RecEnd;
418                 long_slots = slots;
419                 alias_checksum = ds->alias_checksum;
420
421                 slot = slots;
422                 while (1) {
423                         int offset;
424
425                         slot--;
426                         offset = slot * 13;
427                         fat16_towchar(unicode + offset, ds->name0_4, 5);
428                         fat16_towchar(unicode + offset + 5, ds->name5_10, 6);
429                         fat16_towchar(unicode + offset + 11, ds->name11_12, 2);
430
431                         if (ds->id & 0x40) {
432                                 unicode[offset + 13] = 0;
433                         }
434                         if (fat_get_entry(inode,&cpos,&bh,&de,&i_pos) == -1)
435                                 goto EODir;
436                         if (slot == 0)
437                                 break;
438                         ds = (struct msdos_dir_slot *) de;
439                         if (ds->attr !=  ATTR_EXT)
440                                 goto RecEnd;    /* XXX */
441                         if ((ds->id & ~0x40) != slot)
442                                 goto ParseLong;
443                         if (ds->alias_checksum != alias_checksum)
444                                 goto ParseLong;
445                 }
446                 if (de->name[0] == DELETED_FLAG)
447                         goto RecEnd;
448                 if (de->attr ==  ATTR_EXT)
449                         goto ParseLong;
450                 if (IS_FREE(de->name) || (de->attr & ATTR_VOLUME))
451                         goto RecEnd;
452                 for (sum = 0, i = 0; i < 11; i++)
453                         sum = (((sum&1)<<7)|((sum&0xfe)>>1)) + de->name[i];
454                 if (sum != alias_checksum)
455                         long_slots = 0;
456         }
457
458         if ((de->attr & ATTR_HIDDEN) && MSDOS_SB(sb)->options.dotsOK) {
459                 *ptname++ = '.';
460                 dotoffset = 1;
461         }
462
463         memcpy(work, de->name, sizeof(de->name));
464         /* see namei.c, msdos_format_name */
465         if (work[0] == 0x05)
466                 work[0] = 0xE5;
467         for (i = 0, j = 0, last = 0, last_u = 0; i < 8;) {
468                 if (!(c = work[i])) break;
469                 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
470                                         &bufuname[j++], opt_shortname,
471                                         de->lcase & CASE_LOWER_BASE);
472                 if (chl <= 1) {
473                         ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
474                         if (c != ' ') {
475                                 last = i;
476                                 last_u = j;
477                         }
478                 } else {
479                         last_u = j;
480                         for (chi = 0; chi < chl && i < 8; chi++) {
481                                 ptname[i] = work[i];
482                                 i++; last = i;
483                         }
484                 }
485         }
486         i = last;
487         j = last_u;
488         fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
489         ptname[i++] = '.';
490         for (i2 = 0; i2 < 3;) {
491                 if (!(c = de->ext[i2])) break;
492                 chl = fat_shortname2uni(nls_disk, &de->ext[i2], 3 - i2,
493                                         &bufuname[j++], opt_shortname,
494                                         de->lcase & CASE_LOWER_EXT);
495                 if (chl <= 1) {
496                         i2++;
497                         ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
498                         if (c != ' ') {
499                                 last = i;
500                                 last_u = j;
501                         }
502                 } else {
503                         last_u = j;
504                         for (chi = 0; chi < chl && i2 < 3; chi++) {
505                                 ptname[i++] = de->ext[i2++];
506                                 last = i;
507                         }
508                 }
509         }
510         if (!last)
511                 goto RecEnd;
512
513         i = last + dotoffset;
514         j = last_u;
515
516         lpos = cpos - (long_slots+1)*sizeof(struct msdos_dir_entry);
517         if (!memcmp(de->name,MSDOS_DOT,11))
518                 inum = inode->i_ino;
519         else if (!memcmp(de->name,MSDOS_DOTDOT,11)) {
520                 inum = parent_ino(filp->f_dentry);
521         } else {
522                 struct inode *tmp = fat_iget(sb, i_pos);
523                 if (tmp) {
524                         inum = tmp->i_ino;
525                         iput(tmp);
526                 } else
527                         inum = iunique(sb, MSDOS_ROOT_INO);
528         }
529
530         if (isvfat) {
531                 bufuname[j] = 0x0000;
532                 i = utf8 ? utf8_wcstombs(bufname, bufuname, sizeof(bufname))
533                          : uni16_to_x8(bufname, bufuname, uni_xlate, nls_io);
534         }
535
536         if (!long_slots||shortnames) {
537                 if (both)
538                         bufname[i] = '\0';
539                 if (filldir(dirent, bufname, i, *furrfu, inum,
540                             (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
541                         goto FillFailed;
542         } else {
543                 unsigned char longname[275];
544                 int long_len = utf8
545                         ? utf8_wcstombs(longname, unicode, sizeof(longname))
546                         : uni16_to_x8(longname, unicode, uni_xlate,
547                                       nls_io);
548                 if (both) {
549                         memcpy(&longname[long_len+1], bufname, i);
550                         long_len += i;
551                 }
552                 if (filldir(dirent, longname, long_len, *furrfu, inum,
553                             (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
554                         goto FillFailed;
555         }
556
557 RecEnd:
558         furrfu = &lpos;
559         filp->f_pos = cpos;
560         goto GetNew;
561 EODir:
562         filp->f_pos = cpos;
563 FillFailed:
564         if (bh)
565                 brelse(bh);
566         if (unicode) {
567                 free_page((unsigned long) unicode);
568         }
569 out:
570         unlock_kernel();
571         return ret;
572 }
573
574 static int fat_readdir(struct file *filp, void *dirent, filldir_t filldir)
575 {
576         struct inode *inode = filp->f_dentry->d_inode;
577         return fat_readdirx(inode, filp, dirent, filldir, 0, 0);
578 }
579
580 struct fat_ioctl_filldir_callback {
581         struct dirent __user *dirent;
582         int result;
583 };
584
585 static int fat_ioctl_filldir(void *__buf, const char * name, int name_len,
586                              loff_t offset, ino_t ino, unsigned int d_type)
587 {
588         struct fat_ioctl_filldir_callback *buf = __buf;
589         struct dirent __user *d1 = buf->dirent;
590         struct dirent __user *d2 = d1 + 1;
591         int len, slen;
592         int dotdir;
593
594         if (buf->result)
595                 return -EINVAL;
596         buf->result++;
597
598         if ((name_len == 1 && name[0] == '.') ||
599             (name_len == 2 && name[0] == '.' && name[1] == '.')) {
600                 dotdir = 1;
601                 len = name_len;
602         } else {
603                 dotdir = 0;
604                 len = strlen(name);
605         }
606         if (len != name_len) {
607                 slen = name_len - len;
608                 if (copy_to_user(d2->d_name, name, len)         ||
609                     put_user(0, d2->d_name + len)               ||
610                     put_user(len, &d2->d_reclen)                ||
611                     put_user(ino, &d2->d_ino)                   ||
612                     put_user(offset, &d2->d_off)                ||
613                     copy_to_user(d1->d_name, name+len+1, slen)  ||
614                     put_user(0, d1->d_name+slen)                ||
615                     put_user(slen, &d1->d_reclen))
616                         goto efault;
617         } else {
618                 if (put_user(0, d2->d_name)                     ||
619                     put_user(0, &d2->d_reclen)                  ||
620                     copy_to_user(d1->d_name, name, len)         ||
621                     put_user(0, d1->d_name+len)                 ||
622                     put_user(len, &d1->d_reclen))
623                         goto efault;
624         }
625         return 0;
626 efault:
627         buf->result = -EFAULT;
628         return -EFAULT;
629 }
630
631 static int fat_dir_ioctl(struct inode * inode, struct file * filp,
632                   unsigned int cmd, unsigned long arg)
633 {
634         struct fat_ioctl_filldir_callback buf;
635         struct dirent __user *d1;
636         int ret, shortname, both;
637
638         switch (cmd) {
639         case VFAT_IOCTL_READDIR_SHORT:
640                 shortname = 1;
641                 both = 1;
642                 break;
643         case VFAT_IOCTL_READDIR_BOTH:
644                 shortname = 0;
645                 both = 1;
646                 break;
647         default:
648                 return -EINVAL;
649         }
650
651         d1 = (struct dirent __user *)arg;
652         if (!access_ok(VERIFY_WRITE, d1, sizeof(struct dirent[2])))
653                 return -EFAULT;
654         /*
655          * Yes, we don't need this put_user() absolutely. However old
656          * code didn't return the right value. So, app use this value,
657          * in order to check whether it is EOF.
658          */
659         if (put_user(0, &d1->d_reclen))
660                 return -EFAULT;
661
662         buf.dirent = d1;
663         buf.result = 0;
664         down(&inode->i_sem);
665         ret = -ENOENT;
666         if (!IS_DEADDIR(inode)) {
667                 ret = fat_readdirx(inode, filp, &buf, fat_ioctl_filldir,
668                                    shortname, both);
669         }
670         up(&inode->i_sem);
671         if (ret >= 0)
672                 ret = buf.result;
673         return ret;
674 }
675
676 /* This assumes that size of cluster is above the 32*slots */
677
678 int fat_add_entries(struct inode *dir,int slots, struct buffer_head **bh,
679                   struct msdos_dir_entry **de, loff_t *i_pos)
680 {
681         struct super_block *sb = dir->i_sb;
682         loff_t offset, curr;
683         int row;
684         struct buffer_head *new_bh;
685
686         offset = curr = 0;
687         *bh = NULL;
688         row = 0;
689         while (fat_get_entry(dir, &curr, bh, de, i_pos) > -1) {
690                 /* check the maximum size of directory */
691                 if (curr >= FAT_MAX_DIR_SIZE) {
692                         brelse(*bh);
693                         return -ENOSPC;
694                 }
695
696                 if (IS_FREE((*de)->name)) {
697                         if (++row == slots)
698                                 return offset;
699                 } else {
700                         row = 0;
701                         offset = curr;
702                 }
703         }
704         if ((dir->i_ino == MSDOS_ROOT_INO) && (MSDOS_SB(sb)->fat_bits != 32)) 
705                 return -ENOSPC;
706         new_bh = fat_extend_dir(dir);
707         if (IS_ERR(new_bh))
708                 return PTR_ERR(new_bh);
709         brelse(new_bh);
710         do {
711                 fat_get_entry(dir, &curr, bh, de, i_pos);
712         } while (++row < slots);
713
714         return offset;
715 }
716
717 int fat_new_dir(struct inode *dir, struct inode *parent, int is_vfat)
718 {
719         struct buffer_head *bh;
720         struct msdos_dir_entry *de;
721         __le16 date, time;
722
723         bh = fat_extend_dir(dir);
724         if (IS_ERR(bh))
725                 return PTR_ERR(bh);
726
727         /* zeroed out, so... */
728         fat_date_unix2dos(dir->i_mtime.tv_sec,&time,&date);
729         de = (struct msdos_dir_entry*)&bh->b_data[0];
730         memcpy(de[0].name,MSDOS_DOT,MSDOS_NAME);
731         memcpy(de[1].name,MSDOS_DOTDOT,MSDOS_NAME);
732         de[0].attr = de[1].attr = ATTR_DIR;
733         de[0].time = de[1].time = time;
734         de[0].date = de[1].date = date;
735         if (is_vfat) {  /* extra timestamps */
736                 de[0].ctime = de[1].ctime = time;
737                 de[0].adate = de[0].cdate =
738                         de[1].adate = de[1].cdate = date;
739         }
740         de[0].start = CT_LE_W(MSDOS_I(dir)->i_logstart);
741         de[0].starthi = CT_LE_W(MSDOS_I(dir)->i_logstart>>16);
742         de[1].start = CT_LE_W(MSDOS_I(parent)->i_logstart);
743         de[1].starthi = CT_LE_W(MSDOS_I(parent)->i_logstart>>16);
744         mark_buffer_dirty(bh);
745         brelse(bh);
746         dir->i_atime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
747         mark_inode_dirty(dir);
748
749         return 0;
750 }
751
752 static int fat_get_short_entry(struct inode *dir, loff_t *pos,
753                                struct buffer_head **bh,
754                                struct msdos_dir_entry **de, loff_t *i_pos)
755 {
756         while (fat_get_entry(dir, pos, bh, de, i_pos) >= 0) {
757                 /* free entry or long name entry or volume label */
758                 if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME))
759                         return 0;
760         }
761         return -ENOENT;
762 }
763
764 /* See if directory is empty */
765 int fat_dir_empty(struct inode *dir)
766 {
767         struct buffer_head *bh;
768         struct msdos_dir_entry *de;
769         loff_t cpos, i_pos;
770         int result = 0;
771
772         bh = NULL;
773         cpos = 0;
774         while (fat_get_short_entry(dir, &cpos, &bh, &de, &i_pos) >= 0) {
775                 if (strncmp(de->name, MSDOS_DOT   , MSDOS_NAME) &&
776                     strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
777                         result = -ENOTEMPTY;
778                         break;
779                 }
780         }
781         brelse(bh);
782         return result;
783 }
784
785 /*
786  * fat_subdirs counts the number of sub-directories of dir. It can be run
787  * on directories being created.
788  */
789 int fat_subdirs(struct inode *dir)
790 {
791         struct buffer_head *bh;
792         struct msdos_dir_entry *de;
793         loff_t cpos, i_pos;
794         int count = 0;
795
796         bh = NULL;
797         cpos = 0;
798         while (fat_get_short_entry(dir, &cpos, &bh, &de, &i_pos) >= 0) {
799                 if (de->attr & ATTR_DIR)
800                         count++;
801         }
802         brelse(bh);
803         return count;
804 }
805
806 /*
807  * Scans a directory for a given file (name points to its formatted name).
808  * Returns an error code or zero.
809  */
810 int fat_scan(struct inode *dir, const unsigned char *name,
811              struct buffer_head **bh, struct msdos_dir_entry **de,
812              loff_t *i_pos)
813 {
814         loff_t cpos;
815
816         *bh = NULL;
817         cpos = 0;
818         while (fat_get_short_entry(dir, &cpos, bh, de, i_pos) >= 0) {
819                 if (!strncmp((*de)->name, name, MSDOS_NAME))
820                         return 0;
821         }
822         return -ENOENT;
823 }