1
2
3
4
5
6
7
8
9#include <linux/buffer_head.h>
10#include <linux/sched.h>
11#include <linux/slab.h>
12#include <linux/swap.h>
13#include <linux/writeback.h>
14
15#include "attrib.h"
16#include "debug.h"
17#include "layout.h"
18#include "lcnalloc.h"
19#include "malloc.h"
20#include "mft.h"
21#include "ntfs.h"
22#include "types.h"
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70int ntfs_map_runlist_nolock(ntfs_inode *ni, VCN vcn, ntfs_attr_search_ctx *ctx)
71{
72 VCN end_vcn;
73 unsigned long flags;
74 ntfs_inode *base_ni;
75 MFT_RECORD *m;
76 ATTR_RECORD *a;
77 runlist_element *rl;
78 struct page *put_this_page = NULL;
79 int err = 0;
80 bool ctx_is_temporary, ctx_needs_reset;
81 ntfs_attr_search_ctx old_ctx = { NULL, };
82
83 ntfs_debug("Mapping runlist part containing vcn 0x%llx.",
84 (unsigned long long)vcn);
85 if (!NInoAttr(ni))
86 base_ni = ni;
87 else
88 base_ni = ni->ext.base_ntfs_ino;
89 if (!ctx) {
90 ctx_is_temporary = ctx_needs_reset = true;
91 m = map_mft_record(base_ni);
92 if (IS_ERR(m))
93 return PTR_ERR(m);
94 ctx = ntfs_attr_get_search_ctx(base_ni, m);
95 if (unlikely(!ctx)) {
96 err = -ENOMEM;
97 goto err_out;
98 }
99 } else {
100 VCN allocated_size_vcn;
101
102 BUG_ON(IS_ERR(ctx->mrec));
103 a = ctx->attr;
104 BUG_ON(!a->non_resident);
105 ctx_is_temporary = false;
106 end_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn);
107 read_lock_irqsave(&ni->size_lock, flags);
108 allocated_size_vcn = ni->allocated_size >>
109 ni->vol->cluster_size_bits;
110 read_unlock_irqrestore(&ni->size_lock, flags);
111 if (!a->data.non_resident.lowest_vcn && end_vcn <= 0)
112 end_vcn = allocated_size_vcn - 1;
113
114
115
116
117
118
119
120 if (vcn >= allocated_size_vcn || (a->type == ni->type &&
121 a->name_length == ni->name_len &&
122 !memcmp((u8*)a + le16_to_cpu(a->name_offset),
123 ni->name, ni->name_len) &&
124 sle64_to_cpu(a->data.non_resident.lowest_vcn)
125 <= vcn && end_vcn >= vcn))
126 ctx_needs_reset = false;
127 else {
128
129 old_ctx = *ctx;
130
131
132
133
134
135
136
137
138 if (old_ctx.base_ntfs_ino && old_ctx.ntfs_ino !=
139 old_ctx.base_ntfs_ino) {
140 put_this_page = old_ctx.ntfs_ino->page;
141 get_page(put_this_page);
142 }
143
144
145
146
147 ntfs_attr_reinit_search_ctx(ctx);
148 ctx_needs_reset = true;
149 }
150 }
151 if (ctx_needs_reset) {
152 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
153 CASE_SENSITIVE, vcn, NULL, 0, ctx);
154 if (unlikely(err)) {
155 if (err == -ENOENT)
156 err = -EIO;
157 goto err_out;
158 }
159 BUG_ON(!ctx->attr->non_resident);
160 }
161 a = ctx->attr;
162
163
164
165
166
167
168 end_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn) + 1;
169 if (unlikely(vcn && vcn >= end_vcn)) {
170 err = -ENOENT;
171 goto err_out;
172 }
173 rl = ntfs_mapping_pairs_decompress(ni->vol, a, ni->runlist.rl);
174 if (IS_ERR(rl))
175 err = PTR_ERR(rl);
176 else
177 ni->runlist.rl = rl;
178err_out:
179 if (ctx_is_temporary) {
180 if (likely(ctx))
181 ntfs_attr_put_search_ctx(ctx);
182 unmap_mft_record(base_ni);
183 } else if (ctx_needs_reset) {
184
185
186
187
188
189
190
191 if (NInoAttrList(base_ni)) {
192
193
194
195
196
197 if (ctx->ntfs_ino != old_ctx.ntfs_ino) {
198
199
200
201
202 if (ctx->base_ntfs_ino && ctx->ntfs_ino !=
203 ctx->base_ntfs_ino) {
204 unmap_extent_mft_record(ctx->ntfs_ino);
205 ctx->mrec = ctx->base_mrec;
206 BUG_ON(!ctx->mrec);
207 }
208
209
210
211
212 if (old_ctx.base_ntfs_ino &&
213 old_ctx.ntfs_ino !=
214 old_ctx.base_ntfs_ino) {
215retry_map:
216 ctx->mrec = map_mft_record(
217 old_ctx.ntfs_ino);
218
219
220
221
222
223
224
225
226
227
228 if (IS_ERR(ctx->mrec)) {
229 if (PTR_ERR(ctx->mrec) ==
230 -ENOMEM) {
231 schedule();
232 goto retry_map;
233 } else
234 old_ctx.ntfs_ino =
235 old_ctx.
236 base_ntfs_ino;
237 }
238 }
239 }
240
241 if (ctx->mrec != old_ctx.mrec) {
242 if (!IS_ERR(ctx->mrec))
243 old_ctx.attr = (ATTR_RECORD*)(
244 (u8*)ctx->mrec +
245 ((u8*)old_ctx.attr -
246 (u8*)old_ctx.mrec));
247 old_ctx.mrec = ctx->mrec;
248 }
249 }
250
251 *ctx = old_ctx;
252
253
254
255
256
257
258
259
260
261
262
263 if (put_this_page)
264 put_page(put_this_page);
265 }
266 return err;
267}
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284int ntfs_map_runlist(ntfs_inode *ni, VCN vcn)
285{
286 int err = 0;
287
288 down_write(&ni->runlist.lock);
289
290 if (likely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) <=
291 LCN_RL_NOT_MAPPED))
292 err = ntfs_map_runlist_nolock(ni, vcn, NULL);
293 up_write(&ni->runlist.lock);
294 return err;
295}
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327LCN ntfs_attr_vcn_to_lcn_nolock(ntfs_inode *ni, const VCN vcn,
328 const bool write_locked)
329{
330 LCN lcn;
331 unsigned long flags;
332 bool is_retry = false;
333
334 BUG_ON(!ni);
335 ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, %s_locked.",
336 ni->mft_no, (unsigned long long)vcn,
337 write_locked ? "write" : "read");
338 BUG_ON(!NInoNonResident(ni));
339 BUG_ON(vcn < 0);
340 if (!ni->runlist.rl) {
341 read_lock_irqsave(&ni->size_lock, flags);
342 if (!ni->allocated_size) {
343 read_unlock_irqrestore(&ni->size_lock, flags);
344 return LCN_ENOENT;
345 }
346 read_unlock_irqrestore(&ni->size_lock, flags);
347 }
348retry_remap:
349
350 lcn = ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn);
351 if (likely(lcn >= LCN_HOLE)) {
352 ntfs_debug("Done, lcn 0x%llx.", (long long)lcn);
353 return lcn;
354 }
355 if (lcn != LCN_RL_NOT_MAPPED) {
356 if (lcn != LCN_ENOENT)
357 lcn = LCN_EIO;
358 } else if (!is_retry) {
359 int err;
360
361 if (!write_locked) {
362 up_read(&ni->runlist.lock);
363 down_write(&ni->runlist.lock);
364 if (unlikely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) !=
365 LCN_RL_NOT_MAPPED)) {
366 up_write(&ni->runlist.lock);
367 down_read(&ni->runlist.lock);
368 goto retry_remap;
369 }
370 }
371 err = ntfs_map_runlist_nolock(ni, vcn, NULL);
372 if (!write_locked) {
373 up_write(&ni->runlist.lock);
374 down_read(&ni->runlist.lock);
375 }
376 if (likely(!err)) {
377 is_retry = true;
378 goto retry_remap;
379 }
380 if (err == -ENOENT)
381 lcn = LCN_ENOENT;
382 else if (err == -ENOMEM)
383 lcn = LCN_ENOMEM;
384 else
385 lcn = LCN_EIO;
386 }
387 if (lcn != LCN_ENOENT)
388 ntfs_error(ni->vol->sb, "Failed with error code %lli.",
389 (long long)lcn);
390 return lcn;
391}
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450runlist_element *ntfs_attr_find_vcn_nolock(ntfs_inode *ni, const VCN vcn,
451 ntfs_attr_search_ctx *ctx)
452{
453 unsigned long flags;
454 runlist_element *rl;
455 int err = 0;
456 bool is_retry = false;
457
458 BUG_ON(!ni);
459 ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, with%s ctx.",
460 ni->mft_no, (unsigned long long)vcn, ctx ? "" : "out");
461 BUG_ON(!NInoNonResident(ni));
462 BUG_ON(vcn < 0);
463 if (!ni->runlist.rl) {
464 read_lock_irqsave(&ni->size_lock, flags);
465 if (!ni->allocated_size) {
466 read_unlock_irqrestore(&ni->size_lock, flags);
467 return ERR_PTR(-ENOENT);
468 }
469 read_unlock_irqrestore(&ni->size_lock, flags);
470 }
471retry_remap:
472 rl = ni->runlist.rl;
473 if (likely(rl && vcn >= rl[0].vcn)) {
474 while (likely(rl->length)) {
475 if (unlikely(vcn < rl[1].vcn)) {
476 if (likely(rl->lcn >= LCN_HOLE)) {
477 ntfs_debug("Done.");
478 return rl;
479 }
480 break;
481 }
482 rl++;
483 }
484 if (likely(rl->lcn != LCN_RL_NOT_MAPPED)) {
485 if (likely(rl->lcn == LCN_ENOENT))
486 err = -ENOENT;
487 else
488 err = -EIO;
489 }
490 }
491 if (!err && !is_retry) {
492
493
494
495
496 if (IS_ERR(ctx->mrec))
497 err = PTR_ERR(ctx->mrec);
498 else {
499
500
501
502
503 err = ntfs_map_runlist_nolock(ni, vcn, ctx);
504 if (likely(!err)) {
505 is_retry = true;
506 goto retry_remap;
507 }
508 }
509 if (err == -EINVAL)
510 err = -EIO;
511 } else if (!err)
512 err = -EIO;
513 if (err != -ENOENT)
514 ntfs_error(ni->vol->sb, "Failed with error code %i.", err);
515 return ERR_PTR(err);
516}
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575static int ntfs_attr_find(const ATTR_TYPE type, const ntfschar *name,
576 const u32 name_len, const IGNORE_CASE_BOOL ic,
577 const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
578{
579 ATTR_RECORD *a;
580 ntfs_volume *vol = ctx->ntfs_ino->vol;
581 ntfschar *upcase = vol->upcase;
582 u32 upcase_len = vol->upcase_len;
583
584
585
586
587
588 if (ctx->is_first) {
589 a = ctx->attr;
590 ctx->is_first = false;
591 } else
592 a = (ATTR_RECORD*)((u8*)ctx->attr +
593 le32_to_cpu(ctx->attr->length));
594 for (;; a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length))) {
595 if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
596 le32_to_cpu(ctx->mrec->bytes_allocated))
597 break;
598 ctx->attr = a;
599 if (unlikely(le32_to_cpu(a->type) > le32_to_cpu(type) ||
600 a->type == AT_END))
601 return -ENOENT;
602 if (unlikely(!a->length))
603 break;
604 if (a->type != type)
605 continue;
606
607
608
609
610 if (!name) {
611
612 if (a->name_length)
613 return -ENOENT;
614 } else if (!ntfs_are_names_equal(name, name_len,
615 (ntfschar*)((u8*)a + le16_to_cpu(a->name_offset)),
616 a->name_length, ic, upcase, upcase_len)) {
617 register int rc;
618
619 rc = ntfs_collate_names(name, name_len,
620 (ntfschar*)((u8*)a +
621 le16_to_cpu(a->name_offset)),
622 a->name_length, 1, IGNORE_CASE,
623 upcase, upcase_len);
624
625
626
627
628 if (rc == -1)
629 return -ENOENT;
630
631 if (rc)
632 continue;
633 rc = ntfs_collate_names(name, name_len,
634 (ntfschar*)((u8*)a +
635 le16_to_cpu(a->name_offset)),
636 a->name_length, 1, CASE_SENSITIVE,
637 upcase, upcase_len);
638 if (rc == -1)
639 return -ENOENT;
640 if (rc)
641 continue;
642 }
643
644
645
646
647
648 if (!val)
649 return 0;
650
651 else {
652 register int rc;
653
654 rc = memcmp(val, (u8*)a + le16_to_cpu(
655 a->data.resident.value_offset),
656 min_t(u32, val_len, le32_to_cpu(
657 a->data.resident.value_length)));
658
659
660
661
662 if (!rc) {
663 register u32 avl;
664
665 avl = le32_to_cpu(
666 a->data.resident.value_length);
667 if (val_len == avl)
668 return 0;
669 if (val_len < avl)
670 return -ENOENT;
671 } else if (rc < 0)
672 return -ENOENT;
673 }
674 }
675 ntfs_error(vol->sb, "Inode is corrupt. Run chkdsk.");
676 NVolSetErrors(vol);
677 return -EIO;
678}
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696int load_attribute_list(ntfs_volume *vol, runlist *runlist, u8 *al_start,
697 const s64 size, const s64 initialized_size)
698{
699 LCN lcn;
700 u8 *al = al_start;
701 u8 *al_end = al + initialized_size;
702 runlist_element *rl;
703 struct buffer_head *bh;
704 struct super_block *sb;
705 unsigned long block_size;
706 unsigned long block, max_block;
707 int err = 0;
708 unsigned char block_size_bits;
709
710 ntfs_debug("Entering.");
711 if (!vol || !runlist || !al || size <= 0 || initialized_size < 0 ||
712 initialized_size > size)
713 return -EINVAL;
714 if (!initialized_size) {
715 memset(al, 0, size);
716 return 0;
717 }
718 sb = vol->sb;
719 block_size = sb->s_blocksize;
720 block_size_bits = sb->s_blocksize_bits;
721 down_read(&runlist->lock);
722 rl = runlist->rl;
723 if (!rl) {
724 ntfs_error(sb, "Cannot read attribute list since runlist is "
725 "missing.");
726 goto err_out;
727 }
728
729 while (rl->length) {
730 lcn = ntfs_rl_vcn_to_lcn(rl, rl->vcn);
731 ntfs_debug("Reading vcn = 0x%llx, lcn = 0x%llx.",
732 (unsigned long long)rl->vcn,
733 (unsigned long long)lcn);
734
735 if (lcn < 0) {
736 ntfs_error(sb, "ntfs_rl_vcn_to_lcn() failed. Cannot "
737 "read attribute list.");
738 goto err_out;
739 }
740 block = lcn << vol->cluster_size_bits >> block_size_bits;
741
742 max_block = block + (rl->length << vol->cluster_size_bits >>
743 block_size_bits);
744 ntfs_debug("max_block = 0x%lx.", max_block);
745 do {
746 ntfs_debug("Reading block = 0x%lx.", block);
747 bh = sb_bread(sb, block);
748 if (!bh) {
749 ntfs_error(sb, "sb_bread() failed. Cannot "
750 "read attribute list.");
751 goto err_out;
752 }
753 if (al + block_size >= al_end)
754 goto do_final;
755 memcpy(al, bh->b_data, block_size);
756 brelse(bh);
757 al += block_size;
758 } while (++block < max_block);
759 rl++;
760 }
761 if (initialized_size < size) {
762initialize:
763 memset(al_start + initialized_size, 0, size - initialized_size);
764 }
765done:
766 up_read(&runlist->lock);
767 return err;
768do_final:
769 if (al < al_end) {
770
771
772
773
774
775
776
777
778 memcpy(al, bh->b_data, al_end - al);
779 brelse(bh);
780 if (initialized_size < size)
781 goto initialize;
782 goto done;
783 }
784 brelse(bh);
785
786 ntfs_error(sb, "Attribute list buffer overflow. Read attribute list "
787 "is truncated.");
788err_out:
789 err = -EIO;
790 goto done;
791}
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843static int ntfs_external_attr_find(const ATTR_TYPE type,
844 const ntfschar *name, const u32 name_len,
845 const IGNORE_CASE_BOOL ic, const VCN lowest_vcn,
846 const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
847{
848 ntfs_inode *base_ni, *ni;
849 ntfs_volume *vol;
850 ATTR_LIST_ENTRY *al_entry, *next_al_entry;
851 u8 *al_start, *al_end;
852 ATTR_RECORD *a;
853 ntfschar *al_name;
854 u32 al_name_len;
855 int err = 0;
856 static const char *es = " Unmount and run chkdsk.";
857
858 ni = ctx->ntfs_ino;
859 base_ni = ctx->base_ntfs_ino;
860 ntfs_debug("Entering for inode 0x%lx, type 0x%x.", ni->mft_no, type);
861 if (!base_ni) {
862
863 base_ni = ctx->base_ntfs_ino = ctx->ntfs_ino;
864 ctx->base_mrec = ctx->mrec;
865 }
866 if (ni == base_ni)
867 ctx->base_attr = ctx->attr;
868 if (type == AT_END)
869 goto not_found;
870 vol = base_ni->vol;
871 al_start = base_ni->attr_list;
872 al_end = al_start + base_ni->attr_list_size;
873 if (!ctx->al_entry)
874 ctx->al_entry = (ATTR_LIST_ENTRY*)al_start;
875
876
877
878
879 if (ctx->is_first) {
880 al_entry = ctx->al_entry;
881 ctx->is_first = false;
882 } else
883 al_entry = (ATTR_LIST_ENTRY*)((u8*)ctx->al_entry +
884 le16_to_cpu(ctx->al_entry->length));
885 for (;; al_entry = next_al_entry) {
886
887 if ((u8*)al_entry < base_ni->attr_list ||
888 (u8*)al_entry > al_end)
889 break;
890 ctx->al_entry = al_entry;
891
892 if ((u8*)al_entry == al_end)
893 goto not_found;
894 if (!al_entry->length)
895 break;
896 if ((u8*)al_entry + 6 > al_end || (u8*)al_entry +
897 le16_to_cpu(al_entry->length) > al_end)
898 break;
899 next_al_entry = (ATTR_LIST_ENTRY*)((u8*)al_entry +
900 le16_to_cpu(al_entry->length));
901 if (le32_to_cpu(al_entry->type) > le32_to_cpu(type))
902 goto not_found;
903 if (type != al_entry->type)
904 continue;
905
906
907
908
909 al_name_len = al_entry->name_length;
910 al_name = (ntfschar*)((u8*)al_entry + al_entry->name_offset);
911 if (!name) {
912 if (al_name_len)
913 goto not_found;
914 } else if (!ntfs_are_names_equal(al_name, al_name_len, name,
915 name_len, ic, vol->upcase, vol->upcase_len)) {
916 register int rc;
917
918 rc = ntfs_collate_names(name, name_len, al_name,
919 al_name_len, 1, IGNORE_CASE,
920 vol->upcase, vol->upcase_len);
921
922
923
924
925 if (rc == -1)
926 goto not_found;
927
928 if (rc)
929 continue;
930
931
932
933
934
935
936
937
938 rc = ntfs_collate_names(name, name_len, al_name,
939 al_name_len, 1, CASE_SENSITIVE,
940 vol->upcase, vol->upcase_len);
941 if (rc == -1)
942 goto not_found;
943 if (rc)
944 continue;
945 }
946
947
948
949
950
951
952 if (lowest_vcn && (u8*)next_al_entry >= al_start &&
953 (u8*)next_al_entry + 6 < al_end &&
954 (u8*)next_al_entry + le16_to_cpu(
955 next_al_entry->length) <= al_end &&
956 sle64_to_cpu(next_al_entry->lowest_vcn) <=
957 lowest_vcn &&
958 next_al_entry->type == al_entry->type &&
959 next_al_entry->name_length == al_name_len &&
960 ntfs_are_names_equal((ntfschar*)((u8*)
961 next_al_entry +
962 next_al_entry->name_offset),
963 next_al_entry->name_length,
964 al_name, al_name_len, CASE_SENSITIVE,
965 vol->upcase, vol->upcase_len))
966 continue;
967 if (MREF_LE(al_entry->mft_reference) == ni->mft_no) {
968 if (MSEQNO_LE(al_entry->mft_reference) != ni->seq_no) {
969 ntfs_error(vol->sb, "Found stale mft "
970 "reference in attribute list "
971 "of base inode 0x%lx.%s",
972 base_ni->mft_no, es);
973 err = -EIO;
974 break;
975 }
976 } else {
977
978 if (ni != base_ni)
979 unmap_extent_mft_record(ni);
980
981 if (MREF_LE(al_entry->mft_reference) ==
982 base_ni->mft_no) {
983 ni = ctx->ntfs_ino = base_ni;
984 ctx->mrec = ctx->base_mrec;
985 } else {
986
987 ctx->mrec = map_extent_mft_record(base_ni,
988 le64_to_cpu(
989 al_entry->mft_reference), &ni);
990 if (IS_ERR(ctx->mrec)) {
991 ntfs_error(vol->sb, "Failed to map "
992 "extent mft record "
993 "0x%lx of base inode "
994 "0x%lx.%s",
995 MREF_LE(al_entry->
996 mft_reference),
997 base_ni->mft_no, es);
998 err = PTR_ERR(ctx->mrec);
999 if (err == -ENOENT)
1000 err = -EIO;
1001
1002 ni = NULL;
1003 break;
1004 }
1005 ctx->ntfs_ino = ni;
1006 }
1007 ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1008 le16_to_cpu(ctx->mrec->attrs_offset));
1009 }
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025 a = ctx->attr;
1026
1027
1028
1029
1030do_next_attr_loop:
1031 if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
1032 le32_to_cpu(ctx->mrec->bytes_allocated))
1033 break;
1034 if (a->type == AT_END)
1035 break;
1036 if (!a->length)
1037 break;
1038 if (al_entry->instance != a->instance)
1039 goto do_next_attr;
1040
1041
1042
1043
1044
1045 if (al_entry->type != a->type)
1046 break;
1047 if (!ntfs_are_names_equal((ntfschar*)((u8*)a +
1048 le16_to_cpu(a->name_offset)), a->name_length,
1049 al_name, al_name_len, CASE_SENSITIVE,
1050 vol->upcase, vol->upcase_len))
1051 break;
1052 ctx->attr = a;
1053
1054
1055
1056
1057 if (!val || (!a->non_resident && le32_to_cpu(
1058 a->data.resident.value_length) == val_len &&
1059 !memcmp((u8*)a +
1060 le16_to_cpu(a->data.resident.value_offset),
1061 val, val_len))) {
1062 ntfs_debug("Done, found.");
1063 return 0;
1064 }
1065do_next_attr:
1066
1067 a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length));
1068 goto do_next_attr_loop;
1069 }
1070 if (!err) {
1071 ntfs_error(vol->sb, "Base inode 0x%lx contains corrupt "
1072 "attribute list attribute.%s", base_ni->mft_no,
1073 es);
1074 err = -EIO;
1075 }
1076 if (ni != base_ni) {
1077 if (ni)
1078 unmap_extent_mft_record(ni);
1079 ctx->ntfs_ino = base_ni;
1080 ctx->mrec = ctx->base_mrec;
1081 ctx->attr = ctx->base_attr;
1082 }
1083 if (err != -ENOMEM)
1084 NVolSetErrors(vol);
1085 return err;
1086not_found:
1087
1088
1089
1090
1091 if (type == AT_END) {
1092 ntfs_attr_reinit_search_ctx(ctx);
1093 return ntfs_attr_find(AT_END, name, name_len, ic, val, val_len,
1094 ctx);
1095 }
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109 if (ni != base_ni)
1110 unmap_extent_mft_record(ni);
1111 ctx->mrec = ctx->base_mrec;
1112 ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1113 le16_to_cpu(ctx->mrec->attrs_offset));
1114 ctx->is_first = true;
1115 ctx->ntfs_ino = base_ni;
1116 ctx->base_ntfs_ino = NULL;
1117 ctx->base_mrec = NULL;
1118 ctx->base_attr = NULL;
1119
1120
1121
1122
1123
1124
1125
1126 do {
1127 err = ntfs_attr_find(type, name, name_len, ic, val, val_len,
1128 ctx);
1129 } while (!err);
1130 ntfs_debug("Done, not found.");
1131 return err;
1132}
1133
1134
1135
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
1173int ntfs_attr_lookup(const ATTR_TYPE type, const ntfschar *name,
1174 const u32 name_len, const IGNORE_CASE_BOOL ic,
1175 const VCN lowest_vcn, const u8 *val, const u32 val_len,
1176 ntfs_attr_search_ctx *ctx)
1177{
1178 ntfs_inode *base_ni;
1179
1180 ntfs_debug("Entering.");
1181 BUG_ON(IS_ERR(ctx->mrec));
1182 if (ctx->base_ntfs_ino)
1183 base_ni = ctx->base_ntfs_ino;
1184 else
1185 base_ni = ctx->ntfs_ino;
1186
1187 BUG_ON(!base_ni);
1188 if (!NInoAttrList(base_ni) || type == AT_ATTRIBUTE_LIST)
1189 return ntfs_attr_find(type, name, name_len, ic, val, val_len,
1190 ctx);
1191 return ntfs_external_attr_find(type, name, name_len, ic, lowest_vcn,
1192 val, val_len, ctx);
1193}
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203static inline void ntfs_attr_init_search_ctx(ntfs_attr_search_ctx *ctx,
1204 ntfs_inode *ni, MFT_RECORD *mrec)
1205{
1206 *ctx = (ntfs_attr_search_ctx) {
1207 .mrec = mrec,
1208
1209 .attr = (ATTR_RECORD*)((u8*)mrec +
1210 le16_to_cpu(mrec->attrs_offset)),
1211 .is_first = true,
1212 .ntfs_ino = ni,
1213 };
1214}
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226void ntfs_attr_reinit_search_ctx(ntfs_attr_search_ctx *ctx)
1227{
1228 if (likely(!ctx->base_ntfs_ino)) {
1229
1230 ctx->is_first = true;
1231
1232 ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1233 le16_to_cpu(ctx->mrec->attrs_offset));
1234
1235
1236
1237
1238 ctx->al_entry = NULL;
1239 return;
1240 }
1241 if (ctx->ntfs_ino != ctx->base_ntfs_ino)
1242 unmap_extent_mft_record(ctx->ntfs_ino);
1243 ntfs_attr_init_search_ctx(ctx, ctx->base_ntfs_ino, ctx->base_mrec);
1244 return;
1245}
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(ntfs_inode *ni, MFT_RECORD *mrec)
1256{
1257 ntfs_attr_search_ctx *ctx;
1258
1259 ctx = kmem_cache_alloc(ntfs_attr_ctx_cache, GFP_NOFS);
1260 if (ctx)
1261 ntfs_attr_init_search_ctx(ctx, ni, mrec);
1262 return ctx;
1263}
1264
1265
1266
1267
1268
1269
1270
1271
1272void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx)
1273{
1274 if (ctx->base_ntfs_ino && ctx->ntfs_ino != ctx->base_ntfs_ino)
1275 unmap_extent_mft_record(ctx->ntfs_ino);
1276 kmem_cache_free(ntfs_attr_ctx_cache, ctx);
1277 return;
1278}
1279
1280#ifdef NTFS_RW
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292static ATTR_DEF *ntfs_attr_find_in_attrdef(const ntfs_volume *vol,
1293 const ATTR_TYPE type)
1294{
1295 ATTR_DEF *ad;
1296
1297 BUG_ON(!vol->attrdef);
1298 BUG_ON(!type);
1299 for (ad = vol->attrdef; (u8*)ad - (u8*)vol->attrdef <
1300 vol->attrdef_size && ad->type; ++ad) {
1301
1302 if (likely(le32_to_cpu(ad->type) < le32_to_cpu(type)))
1303 continue;
1304
1305 if (likely(ad->type == type))
1306 return ad;
1307
1308 break;
1309 }
1310
1311 ntfs_debug("Attribute type 0x%x not found in $AttrDef.",
1312 le32_to_cpu(type));
1313 return NULL;
1314}
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328int ntfs_attr_size_bounds_check(const ntfs_volume *vol, const ATTR_TYPE type,
1329 const s64 size)
1330{
1331 ATTR_DEF *ad;
1332
1333 BUG_ON(size < 0);
1334
1335
1336
1337
1338 if (unlikely(type == AT_ATTRIBUTE_LIST && size > 256 * 1024))
1339 return -ERANGE;
1340
1341 ad = ntfs_attr_find_in_attrdef(vol, type);
1342 if (unlikely(!ad))
1343 return -ENOENT;
1344
1345 if (((sle64_to_cpu(ad->min_size) > 0) &&
1346 size < sle64_to_cpu(ad->min_size)) ||
1347 ((sle64_to_cpu(ad->max_size) > 0) && size >
1348 sle64_to_cpu(ad->max_size)))
1349 return -ERANGE;
1350 return 0;
1351}
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364int ntfs_attr_can_be_non_resident(const ntfs_volume *vol, const ATTR_TYPE type)
1365{
1366 ATTR_DEF *ad;
1367
1368
1369 ad = ntfs_attr_find_in_attrdef(vol, type);
1370 if (unlikely(!ad))
1371 return -ENOENT;
1372
1373 if (ad->flags & ATTR_DEF_RESIDENT)
1374 return -EPERM;
1375 return 0;
1376}
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396int ntfs_attr_can_be_resident(const ntfs_volume *vol, const ATTR_TYPE type)
1397{
1398 if (type == AT_INDEX_ALLOCATION)
1399 return -EPERM;
1400 return 0;
1401}
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421int ntfs_attr_record_resize(MFT_RECORD *m, ATTR_RECORD *a, u32 new_size)
1422{
1423 ntfs_debug("Entering for new_size %u.", new_size);
1424
1425 if (new_size & 7)
1426 new_size = (new_size + 7) & ~7;
1427
1428 if (new_size != le32_to_cpu(a->length)) {
1429 u32 new_muse = le32_to_cpu(m->bytes_in_use) -
1430 le32_to_cpu(a->length) + new_size;
1431
1432 if (new_muse > le32_to_cpu(m->bytes_allocated))
1433 return -ENOSPC;
1434
1435 memmove((u8*)a + new_size, (u8*)a + le32_to_cpu(a->length),
1436 le32_to_cpu(m->bytes_in_use) - ((u8*)a -
1437 (u8*)m) - le32_to_cpu(a->length));
1438
1439 m->bytes_in_use = cpu_to_le32(new_muse);
1440
1441 if (new_size >= offsetof(ATTR_REC, length) + sizeof(a->length))
1442 a->length = cpu_to_le32(new_size);
1443 }
1444 return 0;
1445}
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465int ntfs_resident_attr_value_resize(MFT_RECORD *m, ATTR_RECORD *a,
1466 const u32 new_size)
1467{
1468 u32 old_size;
1469
1470
1471 if (ntfs_attr_record_resize(m, a,
1472 le16_to_cpu(a->data.resident.value_offset) + new_size))
1473 return -ENOSPC;
1474
1475
1476
1477
1478 old_size = le32_to_cpu(a->data.resident.value_length);
1479 if (new_size > old_size)
1480 memset((u8*)a + le16_to_cpu(a->data.resident.value_offset) +
1481 old_size, 0, new_size - old_size);
1482
1483 a->data.resident.value_length = cpu_to_le32(new_size);
1484 return 0;
1485}
1486
1487
1488
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
1521int ntfs_attr_make_non_resident(ntfs_inode *ni, const u32 data_size)
1522{
1523 s64 new_size;
1524 struct inode *vi = VFS_I(ni);
1525 ntfs_volume *vol = ni->vol;
1526 ntfs_inode *base_ni;
1527 MFT_RECORD *m;
1528 ATTR_RECORD *a;
1529 ntfs_attr_search_ctx *ctx;
1530 struct page *page;
1531 runlist_element *rl;
1532 u8 *kaddr;
1533 unsigned long flags;
1534 int mp_size, mp_ofs, name_ofs, arec_size, err, err2;
1535 u32 attr_size;
1536 u8 old_res_attr_flags;
1537
1538
1539 err = ntfs_attr_can_be_non_resident(vol, ni->type);
1540 if (unlikely(err)) {
1541 if (err == -EPERM)
1542 ntfs_debug("Attribute is not allowed to be "
1543 "non-resident.");
1544 else
1545 ntfs_debug("Attribute not defined on the NTFS "
1546 "volume!");
1547 return err;
1548 }
1549
1550
1551
1552
1553 BUG_ON(NInoCompressed(ni));
1554 BUG_ON(NInoEncrypted(ni));
1555
1556
1557
1558
1559 new_size = (data_size + vol->cluster_size - 1) &
1560 ~(vol->cluster_size - 1);
1561 if (new_size > 0) {
1562
1563
1564
1565
1566 page = find_or_create_page(vi->i_mapping, 0,
1567 mapping_gfp_mask(vi->i_mapping));
1568 if (unlikely(!page))
1569 return -ENOMEM;
1570
1571 rl = ntfs_cluster_alloc(vol, 0, new_size >>
1572 vol->cluster_size_bits, -1, DATA_ZONE, true);
1573 if (IS_ERR(rl)) {
1574 err = PTR_ERR(rl);
1575 ntfs_debug("Failed to allocate cluster%s, error code "
1576 "%i.", (new_size >>
1577 vol->cluster_size_bits) > 1 ? "s" : "",
1578 err);
1579 goto page_err_out;
1580 }
1581 } else {
1582 rl = NULL;
1583 page = NULL;
1584 }
1585
1586 mp_size = ntfs_get_size_for_mapping_pairs(vol, rl, 0, -1);
1587 if (unlikely(mp_size < 0)) {
1588 err = mp_size;
1589 ntfs_debug("Failed to get size for mapping pairs array, error "
1590 "code %i.", err);
1591 goto rl_err_out;
1592 }
1593 down_write(&ni->runlist.lock);
1594 if (!NInoAttr(ni))
1595 base_ni = ni;
1596 else
1597 base_ni = ni->ext.base_ntfs_ino;
1598 m = map_mft_record(base_ni);
1599 if (IS_ERR(m)) {
1600 err = PTR_ERR(m);
1601 m = NULL;
1602 ctx = NULL;
1603 goto err_out;
1604 }
1605 ctx = ntfs_attr_get_search_ctx(base_ni, m);
1606 if (unlikely(!ctx)) {
1607 err = -ENOMEM;
1608 goto err_out;
1609 }
1610 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
1611 CASE_SENSITIVE, 0, NULL, 0, ctx);
1612 if (unlikely(err)) {
1613 if (err == -ENOENT)
1614 err = -EIO;
1615 goto err_out;
1616 }
1617 m = ctx->mrec;
1618 a = ctx->attr;
1619 BUG_ON(NInoNonResident(ni));
1620 BUG_ON(a->non_resident);
1621
1622
1623
1624 if (NInoSparse(ni) || NInoCompressed(ni))
1625 name_ofs = (offsetof(ATTR_REC,
1626 data.non_resident.compressed_size) +
1627 sizeof(a->data.non_resident.compressed_size) +
1628 7) & ~7;
1629 else
1630 name_ofs = (offsetof(ATTR_REC,
1631 data.non_resident.compressed_size) + 7) & ~7;
1632 mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
1633
1634
1635
1636
1637 arec_size = (mp_ofs + mp_size + 7) & ~7;
1638
1639
1640
1641
1642 attr_size = le32_to_cpu(a->data.resident.value_length);
1643 BUG_ON(attr_size != data_size);
1644 if (page && !PageUptodate(page)) {
1645 kaddr = kmap_atomic(page);
1646 memcpy(kaddr, (u8*)a +
1647 le16_to_cpu(a->data.resident.value_offset),
1648 attr_size);
1649 memset(kaddr + attr_size, 0, PAGE_SIZE - attr_size);
1650 kunmap_atomic(kaddr);
1651 flush_dcache_page(page);
1652 SetPageUptodate(page);
1653 }
1654
1655 old_res_attr_flags = a->data.resident.flags;
1656
1657 err = ntfs_attr_record_resize(m, a, arec_size);
1658 if (unlikely(err))
1659 goto err_out;
1660
1661
1662
1663
1664 a->non_resident = 1;
1665
1666 if (a->name_length)
1667 memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
1668 a->name_length * sizeof(ntfschar));
1669 a->name_offset = cpu_to_le16(name_ofs);
1670
1671 a->data.non_resident.lowest_vcn = 0;
1672 a->data.non_resident.highest_vcn = cpu_to_sle64((new_size - 1) >>
1673 vol->cluster_size_bits);
1674 a->data.non_resident.mapping_pairs_offset = cpu_to_le16(mp_ofs);
1675 memset(&a->data.non_resident.reserved, 0,
1676 sizeof(a->data.non_resident.reserved));
1677 a->data.non_resident.allocated_size = cpu_to_sle64(new_size);
1678 a->data.non_resident.data_size =
1679 a->data.non_resident.initialized_size =
1680 cpu_to_sle64(attr_size);
1681 if (NInoSparse(ni) || NInoCompressed(ni)) {
1682 a->data.non_resident.compression_unit = 0;
1683 if (NInoCompressed(ni) || vol->major_ver < 3)
1684 a->data.non_resident.compression_unit = 4;
1685 a->data.non_resident.compressed_size =
1686 a->data.non_resident.allocated_size;
1687 } else
1688 a->data.non_resident.compression_unit = 0;
1689
1690 err = ntfs_mapping_pairs_build(vol, (u8*)a + mp_ofs,
1691 arec_size - mp_ofs, rl, 0, -1, NULL);
1692 if (unlikely(err)) {
1693 ntfs_debug("Failed to build mapping pairs, error code %i.",
1694 err);
1695 goto undo_err_out;
1696 }
1697
1698 ni->runlist.rl = rl;
1699 write_lock_irqsave(&ni->size_lock, flags);
1700 ni->allocated_size = new_size;
1701 if (NInoSparse(ni) || NInoCompressed(ni)) {
1702 ni->itype.compressed.size = ni->allocated_size;
1703 if (a->data.non_resident.compression_unit) {
1704 ni->itype.compressed.block_size = 1U << (a->data.
1705 non_resident.compression_unit +
1706 vol->cluster_size_bits);
1707 ni->itype.compressed.block_size_bits =
1708 ffs(ni->itype.compressed.block_size) -
1709 1;
1710 ni->itype.compressed.block_clusters = 1U <<
1711 a->data.non_resident.compression_unit;
1712 } else {
1713 ni->itype.compressed.block_size = 0;
1714 ni->itype.compressed.block_size_bits = 0;
1715 ni->itype.compressed.block_clusters = 0;
1716 }
1717 vi->i_blocks = ni->itype.compressed.size >> 9;
1718 } else
1719 vi->i_blocks = ni->allocated_size >> 9;
1720 write_unlock_irqrestore(&ni->size_lock, flags);
1721
1722
1723
1724
1725
1726
1727 NInoSetNonResident(ni);
1728
1729 flush_dcache_mft_record_page(ctx->ntfs_ino);
1730 mark_mft_record_dirty(ctx->ntfs_ino);
1731 ntfs_attr_put_search_ctx(ctx);
1732 unmap_mft_record(base_ni);
1733 up_write(&ni->runlist.lock);
1734 if (page) {
1735 set_page_dirty(page);
1736 unlock_page(page);
1737 put_page(page);
1738 }
1739 ntfs_debug("Done.");
1740 return 0;
1741undo_err_out:
1742
1743 a->non_resident = 0;
1744
1745 name_ofs = (offsetof(ATTR_RECORD, data.resident.reserved) +
1746 sizeof(a->data.resident.reserved) + 7) & ~7;
1747 if (a->name_length)
1748 memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
1749 a->name_length * sizeof(ntfschar));
1750 mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
1751 a->name_offset = cpu_to_le16(name_ofs);
1752 arec_size = (mp_ofs + attr_size + 7) & ~7;
1753
1754 err2 = ntfs_attr_record_resize(m, a, arec_size);
1755 if (unlikely(err2)) {
1756
1757
1758
1759
1760
1761
1762
1763
1764 arec_size = le32_to_cpu(a->length);
1765 if ((mp_ofs + attr_size) > arec_size) {
1766 err2 = attr_size;
1767 attr_size = arec_size - mp_ofs;
1768 ntfs_error(vol->sb, "Failed to undo partial resident "
1769 "to non-resident attribute "
1770 "conversion. Truncating inode 0x%lx, "
1771 "attribute type 0x%x from %i bytes to "
1772 "%i bytes to maintain metadata "
1773 "consistency. THIS MEANS YOU ARE "
1774 "LOSING %i BYTES DATA FROM THIS %s.",
1775 vi->i_ino,
1776 (unsigned)le32_to_cpu(ni->type),
1777 err2, attr_size, err2 - attr_size,
1778 ((ni->type == AT_DATA) &&
1779 !ni->name_len) ? "FILE": "ATTRIBUTE");
1780 write_lock_irqsave(&ni->size_lock, flags);
1781 ni->initialized_size = attr_size;
1782 i_size_write(vi, attr_size);
1783 write_unlock_irqrestore(&ni->size_lock, flags);
1784 }
1785 }
1786
1787 a->data.resident.value_length = cpu_to_le32(attr_size);
1788 a->data.resident.value_offset = cpu_to_le16(mp_ofs);
1789 a->data.resident.flags = old_res_attr_flags;
1790 memset(&a->data.resident.reserved, 0,
1791 sizeof(a->data.resident.reserved));
1792
1793 if (page) {
1794 kaddr = kmap_atomic(page);
1795 memcpy((u8*)a + mp_ofs, kaddr, attr_size);
1796 kunmap_atomic(kaddr);
1797 }
1798
1799 write_lock_irqsave(&ni->size_lock, flags);
1800 ni->allocated_size = arec_size - mp_ofs;
1801 write_unlock_irqrestore(&ni->size_lock, flags);
1802
1803 flush_dcache_mft_record_page(ctx->ntfs_ino);
1804 mark_mft_record_dirty(ctx->ntfs_ino);
1805err_out:
1806 if (ctx)
1807 ntfs_attr_put_search_ctx(ctx);
1808 if (m)
1809 unmap_mft_record(base_ni);
1810 ni->runlist.rl = NULL;
1811 up_write(&ni->runlist.lock);
1812rl_err_out:
1813 if (rl) {
1814 if (ntfs_cluster_free_from_rl(vol, rl) < 0) {
1815 ntfs_error(vol->sb, "Failed to release allocated "
1816 "cluster(s) in error code path. Run "
1817 "chkdsk to recover the lost "
1818 "cluster(s).");
1819 NVolSetErrors(vol);
1820 }
1821 ntfs_free(rl);
1822page_err_out:
1823 unlock_page(page);
1824 put_page(page);
1825 }
1826 if (err == -EINVAL)
1827 err = -EIO;
1828 return err;
1829}
1830
1831
1832
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
1890s64 ntfs_attr_extend_allocation(ntfs_inode *ni, s64 new_alloc_size,
1891 const s64 new_data_size, const s64 data_start)
1892{
1893 VCN vcn;
1894 s64 ll, allocated_size, start = data_start;
1895 struct inode *vi = VFS_I(ni);
1896 ntfs_volume *vol = ni->vol;
1897 ntfs_inode *base_ni;
1898 MFT_RECORD *m;
1899 ATTR_RECORD *a;
1900 ntfs_attr_search_ctx *ctx;
1901 runlist_element *rl, *rl2;
1902 unsigned long flags;
1903 int err, mp_size;
1904 u32 attr_len = 0;
1905 bool mp_rebuilt;
1906
1907#ifdef DEBUG
1908 read_lock_irqsave(&ni->size_lock, flags);
1909 allocated_size = ni->allocated_size;
1910 read_unlock_irqrestore(&ni->size_lock, flags);
1911 ntfs_debug("Entering for i_ino 0x%lx, attribute type 0x%x, "
1912 "old_allocated_size 0x%llx, "
1913 "new_allocated_size 0x%llx, new_data_size 0x%llx, "
1914 "data_start 0x%llx.", vi->i_ino,
1915 (unsigned)le32_to_cpu(ni->type),
1916 (unsigned long long)allocated_size,
1917 (unsigned long long)new_alloc_size,
1918 (unsigned long long)new_data_size,
1919 (unsigned long long)start);
1920#endif
1921retry_extend:
1922
1923
1924
1925
1926 if (NInoNonResident(ni)) {
1927 if (start > 0)
1928 start &= ~(s64)vol->cluster_size_mask;
1929 new_alloc_size = (new_alloc_size + vol->cluster_size - 1) &
1930 ~(s64)vol->cluster_size_mask;
1931 }
1932 BUG_ON(new_data_size >= 0 && new_data_size > new_alloc_size);
1933
1934 err = ntfs_attr_size_bounds_check(vol, ni->type, new_alloc_size);
1935 if (unlikely(err)) {
1936
1937 read_lock_irqsave(&ni->size_lock, flags);
1938 allocated_size = ni->allocated_size;
1939 read_unlock_irqrestore(&ni->size_lock, flags);
1940 if (start < 0 || start >= allocated_size) {
1941 if (err == -ERANGE) {
1942 ntfs_error(vol->sb, "Cannot extend allocation "
1943 "of inode 0x%lx, attribute "
1944 "type 0x%x, because the new "
1945 "allocation would exceed the "
1946 "maximum allowed size for "
1947 "this attribute type.",
1948 vi->i_ino, (unsigned)
1949 le32_to_cpu(ni->type));
1950 } else {
1951 ntfs_error(vol->sb, "Cannot extend allocation "
1952 "of inode 0x%lx, attribute "
1953 "type 0x%x, because this "
1954 "attribute type is not "
1955 "defined on the NTFS volume. "
1956 "Possible corruption! You "
1957 "should run chkdsk!",
1958 vi->i_ino, (unsigned)
1959 le32_to_cpu(ni->type));
1960 }
1961 }
1962
1963 if (err == -ERANGE)
1964 err = -EFBIG;
1965 else
1966 err = -EIO;
1967 return err;
1968 }
1969 if (!NInoAttr(ni))
1970 base_ni = ni;
1971 else
1972 base_ni = ni->ext.base_ntfs_ino;
1973
1974
1975
1976
1977 down_write(&ni->runlist.lock);
1978 m = map_mft_record(base_ni);
1979 if (IS_ERR(m)) {
1980 err = PTR_ERR(m);
1981 m = NULL;
1982 ctx = NULL;
1983 goto err_out;
1984 }
1985 ctx = ntfs_attr_get_search_ctx(base_ni, m);
1986 if (unlikely(!ctx)) {
1987 err = -ENOMEM;
1988 goto err_out;
1989 }
1990 read_lock_irqsave(&ni->size_lock, flags);
1991 allocated_size = ni->allocated_size;
1992 read_unlock_irqrestore(&ni->size_lock, flags);
1993
1994
1995
1996
1997 vcn = NInoNonResident(ni) ? allocated_size >> vol->cluster_size_bits :
1998 0;
1999
2000
2001
2002
2003
2004
2005 if (unlikely(new_alloc_size <= allocated_size)) {
2006 ntfs_debug("Allocated size already exceeds requested size.");
2007 new_alloc_size = allocated_size;
2008 if (new_data_size < 0)
2009 goto done;
2010
2011
2012
2013
2014 vcn = 0;
2015 }
2016 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
2017 CASE_SENSITIVE, vcn, NULL, 0, ctx);
2018 if (unlikely(err)) {
2019 if (err == -ENOENT)
2020 err = -EIO;
2021 goto err_out;
2022 }
2023 m = ctx->mrec;
2024 a = ctx->attr;
2025
2026 if (a->non_resident)
2027 goto do_non_resident_extend;
2028 BUG_ON(NInoNonResident(ni));
2029
2030 attr_len = le32_to_cpu(a->data.resident.value_length);
2031
2032
2033
2034
2035
2036 if (new_alloc_size < vol->mft_record_size &&
2037 !ntfs_attr_record_resize(m, a,
2038 le16_to_cpu(a->data.resident.value_offset) +
2039 new_alloc_size)) {
2040
2041 write_lock_irqsave(&ni->size_lock, flags);
2042 ni->allocated_size = le32_to_cpu(a->length) -
2043 le16_to_cpu(a->data.resident.value_offset);
2044 write_unlock_irqrestore(&ni->size_lock, flags);
2045 if (new_data_size >= 0) {
2046 BUG_ON(new_data_size < attr_len);
2047 a->data.resident.value_length =
2048 cpu_to_le32((u32)new_data_size);
2049 }
2050 goto flush_done;
2051 }
2052
2053
2054
2055
2056
2057
2058
2059
2060 ntfs_attr_put_search_ctx(ctx);
2061 unmap_mft_record(base_ni);
2062 up_write(&ni->runlist.lock);
2063
2064
2065
2066
2067 err = ntfs_attr_make_non_resident(ni, attr_len);
2068 if (likely(!err))
2069 goto retry_extend;
2070
2071
2072
2073
2074
2075 if (unlikely(err != -EPERM && err != -ENOSPC)) {
2076
2077 read_lock_irqsave(&ni->size_lock, flags);
2078 allocated_size = ni->allocated_size;
2079 read_unlock_irqrestore(&ni->size_lock, flags);
2080 if (start < 0 || start >= allocated_size)
2081 ntfs_error(vol->sb, "Cannot extend allocation of "
2082 "inode 0x%lx, attribute type 0x%x, "
2083 "because the conversion from resident "
2084 "to non-resident attribute failed "
2085 "with error code %i.", vi->i_ino,
2086 (unsigned)le32_to_cpu(ni->type), err);
2087 if (err != -ENOMEM)
2088 err = -EIO;
2089 goto conv_err_out;
2090 }
2091
2092 read_lock_irqsave(&ni->size_lock, flags);
2093 allocated_size = ni->allocated_size;
2094 read_unlock_irqrestore(&ni->size_lock, flags);
2095 if (start < 0 || start >= allocated_size) {
2096 if (err == -ENOSPC)
2097 ntfs_error(vol->sb, "Not enough space in the mft "
2098 "record/on disk for the non-resident "
2099 "attribute value. This case is not "
2100 "implemented yet.");
2101 else
2102 ntfs_error(vol->sb, "This attribute type may not be "
2103 "non-resident. This case is not "
2104 "implemented yet.");
2105 }
2106 err = -EOPNOTSUPP;
2107 goto conv_err_out;
2108#if 0
2109
2110 if (!err)
2111 goto do_resident_extend;
2112
2113
2114
2115
2116
2117
2118 if (ni->type == AT_ATTRIBUTE_LIST ||
2119 ni->type == AT_STANDARD_INFORMATION) {
2120
2121
2122 err = -EOPNOTSUPP;
2123 if (!err)
2124 goto do_resident_extend;
2125 goto err_out;
2126 }
2127
2128
2129
2130 err = -EOPNOTSUPP;
2131 if (!err)
2132 goto do_resident_extend;
2133
2134 goto err_out;
2135#endif
2136do_non_resident_extend:
2137 BUG_ON(!NInoNonResident(ni));
2138 if (new_alloc_size == allocated_size) {
2139 BUG_ON(vcn);
2140 goto alloc_done;
2141 }
2142
2143
2144
2145
2146
2147
2148
2149
2150 if ((start >= 0 && start <= allocated_size) || ni->type != AT_DATA ||
2151 !NVolSparseEnabled(vol) || NInoSparseDisabled(ni))
2152 goto skip_sparse;
2153
2154
2155 ntfs_debug("Inserting holes is not-implemented yet. Falling back to "
2156 "allocating real clusters instead.");
2157skip_sparse:
2158 rl = ni->runlist.rl;
2159 if (likely(rl)) {
2160
2161 while (rl->length)
2162 rl++;
2163 }
2164
2165 if (unlikely(!rl || rl->lcn == LCN_RL_NOT_MAPPED ||
2166 (rl->lcn == LCN_ENOENT && rl > ni->runlist.rl &&
2167 (rl-1)->lcn == LCN_RL_NOT_MAPPED))) {
2168 if (!rl && !allocated_size)
2169 goto first_alloc;
2170 rl = ntfs_mapping_pairs_decompress(vol, a, ni->runlist.rl);
2171 if (IS_ERR(rl)) {
2172 err = PTR_ERR(rl);
2173 if (start < 0 || start >= allocated_size)
2174 ntfs_error(vol->sb, "Cannot extend allocation "
2175 "of inode 0x%lx, attribute "
2176 "type 0x%x, because the "
2177 "mapping of a runlist "
2178 "fragment failed with error "
2179 "code %i.", vi->i_ino,
2180 (unsigned)le32_to_cpu(ni->type),
2181 err);
2182 if (err != -ENOMEM)
2183 err = -EIO;
2184 goto err_out;
2185 }
2186 ni->runlist.rl = rl;
2187
2188 while (rl->length)
2189 rl++;
2190 }
2191
2192
2193
2194
2195
2196
2197
2198
2199 while (rl->lcn < 0 && rl > ni->runlist.rl)
2200 rl--;
2201first_alloc:
2202
2203
2204
2205 rl2 = ntfs_cluster_alloc(vol, allocated_size >> vol->cluster_size_bits,
2206 (new_alloc_size - allocated_size) >>
2207 vol->cluster_size_bits, (rl && (rl->lcn >= 0)) ?
2208 rl->lcn + rl->length : -1, DATA_ZONE, true);
2209 if (IS_ERR(rl2)) {
2210 err = PTR_ERR(rl2);
2211 if (start < 0 || start >= allocated_size)
2212 ntfs_error(vol->sb, "Cannot extend allocation of "
2213 "inode 0x%lx, attribute type 0x%x, "
2214 "because the allocation of clusters "
2215 "failed with error code %i.", vi->i_ino,
2216 (unsigned)le32_to_cpu(ni->type), err);
2217 if (err != -ENOMEM && err != -ENOSPC)
2218 err = -EIO;
2219 goto err_out;
2220 }
2221 rl = ntfs_runlists_merge(ni->runlist.rl, rl2);
2222 if (IS_ERR(rl)) {
2223 err = PTR_ERR(rl);
2224 if (start < 0 || start >= allocated_size)
2225 ntfs_error(vol->sb, "Cannot extend allocation of "
2226 "inode 0x%lx, attribute type 0x%x, "
2227 "because the runlist merge failed "
2228 "with error code %i.", vi->i_ino,
2229 (unsigned)le32_to_cpu(ni->type), err);
2230 if (err != -ENOMEM)
2231 err = -EIO;
2232 if (ntfs_cluster_free_from_rl(vol, rl2)) {
2233 ntfs_error(vol->sb, "Failed to release allocated "
2234 "cluster(s) in error code path. Run "
2235 "chkdsk to recover the lost "
2236 "cluster(s).");
2237 NVolSetErrors(vol);
2238 }
2239 ntfs_free(rl2);
2240 goto err_out;
2241 }
2242 ni->runlist.rl = rl;
2243 ntfs_debug("Allocated 0x%llx clusters.", (long long)(new_alloc_size -
2244 allocated_size) >> vol->cluster_size_bits);
2245
2246 ll = sle64_to_cpu(a->data.non_resident.lowest_vcn);
2247 rl2 = ntfs_rl_find_vcn_nolock(rl, ll);
2248 BUG_ON(!rl2);
2249 BUG_ON(!rl2->length);
2250 BUG_ON(rl2->lcn < LCN_HOLE);
2251 mp_rebuilt = false;
2252
2253 mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, -1);
2254 if (unlikely(mp_size <= 0)) {
2255 err = mp_size;
2256 if (start < 0 || start >= allocated_size)
2257 ntfs_error(vol->sb, "Cannot extend allocation of "
2258 "inode 0x%lx, attribute type 0x%x, "
2259 "because determining the size for the "
2260 "mapping pairs failed with error code "
2261 "%i.", vi->i_ino,
2262 (unsigned)le32_to_cpu(ni->type), err);
2263 err = -EIO;
2264 goto undo_alloc;
2265 }
2266
2267 attr_len = le32_to_cpu(a->length);
2268 err = ntfs_attr_record_resize(m, a, mp_size +
2269 le16_to_cpu(a->data.non_resident.mapping_pairs_offset));
2270 if (unlikely(err)) {
2271 BUG_ON(err != -ENOSPC);
2272
2273
2274
2275
2276
2277
2278 if (start < 0 || start >= allocated_size)
2279 ntfs_error(vol->sb, "Not enough space in the mft "
2280 "record for the extended attribute "
2281 "record. This case is not "
2282 "implemented yet.");
2283 err = -EOPNOTSUPP;
2284 goto undo_alloc;
2285 }
2286 mp_rebuilt = true;
2287
2288 err = ntfs_mapping_pairs_build(vol, (u8*)a +
2289 le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
2290 mp_size, rl2, ll, -1, NULL);
2291 if (unlikely(err)) {
2292 if (start < 0 || start >= allocated_size)
2293 ntfs_error(vol->sb, "Cannot extend allocation of "
2294 "inode 0x%lx, attribute type 0x%x, "
2295 "because building the mapping pairs "
2296 "failed with error code %i.", vi->i_ino,
2297 (unsigned)le32_to_cpu(ni->type), err);
2298 err = -EIO;
2299 goto undo_alloc;
2300 }
2301
2302 a->data.non_resident.highest_vcn = cpu_to_sle64((new_alloc_size >>
2303 vol->cluster_size_bits) - 1);
2304
2305
2306
2307
2308 if (a->data.non_resident.lowest_vcn) {
2309
2310
2311
2312
2313 flush_dcache_mft_record_page(ctx->ntfs_ino);
2314 mark_mft_record_dirty(ctx->ntfs_ino);
2315 ntfs_attr_reinit_search_ctx(ctx);
2316 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
2317 CASE_SENSITIVE, 0, NULL, 0, ctx);
2318 if (unlikely(err))
2319 goto restore_undo_alloc;
2320
2321 a = ctx->attr;
2322 }
2323 write_lock_irqsave(&ni->size_lock, flags);
2324 ni->allocated_size = new_alloc_size;
2325 a->data.non_resident.allocated_size = cpu_to_sle64(new_alloc_size);
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336 if (NInoSparse(ni) || NInoCompressed(ni)) {
2337 ni->itype.compressed.size += new_alloc_size - allocated_size;
2338 a->data.non_resident.compressed_size =
2339 cpu_to_sle64(ni->itype.compressed.size);
2340 vi->i_blocks = ni->itype.compressed.size >> 9;
2341 } else
2342 vi->i_blocks = new_alloc_size >> 9;
2343 write_unlock_irqrestore(&ni->size_lock, flags);
2344alloc_done:
2345 if (new_data_size >= 0) {
2346 BUG_ON(new_data_size <
2347 sle64_to_cpu(a->data.non_resident.data_size));
2348 a->data.non_resident.data_size = cpu_to_sle64(new_data_size);
2349 }
2350flush_done:
2351
2352 flush_dcache_mft_record_page(ctx->ntfs_ino);
2353 mark_mft_record_dirty(ctx->ntfs_ino);
2354done:
2355 ntfs_attr_put_search_ctx(ctx);
2356 unmap_mft_record(base_ni);
2357 up_write(&ni->runlist.lock);
2358 ntfs_debug("Done, new_allocated_size 0x%llx.",
2359 (unsigned long long)new_alloc_size);
2360 return new_alloc_size;
2361restore_undo_alloc:
2362 if (start < 0 || start >= allocated_size)
2363 ntfs_error(vol->sb, "Cannot complete extension of allocation "
2364 "of inode 0x%lx, attribute type 0x%x, because "
2365 "lookup of first attribute extent failed with "
2366 "error code %i.", vi->i_ino,
2367 (unsigned)le32_to_cpu(ni->type), err);
2368 if (err == -ENOENT)
2369 err = -EIO;
2370 ntfs_attr_reinit_search_ctx(ctx);
2371 if (ntfs_attr_lookup(ni->type, ni->name, ni->name_len, CASE_SENSITIVE,
2372 allocated_size >> vol->cluster_size_bits, NULL, 0,
2373 ctx)) {
2374 ntfs_error(vol->sb, "Failed to find last attribute extent of "
2375 "attribute in error code path. Run chkdsk to "
2376 "recover.");
2377 write_lock_irqsave(&ni->size_lock, flags);
2378 ni->allocated_size = new_alloc_size;
2379
2380
2381
2382
2383
2384 if (NInoSparse(ni) || NInoCompressed(ni)) {
2385 ni->itype.compressed.size += new_alloc_size -
2386 allocated_size;
2387 vi->i_blocks = ni->itype.compressed.size >> 9;
2388 } else
2389 vi->i_blocks = new_alloc_size >> 9;
2390 write_unlock_irqrestore(&ni->size_lock, flags);
2391 ntfs_attr_put_search_ctx(ctx);
2392 unmap_mft_record(base_ni);
2393 up_write(&ni->runlist.lock);
2394
2395
2396
2397
2398 NVolSetErrors(vol);
2399 return err;
2400 }
2401 ctx->attr->data.non_resident.highest_vcn = cpu_to_sle64(
2402 (allocated_size >> vol->cluster_size_bits) - 1);
2403undo_alloc:
2404 ll = allocated_size >> vol->cluster_size_bits;
2405 if (ntfs_cluster_free(ni, ll, -1, ctx) < 0) {
2406 ntfs_error(vol->sb, "Failed to release allocated cluster(s) "
2407 "in error code path. Run chkdsk to recover "
2408 "the lost cluster(s).");
2409 NVolSetErrors(vol);
2410 }
2411 m = ctx->mrec;
2412 a = ctx->attr;
2413
2414
2415
2416
2417
2418
2419 if (ntfs_rl_truncate_nolock(vol, &ni->runlist, ll) || IS_ERR(m)) {
2420 ntfs_error(vol->sb, "Failed to %s in error code path. Run "
2421 "chkdsk to recover.", IS_ERR(m) ?
2422 "restore attribute search context" :
2423 "truncate attribute runlist");
2424 NVolSetErrors(vol);
2425 } else if (mp_rebuilt) {
2426 if (ntfs_attr_record_resize(m, a, attr_len)) {
2427 ntfs_error(vol->sb, "Failed to restore attribute "
2428 "record in error code path. Run "
2429 "chkdsk to recover.");
2430 NVolSetErrors(vol);
2431 } else {
2432 if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu(
2433 a->data.non_resident.
2434 mapping_pairs_offset), attr_len -
2435 le16_to_cpu(a->data.non_resident.
2436 mapping_pairs_offset), rl2, ll, -1,
2437 NULL)) {
2438 ntfs_error(vol->sb, "Failed to restore "
2439 "mapping pairs array in error "
2440 "code path. Run chkdsk to "
2441 "recover.");
2442 NVolSetErrors(vol);
2443 }
2444 flush_dcache_mft_record_page(ctx->ntfs_ino);
2445 mark_mft_record_dirty(ctx->ntfs_ino);
2446 }
2447 }
2448err_out:
2449 if (ctx)
2450 ntfs_attr_put_search_ctx(ctx);
2451 if (m)
2452 unmap_mft_record(base_ni);
2453 up_write(&ni->runlist.lock);
2454conv_err_out:
2455 ntfs_debug("Failed. Returning error code %i.", err);
2456 return err;
2457}
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479int ntfs_attr_set(ntfs_inode *ni, const s64 ofs, const s64 cnt, const u8 val)
2480{
2481 ntfs_volume *vol = ni->vol;
2482 struct address_space *mapping;
2483 struct page *page;
2484 u8 *kaddr;
2485 pgoff_t idx, end;
2486 unsigned start_ofs, end_ofs, size;
2487
2488 ntfs_debug("Entering for ofs 0x%llx, cnt 0x%llx, val 0x%hx.",
2489 (long long)ofs, (long long)cnt, val);
2490 BUG_ON(ofs < 0);
2491 BUG_ON(cnt < 0);
2492 if (!cnt)
2493 goto done;
2494
2495
2496
2497
2498 BUG_ON(NInoCompressed(ni));
2499 BUG_ON(NInoEncrypted(ni));
2500 mapping = VFS_I(ni)->i_mapping;
2501
2502 idx = ofs >> PAGE_SHIFT;
2503 start_ofs = ofs & ~PAGE_MASK;
2504
2505 end = ofs + cnt;
2506 end_ofs = end & ~PAGE_MASK;
2507
2508 if (unlikely(end > i_size_read(VFS_I(ni)))) {
2509 ntfs_error(vol->sb, "Request exceeds end of attribute.");
2510 return -ESPIPE;
2511 }
2512 end >>= PAGE_SHIFT;
2513
2514 if (start_ofs) {
2515 page = read_mapping_page(mapping, idx, NULL);
2516 if (IS_ERR(page)) {
2517 ntfs_error(vol->sb, "Failed to read first partial "
2518 "page (error, index 0x%lx).", idx);
2519 return PTR_ERR(page);
2520 }
2521
2522
2523
2524
2525 size = PAGE_SIZE;
2526 if (idx == end)
2527 size = end_ofs;
2528 kaddr = kmap_atomic(page);
2529 memset(kaddr + start_ofs, val, size - start_ofs);
2530 flush_dcache_page(page);
2531 kunmap_atomic(kaddr);
2532 set_page_dirty(page);
2533 put_page(page);
2534 balance_dirty_pages_ratelimited(mapping);
2535 cond_resched();
2536 if (idx == end)
2537 goto done;
2538 idx++;
2539 }
2540
2541 for (; idx < end; idx++) {
2542
2543 page = grab_cache_page(mapping, idx);
2544 if (unlikely(!page)) {
2545 ntfs_error(vol->sb, "Insufficient memory to grab "
2546 "page (index 0x%lx).", idx);
2547 return -ENOMEM;
2548 }
2549 kaddr = kmap_atomic(page);
2550 memset(kaddr, val, PAGE_SIZE);
2551 flush_dcache_page(page);
2552 kunmap_atomic(kaddr);
2553
2554
2555
2556
2557 if (page_has_buffers(page)) {
2558 struct buffer_head *bh, *head;
2559
2560 bh = head = page_buffers(page);
2561 do {
2562 set_buffer_uptodate(bh);
2563 } while ((bh = bh->b_this_page) != head);
2564 }
2565
2566 SetPageUptodate(page);
2567
2568
2569
2570
2571 set_page_dirty(page);
2572
2573 unlock_page(page);
2574 put_page(page);
2575 balance_dirty_pages_ratelimited(mapping);
2576 cond_resched();
2577 }
2578
2579 if (end_ofs) {
2580 page = read_mapping_page(mapping, idx, NULL);
2581 if (IS_ERR(page)) {
2582 ntfs_error(vol->sb, "Failed to read last partial page "
2583 "(error, index 0x%lx).", idx);
2584 return PTR_ERR(page);
2585 }
2586 kaddr = kmap_atomic(page);
2587 memset(kaddr, val, end_ofs);
2588 flush_dcache_page(page);
2589 kunmap_atomic(kaddr);
2590 set_page_dirty(page);
2591 put_page(page);
2592 balance_dirty_pages_ratelimited(mapping);
2593 cond_resched();
2594 }
2595done:
2596 ntfs_debug("Done.");
2597 return 0;
2598}
2599
2600#endif
2601