| 1 | |
| 2 | |
| 3 | * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
|
| 4 |
|
| 5 |
|
| 6 | #include <linux/slab.h>
|
| 7 | #include <linux/compat.h>
|
| 8 | #include <linux/bio.h>
|
| 9 | #include <linux/buffer_head.h>
|
| 10 | #include <linux/filelock.h>
|
| 11 |
|
| 12 | #include "exfat_raw.h"
|
| 13 | #include "exfat_fs.h"
|
| 14 |
|
| 15 | static int exfat_extract_uni_name(struct exfat_dentry *ep,
|
| 16 | unsigned short *uniname)
|
| 17 | {
|
| 18 | int i, len = 0;
|
| 19 |
|
| 20 | for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
|
| 21 | *uniname = le16_to_cpu(ep->dentry.name.unicode_0_14[i]);
|
| 22 | if (*uniname == 0x0)
|
| 23 | return len;
|
| 24 | uniname++;
|
| 25 | len++;
|
| 26 | }
|
| 27 |
|
| 28 | *uniname = 0x0;
|
| 29 | return len;
|
| 30 |
|
| 31 | }
|
| 32 |
|
| 33 | static int exfat_get_uniname_from_ext_entry(struct super_block *sb,
|
| 34 | struct exfat_chain *p_dir, int entry, unsigned short *uniname)
|
| 35 | {
|
| 36 | int i, err;
|
| 37 | struct exfat_entry_set_cache es;
|
| 38 | unsigned int uni_len = 0, len;
|
| 39 |
|
| 40 | err = exfat_get_dentry_set(&es, sb, p_dir, entry, ES_ALL_ENTRIES);
|
| 41 | if (err)
|
| 42 | return err;
|
| 43 |
|
| 44 | |
| 45 | * First entry : file entry
|
| 46 | * Second entry : stream-extension entry
|
| 47 | * Third entry : first file-name entry
|
| 48 | * So, the index of first file-name dentry should start from 2.
|
| 49 |
|
| 50 | for (i = ES_IDX_FIRST_FILENAME; i < es.num_entries; i++) {
|
| 51 | struct exfat_dentry *ep = exfat_get_dentry_cached(&es, i);
|
| 52 |
|
| 53 |
|
| 54 | if (exfat_get_entry_type(ep) != TYPE_EXTEND)
|
| 55 | break;
|
| 56 |
|
| 57 | len = exfat_extract_uni_name(ep, uniname);
|
| 58 | uni_len += len;
|
| 59 | if (len != EXFAT_FILE_NAME_LEN || uni_len >= MAX_NAME_LENGTH)
|
| 60 | break;
|
| 61 | uniname += EXFAT_FILE_NAME_LEN;
|
| 62 | }
|
| 63 |
|
| 64 | exfat_put_dentry_set(&es, false);
|
| 65 | return 0;
|
| 66 | }
|
| 67 |
|
| 68 |
|
| 69 | static int exfat_readdir(struct inode *inode, loff_t *cpos, struct exfat_dir_entry *dir_entry)
|
| 70 | {
|
| 71 | int i, dentries_per_clu, num_ext, err;
|
| 72 | unsigned int type, clu_offset, max_dentries;
|
| 73 | struct exfat_chain dir, clu;
|
| 74 | struct exfat_uni_name uni_name;
|
| 75 | struct exfat_dentry *ep;
|
| 76 | struct super_block *sb = inode->i_sb;
|
| 77 | struct exfat_sb_info *sbi = EXFAT_SB(sb);
|
| 78 | struct exfat_inode_info *ei = EXFAT_I(inode);
|
| 79 | unsigned int dentry = exfat_bytes_to_dentries(*cpos) & 0xFFFFFFFF;
|
| 80 | struct buffer_head *bh;
|
| 81 |
|
| 82 |
|
| 83 | if (ei->type != TYPE_DIR)
|
| 84 | return -EPERM;
|
| 85 |
|
| 86 | exfat_chain_set(&dir, ei->start_clu,
|
| 87 | exfat_bytes_to_cluster(sbi, i_size_read(inode)), ei->flags);
|
| 88 |
|
| 89 | dentries_per_clu = sbi->dentries_per_clu;
|
| 90 | max_dentries = (unsigned int)min_t(u64, MAX_EXFAT_DENTRIES,
|
| 91 | exfat_cluster_to_dentries(sbi, sbi->num_clusters));
|
| 92 |
|
| 93 | clu_offset = exfat_dentries_to_cluster(sbi, dentry);
|
| 94 | exfat_chain_dup(&clu, &dir);
|
| 95 |
|
| 96 | if (clu.flags == ALLOC_FAT_CHAIN) {
|
| 97 |
|
| 98 | if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER &&
|
| 99 | ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) {
|
| 100 | clu_offset -= ei->hint_bmap.off;
|
| 101 | clu.dir = ei->hint_bmap.clu;
|
| 102 | clu.size -= ei->hint_bmap.off;
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | if (exfat_chain_advance(sb, &clu, clu_offset))
|
| 107 | return -EIO;
|
| 108 |
|
| 109 | while (clu.dir != EXFAT_EOF_CLUSTER && dentry < max_dentries) {
|
| 110 | i = dentry & (dentries_per_clu - 1);
|
| 111 |
|
| 112 | for ( ; i < dentries_per_clu; i++, dentry++) {
|
| 113 | ep = exfat_get_dentry(sb, &clu, i, &bh);
|
| 114 | if (!ep)
|
| 115 | return -EIO;
|
| 116 |
|
| 117 | type = exfat_get_entry_type(ep);
|
| 118 | if (type == TYPE_UNUSED) {
|
| 119 | brelse(bh);
|
| 120 | goto out;
|
| 121 | }
|
| 122 |
|
| 123 | if (type != TYPE_FILE && type != TYPE_DIR) {
|
| 124 | brelse(bh);
|
| 125 | continue;
|
| 126 | }
|
| 127 |
|
| 128 | num_ext = ep->dentry.file.num_ext;
|
| 129 | dir_entry->attr = le16_to_cpu(ep->dentry.file.attr);
|
| 130 |
|
| 131 | *uni_name.name = 0x0;
|
| 132 | err = exfat_get_uniname_from_ext_entry(sb, &clu, i,
|
| 133 | uni_name.name);
|
| 134 | if (err) {
|
| 135 | brelse(bh);
|
| 136 | continue;
|
| 137 | }
|
| 138 | exfat_utf16_to_nls(sb, &uni_name,
|
| 139 | dir_entry->namebuf.lfn,
|
| 140 | dir_entry->namebuf.lfnbuf_len);
|
| 141 | brelse(bh);
|
| 142 |
|
| 143 | ep = exfat_get_dentry(sb, &clu, i + 1, &bh);
|
| 144 | if (!ep)
|
| 145 | return -EIO;
|
| 146 | dir_entry->entry = i;
|
| 147 | dir_entry->dir = clu;
|
| 148 | brelse(bh);
|
| 149 |
|
| 150 | ei->hint_bmap.off = exfat_dentries_to_cluster(sbi, dentry);
|
| 151 | ei->hint_bmap.clu = clu.dir;
|
| 152 |
|
| 153 | *cpos = exfat_dentries_to_bytes(dentry + 1 + num_ext);
|
| 154 | return 0;
|
| 155 | }
|
| 156 |
|
| 157 | if (exfat_chain_advance(sb, &clu, 1))
|
| 158 | return -EIO;
|
| 159 | }
|
| 160 |
|
| 161 | out:
|
| 162 | dir_entry->namebuf.lfn[0] = '\0';
|
| 163 | *cpos = exfat_dentries_to_bytes(dentry);
|
| 164 | return 0;
|
| 165 | }
|
| 166 |
|
| 167 | static void exfat_init_namebuf(struct exfat_dentry_namebuf *nb)
|
| 168 | {
|
| 169 | nb->lfn = NULL;
|
| 170 | nb->lfnbuf_len = 0;
|
| 171 | }
|
| 172 |
|
| 173 | static int exfat_alloc_namebuf(struct exfat_dentry_namebuf *nb)
|
| 174 | {
|
| 175 | nb->lfn = __getname();
|
| 176 | if (!nb->lfn)
|
| 177 | return -ENOMEM;
|
| 178 | nb->lfnbuf_len = MAX_VFSNAME_BUF_SIZE;
|
| 179 | return 0;
|
| 180 | }
|
| 181 |
|
| 182 | static void exfat_free_namebuf(struct exfat_dentry_namebuf *nb)
|
| 183 | {
|
| 184 | if (!nb->lfn)
|
| 185 | return;
|
| 186 |
|
| 187 | __putname(nb->lfn);
|
| 188 | exfat_init_namebuf(nb);
|
| 189 | }
|
| 190 |
|
| 191 | |
| 192 | * Before calling dir_emit*(), sbi->s_lock should be released
|
| 193 | * because page fault can occur in dir_emit*().
|
| 194 |
|
| 195 | #define ITER_POS_FILLED_DOTS (2)
|
| 196 | static int exfat_iterate(struct file *file, struct dir_context *ctx)
|
| 197 | {
|
| 198 | struct inode *inode = file_inode(file);
|
| 199 | struct super_block *sb = inode->i_sb;
|
| 200 | struct inode *tmp;
|
| 201 | struct exfat_dir_entry de;
|
| 202 | struct exfat_dentry_namebuf *nb = &(de.namebuf);
|
| 203 | struct exfat_inode_info *ei = EXFAT_I(inode);
|
| 204 | unsigned long inum;
|
| 205 | loff_t cpos, i_pos;
|
| 206 | int err = 0, fake_offset = 0;
|
| 207 |
|
| 208 | exfat_init_namebuf(nb);
|
| 209 |
|
| 210 | cpos = ctx->pos;
|
| 211 | if (!dir_emit_dots(file, ctx))
|
| 212 | goto out;
|
| 213 |
|
| 214 | if (ctx->pos == ITER_POS_FILLED_DOTS) {
|
| 215 | cpos = 0;
|
| 216 | fake_offset = 1;
|
| 217 | }
|
| 218 |
|
| 219 | cpos = round_up(cpos, DENTRY_SIZE);
|
| 220 |
|
| 221 |
|
| 222 | err = exfat_alloc_namebuf(nb);
|
| 223 | if (err)
|
| 224 | goto out;
|
| 225 | get_new:
|
| 226 | mutex_lock(&EXFAT_SB(sb)->s_lock);
|
| 227 |
|
| 228 | if (ei->flags == ALLOC_NO_FAT_CHAIN && cpos >= i_size_read(inode))
|
| 229 | goto end_of_dir;
|
| 230 |
|
| 231 | err = exfat_readdir(inode, &cpos, &de);
|
| 232 | if (err) {
|
| 233 | |
| 234 | * At least we tried to read a sector.
|
| 235 | * Move cpos to next sector position (should be aligned).
|
| 236 |
|
| 237 | if (err == -EIO) {
|
| 238 | cpos += 1 << (sb->s_blocksize_bits);
|
| 239 | cpos &= ~(loff_t)(sb->s_blocksize - 1);
|
| 240 | }
|
| 241 |
|
| 242 | err = -EIO;
|
| 243 | goto end_of_dir;
|
| 244 | }
|
| 245 |
|
| 246 | if (!nb->lfn[0])
|
| 247 | goto end_of_dir;
|
| 248 |
|
| 249 | i_pos = ((loff_t)de.dir.dir << 32) | (de.entry & 0xffffffff);
|
| 250 | tmp = exfat_iget(sb, i_pos);
|
| 251 | if (tmp) {
|
| 252 | inum = tmp->i_ino;
|
| 253 | iput(tmp);
|
| 254 | } else {
|
| 255 | inum = iunique(sb, EXFAT_ROOT_INO);
|
| 256 | }
|
| 257 |
|
| 258 | mutex_unlock(&EXFAT_SB(sb)->s_lock);
|
| 259 | if (!dir_emit(ctx, nb->lfn, strlen(nb->lfn), inum,
|
| 260 | (de.attr & EXFAT_ATTR_SUBDIR) ? DT_DIR : DT_REG))
|
| 261 | goto out;
|
| 262 | ctx->pos = cpos;
|
| 263 | goto get_new;
|
| 264 |
|
| 265 | end_of_dir:
|
| 266 | if (!cpos && fake_offset)
|
| 267 | cpos = ITER_POS_FILLED_DOTS;
|
| 268 | ctx->pos = cpos;
|
| 269 | mutex_unlock(&EXFAT_SB(sb)->s_lock);
|
| 270 | out:
|
| 271 | |
| 272 | * To improve performance, free namebuf after unlock sb_lock.
|
| 273 | * If namebuf is not allocated, this function do nothing
|
| 274 |
|
| 275 | exfat_free_namebuf(nb);
|
| 276 | return err;
|
| 277 | }
|
| 278 |
|
| 279 | WRAP_DIR_ITER(exfat_iterate) |
| 280 | const struct file_operations exfat_dir_operations = {
|
| 281 | .llseek = generic_file_llseek,
|
| 282 | .read = generic_read_dir,
|
| 283 | .iterate_shared = shared_exfat_iterate,
|
| 284 | .unlocked_ioctl = exfat_ioctl,
|
| 285 | #ifdef CONFIG_COMPAT
|
| 286 | .compat_ioctl = exfat_compat_ioctl,
|
| 287 | #endif
|
| 288 | .fsync = exfat_file_fsync,
|
| 289 | .setlease = generic_setlease,
|
| 290 | };
|
| 291 |
|
| 292 | int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu)
|
| 293 | {
|
| 294 | int ret;
|
| 295 |
|
| 296 | exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN);
|
| 297 |
|
| 298 | ret = exfat_alloc_cluster(inode, 1, clu, IS_DIRSYNC(inode), false);
|
| 299 | if (ret)
|
| 300 | return ret;
|
| 301 |
|
| 302 | ret = exfat_zeroed_cluster(inode, clu->dir);
|
| 303 | if (ret) {
|
| 304 | exfat_free_cluster(inode, clu);
|
| 305 | return ret;
|
| 306 | }
|
| 307 |
|
| 308 | return 0;
|
| 309 | }
|
| 310 |
|
| 311 | int exfat_calc_num_entries(struct exfat_uni_name *p_uniname)
|
| 312 | {
|
| 313 | int len;
|
| 314 |
|
| 315 | len = p_uniname->name_len;
|
| 316 | if (len == 0)
|
| 317 | return -EINVAL;
|
| 318 |
|
| 319 |
|
| 320 | return ES_ENTRY_NUM(len);
|
| 321 | }
|
| 322 |
|
| 323 | unsigned int exfat_get_entry_type(struct exfat_dentry *ep)
|
| 324 | {
|
| 325 | if (ep->type == EXFAT_UNUSED)
|
| 326 | return TYPE_UNUSED;
|
| 327 | if (IS_EXFAT_DELETED(ep->type))
|
| 328 | return TYPE_DELETED;
|
| 329 | if (ep->type == EXFAT_INVAL)
|
| 330 | return TYPE_INVALID;
|
| 331 | if (IS_EXFAT_CRITICAL_PRI(ep->type)) {
|
| 332 | if (ep->type == EXFAT_BITMAP)
|
| 333 | return TYPE_BITMAP;
|
| 334 | if (ep->type == EXFAT_UPCASE)
|
| 335 | return TYPE_UPCASE;
|
| 336 | if (ep->type == EXFAT_VOLUME)
|
| 337 | return TYPE_VOLUME;
|
| 338 | if (ep->type == EXFAT_FILE) {
|
| 339 | if (le16_to_cpu(ep->dentry.file.attr) & EXFAT_ATTR_SUBDIR)
|
| 340 | return TYPE_DIR;
|
| 341 | return TYPE_FILE;
|
| 342 | }
|
| 343 | return TYPE_CRITICAL_PRI;
|
| 344 | }
|
| 345 | if (IS_EXFAT_BENIGN_PRI(ep->type)) {
|
| 346 | if (ep->type == EXFAT_GUID)
|
| 347 | return TYPE_GUID;
|
| 348 | if (ep->type == EXFAT_PADDING)
|
| 349 | return TYPE_PADDING;
|
| 350 | if (ep->type == EXFAT_ACLTAB)
|
| 351 | return TYPE_ACLTAB;
|
| 352 | return TYPE_BENIGN_PRI;
|
| 353 | }
|
| 354 | if (IS_EXFAT_CRITICAL_SEC(ep->type)) {
|
| 355 | if (ep->type == EXFAT_STREAM)
|
| 356 | return TYPE_STREAM;
|
| 357 | if (ep->type == EXFAT_NAME)
|
| 358 | return TYPE_EXTEND;
|
| 359 | if (ep->type == EXFAT_ACL)
|
| 360 | return TYPE_ACL;
|
| 361 | return TYPE_CRITICAL_SEC;
|
| 362 | }
|
| 363 |
|
| 364 | if (ep->type == EXFAT_VENDOR_EXT)
|
| 365 | return TYPE_VENDOR_EXT;
|
| 366 | if (ep->type == EXFAT_VENDOR_ALLOC)
|
| 367 | return TYPE_VENDOR_ALLOC;
|
| 368 |
|
| 369 | return TYPE_BENIGN_SEC;
|
| 370 | }
|
| 371 |
|
| 372 | static void exfat_set_entry_type(struct exfat_dentry *ep, unsigned int type)
|
| 373 | {
|
| 374 | if (type == TYPE_UNUSED) {
|
| 375 | ep->type = EXFAT_UNUSED;
|
| 376 | } else if (type == TYPE_DELETED) {
|
| 377 | ep->type &= EXFAT_DELETE;
|
| 378 | } else if (type == TYPE_STREAM) {
|
| 379 | ep->type = EXFAT_STREAM;
|
| 380 | } else if (type == TYPE_EXTEND) {
|
| 381 | ep->type = EXFAT_NAME;
|
| 382 | } else if (type == TYPE_BITMAP) {
|
| 383 | ep->type = EXFAT_BITMAP;
|
| 384 | } else if (type == TYPE_UPCASE) {
|
| 385 | ep->type = EXFAT_UPCASE;
|
| 386 | } else if (type == TYPE_VOLUME) {
|
| 387 | ep->type = EXFAT_VOLUME;
|
| 388 | } else if (type == TYPE_DIR) {
|
| 389 | ep->type = EXFAT_FILE;
|
| 390 | ep->dentry.file.attr = cpu_to_le16(EXFAT_ATTR_SUBDIR);
|
| 391 | } else if (type == TYPE_FILE) {
|
| 392 | ep->type = EXFAT_FILE;
|
| 393 | ep->dentry.file.attr = cpu_to_le16(EXFAT_ATTR_ARCHIVE);
|
| 394 | }
|
| 395 | }
|
| 396 |
|
| 397 | static void exfat_init_stream_entry(struct exfat_dentry *ep,
|
| 398 | unsigned int start_clu, unsigned long long size)
|
| 399 | {
|
| 400 | memset(ep, 0, sizeof(*ep));
|
| 401 | exfat_set_entry_type(ep, TYPE_STREAM);
|
| 402 | if (size == 0)
|
| 403 | ep->dentry.stream.flags = ALLOC_FAT_CHAIN;
|
| 404 | else
|
| 405 | ep->dentry.stream.flags = ALLOC_NO_FAT_CHAIN;
|
| 406 | ep->dentry.stream.start_clu = cpu_to_le32(start_clu);
|
| 407 | ep->dentry.stream.valid_size = cpu_to_le64(size);
|
| 408 | ep->dentry.stream.size = cpu_to_le64(size);
|
| 409 | }
|
| 410 |
|
| 411 | static void exfat_init_name_entry(struct exfat_dentry *ep,
|
| 412 | unsigned short *uniname)
|
| 413 | {
|
| 414 | int i;
|
| 415 |
|
| 416 | exfat_set_entry_type(ep, TYPE_EXTEND);
|
| 417 | ep->dentry.name.flags = 0x0;
|
| 418 |
|
| 419 | for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
|
| 420 | if (*uniname != 0x0) {
|
| 421 | ep->dentry.name.unicode_0_14[i] = cpu_to_le16(*uniname);
|
| 422 | uniname++;
|
| 423 | } else {
|
| 424 | ep->dentry.name.unicode_0_14[i] = 0x0;
|
| 425 | }
|
| 426 | }
|
| 427 | }
|
| 428 |
|
| 429 | void exfat_init_dir_entry(struct exfat_entry_set_cache *es,
|
| 430 | unsigned int type, unsigned int start_clu,
|
| 431 | unsigned long long size, struct timespec64 *ts)
|
| 432 | {
|
| 433 | struct super_block *sb = es->sb;
|
| 434 | struct exfat_sb_info *sbi = EXFAT_SB(sb);
|
| 435 | struct exfat_dentry *ep;
|
| 436 |
|
| 437 | ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
|
| 438 | memset(ep, 0, sizeof(*ep));
|
| 439 | exfat_set_entry_type(ep, type);
|
| 440 | exfat_set_entry_time(sbi, ts,
|
| 441 | &ep->dentry.file.create_tz,
|
| 442 | &ep->dentry.file.create_time,
|
| 443 | &ep->dentry.file.create_date,
|
| 444 | &ep->dentry.file.create_time_cs);
|
| 445 | exfat_set_entry_time(sbi, ts,
|
| 446 | &ep->dentry.file.modify_tz,
|
| 447 | &ep->dentry.file.modify_time,
|
| 448 | &ep->dentry.file.modify_date,
|
| 449 | &ep->dentry.file.modify_time_cs);
|
| 450 | exfat_set_entry_time(sbi, ts,
|
| 451 | &ep->dentry.file.access_tz,
|
| 452 | &ep->dentry.file.access_time,
|
| 453 | &ep->dentry.file.access_date,
|
| 454 | NULL);
|
| 455 |
|
| 456 | ep = exfat_get_dentry_cached(es, ES_IDX_STREAM);
|
| 457 | exfat_init_stream_entry(ep, start_clu, size);
|
| 458 | }
|
| 459 |
|
| 460 | static void exfat_free_benign_secondary_clusters(struct inode *inode,
|
| 461 | struct exfat_dentry *ep)
|
| 462 | {
|
| 463 | struct super_block *sb = inode->i_sb;
|
| 464 | struct exfat_chain dir;
|
| 465 | unsigned int start_clu =
|
| 466 | le32_to_cpu(ep->dentry.generic_secondary.start_clu);
|
| 467 | u64 size = le64_to_cpu(ep->dentry.generic_secondary.size);
|
| 468 | unsigned char flags = ep->dentry.generic_secondary.flags;
|
| 469 |
|
| 470 | if (!(flags & ALLOC_POSSIBLE) || !start_clu || !size)
|
| 471 | return;
|
| 472 |
|
| 473 | exfat_chain_set(&dir, start_clu,
|
| 474 | exfat_bytes_to_cluster_round_up(EXFAT_SB(sb), size),
|
| 475 | flags);
|
| 476 | exfat_free_cluster(inode, &dir);
|
| 477 | }
|
| 478 |
|
| 479 | |
| 480 | * exfat_init_ext_entry - initialize extension entries in a directory entry set
|
| 481 | * @es: target entry set
|
| 482 | * @num_entries: number of entries excluding benign secondary entries
|
| 483 | * @p_uniname: filename to store
|
| 484 | * @old_es: optional source entry set with benign secondary entries, or NULL
|
| 485 | * @num_extra: number of benign secondary entries to copy from @old_es
|
| 486 | *
|
| 487 | * Set up the file, stream extension, and filename entries in @es, optionally
|
| 488 | * preserving @num_extra benign secondary entries from @old_es. @es and @old_es
|
| 489 | * may refer to the same entry set; excess entries are marked as deleted.
|
| 490 |
|
| 491 | void exfat_init_ext_entry(struct exfat_entry_set_cache *es, int num_entries,
|
| 492 | struct exfat_uni_name *p_uniname,
|
| 493 | struct exfat_entry_set_cache *old_es, int num_extra)
|
| 494 | {
|
| 495 | int i, src_start = 0, old_num;
|
| 496 | unsigned short *uniname = p_uniname->name;
|
| 497 | struct exfat_dentry *ep;
|
| 498 |
|
| 499 | if (WARN_ON(num_extra < 0 || (num_extra && (!old_es ||
|
| 500 | old_es->num_entries < ES_IDX_FIRST_FILENAME + num_extra))))
|
| 501 | num_extra = 0;
|
| 502 |
|
| 503 | |
| 504 | * Save old entry count and source position before modifying
|
| 505 | * es->num_entries, since old_es and es may point to the same
|
| 506 | * entry set.
|
| 507 |
|
| 508 | old_num = es->num_entries;
|
| 509 | if (old_es && num_extra > 0)
|
| 510 | src_start = old_es->num_entries - num_extra;
|
| 511 |
|
| 512 | es->num_entries = num_entries + num_extra;
|
| 513 | ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
|
| 514 | ep->dentry.file.num_ext = (unsigned char)(num_entries - 1 + num_extra);
|
| 515 |
|
| 516 | ep = exfat_get_dentry_cached(es, ES_IDX_STREAM);
|
| 517 | ep->dentry.stream.name_len = p_uniname->name_len;
|
| 518 | ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash);
|
| 519 |
|
| 520 | if (old_es && num_extra > 0) {
|
| 521 | for (i = 0; i < num_extra; i++)
|
| 522 | *exfat_get_dentry_cached(es, num_entries + i) =
|
| 523 | *exfat_get_dentry_cached(old_es, src_start + i);
|
| 524 | }
|
| 525 |
|
| 526 | for (i = ES_IDX_FIRST_FILENAME; i < num_entries; i++) {
|
| 527 | ep = exfat_get_dentry_cached(es, i);
|
| 528 | exfat_init_name_entry(ep, uniname);
|
| 529 | uniname += EXFAT_FILE_NAME_LEN;
|
| 530 | }
|
| 531 |
|
| 532 |
|
| 533 | for (i = num_entries + num_extra; i < old_num; i++) {
|
| 534 | ep = exfat_get_dentry_cached(es, i);
|
| 535 | exfat_set_entry_type(ep, TYPE_DELETED);
|
| 536 | }
|
| 537 |
|
| 538 | exfat_update_dir_chksum(es);
|
| 539 | }
|
| 540 |
|
| 541 | void exfat_remove_entries(struct inode *inode, struct exfat_entry_set_cache *es,
|
| 542 | int order, bool free_benign)
|
| 543 | {
|
| 544 | int i;
|
| 545 | struct exfat_dentry *ep;
|
| 546 |
|
| 547 | for (i = order; i < es->num_entries; i++) {
|
| 548 | ep = exfat_get_dentry_cached(es, i);
|
| 549 |
|
| 550 | if (free_benign && (exfat_get_entry_type(ep) & TYPE_BENIGN_SEC))
|
| 551 | exfat_free_benign_secondary_clusters(inode, ep);
|
| 552 |
|
| 553 | exfat_set_entry_type(ep, TYPE_DELETED);
|
| 554 | }
|
| 555 |
|
| 556 | if (order < es->num_entries)
|
| 557 | es->modified = true;
|
| 558 | }
|
| 559 |
|
| 560 | void exfat_update_dir_chksum(struct exfat_entry_set_cache *es)
|
| 561 | {
|
| 562 | int chksum_type = CS_DIR_ENTRY, i;
|
| 563 | unsigned short chksum = 0;
|
| 564 | struct exfat_dentry *ep;
|
| 565 |
|
| 566 | for (i = ES_IDX_FILE; i < es->num_entries; i++) {
|
| 567 | ep = exfat_get_dentry_cached(es, i);
|
| 568 | chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
|
| 569 | chksum_type);
|
| 570 | chksum_type = CS_DEFAULT;
|
| 571 | }
|
| 572 | ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
|
| 573 | ep->dentry.file.checksum = cpu_to_le16(chksum);
|
| 574 | es->modified = true;
|
| 575 | }
|
| 576 |
|
| 577 | int exfat_put_dentry_set(struct exfat_entry_set_cache *es, int sync)
|
| 578 | {
|
| 579 | int i, err = 0;
|
| 580 |
|
| 581 | if (es->modified)
|
| 582 | err = exfat_update_bhs(es->bh, es->num_bh, sync);
|
| 583 |
|
| 584 | for (i = 0; i < es->num_bh; i++)
|
| 585 | if (err)
|
| 586 | bforget(es->bh[i]);
|
| 587 | else
|
| 588 | brelse(es->bh[i]);
|
| 589 |
|
| 590 | if (IS_DYNAMIC_ES(es))
|
| 591 | kfree(es->bh);
|
| 592 |
|
| 593 | return err;
|
| 594 | }
|
| 595 |
|
| 596 | static int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir,
|
| 597 | int entry, sector_t *sector, int *offset)
|
| 598 | {
|
| 599 | int ret;
|
| 600 | unsigned int off, clu = 0;
|
| 601 | struct exfat_sb_info *sbi = EXFAT_SB(sb);
|
| 602 |
|
| 603 | off = exfat_dentries_to_bytes(entry);
|
| 604 |
|
| 605 | clu = p_dir->dir;
|
| 606 | ret = exfat_cluster_walk(sb, &clu, exfat_bytes_to_cluster(sbi, off),
|
| 607 | p_dir->flags);
|
| 608 | if (ret)
|
| 609 | return ret;
|
| 610 |
|
| 611 | if (clu == EXFAT_EOF_CLUSTER) {
|
| 612 | exfat_fs_error(sb,
|
| 613 | "unexpected early break in cluster chain (clu : %u, len : %d)",
|
| 614 | p_dir->dir,
|
| 615 | exfat_bytes_to_cluster(sbi, off));
|
| 616 | return -EIO;
|
| 617 | }
|
| 618 |
|
| 619 | if (!exfat_test_bitmap(sb, clu)) {
|
| 620 | exfat_err(sb, "failed to test cluster bit(%u)", clu);
|
| 621 | return -EIO;
|
| 622 | }
|
| 623 |
|
| 624 |
|
| 625 | off = exfat_cluster_offset(sbi, off);
|
| 626 |
|
| 627 |
|
| 628 | *offset = exfat_block_offset(sb, off);
|
| 629 |
|
| 630 |
|
| 631 | *sector = exfat_bytes_to_block(sb, off);
|
| 632 | *sector += exfat_cluster_to_sector(sbi, clu);
|
| 633 | return 0;
|
| 634 | }
|
| 635 |
|
| 636 | struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
|
| 637 | struct exfat_chain *p_dir, int entry, struct buffer_head **bh)
|
| 638 | {
|
| 639 | struct exfat_sb_info *sbi = EXFAT_SB(sb);
|
| 640 | unsigned int sect_per_clus = sbi->sect_per_clus;
|
| 641 | unsigned int dentries_per_page = exfat_bytes_to_dentries(PAGE_SIZE);
|
| 642 | int off;
|
| 643 | sector_t sec;
|
| 644 |
|
| 645 | if (p_dir->dir == DIR_DELETED) {
|
| 646 | exfat_err(sb, "abnormal access to deleted dentry");
|
| 647 | return NULL;
|
| 648 | }
|
| 649 |
|
| 650 | if (exfat_find_location(sb, p_dir, entry, &sec, &off))
|
| 651 | return NULL;
|
| 652 |
|
| 653 | if (sect_per_clus > 1 &&
|
| 654 | (entry & (dentries_per_page - 1)) == 0) {
|
| 655 | sector_t ra = sec;
|
| 656 | blkcnt_t cnt = 0;
|
| 657 | unsigned int ra_count = sect_per_clus;
|
| 658 |
|
| 659 |
|
| 660 | if ((sec - sbi->data_start_sector) & (ra_count - 1))
|
| 661 | ra_count = PAGE_SIZE >> sb->s_blocksize_bits;
|
| 662 |
|
| 663 | exfat_blk_readahead(sb, sec, &ra, &cnt, sec + ra_count - 1);
|
| 664 | }
|
| 665 |
|
| 666 | *bh = sb_bread(sb, sec);
|
| 667 | if (!*bh)
|
| 668 | return NULL;
|
| 669 |
|
| 670 | return (struct exfat_dentry *)((*bh)->b_data + off);
|
| 671 | }
|
| 672 |
|
| 673 | enum exfat_validate_dentry_mode {
|
| 674 | ES_MODE_GET_FILE_ENTRY,
|
| 675 | ES_MODE_GET_STRM_ENTRY,
|
| 676 | ES_MODE_GET_NAME_ENTRY,
|
| 677 | ES_MODE_GET_CRITICAL_SEC_ENTRY,
|
| 678 | ES_MODE_GET_BENIGN_SEC_ENTRY,
|
| 679 | };
|
| 680 |
|
| 681 | static bool exfat_validate_entry(unsigned int type,
|
| 682 | enum exfat_validate_dentry_mode *mode)
|
| 683 | {
|
| 684 | if (type == TYPE_UNUSED || type == TYPE_DELETED)
|
| 685 | return false;
|
| 686 |
|
| 687 | switch (*mode) {
|
| 688 | case ES_MODE_GET_FILE_ENTRY:
|
| 689 | if (type != TYPE_STREAM)
|
| 690 | return false;
|
| 691 | *mode = ES_MODE_GET_STRM_ENTRY;
|
| 692 | break;
|
| 693 | case ES_MODE_GET_STRM_ENTRY:
|
| 694 | if (type != TYPE_EXTEND)
|
| 695 | return false;
|
| 696 | *mode = ES_MODE_GET_NAME_ENTRY;
|
| 697 | break;
|
| 698 | case ES_MODE_GET_NAME_ENTRY:
|
| 699 | if (type & TYPE_BENIGN_SEC)
|
| 700 | *mode = ES_MODE_GET_BENIGN_SEC_ENTRY;
|
| 701 | else if (type != TYPE_EXTEND)
|
| 702 | return false;
|
| 703 | break;
|
| 704 | case ES_MODE_GET_BENIGN_SEC_ENTRY:
|
| 705 |
|
| 706 | if (!(type & TYPE_BENIGN_SEC))
|
| 707 | return false;
|
| 708 | break;
|
| 709 | default:
|
| 710 | return false;
|
| 711 | }
|
| 712 |
|
| 713 | return true;
|
| 714 | }
|
| 715 |
|
| 716 | struct exfat_dentry *exfat_get_dentry_cached(
|
| 717 | struct exfat_entry_set_cache *es, int num)
|
| 718 | {
|
| 719 | int off = es->start_off + num * DENTRY_SIZE;
|
| 720 | struct buffer_head *bh = es->bh[exfat_bytes_to_block(es->sb, off)];
|
| 721 | char *p = bh->b_data + exfat_block_offset(es->sb, off);
|
| 722 |
|
| 723 | return (struct exfat_dentry *)p;
|
| 724 | }
|
| 725 |
|
| 726 | |
| 727 | * Returns a set of dentries.
|
| 728 | *
|
| 729 | * Note It provides a direct pointer to bh->data via exfat_get_dentry_cached().
|
| 730 | * User should call exfat_get_dentry_set() after setting 'modified' to apply
|
| 731 | * changes made in this entry set to the real device.
|
| 732 | *
|
| 733 | * in:
|
| 734 | * sb+p_dir+entry: indicates a file/dir
|
| 735 | * num_entries: specifies how many dentries should be included.
|
| 736 | * It will be set to es->num_entries if it is not 0.
|
| 737 | * If num_entries is 0, es->num_entries will be obtained
|
| 738 | * from the first dentry.
|
| 739 | * out:
|
| 740 | * es: pointer of entry set on success.
|
| 741 | * return:
|
| 742 | * 0 on success
|
| 743 | * -error code on failure
|
| 744 |
|
| 745 | static int __exfat_get_dentry_set(struct exfat_entry_set_cache *es,
|
| 746 | struct super_block *sb, struct exfat_chain *p_dir, int entry,
|
| 747 | unsigned int num_entries)
|
| 748 | {
|
| 749 | int ret, i, num_bh;
|
| 750 | unsigned int off;
|
| 751 | sector_t sec;
|
| 752 | struct exfat_sb_info *sbi = EXFAT_SB(sb);
|
| 753 | struct buffer_head *bh;
|
| 754 |
|
| 755 | if (p_dir->dir == DIR_DELETED) {
|
| 756 | exfat_err(sb, "access to deleted dentry");
|
| 757 | return -EIO;
|
| 758 | }
|
| 759 |
|
| 760 | ret = exfat_find_location(sb, p_dir, entry, &sec, &off);
|
| 761 | if (ret)
|
| 762 | return ret;
|
| 763 |
|
| 764 | memset(es, 0, sizeof(*es));
|
| 765 | es->sb = sb;
|
| 766 | es->modified = false;
|
| 767 | es->start_off = off;
|
| 768 | es->bh = es->__bh;
|
| 769 |
|
| 770 | bh = sb_bread(sb, sec);
|
| 771 | if (!bh)
|
| 772 | return -EIO;
|
| 773 | es->bh[es->num_bh++] = bh;
|
| 774 |
|
| 775 | if (num_entries == ES_ALL_ENTRIES) {
|
| 776 | struct exfat_dentry *ep;
|
| 777 |
|
| 778 | ep = exfat_get_dentry_cached(es, ES_IDX_FILE);
|
| 779 | if (ep->type != EXFAT_FILE) {
|
| 780 | brelse(bh);
|
| 781 | return -EIO;
|
| 782 | }
|
| 783 |
|
| 784 | num_entries = ep->dentry.file.num_ext + 1;
|
| 785 | }
|
| 786 |
|
| 787 | es->num_entries = num_entries;
|
| 788 |
|
| 789 | num_bh = exfat_bytes_to_block_round_up(sb, off + num_entries * DENTRY_SIZE);
|
| 790 | if (num_bh > ARRAY_SIZE(es->__bh)) {
|
| 791 | es->bh = kmalloc_objs(*es->bh, num_bh, GFP_NOFS);
|
| 792 | if (!es->bh) {
|
| 793 | brelse(bh);
|
| 794 | return -ENOMEM;
|
| 795 | }
|
| 796 | es->bh[0] = bh;
|
| 797 | }
|
| 798 |
|
| 799 | for (i = 1; i < num_bh; i++) {
|
| 800 |
|
| 801 | if (exfat_is_last_sector_in_cluster(sbi, sec)) {
|
| 802 | unsigned int clu = exfat_sector_to_cluster(sbi, sec);
|
| 803 |
|
| 804 | if (exfat_cluster_walk(sb, &clu, 1, p_dir->flags))
|
| 805 | goto put_es;
|
| 806 | sec = exfat_cluster_to_sector(sbi, clu);
|
| 807 | } else {
|
| 808 | sec++;
|
| 809 | }
|
| 810 |
|
| 811 | bh = sb_bread(sb, sec);
|
| 812 | if (!bh)
|
| 813 | goto put_es;
|
| 814 | es->bh[es->num_bh++] = bh;
|
| 815 | }
|
| 816 |
|
| 817 | return 0;
|
| 818 |
|
| 819 | put_es:
|
| 820 | exfat_put_dentry_set(es, false);
|
| 821 | return -EIO;
|
| 822 | }
|
| 823 |
|
| 824 | int exfat_get_dentry_set(struct exfat_entry_set_cache *es,
|
| 825 | struct super_block *sb, struct exfat_chain *p_dir,
|
| 826 | int entry, unsigned int num_entries)
|
| 827 | {
|
| 828 | int ret, i;
|
| 829 | struct exfat_dentry *ep;
|
| 830 | enum exfat_validate_dentry_mode mode = ES_MODE_GET_FILE_ENTRY;
|
| 831 |
|
| 832 | ret = __exfat_get_dentry_set(es, sb, p_dir, entry, num_entries);
|
| 833 | if (ret < 0)
|
| 834 | return ret;
|
| 835 |
|
| 836 |
|
| 837 | for (i = ES_IDX_STREAM; i < es->num_entries; i++) {
|
| 838 | ep = exfat_get_dentry_cached(es, i);
|
| 839 | if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
|
| 840 | goto put_es;
|
| 841 | }
|
| 842 | return 0;
|
| 843 |
|
| 844 | put_es:
|
| 845 | exfat_put_dentry_set(es, false);
|
| 846 | return -EIO;
|
| 847 | }
|
| 848 |
|
| 849 | static int exfat_validate_empty_dentry_set(struct exfat_entry_set_cache *es)
|
| 850 | {
|
| 851 | struct exfat_dentry *ep;
|
| 852 | struct buffer_head *bh;
|
| 853 | int i, off;
|
| 854 | bool unused_hit = false;
|
| 855 |
|
| 856 | |
| 857 | * ONLY UNUSED OR DELETED DENTRIES ARE ALLOWED:
|
| 858 | * Although it violates the specification for a deleted entry to
|
| 859 | * follow an unused entry, some exFAT implementations could work
|
| 860 | * like this. Therefore, to improve compatibility, let's allow it.
|
| 861 |
|
| 862 | for (i = 0; i < es->num_entries; i++) {
|
| 863 | ep = exfat_get_dentry_cached(es, i);
|
| 864 | if (ep->type == EXFAT_UNUSED) {
|
| 865 | unused_hit = true;
|
| 866 | } else if (!IS_EXFAT_DELETED(ep->type)) {
|
| 867 | if (unused_hit)
|
| 868 | goto err_used_follow_unused;
|
| 869 | i++;
|
| 870 | goto count_skip_entries;
|
| 871 | }
|
| 872 | }
|
| 873 |
|
| 874 | return 0;
|
| 875 |
|
| 876 | err_used_follow_unused:
|
| 877 | off = es->start_off + (i << DENTRY_SIZE_BITS);
|
| 878 | bh = es->bh[exfat_bytes_to_block(es->sb, off)];
|
| 879 |
|
| 880 | exfat_fs_error(es->sb,
|
| 881 | "in sector %lld, dentry %d should be unused, but 0x%x",
|
| 882 | bh->b_blocknr, off >> DENTRY_SIZE_BITS, ep->type);
|
| 883 |
|
| 884 | return -EIO;
|
| 885 |
|
| 886 | count_skip_entries:
|
| 887 | es->num_entries =
|
| 888 | exfat_bytes_to_dentries(exfat_block_to_bytes(es->sb, es->num_bh) - es->start_off);
|
| 889 | for (; i < es->num_entries; i++) {
|
| 890 | ep = exfat_get_dentry_cached(es, i);
|
| 891 | if (IS_EXFAT_DELETED(ep->type))
|
| 892 | break;
|
| 893 | }
|
| 894 |
|
| 895 | return i;
|
| 896 | }
|
| 897 |
|
| 898 | |
| 899 | * Get an empty dentry set.
|
| 900 | *
|
| 901 | * in:
|
| 902 | * sb+p_dir+entry: indicates the empty dentry location
|
| 903 | * num_entries: specifies how many empty dentries should be included.
|
| 904 | * out:
|
| 905 | * es: pointer of empty dentry set on success.
|
| 906 | * return:
|
| 907 | * 0 : on success
|
| 908 | * >0 : the dentries are not empty, the return value is the number of
|
| 909 | * dentries to be skipped for the next lookup.
|
| 910 | * <0 : on failure
|
| 911 |
|
| 912 | int exfat_get_empty_dentry_set(struct exfat_entry_set_cache *es,
|
| 913 | struct super_block *sb, struct exfat_chain *p_dir,
|
| 914 | int entry, unsigned int num_entries)
|
| 915 | {
|
| 916 | int ret;
|
| 917 |
|
| 918 | ret = __exfat_get_dentry_set(es, sb, p_dir, entry, num_entries);
|
| 919 | if (ret < 0)
|
| 920 | return ret;
|
| 921 |
|
| 922 | ret = exfat_validate_empty_dentry_set(es);
|
| 923 | if (ret)
|
| 924 | exfat_put_dentry_set(es, false);
|
| 925 |
|
| 926 | return ret;
|
| 927 | }
|
| 928 |
|
| 929 | static inline void exfat_reset_empty_hint(struct exfat_hint_femp *hint_femp)
|
| 930 | {
|
| 931 | hint_femp->eidx = EXFAT_HINT_NONE;
|
| 932 | hint_femp->count = 0;
|
| 933 | }
|
| 934 |
|
| 935 | static inline void exfat_set_empty_hint(struct exfat_inode_info *ei,
|
| 936 | struct exfat_hint_femp *candi_empty, struct exfat_chain *clu,
|
| 937 | int dentry, int num_entries, int entry_type)
|
| 938 | {
|
| 939 | if (ei->hint_femp.eidx == EXFAT_HINT_NONE ||
|
| 940 | ei->hint_femp.eidx > dentry) {
|
| 941 | int total_entries = exfat_bytes_to_dentries(i_size_read(&ei->vfs_inode));
|
| 942 |
|
| 943 | if (candi_empty->count == 0) {
|
| 944 | candi_empty->cur = *clu;
|
| 945 | candi_empty->eidx = dentry;
|
| 946 | }
|
| 947 |
|
| 948 | if (entry_type == TYPE_UNUSED)
|
| 949 | candi_empty->count += total_entries - dentry;
|
| 950 | else
|
| 951 | candi_empty->count++;
|
| 952 |
|
| 953 | if (candi_empty->count == num_entries ||
|
| 954 | candi_empty->count + candi_empty->eidx == total_entries)
|
| 955 | ei->hint_femp = *candi_empty;
|
| 956 | }
|
| 957 | }
|
| 958 |
|
| 959 | enum {
|
| 960 | DIRENT_STEP_FILE,
|
| 961 | DIRENT_STEP_STRM,
|
| 962 | DIRENT_STEP_NAME,
|
| 963 | DIRENT_STEP_SECD,
|
| 964 | };
|
| 965 |
|
| 966 | |
| 967 | * @ei: inode info of parent directory
|
| 968 | * @p_dir: directory structure of parent directory
|
| 969 | * @num_entries:entry size of p_uniname
|
| 970 | * @hint_opt: If p_uniname is found, filled with optimized dir/entry
|
| 971 | * for traversing cluster chain.
|
| 972 | * @return:
|
| 973 | * >= 0: file directory entry position where the name exists
|
| 974 | * -ENOENT: entry with the name does not exist
|
| 975 | * -EIO: I/O error
|
| 976 |
|
| 977 | int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
|
| 978 | struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
|
| 979 | struct exfat_hint *hint_opt)
|
| 980 | {
|
| 981 | int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len;
|
| 982 | int order, step, name_len = 0;
|
| 983 | int dentries_per_clu;
|
| 984 | unsigned int entry_type;
|
| 985 | unsigned short *uniname = NULL;
|
| 986 | struct exfat_chain clu;
|
| 987 | struct exfat_hint *hint_stat = &ei->hint_stat;
|
| 988 | struct exfat_hint_femp candi_empty;
|
| 989 | struct exfat_sb_info *sbi = EXFAT_SB(sb);
|
| 990 | int num_entries = exfat_calc_num_entries(p_uniname);
|
| 991 | unsigned int clu_count = 0;
|
| 992 |
|
| 993 | if (num_entries < 0)
|
| 994 | return num_entries;
|
| 995 |
|
| 996 | dentries_per_clu = sbi->dentries_per_clu;
|
| 997 |
|
| 998 | exfat_chain_dup(&clu, p_dir);
|
| 999 |
|
| 1000 | if (hint_stat->eidx) {
|
| 1001 | clu.dir = hint_stat->clu;
|
| 1002 | dentry = hint_stat->eidx;
|
| 1003 | end_eidx = dentry;
|
| 1004 | }
|
| 1005 |
|
| 1006 | exfat_reset_empty_hint(&ei->hint_femp);
|
| 1007 |
|
| 1008 | rewind:
|
| 1009 | order = 0;
|
| 1010 | step = DIRENT_STEP_FILE;
|
| 1011 | exfat_reset_empty_hint(&candi_empty);
|
| 1012 |
|
| 1013 | while (clu.dir != EXFAT_EOF_CLUSTER) {
|
| 1014 | i = dentry & (dentries_per_clu - 1);
|
| 1015 | for (; i < dentries_per_clu; i++, dentry++) {
|
| 1016 | struct exfat_dentry *ep;
|
| 1017 | struct buffer_head *bh;
|
| 1018 |
|
| 1019 | if (rewind && dentry == end_eidx)
|
| 1020 | goto not_found;
|
| 1021 |
|
| 1022 | ep = exfat_get_dentry(sb, &clu, i, &bh);
|
| 1023 | if (!ep)
|
| 1024 | return -EIO;
|
| 1025 |
|
| 1026 | entry_type = exfat_get_entry_type(ep);
|
| 1027 |
|
| 1028 | if (entry_type == TYPE_UNUSED ||
|
| 1029 | entry_type == TYPE_DELETED) {
|
| 1030 | step = DIRENT_STEP_FILE;
|
| 1031 |
|
| 1032 | exfat_set_empty_hint(ei, &candi_empty, &clu,
|
| 1033 | dentry, num_entries,
|
| 1034 | entry_type);
|
| 1035 |
|
| 1036 | brelse(bh);
|
| 1037 | if (entry_type == TYPE_UNUSED)
|
| 1038 | goto not_found;
|
| 1039 | continue;
|
| 1040 | }
|
| 1041 |
|
| 1042 | exfat_reset_empty_hint(&candi_empty);
|
| 1043 |
|
| 1044 | if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) {
|
| 1045 | step = DIRENT_STEP_FILE;
|
| 1046 | hint_opt->clu = clu.dir;
|
| 1047 | hint_opt->eidx = i;
|
| 1048 | num_ext = ep->dentry.file.num_ext;
|
| 1049 | step = DIRENT_STEP_STRM;
|
| 1050 | brelse(bh);
|
| 1051 | continue;
|
| 1052 | }
|
| 1053 |
|
| 1054 | if (entry_type == TYPE_STREAM) {
|
| 1055 | u16 name_hash;
|
| 1056 |
|
| 1057 | if (step != DIRENT_STEP_STRM) {
|
| 1058 | step = DIRENT_STEP_FILE;
|
| 1059 | brelse(bh);
|
| 1060 | continue;
|
| 1061 | }
|
| 1062 | step = DIRENT_STEP_FILE;
|
| 1063 | name_hash = le16_to_cpu(
|
| 1064 | ep->dentry.stream.name_hash);
|
| 1065 | if (p_uniname->name_hash == name_hash &&
|
| 1066 | p_uniname->name_len ==
|
| 1067 | ep->dentry.stream.name_len) {
|
| 1068 | step = DIRENT_STEP_NAME;
|
| 1069 | order = 1;
|
| 1070 | name_len = 0;
|
| 1071 | }
|
| 1072 | brelse(bh);
|
| 1073 | continue;
|
| 1074 | }
|
| 1075 |
|
| 1076 | if (entry_type == TYPE_EXTEND) {
|
| 1077 | unsigned short entry_uniname[16], unichar;
|
| 1078 | unsigned int offset;
|
| 1079 |
|
| 1080 | if (step != DIRENT_STEP_NAME ||
|
| 1081 | name_len >= MAX_NAME_LENGTH) {
|
| 1082 | brelse(bh);
|
| 1083 | step = DIRENT_STEP_FILE;
|
| 1084 | continue;
|
| 1085 | }
|
| 1086 |
|
| 1087 | offset = (++order - 2) * EXFAT_FILE_NAME_LEN;
|
| 1088 | len = exfat_extract_uni_name(ep, entry_uniname);
|
| 1089 | brelse(bh);
|
| 1090 | if (offset > MAX_NAME_LENGTH ||
|
| 1091 | len > MAX_NAME_LENGTH - offset) {
|
| 1092 | step = DIRENT_STEP_FILE;
|
| 1093 | continue;
|
| 1094 | }
|
| 1095 | uniname = p_uniname->name + offset;
|
| 1096 | name_len += len;
|
| 1097 |
|
| 1098 | unichar = *(uniname+len);
|
| 1099 | *(uniname+len) = 0x0;
|
| 1100 |
|
| 1101 | if (exfat_uniname_ncmp(sb, uniname,
|
| 1102 | entry_uniname, len)) {
|
| 1103 | step = DIRENT_STEP_FILE;
|
| 1104 | } else if (p_uniname->name_len == name_len) {
|
| 1105 | if (order == num_ext)
|
| 1106 | goto found;
|
| 1107 | step = DIRENT_STEP_SECD;
|
| 1108 | }
|
| 1109 |
|
| 1110 | *(uniname+len) = unichar;
|
| 1111 | continue;
|
| 1112 | }
|
| 1113 |
|
| 1114 | brelse(bh);
|
| 1115 | if (entry_type &
|
| 1116 | (TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
|
| 1117 | if (step == DIRENT_STEP_SECD) {
|
| 1118 | if (++order == num_ext)
|
| 1119 | goto found;
|
| 1120 | continue;
|
| 1121 | }
|
| 1122 | }
|
| 1123 | step = DIRENT_STEP_FILE;
|
| 1124 | }
|
| 1125 |
|
| 1126 | if (exfat_chain_advance(sb, &clu, 1))
|
| 1127 | return -EIO;
|
| 1128 |
|
| 1129 |
|
| 1130 | if (unlikely(++clu_count > EXFAT_DATA_CLUSTER_COUNT(sbi)))
|
| 1131 | goto not_found;
|
| 1132 | }
|
| 1133 |
|
| 1134 | not_found:
|
| 1135 | |
| 1136 | * We started at not 0 index,so we should try to find target
|
| 1137 | * from 0 index to the index we started at.
|
| 1138 |
|
| 1139 | if (!rewind && end_eidx) {
|
| 1140 | rewind = 1;
|
| 1141 | dentry = 0;
|
| 1142 | clu.dir = p_dir->dir;
|
| 1143 | goto rewind;
|
| 1144 | }
|
| 1145 |
|
| 1146 | |
| 1147 | * set the EXFAT_EOF_CLUSTER flag to avoid search
|
| 1148 | * from the beginning again when allocated a new cluster
|
| 1149 |
|
| 1150 | if (ei->hint_femp.eidx == EXFAT_HINT_NONE) {
|
| 1151 | ei->hint_femp.cur.dir = EXFAT_EOF_CLUSTER;
|
| 1152 | ei->hint_femp.eidx = p_dir->size * dentries_per_clu;
|
| 1153 | ei->hint_femp.count = 0;
|
| 1154 | }
|
| 1155 |
|
| 1156 |
|
| 1157 | hint_stat->clu = p_dir->dir;
|
| 1158 | hint_stat->eidx = 0;
|
| 1159 | return -ENOENT;
|
| 1160 |
|
| 1161 | found:
|
| 1162 |
|
| 1163 | if (!((dentry + 1) & (dentries_per_clu - 1))) {
|
| 1164 | int ret = 0;
|
| 1165 |
|
| 1166 | ret = exfat_chain_advance(sb, &clu, 1);
|
| 1167 |
|
| 1168 | if (ret || clu.dir == EXFAT_EOF_CLUSTER) {
|
| 1169 |
|
| 1170 | hint_stat->clu = p_dir->dir;
|
| 1171 | hint_stat->eidx = 0;
|
| 1172 | return (dentry - num_ext);
|
| 1173 | }
|
| 1174 | }
|
| 1175 |
|
| 1176 | hint_stat->clu = clu.dir;
|
| 1177 | hint_stat->eidx = dentry + 1;
|
| 1178 | return dentry - num_ext;
|
| 1179 | }
|
| 1180 |
|
| 1181 | int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
|
| 1182 | {
|
| 1183 | int i, count = 0;
|
| 1184 | int dentries_per_clu;
|
| 1185 | unsigned int entry_type;
|
| 1186 | unsigned int clu_count = 0;
|
| 1187 | struct exfat_chain clu;
|
| 1188 | struct exfat_dentry *ep;
|
| 1189 | struct exfat_sb_info *sbi = EXFAT_SB(sb);
|
| 1190 | struct buffer_head *bh;
|
| 1191 |
|
| 1192 | dentries_per_clu = sbi->dentries_per_clu;
|
| 1193 |
|
| 1194 | exfat_chain_dup(&clu, p_dir);
|
| 1195 |
|
| 1196 | while (clu.dir != EXFAT_EOF_CLUSTER) {
|
| 1197 | for (i = 0; i < dentries_per_clu; i++) {
|
| 1198 | ep = exfat_get_dentry(sb, &clu, i, &bh);
|
| 1199 | if (!ep)
|
| 1200 | return -EIO;
|
| 1201 | entry_type = exfat_get_entry_type(ep);
|
| 1202 | brelse(bh);
|
| 1203 |
|
| 1204 | if (entry_type == TYPE_UNUSED)
|
| 1205 | return count;
|
| 1206 | if (entry_type != TYPE_DIR)
|
| 1207 | continue;
|
| 1208 | count++;
|
| 1209 | }
|
| 1210 |
|
| 1211 | if (exfat_chain_advance(sb, &clu, 1))
|
| 1212 | return -EIO;
|
| 1213 |
|
| 1214 | if (unlikely(++clu_count > sbi->used_clusters)) {
|
| 1215 | exfat_fs_error(sb, "FAT or bitmap is corrupted");
|
| 1216 | return -EIO;
|
| 1217 | }
|
| 1218 | }
|
| 1219 |
|
| 1220 | return count;
|
| 1221 | }
|
| 1222 |
|
| 1223 | static int exfat_get_volume_label_dentry(struct super_block *sb,
|
| 1224 | struct exfat_entry_set_cache *es)
|
| 1225 | {
|
| 1226 | int i;
|
| 1227 | int dentry = 0;
|
| 1228 | unsigned int type;
|
| 1229 | struct exfat_sb_info *sbi = EXFAT_SB(sb);
|
| 1230 | struct exfat_hint_femp hint_femp;
|
| 1231 | struct exfat_inode_info *ei = EXFAT_I(sb->s_root->d_inode);
|
| 1232 | struct exfat_chain clu;
|
| 1233 | struct exfat_dentry *ep;
|
| 1234 | struct buffer_head *bh;
|
| 1235 |
|
| 1236 | hint_femp.eidx = EXFAT_HINT_NONE;
|
| 1237 | exfat_chain_set(&clu, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
|
| 1238 |
|
| 1239 | while (clu.dir != EXFAT_EOF_CLUSTER) {
|
| 1240 | for (i = 0; i < sbi->dentries_per_clu; i++, dentry++) {
|
| 1241 | ep = exfat_get_dentry(sb, &clu, i, &bh);
|
| 1242 | if (!ep)
|
| 1243 | return -EIO;
|
| 1244 |
|
| 1245 | type = exfat_get_entry_type(ep);
|
| 1246 | if (hint_femp.eidx == EXFAT_HINT_NONE) {
|
| 1247 | if (type == TYPE_DELETED || type == TYPE_UNUSED) {
|
| 1248 | hint_femp.cur = clu;
|
| 1249 | hint_femp.eidx = dentry;
|
| 1250 | hint_femp.count = 1;
|
| 1251 | }
|
| 1252 | }
|
| 1253 |
|
| 1254 | if (type == TYPE_UNUSED) {
|
| 1255 | brelse(bh);
|
| 1256 | goto not_found;
|
| 1257 | }
|
| 1258 |
|
| 1259 | if (type != TYPE_VOLUME) {
|
| 1260 | brelse(bh);
|
| 1261 | continue;
|
| 1262 | }
|
| 1263 |
|
| 1264 | memset(es, 0, sizeof(*es));
|
| 1265 | es->sb = sb;
|
| 1266 | es->bh = es->__bh;
|
| 1267 | es->bh[0] = bh;
|
| 1268 | es->num_bh = 1;
|
| 1269 | es->start_off = exfat_dentries_to_bytes(i) % sb->s_blocksize;
|
| 1270 |
|
| 1271 | return 0;
|
| 1272 | }
|
| 1273 |
|
| 1274 | if (exfat_get_next_cluster(sb, &(clu.dir)))
|
| 1275 | return -EIO;
|
| 1276 | }
|
| 1277 |
|
| 1278 | not_found:
|
| 1279 | if (hint_femp.eidx == EXFAT_HINT_NONE) {
|
| 1280 | hint_femp.cur.dir = EXFAT_EOF_CLUSTER;
|
| 1281 | hint_femp.eidx = dentry;
|
| 1282 | hint_femp.count = 0;
|
| 1283 | }
|
| 1284 |
|
| 1285 | ei->hint_femp = hint_femp;
|
| 1286 |
|
| 1287 | return -ENOENT;
|
| 1288 | }
|
| 1289 |
|
| 1290 | int exfat_read_volume_label(struct super_block *sb, struct exfat_uni_name *label_out)
|
| 1291 | {
|
| 1292 | int ret, i;
|
| 1293 | struct exfat_sb_info *sbi = EXFAT_SB(sb);
|
| 1294 | struct exfat_entry_set_cache es;
|
| 1295 | struct exfat_dentry *ep;
|
| 1296 |
|
| 1297 | mutex_lock(&sbi->s_lock);
|
| 1298 |
|
| 1299 | memset(label_out, 0, sizeof(*label_out));
|
| 1300 | ret = exfat_get_volume_label_dentry(sb, &es);
|
| 1301 | if (ret < 0) {
|
| 1302 | |
| 1303 | * ENOENT signifies that a volume label dentry doesn't exist
|
| 1304 | * We will treat this as an empty volume label and not fail.
|
| 1305 |
|
| 1306 | if (ret == -ENOENT)
|
| 1307 | ret = 0;
|
| 1308 |
|
| 1309 | goto unlock;
|
| 1310 | }
|
| 1311 |
|
| 1312 | ep = exfat_get_dentry_cached(&es, 0);
|
| 1313 | label_out->name_len = ep->dentry.volume_label.char_count;
|
| 1314 | if (label_out->name_len > EXFAT_VOLUME_LABEL_LEN) {
|
| 1315 | ret = -EIO;
|
| 1316 | exfat_put_dentry_set(&es, false);
|
| 1317 | goto unlock;
|
| 1318 | }
|
| 1319 |
|
| 1320 | for (i = 0; i < label_out->name_len; i++)
|
| 1321 | label_out->name[i] = le16_to_cpu(ep->dentry.volume_label.volume_label[i]);
|
| 1322 |
|
| 1323 | exfat_put_dentry_set(&es, false);
|
| 1324 | unlock:
|
| 1325 | mutex_unlock(&sbi->s_lock);
|
| 1326 | return ret;
|
| 1327 | }
|
| 1328 |
|
| 1329 | int exfat_write_volume_label(struct super_block *sb,
|
| 1330 | struct exfat_uni_name *label)
|
| 1331 | {
|
| 1332 | int ret, i;
|
| 1333 | struct exfat_sb_info *sbi = EXFAT_SB(sb);
|
| 1334 | struct inode *root_inode = sb->s_root->d_inode;
|
| 1335 | struct exfat_entry_set_cache es;
|
| 1336 | struct exfat_chain clu;
|
| 1337 | struct exfat_dentry *ep;
|
| 1338 |
|
| 1339 | if (label->name_len > EXFAT_VOLUME_LABEL_LEN)
|
| 1340 | return -EINVAL;
|
| 1341 |
|
| 1342 | mutex_lock(&sbi->s_lock);
|
| 1343 |
|
| 1344 | ret = exfat_get_volume_label_dentry(sb, &es);
|
| 1345 | if (ret == -ENOENT) {
|
| 1346 | if (label->name_len == 0) {
|
| 1347 |
|
| 1348 | ret = 0;
|
| 1349 | goto unlock;
|
| 1350 | }
|
| 1351 |
|
| 1352 | ret = exfat_find_empty_entry(root_inode, &clu, 1, &es);
|
| 1353 | }
|
| 1354 |
|
| 1355 | if (ret < 0)
|
| 1356 | goto unlock;
|
| 1357 |
|
| 1358 | ep = exfat_get_dentry_cached(&es, 0);
|
| 1359 |
|
| 1360 | if (label->name_len == 0 && ep->dentry.volume_label.char_count == 0) {
|
| 1361 |
|
| 1362 | exfat_put_dentry_set(&es, 0);
|
| 1363 | goto unlock;
|
| 1364 | }
|
| 1365 |
|
| 1366 | memset(ep, 0, sizeof(*ep));
|
| 1367 | ep->type = EXFAT_VOLUME;
|
| 1368 |
|
| 1369 | for (i = 0; i < label->name_len; i++)
|
| 1370 | ep->dentry.volume_label.volume_label[i] =
|
| 1371 | cpu_to_le16(label->name[i]);
|
| 1372 |
|
| 1373 | ep->dentry.volume_label.char_count = label->name_len;
|
| 1374 | es.modified = true;
|
| 1375 |
|
| 1376 | ret = exfat_put_dentry_set(&es, IS_DIRSYNC(root_inode));
|
| 1377 |
|
| 1378 | unlock:
|
| 1379 | mutex_unlock(&sbi->s_lock);
|
| 1380 | return ret;
|
| 1381 | }
|