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