blob: a4410740627f6aab864057ad581e8ba25be60a6c [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/*
328 * Return values: negative -> error, 0 -> not found, positive -> found,
329 * value is the total amount of slots, including the shortname entry.
330 */
331int fat_search_long(struct inode *inode, const unsigned char *name,
332 int name_len, struct fat_slot_info *sinfo)
333{
334 struct super_block *sb = inode->i_sb;
335 struct msdos_sb_info *sbi = MSDOS_SB(sb);
336 struct buffer_head *bh = NULL;
337 struct msdos_dir_entry *de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 struct nls_table *nls_disk = sbi->nls_disk;
Keith Mokf22032b2008-04-28 02:16:29 -0700339 unsigned char nr_slots;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700340 wchar_t bufuname[14];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 wchar_t *unicode = NULL;
Keith Mokf22032b2008-04-28 02:16:29 -0700342 unsigned char work[MSDOS_NAME];
343 unsigned char *bufname = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 unsigned short opt_shortname = sbi->options.shortname;
345 loff_t cpos = 0;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700346 int chl, i, j, last_u, err, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
OGAWA Hirofumic7a6c4e2008-04-28 02:16:29 -0700348 bufname = __getname();
Keith Mokf22032b2008-04-28 02:16:29 -0700349 if (!bufname)
350 return -ENOMEM;
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 err = -ENOENT;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700353 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700355 goto end_of_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356parse_record:
357 nr_slots = 0;
358 if (de->name[0] == DELETED_FLAG)
359 continue;
360 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
361 continue;
362 if (de->attr != ATTR_EXT && IS_FREE(de->name))
363 continue;
364 if (de->attr == ATTR_EXT) {
Pekka Enbergad2c1602005-10-30 15:03:50 -0800365 int status = fat_parse_long(inode, &cpos, &bh, &de,
366 &unicode, &nr_slots);
367 if (status < 0)
368 return status;
369 else if (status == PARSE_INVALID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 continue;
Pekka Enbergad2c1602005-10-30 15:03:50 -0800371 else if (status == PARSE_NOT_LONGNAME)
372 goto parse_record;
373 else if (status == PARSE_EOF)
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700374 goto end_of_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376
377 memcpy(work, de->name, sizeof(de->name));
378 /* see namei.c, msdos_format_name */
379 if (work[0] == 0x05)
380 work[0] = 0xE5;
381 for (i = 0, j = 0, last_u = 0; i < 8;) {
OGAWA Hirofumi9aacd592007-07-15 23:39:56 -0700382 if (!work[i])
383 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
385 &bufuname[j++], opt_shortname,
386 de->lcase & CASE_LOWER_BASE);
387 if (chl <= 1) {
388 if (work[i] != ' ')
389 last_u = j;
390 } else {
391 last_u = j;
392 }
393 i += chl;
394 }
395 j = last_u;
396 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
OGAWA Hirofumi9aacd592007-07-15 23:39:56 -0700397 for (i = 8; i < MSDOS_NAME;) {
398 if (!work[i])
399 break;
400 chl = fat_shortname2uni(nls_disk, &work[i],
401 MSDOS_NAME - i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 &bufuname[j++], opt_shortname,
403 de->lcase & CASE_LOWER_EXT);
404 if (chl <= 1) {
OGAWA Hirofumi9aacd592007-07-15 23:39:56 -0700405 if (work[i] != ' ')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 last_u = j;
407 } else {
408 last_u = j;
409 }
410 i += chl;
411 }
412 if (!last_u)
413 continue;
414
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700415 /* Compare shortname */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 bufuname[last_u] = 0x0000;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700417 len = fat_uni_to_x8(sbi, bufuname, bufname, PATH_MAX);
418 if (fat_name_match(sbi, name, name_len, bufname, len))
419 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 if (nr_slots) {
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700422 /* Compare longname */
423 len = fat_uni_to_x8(sbi, unicode, bufname, PATH_MAX);
424 if (fat_name_match(sbi, name, name_len, bufname, len))
425 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427 }
428
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700429found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 nr_slots++; /* include the de */
431 sinfo->slot_off = cpos - nr_slots * sizeof(*de);
432 sinfo->nr_slots = nr_slots;
433 sinfo->de = de;
434 sinfo->bh = bh;
435 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
436 err = 0;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700437end_of_dir:
Keith Mokf22032b2008-04-28 02:16:29 -0700438 if (bufname)
OGAWA Hirofumic7a6c4e2008-04-28 02:16:29 -0700439 __putname(bufname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (unicode)
OGAWA Hirofumic7a6c4e2008-04-28 02:16:29 -0700441 __putname(unicode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 return err;
444}
445
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -0800446EXPORT_SYMBOL_GPL(fat_search_long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448struct fat_ioctl_filldir_callback {
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700449 void __user *dirent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 int result;
451 /* for dir ioctl */
452 const char *longname;
453 int long_len;
454 const char *shortname;
455 int short_len;
456};
457
Pekka Enbergad2c1602005-10-30 15:03:50 -0800458static int __fat_readdir(struct inode *inode, struct file *filp, void *dirent,
459 filldir_t filldir, int short_only, int both)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 struct super_block *sb = inode->i_sb;
462 struct msdos_sb_info *sbi = MSDOS_SB(sb);
463 struct buffer_head *bh;
464 struct msdos_dir_entry *de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 struct nls_table *nls_disk = sbi->nls_disk;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700466 unsigned char nr_slots;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 wchar_t bufuname[14];
468 wchar_t *unicode = NULL;
OGAWA Hirofumi9aacd592007-07-15 23:39:56 -0700469 unsigned char c, work[MSDOS_NAME], bufname[56], *ptname = bufname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 unsigned short opt_shortname = sbi->options.shortname;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700471 int isvfat = sbi->options.isvfat;
472 int nocase = sbi->options.nocase;
473 const char *fill_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 unsigned long inum;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700475 unsigned long lpos, dummy, *furrfu = &lpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 loff_t cpos;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700477 int chi, chl, i, i2, j, last, last_u, dotoffset = 0, fill_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 int ret = 0;
479
Linus Torvalds8f593422008-05-19 19:53:01 -0700480 lock_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 cpos = filp->f_pos;
483 /* Fake . and .. for the root directory. */
484 if (inode->i_ino == MSDOS_ROOT_INO) {
485 while (cpos < 2) {
486 if (filldir(dirent, "..", cpos+1, cpos, MSDOS_ROOT_INO, DT_DIR) < 0)
487 goto out;
488 cpos++;
489 filp->f_pos++;
490 }
491 if (cpos == 2) {
492 dummy = 2;
493 furrfu = &dummy;
494 cpos = 0;
495 }
496 }
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700497 if (cpos & (sizeof(struct msdos_dir_entry) - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 ret = -ENOENT;
499 goto out;
500 }
501
502 bh = NULL;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700503get_new:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700505 goto end_of_dir;
Pekka Enbergad2c1602005-10-30 15:03:50 -0800506parse_record:
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700507 nr_slots = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 /* Check for long filename entry */
509 if (isvfat) {
510 if (de->name[0] == DELETED_FLAG)
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700511 goto record_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700513 goto record_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (de->attr != ATTR_EXT && IS_FREE(de->name))
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700515 goto record_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 } else {
517 if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700518 goto record_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520
521 if (isvfat && de->attr == ATTR_EXT) {
Pekka Enbergad2c1602005-10-30 15:03:50 -0800522 int status = fat_parse_long(inode, &cpos, &bh, &de,
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700523 &unicode, &nr_slots);
Pekka Enbergad2c1602005-10-30 15:03:50 -0800524 if (status < 0) {
525 filp->f_pos = cpos;
526 ret = status;
527 goto out;
528 } else if (status == PARSE_INVALID)
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700529 goto record_end;
Pekka Enbergad2c1602005-10-30 15:03:50 -0800530 else if (status == PARSE_NOT_LONGNAME)
531 goto parse_record;
532 else if (status == PARSE_EOF)
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700533 goto end_of_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535
536 if (sbi->options.dotsOK) {
537 ptname = bufname;
538 dotoffset = 0;
539 if (de->attr & ATTR_HIDDEN) {
540 *ptname++ = '.';
541 dotoffset = 1;
542 }
543 }
544
545 memcpy(work, de->name, sizeof(de->name));
546 /* see namei.c, msdos_format_name */
547 if (work[0] == 0x05)
548 work[0] = 0xE5;
549 for (i = 0, j = 0, last = 0, last_u = 0; i < 8;) {
OGAWA Hirofumi9aacd592007-07-15 23:39:56 -0700550 if (!(c = work[i]))
551 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
553 &bufuname[j++], opt_shortname,
554 de->lcase & CASE_LOWER_BASE);
555 if (chl <= 1) {
556 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
557 if (c != ' ') {
558 last = i;
559 last_u = j;
560 }
561 } else {
562 last_u = j;
563 for (chi = 0; chi < chl && i < 8; chi++) {
564 ptname[i] = work[i];
565 i++; last = i;
566 }
567 }
568 }
569 i = last;
570 j = last_u;
571 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
572 ptname[i++] = '.';
OGAWA Hirofumi9aacd592007-07-15 23:39:56 -0700573 for (i2 = 8; i2 < MSDOS_NAME;) {
574 if (!(c = work[i2]))
575 break;
576 chl = fat_shortname2uni(nls_disk, &work[i2], MSDOS_NAME - i2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 &bufuname[j++], opt_shortname,
578 de->lcase & CASE_LOWER_EXT);
579 if (chl <= 1) {
580 i2++;
581 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
582 if (c != ' ') {
583 last = i;
584 last_u = j;
585 }
586 } else {
587 last_u = j;
OGAWA Hirofumi9aacd592007-07-15 23:39:56 -0700588 for (chi = 0; chi < chl && i2 < MSDOS_NAME; chi++) {
589 ptname[i++] = work[i2++];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 last = i;
591 }
592 }
593 }
594 if (!last)
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700595 goto record_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 i = last + dotoffset;
598 j = last_u;
599
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700600 lpos = cpos - (nr_slots + 1) * sizeof(struct msdos_dir_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (!memcmp(de->name, MSDOS_DOT, MSDOS_NAME))
602 inum = inode->i_ino;
603 else if (!memcmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
Josef "Jeff" Sipekdba32302006-12-08 02:36:39 -0800604 inum = parent_ino(filp->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 } else {
606 loff_t i_pos = fat_make_i_pos(sb, bh, de);
607 struct inode *tmp = fat_iget(sb, i_pos);
608 if (tmp) {
609 inum = tmp->i_ino;
610 iput(tmp);
611 } else
612 inum = iunique(sb, MSDOS_ROOT_INO);
613 }
614
615 if (isvfat) {
616 bufuname[j] = 0x0000;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700617 i = fat_uni_to_x8(sbi, bufuname, bufname, sizeof(bufname));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619
620 fill_name = bufname;
621 fill_len = i;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700622 if (!short_only && nr_slots) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 /* convert the unicode long name. 261 is maximum size
624 * of unicode buffer. (13 * slots + nul) */
625 void *longname = unicode + 261;
OGAWA Hirofumic7a6c4e2008-04-28 02:16:29 -0700626 int buf_size = PATH_MAX - (261 * sizeof(unicode[0]));
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700627 int long_len = fat_uni_to_x8(sbi, unicode, longname, buf_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 if (!both) {
630 fill_name = longname;
631 fill_len = long_len;
632 } else {
633 /* hack for fat_ioctl_filldir() */
634 struct fat_ioctl_filldir_callback *p = dirent;
635
636 p->longname = longname;
637 p->long_len = long_len;
638 p->shortname = bufname;
639 p->short_len = i;
640 fill_name = NULL;
641 fill_len = 0;
642 }
643 }
644 if (filldir(dirent, fill_name, fill_len, *furrfu, inum,
645 (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700646 goto fill_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700648record_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 furrfu = &lpos;
650 filp->f_pos = cpos;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700651 goto get_new;
652end_of_dir:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 filp->f_pos = cpos;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700654fill_failed:
Karsten Wiesef3ef6f62005-09-06 15:17:12 -0700655 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (unicode)
OGAWA Hirofumic7a6c4e2008-04-28 02:16:29 -0700657 __putname(unicode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658out:
Linus Torvalds8f593422008-05-19 19:53:01 -0700659 unlock_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return ret;
661}
662
663static int fat_readdir(struct file *filp, void *dirent, filldir_t filldir)
664{
Josef "Jeff" Sipekdba32302006-12-08 02:36:39 -0800665 struct inode *inode = filp->f_path.dentry->d_inode;
Pekka Enbergad2c1602005-10-30 15:03:50 -0800666 return __fat_readdir(inode, filp, dirent, filldir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700669#define FAT_IOCTL_FILLDIR_FUNC(func, dirent_type) \
670static int func(void *__buf, const char *name, int name_len, \
671 loff_t offset, u64 ino, unsigned int d_type) \
672{ \
673 struct fat_ioctl_filldir_callback *buf = __buf; \
674 struct dirent_type __user *d1 = buf->dirent; \
675 struct dirent_type __user *d2 = d1 + 1; \
676 \
677 if (buf->result) \
678 return -EINVAL; \
679 buf->result++; \
680 \
681 if (name != NULL) { \
682 /* dirent has only short name */ \
683 if (name_len >= sizeof(d1->d_name)) \
684 name_len = sizeof(d1->d_name) - 1; \
685 \
686 if (put_user(0, d2->d_name) || \
687 put_user(0, &d2->d_reclen) || \
688 copy_to_user(d1->d_name, name, name_len) || \
689 put_user(0, d1->d_name + name_len) || \
690 put_user(name_len, &d1->d_reclen)) \
691 goto efault; \
692 } else { \
693 /* dirent has short and long name */ \
694 const char *longname = buf->longname; \
695 int long_len = buf->long_len; \
696 const char *shortname = buf->shortname; \
697 int short_len = buf->short_len; \
698 \
699 if (long_len >= sizeof(d1->d_name)) \
700 long_len = sizeof(d1->d_name) - 1; \
701 if (short_len >= sizeof(d1->d_name)) \
702 short_len = sizeof(d1->d_name) - 1; \
703 \
704 if (copy_to_user(d2->d_name, longname, long_len) || \
705 put_user(0, d2->d_name + long_len) || \
706 put_user(long_len, &d2->d_reclen) || \
707 put_user(ino, &d2->d_ino) || \
708 put_user(offset, &d2->d_off) || \
709 copy_to_user(d1->d_name, shortname, short_len) || \
710 put_user(0, d1->d_name + short_len) || \
711 put_user(short_len, &d1->d_reclen)) \
712 goto efault; \
713 } \
714 return 0; \
715efault: \
716 buf->result = -EFAULT; \
717 return -EFAULT; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
Adrian Bunk531f7102008-07-25 01:46:43 -0700720FAT_IOCTL_FILLDIR_FUNC(fat_ioctl_filldir, __fat_dirent)
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700721
722static int fat_ioctl_readdir(struct inode *inode, struct file *filp,
723 void __user *dirent, filldir_t filldir,
724 int short_only, int both)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 struct fat_ioctl_filldir_callback buf;
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700727 int ret;
728
729 buf.dirent = dirent;
730 buf.result = 0;
731 mutex_lock(&inode->i_mutex);
732 ret = -ENOENT;
733 if (!IS_DEADDIR(inode)) {
734 ret = __fat_readdir(inode, filp, &buf, filldir,
735 short_only, both);
736 }
737 mutex_unlock(&inode->i_mutex);
738 if (ret >= 0)
739 ret = buf.result;
740 return ret;
741}
742
743static int fat_dir_ioctl(struct inode *inode, struct file *filp,
744 unsigned int cmd, unsigned long arg)
745{
Adrian Bunk531f7102008-07-25 01:46:43 -0700746 struct __fat_dirent __user *d1 = (struct __fat_dirent __user *)arg;
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700747 int short_only, both;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 switch (cmd) {
750 case VFAT_IOCTL_READDIR_SHORT:
751 short_only = 1;
752 both = 0;
753 break;
754 case VFAT_IOCTL_READDIR_BOTH:
755 short_only = 0;
756 both = 1;
757 break;
758 default:
759 return fat_generic_ioctl(inode, filp, cmd, arg);
760 }
761
Adrian Bunk531f7102008-07-25 01:46:43 -0700762 if (!access_ok(VERIFY_WRITE, d1, sizeof(struct __fat_dirent[2])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return -EFAULT;
764 /*
765 * Yes, we don't need this put_user() absolutely. However old
766 * code didn't return the right value. So, app use this value,
767 * in order to check whether it is EOF.
768 */
769 if (put_user(0, &d1->d_reclen))
770 return -EFAULT;
771
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700772 return fat_ioctl_readdir(inode, filp, d1, fat_ioctl_filldir,
773 short_only, both);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
David Howells188f83d2006-08-31 12:50:04 +0200776#ifdef CONFIG_COMPAT
777#define VFAT_IOCTL_READDIR_BOTH32 _IOR('r', 1, struct compat_dirent[2])
778#define VFAT_IOCTL_READDIR_SHORT32 _IOR('r', 2, struct compat_dirent[2])
779
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700780FAT_IOCTL_FILLDIR_FUNC(fat_compat_ioctl_filldir, compat_dirent)
David Howells188f83d2006-08-31 12:50:04 +0200781
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700782static long fat_compat_dir_ioctl(struct file *filp, unsigned cmd,
David Howells188f83d2006-08-31 12:50:04 +0200783 unsigned long arg)
784{
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700785 struct inode *inode = filp->f_path.dentry->d_inode;
786 struct compat_dirent __user *d1 = compat_ptr(arg);
787 int short_only, both;
David Howells188f83d2006-08-31 12:50:04 +0200788
789 switch (cmd) {
David Howells188f83d2006-08-31 12:50:04 +0200790 case VFAT_IOCTL_READDIR_SHORT32:
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700791 short_only = 1;
792 both = 0;
793 break;
794 case VFAT_IOCTL_READDIR_BOTH32:
795 short_only = 0;
796 both = 1;
David Howells188f83d2006-08-31 12:50:04 +0200797 break;
798 default:
799 return -ENOIOCTLCMD;
800 }
801
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700802 if (!access_ok(VERIFY_WRITE, d1, sizeof(struct compat_dirent[2])))
803 return -EFAULT;
804 /*
805 * Yes, we don't need this put_user() absolutely. However old
806 * code didn't return the right value. So, app use this value,
807 * in order to check whether it is EOF.
808 */
809 if (put_user(0, &d1->d_reclen))
810 return -EFAULT;
811
812 return fat_ioctl_readdir(inode, filp, d1, fat_compat_ioctl_filldir,
813 short_only, both);
David Howells188f83d2006-08-31 12:50:04 +0200814}
815#endif /* CONFIG_COMPAT */
816
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800817const struct file_operations fat_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 .read = generic_read_dir,
819 .readdir = fat_readdir,
820 .ioctl = fat_dir_ioctl,
David Howells188f83d2006-08-31 12:50:04 +0200821#ifdef CONFIG_COMPAT
822 .compat_ioctl = fat_compat_dir_ioctl,
823#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 .fsync = file_fsync,
825};
826
827static int fat_get_short_entry(struct inode *dir, loff_t *pos,
828 struct buffer_head **bh,
829 struct msdos_dir_entry **de)
830{
831 while (fat_get_entry(dir, pos, bh, de) >= 0) {
832 /* free entry or long name entry or volume label */
833 if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME))
834 return 0;
835 }
836 return -ENOENT;
837}
838
839/*
840 * The ".." entry can not provide the "struct fat_slot_info" informations
841 * for inode. So, this function provide the some informations only.
842 */
843int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh,
844 struct msdos_dir_entry **de, loff_t *i_pos)
845{
846 loff_t offset;
847
848 offset = 0;
849 *bh = NULL;
850 while (fat_get_short_entry(dir, &offset, bh, de) >= 0) {
851 if (!strncmp((*de)->name, MSDOS_DOTDOT, MSDOS_NAME)) {
852 *i_pos = fat_make_i_pos(dir->i_sb, *bh, *de);
853 return 0;
854 }
855 }
856 return -ENOENT;
857}
858
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -0800859EXPORT_SYMBOL_GPL(fat_get_dotdot_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861/* See if directory is empty */
862int fat_dir_empty(struct inode *dir)
863{
864 struct buffer_head *bh;
865 struct msdos_dir_entry *de;
866 loff_t cpos;
867 int result = 0;
868
869 bh = NULL;
870 cpos = 0;
871 while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
872 if (strncmp(de->name, MSDOS_DOT , MSDOS_NAME) &&
873 strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
874 result = -ENOTEMPTY;
875 break;
876 }
877 }
878 brelse(bh);
879 return result;
880}
881
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -0800882EXPORT_SYMBOL_GPL(fat_dir_empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884/*
885 * fat_subdirs counts the number of sub-directories of dir. It can be run
886 * on directories being created.
887 */
888int fat_subdirs(struct inode *dir)
889{
890 struct buffer_head *bh;
891 struct msdos_dir_entry *de;
892 loff_t cpos;
893 int count = 0;
894
895 bh = NULL;
896 cpos = 0;
897 while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
898 if (de->attr & ATTR_DIR)
899 count++;
900 }
901 brelse(bh);
902 return count;
903}
904
905/*
906 * Scans a directory for a given file (name points to its formatted name).
907 * Returns an error code or zero.
908 */
909int fat_scan(struct inode *dir, const unsigned char *name,
910 struct fat_slot_info *sinfo)
911{
912 struct super_block *sb = dir->i_sb;
913
914 sinfo->slot_off = 0;
915 sinfo->bh = NULL;
916 while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh,
917 &sinfo->de) >= 0) {
918 if (!strncmp(sinfo->de->name, name, MSDOS_NAME)) {
919 sinfo->slot_off -= sizeof(*sinfo->de);
920 sinfo->nr_slots = 1;
921 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
922 return 0;
923 }
924 }
925 return -ENOENT;
926}
927
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -0800928EXPORT_SYMBOL_GPL(fat_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930static int __fat_remove_entries(struct inode *dir, loff_t pos, int nr_slots)
931{
932 struct super_block *sb = dir->i_sb;
933 struct buffer_head *bh;
934 struct msdos_dir_entry *de, *endp;
935 int err = 0, orig_slots;
936
937 while (nr_slots) {
938 bh = NULL;
939 if (fat_get_entry(dir, &pos, &bh, &de) < 0) {
940 err = -EIO;
941 break;
942 }
943
944 orig_slots = nr_slots;
945 endp = (struct msdos_dir_entry *)(bh->b_data + sb->s_blocksize);
946 while (nr_slots && de < endp) {
947 de->name[0] = DELETED_FLAG;
948 de++;
949 nr_slots--;
950 }
951 mark_buffer_dirty(bh);
952 if (IS_DIRSYNC(dir))
953 err = sync_dirty_buffer(bh);
954 brelse(bh);
955 if (err)
956 break;
957
958 /* pos is *next* de's position, so this does `- sizeof(de)' */
959 pos += ((orig_slots - nr_slots) * sizeof(*de)) - sizeof(*de);
960 }
961
962 return err;
963}
964
965int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo)
966{
967 struct msdos_dir_entry *de;
968 struct buffer_head *bh;
969 int err = 0, nr_slots;
970
971 /*
972 * First stage: Remove the shortname. By this, the directory
973 * entry is removed.
974 */
975 nr_slots = sinfo->nr_slots;
976 de = sinfo->de;
977 sinfo->de = NULL;
978 bh = sinfo->bh;
979 sinfo->bh = NULL;
980 while (nr_slots && de >= (struct msdos_dir_entry *)bh->b_data) {
981 de->name[0] = DELETED_FLAG;
982 de--;
983 nr_slots--;
984 }
985 mark_buffer_dirty(bh);
986 if (IS_DIRSYNC(dir))
987 err = sync_dirty_buffer(bh);
988 brelse(bh);
989 if (err)
990 return err;
991 dir->i_version++;
992
993 if (nr_slots) {
994 /*
995 * Second stage: remove the remaining longname slots.
996 * (This directory entry is already removed, and so return
997 * the success)
998 */
999 err = __fat_remove_entries(dir, sinfo->slot_off, nr_slots);
1000 if (err) {
1001 printk(KERN_WARNING
1002 "FAT: Couldn't remove the long name slots\n");
1003 }
1004 }
1005
1006 dir->i_mtime = dir->i_atime = CURRENT_TIME_SEC;
1007 if (IS_DIRSYNC(dir))
1008 (void)fat_sync_inode(dir);
1009 else
1010 mark_inode_dirty(dir);
1011
1012 return 0;
1013}
1014
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -08001015EXPORT_SYMBOL_GPL(fat_remove_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017static int fat_zeroed_cluster(struct inode *dir, sector_t blknr, int nr_used,
1018 struct buffer_head **bhs, int nr_bhs)
1019{
1020 struct super_block *sb = dir->i_sb;
1021 sector_t last_blknr = blknr + MSDOS_SB(sb)->sec_per_clus;
1022 int err, i, n;
1023
1024 /* Zeroing the unused blocks on this cluster */
1025 blknr += nr_used;
1026 n = nr_used;
1027 while (blknr < last_blknr) {
1028 bhs[n] = sb_getblk(sb, blknr);
1029 if (!bhs[n]) {
1030 err = -ENOMEM;
1031 goto error;
1032 }
1033 memset(bhs[n]->b_data, 0, sb->s_blocksize);
1034 set_buffer_uptodate(bhs[n]);
1035 mark_buffer_dirty(bhs[n]);
1036
1037 n++;
1038 blknr++;
1039 if (n == nr_bhs) {
1040 if (IS_DIRSYNC(dir)) {
1041 err = fat_sync_bhs(bhs, n);
1042 if (err)
1043 goto error;
1044 }
1045 for (i = 0; i < n; i++)
1046 brelse(bhs[i]);
1047 n = 0;
1048 }
1049 }
1050 if (IS_DIRSYNC(dir)) {
1051 err = fat_sync_bhs(bhs, n);
1052 if (err)
1053 goto error;
1054 }
1055 for (i = 0; i < n; i++)
1056 brelse(bhs[i]);
1057
1058 return 0;
1059
1060error:
1061 for (i = 0; i < n; i++)
1062 bforget(bhs[i]);
1063 return err;
1064}
1065
1066int fat_alloc_new_dir(struct inode *dir, struct timespec *ts)
1067{
1068 struct super_block *sb = dir->i_sb;
1069 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1070 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
1071 struct msdos_dir_entry *de;
1072 sector_t blknr;
1073 __le16 date, time;
1074 int err, cluster;
1075
1076 err = fat_alloc_clusters(dir, &cluster, 1);
1077 if (err)
1078 goto error;
1079
1080 blknr = fat_clus_to_blknr(sbi, cluster);
1081 bhs[0] = sb_getblk(sb, blknr);
1082 if (!bhs[0]) {
1083 err = -ENOMEM;
1084 goto error_free;
1085 }
1086
1087 fat_date_unix2dos(ts->tv_sec, &time, &date);
1088
1089 de = (struct msdos_dir_entry *)bhs[0]->b_data;
1090 /* filling the new directory slots ("." and ".." entries) */
1091 memcpy(de[0].name, MSDOS_DOT, MSDOS_NAME);
1092 memcpy(de[1].name, MSDOS_DOTDOT, MSDOS_NAME);
1093 de->attr = de[1].attr = ATTR_DIR;
1094 de[0].lcase = de[1].lcase = 0;
1095 de[0].time = de[1].time = time;
1096 de[0].date = de[1].date = date;
1097 de[0].ctime_cs = de[1].ctime_cs = 0;
1098 if (sbi->options.isvfat) {
1099 /* extra timestamps */
1100 de[0].ctime = de[1].ctime = time;
1101 de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = date;
1102 } else {
1103 de[0].ctime = de[1].ctime = 0;
1104 de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = 0;
1105 }
1106 de[0].start = cpu_to_le16(cluster);
1107 de[0].starthi = cpu_to_le16(cluster >> 16);
1108 de[1].start = cpu_to_le16(MSDOS_I(dir)->i_logstart);
1109 de[1].starthi = cpu_to_le16(MSDOS_I(dir)->i_logstart >> 16);
1110 de[0].size = de[1].size = 0;
1111 memset(de + 2, 0, sb->s_blocksize - 2 * sizeof(*de));
1112 set_buffer_uptodate(bhs[0]);
1113 mark_buffer_dirty(bhs[0]);
1114
1115 err = fat_zeroed_cluster(dir, blknr, 1, bhs, MAX_BUF_PER_PAGE);
1116 if (err)
1117 goto error_free;
1118
1119 return cluster;
1120
1121error_free:
1122 fat_free_clusters(dir, cluster);
1123error:
1124 return err;
1125}
1126
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -08001127EXPORT_SYMBOL_GPL(fat_alloc_new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129static int fat_add_new_entries(struct inode *dir, void *slots, int nr_slots,
1130 int *nr_cluster, struct msdos_dir_entry **de,
1131 struct buffer_head **bh, loff_t *i_pos)
1132{
1133 struct super_block *sb = dir->i_sb;
1134 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1135 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
1136 sector_t blknr, start_blknr, last_blknr;
1137 unsigned long size, copy;
1138 int err, i, n, offset, cluster[2];
1139
1140 /*
1141 * The minimum cluster size is 512bytes, and maximum entry
1142 * size is 32*slots (672bytes). So, iff the cluster size is
1143 * 512bytes, we may need two clusters.
1144 */
1145 size = nr_slots * sizeof(struct msdos_dir_entry);
1146 *nr_cluster = (size + (sbi->cluster_size - 1)) >> sbi->cluster_bits;
1147 BUG_ON(*nr_cluster > 2);
1148
1149 err = fat_alloc_clusters(dir, cluster, *nr_cluster);
1150 if (err)
1151 goto error;
1152
1153 /*
1154 * First stage: Fill the directory entry. NOTE: This cluster
1155 * is not referenced from any inode yet, so updates order is
1156 * not important.
1157 */
1158 i = n = copy = 0;
1159 do {
1160 start_blknr = blknr = fat_clus_to_blknr(sbi, cluster[i]);
1161 last_blknr = start_blknr + sbi->sec_per_clus;
1162 while (blknr < last_blknr) {
1163 bhs[n] = sb_getblk(sb, blknr);
1164 if (!bhs[n]) {
1165 err = -ENOMEM;
1166 goto error_nomem;
1167 }
1168
1169 /* fill the directory entry */
1170 copy = min(size, sb->s_blocksize);
1171 memcpy(bhs[n]->b_data, slots, copy);
1172 slots += copy;
1173 size -= copy;
1174 set_buffer_uptodate(bhs[n]);
1175 mark_buffer_dirty(bhs[n]);
1176 if (!size)
1177 break;
1178 n++;
1179 blknr++;
1180 }
1181 } while (++i < *nr_cluster);
1182
1183 memset(bhs[n]->b_data + copy, 0, sb->s_blocksize - copy);
1184 offset = copy - sizeof(struct msdos_dir_entry);
1185 get_bh(bhs[n]);
1186 *bh = bhs[n];
1187 *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
1188 *i_pos = fat_make_i_pos(sb, *bh, *de);
1189
1190 /* Second stage: clear the rest of cluster, and write outs */
1191 err = fat_zeroed_cluster(dir, start_blknr, ++n, bhs, MAX_BUF_PER_PAGE);
1192 if (err)
1193 goto error_free;
1194
1195 return cluster[0];
1196
1197error_free:
1198 brelse(*bh);
1199 *bh = NULL;
1200 n = 0;
1201error_nomem:
1202 for (i = 0; i < n; i++)
1203 bforget(bhs[i]);
1204 fat_free_clusters(dir, cluster[0]);
1205error:
1206 return err;
1207}
1208
1209int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
1210 struct fat_slot_info *sinfo)
1211{
1212 struct super_block *sb = dir->i_sb;
1213 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1214 struct buffer_head *bh, *prev, *bhs[3]; /* 32*slots (672bytes) */
1215 struct msdos_dir_entry *de;
1216 int err, free_slots, i, nr_bhs;
1217 loff_t pos, i_pos;
1218
1219 sinfo->nr_slots = nr_slots;
1220
1221 /* First stage: search free direcotry entries */
1222 free_slots = nr_bhs = 0;
1223 bh = prev = NULL;
1224 pos = 0;
1225 err = -ENOSPC;
1226 while (fat_get_entry(dir, &pos, &bh, &de) > -1) {
1227 /* check the maximum size of directory */
1228 if (pos >= FAT_MAX_DIR_SIZE)
1229 goto error;
1230
1231 if (IS_FREE(de->name)) {
1232 if (prev != bh) {
1233 get_bh(bh);
1234 bhs[nr_bhs] = prev = bh;
1235 nr_bhs++;
1236 }
1237 free_slots++;
1238 if (free_slots == nr_slots)
1239 goto found;
1240 } else {
1241 for (i = 0; i < nr_bhs; i++)
1242 brelse(bhs[i]);
1243 prev = NULL;
1244 free_slots = nr_bhs = 0;
1245 }
1246 }
1247 if (dir->i_ino == MSDOS_ROOT_INO) {
1248 if (sbi->fat_bits != 32)
1249 goto error;
1250 } else if (MSDOS_I(dir)->i_start == 0) {
1251 printk(KERN_ERR "FAT: Corrupted directory (i_pos %lld)\n",
1252 MSDOS_I(dir)->i_pos);
1253 err = -EIO;
1254 goto error;
1255 }
1256
1257found:
1258 err = 0;
1259 pos -= free_slots * sizeof(*de);
1260 nr_slots -= free_slots;
1261 if (free_slots) {
1262 /*
1263 * Second stage: filling the free entries with new entries.
1264 * NOTE: If this slots has shortname, first, we write
1265 * the long name slots, then write the short name.
1266 */
1267 int size = free_slots * sizeof(*de);
1268 int offset = pos & (sb->s_blocksize - 1);
1269 int long_bhs = nr_bhs - (nr_slots == 0);
1270
1271 /* Fill the long name slots. */
1272 for (i = 0; i < long_bhs; i++) {
1273 int copy = min_t(int, sb->s_blocksize - offset, size);
1274 memcpy(bhs[i]->b_data + offset, slots, copy);
1275 mark_buffer_dirty(bhs[i]);
1276 offset = 0;
1277 slots += copy;
1278 size -= copy;
1279 }
1280 if (long_bhs && IS_DIRSYNC(dir))
1281 err = fat_sync_bhs(bhs, long_bhs);
1282 if (!err && i < nr_bhs) {
1283 /* Fill the short name slot. */
1284 int copy = min_t(int, sb->s_blocksize - offset, size);
1285 memcpy(bhs[i]->b_data + offset, slots, copy);
1286 mark_buffer_dirty(bhs[i]);
1287 if (IS_DIRSYNC(dir))
1288 err = sync_dirty_buffer(bhs[i]);
1289 }
1290 for (i = 0; i < nr_bhs; i++)
1291 brelse(bhs[i]);
1292 if (err)
1293 goto error_remove;
1294 }
1295
1296 if (nr_slots) {
1297 int cluster, nr_cluster;
1298
1299 /*
1300 * Third stage: allocate the cluster for new entries.
1301 * And initialize the cluster with new entries, then
1302 * add the cluster to dir.
1303 */
1304 cluster = fat_add_new_entries(dir, slots, nr_slots, &nr_cluster,
1305 &de, &bh, &i_pos);
1306 if (cluster < 0) {
1307 err = cluster;
1308 goto error_remove;
1309 }
1310 err = fat_chain_add(dir, cluster, nr_cluster);
1311 if (err) {
1312 fat_free_clusters(dir, cluster);
1313 goto error_remove;
1314 }
1315 if (dir->i_size & (sbi->cluster_size - 1)) {
1316 fat_fs_panic(sb, "Odd directory size");
1317 dir->i_size = (dir->i_size + sbi->cluster_size - 1)
1318 & ~((loff_t)sbi->cluster_size - 1);
1319 }
1320 dir->i_size += nr_cluster << sbi->cluster_bits;
1321 MSDOS_I(dir)->mmu_private += nr_cluster << sbi->cluster_bits;
1322 }
1323 sinfo->slot_off = pos;
1324 sinfo->de = de;
1325 sinfo->bh = bh;
1326 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
1327
1328 return 0;
1329
1330error:
1331 brelse(bh);
1332 for (i = 0; i < nr_bhs; i++)
1333 brelse(bhs[i]);
1334 return err;
1335
1336error_remove:
1337 brelse(bh);
1338 if (free_slots)
1339 __fat_remove_entries(dir, pos, free_slots);
1340 return err;
1341}
1342
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -08001343EXPORT_SYMBOL_GPL(fat_add_entries);