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