blob: 5efbd5d7701a1d63721f646958d92f36830807f0 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/buffer_head.h>
David Howells188f83d2006-08-31 12:50:04 +020020#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/uaccess.h>
Andy Shevchenko3ed3dec2010-03-16 05:48:08 +090022#include <linux/kernel.h>
OGAWA Hirofumi9e975da2008-11-06 12:53:46 -080023#include "fat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Alan Stern74675a52009-04-30 10:08:18 -040025/*
26 * Maximum buffer size of short name.
27 * [(MSDOS_NAME + '.') * max one char + nul]
28 * For msdos style, ['.' (hidden) + MSDOS_NAME + '.' + nul]
29 */
30#define FAT_MAX_SHORT_SIZE ((MSDOS_NAME + 1) * NLS_MAX_CHARSET_SIZE + 1)
31/*
32 * Maximum buffer size of unicode chars from slots.
33 * [(max longname slots * 13 (size in a slot) + nul) * sizeof(wchar_t)]
34 */
35#define FAT_MAX_UNI_CHARS ((MSDOS_SLOTS - 1) * 13 + 1)
36#define FAT_MAX_UNI_SIZE (FAT_MAX_UNI_CHARS * sizeof(wchar_t))
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static inline loff_t fat_make_i_pos(struct super_block *sb,
39 struct buffer_head *bh,
40 struct msdos_dir_entry *de)
41{
42 return ((loff_t)bh->b_blocknr << MSDOS_SB(sb)->dir_per_block_bits)
43 | (de - (struct msdos_dir_entry *)bh->b_data);
44}
45
Karsten Wiesef3ef6f62005-09-06 15:17:12 -070046static inline void fat_dir_readahead(struct inode *dir, sector_t iblock,
47 sector_t phys)
48{
49 struct super_block *sb = dir->i_sb;
50 struct msdos_sb_info *sbi = MSDOS_SB(sb);
51 struct buffer_head *bh;
52 int sec;
53
54 /* This is not a first sector of cluster, or sec_per_clus == 1 */
55 if ((iblock & (sbi->sec_per_clus - 1)) || sbi->sec_per_clus == 1)
56 return;
57 /* root dir of FAT12/FAT16 */
58 if ((sbi->fat_bits != 32) && (dir->i_ino == MSDOS_ROOT_INO))
59 return;
60
OGAWA Hirofumi83b7c992006-01-08 01:02:09 -080061 bh = sb_find_get_block(sb, phys);
62 if (bh == NULL || !buffer_uptodate(bh)) {
Karsten Wiesef3ef6f62005-09-06 15:17:12 -070063 for (sec = 0; sec < sbi->sec_per_clus; sec++)
64 sb_breadahead(sb, phys + sec);
65 }
66 brelse(bh);
67}
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/* Returns the inode number of the directory entry at offset pos. If bh is
70 non-NULL, it is brelse'd before. Pos is incremented. The buffer header is
71 returned in bh.
72 AV. Most often we do it item-by-item. Makes sense to optimize.
73 AV. OK, there we go: if both bh and de are non-NULL we assume that we just
74 AV. want the next entry (took one explicit de=NULL in vfat/namei.c).
75 AV. It's done in fat_get_entry() (inlined), here the slow case lives.
76 AV. Additionally, when we return -1 (i.e. reached the end of directory)
77 AV. we make bh NULL.
78 */
79static int fat__get_entry(struct inode *dir, loff_t *pos,
80 struct buffer_head **bh, struct msdos_dir_entry **de)
81{
82 struct super_block *sb = dir->i_sb;
83 sector_t phys, iblock;
OGAWA Hirofumie5174ba2006-01-08 01:02:11 -080084 unsigned long mapped_blocks;
85 int err, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87next:
88 if (*bh)
89 brelse(*bh);
90
91 *bh = NULL;
92 iblock = *pos >> sb->s_blocksize_bits;
OGAWA Hirofumi2bdf67e2008-11-06 12:53:57 -080093 err = fat_bmap(dir, iblock, &phys, &mapped_blocks, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 if (err || !phys)
95 return -1; /* beyond EOF or error */
96
Karsten Wiesef3ef6f62005-09-06 15:17:12 -070097 fat_dir_readahead(dir, iblock, phys);
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 *bh = sb_bread(sb, phys);
100 if (*bh == NULL) {
Alexey Fisher869f58c2011-04-12 21:08:38 +0900101 fat_msg(sb, KERN_ERR, "Directory bread(block %llu) failed",
OGAWA Hirofumic3302932008-11-06 12:53:58 -0800102 (llu)phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 /* skip this block */
104 *pos = (iblock + 1) << sb->s_blocksize_bits;
105 goto next;
106 }
107
108 offset = *pos & (sb->s_blocksize - 1);
109 *pos += sizeof(struct msdos_dir_entry);
110 *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
111
112 return 0;
113}
114
115static inline int fat_get_entry(struct inode *dir, loff_t *pos,
116 struct buffer_head **bh,
117 struct msdos_dir_entry **de)
118{
119 /* Fast stuff first */
120 if (*bh && *de &&
121 (*de - (struct msdos_dir_entry *)(*bh)->b_data) < MSDOS_SB(dir->i_sb)->dir_per_block - 1) {
122 *pos += sizeof(struct msdos_dir_entry);
123 (*de)++;
124 return 0;
125 }
126 return fat__get_entry(dir, pos, bh, de);
127}
128
129/*
Alexey Dobriyan4de151d2006-03-22 00:13:35 +0100130 * Convert Unicode 16 to UTF-8, translated Unicode, or ASCII.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * If uni_xlate is enabled and we can't get a 1:1 conversion, use a
132 * colon as an escape character since it is normally invalid on the vfat
133 * filesystem. The following four characters are the hexadecimal digits
134 * of Unicode value. This lets us do a full dump and restore of Unicode
135 * filenames. We could get into some trouble with long Unicode names,
136 * but ignore that right now.
137 * Ahem... Stack smashing in ring 0 isn't fun. Fixed.
138 */
Alexey Fisher869f58c2011-04-12 21:08:38 +0900139static int uni16_to_x8(struct super_block *sb, unsigned char *ascii,
140 const wchar_t *uni, int len, struct nls_table *nls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Alexey Fisher869f58c2011-04-12 21:08:38 +0900142 int uni_xlate = MSDOS_SB(sb)->options.unicode_xlate;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700143 const wchar_t *ip;
144 wchar_t ec;
Andy Shevchenko3ed3dec2010-03-16 05:48:08 +0900145 unsigned char *op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 int charlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 ip = uni;
149 op = ascii;
150
Keith Mokf22032b2008-04-28 02:16:29 -0700151 while (*ip && ((len - NLS_MAX_CHARSET_SIZE) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 ec = *ip++;
Andy Shevchenko3ed3dec2010-03-16 05:48:08 +0900153 if ((charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE)) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 op += charlen;
Keith Mokf22032b2008-04-28 02:16:29 -0700155 len -= charlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 } else {
157 if (uni_xlate == 1) {
Andy Shevchenko3ed3dec2010-03-16 05:48:08 +0900158 *op++ = ':';
159 op = pack_hex_byte(op, ec >> 8);
160 op = pack_hex_byte(op, ec);
Keith Mokf22032b2008-04-28 02:16:29 -0700161 len -= 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 } else {
163 *op++ = '?';
Keith Mokf22032b2008-04-28 02:16:29 -0700164 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
Keith Mokf22032b2008-04-28 02:16:29 -0700168
169 if (unlikely(*ip)) {
Alexey Fisher869f58c2011-04-12 21:08:38 +0900170 fat_msg(sb, KERN_WARNING, "filename was truncated while "
171 "converting.");
Keith Mokf22032b2008-04-28 02:16:29 -0700172 }
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 *op = 0;
175 return (op - ascii);
176}
177
Alexey Fisher869f58c2011-04-12 21:08:38 +0900178static inline int fat_uni_to_x8(struct super_block *sb, const wchar_t *uni,
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700179 unsigned char *buf, int size)
180{
Alexey Fisher869f58c2011-04-12 21:08:38 +0900181 struct msdos_sb_info *sbi = MSDOS_SB(sb);
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700182 if (sbi->options.utf8)
Alan Stern74675a52009-04-30 10:08:18 -0400183 return utf16s_to_utf8s(uni, FAT_MAX_UNI_CHARS,
184 UTF16_HOST_ENDIAN, buf, size);
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700185 else
Alexey Fisher869f58c2011-04-12 21:08:38 +0900186 return uni16_to_x8(sb, buf, uni, size, sbi->nls_io);
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700187}
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189static inline int
190fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
191{
192 int charlen;
193
194 charlen = t->char2uni(c, clen, uni);
195 if (charlen < 0) {
196 *uni = 0x003f; /* a question mark */
197 charlen = 1;
198 }
199 return charlen;
200}
201
202static inline int
203fat_short2lower_uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
204{
205 int charlen;
206 wchar_t wc;
207
208 charlen = t->char2uni(c, clen, &wc);
209 if (charlen < 0) {
210 *uni = 0x003f; /* a question mark */
211 charlen = 1;
212 } else if (charlen <= 1) {
213 unsigned char nc = t->charset2lower[*c];
214
215 if (!nc)
216 nc = *c;
217
218 if ( (charlen = t->char2uni(&nc, 1, uni)) < 0) {
219 *uni = 0x003f; /* a question mark */
220 charlen = 1;
221 }
222 } else
223 *uni = wc;
224
225 return charlen;
226}
227
228static inline int
229fat_shortname2uni(struct nls_table *nls, unsigned char *buf, int buf_size,
230 wchar_t *uni_buf, unsigned short opt, int lower)
231{
232 int len = 0;
233
234 if (opt & VFAT_SFN_DISPLAY_LOWER)
235 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
236 else if (opt & VFAT_SFN_DISPLAY_WIN95)
237 len = fat_short2uni(nls, buf, buf_size, uni_buf);
238 else if (opt & VFAT_SFN_DISPLAY_WINNT) {
239 if (lower)
240 len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
241 else
242 len = fat_short2uni(nls, buf, buf_size, uni_buf);
243 } else
244 len = fat_short2uni(nls, buf, buf_size, uni_buf);
245
246 return len;
247}
248
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700249static inline int fat_name_match(struct msdos_sb_info *sbi,
250 const unsigned char *a, int a_len,
251 const unsigned char *b, int b_len)
252{
253 if (a_len != b_len)
254 return 0;
255
256 if (sbi->options.name_check != 's')
257 return !nls_strnicmp(sbi->nls_io, a, b, a_len);
258 else
259 return !memcmp(a, b, a_len);
260}
261
Pekka Enbergad2c1602005-10-30 15:03:50 -0800262enum { PARSE_INVALID = 1, PARSE_NOT_LONGNAME, PARSE_EOF, };
263
264/**
265 * fat_parse_long - Parse extended directory entry.
266 *
267 * This function returns zero on success, negative value on error, or one of
268 * the following:
269 *
270 * %PARSE_INVALID - Directory entry is invalid.
271 * %PARSE_NOT_LONGNAME - Directory entry does not contain longname.
272 * %PARSE_EOF - Directory has no more entries.
273 */
274static int fat_parse_long(struct inode *dir, loff_t *pos,
275 struct buffer_head **bh, struct msdos_dir_entry **de,
276 wchar_t **unicode, unsigned char *nr_slots)
277{
278 struct msdos_dir_slot *ds;
279 unsigned char id, slot, slots, alias_checksum;
280
281 if (!*unicode) {
OGAWA Hirofumic7a6c4e2008-04-28 02:16:29 -0700282 *unicode = __getname();
Pekka Enbergad2c1602005-10-30 15:03:50 -0800283 if (!*unicode) {
284 brelse(*bh);
285 return -ENOMEM;
286 }
287 }
288parse_long:
289 slots = 0;
290 ds = (struct msdos_dir_slot *)*de;
291 id = ds->id;
292 if (!(id & 0x40))
293 return PARSE_INVALID;
294 slots = id & ~0x40;
295 if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */
296 return PARSE_INVALID;
297 *nr_slots = slots;
298 alias_checksum = ds->alias_checksum;
299
300 slot = slots;
301 while (1) {
302 int offset;
303
304 slot--;
305 offset = slot * 13;
306 fat16_towchar(*unicode + offset, ds->name0_4, 5);
307 fat16_towchar(*unicode + offset + 5, ds->name5_10, 6);
308 fat16_towchar(*unicode + offset + 11, ds->name11_12, 2);
309
310 if (ds->id & 0x40)
311 (*unicode)[offset + 13] = 0;
312 if (fat_get_entry(dir, pos, bh, de) < 0)
313 return PARSE_EOF;
314 if (slot == 0)
315 break;
316 ds = (struct msdos_dir_slot *)*de;
317 if (ds->attr != ATTR_EXT)
318 return PARSE_NOT_LONGNAME;
319 if ((ds->id & ~0x40) != slot)
320 goto parse_long;
321 if (ds->alias_checksum != alias_checksum)
322 goto parse_long;
323 }
324 if ((*de)->name[0] == DELETED_FLAG)
325 return PARSE_INVALID;
326 if ((*de)->attr == ATTR_EXT)
327 goto parse_long;
328 if (IS_FREE((*de)->name) || ((*de)->attr & ATTR_VOLUME))
329 return PARSE_INVALID;
330 if (fat_checksum((*de)->name) != alias_checksum)
331 *nr_slots = 0;
332
333 return 0;
334}
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336/*
337 * Return values: negative -> error, 0 -> not found, positive -> found,
338 * value is the total amount of slots, including the shortname entry.
339 */
340int fat_search_long(struct inode *inode, const unsigned char *name,
341 int name_len, struct fat_slot_info *sinfo)
342{
343 struct super_block *sb = inode->i_sb;
344 struct msdos_sb_info *sbi = MSDOS_SB(sb);
345 struct buffer_head *bh = NULL;
346 struct msdos_dir_entry *de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 struct nls_table *nls_disk = sbi->nls_disk;
Keith Mokf22032b2008-04-28 02:16:29 -0700348 unsigned char nr_slots;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700349 wchar_t bufuname[14];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 wchar_t *unicode = NULL;
Keith Mokf22032b2008-04-28 02:16:29 -0700351 unsigned char work[MSDOS_NAME];
OGAWA Hirofumi98a15162008-07-25 01:46:44 -0700352 unsigned char bufname[FAT_MAX_SHORT_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 unsigned short opt_shortname = sbi->options.shortname;
354 loff_t cpos = 0;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700355 int chl, i, j, last_u, err, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 err = -ENOENT;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700358 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700360 goto end_of_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361parse_record:
362 nr_slots = 0;
363 if (de->name[0] == DELETED_FLAG)
364 continue;
365 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
366 continue;
367 if (de->attr != ATTR_EXT && IS_FREE(de->name))
368 continue;
369 if (de->attr == ATTR_EXT) {
Pekka Enbergad2c1602005-10-30 15:03:50 -0800370 int status = fat_parse_long(inode, &cpos, &bh, &de,
371 &unicode, &nr_slots);
Darren Jenkins52e9d9f2008-11-06 12:53:48 -0800372 if (status < 0) {
373 err = status;
374 goto end_of_dir;
375 } else if (status == PARSE_INVALID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 continue;
Pekka Enbergad2c1602005-10-30 15:03:50 -0800377 else if (status == PARSE_NOT_LONGNAME)
378 goto parse_record;
379 else if (status == PARSE_EOF)
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700380 goto end_of_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382
383 memcpy(work, de->name, sizeof(de->name));
384 /* see namei.c, msdos_format_name */
385 if (work[0] == 0x05)
386 work[0] = 0xE5;
387 for (i = 0, j = 0, last_u = 0; i < 8;) {
OGAWA Hirofumi9aacd592007-07-15 23:39:56 -0700388 if (!work[i])
389 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
391 &bufuname[j++], opt_shortname,
392 de->lcase & CASE_LOWER_BASE);
393 if (chl <= 1) {
394 if (work[i] != ' ')
395 last_u = j;
396 } else {
397 last_u = j;
398 }
399 i += chl;
400 }
401 j = last_u;
402 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
OGAWA Hirofumi9aacd592007-07-15 23:39:56 -0700403 for (i = 8; i < MSDOS_NAME;) {
404 if (!work[i])
405 break;
406 chl = fat_shortname2uni(nls_disk, &work[i],
407 MSDOS_NAME - i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 &bufuname[j++], opt_shortname,
409 de->lcase & CASE_LOWER_EXT);
410 if (chl <= 1) {
OGAWA Hirofumi9aacd592007-07-15 23:39:56 -0700411 if (work[i] != ' ')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 last_u = j;
413 } else {
414 last_u = j;
415 }
416 i += chl;
417 }
418 if (!last_u)
419 continue;
420
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700421 /* Compare shortname */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 bufuname[last_u] = 0x0000;
Alexey Fisher869f58c2011-04-12 21:08:38 +0900423 len = fat_uni_to_x8(sb, bufuname, bufname, sizeof(bufname));
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700424 if (fat_name_match(sbi, name, name_len, bufname, len))
425 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 if (nr_slots) {
OGAWA Hirofumi98a15162008-07-25 01:46:44 -0700428 void *longname = unicode + FAT_MAX_UNI_CHARS;
429 int size = PATH_MAX - FAT_MAX_UNI_SIZE;
430
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700431 /* Compare longname */
Alexey Fisher869f58c2011-04-12 21:08:38 +0900432 len = fat_uni_to_x8(sb, unicode, longname, size);
OGAWA Hirofumi98a15162008-07-25 01:46:44 -0700433 if (fat_name_match(sbi, name, name_len, longname, len))
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700434 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436 }
437
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700438found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 nr_slots++; /* include the de */
440 sinfo->slot_off = cpos - nr_slots * sizeof(*de);
441 sinfo->nr_slots = nr_slots;
442 sinfo->de = de;
443 sinfo->bh = bh;
444 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
445 err = 0;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700446end_of_dir:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (unicode)
OGAWA Hirofumic7a6c4e2008-04-28 02:16:29 -0700448 __putname(unicode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 return err;
451}
452
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -0800453EXPORT_SYMBOL_GPL(fat_search_long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455struct fat_ioctl_filldir_callback {
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700456 void __user *dirent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 int result;
458 /* for dir ioctl */
459 const char *longname;
460 int long_len;
461 const char *shortname;
462 int short_len;
463};
464
Pekka Enbergad2c1602005-10-30 15:03:50 -0800465static int __fat_readdir(struct inode *inode, struct file *filp, void *dirent,
466 filldir_t filldir, int short_only, int both)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 struct super_block *sb = inode->i_sb;
469 struct msdos_sb_info *sbi = MSDOS_SB(sb);
470 struct buffer_head *bh;
471 struct msdos_dir_entry *de;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 struct nls_table *nls_disk = sbi->nls_disk;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700473 unsigned char nr_slots;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 wchar_t bufuname[14];
475 wchar_t *unicode = NULL;
OGAWA Hirofumi98a15162008-07-25 01:46:44 -0700476 unsigned char c, work[MSDOS_NAME];
477 unsigned char bufname[FAT_MAX_SHORT_SIZE], *ptname = bufname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 unsigned short opt_shortname = sbi->options.shortname;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700479 int isvfat = sbi->options.isvfat;
480 int nocase = sbi->options.nocase;
OGAWA Hirofumidcd8c532008-07-25 01:46:44 -0700481 const char *fill_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 unsigned long inum;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700483 unsigned long lpos, dummy, *furrfu = &lpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 loff_t cpos;
OGAWA Hirofumidcd8c532008-07-25 01:46:44 -0700485 int chi, chl, i, i2, j, last, last_u, dotoffset = 0, fill_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 int ret = 0;
487
Linus Torvalds8f593422008-05-19 19:53:01 -0700488 lock_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 cpos = filp->f_pos;
491 /* Fake . and .. for the root directory. */
492 if (inode->i_ino == MSDOS_ROOT_INO) {
493 while (cpos < 2) {
494 if (filldir(dirent, "..", cpos+1, cpos, MSDOS_ROOT_INO, DT_DIR) < 0)
495 goto out;
496 cpos++;
497 filp->f_pos++;
498 }
499 if (cpos == 2) {
500 dummy = 2;
501 furrfu = &dummy;
502 cpos = 0;
503 }
504 }
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700505 if (cpos & (sizeof(struct msdos_dir_entry) - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 ret = -ENOENT;
507 goto out;
508 }
509
510 bh = NULL;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700511get_new:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700513 goto end_of_dir;
Pekka Enbergad2c1602005-10-30 15:03:50 -0800514parse_record:
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700515 nr_slots = 0;
OGAWA Hirofumidcd8c532008-07-25 01:46:44 -0700516 /*
517 * Check for long filename entry, but if short_only, we don't
518 * need to parse long filename.
519 */
520 if (isvfat && !short_only) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (de->name[0] == DELETED_FLAG)
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700522 goto record_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700524 goto record_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (de->attr != ATTR_EXT && IS_FREE(de->name))
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700526 goto record_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 } else {
528 if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700529 goto record_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531
532 if (isvfat && de->attr == ATTR_EXT) {
Pekka Enbergad2c1602005-10-30 15:03:50 -0800533 int status = fat_parse_long(inode, &cpos, &bh, &de,
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700534 &unicode, &nr_slots);
Pekka Enbergad2c1602005-10-30 15:03:50 -0800535 if (status < 0) {
536 filp->f_pos = cpos;
537 ret = status;
538 goto out;
539 } else if (status == PARSE_INVALID)
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700540 goto record_end;
Pekka Enbergad2c1602005-10-30 15:03:50 -0800541 else if (status == PARSE_NOT_LONGNAME)
542 goto parse_record;
543 else if (status == PARSE_EOF)
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700544 goto end_of_dir;
OGAWA Hirofumidcd8c532008-07-25 01:46:44 -0700545
546 if (nr_slots) {
547 void *longname = unicode + FAT_MAX_UNI_CHARS;
548 int size = PATH_MAX - FAT_MAX_UNI_SIZE;
Alexey Fisher869f58c2011-04-12 21:08:38 +0900549 int len = fat_uni_to_x8(sb, unicode, longname, size);
OGAWA Hirofumidcd8c532008-07-25 01:46:44 -0700550
551 fill_name = longname;
552 fill_len = len;
553 /* !both && !short_only, so we don't need shortname. */
554 if (!both)
555 goto start_filldir;
556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558
559 if (sbi->options.dotsOK) {
560 ptname = bufname;
561 dotoffset = 0;
562 if (de->attr & ATTR_HIDDEN) {
563 *ptname++ = '.';
564 dotoffset = 1;
565 }
566 }
567
568 memcpy(work, de->name, sizeof(de->name));
569 /* see namei.c, msdos_format_name */
570 if (work[0] == 0x05)
571 work[0] = 0xE5;
572 for (i = 0, j = 0, last = 0, last_u = 0; i < 8;) {
OGAWA Hirofumi9aacd592007-07-15 23:39:56 -0700573 if (!(c = work[i]))
574 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
576 &bufuname[j++], opt_shortname,
577 de->lcase & CASE_LOWER_BASE);
578 if (chl <= 1) {
579 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
580 if (c != ' ') {
581 last = i;
582 last_u = j;
583 }
584 } else {
585 last_u = j;
586 for (chi = 0; chi < chl && i < 8; chi++) {
587 ptname[i] = work[i];
588 i++; last = i;
589 }
590 }
591 }
592 i = last;
593 j = last_u;
594 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
595 ptname[i++] = '.';
OGAWA Hirofumi9aacd592007-07-15 23:39:56 -0700596 for (i2 = 8; i2 < MSDOS_NAME;) {
597 if (!(c = work[i2]))
598 break;
599 chl = fat_shortname2uni(nls_disk, &work[i2], MSDOS_NAME - i2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 &bufuname[j++], opt_shortname,
601 de->lcase & CASE_LOWER_EXT);
602 if (chl <= 1) {
603 i2++;
604 ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
605 if (c != ' ') {
606 last = i;
607 last_u = j;
608 }
609 } else {
610 last_u = j;
OGAWA Hirofumi9aacd592007-07-15 23:39:56 -0700611 for (chi = 0; chi < chl && i2 < MSDOS_NAME; chi++) {
612 ptname[i++] = work[i2++];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 last = i;
614 }
615 }
616 }
617 if (!last)
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700618 goto record_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 i = last + dotoffset;
621 j = last_u;
622
OGAWA Hirofumidcd8c532008-07-25 01:46:44 -0700623 if (isvfat) {
624 bufuname[j] = 0x0000;
Alexey Fisher869f58c2011-04-12 21:08:38 +0900625 i = fat_uni_to_x8(sb, bufuname, bufname, sizeof(bufname));
OGAWA Hirofumidcd8c532008-07-25 01:46:44 -0700626 }
627 if (nr_slots) {
628 /* hack for fat_ioctl_filldir() */
629 struct fat_ioctl_filldir_callback *p = dirent;
630
631 p->longname = fill_name;
632 p->long_len = fill_len;
633 p->shortname = bufname;
634 p->short_len = i;
635 fill_name = NULL;
636 fill_len = 0;
637 } else {
638 fill_name = bufname;
639 fill_len = i;
640 }
641
642start_filldir:
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700643 lpos = cpos - (nr_slots + 1) * sizeof(struct msdos_dir_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (!memcmp(de->name, MSDOS_DOT, MSDOS_NAME))
645 inum = inode->i_ino;
646 else if (!memcmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
Josef "Jeff" Sipekdba32302006-12-08 02:36:39 -0800647 inum = parent_ino(filp->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 } else {
649 loff_t i_pos = fat_make_i_pos(sb, bh, de);
650 struct inode *tmp = fat_iget(sb, i_pos);
651 if (tmp) {
652 inum = tmp->i_ino;
653 iput(tmp);
654 } else
655 inum = iunique(sb, MSDOS_ROOT_INO);
656 }
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (filldir(dirent, fill_name, fill_len, *furrfu, inum,
659 (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700660 goto fill_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700662record_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 furrfu = &lpos;
664 filp->f_pos = cpos;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700665 goto get_new;
666end_of_dir:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 filp->f_pos = cpos;
OGAWA Hirofumid6886112008-07-25 01:46:43 -0700668fill_failed:
Karsten Wiesef3ef6f62005-09-06 15:17:12 -0700669 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (unicode)
OGAWA Hirofumic7a6c4e2008-04-28 02:16:29 -0700671 __putname(unicode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672out:
Linus Torvalds8f593422008-05-19 19:53:01 -0700673 unlock_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return ret;
675}
676
677static int fat_readdir(struct file *filp, void *dirent, filldir_t filldir)
678{
Josef "Jeff" Sipekdba32302006-12-08 02:36:39 -0800679 struct inode *inode = filp->f_path.dentry->d_inode;
Pekka Enbergad2c1602005-10-30 15:03:50 -0800680 return __fat_readdir(inode, filp, dirent, filldir, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700683#define FAT_IOCTL_FILLDIR_FUNC(func, dirent_type) \
684static int func(void *__buf, const char *name, int name_len, \
685 loff_t offset, u64 ino, unsigned int d_type) \
686{ \
687 struct fat_ioctl_filldir_callback *buf = __buf; \
688 struct dirent_type __user *d1 = buf->dirent; \
689 struct dirent_type __user *d2 = d1 + 1; \
690 \
691 if (buf->result) \
692 return -EINVAL; \
693 buf->result++; \
694 \
695 if (name != NULL) { \
696 /* dirent has only short name */ \
697 if (name_len >= sizeof(d1->d_name)) \
698 name_len = sizeof(d1->d_name) - 1; \
699 \
700 if (put_user(0, d2->d_name) || \
701 put_user(0, &d2->d_reclen) || \
702 copy_to_user(d1->d_name, name, name_len) || \
703 put_user(0, d1->d_name + name_len) || \
704 put_user(name_len, &d1->d_reclen)) \
705 goto efault; \
706 } else { \
707 /* dirent has short and long name */ \
708 const char *longname = buf->longname; \
709 int long_len = buf->long_len; \
710 const char *shortname = buf->shortname; \
711 int short_len = buf->short_len; \
712 \
713 if (long_len >= sizeof(d1->d_name)) \
714 long_len = sizeof(d1->d_name) - 1; \
715 if (short_len >= sizeof(d1->d_name)) \
716 short_len = sizeof(d1->d_name) - 1; \
717 \
718 if (copy_to_user(d2->d_name, longname, long_len) || \
719 put_user(0, d2->d_name + long_len) || \
720 put_user(long_len, &d2->d_reclen) || \
721 put_user(ino, &d2->d_ino) || \
722 put_user(offset, &d2->d_off) || \
723 copy_to_user(d1->d_name, shortname, short_len) || \
724 put_user(0, d1->d_name + short_len) || \
725 put_user(short_len, &d1->d_reclen)) \
726 goto efault; \
727 } \
728 return 0; \
729efault: \
730 buf->result = -EFAULT; \
731 return -EFAULT; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
Adrian Bunk531f7102008-07-25 01:46:43 -0700734FAT_IOCTL_FILLDIR_FUNC(fat_ioctl_filldir, __fat_dirent)
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700735
736static int fat_ioctl_readdir(struct inode *inode, struct file *filp,
737 void __user *dirent, filldir_t filldir,
738 int short_only, int both)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
740 struct fat_ioctl_filldir_callback buf;
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700741 int ret;
742
743 buf.dirent = dirent;
744 buf.result = 0;
745 mutex_lock(&inode->i_mutex);
746 ret = -ENOENT;
747 if (!IS_DEADDIR(inode)) {
748 ret = __fat_readdir(inode, filp, &buf, filldir,
749 short_only, both);
750 }
751 mutex_unlock(&inode->i_mutex);
752 if (ret >= 0)
753 ret = buf.result;
754 return ret;
755}
756
Arnd Bergmann7845bc3e2010-05-17 08:13:47 +0900757static long fat_dir_ioctl(struct file *filp, unsigned int cmd,
758 unsigned long arg)
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700759{
Arnd Bergmann7845bc3e2010-05-17 08:13:47 +0900760 struct inode *inode = filp->f_path.dentry->d_inode;
Adrian Bunk531f7102008-07-25 01:46:43 -0700761 struct __fat_dirent __user *d1 = (struct __fat_dirent __user *)arg;
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700762 int short_only, both;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 switch (cmd) {
765 case VFAT_IOCTL_READDIR_SHORT:
766 short_only = 1;
767 both = 0;
768 break;
769 case VFAT_IOCTL_READDIR_BOTH:
770 short_only = 0;
771 both = 1;
772 break;
773 default:
Arnd Bergmann7845bc3e2010-05-17 08:13:47 +0900774 return fat_generic_ioctl(filp, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776
Adrian Bunk531f7102008-07-25 01:46:43 -0700777 if (!access_ok(VERIFY_WRITE, d1, sizeof(struct __fat_dirent[2])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return -EFAULT;
779 /*
780 * Yes, we don't need this put_user() absolutely. However old
781 * code didn't return the right value. So, app use this value,
782 * in order to check whether it is EOF.
783 */
784 if (put_user(0, &d1->d_reclen))
785 return -EFAULT;
786
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700787 return fat_ioctl_readdir(inode, filp, d1, fat_ioctl_filldir,
788 short_only, both);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
David Howells188f83d2006-08-31 12:50:04 +0200791#ifdef CONFIG_COMPAT
792#define VFAT_IOCTL_READDIR_BOTH32 _IOR('r', 1, struct compat_dirent[2])
793#define VFAT_IOCTL_READDIR_SHORT32 _IOR('r', 2, struct compat_dirent[2])
794
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700795FAT_IOCTL_FILLDIR_FUNC(fat_compat_ioctl_filldir, compat_dirent)
David Howells188f83d2006-08-31 12:50:04 +0200796
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700797static long fat_compat_dir_ioctl(struct file *filp, unsigned cmd,
David Howells188f83d2006-08-31 12:50:04 +0200798 unsigned long arg)
799{
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700800 struct inode *inode = filp->f_path.dentry->d_inode;
801 struct compat_dirent __user *d1 = compat_ptr(arg);
802 int short_only, both;
David Howells188f83d2006-08-31 12:50:04 +0200803
804 switch (cmd) {
David Howells188f83d2006-08-31 12:50:04 +0200805 case VFAT_IOCTL_READDIR_SHORT32:
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700806 short_only = 1;
807 both = 0;
808 break;
809 case VFAT_IOCTL_READDIR_BOTH32:
810 short_only = 0;
811 both = 1;
David Howells188f83d2006-08-31 12:50:04 +0200812 break;
813 default:
Arnd Bergmann7845bc3e2010-05-17 08:13:47 +0900814 return fat_generic_ioctl(filp, cmd, (unsigned long)arg);
David Howells188f83d2006-08-31 12:50:04 +0200815 }
816
OGAWA Hirofumic483bab2007-05-08 00:31:28 -0700817 if (!access_ok(VERIFY_WRITE, d1, sizeof(struct compat_dirent[2])))
818 return -EFAULT;
819 /*
820 * Yes, we don't need this put_user() absolutely. However old
821 * code didn't return the right value. So, app use this value,
822 * in order to check whether it is EOF.
823 */
824 if (put_user(0, &d1->d_reclen))
825 return -EFAULT;
826
827 return fat_ioctl_readdir(inode, filp, d1, fat_compat_ioctl_filldir,
828 short_only, both);
David Howells188f83d2006-08-31 12:50:04 +0200829}
830#endif /* CONFIG_COMPAT */
831
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800832const struct file_operations fat_dir_operations = {
OGAWA Hirofumi53472bc2008-11-06 12:53:47 -0800833 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 .read = generic_read_dir,
835 .readdir = fat_readdir,
Arnd Bergmann7845bc3e2010-05-17 08:13:47 +0900836 .unlocked_ioctl = fat_dir_ioctl,
David Howells188f83d2006-08-31 12:50:04 +0200837#ifdef CONFIG_COMPAT
838 .compat_ioctl = fat_compat_dir_ioctl,
839#endif
Al Virob5224122009-06-07 13:44:36 -0400840 .fsync = fat_file_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841};
842
843static int fat_get_short_entry(struct inode *dir, loff_t *pos,
844 struct buffer_head **bh,
845 struct msdos_dir_entry **de)
846{
847 while (fat_get_entry(dir, pos, bh, de) >= 0) {
848 /* free entry or long name entry or volume label */
849 if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME))
850 return 0;
851 }
852 return -ENOENT;
853}
854
855/*
856 * The ".." entry can not provide the "struct fat_slot_info" informations
857 * for inode. So, this function provide the some informations only.
858 */
859int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh,
860 struct msdos_dir_entry **de, loff_t *i_pos)
861{
862 loff_t offset;
863
864 offset = 0;
865 *bh = NULL;
866 while (fat_get_short_entry(dir, &offset, bh, de) >= 0) {
867 if (!strncmp((*de)->name, MSDOS_DOTDOT, MSDOS_NAME)) {
868 *i_pos = fat_make_i_pos(dir->i_sb, *bh, *de);
869 return 0;
870 }
871 }
872 return -ENOENT;
873}
874
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -0800875EXPORT_SYMBOL_GPL(fat_get_dotdot_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877/* See if directory is empty */
878int fat_dir_empty(struct inode *dir)
879{
880 struct buffer_head *bh;
881 struct msdos_dir_entry *de;
882 loff_t cpos;
883 int result = 0;
884
885 bh = NULL;
886 cpos = 0;
887 while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
888 if (strncmp(de->name, MSDOS_DOT , MSDOS_NAME) &&
889 strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
890 result = -ENOTEMPTY;
891 break;
892 }
893 }
894 brelse(bh);
895 return result;
896}
897
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -0800898EXPORT_SYMBOL_GPL(fat_dir_empty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900/*
901 * fat_subdirs counts the number of sub-directories of dir. It can be run
902 * on directories being created.
903 */
904int fat_subdirs(struct inode *dir)
905{
906 struct buffer_head *bh;
907 struct msdos_dir_entry *de;
908 loff_t cpos;
909 int count = 0;
910
911 bh = NULL;
912 cpos = 0;
913 while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
914 if (de->attr & ATTR_DIR)
915 count++;
916 }
917 brelse(bh);
918 return count;
919}
920
921/*
922 * Scans a directory for a given file (name points to its formatted name).
923 * Returns an error code or zero.
924 */
925int fat_scan(struct inode *dir, const unsigned char *name,
926 struct fat_slot_info *sinfo)
927{
928 struct super_block *sb = dir->i_sb;
929
930 sinfo->slot_off = 0;
931 sinfo->bh = NULL;
932 while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh,
933 &sinfo->de) >= 0) {
934 if (!strncmp(sinfo->de->name, name, MSDOS_NAME)) {
935 sinfo->slot_off -= sizeof(*sinfo->de);
936 sinfo->nr_slots = 1;
937 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
938 return 0;
939 }
940 }
941 return -ENOENT;
942}
943
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -0800944EXPORT_SYMBOL_GPL(fat_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946static int __fat_remove_entries(struct inode *dir, loff_t pos, int nr_slots)
947{
948 struct super_block *sb = dir->i_sb;
949 struct buffer_head *bh;
950 struct msdos_dir_entry *de, *endp;
951 int err = 0, orig_slots;
952
953 while (nr_slots) {
954 bh = NULL;
955 if (fat_get_entry(dir, &pos, &bh, &de) < 0) {
956 err = -EIO;
957 break;
958 }
959
960 orig_slots = nr_slots;
961 endp = (struct msdos_dir_entry *)(bh->b_data + sb->s_blocksize);
962 while (nr_slots && de < endp) {
963 de->name[0] = DELETED_FLAG;
964 de++;
965 nr_slots--;
966 }
Al Virob5224122009-06-07 13:44:36 -0400967 mark_buffer_dirty_inode(bh, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (IS_DIRSYNC(dir))
969 err = sync_dirty_buffer(bh);
970 brelse(bh);
971 if (err)
972 break;
973
974 /* pos is *next* de's position, so this does `- sizeof(de)' */
975 pos += ((orig_slots - nr_slots) * sizeof(*de)) - sizeof(*de);
976 }
977
978 return err;
979}
980
981int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo)
982{
Alexey Fisher869f58c2011-04-12 21:08:38 +0900983 struct super_block *sb = dir->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 struct msdos_dir_entry *de;
985 struct buffer_head *bh;
986 int err = 0, nr_slots;
987
988 /*
989 * First stage: Remove the shortname. By this, the directory
990 * entry is removed.
991 */
992 nr_slots = sinfo->nr_slots;
993 de = sinfo->de;
994 sinfo->de = NULL;
995 bh = sinfo->bh;
996 sinfo->bh = NULL;
997 while (nr_slots && de >= (struct msdos_dir_entry *)bh->b_data) {
998 de->name[0] = DELETED_FLAG;
999 de--;
1000 nr_slots--;
1001 }
Al Virob5224122009-06-07 13:44:36 -04001002 mark_buffer_dirty_inode(bh, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (IS_DIRSYNC(dir))
1004 err = sync_dirty_buffer(bh);
1005 brelse(bh);
1006 if (err)
1007 return err;
1008 dir->i_version++;
1009
1010 if (nr_slots) {
1011 /*
1012 * Second stage: remove the remaining longname slots.
1013 * (This directory entry is already removed, and so return
1014 * the success)
1015 */
1016 err = __fat_remove_entries(dir, sinfo->slot_off, nr_slots);
1017 if (err) {
Alexey Fisher869f58c2011-04-12 21:08:38 +09001018 fat_msg(sb, KERN_WARNING,
1019 "Couldn't remove the long name slots");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021 }
1022
1023 dir->i_mtime = dir->i_atime = CURRENT_TIME_SEC;
1024 if (IS_DIRSYNC(dir))
1025 (void)fat_sync_inode(dir);
1026 else
1027 mark_inode_dirty(dir);
1028
1029 return 0;
1030}
1031
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -08001032EXPORT_SYMBOL_GPL(fat_remove_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034static int fat_zeroed_cluster(struct inode *dir, sector_t blknr, int nr_used,
1035 struct buffer_head **bhs, int nr_bhs)
1036{
1037 struct super_block *sb = dir->i_sb;
1038 sector_t last_blknr = blknr + MSDOS_SB(sb)->sec_per_clus;
1039 int err, i, n;
1040
1041 /* Zeroing the unused blocks on this cluster */
1042 blknr += nr_used;
1043 n = nr_used;
1044 while (blknr < last_blknr) {
1045 bhs[n] = sb_getblk(sb, blknr);
1046 if (!bhs[n]) {
1047 err = -ENOMEM;
1048 goto error;
1049 }
1050 memset(bhs[n]->b_data, 0, sb->s_blocksize);
1051 set_buffer_uptodate(bhs[n]);
Al Virob5224122009-06-07 13:44:36 -04001052 mark_buffer_dirty_inode(bhs[n], dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 n++;
1055 blknr++;
1056 if (n == nr_bhs) {
1057 if (IS_DIRSYNC(dir)) {
1058 err = fat_sync_bhs(bhs, n);
1059 if (err)
1060 goto error;
1061 }
1062 for (i = 0; i < n; i++)
1063 brelse(bhs[i]);
1064 n = 0;
1065 }
1066 }
1067 if (IS_DIRSYNC(dir)) {
1068 err = fat_sync_bhs(bhs, n);
1069 if (err)
1070 goto error;
1071 }
1072 for (i = 0; i < n; i++)
1073 brelse(bhs[i]);
1074
1075 return 0;
1076
1077error:
1078 for (i = 0; i < n; i++)
1079 bforget(bhs[i]);
1080 return err;
1081}
1082
1083int fat_alloc_new_dir(struct inode *dir, struct timespec *ts)
1084{
1085 struct super_block *sb = dir->i_sb;
1086 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1087 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
1088 struct msdos_dir_entry *de;
1089 sector_t blknr;
1090 __le16 date, time;
OGAWA Hirofumi7decd1c2008-11-06 12:53:47 -08001091 u8 time_cs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 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
OGAWA Hirofumi7decd1c2008-11-06 12:53:47 -08001105 fat_time_unix2fat(sbi, ts, &time, &date, &time_cs);
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (sbi->options.isvfat) {
1116 /* extra timestamps */
1117 de[0].ctime = de[1].ctime = time;
OGAWA Hirofumi7decd1c2008-11-06 12:53:47 -08001118 de[0].ctime_cs = de[1].ctime_cs = time_cs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = date;
1120 } else {
1121 de[0].ctime = de[1].ctime = 0;
OGAWA Hirofumi7decd1c2008-11-06 12:53:47 -08001122 de[0].ctime_cs = de[1].ctime_cs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = 0;
1124 }
1125 de[0].start = cpu_to_le16(cluster);
1126 de[0].starthi = cpu_to_le16(cluster >> 16);
1127 de[1].start = cpu_to_le16(MSDOS_I(dir)->i_logstart);
1128 de[1].starthi = cpu_to_le16(MSDOS_I(dir)->i_logstart >> 16);
1129 de[0].size = de[1].size = 0;
1130 memset(de + 2, 0, sb->s_blocksize - 2 * sizeof(*de));
1131 set_buffer_uptodate(bhs[0]);
Al Virob5224122009-06-07 13:44:36 -04001132 mark_buffer_dirty_inode(bhs[0], dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 err = fat_zeroed_cluster(dir, blknr, 1, bhs, MAX_BUF_PER_PAGE);
1135 if (err)
1136 goto error_free;
1137
1138 return cluster;
1139
1140error_free:
1141 fat_free_clusters(dir, cluster);
1142error:
1143 return err;
1144}
1145
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -08001146EXPORT_SYMBOL_GPL(fat_alloc_new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148static int fat_add_new_entries(struct inode *dir, void *slots, int nr_slots,
1149 int *nr_cluster, struct msdos_dir_entry **de,
1150 struct buffer_head **bh, loff_t *i_pos)
1151{
1152 struct super_block *sb = dir->i_sb;
1153 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1154 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
1155 sector_t blknr, start_blknr, last_blknr;
1156 unsigned long size, copy;
1157 int err, i, n, offset, cluster[2];
1158
1159 /*
1160 * The minimum cluster size is 512bytes, and maximum entry
1161 * size is 32*slots (672bytes). So, iff the cluster size is
1162 * 512bytes, we may need two clusters.
1163 */
1164 size = nr_slots * sizeof(struct msdos_dir_entry);
1165 *nr_cluster = (size + (sbi->cluster_size - 1)) >> sbi->cluster_bits;
1166 BUG_ON(*nr_cluster > 2);
1167
1168 err = fat_alloc_clusters(dir, cluster, *nr_cluster);
1169 if (err)
1170 goto error;
1171
1172 /*
1173 * First stage: Fill the directory entry. NOTE: This cluster
1174 * is not referenced from any inode yet, so updates order is
1175 * not important.
1176 */
1177 i = n = copy = 0;
1178 do {
1179 start_blknr = blknr = fat_clus_to_blknr(sbi, cluster[i]);
1180 last_blknr = start_blknr + sbi->sec_per_clus;
1181 while (blknr < last_blknr) {
1182 bhs[n] = sb_getblk(sb, blknr);
1183 if (!bhs[n]) {
1184 err = -ENOMEM;
1185 goto error_nomem;
1186 }
1187
1188 /* fill the directory entry */
1189 copy = min(size, sb->s_blocksize);
1190 memcpy(bhs[n]->b_data, slots, copy);
1191 slots += copy;
1192 size -= copy;
1193 set_buffer_uptodate(bhs[n]);
Al Virob5224122009-06-07 13:44:36 -04001194 mark_buffer_dirty_inode(bhs[n], dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if (!size)
1196 break;
1197 n++;
1198 blknr++;
1199 }
1200 } while (++i < *nr_cluster);
1201
1202 memset(bhs[n]->b_data + copy, 0, sb->s_blocksize - copy);
1203 offset = copy - sizeof(struct msdos_dir_entry);
1204 get_bh(bhs[n]);
1205 *bh = bhs[n];
1206 *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
1207 *i_pos = fat_make_i_pos(sb, *bh, *de);
1208
1209 /* Second stage: clear the rest of cluster, and write outs */
1210 err = fat_zeroed_cluster(dir, start_blknr, ++n, bhs, MAX_BUF_PER_PAGE);
1211 if (err)
1212 goto error_free;
1213
1214 return cluster[0];
1215
1216error_free:
1217 brelse(*bh);
1218 *bh = NULL;
1219 n = 0;
1220error_nomem:
1221 for (i = 0; i < n; i++)
1222 bforget(bhs[i]);
1223 fat_free_clusters(dir, cluster[0]);
1224error:
1225 return err;
1226}
1227
1228int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
1229 struct fat_slot_info *sinfo)
1230{
1231 struct super_block *sb = dir->i_sb;
1232 struct msdos_sb_info *sbi = MSDOS_SB(sb);
1233 struct buffer_head *bh, *prev, *bhs[3]; /* 32*slots (672bytes) */
Jonas Aberg8c320c02011-08-17 19:10:06 +09001234 struct msdos_dir_entry *uninitialized_var(de);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 int err, free_slots, i, nr_bhs;
1236 loff_t pos, i_pos;
1237
1238 sinfo->nr_slots = nr_slots;
1239
1240 /* First stage: search free direcotry entries */
1241 free_slots = nr_bhs = 0;
1242 bh = prev = NULL;
1243 pos = 0;
1244 err = -ENOSPC;
1245 while (fat_get_entry(dir, &pos, &bh, &de) > -1) {
1246 /* check the maximum size of directory */
1247 if (pos >= FAT_MAX_DIR_SIZE)
1248 goto error;
1249
1250 if (IS_FREE(de->name)) {
1251 if (prev != bh) {
1252 get_bh(bh);
1253 bhs[nr_bhs] = prev = bh;
1254 nr_bhs++;
1255 }
1256 free_slots++;
1257 if (free_slots == nr_slots)
1258 goto found;
1259 } else {
1260 for (i = 0; i < nr_bhs; i++)
1261 brelse(bhs[i]);
1262 prev = NULL;
1263 free_slots = nr_bhs = 0;
1264 }
1265 }
1266 if (dir->i_ino == MSDOS_ROOT_INO) {
1267 if (sbi->fat_bits != 32)
1268 goto error;
1269 } else if (MSDOS_I(dir)->i_start == 0) {
Alexey Fisher869f58c2011-04-12 21:08:38 +09001270 fat_msg(sb, KERN_ERR, "Corrupted directory (i_pos %lld)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 MSDOS_I(dir)->i_pos);
1272 err = -EIO;
1273 goto error;
1274 }
1275
1276found:
1277 err = 0;
1278 pos -= free_slots * sizeof(*de);
1279 nr_slots -= free_slots;
1280 if (free_slots) {
1281 /*
1282 * Second stage: filling the free entries with new entries.
1283 * NOTE: If this slots has shortname, first, we write
1284 * the long name slots, then write the short name.
1285 */
1286 int size = free_slots * sizeof(*de);
1287 int offset = pos & (sb->s_blocksize - 1);
1288 int long_bhs = nr_bhs - (nr_slots == 0);
1289
1290 /* Fill the long name slots. */
1291 for (i = 0; i < long_bhs; i++) {
1292 int copy = min_t(int, sb->s_blocksize - offset, size);
1293 memcpy(bhs[i]->b_data + offset, slots, copy);
Al Virob5224122009-06-07 13:44:36 -04001294 mark_buffer_dirty_inode(bhs[i], dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 offset = 0;
1296 slots += copy;
1297 size -= copy;
1298 }
1299 if (long_bhs && IS_DIRSYNC(dir))
1300 err = fat_sync_bhs(bhs, long_bhs);
1301 if (!err && i < nr_bhs) {
1302 /* Fill the short name slot. */
1303 int copy = min_t(int, sb->s_blocksize - offset, size);
1304 memcpy(bhs[i]->b_data + offset, slots, copy);
Al Virob5224122009-06-07 13:44:36 -04001305 mark_buffer_dirty_inode(bhs[i], dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 if (IS_DIRSYNC(dir))
1307 err = sync_dirty_buffer(bhs[i]);
1308 }
1309 for (i = 0; i < nr_bhs; i++)
1310 brelse(bhs[i]);
1311 if (err)
1312 goto error_remove;
1313 }
1314
1315 if (nr_slots) {
1316 int cluster, nr_cluster;
1317
1318 /*
1319 * Third stage: allocate the cluster for new entries.
1320 * And initialize the cluster with new entries, then
1321 * add the cluster to dir.
1322 */
1323 cluster = fat_add_new_entries(dir, slots, nr_slots, &nr_cluster,
1324 &de, &bh, &i_pos);
1325 if (cluster < 0) {
1326 err = cluster;
1327 goto error_remove;
1328 }
1329 err = fat_chain_add(dir, cluster, nr_cluster);
1330 if (err) {
1331 fat_free_clusters(dir, cluster);
1332 goto error_remove;
1333 }
1334 if (dir->i_size & (sbi->cluster_size - 1)) {
Denis Karpov85c78592009-06-04 02:34:22 +09001335 fat_fs_error(sb, "Odd directory size");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 dir->i_size = (dir->i_size + sbi->cluster_size - 1)
1337 & ~((loff_t)sbi->cluster_size - 1);
1338 }
1339 dir->i_size += nr_cluster << sbi->cluster_bits;
1340 MSDOS_I(dir)->mmu_private += nr_cluster << sbi->cluster_bits;
1341 }
1342 sinfo->slot_off = pos;
1343 sinfo->de = de;
1344 sinfo->bh = bh;
1345 sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
1346
1347 return 0;
1348
1349error:
1350 brelse(bh);
1351 for (i = 0; i < nr_bhs; i++)
1352 brelse(bhs[i]);
1353 return err;
1354
1355error_remove:
1356 brelse(bh);
1357 if (free_slots)
1358 __fat_remove_entries(dir, pos, free_slots);
1359 return err;
1360}
1361
OGAWA Hirofumi7c709d02006-01-08 01:02:10 -08001362EXPORT_SYMBOL_GPL(fat_add_entries);