upgrade to linux 2.6.10-1.12_FC2
[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 struct fat_ioctl_filldir_callback {
329         struct dirent __user *dirent;
330         int result;
331         /* for dir ioctl */
332         const char *longname;
333         int long_len;
334         const char *shortname;
335         int short_len;
336 };
337
338 static int fat_readdirx(struct inode *inode, struct file *filp, void *dirent,
339                         filldir_t filldir, int short_only, int both)
340 {
341         struct super_block *sb = inode->i_sb;
342         struct buffer_head *bh;
343         struct msdos_dir_entry *de;
344         struct nls_table *nls_io = MSDOS_SB(sb)->nls_io;
345         struct nls_table *nls_disk = MSDOS_SB(sb)->nls_disk;
346         unsigned char long_slots;
347         const char *fill_name;
348         int fill_len;
349         wchar_t bufuname[14];
350         wchar_t *unicode = NULL;
351         unsigned char c, work[8], bufname[56], *ptname = bufname;
352         unsigned long lpos, dummy, *furrfu = &lpos;
353         int uni_xlate = MSDOS_SB(sb)->options.unicode_xlate;
354         int isvfat = MSDOS_SB(sb)->options.isvfat;
355         int utf8 = MSDOS_SB(sb)->options.utf8;
356         int nocase = MSDOS_SB(sb)->options.nocase;
357         unsigned short opt_shortname = MSDOS_SB(sb)->options.shortname;
358         unsigned long inum;
359         int chi, chl, i, i2, j, last, last_u, dotoffset = 0;
360         loff_t i_pos, cpos;
361         int ret = 0;
362         
363         lock_kernel();
364
365         cpos = filp->f_pos;
366         /* Fake . and .. for the root directory. */
367         if (inode->i_ino == MSDOS_ROOT_INO) {
368                 while (cpos < 2) {
369                         if (filldir(dirent, "..", cpos+1, cpos, MSDOS_ROOT_INO, DT_DIR) < 0)
370                                 goto out;
371                         cpos++;
372                         filp->f_pos++;
373                 }
374                 if (cpos == 2) {
375                         dummy = 2;
376                         furrfu = &dummy;
377                         cpos = 0;
378                 }
379         }
380         if (cpos & (sizeof(struct msdos_dir_entry)-1)) {
381                 ret = -ENOENT;
382                 goto out;
383         }
384
385         bh = NULL;
386 GetNew:
387         long_slots = 0;
388         if (fat_get_entry(inode,&cpos,&bh,&de,&i_pos) == -1)
389                 goto EODir;
390         /* Check for long filename entry */
391         if (isvfat) {
392                 if (de->name[0] == DELETED_FLAG)
393                         goto RecEnd;
394                 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
395                         goto RecEnd;
396                 if (de->attr != ATTR_EXT && IS_FREE(de->name))
397                         goto RecEnd;
398         } else {
399                 if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
400                         goto RecEnd;
401         }
402
403         if (isvfat && de->attr == ATTR_EXT) {
404                 struct msdos_dir_slot *ds;
405                 unsigned char id;
406                 unsigned char slot;
407                 unsigned char slots;
408                 unsigned char sum;
409                 unsigned char alias_checksum;
410
411                 if (!unicode) {
412                         unicode = (wchar_t *)__get_free_page(GFP_KERNEL);
413                         if (!unicode) {
414                                 filp->f_pos = cpos;
415                                 brelse(bh);
416                                 ret = -ENOMEM;
417                                 goto out;
418                         }
419                 }
420 ParseLong:
421                 slots = 0;
422                 ds = (struct msdos_dir_slot *) de;
423                 id = ds->id;
424                 if (!(id & 0x40))
425                         goto RecEnd;
426                 slots = id & ~0x40;
427                 if (slots > 20 || !slots)       /* ceil(256 * 2 / 26) */
428                         goto RecEnd;
429                 long_slots = slots;
430                 alias_checksum = ds->alias_checksum;
431
432                 slot = slots;
433                 while (1) {
434                         int offset;
435
436                         slot--;
437                         offset = slot * 13;
438                         fat16_towchar(unicode + offset, ds->name0_4, 5);
439                         fat16_towchar(unicode + offset + 5, ds->name5_10, 6);
440                         fat16_towchar(unicode + offset + 11, ds->name11_12, 2);
441
442                         if (ds->id & 0x40) {
443                                 unicode[offset + 13] = 0;
444                         }
445                         if (fat_get_entry(inode,&cpos,&bh,&de,&i_pos) == -1)
446                                 goto EODir;
447                         if (slot == 0)
448                                 break;
449                         ds = (struct msdos_dir_slot *) de;
450                         if (ds->attr !=  ATTR_EXT)
451                                 goto RecEnd;    /* XXX */
452                         if ((ds->id & ~0x40) != slot)
453                                 goto ParseLong;
454                         if (ds->alias_checksum != alias_checksum)
455                                 goto ParseLong;
456                 }
457                 if (de->name[0] == DELETED_FLAG)
458                         goto RecEnd;
459                 if (de->attr ==  ATTR_EXT)
460                         goto ParseLong;
461                 if (IS_FREE(de->name) || (de->attr & ATTR_VOLUME))
462                         goto RecEnd;
463                 for (sum = 0, i = 0; i < 11; i++)
464                         sum = (((sum&1)<<7)|((sum&0xfe)>>1)) + de->name[i];
465                 if (sum != alias_checksum)
466                         long_slots = 0;
467         }
468
469         if ((de->attr & ATTR_HIDDEN) && MSDOS_SB(sb)->options.dotsOK) {
470                 *ptname++ = '.';
471                 dotoffset = 1;
472         }
473
474         memcpy(work, de->name, sizeof(de->name));
475         /* see namei.c, msdos_format_name */
476         if (work[0] == 0x05)
477                 work[0] = 0xE5;
478         for (i = 0, j = 0, last = 0, last_u = 0; i < 8;) {
479                 if (!(c = work[i])) break;
480                 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
481                                         &bufuname[j++], opt_shortname,
482                                         de->lcase & CASE_LOWER_BASE);
483                 if (chl <= 1) {
484                         ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
485                         if (c != ' ') {
486                                 last = i;
487                                 last_u = j;
488                         }
489                 } else {
490                         last_u = j;
491                         for (chi = 0; chi < chl && i < 8; chi++) {
492                                 ptname[i] = work[i];
493                                 i++; last = i;
494                         }
495                 }
496         }
497         i = last;
498         j = last_u;
499         fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
500         ptname[i++] = '.';
501         for (i2 = 0; i2 < 3;) {
502                 if (!(c = de->ext[i2])) break;
503                 chl = fat_shortname2uni(nls_disk, &de->ext[i2], 3 - i2,
504                                         &bufuname[j++], opt_shortname,
505                                         de->lcase & CASE_LOWER_EXT);
506                 if (chl <= 1) {
507                         i2++;
508                         ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
509                         if (c != ' ') {
510                                 last = i;
511                                 last_u = j;
512                         }
513                 } else {
514                         last_u = j;
515                         for (chi = 0; chi < chl && i2 < 3; chi++) {
516                                 ptname[i++] = de->ext[i2++];
517                                 last = i;
518                         }
519                 }
520         }
521         if (!last)
522                 goto RecEnd;
523
524         i = last + dotoffset;
525         j = last_u;
526
527         lpos = cpos - (long_slots+1)*sizeof(struct msdos_dir_entry);
528         if (!memcmp(de->name,MSDOS_DOT,11))
529                 inum = inode->i_ino;
530         else if (!memcmp(de->name,MSDOS_DOTDOT,11)) {
531                 inum = parent_ino(filp->f_dentry);
532         } else {
533                 struct inode *tmp = fat_iget(sb, i_pos);
534                 if (tmp) {
535                         inum = tmp->i_ino;
536                         iput(tmp);
537                 } else
538                         inum = iunique(sb, MSDOS_ROOT_INO);
539         }
540
541         if (isvfat) {
542                 bufuname[j] = 0x0000;
543                 i = utf8 ? utf8_wcstombs(bufname, bufuname, sizeof(bufname))
544                          : uni16_to_x8(bufname, bufuname, uni_xlate, nls_io);
545         }
546
547         fill_name = bufname;
548         fill_len = i;
549         if (!short_only && long_slots) {
550                 /* convert the unicode long name. 261 is maximum size
551                  * of unicode buffer. (13 * slots + nul) */
552                 void *longname = unicode + 261;
553                 int buf_size = PAGE_SIZE - (261 * sizeof(unicode[0]));
554                 int long_len = utf8
555                         ? utf8_wcstombs(longname, unicode, buf_size)
556                         : uni16_to_x8(longname, unicode, uni_xlate, nls_io);
557
558                 if (!both) {
559                         fill_name = longname;
560                         fill_len = long_len;
561                 } else {
562                         /* hack for fat_ioctl_filldir() */
563                         struct fat_ioctl_filldir_callback *p = dirent;
564
565                         p->longname = longname;
566                         p->long_len = long_len;
567                         p->shortname = bufname;
568                         p->short_len = i;
569                         fill_name = NULL;
570                         fill_len = 0;
571                 }
572         }
573         if (filldir(dirent, fill_name, fill_len, *furrfu, inum,
574                     (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
575                 goto FillFailed;
576
577 RecEnd:
578         furrfu = &lpos;
579         filp->f_pos = cpos;
580         goto GetNew;
581 EODir:
582         filp->f_pos = cpos;
583 FillFailed:
584         if (bh)
585                 brelse(bh);
586         if (unicode)
587                 free_page((unsigned long)unicode);
588 out:
589         unlock_kernel();
590         return ret;
591 }
592
593 static int fat_readdir(struct file *filp, void *dirent, filldir_t filldir)
594 {
595         struct inode *inode = filp->f_dentry->d_inode;
596         return fat_readdirx(inode, filp, dirent, filldir, 0, 0);
597 }
598
599 static int fat_ioctl_filldir(void *__buf, const char *name, int name_len,
600                              loff_t offset, ino_t ino, unsigned int d_type)
601 {
602         struct fat_ioctl_filldir_callback *buf = __buf;
603         struct dirent __user *d1 = buf->dirent;
604         struct dirent __user *d2 = d1 + 1;
605
606         if (buf->result)
607                 return -EINVAL;
608         buf->result++;
609
610         if (name != NULL) {
611                 /* dirent has only short name */
612                 if (name_len >= sizeof(d1->d_name))
613                         name_len = sizeof(d1->d_name) - 1;
614
615                 if (put_user(0, d2->d_name)                     ||
616                     put_user(0, &d2->d_reclen)                  ||
617                     copy_to_user(d1->d_name, name, name_len)    ||
618                     put_user(0, d1->d_name + name_len)          ||
619                     put_user(name_len, &d1->d_reclen))
620                         goto efault;
621         } else {
622                 /* dirent has short and long name */
623                 const char *longname = buf->longname;
624                 int long_len = buf->long_len;
625                 const char *shortname = buf->shortname;
626                 int short_len = buf->short_len;
627
628                 if (long_len >= sizeof(d1->d_name))
629                         long_len = sizeof(d1->d_name) - 1;
630                 if (short_len >= sizeof(d1->d_name))
631                         short_len = sizeof(d1->d_name) - 1;
632
633                 if (copy_to_user(d2->d_name, longname, long_len)        ||
634                     put_user(0, d2->d_name + long_len)                  ||
635                     put_user(long_len, &d2->d_reclen)                   ||
636                     put_user(ino, &d2->d_ino)                           ||
637                     put_user(offset, &d2->d_off)                        ||
638                     copy_to_user(d1->d_name, shortname, short_len)      ||
639                     put_user(0, d1->d_name + short_len)                 ||
640                     put_user(short_len, &d1->d_reclen))
641                         goto efault;
642         }
643         return 0;
644 efault:
645         buf->result = -EFAULT;
646         return -EFAULT;
647 }
648
649 static int fat_dir_ioctl(struct inode * inode, struct file * filp,
650                   unsigned int cmd, unsigned long arg)
651 {
652         struct fat_ioctl_filldir_callback buf;
653         struct dirent __user *d1;
654         int ret, short_only, both;
655
656         switch (cmd) {
657         case VFAT_IOCTL_READDIR_SHORT:
658                 short_only = 1;
659                 both = 0;
660                 break;
661         case VFAT_IOCTL_READDIR_BOTH:
662                 short_only = 0;
663                 both = 1;
664                 break;
665         default:
666                 return -EINVAL;
667         }
668
669         d1 = (struct dirent __user *)arg;
670         if (!access_ok(VERIFY_WRITE, d1, sizeof(struct dirent[2])))
671                 return -EFAULT;
672         /*
673          * Yes, we don't need this put_user() absolutely. However old
674          * code didn't return the right value. So, app use this value,
675          * in order to check whether it is EOF.
676          */
677         if (put_user(0, &d1->d_reclen))
678                 return -EFAULT;
679
680         buf.dirent = d1;
681         buf.result = 0;
682         down(&inode->i_sem);
683         ret = -ENOENT;
684         if (!IS_DEADDIR(inode)) {
685                 ret = fat_readdirx(inode, filp, &buf, fat_ioctl_filldir,
686                                    short_only, both);
687         }
688         up(&inode->i_sem);
689         if (ret >= 0)
690                 ret = buf.result;
691         return ret;
692 }
693
694 /* This assumes that size of cluster is above the 32*slots */
695
696 int fat_add_entries(struct inode *dir,int slots, struct buffer_head **bh,
697                   struct msdos_dir_entry **de, loff_t *i_pos)
698 {
699         struct super_block *sb = dir->i_sb;
700         loff_t offset, curr;
701         int row;
702         struct buffer_head *new_bh;
703
704         offset = curr = 0;
705         *bh = NULL;
706         row = 0;
707         while (fat_get_entry(dir, &curr, bh, de, i_pos) > -1) {
708                 /* check the maximum size of directory */
709                 if (curr >= FAT_MAX_DIR_SIZE) {
710                         brelse(*bh);
711                         return -ENOSPC;
712                 }
713
714                 if (IS_FREE((*de)->name)) {
715                         if (++row == slots)
716                                 return offset;
717                 } else {
718                         row = 0;
719                         offset = curr;
720                 }
721         }
722         if ((dir->i_ino == MSDOS_ROOT_INO) && (MSDOS_SB(sb)->fat_bits != 32)) 
723                 return -ENOSPC;
724         new_bh = fat_extend_dir(dir);
725         if (IS_ERR(new_bh))
726                 return PTR_ERR(new_bh);
727         brelse(new_bh);
728         do {
729                 fat_get_entry(dir, &curr, bh, de, i_pos);
730         } while (++row < slots);
731
732         return offset;
733 }
734
735 int fat_new_dir(struct inode *dir, struct inode *parent, int is_vfat)
736 {
737         struct buffer_head *bh;
738         struct msdos_dir_entry *de;
739         __le16 date, time;
740
741         bh = fat_extend_dir(dir);
742         if (IS_ERR(bh))
743                 return PTR_ERR(bh);
744
745         /* zeroed out, so... */
746         fat_date_unix2dos(dir->i_mtime.tv_sec,&time,&date);
747         de = (struct msdos_dir_entry*)&bh->b_data[0];
748         memcpy(de[0].name,MSDOS_DOT,MSDOS_NAME);
749         memcpy(de[1].name,MSDOS_DOTDOT,MSDOS_NAME);
750         de[0].attr = de[1].attr = ATTR_DIR;
751         de[0].time = de[1].time = time;
752         de[0].date = de[1].date = date;
753         if (is_vfat) {  /* extra timestamps */
754                 de[0].ctime = de[1].ctime = time;
755                 de[0].adate = de[0].cdate =
756                         de[1].adate = de[1].cdate = date;
757         }
758         de[0].start = cpu_to_le16(MSDOS_I(dir)->i_logstart);
759         de[0].starthi = cpu_to_le16(MSDOS_I(dir)->i_logstart>>16);
760         de[1].start = cpu_to_le16(MSDOS_I(parent)->i_logstart);
761         de[1].starthi = cpu_to_le16(MSDOS_I(parent)->i_logstart>>16);
762         mark_buffer_dirty(bh);
763         brelse(bh);
764         dir->i_atime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
765         mark_inode_dirty(dir);
766
767         return 0;
768 }
769
770 static int fat_get_short_entry(struct inode *dir, loff_t *pos,
771                                struct buffer_head **bh,
772                                struct msdos_dir_entry **de, loff_t *i_pos)
773 {
774         while (fat_get_entry(dir, pos, bh, de, i_pos) >= 0) {
775                 /* free entry or long name entry or volume label */
776                 if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME))
777                         return 0;
778         }
779         return -ENOENT;
780 }
781
782 /* See if directory is empty */
783 int fat_dir_empty(struct inode *dir)
784 {
785         struct buffer_head *bh;
786         struct msdos_dir_entry *de;
787         loff_t cpos, i_pos;
788         int result = 0;
789
790         bh = NULL;
791         cpos = 0;
792         while (fat_get_short_entry(dir, &cpos, &bh, &de, &i_pos) >= 0) {
793                 if (strncmp(de->name, MSDOS_DOT   , MSDOS_NAME) &&
794                     strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
795                         result = -ENOTEMPTY;
796                         break;
797                 }
798         }
799         brelse(bh);
800         return result;
801 }
802
803 /*
804  * fat_subdirs counts the number of sub-directories of dir. It can be run
805  * on directories being created.
806  */
807 int fat_subdirs(struct inode *dir)
808 {
809         struct buffer_head *bh;
810         struct msdos_dir_entry *de;
811         loff_t cpos, i_pos;
812         int count = 0;
813
814         bh = NULL;
815         cpos = 0;
816         while (fat_get_short_entry(dir, &cpos, &bh, &de, &i_pos) >= 0) {
817                 if (de->attr & ATTR_DIR)
818                         count++;
819         }
820         brelse(bh);
821         return count;
822 }
823
824 /*
825  * Scans a directory for a given file (name points to its formatted name).
826  * Returns an error code or zero.
827  */
828 int fat_scan(struct inode *dir, const unsigned char *name,
829              struct buffer_head **bh, struct msdos_dir_entry **de,
830              loff_t *i_pos)
831 {
832         loff_t cpos;
833
834         *bh = NULL;
835         cpos = 0;
836         while (fat_get_short_entry(dir, &cpos, bh, de, i_pos) >= 0) {
837                 if (!strncmp((*de)->name, name, MSDOS_NAME))
838                         return 0;
839         }
840         return -ENOENT;
841 }