blob: a9d82bbb4729ecf1b65984ed05a2d8c259d2028f [file] [log] [blame]
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001// SPDX-License-Identifier: GPL-2.0
2/*
3 *
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
5 *
Kari Argillandere8b8e972021-08-03 14:57:09 +03006 * TODO: Merge attr_set_size/attr_data_get_block/attr_allocate_frame?
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03007 */
8
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03009#include <linux/fs.h>
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +030010#include <linux/slab.h>
Kari Argillander6e3331e2021-09-07 17:28:42 +030011#include <linux/kernel.h>
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +030012
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
Kari Argillandere8b8e972021-08-03 14:57:09 +030019 * preallocate algorithm.
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +030020 */
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
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +030034static 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/*
Kari Argillandere8b8e972021-08-03 14:57:09 +030058 * attr_load_runs - Load all runs stored in @attr.
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +030059 */
Konstantin Komarovcf760ec2022-07-01 15:15:32 +030060static int attr_load_runs(struct ATTRIB *attr, struct ntfs_inode *ni,
61 struct runs_tree *run, const CLST *vcn)
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +030062{
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);
Edward Lo6db62082022-08-06 00:47:27 +080077
78 if (run_off > asize)
79 return -EINVAL;
80
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +030081 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/*
Kari Argillandere8b8e972021-08-03 14:57:09 +030091 * run_deallocate_ex - Deallocate clusters.
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +030092 */
93static int run_deallocate_ex(struct ntfs_sb_info *sbi, struct runs_tree *run,
94 CLST vcn, CLST len, CLST *done, bool trim)
95{
96 int err = 0;
97 CLST vcn_next, vcn0 = vcn, lcn, clen, dn = 0;
98 size_t idx;
99
100 if (!len)
101 goto out;
102
103 if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx)) {
104failed:
105 run_truncate(run, vcn0);
106 err = -EINVAL;
107 goto out;
108 }
109
110 for (;;) {
111 if (clen > len)
112 clen = len;
113
114 if (!clen) {
115 err = -EINVAL;
116 goto out;
117 }
118
119 if (lcn != SPARSE_LCN) {
Konstantin Komarov20abc642022-07-13 18:18:48 +0300120 if (sbi) {
121 /* mark bitmap range [lcn + clen) as free and trim clusters. */
122 mark_as_free_ex(sbi, lcn, clen, trim);
123 }
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300124 dn += clen;
125 }
126
127 len -= clen;
128 if (!len)
129 break;
130
131 vcn_next = vcn + clen;
132 if (!run_get_entry(run, ++idx, &vcn, &lcn, &clen) ||
133 vcn != vcn_next) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300134 /* Save memory - don't load entire run. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300135 goto failed;
136 }
137 }
138
139out:
140 if (done)
141 *done += dn;
142
143 return err;
144}
145
146/*
Kari Argillandere8b8e972021-08-03 14:57:09 +0300147 * attr_allocate_clusters - Find free space, mark it as used and store in @run.
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300148 */
149int attr_allocate_clusters(struct ntfs_sb_info *sbi, struct runs_tree *run,
150 CLST vcn, CLST lcn, CLST len, CLST *pre_alloc,
151 enum ALLOCATE_OPT opt, CLST *alen, const size_t fr,
Konstantin Komarovc380b522022-10-07 14:02:36 +0300152 CLST *new_lcn, CLST *new_len)
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300153{
154 int err;
155 CLST flen, vcn0 = vcn, pre = pre_alloc ? *pre_alloc : 0;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300156 size_t cnt = run->count;
157
158 for (;;) {
159 err = ntfs_look_for_free_space(sbi, lcn, len + pre, &lcn, &flen,
160 opt);
161
162 if (err == -ENOSPC && pre) {
163 pre = 0;
164 if (*pre_alloc)
165 *pre_alloc = 0;
166 continue;
167 }
168
169 if (err)
170 goto out;
171
Konstantin Komarovc380b522022-10-07 14:02:36 +0300172 if (vcn == vcn0) {
173 /* Return the first fragment. */
174 if (new_lcn)
175 *new_lcn = lcn;
176 if (new_len)
177 *new_len = flen;
178 }
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300179
Kari Argillandere8b8e972021-08-03 14:57:09 +0300180 /* Add new fragment into run storage. */
Konstantin Komarovc380b522022-10-07 14:02:36 +0300181 if (!run_add_entry(run, vcn, lcn, flen, opt & ALLOCATE_MFT)) {
Konstantin Komarovd3624462021-08-31 16:57:40 +0300182 /* Undo last 'ntfs_look_for_free_space' */
Konstantin Komarov0e5b0442022-07-13 18:00:03 +0300183 mark_as_free_ex(sbi, lcn, len, false);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300184 err = -ENOMEM;
185 goto out;
186 }
187
Konstantin Komarovc380b522022-10-07 14:02:36 +0300188 if (opt & ALLOCATE_ZERO) {
189 u8 shift = sbi->cluster_bits - SECTOR_SHIFT;
190
191 err = blkdev_issue_zeroout(sbi->sb->s_bdev,
192 (sector_t)lcn << shift,
193 (sector_t)flen << shift,
194 GFP_NOFS, 0);
195 if (err)
196 goto out;
197 }
198
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300199 vcn += flen;
200
Konstantin Komarovc380b522022-10-07 14:02:36 +0300201 if (flen >= len || (opt & ALLOCATE_MFT) ||
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300202 (fr && run->count - cnt >= fr)) {
203 *alen = vcn - vcn0;
204 return 0;
205 }
206
207 len -= flen;
208 }
209
210out:
Konstantin Komarov78ab59f2021-08-31 18:52:39 +0300211 /* Undo 'ntfs_look_for_free_space' */
212 if (vcn - vcn0) {
213 run_deallocate_ex(sbi, run, vcn0, vcn - vcn0, NULL, false);
214 run_truncate(run, vcn0);
215 }
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300216
217 return err;
218}
219
220/*
Kari Argillandere8b8e972021-08-03 14:57:09 +0300221 * attr_make_nonresident
222 *
223 * If page is not NULL - it is already contains resident data
224 * and locked (called from ni_write_frame()).
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300225 */
226int attr_make_nonresident(struct ntfs_inode *ni, struct ATTRIB *attr,
227 struct ATTR_LIST_ENTRY *le, struct mft_inode *mi,
228 u64 new_size, struct runs_tree *run,
229 struct ATTRIB **ins_attr, struct page *page)
230{
231 struct ntfs_sb_info *sbi;
232 struct ATTRIB *attr_s;
233 struct MFT_REC *rec;
234 u32 used, asize, rsize, aoff, align;
235 bool is_data;
236 CLST len, alen;
237 char *next;
238 int err;
239
240 if (attr->non_res) {
241 *ins_attr = attr;
242 return 0;
243 }
244
245 sbi = mi->sbi;
246 rec = mi->mrec;
247 attr_s = NULL;
248 used = le32_to_cpu(rec->used);
249 asize = le32_to_cpu(attr->size);
250 next = Add2Ptr(attr, asize);
251 aoff = PtrOffset(rec, attr);
252 rsize = le32_to_cpu(attr->res.data_size);
253 is_data = attr->type == ATTR_DATA && !attr->name_len;
254
255 align = sbi->cluster_size;
256 if (is_attr_compressed(attr))
257 align <<= COMPRESSION_UNIT;
258 len = (rsize + align - 1) >> sbi->cluster_bits;
259
260 run_init(run);
261
Kari Argillandere8b8e972021-08-03 14:57:09 +0300262 /* Make a copy of original attribute. */
Kari Argillander195c52b2021-08-24 21:37:07 +0300263 attr_s = kmemdup(attr, asize, GFP_NOFS);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300264 if (!attr_s) {
265 err = -ENOMEM;
266 goto out;
267 }
268
269 if (!len) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300270 /* Empty resident -> Empty nonresident. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300271 alen = 0;
272 } else {
273 const char *data = resident_data(attr);
274
275 err = attr_allocate_clusters(sbi, run, 0, 0, len, NULL,
Konstantin Komarovc380b522022-10-07 14:02:36 +0300276 ALLOCATE_DEF, &alen, 0, NULL,
277 NULL);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300278 if (err)
279 goto out1;
280
281 if (!rsize) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300282 /* Empty resident -> Non empty nonresident. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300283 } else if (!is_data) {
Konstantin Komarov63544672021-09-09 13:15:20 +0300284 err = ntfs_sb_write_run(sbi, run, 0, data, rsize, 0);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300285 if (err)
286 goto out2;
287 } else if (!page) {
288 char *kaddr;
289
290 page = grab_cache_page(ni->vfs_inode.i_mapping, 0);
291 if (!page) {
292 err = -ENOMEM;
293 goto out2;
294 }
295 kaddr = kmap_atomic(page);
296 memcpy(kaddr, data, rsize);
297 memset(kaddr + rsize, 0, PAGE_SIZE - rsize);
298 kunmap_atomic(kaddr);
299 flush_dcache_page(page);
300 SetPageUptodate(page);
301 set_page_dirty(page);
302 unlock_page(page);
303 put_page(page);
304 }
305 }
306
Kari Argillandere8b8e972021-08-03 14:57:09 +0300307 /* Remove original attribute. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300308 used -= asize;
309 memmove(attr, Add2Ptr(attr, asize), used - aoff);
310 rec->used = cpu_to_le32(used);
311 mi->dirty = true;
312 if (le)
313 al_remove_le(ni, le);
314
315 err = ni_insert_nonresident(ni, attr_s->type, attr_name(attr_s),
316 attr_s->name_len, run, 0, alen,
Konstantin Komarovc1e0ab372022-05-13 18:25:04 +0300317 attr_s->flags, &attr, NULL, NULL);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300318 if (err)
319 goto out3;
320
Kari Argillander195c52b2021-08-24 21:37:07 +0300321 kfree(attr_s);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300322 attr->nres.data_size = cpu_to_le64(rsize);
323 attr->nres.valid_size = attr->nres.data_size;
324
325 *ins_attr = attr;
326
327 if (is_data)
328 ni->ni_flags &= ~NI_FLAG_RESIDENT;
329
Kari Argillandere8b8e972021-08-03 14:57:09 +0300330 /* Resident attribute becomes non resident. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300331 return 0;
332
333out3:
334 attr = Add2Ptr(rec, aoff);
335 memmove(next, attr, used - aoff);
336 memcpy(attr, attr_s, asize);
337 rec->used = cpu_to_le32(used + asize);
338 mi->dirty = true;
339out2:
Kari Argillandere8b8e972021-08-03 14:57:09 +0300340 /* Undo: do not trim new allocated clusters. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300341 run_deallocate(sbi, run, false);
342 run_close(run);
343out1:
Kari Argillander195c52b2021-08-24 21:37:07 +0300344 kfree(attr_s);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300345out:
346 return err;
347}
348
349/*
Kari Argillandere8b8e972021-08-03 14:57:09 +0300350 * attr_set_size_res - Helper for attr_set_size().
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300351 */
352static int attr_set_size_res(struct ntfs_inode *ni, struct ATTRIB *attr,
353 struct ATTR_LIST_ENTRY *le, struct mft_inode *mi,
354 u64 new_size, struct runs_tree *run,
355 struct ATTRIB **ins_attr)
356{
357 struct ntfs_sb_info *sbi = mi->sbi;
358 struct MFT_REC *rec = mi->mrec;
359 u32 used = le32_to_cpu(rec->used);
360 u32 asize = le32_to_cpu(attr->size);
361 u32 aoff = PtrOffset(rec, attr);
362 u32 rsize = le32_to_cpu(attr->res.data_size);
363 u32 tail = used - aoff - asize;
364 char *next = Add2Ptr(attr, asize);
Kari Argillanderfa3cacf2021-08-26 11:56:29 +0300365 s64 dsize = ALIGN(new_size, 8) - ALIGN(rsize, 8);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300366
367 if (dsize < 0) {
368 memmove(next + dsize, next, tail);
369 } else if (dsize > 0) {
370 if (used + dsize > sbi->max_bytes_per_attr)
371 return attr_make_nonresident(ni, attr, le, mi, new_size,
372 run, ins_attr, NULL);
373
374 memmove(next + dsize, next, tail);
375 memset(next, 0, dsize);
376 }
377
378 if (new_size > rsize)
379 memset(Add2Ptr(resident_data(attr), rsize), 0,
380 new_size - rsize);
381
382 rec->used = cpu_to_le32(used + dsize);
383 attr->size = cpu_to_le32(asize + dsize);
384 attr->res.data_size = cpu_to_le32(new_size);
385 mi->dirty = true;
386 *ins_attr = attr;
387
388 return 0;
389}
390
391/*
Kari Argillandere8b8e972021-08-03 14:57:09 +0300392 * attr_set_size - Change the size of attribute.
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300393 *
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300394 * Extend:
Kari Argillandere8b8e972021-08-03 14:57:09 +0300395 * - Sparse/compressed: No allocated clusters.
396 * - Normal: Append allocated and preallocated new clusters.
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300397 * Shrink:
Kari Argillandere8b8e972021-08-03 14:57:09 +0300398 * - No deallocate if @keep_prealloc is set.
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300399 */
400int attr_set_size(struct ntfs_inode *ni, enum ATTR_TYPE type,
401 const __le16 *name, u8 name_len, struct runs_tree *run,
402 u64 new_size, const u64 *new_valid, bool keep_prealloc,
403 struct ATTRIB **ret)
404{
405 int err = 0;
406 struct ntfs_sb_info *sbi = ni->mi.sbi;
407 u8 cluster_bits = sbi->cluster_bits;
Konstantin Komarov96de65a2023-01-17 15:01:00 +0400408 bool is_mft = ni->mi.rno == MFT_REC_MFT && type == ATTR_DATA &&
409 !name_len;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300410 u64 old_valid, old_size, old_alloc, new_alloc, new_alloc_tmp;
411 struct ATTRIB *attr = NULL, *attr_b;
412 struct ATTR_LIST_ENTRY *le, *le_b;
413 struct mft_inode *mi, *mi_b;
414 CLST alen, vcn, lcn, new_alen, old_alen, svcn, evcn;
415 CLST next_svcn, pre_alloc = -1, done = 0;
Konstantin Komarov0e5b0442022-07-13 18:00:03 +0300416 bool is_ext, is_bad = false;
Konstantin Komarovad26a9c2022-10-07 20:08:06 +0300417 bool dirty = false;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300418 u32 align;
419 struct MFT_REC *rec;
420
421again:
Konstantin Komarov0e5b0442022-07-13 18:00:03 +0300422 alen = 0;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300423 le_b = NULL;
424 attr_b = ni_find_attr(ni, NULL, &le_b, type, name, name_len, NULL,
425 &mi_b);
426 if (!attr_b) {
427 err = -ENOENT;
Konstantin Komarov0e5b0442022-07-13 18:00:03 +0300428 goto bad_inode;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300429 }
430
431 if (!attr_b->non_res) {
432 err = attr_set_size_res(ni, attr_b, le_b, mi_b, new_size, run,
433 &attr_b);
Konstantin Komarov0e5b0442022-07-13 18:00:03 +0300434 if (err)
435 return err;
436
437 /* Return if file is still resident. */
Konstantin Komarovad26a9c2022-10-07 20:08:06 +0300438 if (!attr_b->non_res) {
439 dirty = true;
Konstantin Komarov0e5b0442022-07-13 18:00:03 +0300440 goto ok1;
Konstantin Komarovad26a9c2022-10-07 20:08:06 +0300441 }
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300442
Kari Argillandere8b8e972021-08-03 14:57:09 +0300443 /* Layout of records may be changed, so do a full search. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300444 goto again;
445 }
446
447 is_ext = is_attr_ext(attr_b);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300448 align = sbi->cluster_size;
Konstantin Komarovce46ae02021-10-01 18:48:49 +0300449 if (is_ext)
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300450 align <<= attr_b->nres.c_unit;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300451
452 old_valid = le64_to_cpu(attr_b->nres.valid_size);
453 old_size = le64_to_cpu(attr_b->nres.data_size);
454 old_alloc = le64_to_cpu(attr_b->nres.alloc_size);
Konstantin Komarov0e5b0442022-07-13 18:00:03 +0300455
456again_1:
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300457 old_alen = old_alloc >> cluster_bits;
458
459 new_alloc = (new_size + align - 1) & ~(u64)(align - 1);
460 new_alen = new_alloc >> cluster_bits;
461
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300462 if (keep_prealloc && new_size < old_size) {
463 attr_b->nres.data_size = cpu_to_le64(new_size);
Konstantin Komarovad26a9c2022-10-07 20:08:06 +0300464 mi_b->dirty = dirty = true;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300465 goto ok;
466 }
467
468 vcn = old_alen - 1;
469
470 svcn = le64_to_cpu(attr_b->nres.svcn);
471 evcn = le64_to_cpu(attr_b->nres.evcn);
472
473 if (svcn <= vcn && vcn <= evcn) {
474 attr = attr_b;
475 le = le_b;
476 mi = mi_b;
477 } else if (!le_b) {
478 err = -EINVAL;
Konstantin Komarov0e5b0442022-07-13 18:00:03 +0300479 goto bad_inode;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300480 } else {
481 le = le_b;
482 attr = ni_find_attr(ni, attr_b, &le, type, name, name_len, &vcn,
483 &mi);
484 if (!attr) {
485 err = -EINVAL;
Konstantin Komarov0e5b0442022-07-13 18:00:03 +0300486 goto bad_inode;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300487 }
488
489next_le_1:
490 svcn = le64_to_cpu(attr->nres.svcn);
491 evcn = le64_to_cpu(attr->nres.evcn);
492 }
Konstantin Komarov0e5b0442022-07-13 18:00:03 +0300493 /*
494 * Here we have:
495 * attr,mi,le - last attribute segment (containing 'vcn').
496 * attr_b,mi_b,le_b - base (primary) attribute segment.
497 */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300498next_le:
499 rec = mi->mrec;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300500 err = attr_load_runs(attr, ni, run, NULL);
501 if (err)
502 goto out;
503
504 if (new_size > old_size) {
505 CLST to_allocate;
506 size_t free;
507
508 if (new_alloc <= old_alloc) {
509 attr_b->nres.data_size = cpu_to_le64(new_size);
Konstantin Komarovad26a9c2022-10-07 20:08:06 +0300510 mi_b->dirty = dirty = true;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300511 goto ok;
512 }
513
Konstantin Komarov0e5b0442022-07-13 18:00:03 +0300514 /*
515 * Add clusters. In simple case we have to:
516 * - allocate space (vcn, lcn, len)
517 * - update packed run in 'mi'
518 * - update attr->nres.evcn
519 * - update attr_b->nres.data_size/attr_b->nres.alloc_size
520 */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300521 to_allocate = new_alen - old_alen;
522add_alloc_in_same_attr_seg:
523 lcn = 0;
524 if (is_mft) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300525 /* MFT allocates clusters from MFT zone. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300526 pre_alloc = 0;
527 } else if (is_ext) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300528 /* No preallocate for sparse/compress. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300529 pre_alloc = 0;
530 } else if (pre_alloc == -1) {
531 pre_alloc = 0;
532 if (type == ATTR_DATA && !name_len &&
Kari Argillander564c97b2021-09-07 18:35:51 +0300533 sbi->options->prealloc) {
Konstantin Komarov96de65a2023-01-17 15:01:00 +0400534 pre_alloc = bytes_to_cluster(
535 sbi, get_pre_allocated(
536 new_size)) -
537 new_alen;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300538 }
539
Kari Argillandere8b8e972021-08-03 14:57:09 +0300540 /* Get the last LCN to allocate from. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300541 if (old_alen &&
542 !run_lookup_entry(run, vcn, &lcn, NULL, NULL)) {
543 lcn = SPARSE_LCN;
544 }
545
546 if (lcn == SPARSE_LCN)
547 lcn = 0;
548 else if (lcn)
549 lcn += 1;
550
551 free = wnd_zeroes(&sbi->used.bitmap);
552 if (to_allocate > free) {
553 err = -ENOSPC;
554 goto out;
555 }
556
557 if (pre_alloc && to_allocate + pre_alloc > free)
558 pre_alloc = 0;
559 }
560
561 vcn = old_alen;
562
563 if (is_ext) {
564 if (!run_add_entry(run, vcn, SPARSE_LCN, to_allocate,
565 false)) {
566 err = -ENOMEM;
567 goto out;
568 }
569 alen = to_allocate;
570 } else {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300571 /* ~3 bytes per fragment. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300572 err = attr_allocate_clusters(
573 sbi, run, vcn, lcn, to_allocate, &pre_alloc,
Konstantin Komarovc380b522022-10-07 14:02:36 +0300574 is_mft ? ALLOCATE_MFT : ALLOCATE_DEF, &alen,
Konstantin Komarov96de65a2023-01-17 15:01:00 +0400575 is_mft ? 0 :
Konstantin Komarovf0377762023-05-08 12:22:05 +0400576 (sbi->record_size -
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300577 le32_to_cpu(rec->used) + 8) /
578 3 +
579 1,
Konstantin Komarovc380b522022-10-07 14:02:36 +0300580 NULL, NULL);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300581 if (err)
582 goto out;
583 }
584
585 done += alen;
586 vcn += alen;
587 if (to_allocate > alen)
588 to_allocate -= alen;
589 else
590 to_allocate = 0;
591
592pack_runs:
593 err = mi_pack_runs(mi, attr, run, vcn - svcn);
594 if (err)
Konstantin Komarov0e5b0442022-07-13 18:00:03 +0300595 goto undo_1;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300596
597 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
598 new_alloc_tmp = (u64)next_svcn << cluster_bits;
599 attr_b->nres.alloc_size = cpu_to_le64(new_alloc_tmp);
Konstantin Komarovad26a9c2022-10-07 20:08:06 +0300600 mi_b->dirty = dirty = true;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300601
602 if (next_svcn >= vcn && !to_allocate) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300603 /* Normal way. Update attribute and exit. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300604 attr_b->nres.data_size = cpu_to_le64(new_size);
605 goto ok;
606 }
607
Kari Argillandere8b8e972021-08-03 14:57:09 +0300608 /* At least two MFT to avoid recursive loop. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300609 if (is_mft && next_svcn == vcn &&
610 ((u64)done << sbi->cluster_bits) >= 2 * sbi->record_size) {
611 new_size = new_alloc_tmp;
612 attr_b->nres.data_size = attr_b->nres.alloc_size;
613 goto ok;
614 }
615
616 if (le32_to_cpu(rec->used) < sbi->record_size) {
617 old_alen = next_svcn;
618 evcn = old_alen - 1;
619 goto add_alloc_in_same_attr_seg;
620 }
621
622 attr_b->nres.data_size = attr_b->nres.alloc_size;
623 if (new_alloc_tmp < old_valid)
624 attr_b->nres.valid_size = attr_b->nres.data_size;
625
626 if (type == ATTR_LIST) {
627 err = ni_expand_list(ni);
628 if (err)
Konstantin Komarov0e5b0442022-07-13 18:00:03 +0300629 goto undo_2;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300630 if (next_svcn < vcn)
631 goto pack_runs;
632
Kari Argillandere8b8e972021-08-03 14:57:09 +0300633 /* Layout of records is changed. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300634 goto again;
635 }
636
637 if (!ni->attr_list.size) {
638 err = ni_create_attr_list(ni);
Konstantin Komarov0e5b0442022-07-13 18:00:03 +0300639 /* In case of error layout of records is not changed. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300640 if (err)
Konstantin Komarov0e5b0442022-07-13 18:00:03 +0300641 goto undo_2;
Kari Argillandere8b8e972021-08-03 14:57:09 +0300642 /* Layout of records is changed. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300643 }
644
645 if (next_svcn >= vcn) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300646 /* This is MFT data, repeat. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300647 goto again;
648 }
649
Kari Argillandere8b8e972021-08-03 14:57:09 +0300650 /* Insert new attribute segment. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300651 err = ni_insert_nonresident(ni, type, name, name_len, run,
652 next_svcn, vcn - next_svcn,
Konstantin Komarovc1e0ab372022-05-13 18:25:04 +0300653 attr_b->flags, &attr, &mi, NULL);
Konstantin Komarov0e5b0442022-07-13 18:00:03 +0300654
655 /*
656 * Layout of records maybe changed.
657 * Find base attribute to update.
658 */
659 le_b = NULL;
660 attr_b = ni_find_attr(ni, NULL, &le_b, type, name, name_len,
661 NULL, &mi_b);
662 if (!attr_b) {
663 err = -EINVAL;
664 goto bad_inode;
665 }
666
667 if (err) {
668 /* ni_insert_nonresident failed. */
669 attr = NULL;
670 goto undo_2;
671 }
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300672
673 if (!is_mft)
674 run_truncate_head(run, evcn + 1);
675
676 svcn = le64_to_cpu(attr->nres.svcn);
677 evcn = le64_to_cpu(attr->nres.evcn);
678
Kari Argillandere8b8e972021-08-03 14:57:09 +0300679 /*
Konstantin Komarov0e5b0442022-07-13 18:00:03 +0300680 * Attribute is in consistency state.
681 * Save this point to restore to if next steps fail.
Kari Argillandere8b8e972021-08-03 14:57:09 +0300682 */
Konstantin Komarov0e5b0442022-07-13 18:00:03 +0300683 old_valid = old_size = old_alloc = (u64)vcn << cluster_bits;
684 attr_b->nres.valid_size = attr_b->nres.data_size =
685 attr_b->nres.alloc_size = cpu_to_le64(old_size);
Konstantin Komarovad26a9c2022-10-07 20:08:06 +0300686 mi_b->dirty = dirty = true;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300687 goto again_1;
688 }
689
690 if (new_size != old_size ||
691 (new_alloc != old_alloc && !keep_prealloc)) {
Konstantin Komarov0e5b0442022-07-13 18:00:03 +0300692 /*
693 * Truncate clusters. In simple case we have to:
694 * - update packed run in 'mi'
695 * - update attr->nres.evcn
696 * - update attr_b->nres.data_size/attr_b->nres.alloc_size
697 * - mark and trim clusters as free (vcn, lcn, len)
698 */
699 CLST dlen = 0;
700
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300701 vcn = max(svcn, new_alen);
702 new_alloc_tmp = (u64)vcn << cluster_bits;
703
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300704 if (vcn > svcn) {
705 err = mi_pack_runs(mi, attr, run, vcn - svcn);
706 if (err)
707 goto out;
708 } else if (le && le->vcn) {
709 u16 le_sz = le16_to_cpu(le->size);
710
711 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +0300712 * NOTE: List entries for one attribute are always
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300713 * the same size. We deal with last entry (vcn==0)
714 * and it is not first in entries array
Kari Argillandere8b8e972021-08-03 14:57:09 +0300715 * (list entry for std attribute always first).
716 * So it is safe to step back.
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300717 */
Konstantin Komarov78ab59f2021-08-31 18:52:39 +0300718 mi_remove_attr(NULL, mi, attr);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300719
720 if (!al_remove_le(ni, le)) {
721 err = -EINVAL;
Konstantin Komarov0e5b0442022-07-13 18:00:03 +0300722 goto bad_inode;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300723 }
724
725 le = (struct ATTR_LIST_ENTRY *)((u8 *)le - le_sz);
726 } else {
727 attr->nres.evcn = cpu_to_le64((u64)vcn - 1);
728 mi->dirty = true;
729 }
730
731 attr_b->nres.alloc_size = cpu_to_le64(new_alloc_tmp);
732
733 if (vcn == new_alen) {
734 attr_b->nres.data_size = cpu_to_le64(new_size);
735 if (new_size < old_valid)
736 attr_b->nres.valid_size =
737 attr_b->nres.data_size;
738 } else {
739 if (new_alloc_tmp <=
740 le64_to_cpu(attr_b->nres.data_size))
741 attr_b->nres.data_size =
742 attr_b->nres.alloc_size;
743 if (new_alloc_tmp <
744 le64_to_cpu(attr_b->nres.valid_size))
745 attr_b->nres.valid_size =
746 attr_b->nres.alloc_size;
747 }
Konstantin Komarovad26a9c2022-10-07 20:08:06 +0300748 mi_b->dirty = dirty = true;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300749
Konstantin Komarov0e5b0442022-07-13 18:00:03 +0300750 err = run_deallocate_ex(sbi, run, vcn, evcn - vcn + 1, &dlen,
751 true);
752 if (err)
753 goto out;
754
755 if (is_ext) {
756 /* dlen - really deallocated clusters. */
757 le64_sub_cpu(&attr_b->nres.total_size,
758 ((u64)dlen << cluster_bits));
759 }
760
761 run_truncate(run, vcn);
762
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300763 if (new_alloc_tmp <= new_alloc)
764 goto ok;
765
766 old_size = new_alloc_tmp;
767 vcn = svcn - 1;
768
769 if (le == le_b) {
770 attr = attr_b;
771 mi = mi_b;
772 evcn = svcn - 1;
773 svcn = 0;
774 goto next_le;
775 }
776
777 if (le->type != type || le->name_len != name_len ||
778 memcmp(le_name(le), name, name_len * sizeof(short))) {
779 err = -EINVAL;
Konstantin Komarov0e5b0442022-07-13 18:00:03 +0300780 goto bad_inode;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300781 }
782
783 err = ni_load_mi(ni, le, &mi);
784 if (err)
785 goto out;
786
787 attr = mi_find_attr(mi, NULL, type, name, name_len, &le->id);
788 if (!attr) {
789 err = -EINVAL;
Konstantin Komarov0e5b0442022-07-13 18:00:03 +0300790 goto bad_inode;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300791 }
792 goto next_le_1;
793 }
794
795ok:
796 if (new_valid) {
797 __le64 valid = cpu_to_le64(min(*new_valid, new_size));
798
799 if (attr_b->nres.valid_size != valid) {
800 attr_b->nres.valid_size = valid;
801 mi_b->dirty = true;
802 }
803 }
804
Konstantin Komarov0e5b0442022-07-13 18:00:03 +0300805ok1:
806 if (ret)
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300807 *ret = attr_b;
808
Konstantin Komarov0e5b0442022-07-13 18:00:03 +0300809 if (((type == ATTR_DATA && !name_len) ||
810 (type == ATTR_ALLOC && name == I30_NAME))) {
Konstantin Komarovad26a9c2022-10-07 20:08:06 +0300811 /* Update inode_set_bytes. */
Konstantin Komarov0e5b0442022-07-13 18:00:03 +0300812 if (attr_b->non_res) {
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300813 new_alloc = le64_to_cpu(attr_b->nres.alloc_size);
814 if (inode_get_bytes(&ni->vfs_inode) != new_alloc) {
815 inode_set_bytes(&ni->vfs_inode, new_alloc);
816 dirty = true;
817 }
818 }
819
Konstantin Komarovad26a9c2022-10-07 20:08:06 +0300820 /* Don't forget to update duplicate information in parent. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300821 if (dirty) {
822 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
823 mark_inode_dirty(&ni->vfs_inode);
824 }
825 }
826
Konstantin Komarov0e5b0442022-07-13 18:00:03 +0300827 return 0;
828
829undo_2:
830 vcn -= alen;
831 attr_b->nres.data_size = cpu_to_le64(old_size);
832 attr_b->nres.valid_size = cpu_to_le64(old_valid);
833 attr_b->nres.alloc_size = cpu_to_le64(old_alloc);
834
835 /* Restore 'attr' and 'mi'. */
836 if (attr)
837 goto restore_run;
838
839 if (le64_to_cpu(attr_b->nres.svcn) <= svcn &&
840 svcn <= le64_to_cpu(attr_b->nres.evcn)) {
841 attr = attr_b;
842 le = le_b;
843 mi = mi_b;
844 } else if (!le_b) {
845 err = -EINVAL;
846 goto bad_inode;
847 } else {
848 le = le_b;
849 attr = ni_find_attr(ni, attr_b, &le, type, name, name_len,
850 &svcn, &mi);
851 if (!attr)
852 goto bad_inode;
853 }
854
855restore_run:
856 if (mi_pack_runs(mi, attr, run, evcn - svcn + 1))
857 is_bad = true;
858
859undo_1:
860 run_deallocate_ex(sbi, run, vcn, alen, NULL, false);
861
862 run_truncate(run, vcn);
863out:
864 if (is_bad) {
865bad_inode:
866 _ntfs_bad_inode(&ni->vfs_inode);
867 }
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300868 return err;
869}
870
Konstantin Komarovc380b522022-10-07 14:02:36 +0300871/*
872 * attr_data_get_block - Returns 'lcn' and 'len' for given 'vcn'.
873 *
874 * @new == NULL means just to get current mapping for 'vcn'
875 * @new != NULL means allocate real cluster if 'vcn' maps to hole
876 * @zero - zeroout new allocated clusters
877 *
878 * NOTE:
879 * - @new != NULL is called only for sparsed or compressed attributes.
880 * - new allocated clusters are zeroed via blkdev_issue_zeroout.
881 */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300882int attr_data_get_block(struct ntfs_inode *ni, CLST vcn, CLST clen, CLST *lcn,
Konstantin Komarovc380b522022-10-07 14:02:36 +0300883 CLST *len, bool *new, bool zero)
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300884{
885 int err = 0;
886 struct runs_tree *run = &ni->file.run;
887 struct ntfs_sb_info *sbi;
888 u8 cluster_bits;
889 struct ATTRIB *attr = NULL, *attr_b;
890 struct ATTR_LIST_ENTRY *le, *le_b;
891 struct mft_inode *mi, *mi_b;
Konstantin Komarovc380b522022-10-07 14:02:36 +0300892 CLST hint, svcn, to_alloc, evcn1, next_svcn, asize, end, vcn0, alen;
Konstantin Komarov910013f2022-10-07 20:20:14 +0300893 CLST alloc, evcn;
Konstantin Komarovc380b522022-10-07 14:02:36 +0300894 unsigned fr;
Konstantin Komarov910013f2022-10-07 20:20:14 +0300895 u64 total_size, total_size0;
896 int step = 0;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300897
898 if (new)
899 *new = false;
900
Konstantin Komarovc380b522022-10-07 14:02:36 +0300901 /* Try to find in cache. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300902 down_read(&ni->file.run_lock);
Konstantin Komarovc380b522022-10-07 14:02:36 +0300903 if (!run_lookup_entry(run, vcn, lcn, len, NULL))
904 *len = 0;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300905 up_read(&ni->file.run_lock);
906
Konstantin Komarovc380b522022-10-07 14:02:36 +0300907 if (*len) {
908 if (*lcn != SPARSE_LCN || !new)
909 return 0; /* Fast normal way without allocation. */
910 else if (clen > *len)
911 clen = *len;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300912 }
913
Konstantin Komarovc380b522022-10-07 14:02:36 +0300914 /* No cluster in cache or we need to allocate cluster in hole. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300915 sbi = ni->mi.sbi;
916 cluster_bits = sbi->cluster_bits;
917
918 ni_lock(ni);
919 down_write(&ni->file.run_lock);
920
921 le_b = NULL;
922 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
923 if (!attr_b) {
924 err = -ENOENT;
925 goto out;
926 }
927
928 if (!attr_b->non_res) {
929 *lcn = RESIDENT_LCN;
930 *len = 1;
931 goto out;
932 }
933
Konstantin Komarovc1e0ab372022-05-13 18:25:04 +0300934 asize = le64_to_cpu(attr_b->nres.alloc_size) >> cluster_bits;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300935 if (vcn >= asize) {
Konstantin Komarov910013f2022-10-07 20:20:14 +0300936 if (new) {
937 err = -EINVAL;
938 } else {
939 *len = 1;
940 *lcn = SPARSE_LCN;
941 }
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300942 goto out;
943 }
944
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300945 svcn = le64_to_cpu(attr_b->nres.svcn);
946 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
947
948 attr = attr_b;
949 le = le_b;
950 mi = mi_b;
951
952 if (le_b && (vcn < svcn || evcn1 <= vcn)) {
953 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
954 &mi);
955 if (!attr) {
956 err = -EINVAL;
957 goto out;
958 }
959 svcn = le64_to_cpu(attr->nres.svcn);
960 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
961 }
962
Konstantin Komarovc380b522022-10-07 14:02:36 +0300963 /* Load in cache actual information. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300964 err = attr_load_runs(attr, ni, run, NULL);
965 if (err)
966 goto out;
967
Konstantin Komarovc380b522022-10-07 14:02:36 +0300968 if (!*len) {
969 if (run_lookup_entry(run, vcn, lcn, len, NULL)) {
970 if (*lcn != SPARSE_LCN || !new)
971 goto ok; /* Slow normal way without allocation. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300972
Konstantin Komarovc380b522022-10-07 14:02:36 +0300973 if (clen > *len)
974 clen = *len;
975 } else if (!new) {
976 /* Here we may return -ENOENT.
977 * In any case caller gets zero length. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300978 goto ok;
979 }
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300980 }
981
982 if (!is_attr_ext(attr_b)) {
Konstantin Komarovc380b522022-10-07 14:02:36 +0300983 /* The code below only for sparsed or compressed attributes. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +0300984 err = -EINVAL;
985 goto out;
986 }
987
Konstantin Komarovc380b522022-10-07 14:02:36 +0300988 vcn0 = vcn;
989 to_alloc = clen;
990 fr = (sbi->record_size - le32_to_cpu(mi->mrec->used) + 8) / 3 + 1;
991 /* Allocate frame aligned clusters.
992 * ntfs.sys usually uses 16 clusters per frame for sparsed or compressed.
993 * ntfs3 uses 1 cluster per frame for new created sparsed files. */
994 if (attr_b->nres.c_unit) {
995 CLST clst_per_frame = 1u << attr_b->nres.c_unit;
996 CLST cmask = ~(clst_per_frame - 1);
997
998 /* Get frame aligned vcn and to_alloc. */
999 vcn = vcn0 & cmask;
1000 to_alloc = ((vcn0 + clen + clst_per_frame - 1) & cmask) - vcn;
1001 if (fr < clst_per_frame)
1002 fr = clst_per_frame;
1003 zero = true;
1004
1005 /* Check if 'vcn' and 'vcn0' in different attribute segments. */
1006 if (vcn < svcn || evcn1 <= vcn) {
1007 /* Load attribute for truncated vcn. */
1008 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0,
1009 &vcn, &mi);
1010 if (!attr) {
1011 err = -EINVAL;
1012 goto out;
1013 }
1014 svcn = le64_to_cpu(attr->nres.svcn);
1015 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
1016 err = attr_load_runs(attr, ni, run, NULL);
1017 if (err)
1018 goto out;
1019 }
1020 }
1021
1022 if (vcn + to_alloc > asize)
1023 to_alloc = asize - vcn;
1024
Kari Argillandere8b8e972021-08-03 14:57:09 +03001025 /* Get the last LCN to allocate from. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001026 hint = 0;
1027
1028 if (vcn > evcn1) {
1029 if (!run_add_entry(run, evcn1, SPARSE_LCN, vcn - evcn1,
1030 false)) {
1031 err = -ENOMEM;
1032 goto out;
1033 }
1034 } else if (vcn && !run_lookup_entry(run, vcn - 1, &hint, NULL, NULL)) {
1035 hint = -1;
1036 }
1037
Konstantin Komarovc380b522022-10-07 14:02:36 +03001038 /* Allocate and zeroout new clusters. */
1039 err = attr_allocate_clusters(sbi, run, vcn, hint + 1, to_alloc, NULL,
1040 zero ? ALLOCATE_ZERO : ALLOCATE_DEF, &alen,
1041 fr, lcn, len);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001042 if (err)
1043 goto out;
1044 *new = true;
Konstantin Komarov910013f2022-10-07 20:20:14 +03001045 step = 1;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001046
Konstantin Komarovc380b522022-10-07 14:02:36 +03001047 end = vcn + alen;
Konstantin Komarov910013f2022-10-07 20:20:14 +03001048 /* Save 'total_size0' to restore if error. */
1049 total_size0 = le64_to_cpu(attr_b->nres.total_size);
1050 total_size = total_size0 + ((u64)alen << cluster_bits);
Konstantin Komarovc380b522022-10-07 14:02:36 +03001051
1052 if (vcn != vcn0) {
1053 if (!run_lookup_entry(run, vcn0, lcn, len, NULL)) {
1054 err = -EINVAL;
1055 goto out;
1056 }
1057 if (*lcn == SPARSE_LCN) {
1058 /* Internal error. Should not happened. */
1059 WARN_ON(1);
1060 err = -EINVAL;
1061 goto out;
1062 }
1063 /* Check case when vcn0 + len overlaps new allocated clusters. */
1064 if (vcn0 + *len > end)
1065 *len = end - vcn0;
1066 }
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001067
1068repack:
1069 err = mi_pack_runs(mi, attr, run, max(end, evcn1) - svcn);
1070 if (err)
1071 goto out;
1072
1073 attr_b->nres.total_size = cpu_to_le64(total_size);
1074 inode_set_bytes(&ni->vfs_inode, total_size);
1075 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
1076
1077 mi_b->dirty = true;
1078 mark_inode_dirty(&ni->vfs_inode);
1079
Kari Argillandere8b8e972021-08-03 14:57:09 +03001080 /* Stored [vcn : next_svcn) from [vcn : end). */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001081 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1082
1083 if (end <= evcn1) {
1084 if (next_svcn == evcn1) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001085 /* Normal way. Update attribute and exit. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001086 goto ok;
1087 }
Kari Argillandere8b8e972021-08-03 14:57:09 +03001088 /* Add new segment [next_svcn : evcn1 - next_svcn). */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001089 if (!ni->attr_list.size) {
1090 err = ni_create_attr_list(ni);
1091 if (err)
Konstantin Komarov910013f2022-10-07 20:20:14 +03001092 goto undo1;
Kari Argillandere8b8e972021-08-03 14:57:09 +03001093 /* Layout of records is changed. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001094 le_b = NULL;
1095 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL,
1096 0, NULL, &mi_b);
1097 if (!attr_b) {
1098 err = -ENOENT;
1099 goto out;
1100 }
1101
1102 attr = attr_b;
1103 le = le_b;
1104 mi = mi_b;
1105 goto repack;
1106 }
1107 }
1108
Konstantin Komarov910013f2022-10-07 20:20:14 +03001109 /*
1110 * The code below may require additional cluster (to extend attribute list)
1111 * and / or one MFT record
1112 * It is too complex to undo operations if -ENOSPC occurs deep inside
1113 * in 'ni_insert_nonresident'.
1114 * Return in advance -ENOSPC here if there are no free cluster and no free MFT.
1115 */
1116 if (!ntfs_check_for_free_space(sbi, 1, 1)) {
1117 /* Undo step 1. */
1118 err = -ENOSPC;
1119 goto undo1;
1120 }
1121
1122 step = 2;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001123 svcn = evcn1;
1124
Kari Argillandere8b8e972021-08-03 14:57:09 +03001125 /* Estimate next attribute. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001126 attr = ni_find_attr(ni, attr, &le, ATTR_DATA, NULL, 0, &svcn, &mi);
1127
Konstantin Komarov910013f2022-10-07 20:20:14 +03001128 if (!attr) {
1129 /* Insert new attribute segment. */
1130 goto ins_ext;
1131 }
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001132
Konstantin Komarov910013f2022-10-07 20:20:14 +03001133 /* Try to update existed attribute segment. */
1134 alloc = bytes_to_cluster(sbi, le64_to_cpu(attr_b->nres.alloc_size));
1135 evcn = le64_to_cpu(attr->nres.evcn);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001136
Konstantin Komarov910013f2022-10-07 20:20:14 +03001137 if (end < next_svcn)
1138 end = next_svcn;
1139 while (end > evcn) {
1140 /* Remove segment [svcn : evcn). */
1141 mi_remove_attr(NULL, mi, attr);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001142
Konstantin Komarov910013f2022-10-07 20:20:14 +03001143 if (!al_remove_le(ni, le)) {
1144 err = -EINVAL;
1145 goto out;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001146 }
1147
Konstantin Komarov910013f2022-10-07 20:20:14 +03001148 if (evcn + 1 >= alloc) {
1149 /* Last attribute segment. */
1150 evcn1 = evcn + 1;
1151 goto ins_ext;
1152 }
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001153
Konstantin Komarov910013f2022-10-07 20:20:14 +03001154 if (ni_load_mi(ni, le, &mi)) {
1155 attr = NULL;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001156 goto out;
Konstantin Komarov910013f2022-10-07 20:20:14 +03001157 }
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001158
Konstantin Komarov910013f2022-10-07 20:20:14 +03001159 attr = mi_find_attr(mi, NULL, ATTR_DATA, NULL, 0, &le->id);
1160 if (!attr) {
1161 err = -EINVAL;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001162 goto out;
Konstantin Komarov910013f2022-10-07 20:20:14 +03001163 }
1164 svcn = le64_to_cpu(attr->nres.svcn);
1165 evcn = le64_to_cpu(attr->nres.evcn);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001166 }
Konstantin Komarov910013f2022-10-07 20:20:14 +03001167
1168 if (end < svcn)
1169 end = svcn;
1170
1171 err = attr_load_runs(attr, ni, run, &end);
1172 if (err)
1173 goto out;
1174
1175 evcn1 = evcn + 1;
1176 attr->nres.svcn = cpu_to_le64(next_svcn);
1177 err = mi_pack_runs(mi, attr, run, evcn1 - next_svcn);
1178 if (err)
1179 goto out;
1180
1181 le->vcn = cpu_to_le64(next_svcn);
1182 ni->attr_list.dirty = true;
1183 mi->dirty = true;
1184 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1185
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001186ins_ext:
1187 if (evcn1 > next_svcn) {
1188 err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
1189 next_svcn, evcn1 - next_svcn,
Konstantin Komarovc1e0ab372022-05-13 18:25:04 +03001190 attr_b->flags, &attr, &mi, NULL);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001191 if (err)
1192 goto out;
1193 }
1194ok:
1195 run_truncate_around(run, vcn);
1196out:
Konstantin Komarov910013f2022-10-07 20:20:14 +03001197 if (err && step > 1) {
1198 /* Too complex to restore. */
1199 _ntfs_bad_inode(&ni->vfs_inode);
1200 }
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001201 up_write(&ni->file.run_lock);
1202 ni_unlock(ni);
1203
1204 return err;
Konstantin Komarov910013f2022-10-07 20:20:14 +03001205
1206undo1:
1207 /* Undo step1. */
1208 attr_b->nres.total_size = cpu_to_le64(total_size0);
1209 inode_set_bytes(&ni->vfs_inode, total_size0);
1210
1211 if (run_deallocate_ex(sbi, run, vcn, alen, NULL, false) ||
1212 !run_add_entry(run, vcn, SPARSE_LCN, alen, false) ||
1213 mi_pack_runs(mi, attr, run, max(end, evcn1) - svcn)) {
1214 _ntfs_bad_inode(&ni->vfs_inode);
1215 }
1216 goto out;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001217}
1218
1219int attr_data_read_resident(struct ntfs_inode *ni, struct page *page)
1220{
1221 u64 vbo;
1222 struct ATTRIB *attr;
1223 u32 data_size;
1224
1225 attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, NULL);
1226 if (!attr)
1227 return -EINVAL;
1228
1229 if (attr->non_res)
1230 return E_NTFS_NONRESIDENT;
1231
1232 vbo = page->index << PAGE_SHIFT;
1233 data_size = le32_to_cpu(attr->res.data_size);
1234 if (vbo < data_size) {
1235 const char *data = resident_data(attr);
1236 char *kaddr = kmap_atomic(page);
1237 u32 use = data_size - vbo;
1238
1239 if (use > PAGE_SIZE)
1240 use = PAGE_SIZE;
1241
1242 memcpy(kaddr, data + vbo, use);
1243 memset(kaddr + use, 0, PAGE_SIZE - use);
1244 kunmap_atomic(kaddr);
1245 flush_dcache_page(page);
1246 SetPageUptodate(page);
1247 } else if (!PageUptodate(page)) {
1248 zero_user_segment(page, 0, PAGE_SIZE);
1249 SetPageUptodate(page);
1250 }
1251
1252 return 0;
1253}
1254
1255int attr_data_write_resident(struct ntfs_inode *ni, struct page *page)
1256{
1257 u64 vbo;
1258 struct mft_inode *mi;
1259 struct ATTRIB *attr;
1260 u32 data_size;
1261
1262 attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi);
1263 if (!attr)
1264 return -EINVAL;
1265
1266 if (attr->non_res) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001267 /* Return special error code to check this case. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001268 return E_NTFS_NONRESIDENT;
1269 }
1270
1271 vbo = page->index << PAGE_SHIFT;
1272 data_size = le32_to_cpu(attr->res.data_size);
1273 if (vbo < data_size) {
1274 char *data = resident_data(attr);
1275 char *kaddr = kmap_atomic(page);
1276 u32 use = data_size - vbo;
1277
1278 if (use > PAGE_SIZE)
1279 use = PAGE_SIZE;
1280 memcpy(data + vbo, kaddr, use);
1281 kunmap_atomic(kaddr);
1282 mi->dirty = true;
1283 }
1284 ni->i_valid = data_size;
1285
1286 return 0;
1287}
1288
1289/*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001290 * attr_load_runs_vcn - Load runs with VCN.
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001291 */
1292int attr_load_runs_vcn(struct ntfs_inode *ni, enum ATTR_TYPE type,
1293 const __le16 *name, u8 name_len, struct runs_tree *run,
1294 CLST vcn)
1295{
1296 struct ATTRIB *attr;
1297 int err;
1298 CLST svcn, evcn;
1299 u16 ro;
1300
Edward Lo26816312022-08-07 01:05:18 +08001301 if (!ni) {
1302 /* Is record corrupted? */
1303 return -ENOENT;
1304 }
1305
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001306 attr = ni_find_attr(ni, NULL, NULL, type, name, name_len, &vcn, NULL);
Konstantin Komarovd3624462021-08-31 16:57:40 +03001307 if (!attr) {
1308 /* Is record corrupted? */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001309 return -ENOENT;
Konstantin Komarovd3624462021-08-31 16:57:40 +03001310 }
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001311
1312 svcn = le64_to_cpu(attr->nres.svcn);
1313 evcn = le64_to_cpu(attr->nres.evcn);
1314
Konstantin Komarovd3624462021-08-31 16:57:40 +03001315 if (evcn < vcn || vcn < svcn) {
1316 /* Is record corrupted? */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001317 return -EINVAL;
Konstantin Komarovd3624462021-08-31 16:57:40 +03001318 }
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001319
1320 ro = le16_to_cpu(attr->nres.run_off);
Edward Lo6db62082022-08-06 00:47:27 +08001321
1322 if (ro > le32_to_cpu(attr->size))
1323 return -EINVAL;
1324
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001325 err = run_unpack_ex(run, ni->mi.sbi, ni->mi.rno, svcn, evcn, svcn,
1326 Add2Ptr(attr, ro), le32_to_cpu(attr->size) - ro);
1327 if (err < 0)
1328 return err;
1329 return 0;
1330}
1331
1332/*
Konstantin Komarovd3624462021-08-31 16:57:40 +03001333 * attr_load_runs_range - Load runs for given range [from to).
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001334 */
1335int attr_load_runs_range(struct ntfs_inode *ni, enum ATTR_TYPE type,
1336 const __le16 *name, u8 name_len, struct runs_tree *run,
1337 u64 from, u64 to)
1338{
1339 struct ntfs_sb_info *sbi = ni->mi.sbi;
1340 u8 cluster_bits = sbi->cluster_bits;
Colin Ian Kingdc8965a2022-04-18 17:00:00 +03001341 CLST vcn;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001342 CLST vcn_last = (to - 1) >> cluster_bits;
1343 CLST lcn, clen;
1344 int err;
1345
1346 for (vcn = from >> cluster_bits; vcn <= vcn_last; vcn += clen) {
1347 if (!run_lookup_entry(run, vcn, &lcn, &clen, NULL)) {
1348 err = attr_load_runs_vcn(ni, type, name, name_len, run,
1349 vcn);
1350 if (err)
1351 return err;
Kari Argillandere8b8e972021-08-03 14:57:09 +03001352 clen = 0; /* Next run_lookup_entry(vcn) must be success. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001353 }
1354 }
1355
1356 return 0;
1357}
1358
1359#ifdef CONFIG_NTFS3_LZX_XPRESS
1360/*
1361 * attr_wof_frame_info
1362 *
Kari Argillandere8b8e972021-08-03 14:57:09 +03001363 * Read header of Xpress/LZX file to get info about frame.
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001364 */
1365int attr_wof_frame_info(struct ntfs_inode *ni, struct ATTRIB *attr,
1366 struct runs_tree *run, u64 frame, u64 frames,
1367 u8 frame_bits, u32 *ondisk_size, u64 *vbo_data)
1368{
1369 struct ntfs_sb_info *sbi = ni->mi.sbi;
1370 u64 vbo[2], off[2], wof_size;
1371 u32 voff;
1372 u8 bytes_per_off;
1373 char *addr;
1374 struct page *page;
1375 int i, err;
1376 __le32 *off32;
1377 __le64 *off64;
1378
1379 if (ni->vfs_inode.i_size < 0x100000000ull) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001380 /* File starts with array of 32 bit offsets. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001381 bytes_per_off = sizeof(__le32);
1382 vbo[1] = frame << 2;
1383 *vbo_data = frames << 2;
1384 } else {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001385 /* File starts with array of 64 bit offsets. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001386 bytes_per_off = sizeof(__le64);
1387 vbo[1] = frame << 3;
1388 *vbo_data = frames << 3;
1389 }
1390
1391 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001392 * Read 4/8 bytes at [vbo - 4(8)] == offset where compressed frame starts.
1393 * Read 4/8 bytes at [vbo] == offset where compressed frame ends.
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001394 */
1395 if (!attr->non_res) {
1396 if (vbo[1] + bytes_per_off > le32_to_cpu(attr->res.data_size)) {
1397 ntfs_inode_err(&ni->vfs_inode, "is corrupted");
1398 return -EINVAL;
1399 }
1400 addr = resident_data(attr);
1401
1402 if (bytes_per_off == sizeof(__le32)) {
1403 off32 = Add2Ptr(addr, vbo[1]);
1404 off[0] = vbo[1] ? le32_to_cpu(off32[-1]) : 0;
1405 off[1] = le32_to_cpu(off32[0]);
1406 } else {
1407 off64 = Add2Ptr(addr, vbo[1]);
1408 off[0] = vbo[1] ? le64_to_cpu(off64[-1]) : 0;
1409 off[1] = le64_to_cpu(off64[0]);
1410 }
1411
1412 *vbo_data += off[0];
1413 *ondisk_size = off[1] - off[0];
1414 return 0;
1415 }
1416
1417 wof_size = le64_to_cpu(attr->nres.data_size);
1418 down_write(&ni->file.run_lock);
1419 page = ni->file.offs_page;
1420 if (!page) {
1421 page = alloc_page(GFP_KERNEL);
1422 if (!page) {
1423 err = -ENOMEM;
1424 goto out;
1425 }
1426 page->index = -1;
1427 ni->file.offs_page = page;
1428 }
1429 lock_page(page);
1430 addr = page_address(page);
1431
1432 if (vbo[1]) {
1433 voff = vbo[1] & (PAGE_SIZE - 1);
1434 vbo[0] = vbo[1] - bytes_per_off;
1435 i = 0;
1436 } else {
1437 voff = 0;
1438 vbo[0] = 0;
1439 off[0] = 0;
1440 i = 1;
1441 }
1442
1443 do {
1444 pgoff_t index = vbo[i] >> PAGE_SHIFT;
1445
1446 if (index != page->index) {
1447 u64 from = vbo[i] & ~(u64)(PAGE_SIZE - 1);
1448 u64 to = min(from + PAGE_SIZE, wof_size);
1449
1450 err = attr_load_runs_range(ni, ATTR_DATA, WOF_NAME,
1451 ARRAY_SIZE(WOF_NAME), run,
1452 from, to);
1453 if (err)
1454 goto out1;
1455
1456 err = ntfs_bio_pages(sbi, run, &page, 1, from,
1457 to - from, REQ_OP_READ);
1458 if (err) {
1459 page->index = -1;
1460 goto out1;
1461 }
1462 page->index = index;
1463 }
1464
1465 if (i) {
1466 if (bytes_per_off == sizeof(__le32)) {
1467 off32 = Add2Ptr(addr, voff);
1468 off[1] = le32_to_cpu(*off32);
1469 } else {
1470 off64 = Add2Ptr(addr, voff);
1471 off[1] = le64_to_cpu(*off64);
1472 }
1473 } else if (!voff) {
1474 if (bytes_per_off == sizeof(__le32)) {
1475 off32 = Add2Ptr(addr, PAGE_SIZE - sizeof(u32));
1476 off[0] = le32_to_cpu(*off32);
1477 } else {
1478 off64 = Add2Ptr(addr, PAGE_SIZE - sizeof(u64));
1479 off[0] = le64_to_cpu(*off64);
1480 }
1481 } else {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001482 /* Two values in one page. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001483 if (bytes_per_off == sizeof(__le32)) {
1484 off32 = Add2Ptr(addr, voff);
1485 off[0] = le32_to_cpu(off32[-1]);
1486 off[1] = le32_to_cpu(off32[0]);
1487 } else {
1488 off64 = Add2Ptr(addr, voff);
1489 off[0] = le64_to_cpu(off64[-1]);
1490 off[1] = le64_to_cpu(off64[0]);
1491 }
1492 break;
1493 }
1494 } while (++i < 2);
1495
1496 *vbo_data += off[0];
1497 *ondisk_size = off[1] - off[0];
1498
1499out1:
1500 unlock_page(page);
1501out:
1502 up_write(&ni->file.run_lock);
1503 return err;
1504}
1505#endif
1506
1507/*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001508 * attr_is_frame_compressed - Used to detect compressed frame.
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001509 */
1510int attr_is_frame_compressed(struct ntfs_inode *ni, struct ATTRIB *attr,
1511 CLST frame, CLST *clst_data)
1512{
1513 int err;
1514 u32 clst_frame;
1515 CLST clen, lcn, vcn, alen, slen, vcn_next;
1516 size_t idx;
1517 struct runs_tree *run;
1518
1519 *clst_data = 0;
1520
1521 if (!is_attr_compressed(attr))
1522 return 0;
1523
1524 if (!attr->non_res)
1525 return 0;
1526
1527 clst_frame = 1u << attr->nres.c_unit;
1528 vcn = frame * clst_frame;
1529 run = &ni->file.run;
1530
1531 if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx)) {
1532 err = attr_load_runs_vcn(ni, attr->type, attr_name(attr),
1533 attr->name_len, run, vcn);
1534 if (err)
1535 return err;
1536
1537 if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx))
1538 return -EINVAL;
1539 }
1540
1541 if (lcn == SPARSE_LCN) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001542 /* Sparsed frame. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001543 return 0;
1544 }
1545
1546 if (clen >= clst_frame) {
1547 /*
1548 * The frame is not compressed 'cause
Kari Argillandere8b8e972021-08-03 14:57:09 +03001549 * it does not contain any sparse clusters.
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001550 */
1551 *clst_data = clst_frame;
1552 return 0;
1553 }
1554
1555 alen = bytes_to_cluster(ni->mi.sbi, le64_to_cpu(attr->nres.alloc_size));
1556 slen = 0;
1557 *clst_data = clen;
1558
1559 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001560 * The frame is compressed if *clst_data + slen >= clst_frame.
1561 * Check next fragments.
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001562 */
1563 while ((vcn += clen) < alen) {
1564 vcn_next = vcn;
1565
1566 if (!run_get_entry(run, ++idx, &vcn, &lcn, &clen) ||
1567 vcn_next != vcn) {
1568 err = attr_load_runs_vcn(ni, attr->type,
1569 attr_name(attr),
1570 attr->name_len, run, vcn_next);
1571 if (err)
1572 return err;
1573 vcn = vcn_next;
1574
1575 if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx))
1576 return -EINVAL;
1577 }
1578
1579 if (lcn == SPARSE_LCN) {
1580 slen += clen;
1581 } else {
1582 if (slen) {
1583 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001584 * Data_clusters + sparse_clusters =
1585 * not enough for frame.
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001586 */
1587 return -EINVAL;
1588 }
1589 *clst_data += clen;
1590 }
1591
1592 if (*clst_data + slen >= clst_frame) {
1593 if (!slen) {
1594 /*
1595 * There is no sparsed clusters in this frame
Kari Argillandere8b8e972021-08-03 14:57:09 +03001596 * so it is not compressed.
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001597 */
1598 *clst_data = clst_frame;
1599 } else {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001600 /* Frame is compressed. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001601 }
1602 break;
1603 }
1604 }
1605
1606 return 0;
1607}
1608
1609/*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001610 * attr_allocate_frame - Allocate/free clusters for @frame.
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001611 *
Kari Argillandere8b8e972021-08-03 14:57:09 +03001612 * Assumed: down_write(&ni->file.run_lock);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001613 */
1614int attr_allocate_frame(struct ntfs_inode *ni, CLST frame, size_t compr_size,
1615 u64 new_valid)
1616{
1617 int err = 0;
1618 struct runs_tree *run = &ni->file.run;
1619 struct ntfs_sb_info *sbi = ni->mi.sbi;
1620 struct ATTRIB *attr = NULL, *attr_b;
1621 struct ATTR_LIST_ENTRY *le, *le_b;
1622 struct mft_inode *mi, *mi_b;
Konstantin Komarovc380b522022-10-07 14:02:36 +03001623 CLST svcn, evcn1, next_svcn, len;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001624 CLST vcn, end, clst_data;
1625 u64 total_size, valid_size, data_size;
1626
1627 le_b = NULL;
1628 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
1629 if (!attr_b)
1630 return -ENOENT;
1631
1632 if (!is_attr_ext(attr_b))
1633 return -EINVAL;
1634
1635 vcn = frame << NTFS_LZNT_CUNIT;
1636 total_size = le64_to_cpu(attr_b->nres.total_size);
1637
1638 svcn = le64_to_cpu(attr_b->nres.svcn);
1639 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
1640 data_size = le64_to_cpu(attr_b->nres.data_size);
1641
1642 if (svcn <= vcn && vcn < evcn1) {
1643 attr = attr_b;
1644 le = le_b;
1645 mi = mi_b;
1646 } else if (!le_b) {
1647 err = -EINVAL;
1648 goto out;
1649 } else {
1650 le = le_b;
1651 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
1652 &mi);
1653 if (!attr) {
1654 err = -EINVAL;
1655 goto out;
1656 }
1657 svcn = le64_to_cpu(attr->nres.svcn);
1658 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
1659 }
1660
1661 err = attr_load_runs(attr, ni, run, NULL);
1662 if (err)
1663 goto out;
1664
1665 err = attr_is_frame_compressed(ni, attr_b, frame, &clst_data);
1666 if (err)
1667 goto out;
1668
1669 total_size -= (u64)clst_data << sbi->cluster_bits;
1670
1671 len = bytes_to_cluster(sbi, compr_size);
1672
1673 if (len == clst_data)
1674 goto out;
1675
1676 if (len < clst_data) {
1677 err = run_deallocate_ex(sbi, run, vcn + len, clst_data - len,
1678 NULL, true);
1679 if (err)
1680 goto out;
1681
1682 if (!run_add_entry(run, vcn + len, SPARSE_LCN, clst_data - len,
1683 false)) {
1684 err = -ENOMEM;
1685 goto out;
1686 }
1687 end = vcn + clst_data;
Kari Argillandere8b8e972021-08-03 14:57:09 +03001688 /* Run contains updated range [vcn + len : end). */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001689 } else {
1690 CLST alen, hint = 0;
Kari Argillandere8b8e972021-08-03 14:57:09 +03001691 /* Get the last LCN to allocate from. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001692 if (vcn + clst_data &&
1693 !run_lookup_entry(run, vcn + clst_data - 1, &hint, NULL,
1694 NULL)) {
1695 hint = -1;
1696 }
1697
1698 err = attr_allocate_clusters(sbi, run, vcn + clst_data,
Konstantin Komarovc380b522022-10-07 14:02:36 +03001699 hint + 1, len - clst_data, NULL,
1700 ALLOCATE_DEF, &alen, 0, NULL,
1701 NULL);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001702 if (err)
1703 goto out;
1704
1705 end = vcn + len;
Kari Argillandere8b8e972021-08-03 14:57:09 +03001706 /* Run contains updated range [vcn + clst_data : end). */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001707 }
1708
1709 total_size += (u64)len << sbi->cluster_bits;
1710
1711repack:
1712 err = mi_pack_runs(mi, attr, run, max(end, evcn1) - svcn);
1713 if (err)
1714 goto out;
1715
1716 attr_b->nres.total_size = cpu_to_le64(total_size);
1717 inode_set_bytes(&ni->vfs_inode, total_size);
1718
1719 mi_b->dirty = true;
1720 mark_inode_dirty(&ni->vfs_inode);
1721
Kari Argillandere8b8e972021-08-03 14:57:09 +03001722 /* Stored [vcn : next_svcn) from [vcn : end). */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001723 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1724
1725 if (end <= evcn1) {
1726 if (next_svcn == evcn1) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001727 /* Normal way. Update attribute and exit. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001728 goto ok;
1729 }
Kari Argillandere8b8e972021-08-03 14:57:09 +03001730 /* Add new segment [next_svcn : evcn1 - next_svcn). */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001731 if (!ni->attr_list.size) {
1732 err = ni_create_attr_list(ni);
1733 if (err)
1734 goto out;
Kari Argillandere8b8e972021-08-03 14:57:09 +03001735 /* Layout of records is changed. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001736 le_b = NULL;
1737 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL,
1738 0, NULL, &mi_b);
1739 if (!attr_b) {
1740 err = -ENOENT;
1741 goto out;
1742 }
1743
1744 attr = attr_b;
1745 le = le_b;
1746 mi = mi_b;
1747 goto repack;
1748 }
1749 }
1750
1751 svcn = evcn1;
1752
Kari Argillandere8b8e972021-08-03 14:57:09 +03001753 /* Estimate next attribute. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001754 attr = ni_find_attr(ni, attr, &le, ATTR_DATA, NULL, 0, &svcn, &mi);
1755
1756 if (attr) {
1757 CLST alloc = bytes_to_cluster(
1758 sbi, le64_to_cpu(attr_b->nres.alloc_size));
1759 CLST evcn = le64_to_cpu(attr->nres.evcn);
1760
1761 if (end < next_svcn)
1762 end = next_svcn;
1763 while (end > evcn) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001764 /* Remove segment [svcn : evcn). */
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001765 mi_remove_attr(NULL, mi, attr);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001766
1767 if (!al_remove_le(ni, le)) {
1768 err = -EINVAL;
1769 goto out;
1770 }
1771
1772 if (evcn + 1 >= alloc) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001773 /* Last attribute segment. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001774 evcn1 = evcn + 1;
1775 goto ins_ext;
1776 }
1777
1778 if (ni_load_mi(ni, le, &mi)) {
1779 attr = NULL;
1780 goto out;
1781 }
1782
1783 attr = mi_find_attr(mi, NULL, ATTR_DATA, NULL, 0,
1784 &le->id);
1785 if (!attr) {
1786 err = -EINVAL;
1787 goto out;
1788 }
1789 svcn = le64_to_cpu(attr->nres.svcn);
1790 evcn = le64_to_cpu(attr->nres.evcn);
1791 }
1792
1793 if (end < svcn)
1794 end = svcn;
1795
1796 err = attr_load_runs(attr, ni, run, &end);
1797 if (err)
1798 goto out;
1799
1800 evcn1 = evcn + 1;
1801 attr->nres.svcn = cpu_to_le64(next_svcn);
1802 err = mi_pack_runs(mi, attr, run, evcn1 - next_svcn);
1803 if (err)
1804 goto out;
1805
1806 le->vcn = cpu_to_le64(next_svcn);
1807 ni->attr_list.dirty = true;
1808 mi->dirty = true;
1809
1810 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1811 }
1812ins_ext:
1813 if (evcn1 > next_svcn) {
1814 err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
1815 next_svcn, evcn1 - next_svcn,
Konstantin Komarovc1e0ab372022-05-13 18:25:04 +03001816 attr_b->flags, &attr, &mi, NULL);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001817 if (err)
1818 goto out;
1819 }
1820ok:
1821 run_truncate_around(run, vcn);
1822out:
1823 if (new_valid > data_size)
1824 new_valid = data_size;
1825
1826 valid_size = le64_to_cpu(attr_b->nres.valid_size);
1827 if (new_valid != valid_size) {
1828 attr_b->nres.valid_size = cpu_to_le64(valid_size);
1829 mi_b->dirty = true;
1830 }
1831
1832 return err;
1833}
1834
Kari Argillandere8b8e972021-08-03 14:57:09 +03001835/*
1836 * attr_collapse_range - Collapse range in file.
1837 */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001838int attr_collapse_range(struct ntfs_inode *ni, u64 vbo, u64 bytes)
1839{
1840 int err = 0;
1841 struct runs_tree *run = &ni->file.run;
1842 struct ntfs_sb_info *sbi = ni->mi.sbi;
1843 struct ATTRIB *attr = NULL, *attr_b;
1844 struct ATTR_LIST_ENTRY *le, *le_b;
1845 struct mft_inode *mi, *mi_b;
1846 CLST svcn, evcn1, len, dealloc, alen;
1847 CLST vcn, end;
1848 u64 valid_size, data_size, alloc_size, total_size;
1849 u32 mask;
1850 __le16 a_flags;
1851
1852 if (!bytes)
1853 return 0;
1854
1855 le_b = NULL;
1856 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
1857 if (!attr_b)
1858 return -ENOENT;
1859
1860 if (!attr_b->non_res) {
1861 /* Attribute is resident. Nothing to do? */
1862 return 0;
1863 }
1864
1865 data_size = le64_to_cpu(attr_b->nres.data_size);
1866 alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
1867 a_flags = attr_b->flags;
1868
1869 if (is_attr_ext(attr_b)) {
1870 total_size = le64_to_cpu(attr_b->nres.total_size);
1871 mask = (sbi->cluster_size << attr_b->nres.c_unit) - 1;
1872 } else {
1873 total_size = alloc_size;
1874 mask = sbi->cluster_mask;
1875 }
1876
1877 if ((vbo & mask) || (bytes & mask)) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001878 /* Allow to collapse only cluster aligned ranges. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001879 return -EINVAL;
1880 }
1881
1882 if (vbo > data_size)
1883 return -EINVAL;
1884
1885 down_write(&ni->file.run_lock);
1886
1887 if (vbo + bytes >= data_size) {
1888 u64 new_valid = min(ni->i_valid, vbo);
1889
Kari Argillandere8b8e972021-08-03 14:57:09 +03001890 /* Simple truncate file at 'vbo'. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001891 truncate_setsize(&ni->vfs_inode, vbo);
1892 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, vbo,
1893 &new_valid, true, NULL);
1894
1895 if (!err && new_valid < ni->i_valid)
1896 ni->i_valid = new_valid;
1897
1898 goto out;
1899 }
1900
1901 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001902 * Enumerate all attribute segments and collapse.
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001903 */
1904 alen = alloc_size >> sbi->cluster_bits;
1905 vcn = vbo >> sbi->cluster_bits;
1906 len = bytes >> sbi->cluster_bits;
1907 end = vcn + len;
1908 dealloc = 0;
1909
1910 svcn = le64_to_cpu(attr_b->nres.svcn);
1911 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
1912
1913 if (svcn <= vcn && vcn < evcn1) {
1914 attr = attr_b;
1915 le = le_b;
1916 mi = mi_b;
1917 } else if (!le_b) {
1918 err = -EINVAL;
1919 goto out;
1920 } else {
1921 le = le_b;
1922 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
1923 &mi);
1924 if (!attr) {
1925 err = -EINVAL;
1926 goto out;
1927 }
1928
1929 svcn = le64_to_cpu(attr->nres.svcn);
1930 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
1931 }
1932
1933 for (;;) {
1934 if (svcn >= end) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001935 /* Shift VCN- */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001936 attr->nres.svcn = cpu_to_le64(svcn - len);
1937 attr->nres.evcn = cpu_to_le64(evcn1 - 1 - len);
1938 if (le) {
1939 le->vcn = attr->nres.svcn;
1940 ni->attr_list.dirty = true;
1941 }
1942 mi->dirty = true;
1943 } else if (svcn < vcn || end < evcn1) {
1944 CLST vcn1, eat, next_svcn;
1945
Kari Argillandere8b8e972021-08-03 14:57:09 +03001946 /* Collapse a part of this attribute segment. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001947 err = attr_load_runs(attr, ni, run, &svcn);
1948 if (err)
1949 goto out;
1950 vcn1 = max(vcn, svcn);
1951 eat = min(end, evcn1) - vcn1;
1952
1953 err = run_deallocate_ex(sbi, run, vcn1, eat, &dealloc,
1954 true);
1955 if (err)
1956 goto out;
1957
1958 if (!run_collapse_range(run, vcn1, eat)) {
1959 err = -ENOMEM;
1960 goto out;
1961 }
1962
1963 if (svcn >= vcn) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001964 /* Shift VCN */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001965 attr->nres.svcn = cpu_to_le64(vcn);
1966 if (le) {
1967 le->vcn = attr->nres.svcn;
1968 ni->attr_list.dirty = true;
1969 }
1970 }
1971
1972 err = mi_pack_runs(mi, attr, run, evcn1 - svcn - eat);
1973 if (err)
1974 goto out;
1975
1976 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
1977 if (next_svcn + eat < evcn1) {
1978 err = ni_insert_nonresident(
1979 ni, ATTR_DATA, NULL, 0, run, next_svcn,
1980 evcn1 - eat - next_svcn, a_flags, &attr,
Konstantin Komarovc1e0ab372022-05-13 18:25:04 +03001981 &mi, &le);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001982 if (err)
1983 goto out;
1984
Kari Argillandere8b8e972021-08-03 14:57:09 +03001985 /* Layout of records maybe changed. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001986 attr_b = NULL;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001987 }
1988
Kari Argillandere8b8e972021-08-03 14:57:09 +03001989 /* Free all allocated memory. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03001990 run_truncate(run, 0);
1991 } else {
1992 u16 le_sz;
1993 u16 roff = le16_to_cpu(attr->nres.run_off);
1994
Edward Lo6db62082022-08-06 00:47:27 +08001995 if (roff > le32_to_cpu(attr->size)) {
1996 err = -EINVAL;
1997 goto out;
1998 }
1999
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002000 run_unpack_ex(RUN_DEALLOCATE, sbi, ni->mi.rno, svcn,
2001 evcn1 - 1, svcn, Add2Ptr(attr, roff),
2002 le32_to_cpu(attr->size) - roff);
2003
Kari Argillandere8b8e972021-08-03 14:57:09 +03002004 /* Delete this attribute segment. */
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03002005 mi_remove_attr(NULL, mi, attr);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002006 if (!le)
2007 break;
2008
2009 le_sz = le16_to_cpu(le->size);
2010 if (!al_remove_le(ni, le)) {
2011 err = -EINVAL;
2012 goto out;
2013 }
2014
2015 if (evcn1 >= alen)
2016 break;
2017
2018 if (!svcn) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03002019 /* Load next record that contains this attribute. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002020 if (ni_load_mi(ni, le, &mi)) {
2021 err = -EINVAL;
2022 goto out;
2023 }
2024
Kari Argillandere8b8e972021-08-03 14:57:09 +03002025 /* Look for required attribute. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002026 attr = mi_find_attr(mi, NULL, ATTR_DATA, NULL,
2027 0, &le->id);
2028 if (!attr) {
2029 err = -EINVAL;
2030 goto out;
2031 }
2032 goto next_attr;
2033 }
2034 le = (struct ATTR_LIST_ENTRY *)((u8 *)le - le_sz);
2035 }
2036
2037 if (evcn1 >= alen)
2038 break;
2039
2040 attr = ni_enum_attr_ex(ni, attr, &le, &mi);
2041 if (!attr) {
2042 err = -EINVAL;
2043 goto out;
2044 }
2045
2046next_attr:
2047 svcn = le64_to_cpu(attr->nres.svcn);
2048 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2049 }
2050
2051 if (!attr_b) {
2052 le_b = NULL;
2053 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL,
2054 &mi_b);
2055 if (!attr_b) {
2056 err = -ENOENT;
2057 goto out;
2058 }
2059 }
2060
2061 data_size -= bytes;
2062 valid_size = ni->i_valid;
2063 if (vbo + bytes <= valid_size)
2064 valid_size -= bytes;
2065 else if (vbo < valid_size)
2066 valid_size = vbo;
2067
2068 attr_b->nres.alloc_size = cpu_to_le64(alloc_size - bytes);
2069 attr_b->nres.data_size = cpu_to_le64(data_size);
2070 attr_b->nres.valid_size = cpu_to_le64(min(valid_size, data_size));
2071 total_size -= (u64)dealloc << sbi->cluster_bits;
2072 if (is_attr_ext(attr_b))
2073 attr_b->nres.total_size = cpu_to_le64(total_size);
2074 mi_b->dirty = true;
2075
Kari Argillandere8b8e972021-08-03 14:57:09 +03002076 /* Update inode size. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002077 ni->i_valid = valid_size;
2078 ni->vfs_inode.i_size = data_size;
2079 inode_set_bytes(&ni->vfs_inode, total_size);
2080 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
2081 mark_inode_dirty(&ni->vfs_inode);
2082
2083out:
2084 up_write(&ni->file.run_lock);
2085 if (err)
Konstantin Komarovc12df452022-07-13 17:55:27 +03002086 _ntfs_bad_inode(&ni->vfs_inode);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002087
2088 return err;
2089}
2090
Kari Argillandere8b8e972021-08-03 14:57:09 +03002091/*
2092 * attr_punch_hole
2093 *
2094 * Not for normal files.
2095 */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002096int attr_punch_hole(struct ntfs_inode *ni, u64 vbo, u64 bytes, u32 *frame_size)
2097{
2098 int err = 0;
2099 struct runs_tree *run = &ni->file.run;
2100 struct ntfs_sb_info *sbi = ni->mi.sbi;
2101 struct ATTRIB *attr = NULL, *attr_b;
2102 struct ATTR_LIST_ENTRY *le, *le_b;
2103 struct mft_inode *mi, *mi_b;
Konstantin Komarov20abc642022-07-13 18:18:48 +03002104 CLST svcn, evcn1, vcn, len, end, alen, hole, next_svcn;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002105 u64 total_size, alloc_size;
2106 u32 mask;
Konstantin Komarovc1e0ab372022-05-13 18:25:04 +03002107 __le16 a_flags;
Konstantin Komarov20abc642022-07-13 18:18:48 +03002108 struct runs_tree run2;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002109
2110 if (!bytes)
2111 return 0;
2112
2113 le_b = NULL;
2114 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
2115 if (!attr_b)
2116 return -ENOENT;
2117
2118 if (!attr_b->non_res) {
Alon Zahavi6d5c9e72022-08-15 14:07:12 +03002119 u32 data_size = le32_to_cpu(attr_b->res.data_size);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002120 u32 from, to;
2121
2122 if (vbo > data_size)
2123 return 0;
2124
2125 from = vbo;
Kari Argillander6e3331e2021-09-07 17:28:42 +03002126 to = min_t(u64, vbo + bytes, data_size);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002127 memset(Add2Ptr(resident_data(attr_b), from), 0, to - from);
2128 return 0;
2129 }
2130
2131 if (!is_attr_ext(attr_b))
2132 return -EOPNOTSUPP;
2133
2134 alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
2135 total_size = le64_to_cpu(attr_b->nres.total_size);
2136
2137 if (vbo >= alloc_size) {
Konstantin Komarovd3624462021-08-31 16:57:40 +03002138 /* NOTE: It is allowed. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002139 return 0;
2140 }
2141
2142 mask = (sbi->cluster_size << attr_b->nres.c_unit) - 1;
2143
2144 bytes += vbo;
2145 if (bytes > alloc_size)
2146 bytes = alloc_size;
2147 bytes -= vbo;
2148
2149 if ((vbo & mask) || (bytes & mask)) {
Konstantin Komarovd3624462021-08-31 16:57:40 +03002150 /* We have to zero a range(s). */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002151 if (frame_size == NULL) {
Konstantin Komarovd3624462021-08-31 16:57:40 +03002152 /* Caller insists range is aligned. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002153 return -EINVAL;
2154 }
2155 *frame_size = mask + 1;
2156 return E_NTFS_NOTALIGNED;
2157 }
2158
2159 down_write(&ni->file.run_lock);
Konstantin Komarov20abc642022-07-13 18:18:48 +03002160 run_init(&run2);
2161 run_truncate(run, 0);
2162
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002163 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +03002164 * Enumerate all attribute segments and punch hole where necessary.
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002165 */
2166 alen = alloc_size >> sbi->cluster_bits;
2167 vcn = vbo >> sbi->cluster_bits;
2168 len = bytes >> sbi->cluster_bits;
2169 end = vcn + len;
Konstantin Komarov20abc642022-07-13 18:18:48 +03002170 hole = 0;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002171
2172 svcn = le64_to_cpu(attr_b->nres.svcn);
2173 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
Konstantin Komarovc1e0ab372022-05-13 18:25:04 +03002174 a_flags = attr_b->flags;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002175
2176 if (svcn <= vcn && vcn < evcn1) {
2177 attr = attr_b;
2178 le = le_b;
2179 mi = mi_b;
2180 } else if (!le_b) {
2181 err = -EINVAL;
Konstantin Komarov20abc642022-07-13 18:18:48 +03002182 goto bad_inode;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002183 } else {
2184 le = le_b;
2185 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
2186 &mi);
2187 if (!attr) {
2188 err = -EINVAL;
Konstantin Komarov20abc642022-07-13 18:18:48 +03002189 goto bad_inode;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002190 }
2191
2192 svcn = le64_to_cpu(attr->nres.svcn);
2193 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2194 }
2195
2196 while (svcn < end) {
Konstantin Komarov20abc642022-07-13 18:18:48 +03002197 CLST vcn1, zero, hole2 = hole;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002198
2199 err = attr_load_runs(attr, ni, run, &svcn);
2200 if (err)
Konstantin Komarov20abc642022-07-13 18:18:48 +03002201 goto done;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002202 vcn1 = max(vcn, svcn);
2203 zero = min(end, evcn1) - vcn1;
2204
Konstantin Komarov20abc642022-07-13 18:18:48 +03002205 /*
2206 * Check range [vcn1 + zero).
2207 * Calculate how many clusters there are.
2208 * Don't do any destructive actions.
2209 */
2210 err = run_deallocate_ex(NULL, run, vcn1, zero, &hole2, false);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002211 if (err)
Konstantin Komarov20abc642022-07-13 18:18:48 +03002212 goto done;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002213
Konstantin Komarov20abc642022-07-13 18:18:48 +03002214 /* Check if required range is already hole. */
2215 if (hole2 == hole)
2216 goto next_attr;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002217
Konstantin Komarov20abc642022-07-13 18:18:48 +03002218 /* Make a clone of run to undo. */
2219 err = run_clone(run, &run2);
2220 if (err)
2221 goto done;
2222
2223 /* Make a hole range (sparse) [vcn1 + zero). */
2224 if (!run_add_entry(run, vcn1, SPARSE_LCN, zero, false)) {
2225 err = -ENOMEM;
2226 goto done;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002227 }
Konstantin Komarov20abc642022-07-13 18:18:48 +03002228
2229 /* Update run in attribute segment. */
2230 err = mi_pack_runs(mi, attr, run, evcn1 - svcn);
2231 if (err)
2232 goto done;
2233 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
2234 if (next_svcn < evcn1) {
2235 /* Insert new attribute segment. */
2236 err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
2237 next_svcn,
2238 evcn1 - next_svcn, a_flags,
2239 &attr, &mi, &le);
2240 if (err)
2241 goto undo_punch;
2242
2243 /* Layout of records maybe changed. */
2244 attr_b = NULL;
2245 }
2246
2247 /* Real deallocate. Should not fail. */
2248 run_deallocate_ex(sbi, &run2, vcn1, zero, &hole, true);
2249
2250next_attr:
Kari Argillandere8b8e972021-08-03 14:57:09 +03002251 /* Free all allocated memory. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002252 run_truncate(run, 0);
2253
2254 if (evcn1 >= alen)
2255 break;
2256
Konstantin Komarov20abc642022-07-13 18:18:48 +03002257 /* Get next attribute segment. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002258 attr = ni_enum_attr_ex(ni, attr, &le, &mi);
2259 if (!attr) {
2260 err = -EINVAL;
Konstantin Komarov20abc642022-07-13 18:18:48 +03002261 goto bad_inode;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002262 }
2263
2264 svcn = le64_to_cpu(attr->nres.svcn);
2265 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2266 }
2267
Konstantin Komarov20abc642022-07-13 18:18:48 +03002268done:
2269 if (!hole)
2270 goto out;
2271
Konstantin Komarov560f7732022-06-30 18:31:26 +03002272 if (!attr_b) {
2273 attr_b = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL,
2274 &mi_b);
2275 if (!attr_b) {
2276 err = -EINVAL;
Konstantin Komarov20abc642022-07-13 18:18:48 +03002277 goto bad_inode;
Konstantin Komarov560f7732022-06-30 18:31:26 +03002278 }
2279 }
Konstantin Komarov20abc642022-07-13 18:18:48 +03002280
2281 total_size -= (u64)hole << sbi->cluster_bits;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002282 attr_b->nres.total_size = cpu_to_le64(total_size);
2283 mi_b->dirty = true;
2284
Kari Argillandere8b8e972021-08-03 14:57:09 +03002285 /* Update inode size. */
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002286 inode_set_bytes(&ni->vfs_inode, total_size);
2287 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
2288 mark_inode_dirty(&ni->vfs_inode);
2289
2290out:
Konstantin Komarov20abc642022-07-13 18:18:48 +03002291 run_close(&run2);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002292 up_write(&ni->file.run_lock);
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002293 return err;
Konstantin Komarov20abc642022-07-13 18:18:48 +03002294
2295bad_inode:
2296 _ntfs_bad_inode(&ni->vfs_inode);
2297 goto out;
2298
2299undo_punch:
2300 /*
2301 * Restore packed runs.
2302 * 'mi_pack_runs' should not fail, cause we restore original.
2303 */
2304 if (mi_pack_runs(mi, attr, &run2, evcn1 - svcn))
2305 goto bad_inode;
2306
2307 goto done;
Konstantin Komarovbe71b5c2021-08-13 17:21:30 +03002308}
Konstantin Komarovaa30ecc2022-06-21 12:46:25 +03002309
2310/*
2311 * attr_insert_range - Insert range (hole) in file.
2312 * Not for normal files.
2313 */
2314int attr_insert_range(struct ntfs_inode *ni, u64 vbo, u64 bytes)
2315{
2316 int err = 0;
2317 struct runs_tree *run = &ni->file.run;
2318 struct ntfs_sb_info *sbi = ni->mi.sbi;
2319 struct ATTRIB *attr = NULL, *attr_b;
2320 struct ATTR_LIST_ENTRY *le, *le_b;
2321 struct mft_inode *mi, *mi_b;
2322 CLST vcn, svcn, evcn1, len, next_svcn;
2323 u64 data_size, alloc_size;
2324 u32 mask;
2325 __le16 a_flags;
2326
2327 if (!bytes)
2328 return 0;
2329
2330 le_b = NULL;
2331 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
2332 if (!attr_b)
2333 return -ENOENT;
2334
2335 if (!is_attr_ext(attr_b)) {
2336 /* It was checked above. See fallocate. */
2337 return -EOPNOTSUPP;
2338 }
2339
2340 if (!attr_b->non_res) {
2341 data_size = le32_to_cpu(attr_b->res.data_size);
Konstantin Komarov13747aa2022-05-12 19:18:11 +03002342 alloc_size = data_size;
Konstantin Komarovaa30ecc2022-06-21 12:46:25 +03002343 mask = sbi->cluster_mask; /* cluster_size - 1 */
2344 } else {
2345 data_size = le64_to_cpu(attr_b->nres.data_size);
Konstantin Komarov13747aa2022-05-12 19:18:11 +03002346 alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
Konstantin Komarovaa30ecc2022-06-21 12:46:25 +03002347 mask = (sbi->cluster_size << attr_b->nres.c_unit) - 1;
2348 }
2349
2350 if (vbo > data_size) {
2351 /* Insert range after the file size is not allowed. */
2352 return -EINVAL;
2353 }
2354
2355 if ((vbo & mask) || (bytes & mask)) {
2356 /* Allow to insert only frame aligned ranges. */
2357 return -EINVAL;
2358 }
2359
Konstantin Komarov13747aa2022-05-12 19:18:11 +03002360 /*
2361 * valid_size <= data_size <= alloc_size
2362 * Check alloc_size for maximum possible.
2363 */
2364 if (bytes > sbi->maxbytes_sparse - alloc_size)
2365 return -EFBIG;
2366
Konstantin Komarovaa30ecc2022-06-21 12:46:25 +03002367 vcn = vbo >> sbi->cluster_bits;
2368 len = bytes >> sbi->cluster_bits;
2369
2370 down_write(&ni->file.run_lock);
2371
2372 if (!attr_b->non_res) {
2373 err = attr_set_size(ni, ATTR_DATA, NULL, 0, run,
Konstantin Komarov9256ec32022-07-13 18:44:24 +03002374 data_size + bytes, NULL, false, NULL);
Konstantin Komarovaa30ecc2022-06-21 12:46:25 +03002375
Konstantin Komarovaa30ecc2022-06-21 12:46:25 +03002376 le_b = NULL;
2377 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL,
2378 &mi_b);
Dan Carpenter4838ec02022-06-30 16:10:56 +03002379 if (!attr_b) {
Konstantin Komarovaa30ecc2022-06-21 12:46:25 +03002380 err = -EINVAL;
Konstantin Komarov9256ec32022-07-13 18:44:24 +03002381 goto bad_inode;
Konstantin Komarovaa30ecc2022-06-21 12:46:25 +03002382 }
Konstantin Komarov9256ec32022-07-13 18:44:24 +03002383
2384 if (err)
2385 goto out;
2386
2387 if (!attr_b->non_res) {
2388 /* Still resident. */
Konstantin Komarov9144b432022-10-06 19:58:11 +03002389 char *data = Add2Ptr(attr_b,
2390 le16_to_cpu(attr_b->res.data_off));
Konstantin Komarov9256ec32022-07-13 18:44:24 +03002391
2392 memmove(data + bytes, data, bytes);
2393 memset(data, 0, bytes);
2394 goto done;
2395 }
2396
2397 /* Resident files becomes nonresident. */
Konstantin Komarovaa30ecc2022-06-21 12:46:25 +03002398 data_size = le64_to_cpu(attr_b->nres.data_size);
2399 alloc_size = le64_to_cpu(attr_b->nres.alloc_size);
2400 }
2401
2402 /*
2403 * Enumerate all attribute segments and shift start vcn.
2404 */
2405 a_flags = attr_b->flags;
2406 svcn = le64_to_cpu(attr_b->nres.svcn);
2407 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
2408
2409 if (svcn <= vcn && vcn < evcn1) {
2410 attr = attr_b;
2411 le = le_b;
2412 mi = mi_b;
2413 } else if (!le_b) {
2414 err = -EINVAL;
Konstantin Komarov9256ec32022-07-13 18:44:24 +03002415 goto bad_inode;
Konstantin Komarovaa30ecc2022-06-21 12:46:25 +03002416 } else {
2417 le = le_b;
2418 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
2419 &mi);
2420 if (!attr) {
2421 err = -EINVAL;
Konstantin Komarov9256ec32022-07-13 18:44:24 +03002422 goto bad_inode;
Konstantin Komarovaa30ecc2022-06-21 12:46:25 +03002423 }
2424
2425 svcn = le64_to_cpu(attr->nres.svcn);
2426 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2427 }
2428
2429 run_truncate(run, 0); /* clear cached values. */
2430 err = attr_load_runs(attr, ni, run, NULL);
2431 if (err)
2432 goto out;
2433
2434 if (!run_insert_range(run, vcn, len)) {
2435 err = -ENOMEM;
2436 goto out;
2437 }
2438
2439 /* Try to pack in current record as much as possible. */
2440 err = mi_pack_runs(mi, attr, run, evcn1 + len - svcn);
2441 if (err)
2442 goto out;
2443
2444 next_svcn = le64_to_cpu(attr->nres.evcn) + 1;
Konstantin Komarovaa30ecc2022-06-21 12:46:25 +03002445
2446 while ((attr = ni_enum_attr_ex(ni, attr, &le, &mi)) &&
2447 attr->type == ATTR_DATA && !attr->name_len) {
2448 le64_add_cpu(&attr->nres.svcn, len);
2449 le64_add_cpu(&attr->nres.evcn, len);
2450 if (le) {
2451 le->vcn = attr->nres.svcn;
2452 ni->attr_list.dirty = true;
2453 }
2454 mi->dirty = true;
2455 }
2456
Konstantin Komarov9256ec32022-07-13 18:44:24 +03002457 if (next_svcn < evcn1 + len) {
2458 err = ni_insert_nonresident(ni, ATTR_DATA, NULL, 0, run,
2459 next_svcn, evcn1 + len - next_svcn,
2460 a_flags, NULL, NULL, NULL);
2461
2462 le_b = NULL;
2463 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL,
2464 &mi_b);
2465 if (!attr_b) {
2466 err = -EINVAL;
2467 goto bad_inode;
2468 }
2469
2470 if (err) {
2471 /* ni_insert_nonresident failed. Try to undo. */
2472 goto undo_insert_range;
2473 }
2474 }
2475
Konstantin Komarovaa30ecc2022-06-21 12:46:25 +03002476 /*
Konstantin Komarov9256ec32022-07-13 18:44:24 +03002477 * Update primary attribute segment.
Konstantin Komarovaa30ecc2022-06-21 12:46:25 +03002478 */
2479 if (vbo <= ni->i_valid)
2480 ni->i_valid += bytes;
2481
Konstantin Komarov9144b432022-10-06 19:58:11 +03002482 attr_b->nres.data_size = cpu_to_le64(data_size + bytes);
2483 attr_b->nres.alloc_size = cpu_to_le64(alloc_size + bytes);
Konstantin Komarovaa30ecc2022-06-21 12:46:25 +03002484
2485 /* ni->valid may be not equal valid_size (temporary). */
2486 if (ni->i_valid > data_size + bytes)
2487 attr_b->nres.valid_size = attr_b->nres.data_size;
2488 else
2489 attr_b->nres.valid_size = cpu_to_le64(ni->i_valid);
2490 mi_b->dirty = true;
2491
Konstantin Komarov9256ec32022-07-13 18:44:24 +03002492done:
Konstantin Komarovaa30ecc2022-06-21 12:46:25 +03002493 ni->vfs_inode.i_size += bytes;
2494 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
2495 mark_inode_dirty(&ni->vfs_inode);
2496
2497out:
2498 run_truncate(run, 0); /* clear cached values. */
2499
2500 up_write(&ni->file.run_lock);
Konstantin Komarovaa30ecc2022-06-21 12:46:25 +03002501
2502 return err;
Konstantin Komarov9256ec32022-07-13 18:44:24 +03002503
2504bad_inode:
2505 _ntfs_bad_inode(&ni->vfs_inode);
2506 goto out;
2507
2508undo_insert_range:
2509 svcn = le64_to_cpu(attr_b->nres.svcn);
2510 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1;
2511
2512 if (svcn <= vcn && vcn < evcn1) {
2513 attr = attr_b;
2514 le = le_b;
2515 mi = mi_b;
2516 } else if (!le_b) {
2517 goto bad_inode;
2518 } else {
2519 le = le_b;
2520 attr = ni_find_attr(ni, attr_b, &le, ATTR_DATA, NULL, 0, &vcn,
2521 &mi);
2522 if (!attr) {
2523 goto bad_inode;
2524 }
2525
2526 svcn = le64_to_cpu(attr->nres.svcn);
2527 evcn1 = le64_to_cpu(attr->nres.evcn) + 1;
2528 }
2529
2530 if (attr_load_runs(attr, ni, run, NULL))
2531 goto bad_inode;
2532
2533 if (!run_collapse_range(run, vcn, len))
2534 goto bad_inode;
2535
2536 if (mi_pack_runs(mi, attr, run, evcn1 + len - svcn))
2537 goto bad_inode;
2538
2539 while ((attr = ni_enum_attr_ex(ni, attr, &le, &mi)) &&
2540 attr->type == ATTR_DATA && !attr->name_len) {
2541 le64_sub_cpu(&attr->nres.svcn, len);
2542 le64_sub_cpu(&attr->nres.evcn, len);
2543 if (le) {
2544 le->vcn = attr->nres.svcn;
2545 ni->attr_list.dirty = true;
2546 }
2547 mi->dirty = true;
2548 }
2549
2550 goto out;
Konstantin Komarovaa30ecc2022-06-21 12:46:25 +03002551}