Blame view

fs/fat/dir.c 33.8 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  /*
   *  linux/fs/fat/dir.c
   *
   *  directory handling functions for fat-based filesystems
   *
   *  Written 1992,1993 by Werner Almesberger
   *
   *  Hidden files 1995 by Albert Cahalan <albert@ccs.neu.edu> <adc@coe.neu.edu>
   *
   *  VFAT extensions by Gordon Chaffee <chaffee@plateau.cs.berkeley.edu>
   *  Merged with msdos fs by Henrik Storner <storner@osiris.ping.dk>
   *  Rewritten for constant inumbers. Plugged buffer overrun in readdir(). AV
   *  Short name translation 1999, 2001 by Wolfram Pienkoss <wp@bszh.de>
   */
  
  #include <linux/module.h>
  #include <linux/slab.h>
  #include <linux/time.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
  #include <linux/buffer_head.h>
188f83dfe   David Howells   [PATCH] BLOCK: Mo...
20
  #include <linux/compat.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21
  #include <asm/uaccess.h>
3ed3dec15   Andy Shevchenko   fat: use pack_hex...
22
  #include <linux/kernel.h>
9e975dae2   OGAWA Hirofumi   fat: split includ...
23
  #include "fat.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24

74675a585   Alan Stern   NLS: update handl...
25
26
27
28
29
30
31
32
33
34
35
36
  /*
   * Maximum buffer size of short name.
   * [(MSDOS_NAME + '.') * max one char + nul]
   * For msdos style, ['.' (hidden) + MSDOS_NAME + '.' + nul]
   */
  #define FAT_MAX_SHORT_SIZE	((MSDOS_NAME + 1) * NLS_MAX_CHARSET_SIZE + 1)
  /*
   * Maximum buffer size of unicode chars from slots.
   * [(max longname slots * 13 (size in a slot) + nul) * sizeof(wchar_t)]
   */
  #define FAT_MAX_UNI_CHARS	((MSDOS_SLOTS - 1) * 13 + 1)
  #define FAT_MAX_UNI_SIZE	(FAT_MAX_UNI_CHARS * sizeof(wchar_t))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37
38
39
40
41
42
43
  static inline loff_t fat_make_i_pos(struct super_block *sb,
  				    struct buffer_head *bh,
  				    struct msdos_dir_entry *de)
  {
  	return ((loff_t)bh->b_blocknr << MSDOS_SB(sb)->dir_per_block_bits)
  		| (de - (struct msdos_dir_entry *)bh->b_data);
  }
f3ef6f63e   Karsten Wiese   [PATCH] Speedup F...
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  static inline void fat_dir_readahead(struct inode *dir, sector_t iblock,
  				     sector_t phys)
  {
  	struct super_block *sb = dir->i_sb;
  	struct msdos_sb_info *sbi = MSDOS_SB(sb);
  	struct buffer_head *bh;
  	int sec;
  
  	/* This is not a first sector of cluster, or sec_per_clus == 1 */
  	if ((iblock & (sbi->sec_per_clus - 1)) || sbi->sec_per_clus == 1)
  		return;
  	/* root dir of FAT12/FAT16 */
  	if ((sbi->fat_bits != 32) && (dir->i_ino == MSDOS_ROOT_INO))
  		return;
83b7c996d   OGAWA Hirofumi   [PATCH] fat: use ...
58
59
  	bh = sb_find_get_block(sb, phys);
  	if (bh == NULL || !buffer_uptodate(bh)) {
f3ef6f63e   Karsten Wiese   [PATCH] Speedup F...
60
61
62
63
64
  		for (sec = 0; sec < sbi->sec_per_clus; sec++)
  			sb_breadahead(sb, phys + sec);
  	}
  	brelse(bh);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  /* Returns the inode number of the directory entry at offset pos. If bh is
     non-NULL, it is brelse'd before. Pos is incremented. The buffer header is
     returned in bh.
     AV. Most often we do it item-by-item. Makes sense to optimize.
     AV. OK, there we go: if both bh and de are non-NULL we assume that we just
     AV. want the next entry (took one explicit de=NULL in vfat/namei.c).
     AV. It's done in fat_get_entry() (inlined), here the slow case lives.
     AV. Additionally, when we return -1 (i.e. reached the end of directory)
     AV. we make bh NULL.
   */
  static int fat__get_entry(struct inode *dir, loff_t *pos,
  			  struct buffer_head **bh, struct msdos_dir_entry **de)
  {
  	struct super_block *sb = dir->i_sb;
  	sector_t phys, iblock;
e5174baae   OGAWA Hirofumi   [PATCH] fat: supp...
80
81
  	unsigned long mapped_blocks;
  	int err, offset;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82
83
84
85
86
87
88
  
  next:
  	if (*bh)
  		brelse(*bh);
  
  	*bh = NULL;
  	iblock = *pos >> sb->s_blocksize_bits;
2bdf67eb1   OGAWA Hirofumi   fat: mmu_private ...
89
  	err = fat_bmap(dir, iblock, &phys, &mapped_blocks, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
90
91
  	if (err || !phys)
  		return -1;	/* beyond EOF or error */
f3ef6f63e   Karsten Wiese   [PATCH] Speedup F...
92
  	fat_dir_readahead(dir, iblock, phys);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
93
94
  	*bh = sb_bread(sb, phys);
  	if (*bh == NULL) {
869f58c0c   Alexey Fisher   fat: Replace all ...
95
  		fat_msg(sb, KERN_ERR, "Directory bread(block %llu) failed",
c3302931d   OGAWA Hirofumi   fat: i_blocks war...
96
  		       (llu)phys);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  		/* skip this block */
  		*pos = (iblock + 1) << sb->s_blocksize_bits;
  		goto next;
  	}
  
  	offset = *pos & (sb->s_blocksize - 1);
  	*pos += sizeof(struct msdos_dir_entry);
  	*de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
  
  	return 0;
  }
  
  static inline int fat_get_entry(struct inode *dir, loff_t *pos,
  				struct buffer_head **bh,
  				struct msdos_dir_entry **de)
  {
  	/* Fast stuff first */
  	if (*bh && *de &&
  	    (*de - (struct msdos_dir_entry *)(*bh)->b_data) < MSDOS_SB(dir->i_sb)->dir_per_block - 1) {
  		*pos += sizeof(struct msdos_dir_entry);
  		(*de)++;
  		return 0;
  	}
  	return fat__get_entry(dir, pos, bh, de);
  }
  
  /*
4de151d8c   Alexey Dobriyan   It's UTF-8
124
   * Convert Unicode 16 to UTF-8, translated Unicode, or ASCII.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
125
126
127
128
129
130
131
132
   * If uni_xlate is enabled and we can't get a 1:1 conversion, use a
   * colon as an escape character since it is normally invalid on the vfat
   * filesystem. The following four characters are the hexadecimal digits
   * of Unicode value. This lets us do a full dump and restore of Unicode
   * filenames. We could get into some trouble with long Unicode names,
   * but ignore that right now.
   * Ahem... Stack smashing in ring 0 isn't fun. Fixed.
   */
869f58c0c   Alexey Fisher   fat: Replace all ...
133
134
  static int uni16_to_x8(struct super_block *sb, unsigned char *ascii,
  		       const wchar_t *uni, int len, struct nls_table *nls)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
135
  {
869f58c0c   Alexey Fisher   fat: Replace all ...
136
  	int uni_xlate = MSDOS_SB(sb)->options.unicode_xlate;
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
137
138
  	const wchar_t *ip;
  	wchar_t ec;
3ed3dec15   Andy Shevchenko   fat: use pack_hex...
139
  	unsigned char *op;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
140
  	int charlen;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
141
142
143
  
  	ip = uni;
  	op = ascii;
f22032ba8   Keith Mok   vfat: bug fix for...
144
  	while (*ip && ((len - NLS_MAX_CHARSET_SIZE) > 0)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
145
  		ec = *ip++;
3ed3dec15   Andy Shevchenko   fat: use pack_hex...
146
  		if ((charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE)) > 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
147
  			op += charlen;
f22032ba8   Keith Mok   vfat: bug fix for...
148
  			len -= charlen;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
149
150
  		} else {
  			if (uni_xlate == 1) {
3ed3dec15   Andy Shevchenko   fat: use pack_hex...
151
  				*op++ = ':';
0a90e0f10   Andy Shevchenko   fat: follow renam...
152
153
  				op = hex_byte_pack(op, ec >> 8);
  				op = hex_byte_pack(op, ec);
f22032ba8   Keith Mok   vfat: bug fix for...
154
  				len -= 5;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155
156
  			} else {
  				*op++ = '?';
f22032ba8   Keith Mok   vfat: bug fix for...
157
  				len--;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
158
159
  			}
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
160
  	}
f22032ba8   Keith Mok   vfat: bug fix for...
161
162
  
  	if (unlikely(*ip)) {
869f58c0c   Alexey Fisher   fat: Replace all ...
163
164
  		fat_msg(sb, KERN_WARNING, "filename was truncated while "
  			"converting.");
f22032ba8   Keith Mok   vfat: bug fix for...
165
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
166
167
168
  	*op = 0;
  	return (op - ascii);
  }
869f58c0c   Alexey Fisher   fat: Replace all ...
169
  static inline int fat_uni_to_x8(struct super_block *sb, const wchar_t *uni,
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
170
171
  				unsigned char *buf, int size)
  {
869f58c0c   Alexey Fisher   fat: Replace all ...
172
  	struct msdos_sb_info *sbi = MSDOS_SB(sb);
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
173
  	if (sbi->options.utf8)
74675a585   Alan Stern   NLS: update handl...
174
175
  		return utf16s_to_utf8s(uni, FAT_MAX_UNI_CHARS,
  				UTF16_HOST_ENDIAN, buf, size);
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
176
  	else
869f58c0c   Alexey Fisher   fat: Replace all ...
177
  		return uni16_to_x8(sb, buf, uni, size, sbi->nls_io);
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
178
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
  static inline int
  fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
  {
  	int charlen;
  
  	charlen = t->char2uni(c, clen, uni);
  	if (charlen < 0) {
  		*uni = 0x003f;	/* a question mark */
  		charlen = 1;
  	}
  	return charlen;
  }
  
  static inline int
  fat_short2lower_uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
  {
  	int charlen;
  	wchar_t wc;
  
  	charlen = t->char2uni(c, clen, &wc);
  	if (charlen < 0) {
  		*uni = 0x003f;	/* a question mark */
  		charlen = 1;
  	} else if (charlen <= 1) {
  		unsigned char nc = t->charset2lower[*c];
  
  		if (!nc)
  			nc = *c;
  
  		if ( (charlen = t->char2uni(&nc, 1, uni)) < 0) {
  			*uni = 0x003f;	/* a question mark */
  			charlen = 1;
  		}
  	} else
  		*uni = wc;
  
  	return charlen;
  }
  
  static inline int
  fat_shortname2uni(struct nls_table *nls, unsigned char *buf, int buf_size,
  		  wchar_t *uni_buf, unsigned short opt, int lower)
  {
  	int len = 0;
  
  	if (opt & VFAT_SFN_DISPLAY_LOWER)
  		len =  fat_short2lower_uni(nls, buf, buf_size, uni_buf);
  	else if (opt & VFAT_SFN_DISPLAY_WIN95)
  		len = fat_short2uni(nls, buf, buf_size, uni_buf);
  	else if (opt & VFAT_SFN_DISPLAY_WINNT) {
  		if (lower)
  			len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
  		else
  			len = fat_short2uni(nls, buf, buf_size, uni_buf);
  	} else
  		len = fat_short2uni(nls, buf, buf_size, uni_buf);
  
  	return len;
  }
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
238
239
240
241
242
243
244
245
246
247
248
249
  static inline int fat_name_match(struct msdos_sb_info *sbi,
  				 const unsigned char *a, int a_len,
  				 const unsigned char *b, int b_len)
  {
  	if (a_len != b_len)
  		return 0;
  
  	if (sbi->options.name_check != 's')
  		return !nls_strnicmp(sbi->nls_io, a, b, a_len);
  	else
  		return !memcmp(a, b, a_len);
  }
ad2c1604d   Pekka Enberg   [PATCH] fat: Remo...
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
  enum { PARSE_INVALID = 1, PARSE_NOT_LONGNAME, PARSE_EOF, };
  
  /**
   * fat_parse_long - Parse extended directory entry.
   *
   * This function returns zero on success, negative value on error, or one of
   * the following:
   *
   * %PARSE_INVALID - Directory entry is invalid.
   * %PARSE_NOT_LONGNAME - Directory entry does not contain longname.
   * %PARSE_EOF - Directory has no more entries.
   */
  static int fat_parse_long(struct inode *dir, loff_t *pos,
  			  struct buffer_head **bh, struct msdos_dir_entry **de,
  			  wchar_t **unicode, unsigned char *nr_slots)
  {
  	struct msdos_dir_slot *ds;
  	unsigned char id, slot, slots, alias_checksum;
  
  	if (!*unicode) {
c7a6c4edc   OGAWA Hirofumi   fat: use __getname()
270
  		*unicode = __getname();
ad2c1604d   Pekka Enberg   [PATCH] fat: Remo...
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
  		if (!*unicode) {
  			brelse(*bh);
  			return -ENOMEM;
  		}
  	}
  parse_long:
  	slots = 0;
  	ds = (struct msdos_dir_slot *)*de;
  	id = ds->id;
  	if (!(id & 0x40))
  		return PARSE_INVALID;
  	slots = id & ~0x40;
  	if (slots > 20 || !slots)	/* ceil(256 * 2 / 26) */
  		return PARSE_INVALID;
  	*nr_slots = slots;
  	alias_checksum = ds->alias_checksum;
  
  	slot = slots;
  	while (1) {
  		int offset;
  
  		slot--;
  		offset = slot * 13;
  		fat16_towchar(*unicode + offset, ds->name0_4, 5);
  		fat16_towchar(*unicode + offset + 5, ds->name5_10, 6);
  		fat16_towchar(*unicode + offset + 11, ds->name11_12, 2);
  
  		if (ds->id & 0x40)
  			(*unicode)[offset + 13] = 0;
  		if (fat_get_entry(dir, pos, bh, de) < 0)
  			return PARSE_EOF;
  		if (slot == 0)
  			break;
  		ds = (struct msdos_dir_slot *)*de;
  		if (ds->attr != ATTR_EXT)
  			return PARSE_NOT_LONGNAME;
  		if ((ds->id & ~0x40) != slot)
  			goto parse_long;
  		if (ds->alias_checksum != alias_checksum)
  			goto parse_long;
  	}
  	if ((*de)->name[0] == DELETED_FLAG)
  		return PARSE_INVALID;
  	if ((*de)->attr == ATTR_EXT)
  		goto parse_long;
  	if (IS_FREE((*de)->name) || ((*de)->attr & ATTR_VOLUME))
  		return PARSE_INVALID;
  	if (fat_checksum((*de)->name) != alias_checksum)
  		*nr_slots = 0;
  
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
323
324
325
326
327
328
329
330
331
332
333
  /*
   * Return values: negative -> error, 0 -> not found, positive -> found,
   * value is the total amount of slots, including the shortname entry.
   */
  int fat_search_long(struct inode *inode, const unsigned char *name,
  		    int name_len, struct fat_slot_info *sinfo)
  {
  	struct super_block *sb = inode->i_sb;
  	struct msdos_sb_info *sbi = MSDOS_SB(sb);
  	struct buffer_head *bh = NULL;
  	struct msdos_dir_entry *de;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
334
  	struct nls_table *nls_disk = sbi->nls_disk;
f22032ba8   Keith Mok   vfat: bug fix for...
335
  	unsigned char nr_slots;
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
336
  	wchar_t bufuname[14];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
337
  	wchar_t *unicode = NULL;
f22032ba8   Keith Mok   vfat: bug fix for...
338
  	unsigned char work[MSDOS_NAME];
98a151600   OGAWA Hirofumi   fat: use same log...
339
  	unsigned char bufname[FAT_MAX_SHORT_SIZE];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
340
341
  	unsigned short opt_shortname = sbi->options.shortname;
  	loff_t cpos = 0;
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
342
  	int chl, i, j, last_u, err, len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
343
344
  
  	err = -ENOENT;
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
345
  	while (1) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
346
  		if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
347
  			goto end_of_dir;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
348
349
350
351
352
353
354
355
356
  parse_record:
  		nr_slots = 0;
  		if (de->name[0] == DELETED_FLAG)
  			continue;
  		if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
  			continue;
  		if (de->attr != ATTR_EXT && IS_FREE(de->name))
  			continue;
  		if (de->attr == ATTR_EXT) {
ad2c1604d   Pekka Enberg   [PATCH] fat: Remo...
357
358
  			int status = fat_parse_long(inode, &cpos, &bh, &de,
  						    &unicode, &nr_slots);
52e9d9f4b   Darren Jenkins   fat: cleanup fat_...
359
360
361
362
  			if (status < 0) {
  				err = status;
  				goto end_of_dir;
  			} else if (status == PARSE_INVALID)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
363
  				continue;
ad2c1604d   Pekka Enberg   [PATCH] fat: Remo...
364
365
366
  			else if (status == PARSE_NOT_LONGNAME)
  				goto parse_record;
  			else if (status == PARSE_EOF)
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
367
  				goto end_of_dir;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
368
369
370
371
372
373
374
  		}
  
  		memcpy(work, de->name, sizeof(de->name));
  		/* see namei.c, msdos_format_name */
  		if (work[0] == 0x05)
  			work[0] = 0xE5;
  		for (i = 0, j = 0, last_u = 0; i < 8;) {
9aacd5993   OGAWA Hirofumi   fat: gcc 4.3 warn...
375
376
  			if (!work[i])
  				break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
377
378
379
380
381
382
383
384
385
386
387
388
389
  			chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
  						&bufuname[j++], opt_shortname,
  						de->lcase & CASE_LOWER_BASE);
  			if (chl <= 1) {
  				if (work[i] != ' ')
  					last_u = j;
  			} else {
  				last_u = j;
  			}
  			i += chl;
  		}
  		j = last_u;
  		fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
9aacd5993   OGAWA Hirofumi   fat: gcc 4.3 warn...
390
391
392
393
394
  		for (i = 8; i < MSDOS_NAME;) {
  			if (!work[i])
  				break;
  			chl = fat_shortname2uni(nls_disk, &work[i],
  						MSDOS_NAME - i,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
395
396
397
  						&bufuname[j++], opt_shortname,
  						de->lcase & CASE_LOWER_EXT);
  			if (chl <= 1) {
9aacd5993   OGAWA Hirofumi   fat: gcc 4.3 warn...
398
  				if (work[i] != ' ')
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
399
400
401
402
403
404
405
406
  					last_u = j;
  			} else {
  				last_u = j;
  			}
  			i += chl;
  		}
  		if (!last_u)
  			continue;
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
407
  		/* Compare shortname */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
408
  		bufuname[last_u] = 0x0000;
869f58c0c   Alexey Fisher   fat: Replace all ...
409
  		len = fat_uni_to_x8(sb, bufuname, bufname, sizeof(bufname));
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
410
411
  		if (fat_name_match(sbi, name, name_len, bufname, len))
  			goto found;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
412
413
  
  		if (nr_slots) {
98a151600   OGAWA Hirofumi   fat: use same log...
414
415
  			void *longname = unicode + FAT_MAX_UNI_CHARS;
  			int size = PATH_MAX - FAT_MAX_UNI_SIZE;
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
416
  			/* Compare longname */
869f58c0c   Alexey Fisher   fat: Replace all ...
417
  			len = fat_uni_to_x8(sb, unicode, longname, size);
98a151600   OGAWA Hirofumi   fat: use same log...
418
  			if (fat_name_match(sbi, name, name_len, longname, len))
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
419
  				goto found;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
420
421
  		}
  	}
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
422
  found:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
423
424
425
426
427
428
429
  	nr_slots++;	/* include the de */
  	sinfo->slot_off = cpos - nr_slots * sizeof(*de);
  	sinfo->nr_slots = nr_slots;
  	sinfo->de = de;
  	sinfo->bh = bh;
  	sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
  	err = 0;
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
430
  end_of_dir:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
431
  	if (unicode)
c7a6c4edc   OGAWA Hirofumi   fat: use __getname()
432
  		__putname(unicode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
433
434
435
  
  	return err;
  }
7c709d00d   OGAWA Hirofumi   [PATCH] fat: s/EX...
436
  EXPORT_SYMBOL_GPL(fat_search_long);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
437
438
  
  struct fat_ioctl_filldir_callback {
c483bab09   OGAWA Hirofumi   fat: fix VFAT com...
439
  	void __user *dirent;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
440
441
442
443
444
445
446
  	int result;
  	/* for dir ioctl */
  	const char *longname;
  	int long_len;
  	const char *shortname;
  	int short_len;
  };
ad2c1604d   Pekka Enberg   [PATCH] fat: Remo...
447
448
  static int __fat_readdir(struct inode *inode, struct file *filp, void *dirent,
  			 filldir_t filldir, int short_only, int both)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
449
450
451
452
453
  {
  	struct super_block *sb = inode->i_sb;
  	struct msdos_sb_info *sbi = MSDOS_SB(sb);
  	struct buffer_head *bh;
  	struct msdos_dir_entry *de;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
454
  	struct nls_table *nls_disk = sbi->nls_disk;
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
455
  	unsigned char nr_slots;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
456
457
  	wchar_t bufuname[14];
  	wchar_t *unicode = NULL;
98a151600   OGAWA Hirofumi   fat: use same log...
458
459
  	unsigned char c, work[MSDOS_NAME];
  	unsigned char bufname[FAT_MAX_SHORT_SIZE], *ptname = bufname;
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
460
  	unsigned short opt_shortname = sbi->options.shortname;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
461
  	int isvfat = sbi->options.isvfat;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
462
  	int nocase = sbi->options.nocase;
dcd8c53f1   OGAWA Hirofumi   fat: small optimi...
463
  	const char *fill_name = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
464
  	unsigned long inum;
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
465
  	unsigned long lpos, dummy, *furrfu = &lpos;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
466
  	loff_t cpos;
dcd8c53f1   OGAWA Hirofumi   fat: small optimi...
467
  	int chi, chl, i, i2, j, last, last_u, dotoffset = 0, fill_len = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
468
  	int ret = 0;
8f5934278   Linus Torvalds   Replace BKL with ...
469
  	lock_super(sb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
  
  	cpos = filp->f_pos;
  	/* Fake . and .. for the root directory. */
  	if (inode->i_ino == MSDOS_ROOT_INO) {
  		while (cpos < 2) {
  			if (filldir(dirent, "..", cpos+1, cpos, MSDOS_ROOT_INO, DT_DIR) < 0)
  				goto out;
  			cpos++;
  			filp->f_pos++;
  		}
  		if (cpos == 2) {
  			dummy = 2;
  			furrfu = &dummy;
  			cpos = 0;
  		}
  	}
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
486
  	if (cpos & (sizeof(struct msdos_dir_entry) - 1)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
487
488
489
490
491
  		ret = -ENOENT;
  		goto out;
  	}
  
  	bh = NULL;
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
492
  get_new:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
493
  	if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
494
  		goto end_of_dir;
ad2c1604d   Pekka Enberg   [PATCH] fat: Remo...
495
  parse_record:
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
496
  	nr_slots = 0;
dcd8c53f1   OGAWA Hirofumi   fat: small optimi...
497
498
499
500
501
  	/*
  	 * Check for long filename entry, but if short_only, we don't
  	 * need to parse long filename.
  	 */
  	if (isvfat && !short_only) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
502
  		if (de->name[0] == DELETED_FLAG)
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
503
  			goto record_end;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
504
  		if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
505
  			goto record_end;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
506
  		if (de->attr != ATTR_EXT && IS_FREE(de->name))
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
507
  			goto record_end;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
508
509
  	} else {
  		if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
510
  			goto record_end;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
511
512
513
  	}
  
  	if (isvfat && de->attr == ATTR_EXT) {
ad2c1604d   Pekka Enberg   [PATCH] fat: Remo...
514
  		int status = fat_parse_long(inode, &cpos, &bh, &de,
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
515
  					    &unicode, &nr_slots);
ad2c1604d   Pekka Enberg   [PATCH] fat: Remo...
516
517
518
519
520
  		if (status < 0) {
  			filp->f_pos = cpos;
  			ret = status;
  			goto out;
  		} else if (status == PARSE_INVALID)
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
521
  			goto record_end;
ad2c1604d   Pekka Enberg   [PATCH] fat: Remo...
522
523
524
  		else if (status == PARSE_NOT_LONGNAME)
  			goto parse_record;
  		else if (status == PARSE_EOF)
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
525
  			goto end_of_dir;
dcd8c53f1   OGAWA Hirofumi   fat: small optimi...
526
527
528
529
  
  		if (nr_slots) {
  			void *longname = unicode + FAT_MAX_UNI_CHARS;
  			int size = PATH_MAX - FAT_MAX_UNI_SIZE;
869f58c0c   Alexey Fisher   fat: Replace all ...
530
  			int len = fat_uni_to_x8(sb, unicode, longname, size);
dcd8c53f1   OGAWA Hirofumi   fat: small optimi...
531
532
533
534
535
536
537
  
  			fill_name = longname;
  			fill_len = len;
  			/* !both && !short_only, so we don't need shortname. */
  			if (!both)
  				goto start_filldir;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
  	}
  
  	if (sbi->options.dotsOK) {
  		ptname = bufname;
  		dotoffset = 0;
  		if (de->attr & ATTR_HIDDEN) {
  			*ptname++ = '.';
  			dotoffset = 1;
  		}
  	}
  
  	memcpy(work, de->name, sizeof(de->name));
  	/* see namei.c, msdos_format_name */
  	if (work[0] == 0x05)
  		work[0] = 0xE5;
  	for (i = 0, j = 0, last = 0, last_u = 0; i < 8;) {
9aacd5993   OGAWA Hirofumi   fat: gcc 4.3 warn...
554
555
  		if (!(c = work[i]))
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
  		chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
  					&bufuname[j++], opt_shortname,
  					de->lcase & CASE_LOWER_BASE);
  		if (chl <= 1) {
  			ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
  			if (c != ' ') {
  				last = i;
  				last_u = j;
  			}
  		} else {
  			last_u = j;
  			for (chi = 0; chi < chl && i < 8; chi++) {
  				ptname[i] = work[i];
  				i++; last = i;
  			}
  		}
  	}
  	i = last;
  	j = last_u;
  	fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
  	ptname[i++] = '.';
9aacd5993   OGAWA Hirofumi   fat: gcc 4.3 warn...
577
578
579
580
  	for (i2 = 8; i2 < MSDOS_NAME;) {
  		if (!(c = work[i2]))
  			break;
  		chl = fat_shortname2uni(nls_disk, &work[i2], MSDOS_NAME - i2,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
581
582
583
584
585
586
587
588
589
590
591
  					&bufuname[j++], opt_shortname,
  					de->lcase & CASE_LOWER_EXT);
  		if (chl <= 1) {
  			i2++;
  			ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
  			if (c != ' ') {
  				last = i;
  				last_u = j;
  			}
  		} else {
  			last_u = j;
9aacd5993   OGAWA Hirofumi   fat: gcc 4.3 warn...
592
593
  			for (chi = 0; chi < chl && i2 < MSDOS_NAME; chi++) {
  				ptname[i++] = work[i2++];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
594
595
596
597
598
  				last = i;
  			}
  		}
  	}
  	if (!last)
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
599
  		goto record_end;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
600
601
602
  
  	i = last + dotoffset;
  	j = last_u;
dcd8c53f1   OGAWA Hirofumi   fat: small optimi...
603
604
  	if (isvfat) {
  		bufuname[j] = 0x0000;
869f58c0c   Alexey Fisher   fat: Replace all ...
605
  		i = fat_uni_to_x8(sb, bufuname, bufname, sizeof(bufname));
dcd8c53f1   OGAWA Hirofumi   fat: small optimi...
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
  	}
  	if (nr_slots) {
  		/* hack for fat_ioctl_filldir() */
  		struct fat_ioctl_filldir_callback *p = dirent;
  
  		p->longname = fill_name;
  		p->long_len = fill_len;
  		p->shortname = bufname;
  		p->short_len = i;
  		fill_name = NULL;
  		fill_len = 0;
  	} else {
  		fill_name = bufname;
  		fill_len = i;
  	}
  
  start_filldir:
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
623
  	lpos = cpos - (nr_slots + 1) * sizeof(struct msdos_dir_entry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
624
625
626
  	if (!memcmp(de->name, MSDOS_DOT, MSDOS_NAME))
  		inum = inode->i_ino;
  	else if (!memcmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
dba323060   Josef "Jeff" Sipek   [PATCH] fat: chan...
627
  		inum = parent_ino(filp->f_path.dentry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
628
629
630
631
632
633
634
635
636
  	} else {
  		loff_t i_pos = fat_make_i_pos(sb, bh, de);
  		struct inode *tmp = fat_iget(sb, i_pos);
  		if (tmp) {
  			inum = tmp->i_ino;
  			iput(tmp);
  		} else
  			inum = iunique(sb, MSDOS_ROOT_INO);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
637
638
  	if (filldir(dirent, fill_name, fill_len, *furrfu, inum,
  		    (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
639
  		goto fill_failed;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
640

d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
641
  record_end:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
642
643
  	furrfu = &lpos;
  	filp->f_pos = cpos;
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
644
645
  	goto get_new;
  end_of_dir:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
646
  	filp->f_pos = cpos;
d68861167   OGAWA Hirofumi   fat: cleanup fs/f...
647
  fill_failed:
f3ef6f63e   Karsten Wiese   [PATCH] Speedup F...
648
  	brelse(bh);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
649
  	if (unicode)
c7a6c4edc   OGAWA Hirofumi   fat: use __getname()
650
  		__putname(unicode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
651
  out:
8f5934278   Linus Torvalds   Replace BKL with ...
652
  	unlock_super(sb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
653
654
655
656
657
  	return ret;
  }
  
  static int fat_readdir(struct file *filp, void *dirent, filldir_t filldir)
  {
dba323060   Josef "Jeff" Sipek   [PATCH] fat: chan...
658
  	struct inode *inode = filp->f_path.dentry->d_inode;
ad2c1604d   Pekka Enberg   [PATCH] fat: Remo...
659
  	return __fat_readdir(inode, filp, dirent, filldir, 0, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
660
  }
c483bab09   OGAWA Hirofumi   fat: fix VFAT com...
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
  #define FAT_IOCTL_FILLDIR_FUNC(func, dirent_type)			   \
  static int func(void *__buf, const char *name, int name_len,		   \
  			     loff_t offset, u64 ino, unsigned int d_type)  \
  {									   \
  	struct fat_ioctl_filldir_callback *buf = __buf;			   \
  	struct dirent_type __user *d1 = buf->dirent;			   \
  	struct dirent_type __user *d2 = d1 + 1;				   \
  									   \
  	if (buf->result)						   \
  		return -EINVAL;						   \
  	buf->result++;							   \
  									   \
  	if (name != NULL) {						   \
  		/* dirent has only short name */			   \
  		if (name_len >= sizeof(d1->d_name))			   \
  			name_len = sizeof(d1->d_name) - 1;		   \
  									   \
  		if (put_user(0, d2->d_name)			||	   \
  		    put_user(0, &d2->d_reclen)			||	   \
  		    copy_to_user(d1->d_name, name, name_len)	||	   \
  		    put_user(0, d1->d_name + name_len)		||	   \
  		    put_user(name_len, &d1->d_reclen))			   \
  			goto efault;					   \
  	} else {							   \
  		/* dirent has short and long name */			   \
  		const char *longname = buf->longname;			   \
  		int long_len = buf->long_len;				   \
  		const char *shortname = buf->shortname;			   \
  		int short_len = buf->short_len;				   \
  									   \
  		if (long_len >= sizeof(d1->d_name))			   \
  			long_len = sizeof(d1->d_name) - 1;		   \
  		if (short_len >= sizeof(d1->d_name))			   \
  			short_len = sizeof(d1->d_name) - 1;		   \
  									   \
  		if (copy_to_user(d2->d_name, longname, long_len)	|| \
  		    put_user(0, d2->d_name + long_len)			|| \
  		    put_user(long_len, &d2->d_reclen)			|| \
  		    put_user(ino, &d2->d_ino)				|| \
  		    put_user(offset, &d2->d_off)			|| \
  		    copy_to_user(d1->d_name, shortname, short_len)	|| \
  		    put_user(0, d1->d_name + short_len)			|| \
  		    put_user(short_len, &d1->d_reclen))			   \
  			goto efault;					   \
  	}								   \
  	return 0;							   \
  efault:									   \
  	buf->result = -EFAULT;						   \
  	return -EFAULT;							   \
  }
531f710f8   Adrian Bunk   fat/dir.c: switch...
711
  FAT_IOCTL_FILLDIR_FUNC(fat_ioctl_filldir, __fat_dirent)
c483bab09   OGAWA Hirofumi   fat: fix VFAT com...
712
713
714
715
  
  static int fat_ioctl_readdir(struct inode *inode, struct file *filp,
  			     void __user *dirent, filldir_t filldir,
  			     int short_only, int both)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
716
  {
c483bab09   OGAWA Hirofumi   fat: fix VFAT com...
717
718
719
720
721
722
723
724
725
726
  	struct fat_ioctl_filldir_callback buf;
  	int ret;
  
  	buf.dirent = dirent;
  	buf.result = 0;
  	mutex_lock(&inode->i_mutex);
  	ret = -ENOENT;
  	if (!IS_DEADDIR(inode)) {
  		ret = __fat_readdir(inode, filp, &buf, filldir,
  				    short_only, both);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
727
  	}
c483bab09   OGAWA Hirofumi   fat: fix VFAT com...
728
729
730
731
  	mutex_unlock(&inode->i_mutex);
  	if (ret >= 0)
  		ret = buf.result;
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
732
  }
7845bc3e1   Arnd Bergmann   fat: convert to u...
733
734
  static long fat_dir_ioctl(struct file *filp, unsigned int cmd,
  			  unsigned long arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
735
  {
7845bc3e1   Arnd Bergmann   fat: convert to u...
736
  	struct inode *inode = filp->f_path.dentry->d_inode;
531f710f8   Adrian Bunk   fat/dir.c: switch...
737
  	struct __fat_dirent __user *d1 = (struct __fat_dirent __user *)arg;
c483bab09   OGAWA Hirofumi   fat: fix VFAT com...
738
  	int short_only, both;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
739
740
741
742
743
744
745
746
747
748
749
  
  	switch (cmd) {
  	case VFAT_IOCTL_READDIR_SHORT:
  		short_only = 1;
  		both = 0;
  		break;
  	case VFAT_IOCTL_READDIR_BOTH:
  		short_only = 0;
  		both = 1;
  		break;
  	default:
7845bc3e1   Arnd Bergmann   fat: convert to u...
750
  		return fat_generic_ioctl(filp, cmd, arg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
751
  	}
531f710f8   Adrian Bunk   fat/dir.c: switch...
752
  	if (!access_ok(VERIFY_WRITE, d1, sizeof(struct __fat_dirent[2])))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
753
754
755
756
757
758
759
760
  		return -EFAULT;
  	/*
  	 * Yes, we don't need this put_user() absolutely. However old
  	 * code didn't return the right value. So, app use this value,
  	 * in order to check whether it is EOF.
  	 */
  	if (put_user(0, &d1->d_reclen))
  		return -EFAULT;
c483bab09   OGAWA Hirofumi   fat: fix VFAT com...
761
762
  	return fat_ioctl_readdir(inode, filp, d1, fat_ioctl_filldir,
  				 short_only, both);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
763
  }
188f83dfe   David Howells   [PATCH] BLOCK: Mo...
764
765
766
  #ifdef CONFIG_COMPAT
  #define	VFAT_IOCTL_READDIR_BOTH32	_IOR('r', 1, struct compat_dirent[2])
  #define	VFAT_IOCTL_READDIR_SHORT32	_IOR('r', 2, struct compat_dirent[2])
c483bab09   OGAWA Hirofumi   fat: fix VFAT com...
767
  FAT_IOCTL_FILLDIR_FUNC(fat_compat_ioctl_filldir, compat_dirent)
188f83dfe   David Howells   [PATCH] BLOCK: Mo...
768

c483bab09   OGAWA Hirofumi   fat: fix VFAT com...
769
  static long fat_compat_dir_ioctl(struct file *filp, unsigned cmd,
188f83dfe   David Howells   [PATCH] BLOCK: Mo...
770
771
  				 unsigned long arg)
  {
c483bab09   OGAWA Hirofumi   fat: fix VFAT com...
772
773
774
  	struct inode *inode = filp->f_path.dentry->d_inode;
  	struct compat_dirent __user *d1 = compat_ptr(arg);
  	int short_only, both;
188f83dfe   David Howells   [PATCH] BLOCK: Mo...
775
776
  
  	switch (cmd) {
188f83dfe   David Howells   [PATCH] BLOCK: Mo...
777
  	case VFAT_IOCTL_READDIR_SHORT32:
c483bab09   OGAWA Hirofumi   fat: fix VFAT com...
778
779
780
781
782
783
  		short_only = 1;
  		both = 0;
  		break;
  	case VFAT_IOCTL_READDIR_BOTH32:
  		short_only = 0;
  		both = 1;
188f83dfe   David Howells   [PATCH] BLOCK: Mo...
784
785
  		break;
  	default:
7845bc3e1   Arnd Bergmann   fat: convert to u...
786
  		return fat_generic_ioctl(filp, cmd, (unsigned long)arg);
188f83dfe   David Howells   [PATCH] BLOCK: Mo...
787
  	}
c483bab09   OGAWA Hirofumi   fat: fix VFAT com...
788
789
790
791
792
793
794
795
796
797
798
799
  	if (!access_ok(VERIFY_WRITE, d1, sizeof(struct compat_dirent[2])))
  		return -EFAULT;
  	/*
  	 * Yes, we don't need this put_user() absolutely. However old
  	 * code didn't return the right value. So, app use this value,
  	 * in order to check whether it is EOF.
  	 */
  	if (put_user(0, &d1->d_reclen))
  		return -EFAULT;
  
  	return fat_ioctl_readdir(inode, filp, d1, fat_compat_ioctl_filldir,
  				 short_only, both);
188f83dfe   David Howells   [PATCH] BLOCK: Mo...
800
801
  }
  #endif /* CONFIG_COMPAT */
4b6f5d20b   Arjan van de Ven   [PATCH] Make most...
802
  const struct file_operations fat_dir_operations = {
53472bc8f   OGAWA Hirofumi   fat: use generic_...
803
  	.llseek		= generic_file_llseek,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
804
805
  	.read		= generic_read_dir,
  	.readdir	= fat_readdir,
7845bc3e1   Arnd Bergmann   fat: convert to u...
806
  	.unlocked_ioctl	= fat_dir_ioctl,
188f83dfe   David Howells   [PATCH] BLOCK: Mo...
807
808
809
  #ifdef CONFIG_COMPAT
  	.compat_ioctl	= fat_compat_dir_ioctl,
  #endif
b522412ae   Al Viro   Sanitize ->fsync(...
810
  	.fsync		= fat_file_fsync,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
  };
  
  static int fat_get_short_entry(struct inode *dir, loff_t *pos,
  			       struct buffer_head **bh,
  			       struct msdos_dir_entry **de)
  {
  	while (fat_get_entry(dir, pos, bh, de) >= 0) {
  		/* free entry or long name entry or volume label */
  		if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME))
  			return 0;
  	}
  	return -ENOENT;
  }
  
  /*
   * The ".." entry can not provide the "struct fat_slot_info" informations
   * for inode. So, this function provide the some informations only.
   */
  int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh,
  			 struct msdos_dir_entry **de, loff_t *i_pos)
  {
  	loff_t offset;
  
  	offset = 0;
  	*bh = NULL;
  	while (fat_get_short_entry(dir, &offset, bh, de) >= 0) {
  		if (!strncmp((*de)->name, MSDOS_DOTDOT, MSDOS_NAME)) {
  			*i_pos = fat_make_i_pos(dir->i_sb, *bh, *de);
  			return 0;
  		}
  	}
  	return -ENOENT;
  }
7c709d00d   OGAWA Hirofumi   [PATCH] fat: s/EX...
844
  EXPORT_SYMBOL_GPL(fat_get_dotdot_entry);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
  
  /* See if directory is empty */
  int fat_dir_empty(struct inode *dir)
  {
  	struct buffer_head *bh;
  	struct msdos_dir_entry *de;
  	loff_t cpos;
  	int result = 0;
  
  	bh = NULL;
  	cpos = 0;
  	while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
  		if (strncmp(de->name, MSDOS_DOT   , MSDOS_NAME) &&
  		    strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
  			result = -ENOTEMPTY;
  			break;
  		}
  	}
  	brelse(bh);
  	return result;
  }
7c709d00d   OGAWA Hirofumi   [PATCH] fat: s/EX...
866
  EXPORT_SYMBOL_GPL(fat_dir_empty);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
  
  /*
   * fat_subdirs counts the number of sub-directories of dir. It can be run
   * on directories being created.
   */
  int fat_subdirs(struct inode *dir)
  {
  	struct buffer_head *bh;
  	struct msdos_dir_entry *de;
  	loff_t cpos;
  	int count = 0;
  
  	bh = NULL;
  	cpos = 0;
  	while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
  		if (de->attr & ATTR_DIR)
  			count++;
  	}
  	brelse(bh);
  	return count;
  }
  
  /*
   * Scans a directory for a given file (name points to its formatted name).
   * Returns an error code or zero.
   */
  int fat_scan(struct inode *dir, const unsigned char *name,
  	     struct fat_slot_info *sinfo)
  {
  	struct super_block *sb = dir->i_sb;
  
  	sinfo->slot_off = 0;
  	sinfo->bh = NULL;
  	while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh,
  				   &sinfo->de) >= 0) {
  		if (!strncmp(sinfo->de->name, name, MSDOS_NAME)) {
  			sinfo->slot_off -= sizeof(*sinfo->de);
  			sinfo->nr_slots = 1;
  			sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
  			return 0;
  		}
  	}
  	return -ENOENT;
  }
7c709d00d   OGAWA Hirofumi   [PATCH] fat: s/EX...
911
  EXPORT_SYMBOL_GPL(fat_scan);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
  
  static int __fat_remove_entries(struct inode *dir, loff_t pos, int nr_slots)
  {
  	struct super_block *sb = dir->i_sb;
  	struct buffer_head *bh;
  	struct msdos_dir_entry *de, *endp;
  	int err = 0, orig_slots;
  
  	while (nr_slots) {
  		bh = NULL;
  		if (fat_get_entry(dir, &pos, &bh, &de) < 0) {
  			err = -EIO;
  			break;
  		}
  
  		orig_slots = nr_slots;
  		endp = (struct msdos_dir_entry *)(bh->b_data + sb->s_blocksize);
  		while (nr_slots && de < endp) {
  			de->name[0] = DELETED_FLAG;
  			de++;
  			nr_slots--;
  		}
b522412ae   Al Viro   Sanitize ->fsync(...
934
  		mark_buffer_dirty_inode(bh, dir);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
  		if (IS_DIRSYNC(dir))
  			err = sync_dirty_buffer(bh);
  		brelse(bh);
  		if (err)
  			break;
  
  		/* pos is *next* de's position, so this does `- sizeof(de)' */
  		pos += ((orig_slots - nr_slots) * sizeof(*de)) - sizeof(*de);
  	}
  
  	return err;
  }
  
  int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo)
  {
869f58c0c   Alexey Fisher   fat: Replace all ...
950
  	struct super_block *sb = dir->i_sb;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
  	struct msdos_dir_entry *de;
  	struct buffer_head *bh;
  	int err = 0, nr_slots;
  
  	/*
  	 * First stage: Remove the shortname. By this, the directory
  	 * entry is removed.
  	 */
  	nr_slots = sinfo->nr_slots;
  	de = sinfo->de;
  	sinfo->de = NULL;
  	bh = sinfo->bh;
  	sinfo->bh = NULL;
  	while (nr_slots && de >= (struct msdos_dir_entry *)bh->b_data) {
  		de->name[0] = DELETED_FLAG;
  		de--;
  		nr_slots--;
  	}
b522412ae   Al Viro   Sanitize ->fsync(...
969
  	mark_buffer_dirty_inode(bh, dir);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
  	if (IS_DIRSYNC(dir))
  		err = sync_dirty_buffer(bh);
  	brelse(bh);
  	if (err)
  		return err;
  	dir->i_version++;
  
  	if (nr_slots) {
  		/*
  		 * Second stage: remove the remaining longname slots.
  		 * (This directory entry is already removed, and so return
  		 * the success)
  		 */
  		err = __fat_remove_entries(dir, sinfo->slot_off, nr_slots);
  		if (err) {
869f58c0c   Alexey Fisher   fat: Replace all ...
985
986
  			fat_msg(sb, KERN_WARNING,
  			       "Couldn't remove the long name slots");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
987
988
989
990
991
992
993
994
995
996
997
  		}
  	}
  
  	dir->i_mtime = dir->i_atime = CURRENT_TIME_SEC;
  	if (IS_DIRSYNC(dir))
  		(void)fat_sync_inode(dir);
  	else
  		mark_inode_dirty(dir);
  
  	return 0;
  }
7c709d00d   OGAWA Hirofumi   [PATCH] fat: s/EX...
998
  EXPORT_SYMBOL_GPL(fat_remove_entries);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
  
  static int fat_zeroed_cluster(struct inode *dir, sector_t blknr, int nr_used,
  			      struct buffer_head **bhs, int nr_bhs)
  {
  	struct super_block *sb = dir->i_sb;
  	sector_t last_blknr = blknr + MSDOS_SB(sb)->sec_per_clus;
  	int err, i, n;
  
  	/* Zeroing the unused blocks on this cluster */
  	blknr += nr_used;
  	n = nr_used;
  	while (blknr < last_blknr) {
  		bhs[n] = sb_getblk(sb, blknr);
  		if (!bhs[n]) {
  			err = -ENOMEM;
  			goto error;
  		}
  		memset(bhs[n]->b_data, 0, sb->s_blocksize);
  		set_buffer_uptodate(bhs[n]);
b522412ae   Al Viro   Sanitize ->fsync(...
1018
  		mark_buffer_dirty_inode(bhs[n], dir);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
  
  		n++;
  		blknr++;
  		if (n == nr_bhs) {
  			if (IS_DIRSYNC(dir)) {
  				err = fat_sync_bhs(bhs, n);
  				if (err)
  					goto error;
  			}
  			for (i = 0; i < n; i++)
  				brelse(bhs[i]);
  			n = 0;
  		}
  	}
  	if (IS_DIRSYNC(dir)) {
  		err = fat_sync_bhs(bhs, n);
  		if (err)
  			goto error;
  	}
  	for (i = 0; i < n; i++)
  		brelse(bhs[i]);
  
  	return 0;
  
  error:
  	for (i = 0; i < n; i++)
  		bforget(bhs[i]);
  	return err;
  }
  
  int fat_alloc_new_dir(struct inode *dir, struct timespec *ts)
  {
  	struct super_block *sb = dir->i_sb;
  	struct msdos_sb_info *sbi = MSDOS_SB(sb);
  	struct buffer_head *bhs[MAX_BUF_PER_PAGE];
  	struct msdos_dir_entry *de;
  	sector_t blknr;
  	__le16 date, time;
7decd1cb0   OGAWA Hirofumi   fat: Fix and clea...
1057
  	u8 time_cs;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
  	int err, cluster;
  
  	err = fat_alloc_clusters(dir, &cluster, 1);
  	if (err)
  		goto error;
  
  	blknr = fat_clus_to_blknr(sbi, cluster);
  	bhs[0] = sb_getblk(sb, blknr);
  	if (!bhs[0]) {
  		err = -ENOMEM;
  		goto error_free;
  	}
7decd1cb0   OGAWA Hirofumi   fat: Fix and clea...
1070
  	fat_time_unix2fat(sbi, ts, &time, &date, &time_cs);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1071
1072
1073
1074
1075
1076
1077
1078
1079
  
  	de = (struct msdos_dir_entry *)bhs[0]->b_data;
  	/* filling the new directory slots ("." and ".." entries) */
  	memcpy(de[0].name, MSDOS_DOT, MSDOS_NAME);
  	memcpy(de[1].name, MSDOS_DOTDOT, MSDOS_NAME);
  	de->attr = de[1].attr = ATTR_DIR;
  	de[0].lcase = de[1].lcase = 0;
  	de[0].time = de[1].time = time;
  	de[0].date = de[1].date = date;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1080
1081
1082
  	if (sbi->options.isvfat) {
  		/* extra timestamps */
  		de[0].ctime = de[1].ctime = time;
7decd1cb0   OGAWA Hirofumi   fat: Fix and clea...
1083
  		de[0].ctime_cs = de[1].ctime_cs = time_cs;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1084
1085
1086
  		de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = date;
  	} else {
  		de[0].ctime = de[1].ctime = 0;
7decd1cb0   OGAWA Hirofumi   fat: Fix and clea...
1087
  		de[0].ctime_cs = de[1].ctime_cs = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1088
1089
1090
1091
1092
1093
1094
1095
1096
  		de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = 0;
  	}
  	de[0].start = cpu_to_le16(cluster);
  	de[0].starthi = cpu_to_le16(cluster >> 16);
  	de[1].start = cpu_to_le16(MSDOS_I(dir)->i_logstart);
  	de[1].starthi = cpu_to_le16(MSDOS_I(dir)->i_logstart >> 16);
  	de[0].size = de[1].size = 0;
  	memset(de + 2, 0, sb->s_blocksize - 2 * sizeof(*de));
  	set_buffer_uptodate(bhs[0]);
b522412ae   Al Viro   Sanitize ->fsync(...
1097
  	mark_buffer_dirty_inode(bhs[0], dir);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
  
  	err = fat_zeroed_cluster(dir, blknr, 1, bhs, MAX_BUF_PER_PAGE);
  	if (err)
  		goto error_free;
  
  	return cluster;
  
  error_free:
  	fat_free_clusters(dir, cluster);
  error:
  	return err;
  }
7c709d00d   OGAWA Hirofumi   [PATCH] fat: s/EX...
1110
  EXPORT_SYMBOL_GPL(fat_alloc_new_dir);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
  
  static int fat_add_new_entries(struct inode *dir, void *slots, int nr_slots,
  			       int *nr_cluster, struct msdos_dir_entry **de,
  			       struct buffer_head **bh, loff_t *i_pos)
  {
  	struct super_block *sb = dir->i_sb;
  	struct msdos_sb_info *sbi = MSDOS_SB(sb);
  	struct buffer_head *bhs[MAX_BUF_PER_PAGE];
  	sector_t blknr, start_blknr, last_blknr;
  	unsigned long size, copy;
  	int err, i, n, offset, cluster[2];
  
  	/*
  	 * The minimum cluster size is 512bytes, and maximum entry
  	 * size is 32*slots (672bytes).  So, iff the cluster size is
  	 * 512bytes, we may need two clusters.
  	 */
  	size = nr_slots * sizeof(struct msdos_dir_entry);
  	*nr_cluster = (size + (sbi->cluster_size - 1)) >> sbi->cluster_bits;
  	BUG_ON(*nr_cluster > 2);
  
  	err = fat_alloc_clusters(dir, cluster, *nr_cluster);
  	if (err)
  		goto error;
  
  	/*
  	 * First stage: Fill the directory entry.  NOTE: This cluster
  	 * is not referenced from any inode yet, so updates order is
  	 * not important.
  	 */
  	i = n = copy = 0;
  	do {
  		start_blknr = blknr = fat_clus_to_blknr(sbi, cluster[i]);
  		last_blknr = start_blknr + sbi->sec_per_clus;
  		while (blknr < last_blknr) {
  			bhs[n] = sb_getblk(sb, blknr);
  			if (!bhs[n]) {
  				err = -ENOMEM;
  				goto error_nomem;
  			}
  
  			/* fill the directory entry */
  			copy = min(size, sb->s_blocksize);
  			memcpy(bhs[n]->b_data, slots, copy);
  			slots += copy;
  			size -= copy;
  			set_buffer_uptodate(bhs[n]);
b522412ae   Al Viro   Sanitize ->fsync(...
1158
  			mark_buffer_dirty_inode(bhs[n], dir);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
  			if (!size)
  				break;
  			n++;
  			blknr++;
  		}
  	} while (++i < *nr_cluster);
  
  	memset(bhs[n]->b_data + copy, 0, sb->s_blocksize - copy);
  	offset = copy - sizeof(struct msdos_dir_entry);
  	get_bh(bhs[n]);
  	*bh = bhs[n];
  	*de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
  	*i_pos = fat_make_i_pos(sb, *bh, *de);
  
  	/* Second stage: clear the rest of cluster, and write outs */
  	err = fat_zeroed_cluster(dir, start_blknr, ++n, bhs, MAX_BUF_PER_PAGE);
  	if (err)
  		goto error_free;
  
  	return cluster[0];
  
  error_free:
  	brelse(*bh);
  	*bh = NULL;
  	n = 0;
  error_nomem:
  	for (i = 0; i < n; i++)
  		bforget(bhs[i]);
  	fat_free_clusters(dir, cluster[0]);
  error:
  	return err;
  }
  
  int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
  		    struct fat_slot_info *sinfo)
  {
  	struct super_block *sb = dir->i_sb;
  	struct msdos_sb_info *sbi = MSDOS_SB(sb);
  	struct buffer_head *bh, *prev, *bhs[3]; /* 32*slots (672bytes) */
8c320c079   Jonas Aberg   fat: fix build wa...
1198
  	struct msdos_dir_entry *uninitialized_var(de);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
  	int err, free_slots, i, nr_bhs;
  	loff_t pos, i_pos;
  
  	sinfo->nr_slots = nr_slots;
  
  	/* First stage: search free direcotry entries */
  	free_slots = nr_bhs = 0;
  	bh = prev = NULL;
  	pos = 0;
  	err = -ENOSPC;
  	while (fat_get_entry(dir, &pos, &bh, &de) > -1) {
  		/* check the maximum size of directory */
  		if (pos >= FAT_MAX_DIR_SIZE)
  			goto error;
  
  		if (IS_FREE(de->name)) {
  			if (prev != bh) {
  				get_bh(bh);
  				bhs[nr_bhs] = prev = bh;
  				nr_bhs++;
  			}
  			free_slots++;
  			if (free_slots == nr_slots)
  				goto found;
  		} else {
  			for (i = 0; i < nr_bhs; i++)
  				brelse(bhs[i]);
  			prev = NULL;
  			free_slots = nr_bhs = 0;
  		}
  	}
  	if (dir->i_ino == MSDOS_ROOT_INO) {
  		if (sbi->fat_bits != 32)
  			goto error;
  	} else if (MSDOS_I(dir)->i_start == 0) {
869f58c0c   Alexey Fisher   fat: Replace all ...
1234
  		fat_msg(sb, KERN_ERR, "Corrupted directory (i_pos %lld)",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
  		       MSDOS_I(dir)->i_pos);
  		err = -EIO;
  		goto error;
  	}
  
  found:
  	err = 0;
  	pos -= free_slots * sizeof(*de);
  	nr_slots -= free_slots;
  	if (free_slots) {
  		/*
  		 * Second stage: filling the free entries with new entries.
  		 * NOTE: If this slots has shortname, first, we write
  		 * the long name slots, then write the short name.
  		 */
  		int size = free_slots * sizeof(*de);
  		int offset = pos & (sb->s_blocksize - 1);
  		int long_bhs = nr_bhs - (nr_slots == 0);
  
  		/* Fill the long name slots. */
  		for (i = 0; i < long_bhs; i++) {
  			int copy = min_t(int, sb->s_blocksize - offset, size);
  			memcpy(bhs[i]->b_data + offset, slots, copy);
b522412ae   Al Viro   Sanitize ->fsync(...
1258
  			mark_buffer_dirty_inode(bhs[i], dir);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
  			offset = 0;
  			slots += copy;
  			size -= copy;
  		}
  		if (long_bhs && IS_DIRSYNC(dir))
  			err = fat_sync_bhs(bhs, long_bhs);
  		if (!err && i < nr_bhs) {
  			/* Fill the short name slot. */
  			int copy = min_t(int, sb->s_blocksize - offset, size);
  			memcpy(bhs[i]->b_data + offset, slots, copy);
b522412ae   Al Viro   Sanitize ->fsync(...
1269
  			mark_buffer_dirty_inode(bhs[i], dir);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
  			if (IS_DIRSYNC(dir))
  				err = sync_dirty_buffer(bhs[i]);
  		}
  		for (i = 0; i < nr_bhs; i++)
  			brelse(bhs[i]);
  		if (err)
  			goto error_remove;
  	}
  
  	if (nr_slots) {
  		int cluster, nr_cluster;
  
  		/*
  		 * Third stage: allocate the cluster for new entries.
  		 * And initialize the cluster with new entries, then
  		 * add the cluster to dir.
  		 */
  		cluster = fat_add_new_entries(dir, slots, nr_slots, &nr_cluster,
  					      &de, &bh, &i_pos);
  		if (cluster < 0) {
  			err = cluster;
  			goto error_remove;
  		}
  		err = fat_chain_add(dir, cluster, nr_cluster);
  		if (err) {
  			fat_free_clusters(dir, cluster);
  			goto error_remove;
  		}
  		if (dir->i_size & (sbi->cluster_size - 1)) {
85c785919   Denis Karpov   FAT: add 'errors'...
1299
  			fat_fs_error(sb, "Odd directory size");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
  			dir->i_size = (dir->i_size + sbi->cluster_size - 1)
  				& ~((loff_t)sbi->cluster_size - 1);
  		}
  		dir->i_size += nr_cluster << sbi->cluster_bits;
  		MSDOS_I(dir)->mmu_private += nr_cluster << sbi->cluster_bits;
  	}
  	sinfo->slot_off = pos;
  	sinfo->de = de;
  	sinfo->bh = bh;
  	sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
  
  	return 0;
  
  error:
  	brelse(bh);
  	for (i = 0; i < nr_bhs; i++)
  		brelse(bhs[i]);
  	return err;
  
  error_remove:
  	brelse(bh);
  	if (free_slots)
  		__fat_remove_entries(dir, pos, free_slots);
  	return err;
  }
7c709d00d   OGAWA Hirofumi   [PATCH] fat: s/EX...
1325
  EXPORT_SYMBOL_GPL(fat_add_entries);