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) { |
Alexey Fisher | 869f58c | 2011-04-12 21:08:38 +0900 | [diff] [blame] | 101 | fat_msg(sb, KERN_ERR, "Directory bread(block %llu) failed", |
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 | */ |
Alexey Fisher | 869f58c | 2011-04-12 21:08:38 +0900 | [diff] [blame] | 139 | static int uni16_to_x8(struct super_block *sb, unsigned char *ascii, |
| 140 | const wchar_t *uni, int len, struct nls_table *nls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | { |
Alexey Fisher | 869f58c | 2011-04-12 21:08:38 +0900 | [diff] [blame] | 142 | int uni_xlate = MSDOS_SB(sb)->options.unicode_xlate; |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 143 | const wchar_t *ip; |
| 144 | wchar_t ec; |
Andy Shevchenko | 3ed3dec | 2010-03-16 05:48:08 +0900 | [diff] [blame] | 145 | unsigned char *op; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | int charlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
| 148 | ip = uni; |
| 149 | op = ascii; |
| 150 | |
Keith Mok | f22032b | 2008-04-28 02:16:29 -0700 | [diff] [blame] | 151 | while (*ip && ((len - NLS_MAX_CHARSET_SIZE) > 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | ec = *ip++; |
Andy Shevchenko | 3ed3dec | 2010-03-16 05:48:08 +0900 | [diff] [blame] | 153 | if ((charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE)) > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | op += charlen; |
Keith Mok | f22032b | 2008-04-28 02:16:29 -0700 | [diff] [blame] | 155 | len -= charlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } else { |
| 157 | if (uni_xlate == 1) { |
Andy Shevchenko | 3ed3dec | 2010-03-16 05:48:08 +0900 | [diff] [blame] | 158 | *op++ = ':'; |
Andy Shevchenko | 0a90e0f | 2011-10-31 17:12:57 -0700 | [diff] [blame] | 159 | op = hex_byte_pack(op, ec >> 8); |
| 160 | op = hex_byte_pack(op, ec); |
Keith Mok | f22032b | 2008-04-28 02:16:29 -0700 | [diff] [blame] | 161 | len -= 5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | } else { |
| 163 | *op++ = '?'; |
Keith Mok | f22032b | 2008-04-28 02:16:29 -0700 | [diff] [blame] | 164 | len--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
Keith Mok | f22032b | 2008-04-28 02:16:29 -0700 | [diff] [blame] | 168 | |
| 169 | if (unlikely(*ip)) { |
Alexey Fisher | 869f58c | 2011-04-12 21:08:38 +0900 | [diff] [blame] | 170 | fat_msg(sb, KERN_WARNING, "filename was truncated while " |
| 171 | "converting."); |
Keith Mok | f22032b | 2008-04-28 02:16:29 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | *op = 0; |
| 175 | return (op - ascii); |
| 176 | } |
| 177 | |
Alexey Fisher | 869f58c | 2011-04-12 21:08:38 +0900 | [diff] [blame] | 178 | static inline int fat_uni_to_x8(struct super_block *sb, const wchar_t *uni, |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 179 | unsigned char *buf, int size) |
| 180 | { |
Alexey Fisher | 869f58c | 2011-04-12 21:08:38 +0900 | [diff] [blame] | 181 | struct msdos_sb_info *sbi = MSDOS_SB(sb); |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 182 | if (sbi->options.utf8) |
Alan Stern | 74675a5 | 2009-04-30 10:08:18 -0400 | [diff] [blame] | 183 | return utf16s_to_utf8s(uni, FAT_MAX_UNI_CHARS, |
| 184 | UTF16_HOST_ENDIAN, buf, size); |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 185 | else |
Alexey Fisher | 869f58c | 2011-04-12 21:08:38 +0900 | [diff] [blame] | 186 | return uni16_to_x8(sb, buf, uni, size, sbi->nls_io); |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | static inline int |
| 190 | fat_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 | |
| 202 | static inline int |
| 203 | fat_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 | |
| 228 | static inline int |
| 229 | fat_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 Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 249 | static 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 Enberg | ad2c160 | 2005-10-30 15:03:50 -0800 | [diff] [blame] | 262 | enum { 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 | */ |
| 274 | static 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 Hirofumi | c7a6c4e | 2008-04-28 02:16:29 -0700 | [diff] [blame] | 282 | *unicode = __getname(); |
Pekka Enberg | ad2c160 | 2005-10-30 15:03:50 -0800 | [diff] [blame] | 283 | if (!*unicode) { |
| 284 | brelse(*bh); |
| 285 | return -ENOMEM; |
| 286 | } |
| 287 | } |
| 288 | parse_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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | /* |
| 337 | * Return values: negative -> error, 0 -> not found, positive -> found, |
| 338 | * value is the total amount of slots, including the shortname entry. |
| 339 | */ |
| 340 | int 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | struct nls_table *nls_disk = sbi->nls_disk; |
Keith Mok | f22032b | 2008-04-28 02:16:29 -0700 | [diff] [blame] | 348 | unsigned char nr_slots; |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 349 | wchar_t bufuname[14]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | wchar_t *unicode = NULL; |
Keith Mok | f22032b | 2008-04-28 02:16:29 -0700 | [diff] [blame] | 351 | unsigned char work[MSDOS_NAME]; |
OGAWA Hirofumi | 98a1516 | 2008-07-25 01:46:44 -0700 | [diff] [blame] | 352 | unsigned char bufname[FAT_MAX_SHORT_SIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | unsigned short opt_shortname = sbi->options.shortname; |
| 354 | loff_t cpos = 0; |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 355 | int chl, i, j, last_u, err, len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | |
| 357 | err = -ENOENT; |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 358 | while (1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | if (fat_get_entry(inode, &cpos, &bh, &de) == -1) |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 360 | goto end_of_dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | parse_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 Enberg | ad2c160 | 2005-10-30 15:03:50 -0800 | [diff] [blame] | 370 | int status = fat_parse_long(inode, &cpos, &bh, &de, |
| 371 | &unicode, &nr_slots); |
Darren Jenkins | 52e9d9f | 2008-11-06 12:53:48 -0800 | [diff] [blame] | 372 | if (status < 0) { |
| 373 | err = status; |
| 374 | goto end_of_dir; |
| 375 | } else if (status == PARSE_INVALID) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | continue; |
Pekka Enberg | ad2c160 | 2005-10-30 15:03:50 -0800 | [diff] [blame] | 377 | else if (status == PARSE_NOT_LONGNAME) |
| 378 | goto parse_record; |
| 379 | else if (status == PARSE_EOF) |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 380 | goto end_of_dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | } |
| 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 Hirofumi | 9aacd59 | 2007-07-15 23:39:56 -0700 | [diff] [blame] | 388 | if (!work[i]) |
| 389 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | 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 Hirofumi | 9aacd59 | 2007-07-15 23:39:56 -0700 | [diff] [blame] | 403 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | &bufuname[j++], opt_shortname, |
| 409 | de->lcase & CASE_LOWER_EXT); |
| 410 | if (chl <= 1) { |
OGAWA Hirofumi | 9aacd59 | 2007-07-15 23:39:56 -0700 | [diff] [blame] | 411 | if (work[i] != ' ') |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | last_u = j; |
| 413 | } else { |
| 414 | last_u = j; |
| 415 | } |
| 416 | i += chl; |
| 417 | } |
| 418 | if (!last_u) |
| 419 | continue; |
| 420 | |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 421 | /* Compare shortname */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | bufuname[last_u] = 0x0000; |
Alexey Fisher | 869f58c | 2011-04-12 21:08:38 +0900 | [diff] [blame] | 423 | len = fat_uni_to_x8(sb, bufuname, bufname, sizeof(bufname)); |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 424 | if (fat_name_match(sbi, name, name_len, bufname, len)) |
| 425 | goto found; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
| 427 | if (nr_slots) { |
OGAWA Hirofumi | 98a1516 | 2008-07-25 01:46:44 -0700 | [diff] [blame] | 428 | void *longname = unicode + FAT_MAX_UNI_CHARS; |
| 429 | int size = PATH_MAX - FAT_MAX_UNI_SIZE; |
| 430 | |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 431 | /* Compare longname */ |
Alexey Fisher | 869f58c | 2011-04-12 21:08:38 +0900 | [diff] [blame] | 432 | len = fat_uni_to_x8(sb, unicode, longname, size); |
OGAWA Hirofumi | 98a1516 | 2008-07-25 01:46:44 -0700 | [diff] [blame] | 433 | if (fat_name_match(sbi, name, name_len, longname, len)) |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 434 | goto found; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 438 | found: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | 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 Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 446 | end_of_dir: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | if (unicode) |
OGAWA Hirofumi | c7a6c4e | 2008-04-28 02:16:29 -0700 | [diff] [blame] | 448 | __putname(unicode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | |
| 450 | return err; |
| 451 | } |
| 452 | |
OGAWA Hirofumi | 7c709d0 | 2006-01-08 01:02:10 -0800 | [diff] [blame] | 453 | EXPORT_SYMBOL_GPL(fat_search_long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
| 455 | struct fat_ioctl_filldir_callback { |
OGAWA Hirofumi | c483bab | 2007-05-08 00:31:28 -0700 | [diff] [blame] | 456 | void __user *dirent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | 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 Enberg | ad2c160 | 2005-10-30 15:03:50 -0800 | [diff] [blame] | 465 | static int __fat_readdir(struct inode *inode, struct file *filp, void *dirent, |
| 466 | filldir_t filldir, int short_only, int both) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | { |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | struct nls_table *nls_disk = sbi->nls_disk; |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 473 | unsigned char nr_slots; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | wchar_t bufuname[14]; |
| 475 | wchar_t *unicode = NULL; |
OGAWA Hirofumi | 98a1516 | 2008-07-25 01:46:44 -0700 | [diff] [blame] | 476 | unsigned char c, work[MSDOS_NAME]; |
| 477 | unsigned char bufname[FAT_MAX_SHORT_SIZE], *ptname = bufname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | unsigned short opt_shortname = sbi->options.shortname; |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 479 | int isvfat = sbi->options.isvfat; |
| 480 | int nocase = sbi->options.nocase; |
OGAWA Hirofumi | dcd8c53 | 2008-07-25 01:46:44 -0700 | [diff] [blame] | 481 | const char *fill_name = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | unsigned long inum; |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 483 | unsigned long lpos, dummy, *furrfu = &lpos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | loff_t cpos; |
OGAWA Hirofumi | dcd8c53 | 2008-07-25 01:46:44 -0700 | [diff] [blame] | 485 | 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] | 486 | int ret = 0; |
| 487 | |
Linus Torvalds | 8f59342 | 2008-05-19 19:53:01 -0700 | [diff] [blame] | 488 | lock_super(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
| 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 Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 505 | if (cpos & (sizeof(struct msdos_dir_entry) - 1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | ret = -ENOENT; |
| 507 | goto out; |
| 508 | } |
| 509 | |
| 510 | bh = NULL; |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 511 | get_new: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | if (fat_get_entry(inode, &cpos, &bh, &de) == -1) |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 513 | goto end_of_dir; |
Pekka Enberg | ad2c160 | 2005-10-30 15:03:50 -0800 | [diff] [blame] | 514 | parse_record: |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 515 | nr_slots = 0; |
OGAWA Hirofumi | dcd8c53 | 2008-07-25 01:46:44 -0700 | [diff] [blame] | 516 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | if (de->name[0] == DELETED_FLAG) |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 522 | goto record_end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME)) |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 524 | goto record_end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | if (de->attr != ATTR_EXT && IS_FREE(de->name)) |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 526 | goto record_end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | } else { |
| 528 | if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name)) |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 529 | goto record_end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | if (isvfat && de->attr == ATTR_EXT) { |
Pekka Enberg | ad2c160 | 2005-10-30 15:03:50 -0800 | [diff] [blame] | 533 | int status = fat_parse_long(inode, &cpos, &bh, &de, |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 534 | &unicode, &nr_slots); |
Pekka Enberg | ad2c160 | 2005-10-30 15:03:50 -0800 | [diff] [blame] | 535 | if (status < 0) { |
| 536 | filp->f_pos = cpos; |
| 537 | ret = status; |
| 538 | goto out; |
| 539 | } else if (status == PARSE_INVALID) |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 540 | goto record_end; |
Pekka Enberg | ad2c160 | 2005-10-30 15:03:50 -0800 | [diff] [blame] | 541 | else if (status == PARSE_NOT_LONGNAME) |
| 542 | goto parse_record; |
| 543 | else if (status == PARSE_EOF) |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 544 | goto end_of_dir; |
OGAWA Hirofumi | dcd8c53 | 2008-07-25 01:46:44 -0700 | [diff] [blame] | 545 | |
| 546 | if (nr_slots) { |
| 547 | void *longname = unicode + FAT_MAX_UNI_CHARS; |
| 548 | int size = PATH_MAX - FAT_MAX_UNI_SIZE; |
Alexey Fisher | 869f58c | 2011-04-12 21:08:38 +0900 | [diff] [blame] | 549 | int len = fat_uni_to_x8(sb, unicode, longname, size); |
OGAWA Hirofumi | dcd8c53 | 2008-07-25 01:46:44 -0700 | [diff] [blame] | 550 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | } |
| 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 Hirofumi | 9aacd59 | 2007-07-15 23:39:56 -0700 | [diff] [blame] | 573 | if (!(c = work[i])) |
| 574 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | 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 Hirofumi | 9aacd59 | 2007-07-15 23:39:56 -0700 | [diff] [blame] | 596 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | &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 Hirofumi | 9aacd59 | 2007-07-15 23:39:56 -0700 | [diff] [blame] | 611 | for (chi = 0; chi < chl && i2 < MSDOS_NAME; chi++) { |
| 612 | ptname[i++] = work[i2++]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | last = i; |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | if (!last) |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 618 | goto record_end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | |
| 620 | i = last + dotoffset; |
| 621 | j = last_u; |
| 622 | |
OGAWA Hirofumi | dcd8c53 | 2008-07-25 01:46:44 -0700 | [diff] [blame] | 623 | if (isvfat) { |
| 624 | bufuname[j] = 0x0000; |
Alexey Fisher | 869f58c | 2011-04-12 21:08:38 +0900 | [diff] [blame] | 625 | i = fat_uni_to_x8(sb, bufuname, bufname, sizeof(bufname)); |
OGAWA Hirofumi | dcd8c53 | 2008-07-25 01:46:44 -0700 | [diff] [blame] | 626 | } |
| 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 | |
| 642 | start_filldir: |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 643 | lpos = cpos - (nr_slots + 1) * sizeof(struct msdos_dir_entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | 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" Sipek | dba3230 | 2006-12-08 02:36:39 -0800 | [diff] [blame] | 647 | inum = parent_ino(filp->f_path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | } 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | if (filldir(dirent, fill_name, fill_len, *furrfu, inum, |
| 659 | (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0) |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 660 | goto fill_failed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 662 | record_end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | furrfu = &lpos; |
| 664 | filp->f_pos = cpos; |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 665 | goto get_new; |
| 666 | end_of_dir: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | filp->f_pos = cpos; |
OGAWA Hirofumi | d688611 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 668 | fill_failed: |
Karsten Wiese | f3ef6f6 | 2005-09-06 15:17:12 -0700 | [diff] [blame] | 669 | brelse(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | if (unicode) |
OGAWA Hirofumi | c7a6c4e | 2008-04-28 02:16:29 -0700 | [diff] [blame] | 671 | __putname(unicode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | out: |
Linus Torvalds | 8f59342 | 2008-05-19 19:53:01 -0700 | [diff] [blame] | 673 | unlock_super(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | return ret; |
| 675 | } |
| 676 | |
| 677 | static int fat_readdir(struct file *filp, void *dirent, filldir_t filldir) |
| 678 | { |
Josef "Jeff" Sipek | dba3230 | 2006-12-08 02:36:39 -0800 | [diff] [blame] | 679 | struct inode *inode = filp->f_path.dentry->d_inode; |
Pekka Enberg | ad2c160 | 2005-10-30 15:03:50 -0800 | [diff] [blame] | 680 | return __fat_readdir(inode, filp, dirent, filldir, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | } |
| 682 | |
OGAWA Hirofumi | c483bab | 2007-05-08 00:31:28 -0700 | [diff] [blame] | 683 | #define FAT_IOCTL_FILLDIR_FUNC(func, dirent_type) \ |
| 684 | static 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; \ |
| 729 | efault: \ |
| 730 | buf->result = -EFAULT; \ |
| 731 | return -EFAULT; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Adrian Bunk | 531f710 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 734 | FAT_IOCTL_FILLDIR_FUNC(fat_ioctl_filldir, __fat_dirent) |
OGAWA Hirofumi | c483bab | 2007-05-08 00:31:28 -0700 | [diff] [blame] | 735 | |
| 736 | static int fat_ioctl_readdir(struct inode *inode, struct file *filp, |
| 737 | void __user *dirent, filldir_t filldir, |
| 738 | int short_only, int both) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | { |
| 740 | struct fat_ioctl_filldir_callback buf; |
OGAWA Hirofumi | c483bab | 2007-05-08 00:31:28 -0700 | [diff] [blame] | 741 | 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 | |
Mike Lockwood | b1042b9 | 2008-03-15 13:29:36 -0400 | [diff] [blame] | 757 | static int fat_ioctl_volume_id(struct inode *dir) |
| 758 | { |
| 759 | struct super_block *sb = dir->i_sb; |
| 760 | struct msdos_sb_info *sbi = MSDOS_SB(sb); |
| 761 | return sbi->vol_id; |
| 762 | } |
| 763 | |
Arnd Bergmann | 7845bc3e | 2010-05-17 08:13:47 +0900 | [diff] [blame] | 764 | static long fat_dir_ioctl(struct file *filp, unsigned int cmd, |
| 765 | unsigned long arg) |
OGAWA Hirofumi | c483bab | 2007-05-08 00:31:28 -0700 | [diff] [blame] | 766 | { |
Arnd Bergmann | 7845bc3e | 2010-05-17 08:13:47 +0900 | [diff] [blame] | 767 | struct inode *inode = filp->f_path.dentry->d_inode; |
Adrian Bunk | 531f710 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 768 | struct __fat_dirent __user *d1 = (struct __fat_dirent __user *)arg; |
OGAWA Hirofumi | c483bab | 2007-05-08 00:31:28 -0700 | [diff] [blame] | 769 | int short_only, both; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | |
| 771 | switch (cmd) { |
| 772 | case VFAT_IOCTL_READDIR_SHORT: |
| 773 | short_only = 1; |
| 774 | both = 0; |
| 775 | break; |
| 776 | case VFAT_IOCTL_READDIR_BOTH: |
| 777 | short_only = 0; |
| 778 | both = 1; |
| 779 | break; |
Mike Lockwood | b1042b9 | 2008-03-15 13:29:36 -0400 | [diff] [blame] | 780 | case VFAT_IOCTL_GET_VOLUME_ID: |
| 781 | return fat_ioctl_volume_id(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | default: |
Arnd Bergmann | 7845bc3e | 2010-05-17 08:13:47 +0900 | [diff] [blame] | 783 | return fat_generic_ioctl(filp, cmd, arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | } |
| 785 | |
Adrian Bunk | 531f710 | 2008-07-25 01:46:43 -0700 | [diff] [blame] | 786 | if (!access_ok(VERIFY_WRITE, d1, sizeof(struct __fat_dirent[2]))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | return -EFAULT; |
| 788 | /* |
| 789 | * Yes, we don't need this put_user() absolutely. However old |
| 790 | * code didn't return the right value. So, app use this value, |
| 791 | * in order to check whether it is EOF. |
| 792 | */ |
| 793 | if (put_user(0, &d1->d_reclen)) |
| 794 | return -EFAULT; |
| 795 | |
OGAWA Hirofumi | c483bab | 2007-05-08 00:31:28 -0700 | [diff] [blame] | 796 | return fat_ioctl_readdir(inode, filp, d1, fat_ioctl_filldir, |
| 797 | short_only, both); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | } |
| 799 | |
David Howells | 188f83d | 2006-08-31 12:50:04 +0200 | [diff] [blame] | 800 | #ifdef CONFIG_COMPAT |
| 801 | #define VFAT_IOCTL_READDIR_BOTH32 _IOR('r', 1, struct compat_dirent[2]) |
| 802 | #define VFAT_IOCTL_READDIR_SHORT32 _IOR('r', 2, struct compat_dirent[2]) |
| 803 | |
OGAWA Hirofumi | c483bab | 2007-05-08 00:31:28 -0700 | [diff] [blame] | 804 | FAT_IOCTL_FILLDIR_FUNC(fat_compat_ioctl_filldir, compat_dirent) |
David Howells | 188f83d | 2006-08-31 12:50:04 +0200 | [diff] [blame] | 805 | |
OGAWA Hirofumi | c483bab | 2007-05-08 00:31:28 -0700 | [diff] [blame] | 806 | static long fat_compat_dir_ioctl(struct file *filp, unsigned cmd, |
David Howells | 188f83d | 2006-08-31 12:50:04 +0200 | [diff] [blame] | 807 | unsigned long arg) |
| 808 | { |
OGAWA Hirofumi | c483bab | 2007-05-08 00:31:28 -0700 | [diff] [blame] | 809 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 810 | struct compat_dirent __user *d1 = compat_ptr(arg); |
| 811 | int short_only, both; |
David Howells | 188f83d | 2006-08-31 12:50:04 +0200 | [diff] [blame] | 812 | |
| 813 | switch (cmd) { |
David Howells | 188f83d | 2006-08-31 12:50:04 +0200 | [diff] [blame] | 814 | case VFAT_IOCTL_READDIR_SHORT32: |
OGAWA Hirofumi | c483bab | 2007-05-08 00:31:28 -0700 | [diff] [blame] | 815 | short_only = 1; |
| 816 | both = 0; |
| 817 | break; |
| 818 | case VFAT_IOCTL_READDIR_BOTH32: |
| 819 | short_only = 0; |
| 820 | both = 1; |
David Howells | 188f83d | 2006-08-31 12:50:04 +0200 | [diff] [blame] | 821 | break; |
| 822 | default: |
Arnd Bergmann | 7845bc3e | 2010-05-17 08:13:47 +0900 | [diff] [blame] | 823 | return fat_generic_ioctl(filp, cmd, (unsigned long)arg); |
David Howells | 188f83d | 2006-08-31 12:50:04 +0200 | [diff] [blame] | 824 | } |
| 825 | |
OGAWA Hirofumi | c483bab | 2007-05-08 00:31:28 -0700 | [diff] [blame] | 826 | if (!access_ok(VERIFY_WRITE, d1, sizeof(struct compat_dirent[2]))) |
| 827 | return -EFAULT; |
| 828 | /* |
| 829 | * Yes, we don't need this put_user() absolutely. However old |
| 830 | * code didn't return the right value. So, app use this value, |
| 831 | * in order to check whether it is EOF. |
| 832 | */ |
| 833 | if (put_user(0, &d1->d_reclen)) |
| 834 | return -EFAULT; |
| 835 | |
| 836 | return fat_ioctl_readdir(inode, filp, d1, fat_compat_ioctl_filldir, |
| 837 | short_only, both); |
David Howells | 188f83d | 2006-08-31 12:50:04 +0200 | [diff] [blame] | 838 | } |
| 839 | #endif /* CONFIG_COMPAT */ |
| 840 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 841 | const struct file_operations fat_dir_operations = { |
OGAWA Hirofumi | 53472bc | 2008-11-06 12:53:47 -0800 | [diff] [blame] | 842 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | .read = generic_read_dir, |
| 844 | .readdir = fat_readdir, |
Arnd Bergmann | 7845bc3e | 2010-05-17 08:13:47 +0900 | [diff] [blame] | 845 | .unlocked_ioctl = fat_dir_ioctl, |
David Howells | 188f83d | 2006-08-31 12:50:04 +0200 | [diff] [blame] | 846 | #ifdef CONFIG_COMPAT |
| 847 | .compat_ioctl = fat_compat_dir_ioctl, |
| 848 | #endif |
Al Viro | b522412 | 2009-06-07 13:44:36 -0400 | [diff] [blame] | 849 | .fsync = fat_file_fsync, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | }; |
| 851 | |
| 852 | static int fat_get_short_entry(struct inode *dir, loff_t *pos, |
| 853 | struct buffer_head **bh, |
| 854 | struct msdos_dir_entry **de) |
| 855 | { |
| 856 | while (fat_get_entry(dir, pos, bh, de) >= 0) { |
| 857 | /* free entry or long name entry or volume label */ |
| 858 | if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME)) |
| 859 | return 0; |
| 860 | } |
| 861 | return -ENOENT; |
| 862 | } |
| 863 | |
| 864 | /* |
| 865 | * The ".." entry can not provide the "struct fat_slot_info" informations |
| 866 | * for inode. So, this function provide the some informations only. |
| 867 | */ |
| 868 | int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh, |
| 869 | struct msdos_dir_entry **de, loff_t *i_pos) |
| 870 | { |
| 871 | loff_t offset; |
| 872 | |
| 873 | offset = 0; |
| 874 | *bh = NULL; |
| 875 | while (fat_get_short_entry(dir, &offset, bh, de) >= 0) { |
| 876 | if (!strncmp((*de)->name, MSDOS_DOTDOT, MSDOS_NAME)) { |
| 877 | *i_pos = fat_make_i_pos(dir->i_sb, *bh, *de); |
| 878 | return 0; |
| 879 | } |
| 880 | } |
| 881 | return -ENOENT; |
| 882 | } |
| 883 | |
OGAWA Hirofumi | 7c709d0 | 2006-01-08 01:02:10 -0800 | [diff] [blame] | 884 | EXPORT_SYMBOL_GPL(fat_get_dotdot_entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | |
| 886 | /* See if directory is empty */ |
| 887 | int fat_dir_empty(struct inode *dir) |
| 888 | { |
| 889 | struct buffer_head *bh; |
| 890 | struct msdos_dir_entry *de; |
| 891 | loff_t cpos; |
| 892 | int result = 0; |
| 893 | |
| 894 | bh = NULL; |
| 895 | cpos = 0; |
| 896 | while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) { |
| 897 | if (strncmp(de->name, MSDOS_DOT , MSDOS_NAME) && |
| 898 | strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) { |
| 899 | result = -ENOTEMPTY; |
| 900 | break; |
| 901 | } |
| 902 | } |
| 903 | brelse(bh); |
| 904 | return result; |
| 905 | } |
| 906 | |
OGAWA Hirofumi | 7c709d0 | 2006-01-08 01:02:10 -0800 | [diff] [blame] | 907 | EXPORT_SYMBOL_GPL(fat_dir_empty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | |
| 909 | /* |
| 910 | * fat_subdirs counts the number of sub-directories of dir. It can be run |
| 911 | * on directories being created. |
| 912 | */ |
| 913 | int fat_subdirs(struct inode *dir) |
| 914 | { |
| 915 | struct buffer_head *bh; |
| 916 | struct msdos_dir_entry *de; |
| 917 | loff_t cpos; |
| 918 | int count = 0; |
| 919 | |
| 920 | bh = NULL; |
| 921 | cpos = 0; |
| 922 | while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) { |
| 923 | if (de->attr & ATTR_DIR) |
| 924 | count++; |
| 925 | } |
| 926 | brelse(bh); |
| 927 | return count; |
| 928 | } |
| 929 | |
| 930 | /* |
| 931 | * Scans a directory for a given file (name points to its formatted name). |
| 932 | * Returns an error code or zero. |
| 933 | */ |
| 934 | int fat_scan(struct inode *dir, const unsigned char *name, |
| 935 | struct fat_slot_info *sinfo) |
| 936 | { |
| 937 | struct super_block *sb = dir->i_sb; |
| 938 | |
| 939 | sinfo->slot_off = 0; |
| 940 | sinfo->bh = NULL; |
| 941 | while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh, |
| 942 | &sinfo->de) >= 0) { |
| 943 | if (!strncmp(sinfo->de->name, name, MSDOS_NAME)) { |
| 944 | sinfo->slot_off -= sizeof(*sinfo->de); |
| 945 | sinfo->nr_slots = 1; |
| 946 | sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de); |
| 947 | return 0; |
| 948 | } |
| 949 | } |
| 950 | return -ENOENT; |
| 951 | } |
| 952 | |
OGAWA Hirofumi | 7c709d0 | 2006-01-08 01:02:10 -0800 | [diff] [blame] | 953 | EXPORT_SYMBOL_GPL(fat_scan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | |
| 955 | static int __fat_remove_entries(struct inode *dir, loff_t pos, int nr_slots) |
| 956 | { |
| 957 | struct super_block *sb = dir->i_sb; |
| 958 | struct buffer_head *bh; |
| 959 | struct msdos_dir_entry *de, *endp; |
| 960 | int err = 0, orig_slots; |
| 961 | |
| 962 | while (nr_slots) { |
| 963 | bh = NULL; |
| 964 | if (fat_get_entry(dir, &pos, &bh, &de) < 0) { |
| 965 | err = -EIO; |
| 966 | break; |
| 967 | } |
| 968 | |
| 969 | orig_slots = nr_slots; |
| 970 | endp = (struct msdos_dir_entry *)(bh->b_data + sb->s_blocksize); |
| 971 | while (nr_slots && de < endp) { |
| 972 | de->name[0] = DELETED_FLAG; |
| 973 | de++; |
| 974 | nr_slots--; |
| 975 | } |
Al Viro | b522412 | 2009-06-07 13:44:36 -0400 | [diff] [blame] | 976 | mark_buffer_dirty_inode(bh, dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | if (IS_DIRSYNC(dir)) |
| 978 | err = sync_dirty_buffer(bh); |
| 979 | brelse(bh); |
| 980 | if (err) |
| 981 | break; |
| 982 | |
| 983 | /* pos is *next* de's position, so this does `- sizeof(de)' */ |
| 984 | pos += ((orig_slots - nr_slots) * sizeof(*de)) - sizeof(*de); |
| 985 | } |
| 986 | |
| 987 | return err; |
| 988 | } |
| 989 | |
| 990 | int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo) |
| 991 | { |
Alexey Fisher | 869f58c | 2011-04-12 21:08:38 +0900 | [diff] [blame] | 992 | struct super_block *sb = dir->i_sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | struct msdos_dir_entry *de; |
| 994 | struct buffer_head *bh; |
| 995 | int err = 0, nr_slots; |
| 996 | |
| 997 | /* |
| 998 | * First stage: Remove the shortname. By this, the directory |
| 999 | * entry is removed. |
| 1000 | */ |
| 1001 | nr_slots = sinfo->nr_slots; |
| 1002 | de = sinfo->de; |
| 1003 | sinfo->de = NULL; |
| 1004 | bh = sinfo->bh; |
| 1005 | sinfo->bh = NULL; |
| 1006 | while (nr_slots && de >= (struct msdos_dir_entry *)bh->b_data) { |
| 1007 | de->name[0] = DELETED_FLAG; |
| 1008 | de--; |
| 1009 | nr_slots--; |
| 1010 | } |
Al Viro | b522412 | 2009-06-07 13:44:36 -0400 | [diff] [blame] | 1011 | mark_buffer_dirty_inode(bh, dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | if (IS_DIRSYNC(dir)) |
| 1013 | err = sync_dirty_buffer(bh); |
| 1014 | brelse(bh); |
| 1015 | if (err) |
| 1016 | return err; |
| 1017 | dir->i_version++; |
| 1018 | |
| 1019 | if (nr_slots) { |
| 1020 | /* |
| 1021 | * Second stage: remove the remaining longname slots. |
| 1022 | * (This directory entry is already removed, and so return |
| 1023 | * the success) |
| 1024 | */ |
| 1025 | err = __fat_remove_entries(dir, sinfo->slot_off, nr_slots); |
| 1026 | if (err) { |
Alexey Fisher | 869f58c | 2011-04-12 21:08:38 +0900 | [diff] [blame] | 1027 | fat_msg(sb, KERN_WARNING, |
| 1028 | "Couldn't remove the long name slots"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | dir->i_mtime = dir->i_atime = CURRENT_TIME_SEC; |
| 1033 | if (IS_DIRSYNC(dir)) |
| 1034 | (void)fat_sync_inode(dir); |
| 1035 | else |
| 1036 | mark_inode_dirty(dir); |
| 1037 | |
| 1038 | return 0; |
| 1039 | } |
| 1040 | |
OGAWA Hirofumi | 7c709d0 | 2006-01-08 01:02:10 -0800 | [diff] [blame] | 1041 | EXPORT_SYMBOL_GPL(fat_remove_entries); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | |
| 1043 | static int fat_zeroed_cluster(struct inode *dir, sector_t blknr, int nr_used, |
| 1044 | struct buffer_head **bhs, int nr_bhs) |
| 1045 | { |
| 1046 | struct super_block *sb = dir->i_sb; |
| 1047 | sector_t last_blknr = blknr + MSDOS_SB(sb)->sec_per_clus; |
| 1048 | int err, i, n; |
| 1049 | |
| 1050 | /* Zeroing the unused blocks on this cluster */ |
| 1051 | blknr += nr_used; |
| 1052 | n = nr_used; |
| 1053 | while (blknr < last_blknr) { |
| 1054 | bhs[n] = sb_getblk(sb, blknr); |
| 1055 | if (!bhs[n]) { |
| 1056 | err = -ENOMEM; |
| 1057 | goto error; |
| 1058 | } |
| 1059 | memset(bhs[n]->b_data, 0, sb->s_blocksize); |
| 1060 | set_buffer_uptodate(bhs[n]); |
Al Viro | b522412 | 2009-06-07 13:44:36 -0400 | [diff] [blame] | 1061 | mark_buffer_dirty_inode(bhs[n], dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | |
| 1063 | n++; |
| 1064 | blknr++; |
| 1065 | if (n == nr_bhs) { |
| 1066 | if (IS_DIRSYNC(dir)) { |
| 1067 | err = fat_sync_bhs(bhs, n); |
| 1068 | if (err) |
| 1069 | goto error; |
| 1070 | } |
| 1071 | for (i = 0; i < n; i++) |
| 1072 | brelse(bhs[i]); |
| 1073 | n = 0; |
| 1074 | } |
| 1075 | } |
| 1076 | if (IS_DIRSYNC(dir)) { |
| 1077 | err = fat_sync_bhs(bhs, n); |
| 1078 | if (err) |
| 1079 | goto error; |
| 1080 | } |
| 1081 | for (i = 0; i < n; i++) |
| 1082 | brelse(bhs[i]); |
| 1083 | |
| 1084 | return 0; |
| 1085 | |
| 1086 | error: |
| 1087 | for (i = 0; i < n; i++) |
| 1088 | bforget(bhs[i]); |
| 1089 | return err; |
| 1090 | } |
| 1091 | |
| 1092 | int fat_alloc_new_dir(struct inode *dir, struct timespec *ts) |
| 1093 | { |
| 1094 | struct super_block *sb = dir->i_sb; |
| 1095 | struct msdos_sb_info *sbi = MSDOS_SB(sb); |
| 1096 | struct buffer_head *bhs[MAX_BUF_PER_PAGE]; |
| 1097 | struct msdos_dir_entry *de; |
| 1098 | sector_t blknr; |
| 1099 | __le16 date, time; |
OGAWA Hirofumi | 7decd1c | 2008-11-06 12:53:47 -0800 | [diff] [blame] | 1100 | u8 time_cs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | int err, cluster; |
| 1102 | |
| 1103 | err = fat_alloc_clusters(dir, &cluster, 1); |
| 1104 | if (err) |
| 1105 | goto error; |
| 1106 | |
| 1107 | blknr = fat_clus_to_blknr(sbi, cluster); |
| 1108 | bhs[0] = sb_getblk(sb, blknr); |
| 1109 | if (!bhs[0]) { |
| 1110 | err = -ENOMEM; |
| 1111 | goto error_free; |
| 1112 | } |
| 1113 | |
OGAWA Hirofumi | 7decd1c | 2008-11-06 12:53:47 -0800 | [diff] [blame] | 1114 | fat_time_unix2fat(sbi, ts, &time, &date, &time_cs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | |
| 1116 | de = (struct msdos_dir_entry *)bhs[0]->b_data; |
| 1117 | /* filling the new directory slots ("." and ".." entries) */ |
| 1118 | memcpy(de[0].name, MSDOS_DOT, MSDOS_NAME); |
| 1119 | memcpy(de[1].name, MSDOS_DOTDOT, MSDOS_NAME); |
| 1120 | de->attr = de[1].attr = ATTR_DIR; |
| 1121 | de[0].lcase = de[1].lcase = 0; |
| 1122 | de[0].time = de[1].time = time; |
| 1123 | de[0].date = de[1].date = date; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | if (sbi->options.isvfat) { |
| 1125 | /* extra timestamps */ |
| 1126 | de[0].ctime = de[1].ctime = time; |
OGAWA Hirofumi | 7decd1c | 2008-11-06 12:53:47 -0800 | [diff] [blame] | 1127 | de[0].ctime_cs = de[1].ctime_cs = time_cs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = date; |
| 1129 | } else { |
| 1130 | de[0].ctime = de[1].ctime = 0; |
OGAWA Hirofumi | 7decd1c | 2008-11-06 12:53:47 -0800 | [diff] [blame] | 1131 | de[0].ctime_cs = de[1].ctime_cs = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = 0; |
| 1133 | } |
| 1134 | de[0].start = cpu_to_le16(cluster); |
| 1135 | de[0].starthi = cpu_to_le16(cluster >> 16); |
| 1136 | de[1].start = cpu_to_le16(MSDOS_I(dir)->i_logstart); |
| 1137 | de[1].starthi = cpu_to_le16(MSDOS_I(dir)->i_logstart >> 16); |
| 1138 | de[0].size = de[1].size = 0; |
| 1139 | memset(de + 2, 0, sb->s_blocksize - 2 * sizeof(*de)); |
| 1140 | set_buffer_uptodate(bhs[0]); |
Al Viro | b522412 | 2009-06-07 13:44:36 -0400 | [diff] [blame] | 1141 | mark_buffer_dirty_inode(bhs[0], dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | |
| 1143 | err = fat_zeroed_cluster(dir, blknr, 1, bhs, MAX_BUF_PER_PAGE); |
| 1144 | if (err) |
| 1145 | goto error_free; |
| 1146 | |
| 1147 | return cluster; |
| 1148 | |
| 1149 | error_free: |
| 1150 | fat_free_clusters(dir, cluster); |
| 1151 | error: |
| 1152 | return err; |
| 1153 | } |
| 1154 | |
OGAWA Hirofumi | 7c709d0 | 2006-01-08 01:02:10 -0800 | [diff] [blame] | 1155 | EXPORT_SYMBOL_GPL(fat_alloc_new_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | |
| 1157 | static int fat_add_new_entries(struct inode *dir, void *slots, int nr_slots, |
| 1158 | int *nr_cluster, struct msdos_dir_entry **de, |
| 1159 | struct buffer_head **bh, loff_t *i_pos) |
| 1160 | { |
| 1161 | struct super_block *sb = dir->i_sb; |
| 1162 | struct msdos_sb_info *sbi = MSDOS_SB(sb); |
| 1163 | struct buffer_head *bhs[MAX_BUF_PER_PAGE]; |
| 1164 | sector_t blknr, start_blknr, last_blknr; |
| 1165 | unsigned long size, copy; |
| 1166 | int err, i, n, offset, cluster[2]; |
| 1167 | |
| 1168 | /* |
| 1169 | * The minimum cluster size is 512bytes, and maximum entry |
| 1170 | * size is 32*slots (672bytes). So, iff the cluster size is |
| 1171 | * 512bytes, we may need two clusters. |
| 1172 | */ |
| 1173 | size = nr_slots * sizeof(struct msdos_dir_entry); |
| 1174 | *nr_cluster = (size + (sbi->cluster_size - 1)) >> sbi->cluster_bits; |
| 1175 | BUG_ON(*nr_cluster > 2); |
| 1176 | |
| 1177 | err = fat_alloc_clusters(dir, cluster, *nr_cluster); |
| 1178 | if (err) |
| 1179 | goto error; |
| 1180 | |
| 1181 | /* |
| 1182 | * First stage: Fill the directory entry. NOTE: This cluster |
| 1183 | * is not referenced from any inode yet, so updates order is |
| 1184 | * not important. |
| 1185 | */ |
| 1186 | i = n = copy = 0; |
| 1187 | do { |
| 1188 | start_blknr = blknr = fat_clus_to_blknr(sbi, cluster[i]); |
| 1189 | last_blknr = start_blknr + sbi->sec_per_clus; |
| 1190 | while (blknr < last_blknr) { |
| 1191 | bhs[n] = sb_getblk(sb, blknr); |
| 1192 | if (!bhs[n]) { |
| 1193 | err = -ENOMEM; |
| 1194 | goto error_nomem; |
| 1195 | } |
| 1196 | |
| 1197 | /* fill the directory entry */ |
| 1198 | copy = min(size, sb->s_blocksize); |
| 1199 | memcpy(bhs[n]->b_data, slots, copy); |
| 1200 | slots += copy; |
| 1201 | size -= copy; |
| 1202 | set_buffer_uptodate(bhs[n]); |
Al Viro | b522412 | 2009-06-07 13:44:36 -0400 | [diff] [blame] | 1203 | mark_buffer_dirty_inode(bhs[n], dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | if (!size) |
| 1205 | break; |
| 1206 | n++; |
| 1207 | blknr++; |
| 1208 | } |
| 1209 | } while (++i < *nr_cluster); |
| 1210 | |
| 1211 | memset(bhs[n]->b_data + copy, 0, sb->s_blocksize - copy); |
| 1212 | offset = copy - sizeof(struct msdos_dir_entry); |
| 1213 | get_bh(bhs[n]); |
| 1214 | *bh = bhs[n]; |
| 1215 | *de = (struct msdos_dir_entry *)((*bh)->b_data + offset); |
| 1216 | *i_pos = fat_make_i_pos(sb, *bh, *de); |
| 1217 | |
| 1218 | /* Second stage: clear the rest of cluster, and write outs */ |
| 1219 | err = fat_zeroed_cluster(dir, start_blknr, ++n, bhs, MAX_BUF_PER_PAGE); |
| 1220 | if (err) |
| 1221 | goto error_free; |
| 1222 | |
| 1223 | return cluster[0]; |
| 1224 | |
| 1225 | error_free: |
| 1226 | brelse(*bh); |
| 1227 | *bh = NULL; |
| 1228 | n = 0; |
| 1229 | error_nomem: |
| 1230 | for (i = 0; i < n; i++) |
| 1231 | bforget(bhs[i]); |
| 1232 | fat_free_clusters(dir, cluster[0]); |
| 1233 | error: |
| 1234 | return err; |
| 1235 | } |
| 1236 | |
| 1237 | int fat_add_entries(struct inode *dir, void *slots, int nr_slots, |
| 1238 | struct fat_slot_info *sinfo) |
| 1239 | { |
| 1240 | struct super_block *sb = dir->i_sb; |
| 1241 | struct msdos_sb_info *sbi = MSDOS_SB(sb); |
| 1242 | struct buffer_head *bh, *prev, *bhs[3]; /* 32*slots (672bytes) */ |
Jonas Aberg | 8c320c0 | 2011-08-17 19:10:06 +0900 | [diff] [blame] | 1243 | struct msdos_dir_entry *uninitialized_var(de); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | int err, free_slots, i, nr_bhs; |
| 1245 | loff_t pos, i_pos; |
| 1246 | |
| 1247 | sinfo->nr_slots = nr_slots; |
| 1248 | |
| 1249 | /* First stage: search free direcotry entries */ |
| 1250 | free_slots = nr_bhs = 0; |
| 1251 | bh = prev = NULL; |
| 1252 | pos = 0; |
| 1253 | err = -ENOSPC; |
| 1254 | while (fat_get_entry(dir, &pos, &bh, &de) > -1) { |
| 1255 | /* check the maximum size of directory */ |
| 1256 | if (pos >= FAT_MAX_DIR_SIZE) |
| 1257 | goto error; |
| 1258 | |
| 1259 | if (IS_FREE(de->name)) { |
| 1260 | if (prev != bh) { |
| 1261 | get_bh(bh); |
| 1262 | bhs[nr_bhs] = prev = bh; |
| 1263 | nr_bhs++; |
| 1264 | } |
| 1265 | free_slots++; |
| 1266 | if (free_slots == nr_slots) |
| 1267 | goto found; |
| 1268 | } else { |
| 1269 | for (i = 0; i < nr_bhs; i++) |
| 1270 | brelse(bhs[i]); |
| 1271 | prev = NULL; |
| 1272 | free_slots = nr_bhs = 0; |
| 1273 | } |
| 1274 | } |
| 1275 | if (dir->i_ino == MSDOS_ROOT_INO) { |
| 1276 | if (sbi->fat_bits != 32) |
| 1277 | goto error; |
| 1278 | } else if (MSDOS_I(dir)->i_start == 0) { |
Alexey Fisher | 869f58c | 2011-04-12 21:08:38 +0900 | [diff] [blame] | 1279 | fat_msg(sb, KERN_ERR, "Corrupted directory (i_pos %lld)", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | MSDOS_I(dir)->i_pos); |
| 1281 | err = -EIO; |
| 1282 | goto error; |
| 1283 | } |
| 1284 | |
| 1285 | found: |
| 1286 | err = 0; |
| 1287 | pos -= free_slots * sizeof(*de); |
| 1288 | nr_slots -= free_slots; |
| 1289 | if (free_slots) { |
| 1290 | /* |
| 1291 | * Second stage: filling the free entries with new entries. |
| 1292 | * NOTE: If this slots has shortname, first, we write |
| 1293 | * the long name slots, then write the short name. |
| 1294 | */ |
| 1295 | int size = free_slots * sizeof(*de); |
| 1296 | int offset = pos & (sb->s_blocksize - 1); |
| 1297 | int long_bhs = nr_bhs - (nr_slots == 0); |
| 1298 | |
| 1299 | /* Fill the long name slots. */ |
| 1300 | for (i = 0; i < long_bhs; i++) { |
| 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 | offset = 0; |
| 1305 | slots += copy; |
| 1306 | size -= copy; |
| 1307 | } |
| 1308 | if (long_bhs && IS_DIRSYNC(dir)) |
| 1309 | err = fat_sync_bhs(bhs, long_bhs); |
| 1310 | if (!err && i < nr_bhs) { |
| 1311 | /* Fill the short name slot. */ |
| 1312 | int copy = min_t(int, sb->s_blocksize - offset, size); |
| 1313 | memcpy(bhs[i]->b_data + offset, slots, copy); |
Al Viro | b522412 | 2009-06-07 13:44:36 -0400 | [diff] [blame] | 1314 | mark_buffer_dirty_inode(bhs[i], dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | if (IS_DIRSYNC(dir)) |
| 1316 | err = sync_dirty_buffer(bhs[i]); |
| 1317 | } |
| 1318 | for (i = 0; i < nr_bhs; i++) |
| 1319 | brelse(bhs[i]); |
| 1320 | if (err) |
| 1321 | goto error_remove; |
| 1322 | } |
| 1323 | |
| 1324 | if (nr_slots) { |
| 1325 | int cluster, nr_cluster; |
| 1326 | |
| 1327 | /* |
| 1328 | * Third stage: allocate the cluster for new entries. |
| 1329 | * And initialize the cluster with new entries, then |
| 1330 | * add the cluster to dir. |
| 1331 | */ |
| 1332 | cluster = fat_add_new_entries(dir, slots, nr_slots, &nr_cluster, |
| 1333 | &de, &bh, &i_pos); |
| 1334 | if (cluster < 0) { |
| 1335 | err = cluster; |
| 1336 | goto error_remove; |
| 1337 | } |
| 1338 | err = fat_chain_add(dir, cluster, nr_cluster); |
| 1339 | if (err) { |
| 1340 | fat_free_clusters(dir, cluster); |
| 1341 | goto error_remove; |
| 1342 | } |
| 1343 | if (dir->i_size & (sbi->cluster_size - 1)) { |
Denis Karpov | 85c7859 | 2009-06-04 02:34:22 +0900 | [diff] [blame] | 1344 | fat_fs_error(sb, "Odd directory size"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | dir->i_size = (dir->i_size + sbi->cluster_size - 1) |
| 1346 | & ~((loff_t)sbi->cluster_size - 1); |
| 1347 | } |
| 1348 | dir->i_size += nr_cluster << sbi->cluster_bits; |
| 1349 | MSDOS_I(dir)->mmu_private += nr_cluster << sbi->cluster_bits; |
| 1350 | } |
| 1351 | sinfo->slot_off = pos; |
| 1352 | sinfo->de = de; |
| 1353 | sinfo->bh = bh; |
| 1354 | sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de); |
| 1355 | |
| 1356 | return 0; |
| 1357 | |
| 1358 | error: |
| 1359 | brelse(bh); |
| 1360 | for (i = 0; i < nr_bhs; i++) |
| 1361 | brelse(bhs[i]); |
| 1362 | return err; |
| 1363 | |
| 1364 | error_remove: |
| 1365 | brelse(bh); |
| 1366 | if (free_slots) |
| 1367 | __fat_remove_entries(dir, pos, free_slots); |
| 1368 | return err; |
| 1369 | } |
| 1370 | |
OGAWA Hirofumi | 7c709d0 | 2006-01-08 01:02:10 -0800 | [diff] [blame] | 1371 | EXPORT_SYMBOL_GPL(fat_add_entries); |