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