blob: 486725ee99ae68a977bc9ca2d8e8cd47990ed579 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
David Howells188f83d2006-08-31 12:50:04 +020023#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/uaccess.h>
25
26static 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
Karsten Wiesef3ef6f62005-09-06 15:17:12 -070034static 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
OGAWA Hirofumi83b7c992006-01-08 01:02:09 -080049 bh = sb_find_get_block(sb, phys);
50 if (bh == NULL || !buffer_uptodate(bh)) {
Karsten Wiesef3ef6f62005-09-06 15:17:12 -070051 for (sec = 0; sec < sbi->sec_per_clus; sec++)
52 sb_breadahead(sb, phys + sec);
53 }
54 brelse(bh);
55}
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/* 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 */
67static 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;
OGAWA Hirofumie5174ba2006-01-08 01:02:11 -080072 unsigned long mapped_blocks;
73 int err, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75next:
76 if (*bh)
77 brelse(*bh);
78
79 *bh = NULL;
80 iblock = *pos >> sb->s_blocksize_bits;
OGAWA Hirofumie5174ba2006-01-08 01:02:11 -080081 err = fat_bmap(dir, iblock, &phys, &mapped_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (err || !phys)
83 return -1; /* beyond EOF or error */
84
Karsten Wiesef3ef6f62005-09-06 15:17:12 -070085 fat_dir_readahead(dir, iblock, phys);
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 *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
103static 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/*
Alexey Dobriyan4de151d2006-03-22 00:13:35 +0100118 * Convert Unicode 16 to UTF-8, translated Unicode, or ASCII.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 * 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 */
Keith Mokf22032b2008-04-28 02:16:29 -0700127static int uni16_to_x8(unsigned char *ascii, wchar_t *uni, int len,
128 int uni_xlate, struct nls_table *nls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 wchar_t *ip, ec;
131 unsigned char *op, nc;
132 int charlen;
133 int k;
134
135 ip = uni;
136 op = ascii;
137
Keith Mokf22032b2008-04-28 02:16:29 -0700138 while (*ip && ((len - NLS_MAX_CHARSET_SIZE) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 ec = *ip++;
140 if ( (charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE)) > 0) {
141 op += charlen;
Keith Mokf22032b2008-04-28 02:16:29 -0700142 len -= charlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 } else {
144 if (uni_xlate == 1) {
145 *op = ':';
146 for (k = 4; k > 0; k--) {
147 nc = ec & 0xF;
148 op[k] = nc > 9 ? nc + ('a' - 10)
149 : nc + '0';
150 ec >>= 4;
151 }
152 op += 5;
Keith Mokf22032b2008-04-28 02:16:29 -0700153 len -= 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 } else {
155 *op++ = '?';
Keith Mokf22032b2008-04-28 02:16:29 -0700156 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
Keith Mokf22032b2008-04-28 02:16:29 -0700160
161 if (unlikely(*ip)) {
162 printk(KERN_WARNING "FAT: filename was truncated while "
163 "converting.");
164 }
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 *op = 0;
167 return (op - ascii);
168}
169
170static inline int
171fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
172{
173 int charlen;
174
175 charlen = t->char2uni(c, clen, uni);
176 if (charlen < 0) {
177 *uni = 0x003f; /* a question mark */
178 charlen = 1;
179 }
180 return charlen;
181}
182
183static inline int
184fat_short2lower_uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
185{
186 int charlen;
187 wchar_t wc;
188
189 charlen = t->char2uni(c, clen, &wc);
190 if (charlen < 0) {
191 *uni = 0x003f; /* a question mark */
192 charlen = 1;
193 } else if (charlen <= 1) {
194 unsigned char nc = t->charset2lower[*c];
195
196 if (!nc)
197 nc = *c;
198
199 if ( (charlen = t->char2uni(&nc, 1, uni)) < 0) {
200 *uni = 0x003f; /* a question mark */
201 charlen = 1;
202 }
203 } else
204 *uni = wc;
205
206 return charlen;
207}
208
209static inline int
210fat_shortname2uni(struct nls_table *nls, unsigned char *buf, int buf_size,
211 wchar_t *uni_buf, unsigned short opt, int lower)
212{
213 int len = 0;
214
215 if (opt & VFAT_SFN_DISPLAY_LOWER)
216 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
217 else if (opt & VFAT_SFN_DISPLAY_WIN95)
218 len = fat_short2uni(nls, buf, buf_size, uni_buf);
219 else if (opt & VFAT_SFN_DISPLAY_WINNT) {
220 if (lower)
221 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
222 else
223 len = fat_short2uni(nls, buf, buf_size, uni_buf);
224 } else
225 len = fat_short2uni(nls, buf, buf_size, uni_buf);
226
227 return len;
228}
229
Pekka Enbergad2c1602005-10-30 15:03:50 -0800230enum { PARSE_INVALID = 1, PARSE_NOT_LONGNAME, PARSE_EOF, };
231
232/**
233 * fat_parse_long - Parse extended directory entry.
234 *
235 * This function returns zero on success, negative value on error, or one of
236 * the following:
237 *
238 * %PARSE_INVALID - Directory entry is invalid.
239 * %PARSE_NOT_LONGNAME - Directory entry does not contain longname.
240 * %PARSE_EOF - Directory has no more entries.
241 */
242static int fat_parse_long(struct inode *dir, loff_t *pos,
243 struct buffer_head **bh, struct msdos_dir_entry **de,
244 wchar_t **unicode, unsigned char *nr_slots)
245{
246 struct msdos_dir_slot *ds;
247 unsigned char id, slot, slots, alias_checksum;
248
249 if (!*unicode) {
OGAWA Hirofumic7a6c4e2008-04-28 02:16:29 -0700250 *unicode = __getname();
Pekka Enbergad2c1602005-10-30 15:03:50 -0800251 if (!*unicode) {
252 brelse(*bh);
253 return -ENOMEM;
254 }
255 }
256parse_long:
257 slots = 0;
258 ds = (struct msdos_dir_slot *)*de;
259 id = ds->id;
260 if (!(id & 0x40))
261 return PARSE_INVALID;
262 slots = id & ~0x40;
263 if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */
264 return PARSE_INVALID;
265 *nr_slots = slots;
266 alias_checksum = ds->alias_checksum;
267
268 slot = slots;
269 while (1) {
270 int offset;
271
272 slot--;
273 offset = slot * 13;
274 fat16_towchar(*unicode + offset, ds->name0_4, 5);
275 fat16_towchar(*unicode + offset + 5, ds->name5_10, 6);
276 fat16_towchar(*unicode + offset + 11, ds->name11_12, 2);
277
278 if (ds->id & 0x40)
279 (*unicode)[offset + 13] = 0;
280 if (fat_get_entry(dir, pos, bh, de) < 0)
281 return PARSE_EOF;
282 if (slot == 0)
283 break;
284 ds = (struct msdos_dir_slot *)*de;
285 if (ds->attr != ATTR_EXT)
286 return PARSE_NOT_LONGNAME;
287 if ((ds->id & ~0x40) != slot)
288 goto parse_long;
289 if (ds->alias_checksum != alias_checksum)
290 goto parse_long;
291 }
292 if ((*de)->name[0] == DELETED_FLAG)
293 return PARSE_INVALID;
294 if ((*de)->attr == ATTR_EXT)
295 goto parse_long;
296 if (IS_FREE((*de)->name) || ((*de)->attr & ATTR_VOLUME))
297 return PARSE_INVALID;
298 if (fat_checksum((*de)->name) != alias_checksum)
299 *nr_slots = 0;
300
301 return 0;
302}
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304/*
305 * Return values: negative -> error, 0 -> not found, positive -> found,
306 * value is the total amount of slots, including the shortname entry.
307 */
308int fat_search_long(struct inode *inode, const unsigned char *name,
309 int name_len, struct fat_slot_info *sinfo)
310{
311 struct super_block *sb = inode->i_sb;
312 struct msdos_sb_info *sbi = MSDOS_SB(sb);
313 struct buffer_head *bh = NULL;
314 struct msdos_dir_entry *de;
315 struct nls_table *nls_io = sbi->nls_io;
316 struct nls_table *nls_disk = sbi->nls_disk;
317 wchar_t bufuname[14];
Keith Mokf22032b2008-04-28 02:16:29 -0700318 unsigned char nr_slots;
319 int xlate_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 wchar_t *unicode = NULL;
Keith Mokf22032b2008-04-28 02:16:29 -0700321 unsigned char work[MSDOS_NAME];
322 unsigned char *bufname = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 int uni_xlate = sbi->options.unicode_xlate;
324 int utf8 = sbi->options.utf8;
325 int anycase = (sbi->options.name_check != 's');
326 unsigned short opt_shortname = sbi->options.shortname;
327 loff_t cpos = 0;
328 int chl, i, j, last_u, err;
329
OGAWA Hirofumic7a6c4e2008-04-28 02:16:29 -0700330 bufname = __getname();
Keith Mokf22032b2008-04-28 02:16:29 -0700331 if (!bufname)
332 return -ENOMEM;
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 err = -ENOENT;
335 while(1) {
336 if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
337 goto EODir;
338parse_record:
339 nr_slots = 0;
340 if (de->name[0] == DELETED_FLAG)
341 continue;
342 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
343 continue;
344 if (de->attr != ATTR_EXT && IS_FREE(de->name))
345 continue;
346 if (de->attr == ATTR_EXT) {
Pekka Enbergad2c1602005-10-30 15:03:50 -0800347 int status = fat_parse_long(inode, &cpos, &bh, &de,
348 &unicode, &nr_slots);
349 if (status < 0)
350 return status;
351 else if (status == PARSE_INVALID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 continue;
Pekka Enbergad2c1602005-10-30 15:03:50 -0800353 else if (status == PARSE_NOT_LONGNAME)
354 goto parse_record;
355 else if (status == PARSE_EOF)
356 goto EODir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358
359 memcpy(work, de->name, sizeof(de->name));
360 /* see namei.c, msdos_format_name */
361 if (work[0] == 0x05)
362 work[0] = 0xE5;
363 for (i = 0, j = 0, last_u = 0; i < 8;) {
OGAWA Hirofumi9aacd592007-07-15 23:39:56 -0700364 if (!work[i])
365 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
367 &bufuname[j++], opt_shortname,
368 de->lcase & CASE_LOWER_BASE);
369 if (chl <= 1) {
370 if (work[i] != ' ')
371 last_u = j;
372 } else {
373 last_u = j;
374 }
375 i += chl;
376 }
377 j = last_u;
378 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
OGAWA Hirofumi9aacd592007-07-15 23:39:56 -0700379 for (i = 8; i < MSDOS_NAME;) {
380 if (!work[i])
381 break;
382 chl = fat_shortname2uni(nls_disk, &work[i],
383 MSDOS_NAME - i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 &bufuname[j++], opt_shortname,
385 de->lcase & CASE_LOWER_EXT);
386 if (chl <= 1) {
OGAWA Hirofumi9aacd592007-07-15 23:39:56 -0700387 if (work[i] != ' ')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 last_u = j;
389 } else {
390 last_u = j;
391 }
392 i += chl;
393 }
394 if (!last_u)
395 continue;
396
397 bufuname[last_u] = 0x0000;
398 xlate_len = utf8
OGAWA Hirofumic7a6c4e2008-04-28 02:16:29 -0700399 ?utf8_wcstombs(bufname, bufuname, PATH_MAX)
400 :uni16_to_x8(bufname, bufuname, PATH_MAX, uni_xlate, nls_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (xlate_len == name_len)
402 if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
403 (anycase && !nls_strnicmp(nls_io, name, bufname,
404 xlate_len)))
405 goto Found;
406
407 if (nr_slots) {
408 xlate_len = utf8
OGAWA Hirofumic7a6c4e2008-04-28 02:16:29 -0700409 ?utf8_wcstombs(bufname, unicode, PATH_MAX)
410 :uni16_to_x8(bufname, unicode, PATH_MAX, uni_xlate, nls_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (xlate_len != name_len)
412 continue;
413 if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
414 (anycase && !nls_strnicmp(nls_io, name, bufname,
415 xlate_len)))
416 goto Found;
417 }
418 }
419
420Found:
421 nr_slots++; /* include the de */
422 sinfo->slot_off = cpos - nr_slots * sizeof(*de);
423 sinfo->nr_slots = nr_slots;
424 sinfo->de = de;
425 sinfo->bh = bh;
426 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
427 err = 0;
428EODir:
Keith Mokf22032b2008-04-28 02:16:29 -0700429 if (bufname)
OGAWA Hirofumic7a6c4e2008-04-28 02:16:29 -0700430 __putname(bufname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (unicode)
OGAWA Hirofumic7a6c4e2008-04-28 02:16:29 -0700432 __putname(unicode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 return err;
435}
436
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -0800437EXPORT_SYMBOL_GPL(fat_search_long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439struct fat_ioctl_filldir_callback {
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700440 void __user *dirent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 int result;
442 /* for dir ioctl */
443 const char *longname;
444 int long_len;
445 const char *shortname;
446 int short_len;
447};
448
Pekka Enbergad2c1602005-10-30 15:03:50 -0800449static int __fat_readdir(struct inode *inode, struct file *filp, void *dirent,
450 filldir_t filldir, int short_only, int both)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 struct super_block *sb = inode->i_sb;
453 struct msdos_sb_info *sbi = MSDOS_SB(sb);
454 struct buffer_head *bh;
455 struct msdos_dir_entry *de;
456 struct nls_table *nls_io = sbi->nls_io;
457 struct nls_table *nls_disk = sbi->nls_disk;
458 unsigned char long_slots;
459 const char *fill_name;
460 int fill_len;
461 wchar_t bufuname[14];
462 wchar_t *unicode = NULL;
OGAWA Hirofumi9aacd592007-07-15 23:39:56 -0700463 unsigned char c, work[MSDOS_NAME], bufname[56], *ptname = bufname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 unsigned long lpos, dummy, *furrfu = &lpos;
465 int uni_xlate = sbi->options.unicode_xlate;
466 int isvfat = sbi->options.isvfat;
467 int utf8 = sbi->options.utf8;
468 int nocase = sbi->options.nocase;
469 unsigned short opt_shortname = sbi->options.shortname;
470 unsigned long inum;
471 int chi, chl, i, i2, j, last, last_u, dotoffset = 0;
472 loff_t cpos;
473 int ret = 0;
474
475 lock_kernel();
476
477 cpos = filp->f_pos;
478 /* Fake . and .. for the root directory. */
479 if (inode->i_ino == MSDOS_ROOT_INO) {
480 while (cpos < 2) {
481 if (filldir(dirent, "..", cpos+1, cpos, MSDOS_ROOT_INO, DT_DIR) < 0)
482 goto out;
483 cpos++;
484 filp->f_pos++;
485 }
486 if (cpos == 2) {
487 dummy = 2;
488 furrfu = &dummy;
489 cpos = 0;
490 }
491 }
492 if (cpos & (sizeof(struct msdos_dir_entry)-1)) {
493 ret = -ENOENT;
494 goto out;
495 }
496
497 bh = NULL;
498GetNew:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
500 goto EODir;
Pekka Enbergad2c1602005-10-30 15:03:50 -0800501parse_record:
502 long_slots = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 /* Check for long filename entry */
504 if (isvfat) {
505 if (de->name[0] == DELETED_FLAG)
506 goto RecEnd;
507 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
508 goto RecEnd;
509 if (de->attr != ATTR_EXT && IS_FREE(de->name))
510 goto RecEnd;
511 } else {
512 if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
513 goto RecEnd;
514 }
515
516 if (isvfat && de->attr == ATTR_EXT) {
Pekka Enbergad2c1602005-10-30 15:03:50 -0800517 int status = fat_parse_long(inode, &cpos, &bh, &de,
518 &unicode, &long_slots);
519 if (status < 0) {
520 filp->f_pos = cpos;
521 ret = status;
522 goto out;
523 } else if (status == PARSE_INVALID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 goto RecEnd;
Pekka Enbergad2c1602005-10-30 15:03:50 -0800525 else if (status == PARSE_NOT_LONGNAME)
526 goto parse_record;
527 else if (status == PARSE_EOF)
528 goto EODir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530
531 if (sbi->options.dotsOK) {
532 ptname = bufname;
533 dotoffset = 0;
534 if (de->attr & ATTR_HIDDEN) {
535 *ptname++ = '.';
536 dotoffset = 1;
537 }
538 }
539
540 memcpy(work, de->name, sizeof(de->name));
541 /* see namei.c, msdos_format_name */
542 if (work[0] == 0x05)
543 work[0] = 0xE5;
544 for (i = 0, j = 0, last = 0, last_u = 0; i < 8;) {
OGAWA Hirofumi9aacd592007-07-15 23:39:56 -0700545 if (!(c = work[i]))
546 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
548 &bufuname[j++], opt_shortname,
549 de->lcase & CASE_LOWER_BASE);
550 if (chl <= 1) {
551 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
552 if (c != ' ') {
553 last = i;
554 last_u = j;
555 }
556 } else {
557 last_u = j;
558 for (chi = 0; chi < chl && i < 8; chi++) {
559 ptname[i] = work[i];
560 i++; last = i;
561 }
562 }
563 }
564 i = last;
565 j = last_u;
566 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
567 ptname[i++] = '.';
OGAWA Hirofumi9aacd592007-07-15 23:39:56 -0700568 for (i2 = 8; i2 < MSDOS_NAME;) {
569 if (!(c = work[i2]))
570 break;
571 chl = fat_shortname2uni(nls_disk, &work[i2], MSDOS_NAME - i2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 &bufuname[j++], opt_shortname,
573 de->lcase & CASE_LOWER_EXT);
574 if (chl <= 1) {
575 i2++;
576 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
577 if (c != ' ') {
578 last = i;
579 last_u = j;
580 }
581 } else {
582 last_u = j;
OGAWA Hirofumi9aacd592007-07-15 23:39:56 -0700583 for (chi = 0; chi < chl && i2 < MSDOS_NAME; chi++) {
584 ptname[i++] = work[i2++];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 last = i;
586 }
587 }
588 }
589 if (!last)
590 goto RecEnd;
591
592 i = last + dotoffset;
593 j = last_u;
594
595 lpos = cpos - (long_slots+1)*sizeof(struct msdos_dir_entry);
596 if (!memcmp(de->name, MSDOS_DOT, MSDOS_NAME))
597 inum = inode->i_ino;
598 else if (!memcmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
Josef "Jeff" Sipekdba32302006-12-08 02:36:39 -0800599 inum = parent_ino(filp->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 } else {
601 loff_t i_pos = fat_make_i_pos(sb, bh, de);
602 struct inode *tmp = fat_iget(sb, i_pos);
603 if (tmp) {
604 inum = tmp->i_ino;
605 iput(tmp);
606 } else
607 inum = iunique(sb, MSDOS_ROOT_INO);
608 }
609
610 if (isvfat) {
611 bufuname[j] = 0x0000;
612 i = utf8 ? utf8_wcstombs(bufname, bufuname, sizeof(bufname))
Keith Mokf22032b2008-04-28 02:16:29 -0700613 : uni16_to_x8(bufname, bufuname, sizeof(bufname), uni_xlate, nls_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615
616 fill_name = bufname;
617 fill_len = i;
618 if (!short_only && long_slots) {
619 /* convert the unicode long name. 261 is maximum size
620 * of unicode buffer. (13 * slots + nul) */
621 void *longname = unicode + 261;
OGAWA Hirofumic7a6c4e2008-04-28 02:16:29 -0700622 int buf_size = PATH_MAX - (261 * sizeof(unicode[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 int long_len = utf8
624 ? utf8_wcstombs(longname, unicode, buf_size)
Keith Mokf22032b2008-04-28 02:16:29 -0700625 : uni16_to_x8(longname, unicode, buf_size, uni_xlate, nls_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 if (!both) {
628 fill_name = longname;
629 fill_len = long_len;
630 } else {
631 /* hack for fat_ioctl_filldir() */
632 struct fat_ioctl_filldir_callback *p = dirent;
633
634 p->longname = longname;
635 p->long_len = long_len;
636 p->shortname = bufname;
637 p->short_len = i;
638 fill_name = NULL;
639 fill_len = 0;
640 }
641 }
642 if (filldir(dirent, fill_name, fill_len, *furrfu, inum,
643 (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
644 goto FillFailed;
645
646RecEnd:
647 furrfu = &lpos;
648 filp->f_pos = cpos;
649 goto GetNew;
650EODir:
651 filp->f_pos = cpos;
652FillFailed:
Karsten Wiesef3ef6f62005-09-06 15:17:12 -0700653 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (unicode)
OGAWA Hirofumic7a6c4e2008-04-28 02:16:29 -0700655 __putname(unicode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656out:
657 unlock_kernel();
658 return ret;
659}
660
661static int fat_readdir(struct file *filp, void *dirent, filldir_t filldir)
662{
Josef "Jeff" Sipekdba32302006-12-08 02:36:39 -0800663 struct inode *inode = filp->f_path.dentry->d_inode;
Pekka Enbergad2c1602005-10-30 15:03:50 -0800664 return __fat_readdir(inode, filp, dirent, filldir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700667#define FAT_IOCTL_FILLDIR_FUNC(func, dirent_type) \
668static int func(void *__buf, const char *name, int name_len, \
669 loff_t offset, u64 ino, unsigned int d_type) \
670{ \
671 struct fat_ioctl_filldir_callback *buf = __buf; \
672 struct dirent_type __user *d1 = buf->dirent; \
673 struct dirent_type __user *d2 = d1 + 1; \
674 \
675 if (buf->result) \
676 return -EINVAL; \
677 buf->result++; \
678 \
679 if (name != NULL) { \
680 /* dirent has only short name */ \
681 if (name_len >= sizeof(d1->d_name)) \
682 name_len = sizeof(d1->d_name) - 1; \
683 \
684 if (put_user(0, d2->d_name) || \
685 put_user(0, &d2->d_reclen) || \
686 copy_to_user(d1->d_name, name, name_len) || \
687 put_user(0, d1->d_name + name_len) || \
688 put_user(name_len, &d1->d_reclen)) \
689 goto efault; \
690 } else { \
691 /* dirent has short and long name */ \
692 const char *longname = buf->longname; \
693 int long_len = buf->long_len; \
694 const char *shortname = buf->shortname; \
695 int short_len = buf->short_len; \
696 \
697 if (long_len >= sizeof(d1->d_name)) \
698 long_len = sizeof(d1->d_name) - 1; \
699 if (short_len >= sizeof(d1->d_name)) \
700 short_len = sizeof(d1->d_name) - 1; \
701 \
702 if (copy_to_user(d2->d_name, longname, long_len) || \
703 put_user(0, d2->d_name + long_len) || \
704 put_user(long_len, &d2->d_reclen) || \
705 put_user(ino, &d2->d_ino) || \
706 put_user(offset, &d2->d_off) || \
707 copy_to_user(d1->d_name, shortname, short_len) || \
708 put_user(0, d1->d_name + short_len) || \
709 put_user(short_len, &d1->d_reclen)) \
710 goto efault; \
711 } \
712 return 0; \
713efault: \
714 buf->result = -EFAULT; \
715 return -EFAULT; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700718FAT_IOCTL_FILLDIR_FUNC(fat_ioctl_filldir, dirent)
719
720static int fat_ioctl_readdir(struct inode *inode, struct file *filp,
721 void __user *dirent, filldir_t filldir,
722 int short_only, int both)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
724 struct fat_ioctl_filldir_callback buf;
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700725 int ret;
726
727 buf.dirent = dirent;
728 buf.result = 0;
729 mutex_lock(&inode->i_mutex);
730 ret = -ENOENT;
731 if (!IS_DEADDIR(inode)) {
732 ret = __fat_readdir(inode, filp, &buf, filldir,
733 short_only, both);
734 }
735 mutex_unlock(&inode->i_mutex);
736 if (ret >= 0)
737 ret = buf.result;
738 return ret;
739}
740
741static int fat_dir_ioctl(struct inode *inode, struct file *filp,
742 unsigned int cmd, unsigned long arg)
743{
744 struct dirent __user *d1 = (struct dirent __user *)arg;
745 int short_only, both;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 switch (cmd) {
748 case VFAT_IOCTL_READDIR_SHORT:
749 short_only = 1;
750 both = 0;
751 break;
752 case VFAT_IOCTL_READDIR_BOTH:
753 short_only = 0;
754 both = 1;
755 break;
756 default:
757 return fat_generic_ioctl(inode, filp, cmd, arg);
758 }
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (!access_ok(VERIFY_WRITE, d1, sizeof(struct dirent[2])))
761 return -EFAULT;
762 /*
763 * Yes, we don't need this put_user() absolutely. However old
764 * code didn't return the right value. So, app use this value,
765 * in order to check whether it is EOF.
766 */
767 if (put_user(0, &d1->d_reclen))
768 return -EFAULT;
769
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700770 return fat_ioctl_readdir(inode, filp, d1, fat_ioctl_filldir,
771 short_only, both);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
David Howells188f83d2006-08-31 12:50:04 +0200774#ifdef CONFIG_COMPAT
775#define VFAT_IOCTL_READDIR_BOTH32 _IOR('r', 1, struct compat_dirent[2])
776#define VFAT_IOCTL_READDIR_SHORT32 _IOR('r', 2, struct compat_dirent[2])
777
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700778FAT_IOCTL_FILLDIR_FUNC(fat_compat_ioctl_filldir, compat_dirent)
David Howells188f83d2006-08-31 12:50:04 +0200779
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700780static long fat_compat_dir_ioctl(struct file *filp, unsigned cmd,
David Howells188f83d2006-08-31 12:50:04 +0200781 unsigned long arg)
782{
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700783 struct inode *inode = filp->f_path.dentry->d_inode;
784 struct compat_dirent __user *d1 = compat_ptr(arg);
785 int short_only, both;
David Howells188f83d2006-08-31 12:50:04 +0200786
787 switch (cmd) {
David Howells188f83d2006-08-31 12:50:04 +0200788 case VFAT_IOCTL_READDIR_SHORT32:
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700789 short_only = 1;
790 both = 0;
791 break;
792 case VFAT_IOCTL_READDIR_BOTH32:
793 short_only = 0;
794 both = 1;
David Howells188f83d2006-08-31 12:50:04 +0200795 break;
796 default:
797 return -ENOIOCTLCMD;
798 }
799
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700800 if (!access_ok(VERIFY_WRITE, d1, sizeof(struct compat_dirent[2])))
801 return -EFAULT;
802 /*
803 * Yes, we don't need this put_user() absolutely. However old
804 * code didn't return the right value. So, app use this value,
805 * in order to check whether it is EOF.
806 */
807 if (put_user(0, &d1->d_reclen))
808 return -EFAULT;
809
810 return fat_ioctl_readdir(inode, filp, d1, fat_compat_ioctl_filldir,
811 short_only, both);
David Howells188f83d2006-08-31 12:50:04 +0200812}
813#endif /* CONFIG_COMPAT */
814
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800815const struct file_operations fat_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 .read = generic_read_dir,
817 .readdir = fat_readdir,
818 .ioctl = fat_dir_ioctl,
David Howells188f83d2006-08-31 12:50:04 +0200819#ifdef CONFIG_COMPAT
820 .compat_ioctl = fat_compat_dir_ioctl,
821#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 .fsync = file_fsync,
823};
824
825static int fat_get_short_entry(struct inode *dir, loff_t *pos,
826 struct buffer_head **bh,
827 struct msdos_dir_entry **de)
828{
829 while (fat_get_entry(dir, pos, bh, de) >= 0) {
830 /* free entry or long name entry or volume label */
831 if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME))
832 return 0;
833 }
834 return -ENOENT;
835}
836
837/*
838 * The ".." entry can not provide the "struct fat_slot_info" informations
839 * for inode. So, this function provide the some informations only.
840 */
841int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh,
842 struct msdos_dir_entry **de, loff_t *i_pos)
843{
844 loff_t offset;
845
846 offset = 0;
847 *bh = NULL;
848 while (fat_get_short_entry(dir, &offset, bh, de) >= 0) {
849 if (!strncmp((*de)->name, MSDOS_DOTDOT, MSDOS_NAME)) {
850 *i_pos = fat_make_i_pos(dir->i_sb, *bh, *de);
851 return 0;
852 }
853 }
854 return -ENOENT;
855}
856
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -0800857EXPORT_SYMBOL_GPL(fat_get_dotdot_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859/* See if directory is empty */
860int fat_dir_empty(struct inode *dir)
861{
862 struct buffer_head *bh;
863 struct msdos_dir_entry *de;
864 loff_t cpos;
865 int result = 0;
866
867 bh = NULL;
868 cpos = 0;
869 while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
870 if (strncmp(de->name, MSDOS_DOT , MSDOS_NAME) &&
871 strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
872 result = -ENOTEMPTY;
873 break;
874 }
875 }
876 brelse(bh);
877 return result;
878}
879
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -0800880EXPORT_SYMBOL_GPL(fat_dir_empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882/*
883 * fat_subdirs counts the number of sub-directories of dir. It can be run
884 * on directories being created.
885 */
886int fat_subdirs(struct inode *dir)
887{
888 struct buffer_head *bh;
889 struct msdos_dir_entry *de;
890 loff_t cpos;
891 int count = 0;
892
893 bh = NULL;
894 cpos = 0;
895 while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
896 if (de->attr & ATTR_DIR)
897 count++;
898 }
899 brelse(bh);
900 return count;
901}
902
903/*
904 * Scans a directory for a given file (name points to its formatted name).
905 * Returns an error code or zero.
906 */
907int fat_scan(struct inode *dir, const unsigned char *name,
908 struct fat_slot_info *sinfo)
909{
910 struct super_block *sb = dir->i_sb;
911
912 sinfo->slot_off = 0;
913 sinfo->bh = NULL;
914 while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh,
915 &sinfo->de) >= 0) {
916 if (!strncmp(sinfo->de->name, name, MSDOS_NAME)) {
917 sinfo->slot_off -= sizeof(*sinfo->de);
918 sinfo->nr_slots = 1;
919 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
920 return 0;
921 }
922 }
923 return -ENOENT;
924}
925
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -0800926EXPORT_SYMBOL_GPL(fat_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928static int __fat_remove_entries(struct inode *dir, loff_t pos, int nr_slots)
929{
930 struct super_block *sb = dir->i_sb;
931 struct buffer_head *bh;
932 struct msdos_dir_entry *de, *endp;
933 int err = 0, orig_slots;
934
935 while (nr_slots) {
936 bh = NULL;
937 if (fat_get_entry(dir, &pos, &bh, &de) < 0) {
938 err = -EIO;
939 break;
940 }
941
942 orig_slots = nr_slots;
943 endp = (struct msdos_dir_entry *)(bh->b_data + sb->s_blocksize);
944 while (nr_slots && de < endp) {
945 de->name[0] = DELETED_FLAG;
946 de++;
947 nr_slots--;
948 }
949 mark_buffer_dirty(bh);
950 if (IS_DIRSYNC(dir))
951 err = sync_dirty_buffer(bh);
952 brelse(bh);
953 if (err)
954 break;
955
956 /* pos is *next* de's position, so this does `- sizeof(de)' */
957 pos += ((orig_slots - nr_slots) * sizeof(*de)) - sizeof(*de);
958 }
959
960 return err;
961}
962
963int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo)
964{
965 struct msdos_dir_entry *de;
966 struct buffer_head *bh;
967 int err = 0, nr_slots;
968
969 /*
970 * First stage: Remove the shortname. By this, the directory
971 * entry is removed.
972 */
973 nr_slots = sinfo->nr_slots;
974 de = sinfo->de;
975 sinfo->de = NULL;
976 bh = sinfo->bh;
977 sinfo->bh = NULL;
978 while (nr_slots && de >= (struct msdos_dir_entry *)bh->b_data) {
979 de->name[0] = DELETED_FLAG;
980 de--;
981 nr_slots--;
982 }
983 mark_buffer_dirty(bh);
984 if (IS_DIRSYNC(dir))
985 err = sync_dirty_buffer(bh);
986 brelse(bh);
987 if (err)
988 return err;
989 dir->i_version++;
990
991 if (nr_slots) {
992 /*
993 * Second stage: remove the remaining longname slots.
994 * (This directory entry is already removed, and so return
995 * the success)
996 */
997 err = __fat_remove_entries(dir, sinfo->slot_off, nr_slots);
998 if (err) {
999 printk(KERN_WARNING
1000 "FAT: Couldn't remove the long name slots\n");
1001 }
1002 }
1003
1004 dir->i_mtime = dir->i_atime = CURRENT_TIME_SEC;
1005 if (IS_DIRSYNC(dir))
1006 (void)fat_sync_inode(dir);
1007 else
1008 mark_inode_dirty(dir);
1009
1010 return 0;
1011}
1012
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -08001013EXPORT_SYMBOL_GPL(fat_remove_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015static int fat_zeroed_cluster(struct inode *dir, sector_t blknr, int nr_used,
1016 struct buffer_head **bhs, int nr_bhs)
1017{
1018 struct super_block *sb = dir->i_sb;
1019 sector_t last_blknr = blknr + MSDOS_SB(sb)->sec_per_clus;
1020 int err, i, n;
1021
1022 /* Zeroing the unused blocks on this cluster */
1023 blknr += nr_used;
1024 n = nr_used;
1025 while (blknr < last_blknr) {
1026 bhs[n] = sb_getblk(sb, blknr);
1027 if (!bhs[n]) {
1028 err = -ENOMEM;
1029 goto error;
1030 }
1031 memset(bhs[n]->b_data, 0, sb->s_blocksize);
1032 set_buffer_uptodate(bhs[n]);
1033 mark_buffer_dirty(bhs[n]);
1034
1035 n++;
1036 blknr++;
1037 if (n == nr_bhs) {
1038 if (IS_DIRSYNC(dir)) {
1039 err = fat_sync_bhs(bhs, n);
1040 if (err)
1041 goto error;
1042 }
1043 for (i = 0; i < n; i++)
1044 brelse(bhs[i]);
1045 n = 0;
1046 }
1047 }
1048 if (IS_DIRSYNC(dir)) {
1049 err = fat_sync_bhs(bhs, n);
1050 if (err)
1051 goto error;
1052 }
1053 for (i = 0; i < n; i++)
1054 brelse(bhs[i]);
1055
1056 return 0;
1057
1058error:
1059 for (i = 0; i < n; i++)
1060 bforget(bhs[i]);
1061 return err;
1062}
1063
1064int fat_alloc_new_dir(struct inode *dir, struct timespec *ts)
1065{
1066 struct super_block *sb = dir->i_sb;
1067 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1068 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
1069 struct msdos_dir_entry *de;
1070 sector_t blknr;
1071 __le16 date, time;
1072 int err, cluster;
1073
1074 err = fat_alloc_clusters(dir, &cluster, 1);
1075 if (err)
1076 goto error;
1077
1078 blknr = fat_clus_to_blknr(sbi, cluster);
1079 bhs[0] = sb_getblk(sb, blknr);
1080 if (!bhs[0]) {
1081 err = -ENOMEM;
1082 goto error_free;
1083 }
1084
1085 fat_date_unix2dos(ts->tv_sec, &time, &date);
1086
1087 de = (struct msdos_dir_entry *)bhs[0]->b_data;
1088 /* filling the new directory slots ("." and ".." entries) */
1089 memcpy(de[0].name, MSDOS_DOT, MSDOS_NAME);
1090 memcpy(de[1].name, MSDOS_DOTDOT, MSDOS_NAME);
1091 de->attr = de[1].attr = ATTR_DIR;
1092 de[0].lcase = de[1].lcase = 0;
1093 de[0].time = de[1].time = time;
1094 de[0].date = de[1].date = date;
1095 de[0].ctime_cs = de[1].ctime_cs = 0;
1096 if (sbi->options.isvfat) {
1097 /* extra timestamps */
1098 de[0].ctime = de[1].ctime = time;
1099 de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = date;
1100 } else {
1101 de[0].ctime = de[1].ctime = 0;
1102 de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = 0;
1103 }
1104 de[0].start = cpu_to_le16(cluster);
1105 de[0].starthi = cpu_to_le16(cluster >> 16);
1106 de[1].start = cpu_to_le16(MSDOS_I(dir)->i_logstart);
1107 de[1].starthi = cpu_to_le16(MSDOS_I(dir)->i_logstart >> 16);
1108 de[0].size = de[1].size = 0;
1109 memset(de + 2, 0, sb->s_blocksize - 2 * sizeof(*de));
1110 set_buffer_uptodate(bhs[0]);
1111 mark_buffer_dirty(bhs[0]);
1112
1113 err = fat_zeroed_cluster(dir, blknr, 1, bhs, MAX_BUF_PER_PAGE);
1114 if (err)
1115 goto error_free;
1116
1117 return cluster;
1118
1119error_free:
1120 fat_free_clusters(dir, cluster);
1121error:
1122 return err;
1123}
1124
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -08001125EXPORT_SYMBOL_GPL(fat_alloc_new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127static int fat_add_new_entries(struct inode *dir, void *slots, int nr_slots,
1128 int *nr_cluster, struct msdos_dir_entry **de,
1129 struct buffer_head **bh, loff_t *i_pos)
1130{
1131 struct super_block *sb = dir->i_sb;
1132 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1133 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
1134 sector_t blknr, start_blknr, last_blknr;
1135 unsigned long size, copy;
1136 int err, i, n, offset, cluster[2];
1137
1138 /*
1139 * The minimum cluster size is 512bytes, and maximum entry
1140 * size is 32*slots (672bytes). So, iff the cluster size is
1141 * 512bytes, we may need two clusters.
1142 */
1143 size = nr_slots * sizeof(struct msdos_dir_entry);
1144 *nr_cluster = (size + (sbi->cluster_size - 1)) >> sbi->cluster_bits;
1145 BUG_ON(*nr_cluster > 2);
1146
1147 err = fat_alloc_clusters(dir, cluster, *nr_cluster);
1148 if (err)
1149 goto error;
1150
1151 /*
1152 * First stage: Fill the directory entry. NOTE: This cluster
1153 * is not referenced from any inode yet, so updates order is
1154 * not important.
1155 */
1156 i = n = copy = 0;
1157 do {
1158 start_blknr = blknr = fat_clus_to_blknr(sbi, cluster[i]);
1159 last_blknr = start_blknr + sbi->sec_per_clus;
1160 while (blknr < last_blknr) {
1161 bhs[n] = sb_getblk(sb, blknr);
1162 if (!bhs[n]) {
1163 err = -ENOMEM;
1164 goto error_nomem;
1165 }
1166
1167 /* fill the directory entry */
1168 copy = min(size, sb->s_blocksize);
1169 memcpy(bhs[n]->b_data, slots, copy);
1170 slots += copy;
1171 size -= copy;
1172 set_buffer_uptodate(bhs[n]);
1173 mark_buffer_dirty(bhs[n]);
1174 if (!size)
1175 break;
1176 n++;
1177 blknr++;
1178 }
1179 } while (++i < *nr_cluster);
1180
1181 memset(bhs[n]->b_data + copy, 0, sb->s_blocksize - copy);
1182 offset = copy - sizeof(struct msdos_dir_entry);
1183 get_bh(bhs[n]);
1184 *bh = bhs[n];
1185 *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
1186 *i_pos = fat_make_i_pos(sb, *bh, *de);
1187
1188 /* Second stage: clear the rest of cluster, and write outs */
1189 err = fat_zeroed_cluster(dir, start_blknr, ++n, bhs, MAX_BUF_PER_PAGE);
1190 if (err)
1191 goto error_free;
1192
1193 return cluster[0];
1194
1195error_free:
1196 brelse(*bh);
1197 *bh = NULL;
1198 n = 0;
1199error_nomem:
1200 for (i = 0; i < n; i++)
1201 bforget(bhs[i]);
1202 fat_free_clusters(dir, cluster[0]);
1203error:
1204 return err;
1205}
1206
1207int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
1208 struct fat_slot_info *sinfo)
1209{
1210 struct super_block *sb = dir->i_sb;
1211 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1212 struct buffer_head *bh, *prev, *bhs[3]; /* 32*slots (672bytes) */
1213 struct msdos_dir_entry *de;
1214 int err, free_slots, i, nr_bhs;
1215 loff_t pos, i_pos;
1216
1217 sinfo->nr_slots = nr_slots;
1218
1219 /* First stage: search free direcotry entries */
1220 free_slots = nr_bhs = 0;
1221 bh = prev = NULL;
1222 pos = 0;
1223 err = -ENOSPC;
1224 while (fat_get_entry(dir, &pos, &bh, &de) > -1) {
1225 /* check the maximum size of directory */
1226 if (pos >= FAT_MAX_DIR_SIZE)
1227 goto error;
1228
1229 if (IS_FREE(de->name)) {
1230 if (prev != bh) {
1231 get_bh(bh);
1232 bhs[nr_bhs] = prev = bh;
1233 nr_bhs++;
1234 }
1235 free_slots++;
1236 if (free_slots == nr_slots)
1237 goto found;
1238 } else {
1239 for (i = 0; i < nr_bhs; i++)
1240 brelse(bhs[i]);
1241 prev = NULL;
1242 free_slots = nr_bhs = 0;
1243 }
1244 }
1245 if (dir->i_ino == MSDOS_ROOT_INO) {
1246 if (sbi->fat_bits != 32)
1247 goto error;
1248 } else if (MSDOS_I(dir)->i_start == 0) {
1249 printk(KERN_ERR "FAT: Corrupted directory (i_pos %lld)\n",
1250 MSDOS_I(dir)->i_pos);
1251 err = -EIO;
1252 goto error;
1253 }
1254
1255found:
1256 err = 0;
1257 pos -= free_slots * sizeof(*de);
1258 nr_slots -= free_slots;
1259 if (free_slots) {
1260 /*
1261 * Second stage: filling the free entries with new entries.
1262 * NOTE: If this slots has shortname, first, we write
1263 * the long name slots, then write the short name.
1264 */
1265 int size = free_slots * sizeof(*de);
1266 int offset = pos & (sb->s_blocksize - 1);
1267 int long_bhs = nr_bhs - (nr_slots == 0);
1268
1269 /* Fill the long name slots. */
1270 for (i = 0; i < long_bhs; i++) {
1271 int copy = min_t(int, sb->s_blocksize - offset, size);
1272 memcpy(bhs[i]->b_data + offset, slots, copy);
1273 mark_buffer_dirty(bhs[i]);
1274 offset = 0;
1275 slots += copy;
1276 size -= copy;
1277 }
1278 if (long_bhs && IS_DIRSYNC(dir))
1279 err = fat_sync_bhs(bhs, long_bhs);
1280 if (!err && i < nr_bhs) {
1281 /* Fill the short name slot. */
1282 int copy = min_t(int, sb->s_blocksize - offset, size);
1283 memcpy(bhs[i]->b_data + offset, slots, copy);
1284 mark_buffer_dirty(bhs[i]);
1285 if (IS_DIRSYNC(dir))
1286 err = sync_dirty_buffer(bhs[i]);
1287 }
1288 for (i = 0; i < nr_bhs; i++)
1289 brelse(bhs[i]);
1290 if (err)
1291 goto error_remove;
1292 }
1293
1294 if (nr_slots) {
1295 int cluster, nr_cluster;
1296
1297 /*
1298 * Third stage: allocate the cluster for new entries.
1299 * And initialize the cluster with new entries, then
1300 * add the cluster to dir.
1301 */
1302 cluster = fat_add_new_entries(dir, slots, nr_slots, &nr_cluster,
1303 &de, &bh, &i_pos);
1304 if (cluster < 0) {
1305 err = cluster;
1306 goto error_remove;
1307 }
1308 err = fat_chain_add(dir, cluster, nr_cluster);
1309 if (err) {
1310 fat_free_clusters(dir, cluster);
1311 goto error_remove;
1312 }
1313 if (dir->i_size & (sbi->cluster_size - 1)) {
1314 fat_fs_panic(sb, "Odd directory size");
1315 dir->i_size = (dir->i_size + sbi->cluster_size - 1)
1316 & ~((loff_t)sbi->cluster_size - 1);
1317 }
1318 dir->i_size += nr_cluster << sbi->cluster_bits;
1319 MSDOS_I(dir)->mmu_private += nr_cluster << sbi->cluster_bits;
1320 }
1321 sinfo->slot_off = pos;
1322 sinfo->de = de;
1323 sinfo->bh = bh;
1324 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
1325
1326 return 0;
1327
1328error:
1329 brelse(bh);
1330 for (i = 0; i < nr_bhs; i++)
1331 brelse(bhs[i]);
1332 return err;
1333
1334error_remove:
1335 brelse(bh);
1336 if (free_slots)
1337 __fat_remove_entries(dir, pos, free_slots);
1338 return err;
1339}
1340
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -08001341EXPORT_SYMBOL_GPL(fat_add_entries);