blob: bae1c3292522eddbb744bcf6e6f84bc56da349e3 [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 */
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700126static int uni16_to_x8(unsigned char *ascii, const wchar_t *uni, int len,
Keith Mokf22032b2008-04-28 02:16:29 -0700127 int uni_xlate, struct nls_table *nls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700129 const wchar_t *ip;
130 wchar_t ec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 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
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700170static inline int fat_uni_to_x8(struct msdos_sb_info *sbi, const wchar_t *uni,
171 unsigned char *buf, int size)
172{
173 if (sbi->options.utf8)
174 return utf8_wcstombs(buf, uni, size);
175 else
176 return uni16_to_x8(buf, uni, size, sbi->options.unicode_xlate,
177 sbi->nls_io);
178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static inline int
181fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
182{
183 int charlen;
184
185 charlen = t->char2uni(c, clen, uni);
186 if (charlen < 0) {
187 *uni = 0x003f; /* a question mark */
188 charlen = 1;
189 }
190 return charlen;
191}
192
193static inline int
194fat_short2lower_uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
195{
196 int charlen;
197 wchar_t wc;
198
199 charlen = t->char2uni(c, clen, &wc);
200 if (charlen < 0) {
201 *uni = 0x003f; /* a question mark */
202 charlen = 1;
203 } else if (charlen <= 1) {
204 unsigned char nc = t->charset2lower[*c];
205
206 if (!nc)
207 nc = *c;
208
209 if ( (charlen = t->char2uni(&nc, 1, uni)) < 0) {
210 *uni = 0x003f; /* a question mark */
211 charlen = 1;
212 }
213 } else
214 *uni = wc;
215
216 return charlen;
217}
218
219static inline int
220fat_shortname2uni(struct nls_table *nls, unsigned char *buf, int buf_size,
221 wchar_t *uni_buf, unsigned short opt, int lower)
222{
223 int len = 0;
224
225 if (opt & VFAT_SFN_DISPLAY_LOWER)
226 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
227 else if (opt & VFAT_SFN_DISPLAY_WIN95)
228 len = fat_short2uni(nls, buf, buf_size, uni_buf);
229 else if (opt & VFAT_SFN_DISPLAY_WINNT) {
230 if (lower)
231 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
232 else
233 len = fat_short2uni(nls, buf, buf_size, uni_buf);
234 } else
235 len = fat_short2uni(nls, buf, buf_size, uni_buf);
236
237 return len;
238}
239
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700240static inline int fat_name_match(struct msdos_sb_info *sbi,
241 const unsigned char *a, int a_len,
242 const unsigned char *b, int b_len)
243{
244 if (a_len != b_len)
245 return 0;
246
247 if (sbi->options.name_check != 's')
248 return !nls_strnicmp(sbi->nls_io, a, b, a_len);
249 else
250 return !memcmp(a, b, a_len);
251}
252
Pekka Enbergad2c1602005-10-30 15:03:50 -0800253enum { PARSE_INVALID = 1, PARSE_NOT_LONGNAME, PARSE_EOF, };
254
255/**
256 * fat_parse_long - Parse extended directory entry.
257 *
258 * This function returns zero on success, negative value on error, or one of
259 * the following:
260 *
261 * %PARSE_INVALID - Directory entry is invalid.
262 * %PARSE_NOT_LONGNAME - Directory entry does not contain longname.
263 * %PARSE_EOF - Directory has no more entries.
264 */
265static int fat_parse_long(struct inode *dir, loff_t *pos,
266 struct buffer_head **bh, struct msdos_dir_entry **de,
267 wchar_t **unicode, unsigned char *nr_slots)
268{
269 struct msdos_dir_slot *ds;
270 unsigned char id, slot, slots, alias_checksum;
271
272 if (!*unicode) {
OGAWA Hirofumic7a6c4e2008-04-28 02:16:29 -0700273 *unicode = __getname();
Pekka Enbergad2c1602005-10-30 15:03:50 -0800274 if (!*unicode) {
275 brelse(*bh);
276 return -ENOMEM;
277 }
278 }
279parse_long:
280 slots = 0;
281 ds = (struct msdos_dir_slot *)*de;
282 id = ds->id;
283 if (!(id & 0x40))
284 return PARSE_INVALID;
285 slots = id & ~0x40;
286 if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */
287 return PARSE_INVALID;
288 *nr_slots = slots;
289 alias_checksum = ds->alias_checksum;
290
291 slot = slots;
292 while (1) {
293 int offset;
294
295 slot--;
296 offset = slot * 13;
297 fat16_towchar(*unicode + offset, ds->name0_4, 5);
298 fat16_towchar(*unicode + offset + 5, ds->name5_10, 6);
299 fat16_towchar(*unicode + offset + 11, ds->name11_12, 2);
300
301 if (ds->id & 0x40)
302 (*unicode)[offset + 13] = 0;
303 if (fat_get_entry(dir, pos, bh, de) < 0)
304 return PARSE_EOF;
305 if (slot == 0)
306 break;
307 ds = (struct msdos_dir_slot *)*de;
308 if (ds->attr != ATTR_EXT)
309 return PARSE_NOT_LONGNAME;
310 if ((ds->id & ~0x40) != slot)
311 goto parse_long;
312 if (ds->alias_checksum != alias_checksum)
313 goto parse_long;
314 }
315 if ((*de)->name[0] == DELETED_FLAG)
316 return PARSE_INVALID;
317 if ((*de)->attr == ATTR_EXT)
318 goto parse_long;
319 if (IS_FREE((*de)->name) || ((*de)->attr & ATTR_VOLUME))
320 return PARSE_INVALID;
321 if (fat_checksum((*de)->name) != alias_checksum)
322 *nr_slots = 0;
323
324 return 0;
325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327/*
OGAWA Hirofumi98a15162008-07-25 01:46:44 -0700328 * Maximum buffer size of short name.
329 * [(MSDOS_NAME + '.') * max one char + nul]
330 * For msdos style, ['.' (hidden) + MSDOS_NAME + '.' + nul]
331 */
332#define FAT_MAX_SHORT_SIZE ((MSDOS_NAME + 1) * NLS_MAX_CHARSET_SIZE + 1)
333/*
334 * Maximum buffer size of unicode chars from slots.
335 * [(max longname slots * 13 (size in a slot) + nul) * sizeof(wchar_t)]
336 */
337#define FAT_MAX_UNI_CHARS ((MSDOS_SLOTS - 1) * 13 + 1)
338#define FAT_MAX_UNI_SIZE (FAT_MAX_UNI_CHARS * sizeof(wchar_t))
339
340/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 * Return values: negative -> error, 0 -> not found, positive -> found,
342 * value is the total amount of slots, including the shortname entry.
343 */
344int fat_search_long(struct inode *inode, const unsigned char *name,
345 int name_len, struct fat_slot_info *sinfo)
346{
347 struct super_block *sb = inode->i_sb;
348 struct msdos_sb_info *sbi = MSDOS_SB(sb);
349 struct buffer_head *bh = NULL;
350 struct msdos_dir_entry *de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 struct nls_table *nls_disk = sbi->nls_disk;
Keith Mokf22032b2008-04-28 02:16:29 -0700352 unsigned char nr_slots;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700353 wchar_t bufuname[14];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 wchar_t *unicode = NULL;
Keith Mokf22032b2008-04-28 02:16:29 -0700355 unsigned char work[MSDOS_NAME];
OGAWA Hirofumi98a15162008-07-25 01:46:44 -0700356 unsigned char bufname[FAT_MAX_SHORT_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 unsigned short opt_shortname = sbi->options.shortname;
358 loff_t cpos = 0;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700359 int chl, i, j, last_u, err, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 err = -ENOENT;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700362 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700364 goto end_of_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365parse_record:
366 nr_slots = 0;
367 if (de->name[0] == DELETED_FLAG)
368 continue;
369 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
370 continue;
371 if (de->attr != ATTR_EXT && IS_FREE(de->name))
372 continue;
373 if (de->attr == ATTR_EXT) {
Pekka Enbergad2c1602005-10-30 15:03:50 -0800374 int status = fat_parse_long(inode, &cpos, &bh, &de,
375 &unicode, &nr_slots);
376 if (status < 0)
377 return status;
378 else if (status == PARSE_INVALID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 continue;
Pekka Enbergad2c1602005-10-30 15:03:50 -0800380 else if (status == PARSE_NOT_LONGNAME)
381 goto parse_record;
382 else if (status == PARSE_EOF)
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700383 goto end_of_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
385
386 memcpy(work, de->name, sizeof(de->name));
387 /* see namei.c, msdos_format_name */
388 if (work[0] == 0x05)
389 work[0] = 0xE5;
390 for (i = 0, j = 0, last_u = 0; i < 8;) {
OGAWA Hirofumi9aacd592007-07-15 23:39:56 -0700391 if (!work[i])
392 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
394 &bufuname[j++], opt_shortname,
395 de->lcase & CASE_LOWER_BASE);
396 if (chl <= 1) {
397 if (work[i] != ' ')
398 last_u = j;
399 } else {
400 last_u = j;
401 }
402 i += chl;
403 }
404 j = last_u;
405 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
OGAWA Hirofumi9aacd592007-07-15 23:39:56 -0700406 for (i = 8; i < MSDOS_NAME;) {
407 if (!work[i])
408 break;
409 chl = fat_shortname2uni(nls_disk, &work[i],
410 MSDOS_NAME - i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 &bufuname[j++], opt_shortname,
412 de->lcase & CASE_LOWER_EXT);
413 if (chl <= 1) {
OGAWA Hirofumi9aacd592007-07-15 23:39:56 -0700414 if (work[i] != ' ')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 last_u = j;
416 } else {
417 last_u = j;
418 }
419 i += chl;
420 }
421 if (!last_u)
422 continue;
423
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700424 /* Compare shortname */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 bufuname[last_u] = 0x0000;
OGAWA Hirofumi98a15162008-07-25 01:46:44 -0700426 len = fat_uni_to_x8(sbi, bufuname, bufname, sizeof(bufname));
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700427 if (fat_name_match(sbi, name, name_len, bufname, len))
428 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 if (nr_slots) {
OGAWA Hirofumi98a15162008-07-25 01:46:44 -0700431 void *longname = unicode + FAT_MAX_UNI_CHARS;
432 int size = PATH_MAX - FAT_MAX_UNI_SIZE;
433
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700434 /* Compare longname */
OGAWA Hirofumi98a15162008-07-25 01:46:44 -0700435 len = fat_uni_to_x8(sbi, unicode, longname, size);
436 if (fat_name_match(sbi, name, name_len, longname, len))
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700437 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439 }
440
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700441found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 nr_slots++; /* include the de */
443 sinfo->slot_off = cpos - nr_slots * sizeof(*de);
444 sinfo->nr_slots = nr_slots;
445 sinfo->de = de;
446 sinfo->bh = bh;
447 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
448 err = 0;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700449end_of_dir:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 if (unicode)
OGAWA Hirofumic7a6c4e2008-04-28 02:16:29 -0700451 __putname(unicode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 return err;
454}
455
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -0800456EXPORT_SYMBOL_GPL(fat_search_long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458struct fat_ioctl_filldir_callback {
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700459 void __user *dirent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 int result;
461 /* for dir ioctl */
462 const char *longname;
463 int long_len;
464 const char *shortname;
465 int short_len;
466};
467
Pekka Enbergad2c1602005-10-30 15:03:50 -0800468static int __fat_readdir(struct inode *inode, struct file *filp, void *dirent,
469 filldir_t filldir, int short_only, int both)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 struct super_block *sb = inode->i_sb;
472 struct msdos_sb_info *sbi = MSDOS_SB(sb);
473 struct buffer_head *bh;
474 struct msdos_dir_entry *de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 struct nls_table *nls_disk = sbi->nls_disk;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700476 unsigned char nr_slots;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 wchar_t bufuname[14];
478 wchar_t *unicode = NULL;
OGAWA Hirofumi98a15162008-07-25 01:46:44 -0700479 unsigned char c, work[MSDOS_NAME];
480 unsigned char bufname[FAT_MAX_SHORT_SIZE], *ptname = bufname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 unsigned short opt_shortname = sbi->options.shortname;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700482 int isvfat = sbi->options.isvfat;
483 int nocase = sbi->options.nocase;
OGAWA Hirofumidcd8c532008-07-25 01:46:44 -0700484 const char *fill_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 unsigned long inum;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700486 unsigned long lpos, dummy, *furrfu = &lpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 loff_t cpos;
OGAWA Hirofumidcd8c532008-07-25 01:46:44 -0700488 int chi, chl, i, i2, j, last, last_u, dotoffset = 0, fill_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 int ret = 0;
490
Linus Torvalds8f593422008-05-19 19:53:01 -0700491 lock_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 cpos = filp->f_pos;
494 /* Fake . and .. for the root directory. */
495 if (inode->i_ino == MSDOS_ROOT_INO) {
496 while (cpos < 2) {
497 if (filldir(dirent, "..", cpos+1, cpos, MSDOS_ROOT_INO, DT_DIR) < 0)
498 goto out;
499 cpos++;
500 filp->f_pos++;
501 }
502 if (cpos == 2) {
503 dummy = 2;
504 furrfu = &dummy;
505 cpos = 0;
506 }
507 }
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700508 if (cpos & (sizeof(struct msdos_dir_entry) - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 ret = -ENOENT;
510 goto out;
511 }
512
513 bh = NULL;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700514get_new:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700516 goto end_of_dir;
Pekka Enbergad2c1602005-10-30 15:03:50 -0800517parse_record:
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700518 nr_slots = 0;
OGAWA Hirofumidcd8c532008-07-25 01:46:44 -0700519 /*
520 * Check for long filename entry, but if short_only, we don't
521 * need to parse long filename.
522 */
523 if (isvfat && !short_only) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (de->name[0] == DELETED_FLAG)
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700525 goto record_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700527 goto record_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (de->attr != ATTR_EXT && IS_FREE(de->name))
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700529 goto record_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 } else {
531 if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700532 goto record_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534
535 if (isvfat && de->attr == ATTR_EXT) {
Pekka Enbergad2c1602005-10-30 15:03:50 -0800536 int status = fat_parse_long(inode, &cpos, &bh, &de,
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700537 &unicode, &nr_slots);
Pekka Enbergad2c1602005-10-30 15:03:50 -0800538 if (status < 0) {
539 filp->f_pos = cpos;
540 ret = status;
541 goto out;
542 } else if (status == PARSE_INVALID)
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700543 goto record_end;
Pekka Enbergad2c1602005-10-30 15:03:50 -0800544 else if (status == PARSE_NOT_LONGNAME)
545 goto parse_record;
546 else if (status == PARSE_EOF)
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700547 goto end_of_dir;
OGAWA Hirofumidcd8c532008-07-25 01:46:44 -0700548
549 if (nr_slots) {
550 void *longname = unicode + FAT_MAX_UNI_CHARS;
551 int size = PATH_MAX - FAT_MAX_UNI_SIZE;
552 int len = fat_uni_to_x8(sbi, unicode, longname, size);
553
554 fill_name = longname;
555 fill_len = len;
556 /* !both && !short_only, so we don't need shortname. */
557 if (!both)
558 goto start_filldir;
559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561
562 if (sbi->options.dotsOK) {
563 ptname = bufname;
564 dotoffset = 0;
565 if (de->attr & ATTR_HIDDEN) {
566 *ptname++ = '.';
567 dotoffset = 1;
568 }
569 }
570
571 memcpy(work, de->name, sizeof(de->name));
572 /* see namei.c, msdos_format_name */
573 if (work[0] == 0x05)
574 work[0] = 0xE5;
575 for (i = 0, j = 0, last = 0, last_u = 0; i < 8;) {
OGAWA Hirofumi9aacd592007-07-15 23:39:56 -0700576 if (!(c = work[i]))
577 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
579 &bufuname[j++], opt_shortname,
580 de->lcase & CASE_LOWER_BASE);
581 if (chl <= 1) {
582 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
583 if (c != ' ') {
584 last = i;
585 last_u = j;
586 }
587 } else {
588 last_u = j;
589 for (chi = 0; chi < chl && i < 8; chi++) {
590 ptname[i] = work[i];
591 i++; last = i;
592 }
593 }
594 }
595 i = last;
596 j = last_u;
597 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
598 ptname[i++] = '.';
OGAWA Hirofumi9aacd592007-07-15 23:39:56 -0700599 for (i2 = 8; i2 < MSDOS_NAME;) {
600 if (!(c = work[i2]))
601 break;
602 chl = fat_shortname2uni(nls_disk, &work[i2], MSDOS_NAME - i2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 &bufuname[j++], opt_shortname,
604 de->lcase & CASE_LOWER_EXT);
605 if (chl <= 1) {
606 i2++;
607 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
608 if (c != ' ') {
609 last = i;
610 last_u = j;
611 }
612 } else {
613 last_u = j;
OGAWA Hirofumi9aacd592007-07-15 23:39:56 -0700614 for (chi = 0; chi < chl && i2 < MSDOS_NAME; chi++) {
615 ptname[i++] = work[i2++];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 last = i;
617 }
618 }
619 }
620 if (!last)
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700621 goto record_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 i = last + dotoffset;
624 j = last_u;
625
OGAWA Hirofumidcd8c532008-07-25 01:46:44 -0700626 if (isvfat) {
627 bufuname[j] = 0x0000;
628 i = fat_uni_to_x8(sbi, bufuname, bufname, sizeof(bufname));
629 }
630 if (nr_slots) {
631 /* hack for fat_ioctl_filldir() */
632 struct fat_ioctl_filldir_callback *p = dirent;
633
634 p->longname = fill_name;
635 p->long_len = fill_len;
636 p->shortname = bufname;
637 p->short_len = i;
638 fill_name = NULL;
639 fill_len = 0;
640 } else {
641 fill_name = bufname;
642 fill_len = i;
643 }
644
645start_filldir:
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700646 lpos = cpos - (nr_slots + 1) * sizeof(struct msdos_dir_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (!memcmp(de->name, MSDOS_DOT, MSDOS_NAME))
648 inum = inode->i_ino;
649 else if (!memcmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
Josef "Jeff" Sipekdba32302006-12-08 02:36:39 -0800650 inum = parent_ino(filp->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 } else {
652 loff_t i_pos = fat_make_i_pos(sb, bh, de);
653 struct inode *tmp = fat_iget(sb, i_pos);
654 if (tmp) {
655 inum = tmp->i_ino;
656 iput(tmp);
657 } else
658 inum = iunique(sb, MSDOS_ROOT_INO);
659 }
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (filldir(dirent, fill_name, fill_len, *furrfu, inum,
662 (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700663 goto fill_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700665record_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 furrfu = &lpos;
667 filp->f_pos = cpos;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700668 goto get_new;
669end_of_dir:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 filp->f_pos = cpos;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700671fill_failed:
Karsten Wiesef3ef6f62005-09-06 15:17:12 -0700672 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (unicode)
OGAWA Hirofumic7a6c4e2008-04-28 02:16:29 -0700674 __putname(unicode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675out:
Linus Torvalds8f593422008-05-19 19:53:01 -0700676 unlock_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return ret;
678}
679
680static int fat_readdir(struct file *filp, void *dirent, filldir_t filldir)
681{
Josef "Jeff" Sipekdba32302006-12-08 02:36:39 -0800682 struct inode *inode = filp->f_path.dentry->d_inode;
Pekka Enbergad2c1602005-10-30 15:03:50 -0800683 return __fat_readdir(inode, filp, dirent, filldir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700686#define FAT_IOCTL_FILLDIR_FUNC(func, dirent_type) \
687static int func(void *__buf, const char *name, int name_len, \
688 loff_t offset, u64 ino, unsigned int d_type) \
689{ \
690 struct fat_ioctl_filldir_callback *buf = __buf; \
691 struct dirent_type __user *d1 = buf->dirent; \
692 struct dirent_type __user *d2 = d1 + 1; \
693 \
694 if (buf->result) \
695 return -EINVAL; \
696 buf->result++; \
697 \
698 if (name != NULL) { \
699 /* dirent has only short name */ \
700 if (name_len >= sizeof(d1->d_name)) \
701 name_len = sizeof(d1->d_name) - 1; \
702 \
703 if (put_user(0, d2->d_name) || \
704 put_user(0, &d2->d_reclen) || \
705 copy_to_user(d1->d_name, name, name_len) || \
706 put_user(0, d1->d_name + name_len) || \
707 put_user(name_len, &d1->d_reclen)) \
708 goto efault; \
709 } else { \
710 /* dirent has short and long name */ \
711 const char *longname = buf->longname; \
712 int long_len = buf->long_len; \
713 const char *shortname = buf->shortname; \
714 int short_len = buf->short_len; \
715 \
716 if (long_len >= sizeof(d1->d_name)) \
717 long_len = sizeof(d1->d_name) - 1; \
718 if (short_len >= sizeof(d1->d_name)) \
719 short_len = sizeof(d1->d_name) - 1; \
720 \
721 if (copy_to_user(d2->d_name, longname, long_len) || \
722 put_user(0, d2->d_name + long_len) || \
723 put_user(long_len, &d2->d_reclen) || \
724 put_user(ino, &d2->d_ino) || \
725 put_user(offset, &d2->d_off) || \
726 copy_to_user(d1->d_name, shortname, short_len) || \
727 put_user(0, d1->d_name + short_len) || \
728 put_user(short_len, &d1->d_reclen)) \
729 goto efault; \
730 } \
731 return 0; \
732efault: \
733 buf->result = -EFAULT; \
734 return -EFAULT; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
Adrian Bunk531f7102008-07-25 01:46:43 -0700737FAT_IOCTL_FILLDIR_FUNC(fat_ioctl_filldir, __fat_dirent)
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700738
739static int fat_ioctl_readdir(struct inode *inode, struct file *filp,
740 void __user *dirent, filldir_t filldir,
741 int short_only, int both)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 struct fat_ioctl_filldir_callback buf;
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700744 int ret;
745
746 buf.dirent = dirent;
747 buf.result = 0;
748 mutex_lock(&inode->i_mutex);
749 ret = -ENOENT;
750 if (!IS_DEADDIR(inode)) {
751 ret = __fat_readdir(inode, filp, &buf, filldir,
752 short_only, both);
753 }
754 mutex_unlock(&inode->i_mutex);
755 if (ret >= 0)
756 ret = buf.result;
757 return ret;
758}
759
760static int fat_dir_ioctl(struct inode *inode, struct file *filp,
761 unsigned int cmd, unsigned long arg)
762{
Adrian Bunk531f7102008-07-25 01:46:43 -0700763 struct __fat_dirent __user *d1 = (struct __fat_dirent __user *)arg;
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700764 int short_only, both;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 switch (cmd) {
767 case VFAT_IOCTL_READDIR_SHORT:
768 short_only = 1;
769 both = 0;
770 break;
771 case VFAT_IOCTL_READDIR_BOTH:
772 short_only = 0;
773 both = 1;
774 break;
775 default:
776 return fat_generic_ioctl(inode, filp, cmd, arg);
777 }
778
Adrian Bunk531f7102008-07-25 01:46:43 -0700779 if (!access_ok(VERIFY_WRITE, d1, sizeof(struct __fat_dirent[2])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return -EFAULT;
781 /*
782 * Yes, we don't need this put_user() absolutely. However old
783 * code didn't return the right value. So, app use this value,
784 * in order to check whether it is EOF.
785 */
786 if (put_user(0, &d1->d_reclen))
787 return -EFAULT;
788
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700789 return fat_ioctl_readdir(inode, filp, d1, fat_ioctl_filldir,
790 short_only, both);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
David Howells188f83d2006-08-31 12:50:04 +0200793#ifdef CONFIG_COMPAT
794#define VFAT_IOCTL_READDIR_BOTH32 _IOR('r', 1, struct compat_dirent[2])
795#define VFAT_IOCTL_READDIR_SHORT32 _IOR('r', 2, struct compat_dirent[2])
796
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700797FAT_IOCTL_FILLDIR_FUNC(fat_compat_ioctl_filldir, compat_dirent)
David Howells188f83d2006-08-31 12:50:04 +0200798
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700799static long fat_compat_dir_ioctl(struct file *filp, unsigned cmd,
David Howells188f83d2006-08-31 12:50:04 +0200800 unsigned long arg)
801{
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700802 struct inode *inode = filp->f_path.dentry->d_inode;
803 struct compat_dirent __user *d1 = compat_ptr(arg);
804 int short_only, both;
David Howells188f83d2006-08-31 12:50:04 +0200805
806 switch (cmd) {
David Howells188f83d2006-08-31 12:50:04 +0200807 case VFAT_IOCTL_READDIR_SHORT32:
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700808 short_only = 1;
809 both = 0;
810 break;
811 case VFAT_IOCTL_READDIR_BOTH32:
812 short_only = 0;
813 both = 1;
David Howells188f83d2006-08-31 12:50:04 +0200814 break;
815 default:
816 return -ENOIOCTLCMD;
817 }
818
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700819 if (!access_ok(VERIFY_WRITE, d1, sizeof(struct compat_dirent[2])))
820 return -EFAULT;
821 /*
822 * Yes, we don't need this put_user() absolutely. However old
823 * code didn't return the right value. So, app use this value,
824 * in order to check whether it is EOF.
825 */
826 if (put_user(0, &d1->d_reclen))
827 return -EFAULT;
828
829 return fat_ioctl_readdir(inode, filp, d1, fat_compat_ioctl_filldir,
830 short_only, both);
David Howells188f83d2006-08-31 12:50:04 +0200831}
832#endif /* CONFIG_COMPAT */
833
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800834const struct file_operations fat_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 .read = generic_read_dir,
836 .readdir = fat_readdir,
837 .ioctl = fat_dir_ioctl,
David Howells188f83d2006-08-31 12:50:04 +0200838#ifdef CONFIG_COMPAT
839 .compat_ioctl = fat_compat_dir_ioctl,
840#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 .fsync = file_fsync,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +0200842 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843};
844
845static int fat_get_short_entry(struct inode *dir, loff_t *pos,
846 struct buffer_head **bh,
847 struct msdos_dir_entry **de)
848{
849 while (fat_get_entry(dir, pos, bh, de) >= 0) {
850 /* free entry or long name entry or volume label */
851 if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME))
852 return 0;
853 }
854 return -ENOENT;
855}
856
857/*
858 * The ".." entry can not provide the "struct fat_slot_info" informations
859 * for inode. So, this function provide the some informations only.
860 */
861int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh,
862 struct msdos_dir_entry **de, loff_t *i_pos)
863{
864 loff_t offset;
865
866 offset = 0;
867 *bh = NULL;
868 while (fat_get_short_entry(dir, &offset, bh, de) >= 0) {
869 if (!strncmp((*de)->name, MSDOS_DOTDOT, MSDOS_NAME)) {
870 *i_pos = fat_make_i_pos(dir->i_sb, *bh, *de);
871 return 0;
872 }
873 }
874 return -ENOENT;
875}
876
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -0800877EXPORT_SYMBOL_GPL(fat_get_dotdot_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879/* See if directory is empty */
880int fat_dir_empty(struct inode *dir)
881{
882 struct buffer_head *bh;
883 struct msdos_dir_entry *de;
884 loff_t cpos;
885 int result = 0;
886
887 bh = NULL;
888 cpos = 0;
889 while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
890 if (strncmp(de->name, MSDOS_DOT , MSDOS_NAME) &&
891 strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
892 result = -ENOTEMPTY;
893 break;
894 }
895 }
896 brelse(bh);
897 return result;
898}
899
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -0800900EXPORT_SYMBOL_GPL(fat_dir_empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902/*
903 * fat_subdirs counts the number of sub-directories of dir. It can be run
904 * on directories being created.
905 */
906int fat_subdirs(struct inode *dir)
907{
908 struct buffer_head *bh;
909 struct msdos_dir_entry *de;
910 loff_t cpos;
911 int count = 0;
912
913 bh = NULL;
914 cpos = 0;
915 while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
916 if (de->attr & ATTR_DIR)
917 count++;
918 }
919 brelse(bh);
920 return count;
921}
922
923/*
924 * Scans a directory for a given file (name points to its formatted name).
925 * Returns an error code or zero.
926 */
927int fat_scan(struct inode *dir, const unsigned char *name,
928 struct fat_slot_info *sinfo)
929{
930 struct super_block *sb = dir->i_sb;
931
932 sinfo->slot_off = 0;
933 sinfo->bh = NULL;
934 while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh,
935 &sinfo->de) >= 0) {
936 if (!strncmp(sinfo->de->name, name, MSDOS_NAME)) {
937 sinfo->slot_off -= sizeof(*sinfo->de);
938 sinfo->nr_slots = 1;
939 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
940 return 0;
941 }
942 }
943 return -ENOENT;
944}
945
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -0800946EXPORT_SYMBOL_GPL(fat_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948static int __fat_remove_entries(struct inode *dir, loff_t pos, int nr_slots)
949{
950 struct super_block *sb = dir->i_sb;
951 struct buffer_head *bh;
952 struct msdos_dir_entry *de, *endp;
953 int err = 0, orig_slots;
954
955 while (nr_slots) {
956 bh = NULL;
957 if (fat_get_entry(dir, &pos, &bh, &de) < 0) {
958 err = -EIO;
959 break;
960 }
961
962 orig_slots = nr_slots;
963 endp = (struct msdos_dir_entry *)(bh->b_data + sb->s_blocksize);
964 while (nr_slots && de < endp) {
965 de->name[0] = DELETED_FLAG;
966 de++;
967 nr_slots--;
968 }
969 mark_buffer_dirty(bh);
970 if (IS_DIRSYNC(dir))
971 err = sync_dirty_buffer(bh);
972 brelse(bh);
973 if (err)
974 break;
975
976 /* pos is *next* de's position, so this does `- sizeof(de)' */
977 pos += ((orig_slots - nr_slots) * sizeof(*de)) - sizeof(*de);
978 }
979
980 return err;
981}
982
983int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo)
984{
985 struct msdos_dir_entry *de;
986 struct buffer_head *bh;
987 int err = 0, nr_slots;
988
989 /*
990 * First stage: Remove the shortname. By this, the directory
991 * entry is removed.
992 */
993 nr_slots = sinfo->nr_slots;
994 de = sinfo->de;
995 sinfo->de = NULL;
996 bh = sinfo->bh;
997 sinfo->bh = NULL;
998 while (nr_slots && de >= (struct msdos_dir_entry *)bh->b_data) {
999 de->name[0] = DELETED_FLAG;
1000 de--;
1001 nr_slots--;
1002 }
1003 mark_buffer_dirty(bh);
1004 if (IS_DIRSYNC(dir))
1005 err = sync_dirty_buffer(bh);
1006 brelse(bh);
1007 if (err)
1008 return err;
1009 dir->i_version++;
1010
1011 if (nr_slots) {
1012 /*
1013 * Second stage: remove the remaining longname slots.
1014 * (This directory entry is already removed, and so return
1015 * the success)
1016 */
1017 err = __fat_remove_entries(dir, sinfo->slot_off, nr_slots);
1018 if (err) {
1019 printk(KERN_WARNING
1020 "FAT: Couldn't remove the long name slots\n");
1021 }
1022 }
1023
1024 dir->i_mtime = dir->i_atime = CURRENT_TIME_SEC;
1025 if (IS_DIRSYNC(dir))
1026 (void)fat_sync_inode(dir);
1027 else
1028 mark_inode_dirty(dir);
1029
1030 return 0;
1031}
1032
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -08001033EXPORT_SYMBOL_GPL(fat_remove_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035static int fat_zeroed_cluster(struct inode *dir, sector_t blknr, int nr_used,
1036 struct buffer_head **bhs, int nr_bhs)
1037{
1038 struct super_block *sb = dir->i_sb;
1039 sector_t last_blknr = blknr + MSDOS_SB(sb)->sec_per_clus;
1040 int err, i, n;
1041
1042 /* Zeroing the unused blocks on this cluster */
1043 blknr += nr_used;
1044 n = nr_used;
1045 while (blknr < last_blknr) {
1046 bhs[n] = sb_getblk(sb, blknr);
1047 if (!bhs[n]) {
1048 err = -ENOMEM;
1049 goto error;
1050 }
1051 memset(bhs[n]->b_data, 0, sb->s_blocksize);
1052 set_buffer_uptodate(bhs[n]);
1053 mark_buffer_dirty(bhs[n]);
1054
1055 n++;
1056 blknr++;
1057 if (n == nr_bhs) {
1058 if (IS_DIRSYNC(dir)) {
1059 err = fat_sync_bhs(bhs, n);
1060 if (err)
1061 goto error;
1062 }
1063 for (i = 0; i < n; i++)
1064 brelse(bhs[i]);
1065 n = 0;
1066 }
1067 }
1068 if (IS_DIRSYNC(dir)) {
1069 err = fat_sync_bhs(bhs, n);
1070 if (err)
1071 goto error;
1072 }
1073 for (i = 0; i < n; i++)
1074 brelse(bhs[i]);
1075
1076 return 0;
1077
1078error:
1079 for (i = 0; i < n; i++)
1080 bforget(bhs[i]);
1081 return err;
1082}
1083
1084int fat_alloc_new_dir(struct inode *dir, struct timespec *ts)
1085{
1086 struct super_block *sb = dir->i_sb;
1087 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1088 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
1089 struct msdos_dir_entry *de;
1090 sector_t blknr;
1091 __le16 date, time;
1092 int err, cluster;
1093
1094 err = fat_alloc_clusters(dir, &cluster, 1);
1095 if (err)
1096 goto error;
1097
1098 blknr = fat_clus_to_blknr(sbi, cluster);
1099 bhs[0] = sb_getblk(sb, blknr);
1100 if (!bhs[0]) {
1101 err = -ENOMEM;
1102 goto error_free;
1103 }
1104
Joe Petersonb271e062008-07-25 01:46:47 -07001105 fat_date_unix2dos(ts->tv_sec, &time, &date, sbi->options.tz_utc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 de = (struct msdos_dir_entry *)bhs[0]->b_data;
1108 /* filling the new directory slots ("." and ".." entries) */
1109 memcpy(de[0].name, MSDOS_DOT, MSDOS_NAME);
1110 memcpy(de[1].name, MSDOS_DOTDOT, MSDOS_NAME);
1111 de->attr = de[1].attr = ATTR_DIR;
1112 de[0].lcase = de[1].lcase = 0;
1113 de[0].time = de[1].time = time;
1114 de[0].date = de[1].date = date;
1115 de[0].ctime_cs = de[1].ctime_cs = 0;
1116 if (sbi->options.isvfat) {
1117 /* extra timestamps */
1118 de[0].ctime = de[1].ctime = time;
1119 de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = date;
1120 } else {
1121 de[0].ctime = de[1].ctime = 0;
1122 de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = 0;
1123 }
1124 de[0].start = cpu_to_le16(cluster);
1125 de[0].starthi = cpu_to_le16(cluster >> 16);
1126 de[1].start = cpu_to_le16(MSDOS_I(dir)->i_logstart);
1127 de[1].starthi = cpu_to_le16(MSDOS_I(dir)->i_logstart >> 16);
1128 de[0].size = de[1].size = 0;
1129 memset(de + 2, 0, sb->s_blocksize - 2 * sizeof(*de));
1130 set_buffer_uptodate(bhs[0]);
1131 mark_buffer_dirty(bhs[0]);
1132
1133 err = fat_zeroed_cluster(dir, blknr, 1, bhs, MAX_BUF_PER_PAGE);
1134 if (err)
1135 goto error_free;
1136
1137 return cluster;
1138
1139error_free:
1140 fat_free_clusters(dir, cluster);
1141error:
1142 return err;
1143}
1144
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -08001145EXPORT_SYMBOL_GPL(fat_alloc_new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147static int fat_add_new_entries(struct inode *dir, void *slots, int nr_slots,
1148 int *nr_cluster, struct msdos_dir_entry **de,
1149 struct buffer_head **bh, loff_t *i_pos)
1150{
1151 struct super_block *sb = dir->i_sb;
1152 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1153 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
1154 sector_t blknr, start_blknr, last_blknr;
1155 unsigned long size, copy;
1156 int err, i, n, offset, cluster[2];
1157
1158 /*
1159 * The minimum cluster size is 512bytes, and maximum entry
1160 * size is 32*slots (672bytes). So, iff the cluster size is
1161 * 512bytes, we may need two clusters.
1162 */
1163 size = nr_slots * sizeof(struct msdos_dir_entry);
1164 *nr_cluster = (size + (sbi->cluster_size - 1)) >> sbi->cluster_bits;
1165 BUG_ON(*nr_cluster > 2);
1166
1167 err = fat_alloc_clusters(dir, cluster, *nr_cluster);
1168 if (err)
1169 goto error;
1170
1171 /*
1172 * First stage: Fill the directory entry. NOTE: This cluster
1173 * is not referenced from any inode yet, so updates order is
1174 * not important.
1175 */
1176 i = n = copy = 0;
1177 do {
1178 start_blknr = blknr = fat_clus_to_blknr(sbi, cluster[i]);
1179 last_blknr = start_blknr + sbi->sec_per_clus;
1180 while (blknr < last_blknr) {
1181 bhs[n] = sb_getblk(sb, blknr);
1182 if (!bhs[n]) {
1183 err = -ENOMEM;
1184 goto error_nomem;
1185 }
1186
1187 /* fill the directory entry */
1188 copy = min(size, sb->s_blocksize);
1189 memcpy(bhs[n]->b_data, slots, copy);
1190 slots += copy;
1191 size -= copy;
1192 set_buffer_uptodate(bhs[n]);
1193 mark_buffer_dirty(bhs[n]);
1194 if (!size)
1195 break;
1196 n++;
1197 blknr++;
1198 }
1199 } while (++i < *nr_cluster);
1200
1201 memset(bhs[n]->b_data + copy, 0, sb->s_blocksize - copy);
1202 offset = copy - sizeof(struct msdos_dir_entry);
1203 get_bh(bhs[n]);
1204 *bh = bhs[n];
1205 *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
1206 *i_pos = fat_make_i_pos(sb, *bh, *de);
1207
1208 /* Second stage: clear the rest of cluster, and write outs */
1209 err = fat_zeroed_cluster(dir, start_blknr, ++n, bhs, MAX_BUF_PER_PAGE);
1210 if (err)
1211 goto error_free;
1212
1213 return cluster[0];
1214
1215error_free:
1216 brelse(*bh);
1217 *bh = NULL;
1218 n = 0;
1219error_nomem:
1220 for (i = 0; i < n; i++)
1221 bforget(bhs[i]);
1222 fat_free_clusters(dir, cluster[0]);
1223error:
1224 return err;
1225}
1226
1227int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
1228 struct fat_slot_info *sinfo)
1229{
1230 struct super_block *sb = dir->i_sb;
1231 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1232 struct buffer_head *bh, *prev, *bhs[3]; /* 32*slots (672bytes) */
1233 struct msdos_dir_entry *de;
1234 int err, free_slots, i, nr_bhs;
1235 loff_t pos, i_pos;
1236
1237 sinfo->nr_slots = nr_slots;
1238
1239 /* First stage: search free direcotry entries */
1240 free_slots = nr_bhs = 0;
1241 bh = prev = NULL;
1242 pos = 0;
1243 err = -ENOSPC;
1244 while (fat_get_entry(dir, &pos, &bh, &de) > -1) {
1245 /* check the maximum size of directory */
1246 if (pos >= FAT_MAX_DIR_SIZE)
1247 goto error;
1248
1249 if (IS_FREE(de->name)) {
1250 if (prev != bh) {
1251 get_bh(bh);
1252 bhs[nr_bhs] = prev = bh;
1253 nr_bhs++;
1254 }
1255 free_slots++;
1256 if (free_slots == nr_slots)
1257 goto found;
1258 } else {
1259 for (i = 0; i < nr_bhs; i++)
1260 brelse(bhs[i]);
1261 prev = NULL;
1262 free_slots = nr_bhs = 0;
1263 }
1264 }
1265 if (dir->i_ino == MSDOS_ROOT_INO) {
1266 if (sbi->fat_bits != 32)
1267 goto error;
1268 } else if (MSDOS_I(dir)->i_start == 0) {
1269 printk(KERN_ERR "FAT: Corrupted directory (i_pos %lld)\n",
1270 MSDOS_I(dir)->i_pos);
1271 err = -EIO;
1272 goto error;
1273 }
1274
1275found:
1276 err = 0;
1277 pos -= free_slots * sizeof(*de);
1278 nr_slots -= free_slots;
1279 if (free_slots) {
1280 /*
1281 * Second stage: filling the free entries with new entries.
1282 * NOTE: If this slots has shortname, first, we write
1283 * the long name slots, then write the short name.
1284 */
1285 int size = free_slots * sizeof(*de);
1286 int offset = pos & (sb->s_blocksize - 1);
1287 int long_bhs = nr_bhs - (nr_slots == 0);
1288
1289 /* Fill the long name slots. */
1290 for (i = 0; i < long_bhs; i++) {
1291 int copy = min_t(int, sb->s_blocksize - offset, size);
1292 memcpy(bhs[i]->b_data + offset, slots, copy);
1293 mark_buffer_dirty(bhs[i]);
1294 offset = 0;
1295 slots += copy;
1296 size -= copy;
1297 }
1298 if (long_bhs && IS_DIRSYNC(dir))
1299 err = fat_sync_bhs(bhs, long_bhs);
1300 if (!err && i < nr_bhs) {
1301 /* Fill the short name slot. */
1302 int copy = min_t(int, sb->s_blocksize - offset, size);
1303 memcpy(bhs[i]->b_data + offset, slots, copy);
1304 mark_buffer_dirty(bhs[i]);
1305 if (IS_DIRSYNC(dir))
1306 err = sync_dirty_buffer(bhs[i]);
1307 }
1308 for (i = 0; i < nr_bhs; i++)
1309 brelse(bhs[i]);
1310 if (err)
1311 goto error_remove;
1312 }
1313
1314 if (nr_slots) {
1315 int cluster, nr_cluster;
1316
1317 /*
1318 * Third stage: allocate the cluster for new entries.
1319 * And initialize the cluster with new entries, then
1320 * add the cluster to dir.
1321 */
1322 cluster = fat_add_new_entries(dir, slots, nr_slots, &nr_cluster,
1323 &de, &bh, &i_pos);
1324 if (cluster < 0) {
1325 err = cluster;
1326 goto error_remove;
1327 }
1328 err = fat_chain_add(dir, cluster, nr_cluster);
1329 if (err) {
1330 fat_free_clusters(dir, cluster);
1331 goto error_remove;
1332 }
1333 if (dir->i_size & (sbi->cluster_size - 1)) {
1334 fat_fs_panic(sb, "Odd directory size");
1335 dir->i_size = (dir->i_size + sbi->cluster_size - 1)
1336 & ~((loff_t)sbi->cluster_size - 1);
1337 }
1338 dir->i_size += nr_cluster << sbi->cluster_bits;
1339 MSDOS_I(dir)->mmu_private += nr_cluster << sbi->cluster_bits;
1340 }
1341 sinfo->slot_off = pos;
1342 sinfo->de = de;
1343 sinfo->bh = bh;
1344 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
1345
1346 return 0;
1347
1348error:
1349 brelse(bh);
1350 for (i = 0; i < nr_bhs; i++)
1351 brelse(bhs[i]);
1352 return err;
1353
1354error_remove:
1355 brelse(bh);
1356 if (free_slots)
1357 __fat_remove_entries(dir, pos, free_slots);
1358 return err;
1359}
1360
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -08001361EXPORT_SYMBOL_GPL(fat_add_entries);