Debian Codegraph @

fs/ntfs3/attrib.c

raw
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 *
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
5 *
6 * TODO: Merge attr_set_size/attr_data_get_block/attr_allocate_frame?
7 */
8
9 #include <linux/fs.h>
10 #include <linux/slab.h>
11 #include <linux/kernel.h>
12
13 #include "debug.h"
14 #include "ntfs.h"
15 #include "ntfs_fs.h"
16
17 /*
18 * You can set external NTFS_MIN_LOG2_OF_CLUMP/NTFS_MAX_LOG2_OF_CLUMP to manage
19 * preallocate algorithm.
20 */
21 #ifndef NTFS_MIN_LOG2_OF_CLUMP
22 #define NTFS_MIN_LOG2_OF_CLUMP 16
23 #endif
24
25 #ifndef NTFS_MAX_LOG2_OF_CLUMP
26 #define NTFS_MAX_LOG2_OF_CLUMP 26
27 #endif
28
29 // 16M
30 #define NTFS_CLUMP_MIN (1 << (NTFS_MIN_LOG2_OF_CLUMP + 8))
31 // 16G
32 #define NTFS_CLUMP_MAX (1ull << (NTFS_MAX_LOG2_OF_CLUMP + 8))
33
34 static inline u64 get_pre_allocated(u64 size)
35 {
36 u32 clump;
37 u8 align_shift;
38 u64 ret;
39
40 if (size <= NTFS_CLUMP_MIN) {
41 clump = 1 << NTFS_MIN_LOG2_OF_CLUMP;
42 align_shift = NTFS_MIN_LOG2_OF_CLUMP;
43 } else if (size >= NTFS_CLUMP_MAX) {
44 clump = 1 << NTFS_MAX_LOG2_OF_CLUMP;
45 align_shift = NTFS_MAX_LOG2_OF_CLUMP;
46 } else {
47 align_shift = NTFS_MIN_LOG2_OF_CLUMP - 1 +
48 __ffs(size >> (8 + NTFS_MIN_LOG2_OF_CLUMP));
49 clump = 1u << align_shift;
50 }
51
52 ret = (((size + clump - 1) >> align_shift)) << align_shift;
53
54 return ret;
55 }
56
57 /*
58 * attr_load_runs - Load all runs stored in @attr.
59 */
60 static int attr_load_runs(struct ATTRIB *attr, struct ntfs_inode *ni,
61 struct runs_tree *run, const CLST *vcn)
62 {
63 int err;
64 CLST svcn = le64_to_cpu(attr->nres.svcn);
65 CLST evcn = le64_to_cpu(attr->nres.evcn);
66 u32 asize;
67 u16 run_off;
68
69 if (svcn >= evcn + 1 || run_is_mapped_full(run, svcn, evcn))
70 return 0;
71
72 if (vcn && (evcn < *vcn || *vcn < svcn))
73 return -EINVAL;
74
75 asize = le32_to_cpu(attr->size);
76 run_off = le16_to_cpu(attr->nres.run_off);
77
78 if (run_off > asize)
79 return -EINVAL;
80
81 err = run_unpack_ex(run, ni->mi.sbi, ni->mi.rno, svcn, evcn,
82 vcn ? *vcn : svcn, Add2Ptr(attr, run_off),
83 asize - run_off);
84 if (err < 0)
85 return err;
86
87 return 0;
88 }
89
90 /*
91 * run_deallocate_ex - Deallocate clusters.
92 */
93 static int run_deallocate_ex(struct ntfs_sb_info *sbi, struct runs_tree *run,
94 CLST vcn, CLST len, CLST *done, bool trim,
95 struct runs_tree *run_da)
96 {
97 int err = 0;
98 CLST vcn_next, vcn0 = vcn, lcn, clen, dn = 0;
99 size_t idx;
100
101 if (!len)
102 goto out;
103
104 if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx)) {
105 failed:
106 run_truncate(run, vcn0);
107 err = -EINVAL;
108 goto out;
109 }
110
111 for (;;) {
112 if (clen > len)
113 clen = len;
114
115 if (!clen) {
116 err = -EINVAL;
117 goto out;
118 }
119
120 if (lcn != SPARSE_LCN) {
121 if (sbi) {
122 /* mark bitmap range [lcn + clen) as free and trim clusters. */
123 mark_as_free_ex(sbi, lcn, clen, trim);
124
125 if (run_da) {
126 CLST da_len;
127 if (!run_remove_range(run_da, vcn, clen,
128 &da_len)) {
129 err = -ENOMEM;
130 goto failed;
131 }
132 ntfs_sub_da(sbi, da_len);
133 }
134 }
135 dn += clen;
136 }
137
138 len -= clen;
139 if (!len)
140 break;
141
142 vcn_next = vcn + clen;
143 if (!run_get_entry(run, ++idx, &vcn, &lcn, &clen) ||
144 vcn != vcn_next) {
145 /* Save memory - don't load entire run. */
146 goto failed;
147 }
148 }
149
150 out:
151 if (done)
152 *done += dn;
153
154 return err;
155 }
156
157 /*
158 * attr_allocate_clusters - Find free space, mark it as used and store in @run.
159 */
160 int attr_allocate_clusters(struct ntfs_sb_info *sbi, struct runs_tree *run,
161 struct runs_tree *run_da, CLST vcn, CLST lcn,
162 CLST len, CLST *pre_alloc, enum ALLOCATE_OPT opt,
163 CLST *alen, const size_t fr, CLST *new_lcn,
164 CLST *new_len)
165 {
166 int err;
167 CLST flen, vcn0 = vcn, pre = pre_alloc ? *pre_alloc : 0;
168 size_t cnt = run->count;
169
170 for (;;) {
171 err = ntfs_look_for_free_space(sbi, lcn, len + pre, &lcn, &flen,
172 opt);
173
174 if (err == -ENOSPC && pre) {
175 pre = 0;
176 if (*pre_alloc)
177 *pre_alloc = 0;
178 continue;
179 }
180
181 if (err == -ENOSPC && new_len && vcn - vcn0) {
182 /* Keep already allocated clusters. */
183 *alen = vcn - vcn0;
184 return 0;
185 }
186
187 if (err)
188 goto out;
189
190 if (vcn == vcn0) {
191 /* Return the first fragment. */
192 if (new_lcn)
193 *new_lcn = lcn;
194 if (new_len)
195 *new_len = flen;
196 }
197
198 /* Add new fragment into run storage. */
199 if (!run_add_entry(run, vcn, lcn, flen, opt & ALLOCATE_MFT)) {
200 undo_alloc:
201 /* Undo last 'ntfs_look_for_free_space' */
202 mark_as_free_ex(sbi, lcn, len, false);
203 err = -ENOMEM;
204 goto out;
205 }
206
207 if (run_da) {
208 CLST da_len;
209 if (!run_remove_range(run_da, vcn, flen, &da_len)) {
210 goto undo_alloc;
211 }
212 ntfs_sub_da(sbi, da_len);
213 }
214
215 if (opt & ALLOCATE_ZERO) {
216 u8 shift = sbi->cluster_bits - SECTOR_SHIFT;
217
218 err = blkdev_issue_zeroout(sbi->sb->s_bdev,
219 (sector_t)lcn << shift,
220 (sector_t)flen << shift,
221 GFP_NOFS, 0);
222 if (err)
223 goto out;
224 }
225
226 vcn += flen;
227
228 if (flen >= len || (opt & ALLOCATE_MFT) ||
229 (opt & ALLOCATE_ONE_FR) || (fr && run->count - cnt >= fr)) {
230 *alen = vcn - vcn0;
231 return 0;
232 }
233
234 len -= flen;
235 }
236
237 out:
238 /* Undo 'ntfs_look_for_free_space' */
239 if (vcn - vcn0) {
240 run_deallocate_ex(sbi, run, vcn0, vcn - vcn0, NULL, false,
241 run_da);
242 run_truncate(run, vcn0);
243 }
244
245 return err;
246 }
247
248 /*
249 * attr_make_nonresident
250 *
251 * If page is not NULL - it is already contains resident data
252 * and locked (called from ni_write_frame()).
253 */
254 int attr_make_nonresident(struct ntfs_inode *ni, struct ATTRIB *attr,
255 struct ATTR_LIST_ENTRY *le, struct mft_inode *mi,
256 u64 new_size, struct runs_tree *run,
257 struct ATTRIB **ins_attr, struct page *page)
258 {
259 struct ntfs_sb_info *sbi;
260 struct ATTRIB *attr_s;
261 struct MFT_REC *rec;
262 u32 used, asize, rsize, aoff;
263 bool is_data;
264 CLST len, alen;
265 char *next;
266 int err;
267
268 if (attr->non_res) {
269 *ins_attr = attr;
270 return 0;
271 }
272
273 sbi = mi->sbi;
274 rec = mi->mrec;
275 attr_s = NULL;
276 used = le32_to_cpu(rec->used);
277 asize = le32_to_cpu(attr->size);
278 next = Add2Ptr(attr, asize);
279 aoff = PtrOffset(rec, attr);
280 rsize = le32_to_cpu(attr->res.data_size);
281 is_data = attr->type == ATTR_DATA && !attr->name_len;
282
283 /* len - how many clusters required to store 'rsize' bytes */
284 if (is_attr_compressed(attr)) {
285 u8 shift = sbi->cluster_bits + NTFS_LZNT_CUNIT;
286 len = ((rsize + (1u << shift) - 1) >> shift) << NTFS_LZNT_CUNIT;
287 } else {
288 len = bytes_to_cluster(sbi, rsize);
289 }
290
291 run_init(run);
292
293 /* Make a copy of original attribute. */
294 attr_s = kmemdup(attr, asize, GFP_NOFS);
295 if (!attr_s) {
296 err = -ENOMEM;
297 goto out;
298 }
299
300 if (!len) {
301 /* Empty resident -> Empty nonresident. */
302 alen = 0;
303 } else {
304 const char *data = resident_data(attr);
305
306 err = attr_allocate_clusters(sbi, run, NULL, 0, 0, len, NULL,
307 ALLOCATE_DEF, &alen, 0, NULL,
308 NULL);
309 if (err)
310 goto out1;
311
312 if (!rsize) {
313 /* Empty resident -> Non empty nonresident. */
314 } else if (!is_data) {
315 err = ntfs_sb_write_run(sbi, run, 0, data, rsize, 0);
316 if (err)
317 goto out2;
318 } else if (!page) {
319 struct address_space *mapping = ni->vfs_inode.i_mapping;
320 struct folio *folio;
321
322 folio = __filemap_get_folio(
323 mapping, 0, FGP_LOCK | FGP_ACCESSED | FGP_CREAT,
324 mapping_gfp_mask(mapping));
325 if (IS_ERR(folio)) {
326 err = PTR_ERR(folio);
327 goto out2;
328 }
329 folio_fill_tail(folio, 0, data, rsize);
330 folio_mark_uptodate(folio);
331 folio_mark_dirty(folio);
332 folio_unlock(folio);
333 folio_put(folio);
334 }
335 }
336
337 /* Remove original attribute. */
338 used -= asize;
339 memmove(attr, Add2Ptr(attr, asize), used - aoff);
340 rec->used = cpu_to_le32(used);
341 mi->dirty = true;
342 if (le)
343 al_remove_le(ni, le);
344
345 err = ni_insert_nonresident(ni, attr_s->type, attr_name(attr_s),
346 attr_s->name_len, run, 0, alen,
347 attr_s->flags, &attr, NULL, NULL);
348 if (err)
349 goto out3;
350
351 kfree(attr_s);
352 attr->nres.data_size = cpu_to_le64(rsize);
353 attr->nres.valid_size = attr->nres.data_size;
354
355 *ins_attr = attr;
356
357 if (is_data)
358 ni->ni_flags &= ~NI_FLAG_RESIDENT;
359
360 /* Resident attribute becomes non resident. */
361 return 0;
362
363 out3:
364 attr = Add2Ptr(rec, aoff);
365 memmove(next, attr, used - aoff);
366 memcpy(attr, attr_s, asize);
367 rec->used = cpu_to_le32(used + asize);
368 mi->dirty = true;
369 out2:
370 /* Undo: do not trim new allocated clusters. */
371 run_deallocate(sbi, run, false);
372 run_close(run);
373 out1:
374 kfree(attr_s);
375 out:
376 return err;
377 }
378
379 /*
380 * attr_set_size_res - Helper for attr_set_size().
381 */
382 static int attr_set_size_res(struct ntfs_inode *ni, struct ATTRIB *attr,
383 struct ATTR_LIST_ENTRY *le, struct mft_inode *mi,
384 u64 new_size, struct runs_tree *run,
385 struct ATTRIB **ins_attr)
386 {
387 struct ntfs_sb_info *sbi = mi->sbi;
388 struct MFT_REC *rec = mi->mrec;
389 u32 used = le32_to_cpu(rec->used);
390 u32 asize = le32_to_cpu(attr->size);
391 u32 aoff = PtrOffset(rec, attr);
392 u32 rsize = le32_to_cpu(attr->res.data_size);
393 u32 tail = used - aoff - asize;
394 char *next = Add2Ptr(attr, asize);
395 s64 dsize = ALIGN(new_size, 8) - ALIGN(rsize, 8);
396
397 if (dsize < 0) {
398 memmove(next + dsize, next, tail);
399 } else if (dsize > 0) {
400 if (used + dsize > sbi->max_bytes_per_attr)
401 return attr_make_nonresident(ni, attr, le, mi, new_size,
402 run, ins_attr, NULL);
403
404 memmove(next + dsize, next, tail);
405 memset(next, 0, dsize);
406 }
407
408 if (new_size > rsize)
409 memset(Add2Ptr(resident_data(attr), rsize), 0,
410 new_size - rsize);
411
412 rec->used = cpu_to_le32(used + dsize);
413 attr->size = cpu_to_le32(asize + dsize);
414 attr->res.data_size = cpu_to_le32(new_size);
415 mi->dirty = true;
416 *ins_attr = attr;
417
418 return 0;
419 }
420
421 /*
422 * attr_set_size_ex - Change the size of attribute.
423 *
424 * Extend:
425 * - Sparse/compressed: No allocated clusters.
426 * - Normal: Append allocated and preallocated new clusters.
427 * Shrink:
428 * - No deallocate if @keep_prealloc is set.
429 */
430 int attr_set_size_ex(struct ntfs_inode *ni, enum ATTR_TYPE type,
431 const __le16 *name, u8 name_len, struct runs_tree *run,
432 u64 new_size, const u64 *new_valid, bool keep_prealloc,
433 struct ATTRIB **ret, bool no_da)
434 {
435 int err = 0;
436 struct ntfs_sb_info *sbi = ni->mi.sbi;
437 u8 cluster_bits = sbi->cluster_bits;
438 bool is_mft = ni->mi.rno == MFT_REC_MFT && type == ATTR_DATA &&
439 !name_len;
440 u64 old_valid, old_size, old_alloc, new_alloc_tmp;
441 u64 new_alloc = 0;
442 struct ATTRIB *attr = NULL, *attr_b;
443 struct ATTR_LIST_ENTRY *le, *le_b;
444 struct mft_inode *mi, *mi_b;
445 CLST alen, vcn, lcn, new_alen, old_alen, svcn, evcn;
446 CLST next_svcn, pre_alloc = -1, done = 0;
447 bool is_ext = false, is_bad = false;
448 bool dirty = false;
449 struct runs_tree *run_da = run == &ni->file.run ? &ni->file.run_da :
450 NULL;
451 bool da = !is_mft && sbi->options->delalloc && run_da && !no_da;
452 u32 align;
453 struct MFT_REC *rec;
454
455 again:
456 alen = 0;
457 le_b = NULL;
458 attr_b = ni_find_attr(ni, NULL, &le_b, type, name, name_len, NULL,
459 &mi_b);
460 if (!attr_b) {
461 err = -ENOENT;
462 goto bad_inode;
463 }
464
465 if (!attr_b->non_res) {
466 err = attr_set_size_res(ni, attr_b, le_b, mi_b, new_size, run,
467 &attr_b);
468 if (err)
469 return err;
470
471 /* Return if file is still resident. */
472 if (!attr_b->non_res) {
473 dirty = true;
474 goto ok1;
475 }
476
477 /* Layout of records may be changed, so do a full search. */
478 goto again;
479 }
480
481 is_ext = is_attr_ext(attr_b);
482 align = sbi->cluster_size;
483 if (is_ext) {
484 align <<= attr_b->nres.c_unit;
485 keep_prealloc = false;
486 da = false;
487 }
488
489 old_valid = le64_to_cpu(attr_b->nres.valid_size);
490 old_size = le64_to_cpu(attr_b->nres.data_size);
491 old_alloc = le64_to_cpu(attr_b->nres.alloc_size);
492
493 again_1:
494 old_alen = old_alloc >> cluster_bits;
495
496 new_alloc = (new_size + align - 1) & ~(u64)(align - 1);
497 new_alen = new_alloc >> cluster_bits;
498
499 if (keep_prealloc && new_size < old_size) {
500 attr_b->nres.data_size = cpu_to_le64(new_size);
501 mi_b->dirty = dirty = true;
502 goto ok;
503 }
504
505 if (da &&
506 (vcn = old_alen + run_len(&ni->file.run_da), new_alen > vcn)) {
507 /* Resize up normal file. Delay new clusters allocation. */
508 alen = new_alen - vcn;
509
510 if (ntfs_check_free_space(sbi, alen, 0, true)) {
511 if (!run_add_entry(&ni->file.run_da, vcn, SPARSE_LCN,
512 alen, false)) {
513 err = -ENOMEM;
514 goto out;
515 }
516
517 ntfs_add_da(sbi, alen);
518 goto ok1;
519 }
520 }
521
522 if (!keep_prealloc && run_da && run_da->count &&
523 (vcn = run_get_max_vcn(run_da), new_alen < vcn)) {
524 /* Shrink delayed clusters. */
525
526 /* Try to remove fragment from delay allocated run. */
527 if (!run_remove_range(run_da, new_alen, vcn - new_alen,
528 &alen)) {
529 err = -ENOMEM;
530 goto out;
531 }
532
533 ntfs_sub_da(sbi, alen);
534 }
535
536 vcn = old_alen - 1;
537
538 svcn = le64_to_cpu(attr_b->nres.svcn);
539 evcn = le64_to_cpu(attr_b->nres.evcn);
540
541 if (svcn <= vcn && vcn <= evcn) {
542 attr = attr_b;
543 le = le_b;
544 mi = mi_b;
545 } else if (!le_b) {
546 err = -EINVAL;
547 goto bad_inode;
548 } else {
549 le = le_b;
550 attr = ni_find_attr(ni, attr_b, &le, type, name, name_len, &vcn,
551 &mi);
552 if (!attr) {
553 err = -EINVAL;
554 goto bad_inode;
555 }
556
557 next_le_1:
558 svcn = le64_to_cpu(attr->nres.svcn);
559 evcn = le64_to_cpu(attr->nres.evcn);
560 }
561 /*
562 * Here we have:
563 * attr,mi,le - last attribute segment (containing 'vcn').
564 * attr_b,mi_b,le_b - base (primary) attribute segment.
565 */
566 next_le:
567 rec = mi->mrec;
568 err = attr_load_runs(attr, ni, run, NULL);
569 if (err)
570 goto out;
571
572 if (new_size > old_size) {
573 CLST to_allocate;
574 size_t free;
575
576 if (new_alloc <= old_alloc) {
577 attr_b->nres.data_size = cpu_to_le64(new_size);
578 mi_b->dirty = dirty = true;
579 goto ok;
580 }
581
582 /*
583 * Add clusters. In simple case we have to:
584 * - allocate space (vcn, lcn, len)
585 * - update packed run in 'mi'
586 * - update attr->nres.evcn
587 * - update attr_b->nres.data_size/attr_b->nres.alloc_size
588 */
589 to_allocate = new_alen - old_alen;
590 add_alloc_in_same_attr_seg:
591 lcn = 0;
592 if (is_mft) {
593 /* MFT allocates clusters from MFT zone. */
594 pre_alloc = 0;
595 } else if (is_ext) {
596 /* No preallocate for sparse/compress. */
597 pre_alloc = 0;
598 } else if (pre_alloc == -1) {
599 pre_alloc = 0;
600 if (type == ATTR_DATA && !name_len &&
601 sbi->options->prealloc) {
602 pre_alloc = bytes_to_cluster(
603 sbi, get_pre_allocated(
604 new_size)) -
605 new_alen;
606 }
607
608 /* Get the last LCN to allocate from. */
609 if (old_alen &&
610 !run_lookup_entry(run, vcn, &lcn, NULL, NULL)) {
611 lcn = SPARSE_LCN;
612 }
613
614 if (lcn == SPARSE_LCN)
615 lcn = 0;
616 else if (lcn)
617 lcn += 1;
618
619 free = wnd_zeroes(&sbi->used.bitmap);
620 if (to_allocate > free) {
621 err = -ENOSPC;
622 goto out;
623 }
624
625 if (pre_alloc && to_allocate + pre_alloc > free)
626 pre_alloc = 0;
627 }
628
629 vcn = old_alen;
630
631 if (is_ext) {
632 if (!run_add_entry(run, vcn, SPARSE_LCN, to_allocate,
633 false)) {
634 err = -ENOMEM;
635 goto out;
636 }
637 alen = to_allocate;
638 } else {
639 /* ~3 bytes per fragment. */
640 err = attr_allocate_clusters(
641 sbi, run, run_da, vcn, lcn, to_allocate,
642 &pre_alloc,
643 is_mft ? ALLOCATE_MFT : ALLOCATE_DEF, &alen,
644 is_mft ? 0 :
645 (sbi->record_size -
646 le32_to_cpu(rec->used) + 8) /
647 3 +
648 1,
649 NULL, NULL);
650 if (err)
651 goto out;
652 }
653
654 done += alen;
655 vcn += alen;
656 if (to_allocate > alen)
657 to_allocate -= alen;
658 else
659 to_allocate = 0;
660
661 pack_runs:
662 err = mi_pack_runs(mi, attr, run, vcn - svcn);
663 if (err)
664 goto undo_1;
665
666 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
667 new_alloc_tmp = (u64)next_svcn << cluster_bits;
668 attr_b->nres.alloc_size = cpu_to_le64(new_alloc_tmp);
669 mi_b->dirty = dirty = true;
670
671 if (next_svcn >= vcn && !to_allocate) {
672 /* Normal way. Update attribute and exit. */
673 attr_b->nres.data_size = cpu_to_le64(new_size);
674 goto ok;
675 }
676
677 /* At least two MFT to avoid recursive loop. */
678 if (is_mft && next_svcn == vcn &&
679 ((u64)done << sbi->cluster_bits) >= 2 * sbi->record_size) {
680 new_size = new_alloc_tmp;
681 attr_b->nres.data_size = attr_b->nres.alloc_size;
682 goto ok;
683 }
684
685 if (le32_to_cpu(rec->used) < sbi->record_size) {
686 old_alen = next_svcn;
687 evcn = old_alen - 1;
688 goto add_alloc_in_same_attr_seg;
689 }
690
691 attr_b->nres.data_size = attr_b->nres.alloc_size;
692 if (new_alloc_tmp < old_valid)
693 attr_b->nres.valid_size = attr_b->nres.data_size;
694
695 if (type == ATTR_LIST) {
696 err = ni_expand_list(ni);
697 if (err)
698 goto undo_2;
699 if (next_svcn < vcn)
700 goto pack_runs;
701
702 /* Layout of records is changed. */
703 goto again;
704 }
705
706 if (!ni->attr_list.size) {
707 err = ni_create_attr_list(ni);
708 /* In case of error layout of records is not changed. */
709 if (err)
710 goto undo_2;
711 /* Layout of records is changed. */
712 }
713
714 if (next_svcn >= vcn) {
715 /* This is MFT data, repeat. */
716 goto again;
717 }
718
719 /* Insert new attribute segment. */
720 err = ni_insert_nonresident(ni, type, name, name_len, run,
721 next_svcn, vcn - next_svcn,
722 attr_b->flags, &attr, &mi, NULL);
723
724 /*
725 * Layout of records maybe changed.
726 * Find base attribute to update.
727 */
728 le_b = NULL;
729 attr_b = ni_find_attr(ni, NULL, &le_b, type, name, name_len,
730 NULL, &mi_b);
731 if (!attr_b) {
732 err = -EINVAL;
733 goto bad_inode;
734 }
735
736 if (err) {
737 /* ni_insert_nonresident failed. */
738 attr = NULL;
739 goto undo_2;
740 }
741
742 /* keep runs for $MFT::$ATTR_DATA and $MFT::$ATTR_BITMAP. */
743 if (ni->mi.rno != MFT_REC_MFT)
744 run_truncate_head(run, evcn + 1);
745
746 svcn = le64_to_cpu(attr->nres.svcn);
747 evcn = le64_to_cpu(attr->nres.evcn);
748
749 /*
750 * Attribute is in consistency state.
751 * Save this point to restore to if next steps fail.
752 */
753 old_valid = old_size = old_alloc = (u64)vcn << cluster_bits;
754 attr_b->nres.valid_size = attr_b->nres.data_size =
755 attr_b->nres.alloc_size = cpu_to_le64(old_size);
756 mi_b->dirty = dirty = true;
757 goto again_1;
758 }
759
760 if (new_size != old_size ||
761 (new_alloc != old_alloc && !keep_prealloc)) {
762 /*
763 * Truncate clusters. In simple case we have to:
764 * - update packed run in 'mi'
765 * - update attr->nres.evcn
766 * - update attr_b->nres.data_size/attr_b->nres.alloc_size
767 * - mark and trim clusters as free (vcn, lcn, len)
768 */
769 CLST dlen = 0;
770
771 vcn = max(svcn, new_alen);
772 new_alloc_tmp = (u64)vcn << cluster_bits;
773
774 if (vcn > svcn) {
775 err = mi_pack_runs(mi, attr, run, vcn - svcn);
776 if (err)
777 goto out;
778 } else if (le && le->vcn) {
779 u16 le_sz = le16_to_cpu(le->size);
780
781 /*
782 * NOTE: List entries for one attribute are always
783 * the same size. We deal with last entry (vcn==0)
784 * and it is not first in entries array
785 * (list entry for std attribute always first).
786 * So it is safe to step back.
787 */
788 mi_remove_attr(NULL, mi, attr);
789
790 if (!al_remove_le(ni, le)) {
791 err = -EINVAL;
792 goto bad_inode;
793 }
794
795 le = (struct ATTR_LIST_ENTRY *)((u8 *)le - le_sz);
796 } else {
797 attr->nres.evcn = cpu_to_le64((u64)vcn - 1);
798 mi->dirty = true;
799 }
800
801 attr_b->nres.alloc_size = cpu_to_le64(new_alloc_tmp);
802
803 if (vcn == new_alen) {
804 attr_b->nres.data_size = cpu_to_le64(new_size);
805 if (new_size < old_valid)
806 attr_b->nres.valid_size =
807 attr_b->nres.data_size;
808 } else {
809 if (new_alloc_tmp <=
810 le64_to_cpu(attr_b->nres.data_size))
811 attr_b->nres.data_size =
812 attr_b->nres.alloc_size;
813 if (new_alloc_tmp <
814 le64_to_cpu(attr_b->nres.valid_size))
815 attr_b->nres.valid_size =
816 attr_b->nres.alloc_size;
817 }
818 mi_b->dirty = dirty = true;
819
820 err = run_deallocate_ex(sbi, run, vcn, evcn - vcn + 1, &dlen,
821 true, run_da);
822 if (err)
823 goto out;
824
825 if (is_ext) {
826 /* dlen - really deallocated clusters. */
827 le64_sub_cpu(&attr_b->nres.total_size,
828 (u64)dlen << cluster_bits);
829 }
830
831 run_truncate(run, vcn);
832
833 if (new_alloc_tmp <= new_alloc)
834 goto ok;
835
836 old_size = new_alloc_tmp;
837 vcn = svcn - 1;
838
839 if (le == le_b) {
840 attr = attr_b;
841 mi = mi_b;
842 evcn = svcn - 1;
843 svcn = 0;
844 goto next_le;
845 }
846
847 if (le->type != type || le->name_len != name_len ||
848 memcmp(le_name(le), name, name_len * sizeof(short))) {
849 err = -EINVAL;
850 goto bad_inode;
851 }
852
853 err = ni_load_mi(ni, le, &mi);
854 if (err)
855 goto out;
856
857 attr = mi_find_attr(ni, mi, NULL, type, name, name_len,
858 &le->id);
859 if (!attr) {
860 err = -EINVAL;
861 goto bad_inode;
862 }
863 goto next_le_1;
864 }
865
866 ok:
867 if (new_valid) {
868 __le64 valid = cpu_to_le64(min(*new_valid, new_size));
869
870 if (attr_b->nres.valid_size != valid) {
871 attr_b->nres.valid_size = valid;
872 mi_b->dirty = true;
873 }
874 }
875
876 ok1:
877 if (ret)
878 *ret = attr_b;
879
880 if (((type == ATTR_DATA && !name_len) ||
881 (type == ATTR_ALLOC && name == I30_NAME))) {
882 /* Update inode_set_bytes. */
883 if (attr_b->non_res &&
884 inode_get_bytes(&ni->vfs_inode) != new_alloc) {
885 inode_set_bytes(&ni->vfs_inode, new_alloc);
886 dirty = true;
887 }
888
889 i_size_write(&ni->vfs_inode, new_size);
890
891 /* Don't forget to update duplicate information in parent. */
892 if (dirty) {
893 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
894 mark_inode_dirty(&ni->vfs_inode);
895 }
896 }
897
898 return 0;
899
900 undo_2:
901 vcn -= alen;
902 attr_b->nres.data_size = cpu_to_le64(old_size);
903 attr_b->nres.valid_size = cpu_to_le64(old_valid);
904 attr_b->nres.alloc_size = cpu_to_le64(old_alloc);
905
906 /* Restore 'attr' and 'mi'. */
907 if (attr)
908 goto restore_run;
909
910 if (le64_to_cpu(attr_b->nres.svcn) <= svcn &&
911 svcn <= le64_to_cpu(attr_b->nres.evcn)) {
912 attr = attr_b;
913 le = le_b;
914 mi = mi_b;
915 } else if (!le_b) {
916 err = -EINVAL;
917 goto bad_inode;
918 } else {
919 le = le_b;
920 attr = ni_find_attr(ni, attr_b, &le, type, name, name_len,
921 &svcn, &mi);
922 if (!attr)
923 goto bad_inode;
924 }
925
926 restore_run:
927 if (mi_pack_runs(mi, attr, run, evcn - svcn + 1))
928 is_bad = true;
929
930 undo_1:
931 run_deallocate_ex(sbi, run, vcn, alen, NULL, false, run_da);
932
933 run_truncate(run, vcn);
934 out:
935 if (is_bad) {
936 bad_inode:
937 _ntfs_bad_inode(&ni->vfs_inode);
938 }
939 return err;
940 }
941
942 /*
943 * attr_data_get_block - Returns 'lcn' and 'len' for given 'vcn'.
944 *
945 * @new == NULL means just to get current mapping for 'vcn'
946 * @new != NULL means allocate real cluster if 'vcn' maps to hole
947 * @zero - zeroout new allocated clusters
948 *
949 * NOTE:
950 * - @new != NULL is called only for sparsed or compressed attributes.
951 * - new allocated clusters are zeroed via blkdev_issue_zeroout.
952 */
953 int attr_data_get_block(struct ntfs_inode *ni, CLST vcn, CLST clen, CLST *lcn,
954 CLST *len, bool *new, bool zero, void **res, bool no_da)
955 {
956 int err;
957
958 if (new)
959 *new = false;
960 if (res)
961 *res = NULL;
962
963 /* Try to find in cache. */
964 down_read(&ni->file.run_lock);
965 if (!no_da && run_lookup_entry(&ni->file.run_da, vcn, lcn, len, NULL)) {
966 /* The requested vcn is delay allocated. */
967 *lcn = DELALLOC_LCN;
968 } else if (run_lookup_entry(&ni->file.run, vcn, lcn, len, NULL)) {
969 /* The requested vcn is known in current run. */
970 } else {
971 *len = 0;
972 }
973 up_read(&ni->file.run_lock);
974
975 if (*len && (*lcn != SPARSE_LCN || !new))
976 return 0; /* Fast normal way without allocation. */
977
978 /* No cluster in cache or we need to allocate cluster in hole. */
979 ni_lock(ni);
980 down_write(&ni->file.run_lock);
981
982 err = attr_data_get_block_locked(ni, vcn, clen, lcn, len, new, zero,
983 res, no_da);
984
985 up_write(&ni->file.run_lock);
986 ni_unlock(ni);
987
988 return err;
989 }
990
991 /*
992 * attr_data_get_block_locked - Helper for attr_data_get_block.
993 */
994 int attr_data_get_block_locked(struct ntfs_inode *ni, CLST vcn, CLST clen,
995 CLST *lcn, CLST *len, bool *new, bool zero,
996 void **res, bool no_da)
997 {
998 int err = 0;
999 struct ntfs_sb_info *sbi = ni->mi.sbi;
1000 struct runs_tree *run = &ni->file.run;
1001 struct runs_tree *run_da = &ni->file.run_da;
1002 bool da = sbi->options->delalloc && !no_da;
1003 u8 cluster_bits;
1004 struct ATTRIB *attr, *attr_b;
1005 struct ATTR_LIST_ENTRY *le, *le_b;
1006 struct mft_inode *mi, *mi_b;
1007 CLST hint, svcn, to_alloc, evcn1, next_svcn, asize, end, vcn0;
1008 CLST alloc, evcn;
1009 unsigned fr;
1010 u64 total_size, total_size0;
1011 int step;
1012
1013 again:
1014 if (da && run_lookup_entry(run_da, vcn, lcn, len, NULL)) {
1015 /* The requested vcn is delay allocated. */
1016 *lcn = DELALLOC_LCN;
1017 } else if (run_lookup_entry(run, vcn, lcn, len, NULL)) {
1018 /* The requested vcn is known in current run. */
1019 } else {
1020 *len = 0;
1021 }
1022
1023 if (*len) {
1024 if (*lcn != SPARSE_LCN || !new)
1025 goto out; /* normal way without allocation. */
1026 if (clen > *len)
1027 clen = *len;
1028 }
1029
1030 cluster_bits = sbi->cluster_bits;
1031 step = 0;
1032
1033 le_b = NULL;
1034 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
1035 if (!attr_b) {
1036 err = -ENOENT;
1037 goto out;
1038 }
1039
1040 if (!attr_b->non_res) {
1041 u32 data_size = le32_to_cpu(attr_b->res.data_size);
1042 *lcn = RESIDENT_LCN;
1043 *len = data_size;
1044 if (res && data_size) {
1045 *res = kmemdup(resident_data(attr_b), data_size,
1046 GFP_KERNEL);
1047 if (!*res)
1048 err = -ENOMEM;
1049 }
1050 goto out;
1051 }
1052
1053 asize = le64_to_cpu(attr_b->nres.alloc_size) >> cluster_bits;
1054 if (vcn >= asize) {
1055 if (new) {
1056 err = -EINVAL;
1057 } else {
1058 *len = 1;
1059 *lcn = EOF_LCN;
1060 }
1061 goto out;
1062 }
1063
1064 svcn = le64_to_cpu(attr_b->nres.svcn);
1065 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
1066
1067 attr = attr_b;
1068 le = le_b;
1069 mi = mi_b;
1070
1071 if (le_b && (vcn < svcn || evcn1 <= vcn)) {
1072 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
1073 &mi);
1074 if (!attr) {
1075 err = -EINVAL;
1076 goto out;
1077 }
1078 svcn = le64_to_cpu(attr->nres.svcn);
1079 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
1080 }
1081
1082 /* Load in cache actual information. */
1083 err = attr_load_runs(attr, ni, run, NULL);
1084 if (err)
1085 goto out;
1086
1087 /* Check for compressed frame. */
1088 err = attr_is_frame_compressed(ni, attr_b, vcn >> NTFS_LZNT_CUNIT,
1089 &hint, run);
1090 if (err)
1091 goto out;
1092
1093 if (hint) {
1094 /* if frame is compressed - don't touch it. */
1095 *lcn = COMPRESSED_LCN;
1096 /* length to the end of frame. */
1097 *len = NTFS_LZNT_CLUSTERS - (vcn & (NTFS_LZNT_CLUSTERS - 1));
1098 err = 0;
1099 goto out;
1100 }
1101
1102 if (!*len) {
1103 if (run_lookup_entry(run, vcn, lcn, len, NULL)) {
1104 if (*lcn != SPARSE_LCN || !new)
1105 goto ok; /* Slow normal way without allocation. */
1106
1107 if (clen > *len)
1108 clen = *len;
1109 } else if (!new) {
1110 /* Here we may return -ENOENT.
1111 * In any case caller gets zero length. */
1112 goto ok;
1113 }
1114 }
1115
1116 if (!is_attr_ext(attr_b)) {
1117 /* The code below only for sparsed or compressed attributes. */
1118 err = -EINVAL;
1119 goto out;
1120 }
1121
1122 vcn0 = vcn;
1123 to_alloc = clen;
1124 fr = (sbi->record_size - le32_to_cpu(mi->mrec->used) + 8) / 3 + 1;
1125 /* Allocate frame aligned clusters.
1126 * ntfs.sys usually uses 16 clusters per frame for sparsed or compressed.
1127 * ntfs3 uses 1 cluster per frame for new created sparsed files. */
1128 if (attr_b->nres.c_unit) {
1129 CLST clst_per_frame = 1u << attr_b->nres.c_unit;
1130 CLST cmask = ~(clst_per_frame - 1);
1131
1132 /* Get frame aligned vcn and to_alloc. */
1133 vcn = vcn0 & cmask;
1134 to_alloc = ((vcn0 + clen + clst_per_frame - 1) & cmask) - vcn;
1135 if (fr < clst_per_frame)
1136 fr = clst_per_frame;
1137 if (vcn != vcn0)
1138 zero = true;
1139
1140 /* Check if 'vcn' and 'vcn0' in different attribute segments. */
1141 if (vcn < svcn || evcn1 <= vcn) {
1142 struct ATTRIB *attr2;
1143 /* Load runs for truncated vcn. */
1144 attr2 = ni_find_attr(ni, attr_b, &le_b, ATTR_DATA, NULL,
1145 0, &vcn, &mi);
1146 if (!attr2) {
1147 err = -EINVAL;
1148 goto out;
1149 }
1150 evcn1 = le64_to_cpu(attr2->nres.evcn) + 1;
1151 err = attr_load_runs(attr2, ni, run, NULL);
1152 if (err)
1153 goto out;
1154 }
1155
1156 if (vcn0 < svcn || evcn1 <= vcn0) {
1157 struct ATTRIB *attr2;
1158
1159 attr2 = ni_find_attr(ni, attr_b, &le_b, ATTR_DATA, NULL,
1160 0, &vcn0, &mi);
1161 if (!attr2) {
1162 err = -EINVAL;
1163 goto out;
1164 }
1165 err = attr_load_runs(attr2, ni, run, NULL);
1166 if (err)
1167 goto out;
1168 }
1169
1170 da = false; /* no delalloc for compressed file. */
1171 }
1172
1173 if (vcn + to_alloc > asize)
1174 to_alloc = asize - vcn;
1175
1176 if (da) {
1177 CLST rlen1, rlen2;
1178 if (!ntfs_check_free_space(sbi, to_alloc, 0, true)) {
1179 err = ni_allocate_da_blocks_locked(ni);
1180 if (err)
1181 goto out;
1182 /* Layout of records may be changed. Start again without 'da'. */
1183 da = false;
1184 goto again;
1185 }
1186
1187 /* run_add_entry consolidates existed ranges. */
1188 rlen1 = run_len(run_da);
1189 if (!run_add_entry(run_da, vcn, SPARSE_LCN, to_alloc, false)) {
1190 err = -ENOMEM;
1191 goto out;
1192 }
1193 rlen2 = run_len(run_da);
1194
1195 /* new added delay clusters = rlen2 - rlen1. */
1196 ntfs_add_da(sbi, rlen2 - rlen1);
1197 *len = to_alloc;
1198 *lcn = DELALLOC_LCN;
1199 goto ok;
1200 }
1201
1202 /* Get the last LCN to allocate from. */
1203 hint = 0;
1204
1205 if (vcn > evcn1) {
1206 if (!run_add_entry(run, evcn1, SPARSE_LCN, vcn - evcn1,
1207 false)) {
1208 err = -ENOMEM;
1209 goto out;
1210 }
1211 } else if (vcn && !run_lookup_entry(run, vcn - 1, &hint, NULL, NULL)) {
1212 hint = -1;
1213 }
1214
1215 /* Allocate and zeroout new clusters. */
1216 err = attr_allocate_clusters(sbi, run, run_da, vcn, hint + 1, to_alloc,
1217 NULL,
1218 zero ? ALLOCATE_ZERO : ALLOCATE_ONE_FR,
1219 len, fr, lcn, len);
1220 if (err)
1221 goto out;
1222 *new = true;
1223 step = 1;
1224
1225 end = vcn + *len;
1226 /* Save 'total_size0' to restore if error. */
1227 total_size0 = le64_to_cpu(attr_b->nres.total_size);
1228 total_size = total_size0 + ((u64)*len << cluster_bits);
1229
1230 if (vcn != vcn0) {
1231 if (!run_lookup_entry(run, vcn0, lcn, len, NULL)) {
1232 err = -EINVAL;
1233 goto out;
1234 }
1235 if (*lcn == SPARSE_LCN) {
1236 /* Internal error. Should not happened. */
1237 WARN_ON(1);
1238 err = -EINVAL;
1239 goto out;
1240 }
1241 /* Check case when vcn0 + len overlaps new allocated clusters. */
1242 if (vcn0 + *len > end)
1243 *len = end - vcn0;
1244 }
1245
1246 repack:
1247 err = mi_pack_runs(mi, attr, run, max(end, evcn1) - svcn);
1248 if (err)
1249 goto out;
1250
1251 attr_b->nres.total_size = cpu_to_le64(total_size);
1252 inode_set_bytes(&ni->vfs_inode, total_size);
1253 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
1254
1255 mi_b->dirty = true;
1256 mark_inode_dirty(&ni->vfs_inode);
1257
1258 /* Stored [vcn : next_svcn) from [vcn : end). */
1259 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1260
1261 if (end <= evcn1) {
1262 if (next_svcn == evcn1) {
1263 /* Normal way. Update attribute and exit. */
1264 goto ok;
1265 }
1266 /* Add new segment [next_svcn : evcn1 - next_svcn). */
1267 if (!ni->attr_list.size) {
1268 err = ni_create_attr_list(ni);
1269 if (err)
1270 goto undo1;
1271 /* Layout of records is changed. */
1272 le_b = NULL;
1273 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL,
1274 0, NULL, &mi_b);
1275 if (!attr_b) {
1276 err = -ENOENT;
1277 goto out;
1278 }
1279
1280 attr = attr_b;
1281 le = le_b;
1282 mi = mi_b;
1283 goto repack;
1284 }
1285 }
1286
1287 /*
1288 * The code below may require additional cluster (to extend attribute list)
1289 * and / or one MFT record
1290 * It is too complex to undo operations if -ENOSPC occurs deep inside
1291 * in 'ni_insert_nonresident'.
1292 * Return in advance -ENOSPC here if there are no free cluster and no free MFT.
1293 */
1294 if (!ntfs_check_free_space(sbi, 1, 1, false)) {
1295 /* Undo step 1. */
1296 err = -ENOSPC;
1297 goto undo1;
1298 }
1299
1300 step = 2;
1301 svcn = evcn1;
1302
1303 /* Estimate next attribute. */
1304 attr = ni_find_attr(ni, attr, &le, ATTR_DATA, NULL, 0, &svcn, &mi);
1305
1306 if (!attr) {
1307 /* Insert new attribute segment. */
1308 goto ins_ext;
1309 }
1310
1311 /* Try to update existed attribute segment. */
1312 alloc = bytes_to_cluster(sbi, le64_to_cpu(attr_b->nres.alloc_size));
1313 evcn = le64_to_cpu(attr->nres.evcn);
1314
1315 if (end < next_svcn)
1316 end = next_svcn;
1317 while (end > evcn) {
1318 /* Remove segment [svcn : evcn). */
1319 mi_remove_attr(NULL, mi, attr);
1320
1321 if (!al_remove_le(ni, le)) {
1322 err = -EINVAL;
1323 goto out;
1324 }
1325
1326 if (evcn + 1 >= alloc) {
1327 /* Last attribute segment. */
1328 evcn1 = evcn + 1;
1329 goto ins_ext;
1330 }
1331
1332 if (ni_load_mi(ni, le, &mi)) {
1333 attr = NULL;
1334 goto out;
1335 }
1336
1337 attr = mi_find_attr(ni, mi, NULL, ATTR_DATA, NULL, 0, &le->id);
1338 if (!attr) {
1339 err = -EINVAL;
1340 goto out;
1341 }
1342 svcn = le64_to_cpu(attr->nres.svcn);
1343 evcn = le64_to_cpu(attr->nres.evcn);
1344 }
1345
1346 if (end < svcn)
1347 end = svcn;
1348
1349 err = attr_load_runs(attr, ni, run, &end);
1350 if (err)
1351 goto out;
1352
1353 evcn1 = evcn + 1;
1354 attr->nres.svcn = cpu_to_le64(next_svcn);
1355 err = mi_pack_runs(mi, attr, run, evcn1 - next_svcn);
1356 if (err)
1357 goto out;
1358
1359 le->vcn = cpu_to_le64(next_svcn);
1360 ni->attr_list.dirty = true;
1361 mi->dirty = true;
1362 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1363
1364 ins_ext:
1365 if (evcn1 > next_svcn) {
1366 err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
1367 next_svcn, evcn1 - next_svcn,
1368 attr_b->flags, &attr, &mi, NULL);
1369 if (err)
1370 goto out;
1371 }
1372 ok:
1373 run_truncate_around(run, vcn);
1374 out:
1375 if (err && step > 1) {
1376 /* Too complex to restore. */
1377 _ntfs_bad_inode(&ni->vfs_inode);
1378 }
1379
1380 return err;
1381
1382 undo1:
1383 /* Undo step1. */
1384 attr_b->nres.total_size = cpu_to_le64(total_size0);
1385 inode_set_bytes(&ni->vfs_inode, total_size0);
1386
1387 if (run_deallocate_ex(sbi, run, vcn, *len, NULL, false, run_da) ||
1388 !run_add_entry(run, vcn, SPARSE_LCN, *len, false) ||
1389 mi_pack_runs(mi, attr, run, max(end, evcn1) - svcn)) {
1390 _ntfs_bad_inode(&ni->vfs_inode);
1391 }
1392 goto out;
1393 }
1394
1395 int attr_data_write_resident(struct ntfs_inode *ni, struct folio *folio)
1396 {
1397 u64 vbo;
1398 struct mft_inode *mi;
1399 struct ATTRIB *attr;
1400 u32 data_size;
1401
1402 attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi);
1403 if (!attr)
1404 return -EINVAL;
1405
1406 if (attr->non_res) {
1407 /* Return special error code to check this case. */
1408 return E_NTFS_NONRESIDENT;
1409 }
1410
1411 vbo = folio_pos(folio);
1412 data_size = le32_to_cpu(attr->res.data_size);
1413 if (vbo < data_size) {
1414 char *data = resident_data(attr);
1415 size_t len = min(data_size - vbo, folio_size(folio));
1416
1417 memcpy_from_folio(data + vbo, folio, 0, len);
1418 mi->dirty = true;
1419 }
1420 ni->i_valid = data_size;
1421
1422 return 0;
1423 }
1424
1425 /*
1426 * attr_load_runs_vcn - Load runs with VCN.
1427 */
1428 int attr_load_runs_vcn(struct ntfs_inode *ni, enum ATTR_TYPE type,
1429 const __le16 *name, u8 name_len, struct runs_tree *run,
1430 CLST vcn)
1431 {
1432 struct ATTRIB *attr;
1433 int err;
1434 CLST svcn, evcn;
1435 u16 ro;
1436
1437 if (!ni) {
1438 /* Is record corrupted? */
1439 return -ENOENT;
1440 }
1441
1442 attr = ni_find_attr(ni, NULL, NULL, type, name, name_len, &vcn, NULL);
1443 if (!attr) {
1444 /* Is record corrupted? */
1445 return -ENOENT;
1446 }
1447
1448 svcn = le64_to_cpu(attr->nres.svcn);
1449 evcn = le64_to_cpu(attr->nres.evcn);
1450
1451 if (evcn < vcn || vcn < svcn) {
1452 /* Is record corrupted? */
1453 return -EINVAL;
1454 }
1455
1456 ro = le16_to_cpu(attr->nres.run_off);
1457
1458 if (ro > le32_to_cpu(attr->size))
1459 return -EINVAL;
1460
1461 err = run_unpack_ex(run, ni->mi.sbi, ni->mi.rno, svcn, evcn, svcn,
1462 Add2Ptr(attr, ro), le32_to_cpu(attr->size) - ro);
1463 if (err < 0)
1464 return err;
1465 return 0;
1466 }
1467
1468 /*
1469 * attr_load_runs_range - Load runs for given range [from to).
1470 */
1471 int attr_load_runs_range(struct ntfs_inode *ni, enum ATTR_TYPE type,
1472 const __le16 *name, u8 name_len, struct runs_tree *run,
1473 u64 from, u64 to)
1474 {
1475 struct ntfs_sb_info *sbi = ni->mi.sbi;
1476 u8 cluster_bits = sbi->cluster_bits;
1477 CLST vcn;
1478 CLST vcn_last = (to - 1) >> cluster_bits;
1479 CLST lcn, clen;
1480 int err = 0;
1481 int retry = 0;
1482
1483 for (vcn = from >> cluster_bits; vcn <= vcn_last; vcn += clen) {
1484 if (run_lookup_entry(run, vcn, &lcn, &clen, NULL)) {
1485 retry = 0;
1486 continue;
1487 }
1488 if (retry) {
1489 err = -EINVAL;
1490 break;
1491 }
1492 err = attr_load_runs_vcn(ni, type, name, name_len, run, vcn);
1493 if (err)
1494 break;
1495
1496 clen = 0; /* Next run_lookup_entry(vcn) must be success. */
1497 retry++;
1498 }
1499
1500 return err;
1501 }
1502
1503 #ifdef CONFIG_NTFS3_LZX_XPRESS
1504 /*
1505 * attr_wof_frame_info
1506 *
1507 * Read header of Xpress/LZX file to get info about frame.
1508 */
1509 int attr_wof_frame_info(struct ntfs_inode *ni, struct ATTRIB *attr,
1510 struct runs_tree *run, u64 frame, u64 frames,
1511 u8 frame_bits, u32 *ondisk_size, u64 *vbo_data)
1512 {
1513 struct ntfs_sb_info *sbi = ni->mi.sbi;
1514 u64 vbo[2], off[2], wof_size;
1515 u32 voff;
1516 u8 bytes_per_off;
1517 char *addr;
1518 struct folio *folio;
1519 int i, err;
1520 __le32 *off32;
1521 __le64 *off64;
1522
1523 if (ni->vfs_inode.i_size < 0x100000000ull) {
1524 /* File starts with array of 32 bit offsets. */
1525 bytes_per_off = sizeof(__le32);
1526 vbo[1] = frame << 2;
1527 *vbo_data = frames << 2;
1528 } else {
1529 /* File starts with array of 64 bit offsets. */
1530 bytes_per_off = sizeof(__le64);
1531 vbo[1] = frame << 3;
1532 *vbo_data = frames << 3;
1533 }
1534
1535 /*
1536 * Read 4/8 bytes at [vbo - 4(8)] == offset where compressed frame starts.
1537 * Read 4/8 bytes at [vbo] == offset where compressed frame ends.
1538 */
1539 if (!attr->non_res) {
1540 if (vbo[1] + bytes_per_off > le32_to_cpu(attr->res.data_size)) {
1541 _ntfs_bad_inode(&ni->vfs_inode);
1542 return -EINVAL;
1543 }
1544 addr = resident_data(attr);
1545
1546 if (bytes_per_off == sizeof(__le32)) {
1547 off32 = Add2Ptr(addr, vbo[1]);
1548 off[0] = vbo[1] ? le32_to_cpu(off32[-1]) : 0;
1549 off[1] = le32_to_cpu(off32[0]);
1550 } else {
1551 off64 = Add2Ptr(addr, vbo[1]);
1552 off[0] = vbo[1] ? le64_to_cpu(off64[-1]) : 0;
1553 off[1] = le64_to_cpu(off64[0]);
1554 }
1555
1556 *vbo_data += off[0];
1557 *ondisk_size = off[1] - off[0];
1558 return 0;
1559 }
1560
1561 wof_size = le64_to_cpu(attr->nres.data_size);
1562 down_write(&ni->file.run_lock);
1563 folio = ni->file.offs_folio;
1564 if (!folio) {
1565 folio = folio_alloc(GFP_KERNEL, 0);
1566 if (!folio) {
1567 err = -ENOMEM;
1568 goto out;
1569 }
1570 folio->index = -1;
1571 ni->file.offs_folio = folio;
1572 }
1573 folio_lock(folio);
1574 addr = folio_address(folio);
1575
1576 if (vbo[1]) {
1577 voff = vbo[1] & (PAGE_SIZE - 1);
1578 vbo[0] = vbo[1] - bytes_per_off;
1579 i = 0;
1580 } else {
1581 voff = 0;
1582 vbo[0] = 0;
1583 off[0] = 0;
1584 i = 1;
1585 }
1586
1587 do {
1588 pgoff_t index = vbo[i] >> PAGE_SHIFT;
1589
1590 if (index != folio->index) {
1591 u64 from = vbo[i] & ~(u64)(PAGE_SIZE - 1);
1592 u64 to = min(from + PAGE_SIZE, wof_size);
1593
1594 err = attr_load_runs_range(ni, ATTR_DATA, WOF_NAME,
1595 ARRAY_SIZE(WOF_NAME), run,
1596 from, to);
1597 if (err)
1598 goto out1;
1599
1600 err = ntfs_read_run(sbi, run, addr, from, to - from);
1601 if (err) {
1602 folio->index = -1;
1603 goto out1;
1604 }
1605 folio->index = index;
1606 }
1607
1608 if (i) {
1609 if (bytes_per_off == sizeof(__le32)) {
1610 off32 = Add2Ptr(addr, voff);
1611 off[1] = le32_to_cpu(*off32);
1612 } else {
1613 off64 = Add2Ptr(addr, voff);
1614 off[1] = le64_to_cpu(*off64);
1615 }
1616 } else if (!voff) {
1617 if (bytes_per_off == sizeof(__le32)) {
1618 off32 = Add2Ptr(addr, PAGE_SIZE - sizeof(u32));
1619 off[0] = le32_to_cpu(*off32);
1620 } else {
1621 off64 = Add2Ptr(addr, PAGE_SIZE - sizeof(u64));
1622 off[0] = le64_to_cpu(*off64);
1623 }
1624 } else {
1625 /* Two values in one page. */
1626 if (bytes_per_off == sizeof(__le32)) {
1627 off32 = Add2Ptr(addr, voff);
1628 off[0] = le32_to_cpu(off32[-1]);
1629 off[1] = le32_to_cpu(off32[0]);
1630 } else {
1631 off64 = Add2Ptr(addr, voff);
1632 off[0] = le64_to_cpu(off64[-1]);
1633 off[1] = le64_to_cpu(off64[0]);
1634 }
1635 break;
1636 }
1637 } while (++i < 2);
1638
1639 *vbo_data += off[0];
1640 *ondisk_size = off[1] - off[0];
1641
1642 out1:
1643 folio_unlock(folio);
1644 out:
1645 up_write(&ni->file.run_lock);
1646 return err;
1647 }
1648 #endif
1649
1650 /*
1651 * attr_is_frame_compressed - Used to detect compressed frame.
1652 *
1653 * attr - base (primary) attribute segment.
1654 * run - run to use, usually == &ni->file.run.
1655 * Only base segments contains valid 'attr->nres.c_unit'
1656 */
1657 int attr_is_frame_compressed(struct ntfs_inode *ni, struct ATTRIB *attr,
1658 CLST frame, CLST *clst_data, struct runs_tree *run)
1659 {
1660 int err;
1661 u32 clst_frame;
1662 CLST clen, lcn, vcn, alen, slen, vcn_next;
1663 size_t idx;
1664
1665 *clst_data = 0;
1666
1667 if (!is_attr_compressed(attr))
1668 return 0;
1669
1670 if (!attr->non_res)
1671 return 0;
1672
1673 clst_frame = 1u << attr->nres.c_unit;
1674 vcn = frame * clst_frame;
1675
1676 if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx)) {
1677 err = attr_load_runs_vcn(ni, attr->type, attr_name(attr),
1678 attr->name_len, run, vcn);
1679 if (err)
1680 return err;
1681
1682 if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx))
1683 return -EINVAL;
1684 }
1685
1686 if (lcn == SPARSE_LCN) {
1687 /* Sparsed frame. */
1688 return 0;
1689 }
1690
1691 if (clen >= clst_frame) {
1692 /*
1693 * The frame is not compressed 'cause
1694 * it does not contain any sparse clusters.
1695 */
1696 *clst_data = clst_frame;
1697 return 0;
1698 }
1699
1700 alen = bytes_to_cluster(ni->mi.sbi, le64_to_cpu(attr->nres.alloc_size));
1701 slen = 0;
1702 *clst_data = clen;
1703
1704 /*
1705 * The frame is compressed if *clst_data + slen >= clst_frame.
1706 * Check next fragments.
1707 */
1708 while ((vcn += clen) < alen) {
1709 vcn_next = vcn;
1710
1711 if (!run_get_entry(run, ++idx, &vcn, &lcn, &clen) ||
1712 vcn_next != vcn) {
1713 err = attr_load_runs_vcn(ni, attr->type,
1714 attr_name(attr),
1715 attr->name_len, run, vcn_next);
1716 if (err)
1717 return err;
1718 vcn = vcn_next;
1719
1720 if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx))
1721 return -EINVAL;
1722 }
1723
1724 if (lcn == SPARSE_LCN) {
1725 slen += clen;
1726 } else {
1727 if (slen) {
1728 /*
1729 * Data_clusters + sparse_clusters =
1730 * not enough for frame.
1731 */
1732 return -EINVAL;
1733 }
1734 *clst_data += clen;
1735 }
1736
1737 if (*clst_data + slen >= clst_frame) {
1738 if (!slen) {
1739 /*
1740 * There is no sparsed clusters in this frame
1741 * so it is not compressed.
1742 */
1743 *clst_data = clst_frame;
1744 } else {
1745 /* Frame is compressed. */
1746 }
1747 break;
1748 }
1749 }
1750
1751 return 0;
1752 }
1753
1754 /*
1755 * attr_allocate_frame - Allocate/free clusters for @frame.
1756 *
1757 * Assumed: down_write(&ni->file.run_lock);
1758 */
1759 int attr_allocate_frame(struct ntfs_inode *ni, CLST frame, size_t compr_size,
1760 u64 new_valid)
1761 {
1762 int err = 0;
1763 struct runs_tree *run = &ni->file.run;
1764 struct ntfs_sb_info *sbi = ni->mi.sbi;
1765 struct ATTRIB *attr = NULL, *attr_b;
1766 struct ATTR_LIST_ENTRY *le, *le_b;
1767 struct mft_inode *mi, *mi_b;
1768 CLST svcn, evcn1, next_svcn, len;
1769 CLST vcn, end, clst_data;
1770 u64 total_size, valid_size, data_size;
1771
1772 le_b = NULL;
1773 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
1774 if (!attr_b)
1775 return -ENOENT;
1776
1777 if (!is_attr_ext(attr_b))
1778 return -EINVAL;
1779
1780 vcn = frame << NTFS_LZNT_CUNIT;
1781 total_size = le64_to_cpu(attr_b->nres.total_size);
1782
1783 svcn = le64_to_cpu(attr_b->nres.svcn);
1784 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
1785 data_size = le64_to_cpu(attr_b->nres.data_size);
1786
1787 if (svcn <= vcn && vcn < evcn1) {
1788 attr = attr_b;
1789 le = le_b;
1790 mi = mi_b;
1791 } else if (!le_b) {
1792 err = -EINVAL;
1793 goto out;
1794 } else {
1795 le = le_b;
1796 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
1797 &mi);
1798 if (!attr) {
1799 err = -EINVAL;
1800 goto out;
1801 }
1802 svcn = le64_to_cpu(attr->nres.svcn);
1803 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
1804 }
1805
1806 err = attr_load_runs(attr, ni, run, NULL);
1807 if (err)
1808 goto out;
1809
1810 err = attr_is_frame_compressed(ni, attr_b, frame, &clst_data, run);
1811 if (err)
1812 goto out;
1813
1814 total_size -= (u64)clst_data << sbi->cluster_bits;
1815
1816 len = bytes_to_cluster(sbi, compr_size);
1817
1818 if (len == clst_data)
1819 goto out;
1820
1821 if (len < clst_data) {
1822 err = run_deallocate_ex(sbi, run, vcn + len, clst_data - len,
1823 NULL, true, NULL);
1824 if (err)
1825 goto out;
1826
1827 if (!run_add_entry(run, vcn + len, SPARSE_LCN, clst_data - len,
1828 false)) {
1829 err = -ENOMEM;
1830 goto out;
1831 }
1832 end = vcn + clst_data;
1833 /* Run contains updated range [vcn + len : end). */
1834 } else {
1835 CLST alen, hint = 0;
1836 /* Get the last LCN to allocate from. */
1837 if (vcn + clst_data &&
1838 !run_lookup_entry(run, vcn + clst_data - 1, &hint, NULL,
1839 NULL)) {
1840 hint = -1;
1841 }
1842
1843 err = attr_allocate_clusters(sbi, run, NULL, vcn + clst_data,
1844 hint + 1, len - clst_data, NULL,
1845 ALLOCATE_DEF, &alen, 0, NULL,
1846 NULL);
1847 if (err)
1848 goto out;
1849
1850 end = vcn + len;
1851 /* Run contains updated range [vcn + clst_data : end). */
1852 }
1853
1854 total_size += (u64)len << sbi->cluster_bits;
1855
1856 repack:
1857 err = mi_pack_runs(mi, attr, run, max(end, evcn1) - svcn);
1858 if (err)
1859 goto out;
1860
1861 attr_b->nres.total_size = cpu_to_le64(total_size);
1862 inode_set_bytes(&ni->vfs_inode, total_size);
1863 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
1864
1865 mi_b->dirty = true;
1866 mark_inode_dirty(&ni->vfs_inode);
1867
1868 /* Stored [vcn : next_svcn) from [vcn : end). */
1869 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1870
1871 if (end <= evcn1) {
1872 if (next_svcn == evcn1) {
1873 /* Normal way. Update attribute and exit. */
1874 goto ok;
1875 }
1876 /* Add new segment [next_svcn : evcn1 - next_svcn). */
1877 if (!ni->attr_list.size) {
1878 err = ni_create_attr_list(ni);
1879 if (err)
1880 goto out;
1881 /* Layout of records is changed. */
1882 le_b = NULL;
1883 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL,
1884 0, NULL, &mi_b);
1885 if (!attr_b) {
1886 err = -ENOENT;
1887 goto out;
1888 }
1889
1890 attr = attr_b;
1891 le = le_b;
1892 mi = mi_b;
1893 goto repack;
1894 }
1895 }
1896
1897 svcn = evcn1;
1898
1899 /* Estimate next attribute. */
1900 attr = ni_find_attr(ni, attr, &le, ATTR_DATA, NULL, 0, &svcn, &mi);
1901
1902 if (attr) {
1903 CLST alloc = bytes_to_cluster(
1904 sbi, le64_to_cpu(attr_b->nres.alloc_size));
1905 CLST evcn = le64_to_cpu(attr->nres.evcn);
1906
1907 if (end < next_svcn)
1908 end = next_svcn;
1909 while (end > evcn) {
1910 /* Remove segment [svcn : evcn). */
1911 mi_remove_attr(NULL, mi, attr);
1912
1913 if (!al_remove_le(ni, le)) {
1914 err = -EINVAL;
1915 goto out;
1916 }
1917
1918 if (evcn + 1 >= alloc) {
1919 /* Last attribute segment. */
1920 evcn1 = evcn + 1;
1921 goto ins_ext;
1922 }
1923
1924 if (ni_load_mi(ni, le, &mi)) {
1925 attr = NULL;
1926 goto out;
1927 }
1928
1929 attr = mi_find_attr(ni, mi, NULL, ATTR_DATA, NULL, 0,
1930 &le->id);
1931 if (!attr) {
1932 err = -EINVAL;
1933 goto out;
1934 }
1935 svcn = le64_to_cpu(attr->nres.svcn);
1936 evcn = le64_to_cpu(attr->nres.evcn);
1937 }
1938
1939 if (end < svcn)
1940 end = svcn;
1941
1942 err = attr_load_runs(attr, ni, run, &end);
1943 if (err)
1944 goto out;
1945
1946 evcn1 = evcn + 1;
1947 attr->nres.svcn = cpu_to_le64(next_svcn);
1948 err = mi_pack_runs(mi, attr, run, evcn1 - next_svcn);
1949 if (err)
1950 goto out;
1951
1952 le->vcn = cpu_to_le64(next_svcn);
1953 ni->attr_list.dirty = true;
1954 mi->dirty = true;
1955
1956 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1957 }
1958 ins_ext:
1959 if (evcn1 > next_svcn) {
1960 err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
1961 next_svcn, evcn1 - next_svcn,
1962 attr_b->flags, &attr, &mi, NULL);
1963 if (err)
1964 goto out;
1965 }
1966 ok:
1967 run_truncate_around(run, vcn);
1968 out:
1969 if (attr_b) {
1970 if (new_valid > data_size)
1971 new_valid = data_size;
1972
1973 valid_size = le64_to_cpu(attr_b->nres.valid_size);
1974 if (new_valid != valid_size) {
1975 attr_b->nres.valid_size = cpu_to_le64(valid_size);
1976 mi_b->dirty = true;
1977 }
1978 }
1979
1980 return err;
1981 }
1982
1983 /*
1984 * attr_collapse_range - Collapse range in file.
1985 */
1986 int attr_collapse_range(struct ntfs_inode *ni, u64 vbo, u64 bytes)
1987 {
1988 int err = 0;
1989 struct runs_tree *run = &ni->file.run;
1990 struct ntfs_sb_info *sbi = ni->mi.sbi;
1991 struct ATTRIB *attr = NULL, *attr_b;
1992 struct ATTR_LIST_ENTRY *le, *le_b;
1993 struct mft_inode *mi, *mi_b;
1994 CLST svcn, evcn1, len, dealloc, alen, done;
1995 CLST vcn, end;
1996 u64 valid_size, data_size, alloc_size, total_size;
1997 u32 mask;
1998 u64 i_size;
1999 __le16 a_flags;
2000
2001 if (!bytes)
2002 return 0;
2003
2004 le_b = NULL;
2005 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
2006 if (!attr_b)
2007 return -ENOENT;
2008
2009 if (!attr_b->non_res) {
2010 /* Attribute is resident. Nothing to do? */
2011 return 0;
2012 }
2013
2014 mask = is_attr_ext(attr_b) ?
2015 ((sbi->cluster_size << attr_b->nres.c_unit) - 1) :
2016 sbi->cluster_mask;
2017 if ((vbo | bytes) & mask) {
2018 /* Allow to collapse only cluster aligned ranges. */
2019 return -EINVAL;
2020 }
2021
2022 /* i_size - size of file with delay allocated clusters. */
2023 i_size = ni->vfs_inode.i_size;
2024
2025 if (vbo > i_size)
2026 return -EINVAL;
2027
2028 down_write(&ni->file.run_lock);
2029
2030 if (vbo + bytes >= i_size) {
2031 valid_size = min(ni->i_valid, vbo);
2032
2033 /* Simple truncate file at 'vbo'. */
2034 truncate_setsize(&ni->vfs_inode, vbo);
2035 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, vbo,
2036 &valid_size, true);
2037
2038 if (!err && valid_size < ni->i_valid)
2039 ni->i_valid = valid_size;
2040
2041 goto out;
2042 }
2043
2044 vcn = vbo >> sbi->cluster_bits;
2045 len = bytes >> sbi->cluster_bits;
2046 end = vcn + len;
2047 dealloc = 0;
2048 done = 0;
2049
2050 /*
2051 * Check delayed clusters.
2052 */
2053 if (ni->file.run_da.count) {
2054 struct runs_tree *run_da = &ni->file.run_da;
2055 if (run_is_mapped_full(run_da, vcn, end - 1)) {
2056 /*
2057 * The requested range is full in delayed clusters.
2058 */
2059 err = attr_set_size_ex(ni, ATTR_DATA, NULL, 0, run,
2060 i_size - bytes, NULL, false,
2061 NULL, true);
2062 goto out;
2063 }
2064
2065 /* Collapse request crosses real and delayed clusters. */
2066 err = ni_allocate_da_blocks_locked(ni);
2067 if (err)
2068 goto out;
2069
2070 /* Layout of records maybe changed. */
2071 le_b = NULL;
2072 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL,
2073 &mi_b);
2074 if (!attr_b || !attr_b->non_res) {
2075 err = -ENOENT;
2076 goto out;
2077 }
2078 }
2079
2080 data_size = le64_to_cpu(attr_b->nres.data_size);
2081 alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
2082 total_size = is_attr_ext(attr_b) ?
2083 le64_to_cpu(attr_b->nres.total_size) :
2084 alloc_size;
2085 alen = alloc_size >> sbi->cluster_bits;
2086 a_flags = attr_b->flags;
2087 svcn = le64_to_cpu(attr_b->nres.svcn);
2088 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
2089
2090 if (svcn <= vcn && vcn < evcn1) {
2091 attr = attr_b;
2092 le = le_b;
2093 mi = mi_b;
2094 goto check_seg;
2095 }
2096
2097 if (!le_b) {
2098 err = -EINVAL;
2099 goto out;
2100 }
2101
2102 le = le_b;
2103 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn, &mi);
2104 if (!attr) {
2105 err = -EINVAL;
2106 goto out;
2107 }
2108
2109 /*
2110 * Enumerate all attribute segments and collapse.
2111 */
2112 for (;;) {
2113 CLST vcn1, eat, next_svcn;
2114
2115 svcn = le64_to_cpu(attr->nres.svcn);
2116 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2117
2118 check_seg:
2119 if (svcn >= end) {
2120 /* Shift VCN- */
2121 attr->nres.svcn = cpu_to_le64(svcn - len);
2122 attr->nres.evcn = cpu_to_le64(evcn1 - 1 - len);
2123 if (le) {
2124 le->vcn = attr->nres.svcn;
2125 ni->attr_list.dirty = true;
2126 }
2127 mi->dirty = true;
2128 goto next_attr;
2129 }
2130
2131 run_truncate(run, 0);
2132 err = attr_load_runs(attr, ni, run, &svcn);
2133 if (err)
2134 goto out;
2135
2136 vcn1 = vcn + done; /* original vcn in attr/run. */
2137 eat = min(end, evcn1) - vcn1;
2138
2139 err = run_deallocate_ex(sbi, run, vcn1, eat, &dealloc, true,
2140 NULL);
2141 if (err)
2142 goto out;
2143
2144 if (svcn + eat < evcn1) {
2145 /* Collapse a part of this attribute segment. */
2146 if (!run_collapse_range(run, vcn1, eat, done)) {
2147 err = -ENOMEM;
2148 goto out;
2149 }
2150
2151 if (svcn >= vcn) {
2152 /* Shift VCN */
2153 attr->nres.svcn = cpu_to_le64(vcn);
2154 if (le && attr->nres.svcn != le->vcn) {
2155 le->vcn = attr->nres.svcn;
2156 ni->attr_list.dirty = true;
2157 }
2158 }
2159
2160 err = mi_pack_runs(mi, attr, run, evcn1 - svcn - eat);
2161 if (err)
2162 goto out;
2163
2164 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
2165 if (next_svcn + eat + done < evcn1) {
2166 err = ni_insert_nonresident(
2167 ni, ATTR_DATA, NULL, 0, run, next_svcn,
2168 evcn1 - eat - next_svcn, a_flags, &attr,
2169 &mi, &le);
2170 if (err)
2171 goto out;
2172
2173 /* Layout of records maybe changed. */
2174 attr_b = NULL;
2175 }
2176
2177 /* Free all allocated memory. */
2178 run_truncate(run, 0);
2179 done += eat;
2180 } else {
2181 u16 le_sz;
2182
2183 /* Delete this attribute segment. */
2184 mi_remove_attr(NULL, mi, attr);
2185 if (!le)
2186 break;
2187
2188 le_sz = le16_to_cpu(le->size);
2189 if (!al_remove_le(ni, le)) {
2190 err = -EINVAL;
2191 goto out;
2192 }
2193
2194 done += evcn1 - svcn;
2195 if (evcn1 >= alen)
2196 break;
2197
2198 if (!svcn) {
2199 /* Load next record that contains this attribute. */
2200 if (ni_load_mi(ni, le, &mi)) {
2201 err = -EINVAL;
2202 goto out;
2203 }
2204
2205 /* Look for required attribute. */
2206 attr = mi_find_attr(ni, mi, NULL, ATTR_DATA,
2207 NULL, 0, &le->id);
2208 if (!attr) {
2209 err = -EINVAL;
2210 goto out;
2211 }
2212 continue;
2213 }
2214 le = (struct ATTR_LIST_ENTRY *)((u8 *)le - le_sz);
2215 }
2216
2217 next_attr:
2218 if (evcn1 >= alen)
2219 break;
2220
2221 attr = ni_enum_attr_ex(ni, attr, &le, &mi);
2222 if (!attr) {
2223 err = -EINVAL;
2224 goto out;
2225 }
2226 }
2227
2228 if (!attr_b) {
2229 le_b = NULL;
2230 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL,
2231 &mi_b);
2232 if (!attr_b) {
2233 err = -ENOENT;
2234 goto out;
2235 }
2236 }
2237
2238 data_size -= bytes;
2239 valid_size = ni->i_valid;
2240 if (vbo + bytes <= valid_size)
2241 valid_size -= bytes;
2242 else if (vbo < valid_size)
2243 valid_size = vbo;
2244
2245 attr_b->nres.alloc_size = cpu_to_le64(alloc_size - bytes);
2246 attr_b->nres.data_size = cpu_to_le64(data_size);
2247 attr_b->nres.valid_size = cpu_to_le64(min(valid_size, data_size));
2248 total_size -= (u64)dealloc << sbi->cluster_bits;
2249 if (is_attr_ext(attr_b))
2250 attr_b->nres.total_size = cpu_to_le64(total_size);
2251 mi_b->dirty = true;
2252
2253 /* Update inode size. */
2254 ni->i_valid = valid_size;
2255 i_size_write(&ni->vfs_inode, data_size);
2256 inode_set_bytes(&ni->vfs_inode, total_size);
2257 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
2258 mark_inode_dirty(&ni->vfs_inode);
2259
2260 out:
2261 up_write(&ni->file.run_lock);
2262 if (err)
2263 _ntfs_bad_inode(&ni->vfs_inode);
2264
2265 return err;
2266 }
2267
2268 /*
2269 * attr_punch_hole
2270 *
2271 * Not for normal files.
2272 */
2273 int attr_punch_hole(struct ntfs_inode *ni, u64 vbo, u64 bytes, u32 *frame_size)
2274 {
2275 int err = 0;
2276 struct runs_tree *run = &ni->file.run;
2277 struct ntfs_sb_info *sbi = ni->mi.sbi;
2278 struct ATTRIB *attr = NULL, *attr_b;
2279 struct ATTR_LIST_ENTRY *le, *le_b;
2280 struct mft_inode *mi, *mi_b;
2281 CLST svcn, evcn1, vcn, len, end, alen, hole, next_svcn;
2282 u64 total_size, alloc_size;
2283 u32 mask;
2284 __le16 a_flags;
2285 struct runs_tree run2;
2286
2287 if (!bytes)
2288 return 0;
2289
2290 le_b = NULL;
2291 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
2292 if (!attr_b)
2293 return -ENOENT;
2294
2295 if (!attr_b->non_res) {
2296 u32 data_size = le32_to_cpu(attr_b->res.data_size);
2297 u32 from, to;
2298
2299 if (vbo > data_size)
2300 return 0;
2301
2302 from = vbo;
2303 to = min_t(u64, vbo + bytes, data_size);
2304 memset(Add2Ptr(resident_data(attr_b), from), 0, to - from);
2305 return 0;
2306 }
2307
2308 if (!is_attr_ext(attr_b))
2309 return -EOPNOTSUPP;
2310
2311 alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
2312 total_size = le64_to_cpu(attr_b->nres.total_size);
2313
2314 if (vbo >= alloc_size) {
2315 /* NOTE: It is allowed. */
2316 return 0;
2317 }
2318
2319 mask = (sbi->cluster_size << attr_b->nres.c_unit) - 1;
2320
2321 bytes += vbo;
2322 if (bytes > alloc_size)
2323 bytes = alloc_size;
2324 bytes -= vbo;
2325
2326 if ((vbo | bytes) & mask) {
2327 /* We have to zero a range(s). */
2328 if (!frame_size) {
2329 /* Caller insists range is aligned. */
2330 return -EINVAL;
2331 }
2332 *frame_size = mask + 1;
2333 return E_NTFS_NOTALIGNED;
2334 }
2335
2336 down_write(&ni->file.run_lock);
2337 run_init(&run2);
2338 run_truncate(run, 0);
2339
2340 /*
2341 * Enumerate all attribute segments and punch hole where necessary.
2342 */
2343 alen = alloc_size >> sbi->cluster_bits;
2344 vcn = vbo >> sbi->cluster_bits;
2345 len = bytes >> sbi->cluster_bits;
2346 end = vcn + len;
2347 hole = 0;
2348
2349 svcn = le64_to_cpu(attr_b->nres.svcn);
2350 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
2351 a_flags = attr_b->flags;
2352
2353 if (svcn <= vcn && vcn < evcn1) {
2354 attr = attr_b;
2355 le = le_b;
2356 mi = mi_b;
2357 } else if (!le_b) {
2358 err = -EINVAL;
2359 goto bad_inode;
2360 } else {
2361 le = le_b;
2362 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
2363 &mi);
2364 if (!attr) {
2365 err = -EINVAL;
2366 goto bad_inode;
2367 }
2368
2369 svcn = le64_to_cpu(attr->nres.svcn);
2370 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2371 }
2372
2373 while (svcn < end) {
2374 CLST vcn1, zero, hole2 = hole;
2375
2376 err = attr_load_runs(attr, ni, run, &svcn);
2377 if (err)
2378 goto done;
2379 vcn1 = max(vcn, svcn);
2380 zero = min(end, evcn1) - vcn1;
2381
2382 /*
2383 * Check range [vcn1 + zero).
2384 * Calculate how many clusters there are.
2385 * Don't do any destructive actions.
2386 */
2387 err = run_deallocate_ex(NULL, run, vcn1, zero, &hole2, false,
2388 NULL);
2389 if (err)
2390 goto done;
2391
2392 /* Check if required range is already hole. */
2393 if (hole2 == hole)
2394 goto next_attr;
2395
2396 /* Make a clone of run to undo. */
2397 err = run_clone(run, &run2);
2398 if (err)
2399 goto done;
2400
2401 /* Make a hole range (sparse) [vcn1 + zero). */
2402 if (!run_add_entry(run, vcn1, SPARSE_LCN, zero, false)) {
2403 err = -ENOMEM;
2404 goto done;
2405 }
2406
2407 /* Update run in attribute segment. */
2408 err = mi_pack_runs(mi, attr, run, evcn1 - svcn);
2409 if (err)
2410 goto done;
2411 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
2412 if (next_svcn < evcn1) {
2413 /* Insert new attribute segment. */
2414 err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
2415 next_svcn,
2416 evcn1 - next_svcn, a_flags,
2417 &attr, &mi, &le);
2418 if (err)
2419 goto undo_punch;
2420
2421 /* Layout of records maybe changed. */
2422 attr_b = NULL;
2423 }
2424
2425 /* Real deallocate. Should not fail. */
2426 run_deallocate_ex(sbi, &run2, vcn1, zero, &hole, true,
2427 &ni->file.run_da);
2428
2429 next_attr:
2430 /* Free all allocated memory. */
2431 run_truncate(run, 0);
2432
2433 if (evcn1 >= alen)
2434 break;
2435
2436 /* Get next attribute segment. */
2437 attr = ni_enum_attr_ex(ni, attr, &le, &mi);
2438 if (!attr) {
2439 err = -EINVAL;
2440 goto bad_inode;
2441 }
2442
2443 svcn = le64_to_cpu(attr->nres.svcn);
2444 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2445 }
2446
2447 done:
2448 if (!hole)
2449 goto out;
2450
2451 if (!attr_b) {
2452 attr_b = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL,
2453 &mi_b);
2454 if (!attr_b) {
2455 err = -EINVAL;
2456 goto bad_inode;
2457 }
2458 }
2459
2460 total_size -= (u64)hole << sbi->cluster_bits;
2461 attr_b->nres.total_size = cpu_to_le64(total_size);
2462 mi_b->dirty = true;
2463
2464 /* Update inode size. */
2465 inode_set_bytes(&ni->vfs_inode, total_size);
2466 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
2467 mark_inode_dirty(&ni->vfs_inode);
2468
2469 out:
2470 run_close(&run2);
2471 up_write(&ni->file.run_lock);
2472 return err;
2473
2474 bad_inode:
2475 _ntfs_bad_inode(&ni->vfs_inode);
2476 goto out;
2477
2478 undo_punch:
2479 /*
2480 * Restore packed runs.
2481 * 'mi_pack_runs' should not fail, cause we restore original.
2482 */
2483 if (mi_pack_runs(mi, attr, &run2, evcn1 - svcn))
2484 goto bad_inode;
2485
2486 goto done;
2487 }
2488
2489 /*
2490 * attr_insert_range - Insert range (hole) in file.
2491 * Not for normal files.
2492 */
2493 int attr_insert_range(struct ntfs_inode *ni, u64 vbo, u64 bytes)
2494 {
2495 int err = 0;
2496 struct runs_tree *run = &ni->file.run;
2497 struct ntfs_sb_info *sbi = ni->mi.sbi;
2498 struct ATTRIB *attr = NULL, *attr_b;
2499 struct ATTR_LIST_ENTRY *le, *le_b;
2500 struct mft_inode *mi, *mi_b;
2501 CLST vcn, svcn, evcn1, len, next_svcn;
2502 u64 data_size, alloc_size;
2503 u32 mask;
2504 __le16 a_flags;
2505
2506 if (!bytes)
2507 return 0;
2508
2509 le_b = NULL;
2510 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
2511 if (!attr_b)
2512 return -ENOENT;
2513
2514 if (!is_attr_ext(attr_b)) {
2515 /* It was checked above. See fallocate. */
2516 return -EOPNOTSUPP;
2517 }
2518
2519 if (!attr_b->non_res) {
2520 data_size = le32_to_cpu(attr_b->res.data_size);
2521 alloc_size = data_size;
2522 mask = sbi->cluster_mask; /* cluster_size - 1 */
2523 } else {
2524 data_size = le64_to_cpu(attr_b->nres.data_size);
2525 alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
2526 mask = (sbi->cluster_size << attr_b->nres.c_unit) - 1;
2527 }
2528
2529 if (vbo >= data_size) {
2530 /*
2531 * Insert range after the file size is not allowed.
2532 * If the offset is equal to or greater than the end of
2533 * file, an error is returned. For such operations (i.e., inserting
2534 * a hole at the end of file), ftruncate(2) should be used.
2535 */
2536 return -EINVAL;
2537 }
2538
2539 if ((vbo | bytes) & mask) {
2540 /* Allow to insert only frame aligned ranges. */
2541 return -EINVAL;
2542 }
2543
2544 /*
2545 * valid_size <= data_size <= alloc_size
2546 * Check alloc_size for maximum possible.
2547 */
2548 if (bytes > sbi->maxbytes_sparse - alloc_size)
2549 return -EFBIG;
2550
2551 vcn = vbo >> sbi->cluster_bits;
2552 len = bytes >> sbi->cluster_bits;
2553
2554 down_write(&ni->file.run_lock);
2555
2556 if (!attr_b->non_res) {
2557 err = attr_set_size(ni, ATTR_DATA, NULL, 0, run,
2558 data_size + bytes, NULL, false);
2559
2560 le_b = NULL;
2561 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL,
2562 &mi_b);
2563 if (!attr_b) {
2564 err = -EINVAL;
2565 goto bad_inode;
2566 }
2567
2568 if (err)
2569 goto out;
2570
2571 if (!attr_b->non_res) {
2572 /* Still resident. */
2573 char *data = Add2Ptr(attr_b,
2574 le16_to_cpu(attr_b->res.data_off));
2575
2576 memmove(data + bytes, data, bytes);
2577 memset(data, 0, bytes);
2578 goto done;
2579 }
2580
2581 /* Resident file becomes nonresident. */
2582 data_size = le64_to_cpu(attr_b->nres.data_size);
2583 alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
2584 }
2585
2586 /*
2587 * Enumerate all attribute segments and shift start vcn.
2588 */
2589 a_flags = attr_b->flags;
2590 svcn = le64_to_cpu(attr_b->nres.svcn);
2591 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
2592
2593 if (svcn <= vcn && vcn < evcn1) {
2594 attr = attr_b;
2595 le = le_b;
2596 mi = mi_b;
2597 } else if (!le_b) {
2598 err = -EINVAL;
2599 goto bad_inode;
2600 } else {
2601 le = le_b;
2602 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
2603 &mi);
2604 if (!attr) {
2605 err = -EINVAL;
2606 goto bad_inode;
2607 }
2608
2609 svcn = le64_to_cpu(attr->nres.svcn);
2610 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2611 }
2612
2613 run_truncate(run, 0); /* clear cached values. */
2614 err = attr_load_runs(attr, ni, run, NULL);
2615 if (err)
2616 goto out;
2617
2618 err = run_insert_range(run, vcn, len);
2619 if (err)
2620 goto out;
2621
2622 err = run_insert_range_da(&ni->file.run_da, vcn, len);
2623 if (err)
2624 goto out;
2625
2626 /* Try to pack in current record as much as possible. */
2627 err = mi_pack_runs(mi, attr, run, evcn1 + len - svcn);
2628 if (err)
2629 goto out;
2630
2631 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
2632
2633 while ((attr = ni_enum_attr_ex(ni, attr, &le, &mi)) &&
2634 attr->type == ATTR_DATA && !attr->name_len) {
2635 le64_add_cpu(&attr->nres.svcn, len);
2636 le64_add_cpu(&attr->nres.evcn, len);
2637 if (le) {
2638 le->vcn = attr->nres.svcn;
2639 ni->attr_list.dirty = true;
2640 }
2641 mi->dirty = true;
2642 }
2643
2644 if (next_svcn < evcn1 + len) {
2645 err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
2646 next_svcn, evcn1 + len - next_svcn,
2647 a_flags, NULL, NULL, NULL);
2648
2649 le_b = NULL;
2650 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL,
2651 &mi_b);
2652 if (!attr_b) {
2653 err = -EINVAL;
2654 goto bad_inode;
2655 }
2656
2657 if (err) {
2658 /* ni_insert_nonresident failed. Try to undo. */
2659 goto undo_insert_range;
2660 }
2661 }
2662
2663 /*
2664 * Update primary attribute segment.
2665 */
2666 if (vbo <= ni->i_valid)
2667 ni->i_valid += bytes;
2668
2669 attr_b->nres.data_size = cpu_to_le64(data_size + bytes);
2670 attr_b->nres.alloc_size = cpu_to_le64(alloc_size + bytes);
2671
2672 /* ni->valid may be not equal valid_size (temporary). */
2673 if (ni->i_valid > data_size + bytes)
2674 attr_b->nres.valid_size = attr_b->nres.data_size;
2675 else
2676 attr_b->nres.valid_size = cpu_to_le64(ni->i_valid);
2677 mi_b->dirty = true;
2678
2679 done:
2680 i_size_write(&ni->vfs_inode, ni->vfs_inode.i_size + bytes);
2681 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
2682 mark_inode_dirty(&ni->vfs_inode);
2683
2684 out:
2685 run_truncate(run, 0); /* clear cached values. */
2686
2687 up_write(&ni->file.run_lock);
2688
2689 return err;
2690
2691 bad_inode:
2692 _ntfs_bad_inode(&ni->vfs_inode);
2693 goto out;
2694
2695 undo_insert_range:
2696 svcn = le64_to_cpu(attr_b->nres.svcn);
2697 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
2698
2699 if (svcn <= vcn && vcn < evcn1) {
2700 attr = attr_b;
2701 le = le_b;
2702 mi = mi_b;
2703 } else if (!le_b) {
2704 goto bad_inode;
2705 } else {
2706 le = le_b;
2707 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
2708 &mi);
2709 if (!attr) {
2710 goto bad_inode;
2711 }
2712
2713 svcn = le64_to_cpu(attr->nres.svcn);
2714 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2715 }
2716
2717 if (attr_load_runs(attr, ni, run, NULL))
2718 goto bad_inode;
2719
2720 if (!run_collapse_range(run, vcn, len, 0))
2721 goto bad_inode;
2722
2723 if (mi_pack_runs(mi, attr, run, evcn1 + len - svcn))
2724 goto bad_inode;
2725
2726 while ((attr = ni_enum_attr_ex(ni, attr, &le, &mi)) &&
2727 attr->type == ATTR_DATA && !attr->name_len) {
2728 le64_sub_cpu(&attr->nres.svcn, len);
2729 le64_sub_cpu(&attr->nres.evcn, len);
2730 if (le) {
2731 le->vcn = attr->nres.svcn;
2732 ni->attr_list.dirty = true;
2733 }
2734 mi->dirty = true;
2735 }
2736
2737 goto out;
2738 }
2739
2740 /*
2741 * attr_force_nonresident
2742 *
2743 * Convert default data attribute into non resident form.
2744 */
2745 int attr_force_nonresident(struct ntfs_inode *ni)
2746 {
2747 int err;
2748 struct ATTRIB *attr;
2749 struct ATTR_LIST_ENTRY *le = NULL;
2750 struct mft_inode *mi;
2751
2752 attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL, &mi);
2753 if (!attr) {
2754 _ntfs_bad_inode(&ni->vfs_inode);
2755 return -ENOENT;
2756 }
2757
2758 if (attr->non_res) {
2759 /* Already non resident. */
2760 return 0;
2761 }
2762
2763 down_write(&ni->file.run_lock);
2764 err = attr_make_nonresident(ni, attr, le, mi,
2765 le32_to_cpu(attr->res.data_size),
2766 &ni->file.run, &attr, NULL);
2767 up_write(&ni->file.run_lock);
2768
2769 return err;
2770 }