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