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