0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/buffer_head.h>
0010 #include <linux/sched.h>
0011 #include <linux/slab.h>
0012 #include <linux/swap.h>
0013 #include <linux/writeback.h>
0014
0015 #include "attrib.h"
0016 #include "debug.h"
0017 #include "layout.h"
0018 #include "lcnalloc.h"
0019 #include "malloc.h"
0020 #include "mft.h"
0021 #include "ntfs.h"
0022 #include "types.h"
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070 int ntfs_map_runlist_nolock(ntfs_inode *ni, VCN vcn, ntfs_attr_search_ctx *ctx)
0071 {
0072 VCN end_vcn;
0073 unsigned long flags;
0074 ntfs_inode *base_ni;
0075 MFT_RECORD *m;
0076 ATTR_RECORD *a;
0077 runlist_element *rl;
0078 struct page *put_this_page = NULL;
0079 int err = 0;
0080 bool ctx_is_temporary, ctx_needs_reset;
0081 ntfs_attr_search_ctx old_ctx = { NULL, };
0082
0083 ntfs_debug("Mapping runlist part containing vcn 0x%llx.",
0084 (unsigned long long)vcn);
0085 if (!NInoAttr(ni))
0086 base_ni = ni;
0087 else
0088 base_ni = ni->ext.base_ntfs_ino;
0089 if (!ctx) {
0090 ctx_is_temporary = ctx_needs_reset = true;
0091 m = map_mft_record(base_ni);
0092 if (IS_ERR(m))
0093 return PTR_ERR(m);
0094 ctx = ntfs_attr_get_search_ctx(base_ni, m);
0095 if (unlikely(!ctx)) {
0096 err = -ENOMEM;
0097 goto err_out;
0098 }
0099 } else {
0100 VCN allocated_size_vcn;
0101
0102 BUG_ON(IS_ERR(ctx->mrec));
0103 a = ctx->attr;
0104 BUG_ON(!a->non_resident);
0105 ctx_is_temporary = false;
0106 end_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn);
0107 read_lock_irqsave(&ni->size_lock, flags);
0108 allocated_size_vcn = ni->allocated_size >>
0109 ni->vol->cluster_size_bits;
0110 read_unlock_irqrestore(&ni->size_lock, flags);
0111 if (!a->data.non_resident.lowest_vcn && end_vcn <= 0)
0112 end_vcn = allocated_size_vcn - 1;
0113
0114
0115
0116
0117
0118
0119
0120 if (vcn >= allocated_size_vcn || (a->type == ni->type &&
0121 a->name_length == ni->name_len &&
0122 !memcmp((u8*)a + le16_to_cpu(a->name_offset),
0123 ni->name, ni->name_len) &&
0124 sle64_to_cpu(a->data.non_resident.lowest_vcn)
0125 <= vcn && end_vcn >= vcn))
0126 ctx_needs_reset = false;
0127 else {
0128
0129 old_ctx = *ctx;
0130
0131
0132
0133
0134
0135
0136
0137
0138 if (old_ctx.base_ntfs_ino && old_ctx.ntfs_ino !=
0139 old_ctx.base_ntfs_ino) {
0140 put_this_page = old_ctx.ntfs_ino->page;
0141 get_page(put_this_page);
0142 }
0143
0144
0145
0146
0147 ntfs_attr_reinit_search_ctx(ctx);
0148 ctx_needs_reset = true;
0149 }
0150 }
0151 if (ctx_needs_reset) {
0152 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
0153 CASE_SENSITIVE, vcn, NULL, 0, ctx);
0154 if (unlikely(err)) {
0155 if (err == -ENOENT)
0156 err = -EIO;
0157 goto err_out;
0158 }
0159 BUG_ON(!ctx->attr->non_resident);
0160 }
0161 a = ctx->attr;
0162
0163
0164
0165
0166
0167
0168 end_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn) + 1;
0169 if (unlikely(vcn && vcn >= end_vcn)) {
0170 err = -ENOENT;
0171 goto err_out;
0172 }
0173 rl = ntfs_mapping_pairs_decompress(ni->vol, a, ni->runlist.rl);
0174 if (IS_ERR(rl))
0175 err = PTR_ERR(rl);
0176 else
0177 ni->runlist.rl = rl;
0178 err_out:
0179 if (ctx_is_temporary) {
0180 if (likely(ctx))
0181 ntfs_attr_put_search_ctx(ctx);
0182 unmap_mft_record(base_ni);
0183 } else if (ctx_needs_reset) {
0184
0185
0186
0187
0188
0189
0190
0191 if (NInoAttrList(base_ni)) {
0192
0193
0194
0195
0196
0197 if (ctx->ntfs_ino != old_ctx.ntfs_ino) {
0198
0199
0200
0201
0202 if (ctx->base_ntfs_ino && ctx->ntfs_ino !=
0203 ctx->base_ntfs_ino) {
0204 unmap_extent_mft_record(ctx->ntfs_ino);
0205 ctx->mrec = ctx->base_mrec;
0206 BUG_ON(!ctx->mrec);
0207 }
0208
0209
0210
0211
0212 if (old_ctx.base_ntfs_ino &&
0213 old_ctx.ntfs_ino !=
0214 old_ctx.base_ntfs_ino) {
0215 retry_map:
0216 ctx->mrec = map_mft_record(
0217 old_ctx.ntfs_ino);
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228 if (IS_ERR(ctx->mrec)) {
0229 if (PTR_ERR(ctx->mrec) ==
0230 -ENOMEM) {
0231 schedule();
0232 goto retry_map;
0233 } else
0234 old_ctx.ntfs_ino =
0235 old_ctx.
0236 base_ntfs_ino;
0237 }
0238 }
0239 }
0240
0241 if (ctx->mrec != old_ctx.mrec) {
0242 if (!IS_ERR(ctx->mrec))
0243 old_ctx.attr = (ATTR_RECORD*)(
0244 (u8*)ctx->mrec +
0245 ((u8*)old_ctx.attr -
0246 (u8*)old_ctx.mrec));
0247 old_ctx.mrec = ctx->mrec;
0248 }
0249 }
0250
0251 *ctx = old_ctx;
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263 if (put_this_page)
0264 put_page(put_this_page);
0265 }
0266 return err;
0267 }
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284 int ntfs_map_runlist(ntfs_inode *ni, VCN vcn)
0285 {
0286 int err = 0;
0287
0288 down_write(&ni->runlist.lock);
0289
0290 if (likely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) <=
0291 LCN_RL_NOT_MAPPED))
0292 err = ntfs_map_runlist_nolock(ni, vcn, NULL);
0293 up_write(&ni->runlist.lock);
0294 return err;
0295 }
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327 LCN ntfs_attr_vcn_to_lcn_nolock(ntfs_inode *ni, const VCN vcn,
0328 const bool write_locked)
0329 {
0330 LCN lcn;
0331 unsigned long flags;
0332 bool is_retry = false;
0333
0334 BUG_ON(!ni);
0335 ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, %s_locked.",
0336 ni->mft_no, (unsigned long long)vcn,
0337 write_locked ? "write" : "read");
0338 BUG_ON(!NInoNonResident(ni));
0339 BUG_ON(vcn < 0);
0340 if (!ni->runlist.rl) {
0341 read_lock_irqsave(&ni->size_lock, flags);
0342 if (!ni->allocated_size) {
0343 read_unlock_irqrestore(&ni->size_lock, flags);
0344 return LCN_ENOENT;
0345 }
0346 read_unlock_irqrestore(&ni->size_lock, flags);
0347 }
0348 retry_remap:
0349
0350 lcn = ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn);
0351 if (likely(lcn >= LCN_HOLE)) {
0352 ntfs_debug("Done, lcn 0x%llx.", (long long)lcn);
0353 return lcn;
0354 }
0355 if (lcn != LCN_RL_NOT_MAPPED) {
0356 if (lcn != LCN_ENOENT)
0357 lcn = LCN_EIO;
0358 } else if (!is_retry) {
0359 int err;
0360
0361 if (!write_locked) {
0362 up_read(&ni->runlist.lock);
0363 down_write(&ni->runlist.lock);
0364 if (unlikely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) !=
0365 LCN_RL_NOT_MAPPED)) {
0366 up_write(&ni->runlist.lock);
0367 down_read(&ni->runlist.lock);
0368 goto retry_remap;
0369 }
0370 }
0371 err = ntfs_map_runlist_nolock(ni, vcn, NULL);
0372 if (!write_locked) {
0373 up_write(&ni->runlist.lock);
0374 down_read(&ni->runlist.lock);
0375 }
0376 if (likely(!err)) {
0377 is_retry = true;
0378 goto retry_remap;
0379 }
0380 if (err == -ENOENT)
0381 lcn = LCN_ENOENT;
0382 else if (err == -ENOMEM)
0383 lcn = LCN_ENOMEM;
0384 else
0385 lcn = LCN_EIO;
0386 }
0387 if (lcn != LCN_ENOENT)
0388 ntfs_error(ni->vol->sb, "Failed with error code %lli.",
0389 (long long)lcn);
0390 return lcn;
0391 }
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432
0433
0434
0435
0436
0437
0438
0439
0440
0441
0442
0443
0444
0445
0446
0447
0448
0449
0450 runlist_element *ntfs_attr_find_vcn_nolock(ntfs_inode *ni, const VCN vcn,
0451 ntfs_attr_search_ctx *ctx)
0452 {
0453 unsigned long flags;
0454 runlist_element *rl;
0455 int err = 0;
0456 bool is_retry = false;
0457
0458 BUG_ON(!ni);
0459 ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, with%s ctx.",
0460 ni->mft_no, (unsigned long long)vcn, ctx ? "" : "out");
0461 BUG_ON(!NInoNonResident(ni));
0462 BUG_ON(vcn < 0);
0463 if (!ni->runlist.rl) {
0464 read_lock_irqsave(&ni->size_lock, flags);
0465 if (!ni->allocated_size) {
0466 read_unlock_irqrestore(&ni->size_lock, flags);
0467 return ERR_PTR(-ENOENT);
0468 }
0469 read_unlock_irqrestore(&ni->size_lock, flags);
0470 }
0471 retry_remap:
0472 rl = ni->runlist.rl;
0473 if (likely(rl && vcn >= rl[0].vcn)) {
0474 while (likely(rl->length)) {
0475 if (unlikely(vcn < rl[1].vcn)) {
0476 if (likely(rl->lcn >= LCN_HOLE)) {
0477 ntfs_debug("Done.");
0478 return rl;
0479 }
0480 break;
0481 }
0482 rl++;
0483 }
0484 if (likely(rl->lcn != LCN_RL_NOT_MAPPED)) {
0485 if (likely(rl->lcn == LCN_ENOENT))
0486 err = -ENOENT;
0487 else
0488 err = -EIO;
0489 }
0490 }
0491 if (!err && !is_retry) {
0492
0493
0494
0495
0496 if (IS_ERR(ctx->mrec))
0497 err = PTR_ERR(ctx->mrec);
0498 else {
0499
0500
0501
0502
0503 err = ntfs_map_runlist_nolock(ni, vcn, ctx);
0504 if (likely(!err)) {
0505 is_retry = true;
0506 goto retry_remap;
0507 }
0508 }
0509 if (err == -EINVAL)
0510 err = -EIO;
0511 } else if (!err)
0512 err = -EIO;
0513 if (err != -ENOENT)
0514 ntfs_error(ni->vol->sb, "Failed with error code %i.", err);
0515 return ERR_PTR(err);
0516 }
0517
0518
0519
0520
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530
0531
0532
0533
0534
0535
0536
0537
0538
0539
0540
0541
0542
0543
0544
0545
0546
0547
0548
0549
0550
0551
0552
0553
0554
0555
0556
0557
0558
0559
0560
0561
0562
0563
0564
0565
0566
0567
0568
0569
0570
0571
0572
0573
0574
0575 static int ntfs_attr_find(const ATTR_TYPE type, const ntfschar *name,
0576 const u32 name_len, const IGNORE_CASE_BOOL ic,
0577 const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
0578 {
0579 ATTR_RECORD *a;
0580 ntfs_volume *vol = ctx->ntfs_ino->vol;
0581 ntfschar *upcase = vol->upcase;
0582 u32 upcase_len = vol->upcase_len;
0583
0584
0585
0586
0587
0588 if (ctx->is_first) {
0589 a = ctx->attr;
0590 ctx->is_first = false;
0591 } else
0592 a = (ATTR_RECORD*)((u8*)ctx->attr +
0593 le32_to_cpu(ctx->attr->length));
0594 for (;; a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length))) {
0595 u8 *mrec_end = (u8 *)ctx->mrec +
0596 le32_to_cpu(ctx->mrec->bytes_allocated);
0597 u8 *name_end = (u8 *)a + le16_to_cpu(a->name_offset) +
0598 a->name_length * sizeof(ntfschar);
0599 if ((u8*)a < (u8*)ctx->mrec || (u8*)a > mrec_end ||
0600 name_end > mrec_end)
0601 break;
0602 ctx->attr = a;
0603 if (unlikely(le32_to_cpu(a->type) > le32_to_cpu(type) ||
0604 a->type == AT_END))
0605 return -ENOENT;
0606 if (unlikely(!a->length))
0607 break;
0608 if (a->type != type)
0609 continue;
0610
0611
0612
0613
0614 if (!name) {
0615
0616 if (a->name_length)
0617 return -ENOENT;
0618 } else if (!ntfs_are_names_equal(name, name_len,
0619 (ntfschar*)((u8*)a + le16_to_cpu(a->name_offset)),
0620 a->name_length, ic, upcase, upcase_len)) {
0621 register int rc;
0622
0623 rc = ntfs_collate_names(name, name_len,
0624 (ntfschar*)((u8*)a +
0625 le16_to_cpu(a->name_offset)),
0626 a->name_length, 1, IGNORE_CASE,
0627 upcase, upcase_len);
0628
0629
0630
0631
0632 if (rc == -1)
0633 return -ENOENT;
0634
0635 if (rc)
0636 continue;
0637 rc = ntfs_collate_names(name, name_len,
0638 (ntfschar*)((u8*)a +
0639 le16_to_cpu(a->name_offset)),
0640 a->name_length, 1, CASE_SENSITIVE,
0641 upcase, upcase_len);
0642 if (rc == -1)
0643 return -ENOENT;
0644 if (rc)
0645 continue;
0646 }
0647
0648
0649
0650
0651
0652 if (!val)
0653 return 0;
0654
0655 else {
0656 register int rc;
0657
0658 rc = memcmp(val, (u8*)a + le16_to_cpu(
0659 a->data.resident.value_offset),
0660 min_t(u32, val_len, le32_to_cpu(
0661 a->data.resident.value_length)));
0662
0663
0664
0665
0666 if (!rc) {
0667 register u32 avl;
0668
0669 avl = le32_to_cpu(
0670 a->data.resident.value_length);
0671 if (val_len == avl)
0672 return 0;
0673 if (val_len < avl)
0674 return -ENOENT;
0675 } else if (rc < 0)
0676 return -ENOENT;
0677 }
0678 }
0679 ntfs_error(vol->sb, "Inode is corrupt. Run chkdsk.");
0680 NVolSetErrors(vol);
0681 return -EIO;
0682 }
0683
0684
0685
0686
0687
0688
0689
0690
0691
0692
0693
0694
0695
0696
0697
0698
0699
0700 int load_attribute_list(ntfs_volume *vol, runlist *runlist, u8 *al_start,
0701 const s64 size, const s64 initialized_size)
0702 {
0703 LCN lcn;
0704 u8 *al = al_start;
0705 u8 *al_end = al + initialized_size;
0706 runlist_element *rl;
0707 struct buffer_head *bh;
0708 struct super_block *sb;
0709 unsigned long block_size;
0710 unsigned long block, max_block;
0711 int err = 0;
0712 unsigned char block_size_bits;
0713
0714 ntfs_debug("Entering.");
0715 if (!vol || !runlist || !al || size <= 0 || initialized_size < 0 ||
0716 initialized_size > size)
0717 return -EINVAL;
0718 if (!initialized_size) {
0719 memset(al, 0, size);
0720 return 0;
0721 }
0722 sb = vol->sb;
0723 block_size = sb->s_blocksize;
0724 block_size_bits = sb->s_blocksize_bits;
0725 down_read(&runlist->lock);
0726 rl = runlist->rl;
0727 if (!rl) {
0728 ntfs_error(sb, "Cannot read attribute list since runlist is "
0729 "missing.");
0730 goto err_out;
0731 }
0732
0733 while (rl->length) {
0734 lcn = ntfs_rl_vcn_to_lcn(rl, rl->vcn);
0735 ntfs_debug("Reading vcn = 0x%llx, lcn = 0x%llx.",
0736 (unsigned long long)rl->vcn,
0737 (unsigned long long)lcn);
0738
0739 if (lcn < 0) {
0740 ntfs_error(sb, "ntfs_rl_vcn_to_lcn() failed. Cannot "
0741 "read attribute list.");
0742 goto err_out;
0743 }
0744 block = lcn << vol->cluster_size_bits >> block_size_bits;
0745
0746 max_block = block + (rl->length << vol->cluster_size_bits >>
0747 block_size_bits);
0748 ntfs_debug("max_block = 0x%lx.", max_block);
0749 do {
0750 ntfs_debug("Reading block = 0x%lx.", block);
0751 bh = sb_bread(sb, block);
0752 if (!bh) {
0753 ntfs_error(sb, "sb_bread() failed. Cannot "
0754 "read attribute list.");
0755 goto err_out;
0756 }
0757 if (al + block_size >= al_end)
0758 goto do_final;
0759 memcpy(al, bh->b_data, block_size);
0760 brelse(bh);
0761 al += block_size;
0762 } while (++block < max_block);
0763 rl++;
0764 }
0765 if (initialized_size < size) {
0766 initialize:
0767 memset(al_start + initialized_size, 0, size - initialized_size);
0768 }
0769 done:
0770 up_read(&runlist->lock);
0771 return err;
0772 do_final:
0773 if (al < al_end) {
0774
0775
0776
0777
0778
0779
0780
0781
0782 memcpy(al, bh->b_data, al_end - al);
0783 brelse(bh);
0784 if (initialized_size < size)
0785 goto initialize;
0786 goto done;
0787 }
0788 brelse(bh);
0789
0790 ntfs_error(sb, "Attribute list buffer overflow. Read attribute list "
0791 "is truncated.");
0792 err_out:
0793 err = -EIO;
0794 goto done;
0795 }
0796
0797
0798
0799
0800
0801
0802
0803
0804
0805
0806
0807
0808
0809
0810
0811
0812
0813
0814
0815
0816
0817
0818
0819
0820
0821
0822
0823
0824
0825
0826
0827
0828
0829
0830
0831
0832
0833
0834
0835
0836
0837
0838
0839
0840
0841
0842
0843
0844
0845
0846
0847 static int ntfs_external_attr_find(const ATTR_TYPE type,
0848 const ntfschar *name, const u32 name_len,
0849 const IGNORE_CASE_BOOL ic, const VCN lowest_vcn,
0850 const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
0851 {
0852 ntfs_inode *base_ni, *ni;
0853 ntfs_volume *vol;
0854 ATTR_LIST_ENTRY *al_entry, *next_al_entry;
0855 u8 *al_start, *al_end;
0856 ATTR_RECORD *a;
0857 ntfschar *al_name;
0858 u32 al_name_len;
0859 int err = 0;
0860 static const char *es = " Unmount and run chkdsk.";
0861
0862 ni = ctx->ntfs_ino;
0863 base_ni = ctx->base_ntfs_ino;
0864 ntfs_debug("Entering for inode 0x%lx, type 0x%x.", ni->mft_no, type);
0865 if (!base_ni) {
0866
0867 base_ni = ctx->base_ntfs_ino = ctx->ntfs_ino;
0868 ctx->base_mrec = ctx->mrec;
0869 }
0870 if (ni == base_ni)
0871 ctx->base_attr = ctx->attr;
0872 if (type == AT_END)
0873 goto not_found;
0874 vol = base_ni->vol;
0875 al_start = base_ni->attr_list;
0876 al_end = al_start + base_ni->attr_list_size;
0877 if (!ctx->al_entry)
0878 ctx->al_entry = (ATTR_LIST_ENTRY*)al_start;
0879
0880
0881
0882
0883 if (ctx->is_first) {
0884 al_entry = ctx->al_entry;
0885 ctx->is_first = false;
0886 } else
0887 al_entry = (ATTR_LIST_ENTRY*)((u8*)ctx->al_entry +
0888 le16_to_cpu(ctx->al_entry->length));
0889 for (;; al_entry = next_al_entry) {
0890
0891 if ((u8*)al_entry < base_ni->attr_list ||
0892 (u8*)al_entry > al_end)
0893 break;
0894 ctx->al_entry = al_entry;
0895
0896 if ((u8*)al_entry == al_end)
0897 goto not_found;
0898 if (!al_entry->length)
0899 break;
0900 if ((u8*)al_entry + 6 > al_end || (u8*)al_entry +
0901 le16_to_cpu(al_entry->length) > al_end)
0902 break;
0903 next_al_entry = (ATTR_LIST_ENTRY*)((u8*)al_entry +
0904 le16_to_cpu(al_entry->length));
0905 if (le32_to_cpu(al_entry->type) > le32_to_cpu(type))
0906 goto not_found;
0907 if (type != al_entry->type)
0908 continue;
0909
0910
0911
0912
0913 al_name_len = al_entry->name_length;
0914 al_name = (ntfschar*)((u8*)al_entry + al_entry->name_offset);
0915 if (!name) {
0916 if (al_name_len)
0917 goto not_found;
0918 } else if (!ntfs_are_names_equal(al_name, al_name_len, name,
0919 name_len, ic, vol->upcase, vol->upcase_len)) {
0920 register int rc;
0921
0922 rc = ntfs_collate_names(name, name_len, al_name,
0923 al_name_len, 1, IGNORE_CASE,
0924 vol->upcase, vol->upcase_len);
0925
0926
0927
0928
0929 if (rc == -1)
0930 goto not_found;
0931
0932 if (rc)
0933 continue;
0934
0935
0936
0937
0938
0939
0940
0941
0942 rc = ntfs_collate_names(name, name_len, al_name,
0943 al_name_len, 1, CASE_SENSITIVE,
0944 vol->upcase, vol->upcase_len);
0945 if (rc == -1)
0946 goto not_found;
0947 if (rc)
0948 continue;
0949 }
0950
0951
0952
0953
0954
0955
0956 if (lowest_vcn && (u8*)next_al_entry >= al_start &&
0957 (u8*)next_al_entry + 6 < al_end &&
0958 (u8*)next_al_entry + le16_to_cpu(
0959 next_al_entry->length) <= al_end &&
0960 sle64_to_cpu(next_al_entry->lowest_vcn) <=
0961 lowest_vcn &&
0962 next_al_entry->type == al_entry->type &&
0963 next_al_entry->name_length == al_name_len &&
0964 ntfs_are_names_equal((ntfschar*)((u8*)
0965 next_al_entry +
0966 next_al_entry->name_offset),
0967 next_al_entry->name_length,
0968 al_name, al_name_len, CASE_SENSITIVE,
0969 vol->upcase, vol->upcase_len))
0970 continue;
0971 if (MREF_LE(al_entry->mft_reference) == ni->mft_no) {
0972 if (MSEQNO_LE(al_entry->mft_reference) != ni->seq_no) {
0973 ntfs_error(vol->sb, "Found stale mft "
0974 "reference in attribute list "
0975 "of base inode 0x%lx.%s",
0976 base_ni->mft_no, es);
0977 err = -EIO;
0978 break;
0979 }
0980 } else {
0981
0982 if (ni != base_ni)
0983 unmap_extent_mft_record(ni);
0984
0985 if (MREF_LE(al_entry->mft_reference) ==
0986 base_ni->mft_no) {
0987 ni = ctx->ntfs_ino = base_ni;
0988 ctx->mrec = ctx->base_mrec;
0989 } else {
0990
0991 ctx->mrec = map_extent_mft_record(base_ni,
0992 le64_to_cpu(
0993 al_entry->mft_reference), &ni);
0994 if (IS_ERR(ctx->mrec)) {
0995 ntfs_error(vol->sb, "Failed to map "
0996 "extent mft record "
0997 "0x%lx of base inode "
0998 "0x%lx.%s",
0999 MREF_LE(al_entry->
1000 mft_reference),
1001 base_ni->mft_no, es);
1002 err = PTR_ERR(ctx->mrec);
1003 if (err == -ENOENT)
1004 err = -EIO;
1005
1006 ni = NULL;
1007 break;
1008 }
1009 ctx->ntfs_ino = ni;
1010 }
1011 ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1012 le16_to_cpu(ctx->mrec->attrs_offset));
1013 }
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029 a = ctx->attr;
1030
1031
1032
1033
1034 do_next_attr_loop:
1035 if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
1036 le32_to_cpu(ctx->mrec->bytes_allocated))
1037 break;
1038 if (a->type == AT_END)
1039 break;
1040 if (!a->length)
1041 break;
1042 if (al_entry->instance != a->instance)
1043 goto do_next_attr;
1044
1045
1046
1047
1048
1049 if (al_entry->type != a->type)
1050 break;
1051 if (!ntfs_are_names_equal((ntfschar*)((u8*)a +
1052 le16_to_cpu(a->name_offset)), a->name_length,
1053 al_name, al_name_len, CASE_SENSITIVE,
1054 vol->upcase, vol->upcase_len))
1055 break;
1056 ctx->attr = a;
1057
1058
1059
1060
1061 if (!val || (!a->non_resident && le32_to_cpu(
1062 a->data.resident.value_length) == val_len &&
1063 !memcmp((u8*)a +
1064 le16_to_cpu(a->data.resident.value_offset),
1065 val, val_len))) {
1066 ntfs_debug("Done, found.");
1067 return 0;
1068 }
1069 do_next_attr:
1070
1071 a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length));
1072 goto do_next_attr_loop;
1073 }
1074 if (!err) {
1075 ntfs_error(vol->sb, "Base inode 0x%lx contains corrupt "
1076 "attribute list attribute.%s", base_ni->mft_no,
1077 es);
1078 err = -EIO;
1079 }
1080 if (ni != base_ni) {
1081 if (ni)
1082 unmap_extent_mft_record(ni);
1083 ctx->ntfs_ino = base_ni;
1084 ctx->mrec = ctx->base_mrec;
1085 ctx->attr = ctx->base_attr;
1086 }
1087 if (err != -ENOMEM)
1088 NVolSetErrors(vol);
1089 return err;
1090 not_found:
1091
1092
1093
1094
1095 if (type == AT_END) {
1096 ntfs_attr_reinit_search_ctx(ctx);
1097 return ntfs_attr_find(AT_END, name, name_len, ic, val, val_len,
1098 ctx);
1099 }
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113 if (ni != base_ni)
1114 unmap_extent_mft_record(ni);
1115 ctx->mrec = ctx->base_mrec;
1116 ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1117 le16_to_cpu(ctx->mrec->attrs_offset));
1118 ctx->is_first = true;
1119 ctx->ntfs_ino = base_ni;
1120 ctx->base_ntfs_ino = NULL;
1121 ctx->base_mrec = NULL;
1122 ctx->base_attr = NULL;
1123
1124
1125
1126
1127
1128
1129
1130 do {
1131 err = ntfs_attr_find(type, name, name_len, ic, val, val_len,
1132 ctx);
1133 } while (!err);
1134 ntfs_debug("Done, not found.");
1135 return err;
1136 }
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177 int ntfs_attr_lookup(const ATTR_TYPE type, const ntfschar *name,
1178 const u32 name_len, const IGNORE_CASE_BOOL ic,
1179 const VCN lowest_vcn, const u8 *val, const u32 val_len,
1180 ntfs_attr_search_ctx *ctx)
1181 {
1182 ntfs_inode *base_ni;
1183
1184 ntfs_debug("Entering.");
1185 BUG_ON(IS_ERR(ctx->mrec));
1186 if (ctx->base_ntfs_ino)
1187 base_ni = ctx->base_ntfs_ino;
1188 else
1189 base_ni = ctx->ntfs_ino;
1190
1191 BUG_ON(!base_ni);
1192 if (!NInoAttrList(base_ni) || type == AT_ATTRIBUTE_LIST)
1193 return ntfs_attr_find(type, name, name_len, ic, val, val_len,
1194 ctx);
1195 return ntfs_external_attr_find(type, name, name_len, ic, lowest_vcn,
1196 val, val_len, ctx);
1197 }
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207 static inline void ntfs_attr_init_search_ctx(ntfs_attr_search_ctx *ctx,
1208 ntfs_inode *ni, MFT_RECORD *mrec)
1209 {
1210 *ctx = (ntfs_attr_search_ctx) {
1211 .mrec = mrec,
1212
1213 .attr = (ATTR_RECORD*)((u8*)mrec +
1214 le16_to_cpu(mrec->attrs_offset)),
1215 .is_first = true,
1216 .ntfs_ino = ni,
1217 };
1218 }
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230 void ntfs_attr_reinit_search_ctx(ntfs_attr_search_ctx *ctx)
1231 {
1232 if (likely(!ctx->base_ntfs_ino)) {
1233
1234 ctx->is_first = true;
1235
1236 ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1237 le16_to_cpu(ctx->mrec->attrs_offset));
1238
1239
1240
1241
1242 ctx->al_entry = NULL;
1243 return;
1244 }
1245 if (ctx->ntfs_ino != ctx->base_ntfs_ino)
1246 unmap_extent_mft_record(ctx->ntfs_ino);
1247 ntfs_attr_init_search_ctx(ctx, ctx->base_ntfs_ino, ctx->base_mrec);
1248 return;
1249 }
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259 ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(ntfs_inode *ni, MFT_RECORD *mrec)
1260 {
1261 ntfs_attr_search_ctx *ctx;
1262
1263 ctx = kmem_cache_alloc(ntfs_attr_ctx_cache, GFP_NOFS);
1264 if (ctx)
1265 ntfs_attr_init_search_ctx(ctx, ni, mrec);
1266 return ctx;
1267 }
1268
1269
1270
1271
1272
1273
1274
1275
1276 void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx)
1277 {
1278 if (ctx->base_ntfs_ino && ctx->ntfs_ino != ctx->base_ntfs_ino)
1279 unmap_extent_mft_record(ctx->ntfs_ino);
1280 kmem_cache_free(ntfs_attr_ctx_cache, ctx);
1281 return;
1282 }
1283
1284 #ifdef NTFS_RW
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296 static ATTR_DEF *ntfs_attr_find_in_attrdef(const ntfs_volume *vol,
1297 const ATTR_TYPE type)
1298 {
1299 ATTR_DEF *ad;
1300
1301 BUG_ON(!vol->attrdef);
1302 BUG_ON(!type);
1303 for (ad = vol->attrdef; (u8*)ad - (u8*)vol->attrdef <
1304 vol->attrdef_size && ad->type; ++ad) {
1305
1306 if (likely(le32_to_cpu(ad->type) < le32_to_cpu(type)))
1307 continue;
1308
1309 if (likely(ad->type == type))
1310 return ad;
1311
1312 break;
1313 }
1314
1315 ntfs_debug("Attribute type 0x%x not found in $AttrDef.",
1316 le32_to_cpu(type));
1317 return NULL;
1318 }
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332 int ntfs_attr_size_bounds_check(const ntfs_volume *vol, const ATTR_TYPE type,
1333 const s64 size)
1334 {
1335 ATTR_DEF *ad;
1336
1337 BUG_ON(size < 0);
1338
1339
1340
1341
1342 if (unlikely(type == AT_ATTRIBUTE_LIST && size > 256 * 1024))
1343 return -ERANGE;
1344
1345 ad = ntfs_attr_find_in_attrdef(vol, type);
1346 if (unlikely(!ad))
1347 return -ENOENT;
1348
1349 if (((sle64_to_cpu(ad->min_size) > 0) &&
1350 size < sle64_to_cpu(ad->min_size)) ||
1351 ((sle64_to_cpu(ad->max_size) > 0) && size >
1352 sle64_to_cpu(ad->max_size)))
1353 return -ERANGE;
1354 return 0;
1355 }
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368 int ntfs_attr_can_be_non_resident(const ntfs_volume *vol, const ATTR_TYPE type)
1369 {
1370 ATTR_DEF *ad;
1371
1372
1373 ad = ntfs_attr_find_in_attrdef(vol, type);
1374 if (unlikely(!ad))
1375 return -ENOENT;
1376
1377 if (ad->flags & ATTR_DEF_RESIDENT)
1378 return -EPERM;
1379 return 0;
1380 }
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400 int ntfs_attr_can_be_resident(const ntfs_volume *vol, const ATTR_TYPE type)
1401 {
1402 if (type == AT_INDEX_ALLOCATION)
1403 return -EPERM;
1404 return 0;
1405 }
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425 int ntfs_attr_record_resize(MFT_RECORD *m, ATTR_RECORD *a, u32 new_size)
1426 {
1427 ntfs_debug("Entering for new_size %u.", new_size);
1428
1429 if (new_size & 7)
1430 new_size = (new_size + 7) & ~7;
1431
1432 if (new_size != le32_to_cpu(a->length)) {
1433 u32 new_muse = le32_to_cpu(m->bytes_in_use) -
1434 le32_to_cpu(a->length) + new_size;
1435
1436 if (new_muse > le32_to_cpu(m->bytes_allocated))
1437 return -ENOSPC;
1438
1439 memmove((u8*)a + new_size, (u8*)a + le32_to_cpu(a->length),
1440 le32_to_cpu(m->bytes_in_use) - ((u8*)a -
1441 (u8*)m) - le32_to_cpu(a->length));
1442
1443 m->bytes_in_use = cpu_to_le32(new_muse);
1444
1445 if (new_size >= offsetof(ATTR_REC, length) + sizeof(a->length))
1446 a->length = cpu_to_le32(new_size);
1447 }
1448 return 0;
1449 }
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469 int ntfs_resident_attr_value_resize(MFT_RECORD *m, ATTR_RECORD *a,
1470 const u32 new_size)
1471 {
1472 u32 old_size;
1473
1474
1475 if (ntfs_attr_record_resize(m, a,
1476 le16_to_cpu(a->data.resident.value_offset) + new_size))
1477 return -ENOSPC;
1478
1479
1480
1481
1482 old_size = le32_to_cpu(a->data.resident.value_length);
1483 if (new_size > old_size)
1484 memset((u8*)a + le16_to_cpu(a->data.resident.value_offset) +
1485 old_size, 0, new_size - old_size);
1486
1487 a->data.resident.value_length = cpu_to_le32(new_size);
1488 return 0;
1489 }
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525 int ntfs_attr_make_non_resident(ntfs_inode *ni, const u32 data_size)
1526 {
1527 s64 new_size;
1528 struct inode *vi = VFS_I(ni);
1529 ntfs_volume *vol = ni->vol;
1530 ntfs_inode *base_ni;
1531 MFT_RECORD *m;
1532 ATTR_RECORD *a;
1533 ntfs_attr_search_ctx *ctx;
1534 struct page *page;
1535 runlist_element *rl;
1536 u8 *kaddr;
1537 unsigned long flags;
1538 int mp_size, mp_ofs, name_ofs, arec_size, err, err2;
1539 u32 attr_size;
1540 u8 old_res_attr_flags;
1541
1542
1543 err = ntfs_attr_can_be_non_resident(vol, ni->type);
1544 if (unlikely(err)) {
1545 if (err == -EPERM)
1546 ntfs_debug("Attribute is not allowed to be "
1547 "non-resident.");
1548 else
1549 ntfs_debug("Attribute not defined on the NTFS "
1550 "volume!");
1551 return err;
1552 }
1553
1554
1555
1556
1557 BUG_ON(NInoCompressed(ni));
1558 BUG_ON(NInoEncrypted(ni));
1559
1560
1561
1562
1563 new_size = (data_size + vol->cluster_size - 1) &
1564 ~(vol->cluster_size - 1);
1565 if (new_size > 0) {
1566
1567
1568
1569
1570 page = find_or_create_page(vi->i_mapping, 0,
1571 mapping_gfp_mask(vi->i_mapping));
1572 if (unlikely(!page))
1573 return -ENOMEM;
1574
1575 rl = ntfs_cluster_alloc(vol, 0, new_size >>
1576 vol->cluster_size_bits, -1, DATA_ZONE, true);
1577 if (IS_ERR(rl)) {
1578 err = PTR_ERR(rl);
1579 ntfs_debug("Failed to allocate cluster%s, error code "
1580 "%i.", (new_size >>
1581 vol->cluster_size_bits) > 1 ? "s" : "",
1582 err);
1583 goto page_err_out;
1584 }
1585 } else {
1586 rl = NULL;
1587 page = NULL;
1588 }
1589
1590 mp_size = ntfs_get_size_for_mapping_pairs(vol, rl, 0, -1);
1591 if (unlikely(mp_size < 0)) {
1592 err = mp_size;
1593 ntfs_debug("Failed to get size for mapping pairs array, error "
1594 "code %i.", err);
1595 goto rl_err_out;
1596 }
1597 down_write(&ni->runlist.lock);
1598 if (!NInoAttr(ni))
1599 base_ni = ni;
1600 else
1601 base_ni = ni->ext.base_ntfs_ino;
1602 m = map_mft_record(base_ni);
1603 if (IS_ERR(m)) {
1604 err = PTR_ERR(m);
1605 m = NULL;
1606 ctx = NULL;
1607 goto err_out;
1608 }
1609 ctx = ntfs_attr_get_search_ctx(base_ni, m);
1610 if (unlikely(!ctx)) {
1611 err = -ENOMEM;
1612 goto err_out;
1613 }
1614 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
1615 CASE_SENSITIVE, 0, NULL, 0, ctx);
1616 if (unlikely(err)) {
1617 if (err == -ENOENT)
1618 err = -EIO;
1619 goto err_out;
1620 }
1621 m = ctx->mrec;
1622 a = ctx->attr;
1623 BUG_ON(NInoNonResident(ni));
1624 BUG_ON(a->non_resident);
1625
1626
1627
1628 if (NInoSparse(ni) || NInoCompressed(ni))
1629 name_ofs = (offsetof(ATTR_REC,
1630 data.non_resident.compressed_size) +
1631 sizeof(a->data.non_resident.compressed_size) +
1632 7) & ~7;
1633 else
1634 name_ofs = (offsetof(ATTR_REC,
1635 data.non_resident.compressed_size) + 7) & ~7;
1636 mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
1637
1638
1639
1640
1641 arec_size = (mp_ofs + mp_size + 7) & ~7;
1642
1643
1644
1645
1646 attr_size = le32_to_cpu(a->data.resident.value_length);
1647 BUG_ON(attr_size != data_size);
1648 if (page && !PageUptodate(page)) {
1649 kaddr = kmap_atomic(page);
1650 memcpy(kaddr, (u8*)a +
1651 le16_to_cpu(a->data.resident.value_offset),
1652 attr_size);
1653 memset(kaddr + attr_size, 0, PAGE_SIZE - attr_size);
1654 kunmap_atomic(kaddr);
1655 flush_dcache_page(page);
1656 SetPageUptodate(page);
1657 }
1658
1659 old_res_attr_flags = a->data.resident.flags;
1660
1661 err = ntfs_attr_record_resize(m, a, arec_size);
1662 if (unlikely(err))
1663 goto err_out;
1664
1665
1666
1667
1668 a->non_resident = 1;
1669
1670 if (a->name_length)
1671 memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
1672 a->name_length * sizeof(ntfschar));
1673 a->name_offset = cpu_to_le16(name_ofs);
1674
1675 a->data.non_resident.lowest_vcn = 0;
1676 a->data.non_resident.highest_vcn = cpu_to_sle64((new_size - 1) >>
1677 vol->cluster_size_bits);
1678 a->data.non_resident.mapping_pairs_offset = cpu_to_le16(mp_ofs);
1679 memset(&a->data.non_resident.reserved, 0,
1680 sizeof(a->data.non_resident.reserved));
1681 a->data.non_resident.allocated_size = cpu_to_sle64(new_size);
1682 a->data.non_resident.data_size =
1683 a->data.non_resident.initialized_size =
1684 cpu_to_sle64(attr_size);
1685 if (NInoSparse(ni) || NInoCompressed(ni)) {
1686 a->data.non_resident.compression_unit = 0;
1687 if (NInoCompressed(ni) || vol->major_ver < 3)
1688 a->data.non_resident.compression_unit = 4;
1689 a->data.non_resident.compressed_size =
1690 a->data.non_resident.allocated_size;
1691 } else
1692 a->data.non_resident.compression_unit = 0;
1693
1694 err = ntfs_mapping_pairs_build(vol, (u8*)a + mp_ofs,
1695 arec_size - mp_ofs, rl, 0, -1, NULL);
1696 if (unlikely(err)) {
1697 ntfs_debug("Failed to build mapping pairs, error code %i.",
1698 err);
1699 goto undo_err_out;
1700 }
1701
1702 ni->runlist.rl = rl;
1703 write_lock_irqsave(&ni->size_lock, flags);
1704 ni->allocated_size = new_size;
1705 if (NInoSparse(ni) || NInoCompressed(ni)) {
1706 ni->itype.compressed.size = ni->allocated_size;
1707 if (a->data.non_resident.compression_unit) {
1708 ni->itype.compressed.block_size = 1U << (a->data.
1709 non_resident.compression_unit +
1710 vol->cluster_size_bits);
1711 ni->itype.compressed.block_size_bits =
1712 ffs(ni->itype.compressed.block_size) -
1713 1;
1714 ni->itype.compressed.block_clusters = 1U <<
1715 a->data.non_resident.compression_unit;
1716 } else {
1717 ni->itype.compressed.block_size = 0;
1718 ni->itype.compressed.block_size_bits = 0;
1719 ni->itype.compressed.block_clusters = 0;
1720 }
1721 vi->i_blocks = ni->itype.compressed.size >> 9;
1722 } else
1723 vi->i_blocks = ni->allocated_size >> 9;
1724 write_unlock_irqrestore(&ni->size_lock, flags);
1725
1726
1727
1728
1729
1730
1731 NInoSetNonResident(ni);
1732
1733 flush_dcache_mft_record_page(ctx->ntfs_ino);
1734 mark_mft_record_dirty(ctx->ntfs_ino);
1735 ntfs_attr_put_search_ctx(ctx);
1736 unmap_mft_record(base_ni);
1737 up_write(&ni->runlist.lock);
1738 if (page) {
1739 set_page_dirty(page);
1740 unlock_page(page);
1741 put_page(page);
1742 }
1743 ntfs_debug("Done.");
1744 return 0;
1745 undo_err_out:
1746
1747 a->non_resident = 0;
1748
1749 name_ofs = (offsetof(ATTR_RECORD, data.resident.reserved) +
1750 sizeof(a->data.resident.reserved) + 7) & ~7;
1751 if (a->name_length)
1752 memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
1753 a->name_length * sizeof(ntfschar));
1754 mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
1755 a->name_offset = cpu_to_le16(name_ofs);
1756 arec_size = (mp_ofs + attr_size + 7) & ~7;
1757
1758 err2 = ntfs_attr_record_resize(m, a, arec_size);
1759 if (unlikely(err2)) {
1760
1761
1762
1763
1764
1765
1766
1767
1768 arec_size = le32_to_cpu(a->length);
1769 if ((mp_ofs + attr_size) > arec_size) {
1770 err2 = attr_size;
1771 attr_size = arec_size - mp_ofs;
1772 ntfs_error(vol->sb, "Failed to undo partial resident "
1773 "to non-resident attribute "
1774 "conversion. Truncating inode 0x%lx, "
1775 "attribute type 0x%x from %i bytes to "
1776 "%i bytes to maintain metadata "
1777 "consistency. THIS MEANS YOU ARE "
1778 "LOSING %i BYTES DATA FROM THIS %s.",
1779 vi->i_ino,
1780 (unsigned)le32_to_cpu(ni->type),
1781 err2, attr_size, err2 - attr_size,
1782 ((ni->type == AT_DATA) &&
1783 !ni->name_len) ? "FILE": "ATTRIBUTE");
1784 write_lock_irqsave(&ni->size_lock, flags);
1785 ni->initialized_size = attr_size;
1786 i_size_write(vi, attr_size);
1787 write_unlock_irqrestore(&ni->size_lock, flags);
1788 }
1789 }
1790
1791 a->data.resident.value_length = cpu_to_le32(attr_size);
1792 a->data.resident.value_offset = cpu_to_le16(mp_ofs);
1793 a->data.resident.flags = old_res_attr_flags;
1794 memset(&a->data.resident.reserved, 0,
1795 sizeof(a->data.resident.reserved));
1796
1797 if (page) {
1798 kaddr = kmap_atomic(page);
1799 memcpy((u8*)a + mp_ofs, kaddr, attr_size);
1800 kunmap_atomic(kaddr);
1801 }
1802
1803 write_lock_irqsave(&ni->size_lock, flags);
1804 ni->allocated_size = arec_size - mp_ofs;
1805 write_unlock_irqrestore(&ni->size_lock, flags);
1806
1807 flush_dcache_mft_record_page(ctx->ntfs_ino);
1808 mark_mft_record_dirty(ctx->ntfs_ino);
1809 err_out:
1810 if (ctx)
1811 ntfs_attr_put_search_ctx(ctx);
1812 if (m)
1813 unmap_mft_record(base_ni);
1814 ni->runlist.rl = NULL;
1815 up_write(&ni->runlist.lock);
1816 rl_err_out:
1817 if (rl) {
1818 if (ntfs_cluster_free_from_rl(vol, rl) < 0) {
1819 ntfs_error(vol->sb, "Failed to release allocated "
1820 "cluster(s) in error code path. Run "
1821 "chkdsk to recover the lost "
1822 "cluster(s).");
1823 NVolSetErrors(vol);
1824 }
1825 ntfs_free(rl);
1826 page_err_out:
1827 unlock_page(page);
1828 put_page(page);
1829 }
1830 if (err == -EINVAL)
1831 err = -EIO;
1832 return err;
1833 }
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894 s64 ntfs_attr_extend_allocation(ntfs_inode *ni, s64 new_alloc_size,
1895 const s64 new_data_size, const s64 data_start)
1896 {
1897 VCN vcn;
1898 s64 ll, allocated_size, start = data_start;
1899 struct inode *vi = VFS_I(ni);
1900 ntfs_volume *vol = ni->vol;
1901 ntfs_inode *base_ni;
1902 MFT_RECORD *m;
1903 ATTR_RECORD *a;
1904 ntfs_attr_search_ctx *ctx;
1905 runlist_element *rl, *rl2;
1906 unsigned long flags;
1907 int err, mp_size;
1908 u32 attr_len = 0;
1909 bool mp_rebuilt;
1910
1911 #ifdef DEBUG
1912 read_lock_irqsave(&ni->size_lock, flags);
1913 allocated_size = ni->allocated_size;
1914 read_unlock_irqrestore(&ni->size_lock, flags);
1915 ntfs_debug("Entering for i_ino 0x%lx, attribute type 0x%x, "
1916 "old_allocated_size 0x%llx, "
1917 "new_allocated_size 0x%llx, new_data_size 0x%llx, "
1918 "data_start 0x%llx.", vi->i_ino,
1919 (unsigned)le32_to_cpu(ni->type),
1920 (unsigned long long)allocated_size,
1921 (unsigned long long)new_alloc_size,
1922 (unsigned long long)new_data_size,
1923 (unsigned long long)start);
1924 #endif
1925 retry_extend:
1926
1927
1928
1929
1930 if (NInoNonResident(ni)) {
1931 if (start > 0)
1932 start &= ~(s64)vol->cluster_size_mask;
1933 new_alloc_size = (new_alloc_size + vol->cluster_size - 1) &
1934 ~(s64)vol->cluster_size_mask;
1935 }
1936 BUG_ON(new_data_size >= 0 && new_data_size > new_alloc_size);
1937
1938 err = ntfs_attr_size_bounds_check(vol, ni->type, new_alloc_size);
1939 if (unlikely(err)) {
1940
1941 read_lock_irqsave(&ni->size_lock, flags);
1942 allocated_size = ni->allocated_size;
1943 read_unlock_irqrestore(&ni->size_lock, flags);
1944 if (start < 0 || start >= allocated_size) {
1945 if (err == -ERANGE) {
1946 ntfs_error(vol->sb, "Cannot extend allocation "
1947 "of inode 0x%lx, attribute "
1948 "type 0x%x, because the new "
1949 "allocation would exceed the "
1950 "maximum allowed size for "
1951 "this attribute type.",
1952 vi->i_ino, (unsigned)
1953 le32_to_cpu(ni->type));
1954 } else {
1955 ntfs_error(vol->sb, "Cannot extend allocation "
1956 "of inode 0x%lx, attribute "
1957 "type 0x%x, because this "
1958 "attribute type is not "
1959 "defined on the NTFS volume. "
1960 "Possible corruption! You "
1961 "should run chkdsk!",
1962 vi->i_ino, (unsigned)
1963 le32_to_cpu(ni->type));
1964 }
1965 }
1966
1967 if (err == -ERANGE)
1968 err = -EFBIG;
1969 else
1970 err = -EIO;
1971 return err;
1972 }
1973 if (!NInoAttr(ni))
1974 base_ni = ni;
1975 else
1976 base_ni = ni->ext.base_ntfs_ino;
1977
1978
1979
1980
1981 down_write(&ni->runlist.lock);
1982 m = map_mft_record(base_ni);
1983 if (IS_ERR(m)) {
1984 err = PTR_ERR(m);
1985 m = NULL;
1986 ctx = NULL;
1987 goto err_out;
1988 }
1989 ctx = ntfs_attr_get_search_ctx(base_ni, m);
1990 if (unlikely(!ctx)) {
1991 err = -ENOMEM;
1992 goto err_out;
1993 }
1994 read_lock_irqsave(&ni->size_lock, flags);
1995 allocated_size = ni->allocated_size;
1996 read_unlock_irqrestore(&ni->size_lock, flags);
1997
1998
1999
2000
2001 vcn = NInoNonResident(ni) ? allocated_size >> vol->cluster_size_bits :
2002 0;
2003
2004
2005
2006
2007
2008
2009 if (unlikely(new_alloc_size <= allocated_size)) {
2010 ntfs_debug("Allocated size already exceeds requested size.");
2011 new_alloc_size = allocated_size;
2012 if (new_data_size < 0)
2013 goto done;
2014
2015
2016
2017
2018 vcn = 0;
2019 }
2020 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
2021 CASE_SENSITIVE, vcn, NULL, 0, ctx);
2022 if (unlikely(err)) {
2023 if (err == -ENOENT)
2024 err = -EIO;
2025 goto err_out;
2026 }
2027 m = ctx->mrec;
2028 a = ctx->attr;
2029
2030 if (a->non_resident)
2031 goto do_non_resident_extend;
2032 BUG_ON(NInoNonResident(ni));
2033
2034 attr_len = le32_to_cpu(a->data.resident.value_length);
2035
2036
2037
2038
2039
2040 if (new_alloc_size < vol->mft_record_size &&
2041 !ntfs_attr_record_resize(m, a,
2042 le16_to_cpu(a->data.resident.value_offset) +
2043 new_alloc_size)) {
2044
2045 write_lock_irqsave(&ni->size_lock, flags);
2046 ni->allocated_size = le32_to_cpu(a->length) -
2047 le16_to_cpu(a->data.resident.value_offset);
2048 write_unlock_irqrestore(&ni->size_lock, flags);
2049 if (new_data_size >= 0) {
2050 BUG_ON(new_data_size < attr_len);
2051 a->data.resident.value_length =
2052 cpu_to_le32((u32)new_data_size);
2053 }
2054 goto flush_done;
2055 }
2056
2057
2058
2059
2060
2061
2062
2063
2064 ntfs_attr_put_search_ctx(ctx);
2065 unmap_mft_record(base_ni);
2066 up_write(&ni->runlist.lock);
2067
2068
2069
2070
2071 err = ntfs_attr_make_non_resident(ni, attr_len);
2072 if (likely(!err))
2073 goto retry_extend;
2074
2075
2076
2077
2078
2079 if (unlikely(err != -EPERM && err != -ENOSPC)) {
2080
2081 read_lock_irqsave(&ni->size_lock, flags);
2082 allocated_size = ni->allocated_size;
2083 read_unlock_irqrestore(&ni->size_lock, flags);
2084 if (start < 0 || start >= allocated_size)
2085 ntfs_error(vol->sb, "Cannot extend allocation of "
2086 "inode 0x%lx, attribute type 0x%x, "
2087 "because the conversion from resident "
2088 "to non-resident attribute failed "
2089 "with error code %i.", vi->i_ino,
2090 (unsigned)le32_to_cpu(ni->type), err);
2091 if (err != -ENOMEM)
2092 err = -EIO;
2093 goto conv_err_out;
2094 }
2095
2096 read_lock_irqsave(&ni->size_lock, flags);
2097 allocated_size = ni->allocated_size;
2098 read_unlock_irqrestore(&ni->size_lock, flags);
2099 if (start < 0 || start >= allocated_size) {
2100 if (err == -ENOSPC)
2101 ntfs_error(vol->sb, "Not enough space in the mft "
2102 "record/on disk for the non-resident "
2103 "attribute value. This case is not "
2104 "implemented yet.");
2105 else
2106 ntfs_error(vol->sb, "This attribute type may not be "
2107 "non-resident. This case is not "
2108 "implemented yet.");
2109 }
2110 err = -EOPNOTSUPP;
2111 goto conv_err_out;
2112 #if 0
2113
2114 if (!err)
2115 goto do_resident_extend;
2116
2117
2118
2119
2120
2121
2122 if (ni->type == AT_ATTRIBUTE_LIST ||
2123 ni->type == AT_STANDARD_INFORMATION) {
2124
2125
2126 err = -EOPNOTSUPP;
2127 if (!err)
2128 goto do_resident_extend;
2129 goto err_out;
2130 }
2131
2132
2133
2134 err = -EOPNOTSUPP;
2135 if (!err)
2136 goto do_resident_extend;
2137
2138 goto err_out;
2139 #endif
2140 do_non_resident_extend:
2141 BUG_ON(!NInoNonResident(ni));
2142 if (new_alloc_size == allocated_size) {
2143 BUG_ON(vcn);
2144 goto alloc_done;
2145 }
2146
2147
2148
2149
2150
2151
2152
2153
2154 if ((start >= 0 && start <= allocated_size) || ni->type != AT_DATA ||
2155 !NVolSparseEnabled(vol) || NInoSparseDisabled(ni))
2156 goto skip_sparse;
2157
2158
2159 ntfs_debug("Inserting holes is not-implemented yet. Falling back to "
2160 "allocating real clusters instead.");
2161 skip_sparse:
2162 rl = ni->runlist.rl;
2163 if (likely(rl)) {
2164
2165 while (rl->length)
2166 rl++;
2167 }
2168
2169 if (unlikely(!rl || rl->lcn == LCN_RL_NOT_MAPPED ||
2170 (rl->lcn == LCN_ENOENT && rl > ni->runlist.rl &&
2171 (rl-1)->lcn == LCN_RL_NOT_MAPPED))) {
2172 if (!rl && !allocated_size)
2173 goto first_alloc;
2174 rl = ntfs_mapping_pairs_decompress(vol, a, ni->runlist.rl);
2175 if (IS_ERR(rl)) {
2176 err = PTR_ERR(rl);
2177 if (start < 0 || start >= allocated_size)
2178 ntfs_error(vol->sb, "Cannot extend allocation "
2179 "of inode 0x%lx, attribute "
2180 "type 0x%x, because the "
2181 "mapping of a runlist "
2182 "fragment failed with error "
2183 "code %i.", vi->i_ino,
2184 (unsigned)le32_to_cpu(ni->type),
2185 err);
2186 if (err != -ENOMEM)
2187 err = -EIO;
2188 goto err_out;
2189 }
2190 ni->runlist.rl = rl;
2191
2192 while (rl->length)
2193 rl++;
2194 }
2195
2196
2197
2198
2199
2200
2201
2202
2203 while (rl->lcn < 0 && rl > ni->runlist.rl)
2204 rl--;
2205 first_alloc:
2206
2207
2208
2209 rl2 = ntfs_cluster_alloc(vol, allocated_size >> vol->cluster_size_bits,
2210 (new_alloc_size - allocated_size) >>
2211 vol->cluster_size_bits, (rl && (rl->lcn >= 0)) ?
2212 rl->lcn + rl->length : -1, DATA_ZONE, true);
2213 if (IS_ERR(rl2)) {
2214 err = PTR_ERR(rl2);
2215 if (start < 0 || start >= allocated_size)
2216 ntfs_error(vol->sb, "Cannot extend allocation of "
2217 "inode 0x%lx, attribute type 0x%x, "
2218 "because the allocation of clusters "
2219 "failed with error code %i.", vi->i_ino,
2220 (unsigned)le32_to_cpu(ni->type), err);
2221 if (err != -ENOMEM && err != -ENOSPC)
2222 err = -EIO;
2223 goto err_out;
2224 }
2225 rl = ntfs_runlists_merge(ni->runlist.rl, rl2);
2226 if (IS_ERR(rl)) {
2227 err = PTR_ERR(rl);
2228 if (start < 0 || start >= allocated_size)
2229 ntfs_error(vol->sb, "Cannot extend allocation of "
2230 "inode 0x%lx, attribute type 0x%x, "
2231 "because the runlist merge failed "
2232 "with error code %i.", vi->i_ino,
2233 (unsigned)le32_to_cpu(ni->type), err);
2234 if (err != -ENOMEM)
2235 err = -EIO;
2236 if (ntfs_cluster_free_from_rl(vol, rl2)) {
2237 ntfs_error(vol->sb, "Failed to release allocated "
2238 "cluster(s) in error code path. Run "
2239 "chkdsk to recover the lost "
2240 "cluster(s).");
2241 NVolSetErrors(vol);
2242 }
2243 ntfs_free(rl2);
2244 goto err_out;
2245 }
2246 ni->runlist.rl = rl;
2247 ntfs_debug("Allocated 0x%llx clusters.", (long long)(new_alloc_size -
2248 allocated_size) >> vol->cluster_size_bits);
2249
2250 ll = sle64_to_cpu(a->data.non_resident.lowest_vcn);
2251 rl2 = ntfs_rl_find_vcn_nolock(rl, ll);
2252 BUG_ON(!rl2);
2253 BUG_ON(!rl2->length);
2254 BUG_ON(rl2->lcn < LCN_HOLE);
2255 mp_rebuilt = false;
2256
2257 mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, -1);
2258 if (unlikely(mp_size <= 0)) {
2259 err = mp_size;
2260 if (start < 0 || start >= allocated_size)
2261 ntfs_error(vol->sb, "Cannot extend allocation of "
2262 "inode 0x%lx, attribute type 0x%x, "
2263 "because determining the size for the "
2264 "mapping pairs failed with error code "
2265 "%i.", vi->i_ino,
2266 (unsigned)le32_to_cpu(ni->type), err);
2267 err = -EIO;
2268 goto undo_alloc;
2269 }
2270
2271 attr_len = le32_to_cpu(a->length);
2272 err = ntfs_attr_record_resize(m, a, mp_size +
2273 le16_to_cpu(a->data.non_resident.mapping_pairs_offset));
2274 if (unlikely(err)) {
2275 BUG_ON(err != -ENOSPC);
2276
2277
2278
2279
2280
2281
2282 if (start < 0 || start >= allocated_size)
2283 ntfs_error(vol->sb, "Not enough space in the mft "
2284 "record for the extended attribute "
2285 "record. This case is not "
2286 "implemented yet.");
2287 err = -EOPNOTSUPP;
2288 goto undo_alloc;
2289 }
2290 mp_rebuilt = true;
2291
2292 err = ntfs_mapping_pairs_build(vol, (u8*)a +
2293 le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
2294 mp_size, rl2, ll, -1, NULL);
2295 if (unlikely(err)) {
2296 if (start < 0 || start >= allocated_size)
2297 ntfs_error(vol->sb, "Cannot extend allocation of "
2298 "inode 0x%lx, attribute type 0x%x, "
2299 "because building the mapping pairs "
2300 "failed with error code %i.", vi->i_ino,
2301 (unsigned)le32_to_cpu(ni->type), err);
2302 err = -EIO;
2303 goto undo_alloc;
2304 }
2305
2306 a->data.non_resident.highest_vcn = cpu_to_sle64((new_alloc_size >>
2307 vol->cluster_size_bits) - 1);
2308
2309
2310
2311
2312 if (a->data.non_resident.lowest_vcn) {
2313
2314
2315
2316
2317 flush_dcache_mft_record_page(ctx->ntfs_ino);
2318 mark_mft_record_dirty(ctx->ntfs_ino);
2319 ntfs_attr_reinit_search_ctx(ctx);
2320 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
2321 CASE_SENSITIVE, 0, NULL, 0, ctx);
2322 if (unlikely(err))
2323 goto restore_undo_alloc;
2324
2325 a = ctx->attr;
2326 }
2327 write_lock_irqsave(&ni->size_lock, flags);
2328 ni->allocated_size = new_alloc_size;
2329 a->data.non_resident.allocated_size = cpu_to_sle64(new_alloc_size);
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340 if (NInoSparse(ni) || NInoCompressed(ni)) {
2341 ni->itype.compressed.size += new_alloc_size - allocated_size;
2342 a->data.non_resident.compressed_size =
2343 cpu_to_sle64(ni->itype.compressed.size);
2344 vi->i_blocks = ni->itype.compressed.size >> 9;
2345 } else
2346 vi->i_blocks = new_alloc_size >> 9;
2347 write_unlock_irqrestore(&ni->size_lock, flags);
2348 alloc_done:
2349 if (new_data_size >= 0) {
2350 BUG_ON(new_data_size <
2351 sle64_to_cpu(a->data.non_resident.data_size));
2352 a->data.non_resident.data_size = cpu_to_sle64(new_data_size);
2353 }
2354 flush_done:
2355
2356 flush_dcache_mft_record_page(ctx->ntfs_ino);
2357 mark_mft_record_dirty(ctx->ntfs_ino);
2358 done:
2359 ntfs_attr_put_search_ctx(ctx);
2360 unmap_mft_record(base_ni);
2361 up_write(&ni->runlist.lock);
2362 ntfs_debug("Done, new_allocated_size 0x%llx.",
2363 (unsigned long long)new_alloc_size);
2364 return new_alloc_size;
2365 restore_undo_alloc:
2366 if (start < 0 || start >= allocated_size)
2367 ntfs_error(vol->sb, "Cannot complete extension of allocation "
2368 "of inode 0x%lx, attribute type 0x%x, because "
2369 "lookup of first attribute extent failed with "
2370 "error code %i.", vi->i_ino,
2371 (unsigned)le32_to_cpu(ni->type), err);
2372 if (err == -ENOENT)
2373 err = -EIO;
2374 ntfs_attr_reinit_search_ctx(ctx);
2375 if (ntfs_attr_lookup(ni->type, ni->name, ni->name_len, CASE_SENSITIVE,
2376 allocated_size >> vol->cluster_size_bits, NULL, 0,
2377 ctx)) {
2378 ntfs_error(vol->sb, "Failed to find last attribute extent of "
2379 "attribute in error code path. Run chkdsk to "
2380 "recover.");
2381 write_lock_irqsave(&ni->size_lock, flags);
2382 ni->allocated_size = new_alloc_size;
2383
2384
2385
2386
2387
2388 if (NInoSparse(ni) || NInoCompressed(ni)) {
2389 ni->itype.compressed.size += new_alloc_size -
2390 allocated_size;
2391 vi->i_blocks = ni->itype.compressed.size >> 9;
2392 } else
2393 vi->i_blocks = new_alloc_size >> 9;
2394 write_unlock_irqrestore(&ni->size_lock, flags);
2395 ntfs_attr_put_search_ctx(ctx);
2396 unmap_mft_record(base_ni);
2397 up_write(&ni->runlist.lock);
2398
2399
2400
2401
2402 NVolSetErrors(vol);
2403 return err;
2404 }
2405 ctx->attr->data.non_resident.highest_vcn = cpu_to_sle64(
2406 (allocated_size >> vol->cluster_size_bits) - 1);
2407 undo_alloc:
2408 ll = allocated_size >> vol->cluster_size_bits;
2409 if (ntfs_cluster_free(ni, ll, -1, ctx) < 0) {
2410 ntfs_error(vol->sb, "Failed to release allocated cluster(s) "
2411 "in error code path. Run chkdsk to recover "
2412 "the lost cluster(s).");
2413 NVolSetErrors(vol);
2414 }
2415 m = ctx->mrec;
2416 a = ctx->attr;
2417
2418
2419
2420
2421
2422
2423 if (ntfs_rl_truncate_nolock(vol, &ni->runlist, ll) || IS_ERR(m)) {
2424 ntfs_error(vol->sb, "Failed to %s in error code path. Run "
2425 "chkdsk to recover.", IS_ERR(m) ?
2426 "restore attribute search context" :
2427 "truncate attribute runlist");
2428 NVolSetErrors(vol);
2429 } else if (mp_rebuilt) {
2430 if (ntfs_attr_record_resize(m, a, attr_len)) {
2431 ntfs_error(vol->sb, "Failed to restore attribute "
2432 "record in error code path. Run "
2433 "chkdsk to recover.");
2434 NVolSetErrors(vol);
2435 } else {
2436 if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu(
2437 a->data.non_resident.
2438 mapping_pairs_offset), attr_len -
2439 le16_to_cpu(a->data.non_resident.
2440 mapping_pairs_offset), rl2, ll, -1,
2441 NULL)) {
2442 ntfs_error(vol->sb, "Failed to restore "
2443 "mapping pairs array in error "
2444 "code path. Run chkdsk to "
2445 "recover.");
2446 NVolSetErrors(vol);
2447 }
2448 flush_dcache_mft_record_page(ctx->ntfs_ino);
2449 mark_mft_record_dirty(ctx->ntfs_ino);
2450 }
2451 }
2452 err_out:
2453 if (ctx)
2454 ntfs_attr_put_search_ctx(ctx);
2455 if (m)
2456 unmap_mft_record(base_ni);
2457 up_write(&ni->runlist.lock);
2458 conv_err_out:
2459 ntfs_debug("Failed. Returning error code %i.", err);
2460 return err;
2461 }
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483 int ntfs_attr_set(ntfs_inode *ni, const s64 ofs, const s64 cnt, const u8 val)
2484 {
2485 ntfs_volume *vol = ni->vol;
2486 struct address_space *mapping;
2487 struct page *page;
2488 u8 *kaddr;
2489 pgoff_t idx, end;
2490 unsigned start_ofs, end_ofs, size;
2491
2492 ntfs_debug("Entering for ofs 0x%llx, cnt 0x%llx, val 0x%hx.",
2493 (long long)ofs, (long long)cnt, val);
2494 BUG_ON(ofs < 0);
2495 BUG_ON(cnt < 0);
2496 if (!cnt)
2497 goto done;
2498
2499
2500
2501
2502 BUG_ON(NInoCompressed(ni));
2503 BUG_ON(NInoEncrypted(ni));
2504 mapping = VFS_I(ni)->i_mapping;
2505
2506 idx = ofs >> PAGE_SHIFT;
2507 start_ofs = ofs & ~PAGE_MASK;
2508
2509 end = ofs + cnt;
2510 end_ofs = end & ~PAGE_MASK;
2511
2512 if (unlikely(end > i_size_read(VFS_I(ni)))) {
2513 ntfs_error(vol->sb, "Request exceeds end of attribute.");
2514 return -ESPIPE;
2515 }
2516 end >>= PAGE_SHIFT;
2517
2518 if (start_ofs) {
2519 page = read_mapping_page(mapping, idx, NULL);
2520 if (IS_ERR(page)) {
2521 ntfs_error(vol->sb, "Failed to read first partial "
2522 "page (error, index 0x%lx).", idx);
2523 return PTR_ERR(page);
2524 }
2525
2526
2527
2528
2529 size = PAGE_SIZE;
2530 if (idx == end)
2531 size = end_ofs;
2532 kaddr = kmap_atomic(page);
2533 memset(kaddr + start_ofs, val, size - start_ofs);
2534 flush_dcache_page(page);
2535 kunmap_atomic(kaddr);
2536 set_page_dirty(page);
2537 put_page(page);
2538 balance_dirty_pages_ratelimited(mapping);
2539 cond_resched();
2540 if (idx == end)
2541 goto done;
2542 idx++;
2543 }
2544
2545 for (; idx < end; idx++) {
2546
2547 page = grab_cache_page(mapping, idx);
2548 if (unlikely(!page)) {
2549 ntfs_error(vol->sb, "Insufficient memory to grab "
2550 "page (index 0x%lx).", idx);
2551 return -ENOMEM;
2552 }
2553 kaddr = kmap_atomic(page);
2554 memset(kaddr, val, PAGE_SIZE);
2555 flush_dcache_page(page);
2556 kunmap_atomic(kaddr);
2557
2558
2559
2560
2561 if (page_has_buffers(page)) {
2562 struct buffer_head *bh, *head;
2563
2564 bh = head = page_buffers(page);
2565 do {
2566 set_buffer_uptodate(bh);
2567 } while ((bh = bh->b_this_page) != head);
2568 }
2569
2570 SetPageUptodate(page);
2571
2572
2573
2574
2575 set_page_dirty(page);
2576
2577 unlock_page(page);
2578 put_page(page);
2579 balance_dirty_pages_ratelimited(mapping);
2580 cond_resched();
2581 }
2582
2583 if (end_ofs) {
2584 page = read_mapping_page(mapping, idx, NULL);
2585 if (IS_ERR(page)) {
2586 ntfs_error(vol->sb, "Failed to read last partial page "
2587 "(error, index 0x%lx).", idx);
2588 return PTR_ERR(page);
2589 }
2590 kaddr = kmap_atomic(page);
2591 memset(kaddr, val, end_ofs);
2592 flush_dcache_page(page);
2593 kunmap_atomic(kaddr);
2594 set_page_dirty(page);
2595 put_page(page);
2596 balance_dirty_pages_ratelimited(mapping);
2597 cond_resched();
2598 }
2599 done:
2600 ntfs_debug("Done.");
2601 return 0;
2602 }
2603
2604 #endif