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