Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/buffer_head.h> |
David Howells | 188f83d | 2006-08-31 12:50:04 +0200 | [diff] [blame] | 20 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <asm/uaccess.h> |
Andy Shevchenko | 3ed3dec | 2010-03-16 05:48:08 +0900 | [diff] [blame] | 22 | #include <linux/kernel.h> |
OGAWA Hirofumi | 9e975da | 2008-11-06 12:53:46 -0800 | [diff] [blame] | 23 | #include "fat.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Alan Stern | 74675a5 | 2009-04-30 10:08:18 -0400 | [diff] [blame] | 25 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | static 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 Wiese | f3ef6f6 | 2005-09-06 15:17:12 -0700 | [diff] [blame] | 46 | static 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 Hirofumi | 83b7c99 | 2006-01-08 01:02:09 -0800 | [diff] [blame] | 61 | bh = sb_find_get_block(sb, phys); |
| 62 | if (bh == NULL || !buffer_uptodate(bh)) { |
Karsten Wiese | f3ef6f6 | 2005-09-06 15:17:12 -0700 | [diff] [blame] | 63 | for (sec = 0; sec < sbi->sec_per_clus; sec++) |
| 64 | sb_breadahead(sb, phys + sec); |
| 65 | } |
| 66 | brelse(bh); |
| 67 | } |
| 68 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | /* 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 | */ |
| 79 | static 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 Hirofumi | e5174ba | 2006-01-08 01:02:11 -0800 | [diff] [blame] | 84 | unsigned long mapped_blocks; |
| 85 | int err, offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | next: |
| 88 | if (*bh) |
| 89 | brelse(*bh); |
| 90 | |
| 91 | *bh = NULL; |
| 92 | iblock = *pos >> sb->s_blocksize_bits; |
OGAWA Hirofumi | 2bdf67e | 2008-11-06 12:53:57 -0800 | [diff] [blame] | 93 | err = fat_bmap(dir, iblock, &phys, &mapped_blocks, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | if (err || !phys) |
| 95 | return -1; /* beyond EOF or error */ |
| 96 | |
Karsten Wiese | f3ef6f6 | 2005-09-06 15:17:12 -0700 | [diff] [blame] | 97 | fat_dir_readahead(dir, iblock, phys); |
| 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | *bh = sb_bread(sb, phys); |
| 100 | if (*bh == NULL) { |
| 101 | printk(KERN_ERR "FAT: Directory bread(block %llu) failed\n", |
OGAWA Hirofumi | c330293 | 2008-11-06 12:53:58 -0800 | [diff] [blame] | 102 | (llu)phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | /* 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 | |
| 115 | static 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 Dobriyan | 4de151d | 2006-03-22 00:13:35 +0100 | [diff] [blame] | 130 | * Convert Unicode 16 to UTF-8, translated Unicode, or ASCII. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | * 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 | */ |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 139 | static int uni16_to_x8(unsigned char *ascii, const wchar_t *uni, int len, |
Keith Mok | f22032b | 2008-04-28 02:16:29 -0700 | [diff] [blame] | 140 | int uni_xlate, struct nls_table *nls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | { |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 142 | const wchar_t *ip; |
| 143 | wchar_t ec; |
Andy Shevchenko | 3ed3dec | 2010-03-16 05:48:08 +0900 | [diff] [blame] | 144 | unsigned char *op; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | int charlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | ip = uni; |
| 148 | op = ascii; |
| 149 | |
Keith Mok | f22032b | 2008-04-28 02:16:29 -0700 | [diff] [blame] | 150 | while (*ip && ((len - NLS_MAX_CHARSET_SIZE) > 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | ec = *ip++; |
Andy Shevchenko | 3ed3dec | 2010-03-16 05:48:08 +0900 | [diff] [blame] | 152 | if ((charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE)) > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | op += charlen; |
Keith Mok | f22032b | 2008-04-28 02:16:29 -0700 | [diff] [blame] | 154 | len -= charlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | } else { |
| 156 | if (uni_xlate == 1) { |
Andy Shevchenko | 3ed3dec | 2010-03-16 05:48:08 +0900 | [diff] [blame] | 157 | *op++ = ':'; |
| 158 | op = pack_hex_byte(op, ec >> 8); |
| 159 | op = pack_hex_byte(op, ec); |
Keith Mok | f22032b | 2008-04-28 02:16:29 -0700 | [diff] [blame] | 160 | len -= 5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } else { |
| 162 | *op++ = '?'; |
Keith Mok | f22032b | 2008-04-28 02:16:29 -0700 | [diff] [blame] | 163 | len--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
Keith Mok | f22032b | 2008-04-28 02:16:29 -0700 | [diff] [blame] | 167 | |
| 168 | if (unlikely(*ip)) { |
| 169 | printk(KERN_WARNING "FAT: filename was truncated while " |
| 170 | "converting."); |
| 171 | } |
| 172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | *op = 0; |
| 174 | return (op - ascii); |
| 175 | } |
| 176 | |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 177 | static inline int fat_uni_to_x8(struct msdos_sb_info *sbi, const wchar_t *uni, |
| 178 | unsigned char *buf, int size) |
| 179 | { |
| 180 | if (sbi->options.utf8) |
Alan Stern | 74675a5 | 2009-04-30 10:08:18 -0400 | [diff] [blame] | 181 | return utf16s_to_utf8s(uni, FAT_MAX_UNI_CHARS, |
| 182 | UTF16_HOST_ENDIAN, buf, size); |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 183 | else |
| 184 | return uni16_to_x8(buf, uni, size, sbi->options.unicode_xlate, |
| 185 | sbi->nls_io); |
| 186 | } |
| 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | static inline int |
| 189 | fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni) |
| 190 | { |
| 191 | int charlen; |
| 192 | |
| 193 | charlen = t->char2uni(c, clen, uni); |
| 194 | if (charlen < 0) { |
| 195 | *uni = 0x003f; /* a question mark */ |
| 196 | charlen = 1; |
| 197 | } |
| 198 | return charlen; |
| 199 | } |
| 200 | |
| 201 | static inline int |
| 202 | fat_short2lower_uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni) |
| 203 | { |
| 204 | int charlen; |
| 205 | wchar_t wc; |
| 206 | |
| 207 | charlen = t->char2uni(c, clen, &wc); |
| 208 | if (charlen < 0) { |
| 209 | *uni = 0x003f; /* a question mark */ |
| 210 | charlen = 1; |
| 211 | } else if (charlen <= 1) { |
| 212 | unsigned char nc = t->charset2lower[*c]; |
| 213 | |
| 214 | if (!nc) |
| 215 | nc = *c; |
| 216 | |
| 217 | if ( (charlen = t->char2uni(&nc, 1, uni)) < 0) { |
| 218 | *uni = 0x003f; /* a question mark */ |
| 219 | charlen = 1; |
| 220 | } |
| 221 | } else |
| 222 | *uni = wc; |
| 223 | |
| 224 | return charlen; |
| 225 | } |
| 226 | |
| 227 | static inline int |
| 228 | fat_shortname2uni(struct nls_table *nls, unsigned char *buf, int buf_size, |
| 229 | wchar_t *uni_buf, unsigned short opt, int lower) |
| 230 | { |
| 231 | int len = 0; |
| 232 | |
| 233 | if (opt & VFAT_SFN_DISPLAY_LOWER) |
| 234 | len = fat_short2lower_uni(nls, buf, buf_size, uni_buf); |
| 235 | else if (opt & VFAT_SFN_DISPLAY_WIN95) |
| 236 | len = fat_short2uni(nls, buf, buf_size, uni_buf); |
| 237 | else if (opt & VFAT_SFN_DISPLAY_WINNT) { |
| 238 | if (lower) |
| 239 | len = fat_short2lower_uni(nls, buf, buf_size, uni_buf); |
| 240 | else |
| 241 | len = fat_short2uni(nls, buf, buf_size, uni_buf); |
| 242 | } else |
| 243 | len = fat_short2uni(nls, buf, buf_size, uni_buf); |
| 244 | |
| 245 | return len; |
| 246 | } |
| 247 | |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 248 | static inline int fat_name_match(struct msdos_sb_info *sbi, |
| 249 | const unsigned char *a, int a_len, |
| 250 | const unsigned char *b, int b_len) |
| 251 | { |
| 252 | if (a_len != b_len) |
| 253 | return 0; |
| 254 | |
| 255 | if (sbi->options.name_check != 's') |
| 256 | return !nls_strnicmp(sbi->nls_io, a, b, a_len); |
| 257 | else |
| 258 | return !memcmp(a, b, a_len); |
| 259 | } |
| 260 | |
Pekka Enberg | ad2c160 | 2005-10-30 15:03:50 -0800 | [diff] [blame] | 261 | enum { PARSE_INVALID = 1, PARSE_NOT_LONGNAME, PARSE_EOF, }; |
| 262 | |
| 263 | /** |
| 264 | * fat_parse_long - Parse extended directory entry. |
| 265 | * |
| 266 | * This function returns zero on success, negative value on error, or one of |
| 267 | * the following: |
| 268 | * |
| 269 | * %PARSE_INVALID - Directory entry is invalid. |
| 270 | * %PARSE_NOT_LONGNAME - Directory entry does not contain longname. |
| 271 | * %PARSE_EOF - Directory has no more entries. |
| 272 | */ |
| 273 | static int fat_parse_long(struct inode *dir, loff_t *pos, |
| 274 | struct buffer_head **bh, struct msdos_dir_entry **de, |
| 275 | wchar_t **unicode, unsigned char *nr_slots) |
| 276 | { |
| 277 | struct msdos_dir_slot *ds; |
| 278 | unsigned char id, slot, slots, alias_checksum; |
| 279 | |
| 280 | if (!*unicode) { |
OGAWA Hirofumi | c7a6c4e | 2008-04-28 02:16:29 -0700 | [diff] [blame] | 281 | *unicode = __getname(); |
Pekka Enberg | ad2c160 | 2005-10-30 15:03:50 -0800 | [diff] [blame] | 282 | if (!*unicode) { |
| 283 | brelse(*bh); |
| 284 | return -ENOMEM; |
| 285 | } |
| 286 | } |
| 287 | parse_long: |
| 288 | slots = 0; |
| 289 | ds = (struct msdos_dir_slot *)*de; |
| 290 | id = ds->id; |
| 291 | if (!(id & 0x40)) |
| 292 | return PARSE_INVALID; |
| 293 | slots = id & ~0x40; |
| 294 | if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */ |
| 295 | return PARSE_INVALID; |
| 296 | *nr_slots = slots; |
| 297 | alias_checksum = ds->alias_checksum; |
| 298 | |
| 299 | slot = slots; |
| 300 | while (1) { |
| 301 | int offset; |
| 302 | |
| 303 | slot--; |
| 304 | offset = slot * 13; |
| 305 | fat16_towchar(*unicode + offset, ds->name0_4, 5); |
| 306 | fat16_towchar(*unicode + offset + 5, ds->name5_10, 6); |
| 307 | fat16_towchar(*unicode + offset + 11, ds->name11_12, 2); |
| 308 | |
| 309 | if (ds->id & 0x40) |
| 310 | (*unicode)[offset + 13] = 0; |
| 311 | if (fat_get_entry(dir, pos, bh, de) < 0) |
| 312 | return PARSE_EOF; |
| 313 | if (slot == 0) |
| 314 | break; |
| 315 | ds = (struct msdos_dir_slot *)*de; |
| 316 | if (ds->attr != ATTR_EXT) |
| 317 | return PARSE_NOT_LONGNAME; |
| 318 | if ((ds->id & ~0x40) != slot) |
| 319 | goto parse_long; |
| 320 | if (ds->alias_checksum != alias_checksum) |
| 321 | goto parse_long; |
| 322 | } |
| 323 | if ((*de)->name[0] == DELETED_FLAG) |
| 324 | return PARSE_INVALID; |
| 325 | if ((*de)->attr == ATTR_EXT) |
| 326 | goto parse_long; |
| 327 | if (IS_FREE((*de)->name) || ((*de)->attr & ATTR_VOLUME)) |
| 328 | return PARSE_INVALID; |
| 329 | if (fat_checksum((*de)->name) != alias_checksum) |
| 330 | *nr_slots = 0; |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | /* |
| 336 | * Return values: negative -> error, 0 -> not found, positive -> found, |
| 337 | * value is the total amount of slots, including the shortname entry. |
| 338 | */ |
| 339 | int fat_search_long(struct inode *inode, const unsigned char *name, |
| 340 | int name_len, struct fat_slot_info *sinfo) |
| 341 | { |
| 342 | struct super_block *sb = inode->i_sb; |
| 343 | struct msdos_sb_info *sbi = MSDOS_SB(sb); |
| 344 | struct buffer_head *bh = NULL; |
| 345 | struct msdos_dir_entry *de; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | struct nls_table *nls_disk = sbi->nls_disk; |
Keith Mok | f22032b | 2008-04-28 02:16:29 -0700 | [diff] [blame] | 347 | unsigned char nr_slots; |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 348 | wchar_t bufuname[14]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | wchar_t *unicode = NULL; |
Keith Mok | f22032b | 2008-04-28 02:16:29 -0700 | [diff] [blame] | 350 | unsigned char work[MSDOS_NAME]; |
OGAWA Hirofumi | 98a1516 | 2008-07-25 01:46:44 -0700 | [diff] [blame] | 351 | unsigned char bufname[FAT_MAX_SHORT_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | unsigned short opt_shortname = sbi->options.shortname; |
| 353 | loff_t cpos = 0; |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 354 | int chl, i, j, last_u, err, len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
| 356 | err = -ENOENT; |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 357 | while (1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | if (fat_get_entry(inode, &cpos, &bh, &de) == -1) |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 359 | goto end_of_dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | parse_record: |
| 361 | nr_slots = 0; |
| 362 | if (de->name[0] == DELETED_FLAG) |
| 363 | continue; |
| 364 | if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME)) |
| 365 | continue; |
| 366 | if (de->attr != ATTR_EXT && IS_FREE(de->name)) |
| 367 | continue; |
| 368 | if (de->attr == ATTR_EXT) { |
Pekka Enberg | ad2c160 | 2005-10-30 15:03:50 -0800 | [diff] [blame] | 369 | int status = fat_parse_long(inode, &cpos, &bh, &de, |
| 370 | &unicode, &nr_slots); |
Darren Jenkins | 52e9d9f | 2008-11-06 12:53:48 -0800 | [diff] [blame] | 371 | if (status < 0) { |
| 372 | err = status; |
| 373 | goto end_of_dir; |
| 374 | } else if (status == PARSE_INVALID) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | continue; |
Pekka Enberg | ad2c160 | 2005-10-30 15:03:50 -0800 | [diff] [blame] | 376 | else if (status == PARSE_NOT_LONGNAME) |
| 377 | goto parse_record; |
| 378 | else if (status == PARSE_EOF) |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 379 | goto end_of_dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | memcpy(work, de->name, sizeof(de->name)); |
| 383 | /* see namei.c, msdos_format_name */ |
| 384 | if (work[0] == 0x05) |
| 385 | work[0] = 0xE5; |
| 386 | for (i = 0, j = 0, last_u = 0; i < 8;) { |
OGAWA Hirofumi | 9aacd59 | 2007-07-15 23:39:56 -0700 | [diff] [blame] | 387 | if (!work[i]) |
| 388 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | chl = fat_shortname2uni(nls_disk, &work[i], 8 - i, |
| 390 | &bufuname[j++], opt_shortname, |
| 391 | de->lcase & CASE_LOWER_BASE); |
| 392 | if (chl <= 1) { |
| 393 | if (work[i] != ' ') |
| 394 | last_u = j; |
| 395 | } else { |
| 396 | last_u = j; |
| 397 | } |
| 398 | i += chl; |
| 399 | } |
| 400 | j = last_u; |
| 401 | fat_short2uni(nls_disk, ".", 1, &bufuname[j++]); |
OGAWA Hirofumi | 9aacd59 | 2007-07-15 23:39:56 -0700 | [diff] [blame] | 402 | for (i = 8; i < MSDOS_NAME;) { |
| 403 | if (!work[i]) |
| 404 | break; |
| 405 | chl = fat_shortname2uni(nls_disk, &work[i], |
| 406 | MSDOS_NAME - i, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | &bufuname[j++], opt_shortname, |
| 408 | de->lcase & CASE_LOWER_EXT); |
| 409 | if (chl <= 1) { |
OGAWA Hirofumi | 9aacd59 | 2007-07-15 23:39:56 -0700 | [diff] [blame] | 410 | if (work[i] != ' ') |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | last_u = j; |
| 412 | } else { |
| 413 | last_u = j; |
| 414 | } |
| 415 | i += chl; |
| 416 | } |
| 417 | if (!last_u) |
| 418 | continue; |
| 419 | |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 420 | /* Compare shortname */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | bufuname[last_u] = 0x0000; |
OGAWA Hirofumi | 98a1516 | 2008-07-25 01:46:44 -0700 | [diff] [blame] | 422 | len = fat_uni_to_x8(sbi, bufuname, bufname, sizeof(bufname)); |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 423 | if (fat_name_match(sbi, name, name_len, bufname, len)) |
| 424 | goto found; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
| 426 | if (nr_slots) { |
OGAWA Hirofumi | 98a1516 | 2008-07-25 01:46:44 -0700 | [diff] [blame] | 427 | void *longname = unicode + FAT_MAX_UNI_CHARS; |
| 428 | int size = PATH_MAX - FAT_MAX_UNI_SIZE; |
| 429 | |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 430 | /* Compare longname */ |
OGAWA Hirofumi | 98a1516 | 2008-07-25 01:46:44 -0700 | [diff] [blame] | 431 | len = fat_uni_to_x8(sbi, unicode, longname, size); |
| 432 | if (fat_name_match(sbi, name, name_len, longname, len)) |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 433 | goto found; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 437 | found: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | nr_slots++; /* include the de */ |
| 439 | sinfo->slot_off = cpos - nr_slots * sizeof(*de); |
| 440 | sinfo->nr_slots = nr_slots; |
| 441 | sinfo->de = de; |
| 442 | sinfo->bh = bh; |
| 443 | sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de); |
| 444 | err = 0; |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 445 | end_of_dir: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | if (unicode) |
OGAWA Hirofumi | c7a6c4e | 2008-04-28 02:16:29 -0700 | [diff] [blame] | 447 | __putname(unicode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | |
| 449 | return err; |
| 450 | } |
| 451 | |
OGAWA Hirofumi | 7c709d0 | 2006-01-08 01:02:10 -0800 | [diff] [blame] | 452 | EXPORT_SYMBOL_GPL(fat_search_long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | |
| 454 | struct fat_ioctl_filldir_callback { |
OGAWA Hirofumi | c483bab | 2007-05-08 00:31:28 -0700 | [diff] [blame] | 455 | void __user *dirent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | int result; |
| 457 | /* for dir ioctl */ |
| 458 | const char *longname; |
| 459 | int long_len; |
| 460 | const char *shortname; |
| 461 | int short_len; |
| 462 | }; |
| 463 | |
Pekka Enberg | ad2c160 | 2005-10-30 15:03:50 -0800 | [diff] [blame] | 464 | static int __fat_readdir(struct inode *inode, struct file *filp, void *dirent, |
| 465 | filldir_t filldir, int short_only, int both) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | { |
| 467 | struct super_block *sb = inode->i_sb; |
| 468 | struct msdos_sb_info *sbi = MSDOS_SB(sb); |
| 469 | struct buffer_head *bh; |
| 470 | struct msdos_dir_entry *de; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | struct nls_table *nls_disk = sbi->nls_disk; |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 472 | unsigned char nr_slots; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | wchar_t bufuname[14]; |
| 474 | wchar_t *unicode = NULL; |
OGAWA Hirofumi | 98a1516 | 2008-07-25 01:46:44 -0700 | [diff] [blame] | 475 | unsigned char c, work[MSDOS_NAME]; |
| 476 | unsigned char bufname[FAT_MAX_SHORT_SIZE], *ptname = bufname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | unsigned short opt_shortname = sbi->options.shortname; |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 478 | int isvfat = sbi->options.isvfat; |
| 479 | int nocase = sbi->options.nocase; |
OGAWA Hirofumi | dcd8c53 | 2008-07-25 01:46:44 -0700 | [diff] [blame] | 480 | const char *fill_name = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | unsigned long inum; |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 482 | unsigned long lpos, dummy, *furrfu = &lpos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | loff_t cpos; |
OGAWA Hirofumi | dcd8c53 | 2008-07-25 01:46:44 -0700 | [diff] [blame] | 484 | int chi, chl, i, i2, j, last, last_u, dotoffset = 0, fill_len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | int ret = 0; |
| 486 | |
Linus Torvalds | 8f59342 | 2008-05-19 19:53:01 -0700 | [diff] [blame] | 487 | lock_super(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | |
| 489 | cpos = filp->f_pos; |
| 490 | /* Fake . and .. for the root directory. */ |
| 491 | if (inode->i_ino == MSDOS_ROOT_INO) { |
| 492 | while (cpos < 2) { |
| 493 | if (filldir(dirent, "..", cpos+1, cpos, MSDOS_ROOT_INO, DT_DIR) < 0) |
| 494 | goto out; |
| 495 | cpos++; |
| 496 | filp->f_pos++; |
| 497 | } |
| 498 | if (cpos == 2) { |
| 499 | dummy = 2; |
| 500 | furrfu = &dummy; |
| 501 | cpos = 0; |
| 502 | } |
| 503 | } |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 504 | if (cpos & (sizeof(struct msdos_dir_entry) - 1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | ret = -ENOENT; |
| 506 | goto out; |
| 507 | } |
| 508 | |
| 509 | bh = NULL; |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 510 | get_new: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | if (fat_get_entry(inode, &cpos, &bh, &de) == -1) |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 512 | goto end_of_dir; |
Pekka Enberg | ad2c160 | 2005-10-30 15:03:50 -0800 | [diff] [blame] | 513 | parse_record: |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 514 | nr_slots = 0; |
OGAWA Hirofumi | dcd8c53 | 2008-07-25 01:46:44 -0700 | [diff] [blame] | 515 | /* |
| 516 | * Check for long filename entry, but if short_only, we don't |
| 517 | * need to parse long filename. |
| 518 | */ |
| 519 | if (isvfat && !short_only) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | if (de->name[0] == DELETED_FLAG) |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 521 | goto record_end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME)) |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 523 | goto record_end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | if (de->attr != ATTR_EXT && IS_FREE(de->name)) |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 525 | goto record_end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | } else { |
| 527 | if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name)) |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 528 | goto record_end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | if (isvfat && de->attr == ATTR_EXT) { |
Pekka Enberg | ad2c160 | 2005-10-30 15:03:50 -0800 | [diff] [blame] | 532 | int status = fat_parse_long(inode, &cpos, &bh, &de, |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 533 | &unicode, &nr_slots); |
Pekka Enberg | ad2c160 | 2005-10-30 15:03:50 -0800 | [diff] [blame] | 534 | if (status < 0) { |
| 535 | filp->f_pos = cpos; |
| 536 | ret = status; |
| 537 | goto out; |
| 538 | } else if (status == PARSE_INVALID) |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 539 | goto record_end; |
Pekka Enberg | ad2c160 | 2005-10-30 15:03:50 -0800 | [diff] [blame] | 540 | else if (status == PARSE_NOT_LONGNAME) |
| 541 | goto parse_record; |
| 542 | else if (status == PARSE_EOF) |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 543 | goto end_of_dir; |
OGAWA Hirofumi | dcd8c53 | 2008-07-25 01:46:44 -0700 | [diff] [blame] | 544 | |
| 545 | if (nr_slots) { |
| 546 | void *longname = unicode + FAT_MAX_UNI_CHARS; |
| 547 | int size = PATH_MAX - FAT_MAX_UNI_SIZE; |
| 548 | int len = fat_uni_to_x8(sbi, unicode, longname, size); |
| 549 | |
| 550 | fill_name = longname; |
| 551 | fill_len = len; |
| 552 | /* !both && !short_only, so we don't need shortname. */ |
| 553 | if (!both) |
| 554 | goto start_filldir; |
| 555 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | if (sbi->options.dotsOK) { |
| 559 | ptname = bufname; |
| 560 | dotoffset = 0; |
| 561 | if (de->attr & ATTR_HIDDEN) { |
| 562 | *ptname++ = '.'; |
| 563 | dotoffset = 1; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | memcpy(work, de->name, sizeof(de->name)); |
| 568 | /* see namei.c, msdos_format_name */ |
| 569 | if (work[0] == 0x05) |
| 570 | work[0] = 0xE5; |
| 571 | for (i = 0, j = 0, last = 0, last_u = 0; i < 8;) { |
OGAWA Hirofumi | 9aacd59 | 2007-07-15 23:39:56 -0700 | [diff] [blame] | 572 | if (!(c = work[i])) |
| 573 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | chl = fat_shortname2uni(nls_disk, &work[i], 8 - i, |
| 575 | &bufuname[j++], opt_shortname, |
| 576 | de->lcase & CASE_LOWER_BASE); |
| 577 | if (chl <= 1) { |
| 578 | ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c; |
| 579 | if (c != ' ') { |
| 580 | last = i; |
| 581 | last_u = j; |
| 582 | } |
| 583 | } else { |
| 584 | last_u = j; |
| 585 | for (chi = 0; chi < chl && i < 8; chi++) { |
| 586 | ptname[i] = work[i]; |
| 587 | i++; last = i; |
| 588 | } |
| 589 | } |
| 590 | } |
| 591 | i = last; |
| 592 | j = last_u; |
| 593 | fat_short2uni(nls_disk, ".", 1, &bufuname[j++]); |
| 594 | ptname[i++] = '.'; |
OGAWA Hirofumi | 9aacd59 | 2007-07-15 23:39:56 -0700 | [diff] [blame] | 595 | for (i2 = 8; i2 < MSDOS_NAME;) { |
| 596 | if (!(c = work[i2])) |
| 597 | break; |
| 598 | chl = fat_shortname2uni(nls_disk, &work[i2], MSDOS_NAME - i2, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | &bufuname[j++], opt_shortname, |
| 600 | de->lcase & CASE_LOWER_EXT); |
| 601 | if (chl <= 1) { |
| 602 | i2++; |
| 603 | ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c; |
| 604 | if (c != ' ') { |
| 605 | last = i; |
| 606 | last_u = j; |
| 607 | } |
| 608 | } else { |
| 609 | last_u = j; |
OGAWA Hirofumi | 9aacd59 | 2007-07-15 23:39:56 -0700 | [diff] [blame] | 610 | for (chi = 0; chi < chl && i2 < MSDOS_NAME; chi++) { |
| 611 | ptname[i++] = work[i2++]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | last = i; |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | if (!last) |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 617 | goto record_end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | |
| 619 | i = last + dotoffset; |
| 620 | j = last_u; |
| 621 | |
OGAWA Hirofumi | dcd8c53 | 2008-07-25 01:46:44 -0700 | [diff] [blame] | 622 | if (isvfat) { |
| 623 | bufuname[j] = 0x0000; |
| 624 | i = fat_uni_to_x8(sbi, bufuname, bufname, sizeof(bufname)); |
| 625 | } |
| 626 | if (nr_slots) { |
| 627 | /* hack for fat_ioctl_filldir() */ |
| 628 | struct fat_ioctl_filldir_callback *p = dirent; |
| 629 | |
| 630 | p->longname = fill_name; |
| 631 | p->long_len = fill_len; |
| 632 | p->shortname = bufname; |
| 633 | p->short_len = i; |
| 634 | fill_name = NULL; |
| 635 | fill_len = 0; |
| 636 | } else { |
| 637 | fill_name = bufname; |
| 638 | fill_len = i; |
| 639 | } |
| 640 | |
| 641 | start_filldir: |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 642 | lpos = cpos - (nr_slots + 1) * sizeof(struct msdos_dir_entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | if (!memcmp(de->name, MSDOS_DOT, MSDOS_NAME)) |
| 644 | inum = inode->i_ino; |
| 645 | else if (!memcmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) { |
Josef "Jeff" Sipek | dba3230 | 2006-12-08 02:36:39 -0800 | [diff] [blame] | 646 | inum = parent_ino(filp->f_path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | } else { |
| 648 | loff_t i_pos = fat_make_i_pos(sb, bh, de); |
| 649 | struct inode *tmp = fat_iget(sb, i_pos); |
| 650 | if (tmp) { |
| 651 | inum = tmp->i_ino; |
| 652 | iput(tmp); |
| 653 | } else |
| 654 | inum = iunique(sb, MSDOS_ROOT_INO); |
| 655 | } |
| 656 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | if (filldir(dirent, fill_name, fill_len, *furrfu, inum, |
| 658 | (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0) |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 659 | goto fill_failed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 661 | record_end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | furrfu = &lpos; |
| 663 | filp->f_pos = cpos; |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 664 | goto get_new; |
| 665 | end_of_dir: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | filp->f_pos = cpos; |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 667 | fill_failed: |
Karsten Wiese | f3ef6f6 | 2005-09-06 15:17:12 -0700 | [diff] [blame] | 668 | brelse(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | if (unicode) |
OGAWA Hirofumi | c7a6c4e | 2008-04-28 02:16:29 -0700 | [diff] [blame] | 670 | __putname(unicode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | out: |
Linus Torvalds | 8f59342 | 2008-05-19 19:53:01 -0700 | [diff] [blame] | 672 | unlock_super(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | return ret; |
| 674 | } |
| 675 | |
| 676 | static int fat_readdir(struct file *filp, void *dirent, filldir_t filldir) |
| 677 | { |
Josef "Jeff" Sipek | dba3230 | 2006-12-08 02:36:39 -0800 | [diff] [blame] | 678 | struct inode *inode = filp->f_path.dentry->d_inode; |
Pekka Enberg | ad2c160 | 2005-10-30 15:03:50 -0800 | [diff] [blame] | 679 | return __fat_readdir(inode, filp, dirent, filldir, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | } |
| 681 | |
OGAWA Hirofumi | c483bab | 2007-05-08 00:31:28 -0700 | [diff] [blame] | 682 | #define FAT_IOCTL_FILLDIR_FUNC(func, dirent_type) \ |
| 683 | static int func(void *__buf, const char *name, int name_len, \ |
| 684 | loff_t offset, u64 ino, unsigned int d_type) \ |
| 685 | { \ |
| 686 | struct fat_ioctl_filldir_callback *buf = __buf; \ |
| 687 | struct dirent_type __user *d1 = buf->dirent; \ |
| 688 | struct dirent_type __user *d2 = d1 + 1; \ |
| 689 | \ |
| 690 | if (buf->result) \ |
| 691 | return -EINVAL; \ |
| 692 | buf->result++; \ |
| 693 | \ |
| 694 | if (name != NULL) { \ |
| 695 | /* dirent has only short name */ \ |
| 696 | if (name_len >= sizeof(d1->d_name)) \ |
| 697 | name_len = sizeof(d1->d_name) - 1; \ |
| 698 | \ |
| 699 | if (put_user(0, d2->d_name) || \ |
| 700 | put_user(0, &d2->d_reclen) || \ |
| 701 | copy_to_user(d1->d_name, name, name_len) || \ |
| 702 | put_user(0, d1->d_name + name_len) || \ |
| 703 | put_user(name_len, &d1->d_reclen)) \ |
| 704 | goto efault; \ |
| 705 | } else { \ |
| 706 | /* dirent has short and long name */ \ |
| 707 | const char *longname = buf->longname; \ |
| 708 | int long_len = buf->long_len; \ |
| 709 | const char *shortname = buf->shortname; \ |
| 710 | int short_len = buf->short_len; \ |
| 711 | \ |
| 712 | if (long_len >= sizeof(d1->d_name)) \ |
| 713 | long_len = sizeof(d1->d_name) - 1; \ |
| 714 | if (short_len >= sizeof(d1->d_name)) \ |
| 715 | short_len = sizeof(d1->d_name) - 1; \ |
| 716 | \ |
| 717 | if (copy_to_user(d2->d_name, longname, long_len) || \ |
| 718 | put_user(0, d2->d_name + long_len) || \ |
| 719 | put_user(long_len, &d2->d_reclen) || \ |
| 720 | put_user(ino, &d2->d_ino) || \ |
| 721 | put_user(offset, &d2->d_off) || \ |
| 722 | copy_to_user(d1->d_name, shortname, short_len) || \ |
| 723 | put_user(0, d1->d_name + short_len) || \ |
| 724 | put_user(short_len, &d1->d_reclen)) \ |
| 725 | goto efault; \ |
| 726 | } \ |
| 727 | return 0; \ |
| 728 | efault: \ |
| 729 | buf->result = -EFAULT; \ |
| 730 | return -EFAULT; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | } |
| 732 | |
Adrian Bunk | 531f710 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 733 | FAT_IOCTL_FILLDIR_FUNC(fat_ioctl_filldir, __fat_dirent) |
OGAWA Hirofumi | c483bab | 2007-05-08 00:31:28 -0700 | [diff] [blame] | 734 | |
| 735 | static int fat_ioctl_readdir(struct inode *inode, struct file *filp, |
| 736 | void __user *dirent, filldir_t filldir, |
| 737 | int short_only, int both) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | { |
| 739 | struct fat_ioctl_filldir_callback buf; |
OGAWA Hirofumi | c483bab | 2007-05-08 00:31:28 -0700 | [diff] [blame] | 740 | int ret; |
| 741 | |
| 742 | buf.dirent = dirent; |
| 743 | buf.result = 0; |
| 744 | mutex_lock(&inode->i_mutex); |
| 745 | ret = -ENOENT; |
| 746 | if (!IS_DEADDIR(inode)) { |
| 747 | ret = __fat_readdir(inode, filp, &buf, filldir, |
| 748 | short_only, both); |
| 749 | } |
| 750 | mutex_unlock(&inode->i_mutex); |
| 751 | if (ret >= 0) |
| 752 | ret = buf.result; |
| 753 | return ret; |
| 754 | } |
| 755 | |
Arnd Bergmann | 7845bc3e | 2010-05-17 08:13:47 +0900 | [diff] [blame] | 756 | static long fat_dir_ioctl(struct file *filp, unsigned int cmd, |
| 757 | unsigned long arg) |
OGAWA Hirofumi | c483bab | 2007-05-08 00:31:28 -0700 | [diff] [blame] | 758 | { |
Arnd Bergmann | 7845bc3e | 2010-05-17 08:13:47 +0900 | [diff] [blame] | 759 | struct inode *inode = filp->f_path.dentry->d_inode; |
Adrian Bunk | 531f710 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 760 | struct __fat_dirent __user *d1 = (struct __fat_dirent __user *)arg; |
OGAWA Hirofumi | c483bab | 2007-05-08 00:31:28 -0700 | [diff] [blame] | 761 | int short_only, both; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | |
| 763 | switch (cmd) { |
| 764 | case VFAT_IOCTL_READDIR_SHORT: |
| 765 | short_only = 1; |
| 766 | both = 0; |
| 767 | break; |
| 768 | case VFAT_IOCTL_READDIR_BOTH: |
| 769 | short_only = 0; |
| 770 | both = 1; |
| 771 | break; |
| 772 | default: |
Arnd Bergmann | 7845bc3e | 2010-05-17 08:13:47 +0900 | [diff] [blame] | 773 | return fat_generic_ioctl(filp, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Adrian Bunk | 531f710 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 776 | if (!access_ok(VERIFY_WRITE, d1, sizeof(struct __fat_dirent[2]))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | return -EFAULT; |
| 778 | /* |
| 779 | * Yes, we don't need this put_user() absolutely. However old |
| 780 | * code didn't return the right value. So, app use this value, |
| 781 | * in order to check whether it is EOF. |
| 782 | */ |
| 783 | if (put_user(0, &d1->d_reclen)) |
| 784 | return -EFAULT; |
| 785 | |
OGAWA Hirofumi | c483bab | 2007-05-08 00:31:28 -0700 | [diff] [blame] | 786 | return fat_ioctl_readdir(inode, filp, d1, fat_ioctl_filldir, |
| 787 | short_only, both); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | } |
| 789 | |
David Howells | 188f83d | 2006-08-31 12:50:04 +0200 | [diff] [blame] | 790 | #ifdef CONFIG_COMPAT |
| 791 | #define VFAT_IOCTL_READDIR_BOTH32 _IOR('r', 1, struct compat_dirent[2]) |
| 792 | #define VFAT_IOCTL_READDIR_SHORT32 _IOR('r', 2, struct compat_dirent[2]) |
| 793 | |
OGAWA Hirofumi | c483bab | 2007-05-08 00:31:28 -0700 | [diff] [blame] | 794 | FAT_IOCTL_FILLDIR_FUNC(fat_compat_ioctl_filldir, compat_dirent) |
David Howells | 188f83d | 2006-08-31 12:50:04 +0200 | [diff] [blame] | 795 | |
OGAWA Hirofumi | c483bab | 2007-05-08 00:31:28 -0700 | [diff] [blame] | 796 | static long fat_compat_dir_ioctl(struct file *filp, unsigned cmd, |
David Howells | 188f83d | 2006-08-31 12:50:04 +0200 | [diff] [blame] | 797 | unsigned long arg) |
| 798 | { |
OGAWA Hirofumi | c483bab | 2007-05-08 00:31:28 -0700 | [diff] [blame] | 799 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 800 | struct compat_dirent __user *d1 = compat_ptr(arg); |
| 801 | int short_only, both; |
David Howells | 188f83d | 2006-08-31 12:50:04 +0200 | [diff] [blame] | 802 | |
| 803 | switch (cmd) { |
David Howells | 188f83d | 2006-08-31 12:50:04 +0200 | [diff] [blame] | 804 | case VFAT_IOCTL_READDIR_SHORT32: |
OGAWA Hirofumi | c483bab | 2007-05-08 00:31:28 -0700 | [diff] [blame] | 805 | short_only = 1; |
| 806 | both = 0; |
| 807 | break; |
| 808 | case VFAT_IOCTL_READDIR_BOTH32: |
| 809 | short_only = 0; |
| 810 | both = 1; |
David Howells | 188f83d | 2006-08-31 12:50:04 +0200 | [diff] [blame] | 811 | break; |
| 812 | default: |
Arnd Bergmann | 7845bc3e | 2010-05-17 08:13:47 +0900 | [diff] [blame] | 813 | return fat_generic_ioctl(filp, cmd, (unsigned long)arg); |
David Howells | 188f83d | 2006-08-31 12:50:04 +0200 | [diff] [blame] | 814 | } |
| 815 | |
OGAWA Hirofumi | c483bab | 2007-05-08 00:31:28 -0700 | [diff] [blame] | 816 | if (!access_ok(VERIFY_WRITE, d1, sizeof(struct compat_dirent[2]))) |
| 817 | return -EFAULT; |
| 818 | /* |
| 819 | * Yes, we don't need this put_user() absolutely. However old |
| 820 | * code didn't return the right value. So, app use this value, |
| 821 | * in order to check whether it is EOF. |
| 822 | */ |
| 823 | if (put_user(0, &d1->d_reclen)) |
| 824 | return -EFAULT; |
| 825 | |
| 826 | return fat_ioctl_readdir(inode, filp, d1, fat_compat_ioctl_filldir, |
| 827 | short_only, both); |
David Howells | 188f83d | 2006-08-31 12:50:04 +0200 | [diff] [blame] | 828 | } |
| 829 | #endif /* CONFIG_COMPAT */ |
| 830 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 831 | const struct file_operations fat_dir_operations = { |
OGAWA Hirofumi | 53472bc | 2008-11-06 12:53:47 -0800 | [diff] [blame] | 832 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | .read = generic_read_dir, |
| 834 | .readdir = fat_readdir, |
Arnd Bergmann | 7845bc3e | 2010-05-17 08:13:47 +0900 | [diff] [blame] | 835 | .unlocked_ioctl = fat_dir_ioctl, |
David Howells | 188f83d | 2006-08-31 12:50:04 +0200 | [diff] [blame] | 836 | #ifdef CONFIG_COMPAT |
| 837 | .compat_ioctl = fat_compat_dir_ioctl, |
| 838 | #endif |
Al Viro | b522412 | 2009-06-07 13:44:36 -0400 | [diff] [blame] | 839 | .fsync = fat_file_fsync, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | }; |
| 841 | |
| 842 | static int fat_get_short_entry(struct inode *dir, loff_t *pos, |
| 843 | struct buffer_head **bh, |
| 844 | struct msdos_dir_entry **de) |
| 845 | { |
| 846 | while (fat_get_entry(dir, pos, bh, de) >= 0) { |
| 847 | /* free entry or long name entry or volume label */ |
| 848 | if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME)) |
| 849 | return 0; |
| 850 | } |
| 851 | return -ENOENT; |
| 852 | } |
| 853 | |
| 854 | /* |
| 855 | * The ".." entry can not provide the "struct fat_slot_info" informations |
| 856 | * for inode. So, this function provide the some informations only. |
| 857 | */ |
| 858 | int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh, |
| 859 | struct msdos_dir_entry **de, loff_t *i_pos) |
| 860 | { |
| 861 | loff_t offset; |
| 862 | |
| 863 | offset = 0; |
| 864 | *bh = NULL; |
| 865 | while (fat_get_short_entry(dir, &offset, bh, de) >= 0) { |
| 866 | if (!strncmp((*de)->name, MSDOS_DOTDOT, MSDOS_NAME)) { |
| 867 | *i_pos = fat_make_i_pos(dir->i_sb, *bh, *de); |
| 868 | return 0; |
| 869 | } |
| 870 | } |
| 871 | return -ENOENT; |
| 872 | } |
| 873 | |
OGAWA Hirofumi | 7c709d0 | 2006-01-08 01:02:10 -0800 | [diff] [blame] | 874 | EXPORT_SYMBOL_GPL(fat_get_dotdot_entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | |
| 876 | /* See if directory is empty */ |
| 877 | int fat_dir_empty(struct inode *dir) |
| 878 | { |
| 879 | struct buffer_head *bh; |
| 880 | struct msdos_dir_entry *de; |
| 881 | loff_t cpos; |
| 882 | int result = 0; |
| 883 | |
| 884 | bh = NULL; |
| 885 | cpos = 0; |
| 886 | while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) { |
| 887 | if (strncmp(de->name, MSDOS_DOT , MSDOS_NAME) && |
| 888 | strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) { |
| 889 | result = -ENOTEMPTY; |
| 890 | break; |
| 891 | } |
| 892 | } |
| 893 | brelse(bh); |
| 894 | return result; |
| 895 | } |
| 896 | |
OGAWA Hirofumi | 7c709d0 | 2006-01-08 01:02:10 -0800 | [diff] [blame] | 897 | EXPORT_SYMBOL_GPL(fat_dir_empty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | |
| 899 | /* |
| 900 | * fat_subdirs counts the number of sub-directories of dir. It can be run |
| 901 | * on directories being created. |
| 902 | */ |
| 903 | int fat_subdirs(struct inode *dir) |
| 904 | { |
| 905 | struct buffer_head *bh; |
| 906 | struct msdos_dir_entry *de; |
| 907 | loff_t cpos; |
| 908 | int count = 0; |
| 909 | |
| 910 | bh = NULL; |
| 911 | cpos = 0; |
| 912 | while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) { |
| 913 | if (de->attr & ATTR_DIR) |
| 914 | count++; |
| 915 | } |
| 916 | brelse(bh); |
| 917 | return count; |
| 918 | } |
| 919 | |
| 920 | /* |
| 921 | * Scans a directory for a given file (name points to its formatted name). |
| 922 | * Returns an error code or zero. |
| 923 | */ |
| 924 | int fat_scan(struct inode *dir, const unsigned char *name, |
| 925 | struct fat_slot_info *sinfo) |
| 926 | { |
| 927 | struct super_block *sb = dir->i_sb; |
| 928 | |
| 929 | sinfo->slot_off = 0; |
| 930 | sinfo->bh = NULL; |
| 931 | while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh, |
| 932 | &sinfo->de) >= 0) { |
| 933 | if (!strncmp(sinfo->de->name, name, MSDOS_NAME)) { |
| 934 | sinfo->slot_off -= sizeof(*sinfo->de); |
| 935 | sinfo->nr_slots = 1; |
| 936 | sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de); |
| 937 | return 0; |
| 938 | } |
| 939 | } |
| 940 | return -ENOENT; |
| 941 | } |
| 942 | |
OGAWA Hirofumi | 7c709d0 | 2006-01-08 01:02:10 -0800 | [diff] [blame] | 943 | EXPORT_SYMBOL_GPL(fat_scan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | |
| 945 | static int __fat_remove_entries(struct inode *dir, loff_t pos, int nr_slots) |
| 946 | { |
| 947 | struct super_block *sb = dir->i_sb; |
| 948 | struct buffer_head *bh; |
| 949 | struct msdos_dir_entry *de, *endp; |
| 950 | int err = 0, orig_slots; |
| 951 | |
| 952 | while (nr_slots) { |
| 953 | bh = NULL; |
| 954 | if (fat_get_entry(dir, &pos, &bh, &de) < 0) { |
| 955 | err = -EIO; |
| 956 | break; |
| 957 | } |
| 958 | |
| 959 | orig_slots = nr_slots; |
| 960 | endp = (struct msdos_dir_entry *)(bh->b_data + sb->s_blocksize); |
| 961 | while (nr_slots && de < endp) { |
| 962 | de->name[0] = DELETED_FLAG; |
| 963 | de++; |
| 964 | nr_slots--; |
| 965 | } |
Al Viro | b522412 | 2009-06-07 13:44:36 -0400 | [diff] [blame] | 966 | mark_buffer_dirty_inode(bh, dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | if (IS_DIRSYNC(dir)) |
| 968 | err = sync_dirty_buffer(bh); |
| 969 | brelse(bh); |
| 970 | if (err) |
| 971 | break; |
| 972 | |
| 973 | /* pos is *next* de's position, so this does `- sizeof(de)' */ |
| 974 | pos += ((orig_slots - nr_slots) * sizeof(*de)) - sizeof(*de); |
| 975 | } |
| 976 | |
| 977 | return err; |
| 978 | } |
| 979 | |
| 980 | int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo) |
| 981 | { |
| 982 | struct msdos_dir_entry *de; |
| 983 | struct buffer_head *bh; |
| 984 | int err = 0, nr_slots; |
| 985 | |
| 986 | /* |
| 987 | * First stage: Remove the shortname. By this, the directory |
| 988 | * entry is removed. |
| 989 | */ |
| 990 | nr_slots = sinfo->nr_slots; |
| 991 | de = sinfo->de; |
| 992 | sinfo->de = NULL; |
| 993 | bh = sinfo->bh; |
| 994 | sinfo->bh = NULL; |
| 995 | while (nr_slots && de >= (struct msdos_dir_entry *)bh->b_data) { |
| 996 | de->name[0] = DELETED_FLAG; |
| 997 | de--; |
| 998 | nr_slots--; |
| 999 | } |
Al Viro | b522412 | 2009-06-07 13:44:36 -0400 | [diff] [blame] | 1000 | mark_buffer_dirty_inode(bh, dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | if (IS_DIRSYNC(dir)) |
| 1002 | err = sync_dirty_buffer(bh); |
| 1003 | brelse(bh); |
| 1004 | if (err) |
| 1005 | return err; |
| 1006 | dir->i_version++; |
| 1007 | |
| 1008 | if (nr_slots) { |
| 1009 | /* |
| 1010 | * Second stage: remove the remaining longname slots. |
| 1011 | * (This directory entry is already removed, and so return |
| 1012 | * the success) |
| 1013 | */ |
| 1014 | err = __fat_remove_entries(dir, sinfo->slot_off, nr_slots); |
| 1015 | if (err) { |
| 1016 | printk(KERN_WARNING |
| 1017 | "FAT: Couldn't remove the long name slots\n"); |
| 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | dir->i_mtime = dir->i_atime = CURRENT_TIME_SEC; |
| 1022 | if (IS_DIRSYNC(dir)) |
| 1023 | (void)fat_sync_inode(dir); |
| 1024 | else |
| 1025 | mark_inode_dirty(dir); |
| 1026 | |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |
OGAWA Hirofumi | 7c709d0 | 2006-01-08 01:02:10 -0800 | [diff] [blame] | 1030 | EXPORT_SYMBOL_GPL(fat_remove_entries); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | |
| 1032 | static int fat_zeroed_cluster(struct inode *dir, sector_t blknr, int nr_used, |
| 1033 | struct buffer_head **bhs, int nr_bhs) |
| 1034 | { |
| 1035 | struct super_block *sb = dir->i_sb; |
| 1036 | sector_t last_blknr = blknr + MSDOS_SB(sb)->sec_per_clus; |
| 1037 | int err, i, n; |
| 1038 | |
| 1039 | /* Zeroing the unused blocks on this cluster */ |
| 1040 | blknr += nr_used; |
| 1041 | n = nr_used; |
| 1042 | while (blknr < last_blknr) { |
| 1043 | bhs[n] = sb_getblk(sb, blknr); |
| 1044 | if (!bhs[n]) { |
| 1045 | err = -ENOMEM; |
| 1046 | goto error; |
| 1047 | } |
| 1048 | memset(bhs[n]->b_data, 0, sb->s_blocksize); |
| 1049 | set_buffer_uptodate(bhs[n]); |
Al Viro | b522412 | 2009-06-07 13:44:36 -0400 | [diff] [blame] | 1050 | mark_buffer_dirty_inode(bhs[n], dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | |
| 1052 | n++; |
| 1053 | blknr++; |
| 1054 | if (n == nr_bhs) { |
| 1055 | if (IS_DIRSYNC(dir)) { |
| 1056 | err = fat_sync_bhs(bhs, n); |
| 1057 | if (err) |
| 1058 | goto error; |
| 1059 | } |
| 1060 | for (i = 0; i < n; i++) |
| 1061 | brelse(bhs[i]); |
| 1062 | n = 0; |
| 1063 | } |
| 1064 | } |
| 1065 | if (IS_DIRSYNC(dir)) { |
| 1066 | err = fat_sync_bhs(bhs, n); |
| 1067 | if (err) |
| 1068 | goto error; |
| 1069 | } |
| 1070 | for (i = 0; i < n; i++) |
| 1071 | brelse(bhs[i]); |
| 1072 | |
| 1073 | return 0; |
| 1074 | |
| 1075 | error: |
| 1076 | for (i = 0; i < n; i++) |
| 1077 | bforget(bhs[i]); |
| 1078 | return err; |
| 1079 | } |
| 1080 | |
| 1081 | int fat_alloc_new_dir(struct inode *dir, struct timespec *ts) |
| 1082 | { |
| 1083 | struct super_block *sb = dir->i_sb; |
| 1084 | struct msdos_sb_info *sbi = MSDOS_SB(sb); |
| 1085 | struct buffer_head *bhs[MAX_BUF_PER_PAGE]; |
| 1086 | struct msdos_dir_entry *de; |
| 1087 | sector_t blknr; |
| 1088 | __le16 date, time; |
OGAWA Hirofumi | 7decd1c | 2008-11-06 12:53:47 -0800 | [diff] [blame] | 1089 | u8 time_cs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | int err, cluster; |
| 1091 | |
| 1092 | err = fat_alloc_clusters(dir, &cluster, 1); |
| 1093 | if (err) |
| 1094 | goto error; |
| 1095 | |
| 1096 | blknr = fat_clus_to_blknr(sbi, cluster); |
| 1097 | bhs[0] = sb_getblk(sb, blknr); |
| 1098 | if (!bhs[0]) { |
| 1099 | err = -ENOMEM; |
| 1100 | goto error_free; |
| 1101 | } |
| 1102 | |
OGAWA Hirofumi | 7decd1c | 2008-11-06 12:53:47 -0800 | [diff] [blame] | 1103 | fat_time_unix2fat(sbi, ts, &time, &date, &time_cs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | |
| 1105 | de = (struct msdos_dir_entry *)bhs[0]->b_data; |
| 1106 | /* filling the new directory slots ("." and ".." entries) */ |
| 1107 | memcpy(de[0].name, MSDOS_DOT, MSDOS_NAME); |
| 1108 | memcpy(de[1].name, MSDOS_DOTDOT, MSDOS_NAME); |
| 1109 | de->attr = de[1].attr = ATTR_DIR; |
| 1110 | de[0].lcase = de[1].lcase = 0; |
| 1111 | de[0].time = de[1].time = time; |
| 1112 | de[0].date = de[1].date = date; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | if (sbi->options.isvfat) { |
| 1114 | /* extra timestamps */ |
| 1115 | de[0].ctime = de[1].ctime = time; |
OGAWA Hirofumi | 7decd1c | 2008-11-06 12:53:47 -0800 | [diff] [blame] | 1116 | de[0].ctime_cs = de[1].ctime_cs = time_cs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = date; |
| 1118 | } else { |
| 1119 | de[0].ctime = de[1].ctime = 0; |
OGAWA Hirofumi | 7decd1c | 2008-11-06 12:53:47 -0800 | [diff] [blame] | 1120 | de[0].ctime_cs = de[1].ctime_cs = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = 0; |
| 1122 | } |
| 1123 | de[0].start = cpu_to_le16(cluster); |
| 1124 | de[0].starthi = cpu_to_le16(cluster >> 16); |
| 1125 | de[1].start = cpu_to_le16(MSDOS_I(dir)->i_logstart); |
| 1126 | de[1].starthi = cpu_to_le16(MSDOS_I(dir)->i_logstart >> 16); |
| 1127 | de[0].size = de[1].size = 0; |
| 1128 | memset(de + 2, 0, sb->s_blocksize - 2 * sizeof(*de)); |
| 1129 | set_buffer_uptodate(bhs[0]); |
Al Viro | b522412 | 2009-06-07 13:44:36 -0400 | [diff] [blame] | 1130 | mark_buffer_dirty_inode(bhs[0], dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | |
| 1132 | err = fat_zeroed_cluster(dir, blknr, 1, bhs, MAX_BUF_PER_PAGE); |
| 1133 | if (err) |
| 1134 | goto error_free; |
| 1135 | |
| 1136 | return cluster; |
| 1137 | |
| 1138 | error_free: |
| 1139 | fat_free_clusters(dir, cluster); |
| 1140 | error: |
| 1141 | return err; |
| 1142 | } |
| 1143 | |
OGAWA Hirofumi | 7c709d0 | 2006-01-08 01:02:10 -0800 | [diff] [blame] | 1144 | EXPORT_SYMBOL_GPL(fat_alloc_new_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | |
| 1146 | static int fat_add_new_entries(struct inode *dir, void *slots, int nr_slots, |
| 1147 | int *nr_cluster, struct msdos_dir_entry **de, |
| 1148 | struct buffer_head **bh, loff_t *i_pos) |
| 1149 | { |
| 1150 | struct super_block *sb = dir->i_sb; |
| 1151 | struct msdos_sb_info *sbi = MSDOS_SB(sb); |
| 1152 | struct buffer_head *bhs[MAX_BUF_PER_PAGE]; |
| 1153 | sector_t blknr, start_blknr, last_blknr; |
| 1154 | unsigned long size, copy; |
| 1155 | int err, i, n, offset, cluster[2]; |
| 1156 | |
| 1157 | /* |
| 1158 | * The minimum cluster size is 512bytes, and maximum entry |
| 1159 | * size is 32*slots (672bytes). So, iff the cluster size is |
| 1160 | * 512bytes, we may need two clusters. |
| 1161 | */ |
| 1162 | size = nr_slots * sizeof(struct msdos_dir_entry); |
| 1163 | *nr_cluster = (size + (sbi->cluster_size - 1)) >> sbi->cluster_bits; |
| 1164 | BUG_ON(*nr_cluster > 2); |
| 1165 | |
| 1166 | err = fat_alloc_clusters(dir, cluster, *nr_cluster); |
| 1167 | if (err) |
| 1168 | goto error; |
| 1169 | |
| 1170 | /* |
| 1171 | * First stage: Fill the directory entry. NOTE: This cluster |
| 1172 | * is not referenced from any inode yet, so updates order is |
| 1173 | * not important. |
| 1174 | */ |
| 1175 | i = n = copy = 0; |
| 1176 | do { |
| 1177 | start_blknr = blknr = fat_clus_to_blknr(sbi, cluster[i]); |
| 1178 | last_blknr = start_blknr + sbi->sec_per_clus; |
| 1179 | while (blknr < last_blknr) { |
| 1180 | bhs[n] = sb_getblk(sb, blknr); |
| 1181 | if (!bhs[n]) { |
| 1182 | err = -ENOMEM; |
| 1183 | goto error_nomem; |
| 1184 | } |
| 1185 | |
| 1186 | /* fill the directory entry */ |
| 1187 | copy = min(size, sb->s_blocksize); |
| 1188 | memcpy(bhs[n]->b_data, slots, copy); |
| 1189 | slots += copy; |
| 1190 | size -= copy; |
| 1191 | set_buffer_uptodate(bhs[n]); |
Al Viro | b522412 | 2009-06-07 13:44:36 -0400 | [diff] [blame] | 1192 | mark_buffer_dirty_inode(bhs[n], dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | if (!size) |
| 1194 | break; |
| 1195 | n++; |
| 1196 | blknr++; |
| 1197 | } |
| 1198 | } while (++i < *nr_cluster); |
| 1199 | |
| 1200 | memset(bhs[n]->b_data + copy, 0, sb->s_blocksize - copy); |
| 1201 | offset = copy - sizeof(struct msdos_dir_entry); |
| 1202 | get_bh(bhs[n]); |
| 1203 | *bh = bhs[n]; |
| 1204 | *de = (struct msdos_dir_entry *)((*bh)->b_data + offset); |
| 1205 | *i_pos = fat_make_i_pos(sb, *bh, *de); |
| 1206 | |
| 1207 | /* Second stage: clear the rest of cluster, and write outs */ |
| 1208 | err = fat_zeroed_cluster(dir, start_blknr, ++n, bhs, MAX_BUF_PER_PAGE); |
| 1209 | if (err) |
| 1210 | goto error_free; |
| 1211 | |
| 1212 | return cluster[0]; |
| 1213 | |
| 1214 | error_free: |
| 1215 | brelse(*bh); |
| 1216 | *bh = NULL; |
| 1217 | n = 0; |
| 1218 | error_nomem: |
| 1219 | for (i = 0; i < n; i++) |
| 1220 | bforget(bhs[i]); |
| 1221 | fat_free_clusters(dir, cluster[0]); |
| 1222 | error: |
| 1223 | return err; |
| 1224 | } |
| 1225 | |
| 1226 | int fat_add_entries(struct inode *dir, void *slots, int nr_slots, |
| 1227 | struct fat_slot_info *sinfo) |
| 1228 | { |
| 1229 | struct super_block *sb = dir->i_sb; |
| 1230 | struct msdos_sb_info *sbi = MSDOS_SB(sb); |
| 1231 | struct buffer_head *bh, *prev, *bhs[3]; /* 32*slots (672bytes) */ |
| 1232 | struct msdos_dir_entry *de; |
| 1233 | int err, free_slots, i, nr_bhs; |
| 1234 | loff_t pos, i_pos; |
| 1235 | |
| 1236 | sinfo->nr_slots = nr_slots; |
| 1237 | |
| 1238 | /* First stage: search free direcotry entries */ |
| 1239 | free_slots = nr_bhs = 0; |
| 1240 | bh = prev = NULL; |
| 1241 | pos = 0; |
| 1242 | err = -ENOSPC; |
| 1243 | while (fat_get_entry(dir, &pos, &bh, &de) > -1) { |
| 1244 | /* check the maximum size of directory */ |
| 1245 | if (pos >= FAT_MAX_DIR_SIZE) |
| 1246 | goto error; |
| 1247 | |
| 1248 | if (IS_FREE(de->name)) { |
| 1249 | if (prev != bh) { |
| 1250 | get_bh(bh); |
| 1251 | bhs[nr_bhs] = prev = bh; |
| 1252 | nr_bhs++; |
| 1253 | } |
| 1254 | free_slots++; |
| 1255 | if (free_slots == nr_slots) |
| 1256 | goto found; |
| 1257 | } else { |
| 1258 | for (i = 0; i < nr_bhs; i++) |
| 1259 | brelse(bhs[i]); |
| 1260 | prev = NULL; |
| 1261 | free_slots = nr_bhs = 0; |
| 1262 | } |
| 1263 | } |
| 1264 | if (dir->i_ino == MSDOS_ROOT_INO) { |
| 1265 | if (sbi->fat_bits != 32) |
| 1266 | goto error; |
| 1267 | } else if (MSDOS_I(dir)->i_start == 0) { |
| 1268 | printk(KERN_ERR "FAT: Corrupted directory (i_pos %lld)\n", |
| 1269 | MSDOS_I(dir)->i_pos); |
| 1270 | err = -EIO; |
| 1271 | goto error; |
| 1272 | } |
| 1273 | |
| 1274 | found: |
| 1275 | err = 0; |
| 1276 | pos -= free_slots * sizeof(*de); |
| 1277 | nr_slots -= free_slots; |
| 1278 | if (free_slots) { |
| 1279 | /* |
| 1280 | * Second stage: filling the free entries with new entries. |
| 1281 | * NOTE: If this slots has shortname, first, we write |
| 1282 | * the long name slots, then write the short name. |
| 1283 | */ |
| 1284 | int size = free_slots * sizeof(*de); |
| 1285 | int offset = pos & (sb->s_blocksize - 1); |
| 1286 | int long_bhs = nr_bhs - (nr_slots == 0); |
| 1287 | |
| 1288 | /* Fill the long name slots. */ |
| 1289 | for (i = 0; i < long_bhs; i++) { |
| 1290 | int copy = min_t(int, sb->s_blocksize - offset, size); |
| 1291 | memcpy(bhs[i]->b_data + offset, slots, copy); |
Al Viro | b522412 | 2009-06-07 13:44:36 -0400 | [diff] [blame] | 1292 | mark_buffer_dirty_inode(bhs[i], dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | offset = 0; |
| 1294 | slots += copy; |
| 1295 | size -= copy; |
| 1296 | } |
| 1297 | if (long_bhs && IS_DIRSYNC(dir)) |
| 1298 | err = fat_sync_bhs(bhs, long_bhs); |
| 1299 | if (!err && i < nr_bhs) { |
| 1300 | /* Fill the short name slot. */ |
| 1301 | int copy = min_t(int, sb->s_blocksize - offset, size); |
| 1302 | memcpy(bhs[i]->b_data + offset, slots, copy); |
Al Viro | b522412 | 2009-06-07 13:44:36 -0400 | [diff] [blame] | 1303 | mark_buffer_dirty_inode(bhs[i], dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | if (IS_DIRSYNC(dir)) |
| 1305 | err = sync_dirty_buffer(bhs[i]); |
| 1306 | } |
| 1307 | for (i = 0; i < nr_bhs; i++) |
| 1308 | brelse(bhs[i]); |
| 1309 | if (err) |
| 1310 | goto error_remove; |
| 1311 | } |
| 1312 | |
| 1313 | if (nr_slots) { |
| 1314 | int cluster, nr_cluster; |
| 1315 | |
| 1316 | /* |
| 1317 | * Third stage: allocate the cluster for new entries. |
| 1318 | * And initialize the cluster with new entries, then |
| 1319 | * add the cluster to dir. |
| 1320 | */ |
| 1321 | cluster = fat_add_new_entries(dir, slots, nr_slots, &nr_cluster, |
| 1322 | &de, &bh, &i_pos); |
| 1323 | if (cluster < 0) { |
| 1324 | err = cluster; |
| 1325 | goto error_remove; |
| 1326 | } |
| 1327 | err = fat_chain_add(dir, cluster, nr_cluster); |
| 1328 | if (err) { |
| 1329 | fat_free_clusters(dir, cluster); |
| 1330 | goto error_remove; |
| 1331 | } |
| 1332 | if (dir->i_size & (sbi->cluster_size - 1)) { |
Denis Karpov | 85c7859 | 2009-06-04 02:34:22 +0900 | [diff] [blame] | 1333 | fat_fs_error(sb, "Odd directory size"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | dir->i_size = (dir->i_size + sbi->cluster_size - 1) |
| 1335 | & ~((loff_t)sbi->cluster_size - 1); |
| 1336 | } |
| 1337 | dir->i_size += nr_cluster << sbi->cluster_bits; |
| 1338 | MSDOS_I(dir)->mmu_private += nr_cluster << sbi->cluster_bits; |
| 1339 | } |
| 1340 | sinfo->slot_off = pos; |
| 1341 | sinfo->de = de; |
| 1342 | sinfo->bh = bh; |
| 1343 | sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de); |
| 1344 | |
| 1345 | return 0; |
| 1346 | |
| 1347 | error: |
| 1348 | brelse(bh); |
| 1349 | for (i = 0; i < nr_bhs; i++) |
| 1350 | brelse(bhs[i]); |
| 1351 | return err; |
| 1352 | |
| 1353 | error_remove: |
| 1354 | brelse(bh); |
| 1355 | if (free_slots) |
| 1356 | __fat_remove_entries(dir, pos, free_slots); |
| 1357 | return err; |
| 1358 | } |
| 1359 | |
OGAWA Hirofumi | 7c709d0 | 2006-01-08 01:02:10 -0800 | [diff] [blame] | 1360 | EXPORT_SYMBOL_GPL(fat_add_entries); |