fs/ntfs3: Make static function attr_load_runs
[linux-block.git] / fs / ntfs3 / frecord.c
CommitLineData
4342306f
KK
1// SPDX-License-Identifier: GPL-2.0
2/*
3 *
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
5 *
6 */
7
4342306f
KK
8#include <linux/fiemap.h>
9#include <linux/fs.h>
19d1b787 10#include <linux/minmax.h>
4342306f
KK
11#include <linux/vmalloc.h>
12
13#include "debug.h"
14#include "ntfs.h"
15#include "ntfs_fs.h"
16#ifdef CONFIG_NTFS3_LZX_XPRESS
17#include "lib/lib.h"
18#endif
19
20static struct mft_inode *ni_ins_mi(struct ntfs_inode *ni, struct rb_root *tree,
21 CLST ino, struct rb_node *ins)
22{
23 struct rb_node **p = &tree->rb_node;
24 struct rb_node *pr = NULL;
25
26 while (*p) {
27 struct mft_inode *mi;
28
29 pr = *p;
30 mi = rb_entry(pr, struct mft_inode, node);
31 if (mi->rno > ino)
32 p = &pr->rb_left;
33 else if (mi->rno < ino)
34 p = &pr->rb_right;
35 else
36 return mi;
37 }
38
39 if (!ins)
40 return NULL;
41
42 rb_link_node(ins, pr, p);
43 rb_insert_color(ins, tree);
44 return rb_entry(ins, struct mft_inode, node);
45}
46
47/*
e8b8e97f 48 * ni_find_mi - Find mft_inode by record number.
4342306f
KK
49 */
50static struct mft_inode *ni_find_mi(struct ntfs_inode *ni, CLST rno)
51{
52 return ni_ins_mi(ni, &ni->mi_tree, rno, NULL);
53}
54
55/*
e8b8e97f 56 * ni_add_mi - Add new mft_inode into ntfs_inode.
d3624466 57 */
4342306f
KK
58static void ni_add_mi(struct ntfs_inode *ni, struct mft_inode *mi)
59{
60 ni_ins_mi(ni, &ni->mi_tree, mi->rno, &mi->node);
61}
62
63/*
e8b8e97f 64 * ni_remove_mi - Remove mft_inode from ntfs_inode.
4342306f
KK
65 */
66void ni_remove_mi(struct ntfs_inode *ni, struct mft_inode *mi)
67{
68 rb_erase(&mi->node, &ni->mi_tree);
69}
70
d3624466
KK
71/*
72 * ni_std - Return: Pointer into std_info from primary record.
4342306f
KK
73 */
74struct ATTR_STD_INFO *ni_std(struct ntfs_inode *ni)
75{
76 const struct ATTRIB *attr;
77
78 attr = mi_find_attr(&ni->mi, NULL, ATTR_STD, NULL, 0, NULL);
79 return attr ? resident_data_ex(attr, sizeof(struct ATTR_STD_INFO))
80 : NULL;
81}
82
83/*
84 * ni_std5
85 *
e8b8e97f 86 * Return: Pointer into std_info from primary record.
4342306f
KK
87 */
88struct ATTR_STD_INFO5 *ni_std5(struct ntfs_inode *ni)
89{
90 const struct ATTRIB *attr;
91
92 attr = mi_find_attr(&ni->mi, NULL, ATTR_STD, NULL, 0, NULL);
93
94 return attr ? resident_data_ex(attr, sizeof(struct ATTR_STD_INFO5))
95 : NULL;
96}
97
98/*
e8b8e97f 99 * ni_clear - Clear resources allocated by ntfs_inode.
4342306f
KK
100 */
101void ni_clear(struct ntfs_inode *ni)
102{
103 struct rb_node *node;
104
105 if (!ni->vfs_inode.i_nlink && is_rec_inuse(ni->mi.mrec))
106 ni_delete_all(ni);
107
108 al_destroy(ni);
109
110 for (node = rb_first(&ni->mi_tree); node;) {
111 struct rb_node *next = rb_next(node);
112 struct mft_inode *mi = rb_entry(node, struct mft_inode, node);
113
114 rb_erase(node, &ni->mi_tree);
115 mi_put(mi);
116 node = next;
117 }
118
e8b8e97f 119 /* Bad inode always has mode == S_IFREG. */
4342306f
KK
120 if (ni->ni_flags & NI_FLAG_DIR)
121 indx_clear(&ni->dir);
122 else {
123 run_close(&ni->file.run);
124#ifdef CONFIG_NTFS3_LZX_XPRESS
125 if (ni->file.offs_page) {
e8b8e97f 126 /* On-demand allocated page for offsets. */
4342306f
KK
127 put_page(ni->file.offs_page);
128 ni->file.offs_page = NULL;
129 }
130#endif
131 }
132
133 mi_clear(&ni->mi);
134}
135
136/*
e8b8e97f 137 * ni_load_mi_ex - Find mft_inode by record number.
4342306f
KK
138 */
139int ni_load_mi_ex(struct ntfs_inode *ni, CLST rno, struct mft_inode **mi)
140{
141 int err;
142 struct mft_inode *r;
143
144 r = ni_find_mi(ni, rno);
145 if (r)
146 goto out;
147
148 err = mi_get(ni->mi.sbi, rno, &r);
149 if (err)
150 return err;
151
152 ni_add_mi(ni, r);
153
154out:
155 if (mi)
156 *mi = r;
157 return 0;
158}
159
160/*
e8b8e97f 161 * ni_load_mi - Load mft_inode corresponded list_entry.
4342306f 162 */
78ab59fe 163int ni_load_mi(struct ntfs_inode *ni, const struct ATTR_LIST_ENTRY *le,
4342306f
KK
164 struct mft_inode **mi)
165{
166 CLST rno;
167
168 if (!le) {
169 *mi = &ni->mi;
170 return 0;
171 }
172
173 rno = ino_get(&le->ref);
174 if (rno == ni->mi.rno) {
175 *mi = &ni->mi;
176 return 0;
177 }
178 return ni_load_mi_ex(ni, rno, mi);
179}
180
181/*
182 * ni_find_attr
183 *
e8b8e97f 184 * Return: Attribute and record this attribute belongs to.
4342306f
KK
185 */
186struct ATTRIB *ni_find_attr(struct ntfs_inode *ni, struct ATTRIB *attr,
187 struct ATTR_LIST_ENTRY **le_o, enum ATTR_TYPE type,
188 const __le16 *name, u8 name_len, const CLST *vcn,
189 struct mft_inode **mi)
190{
191 struct ATTR_LIST_ENTRY *le;
192 struct mft_inode *m;
193
194 if (!ni->attr_list.size ||
195 (!name_len && (type == ATTR_LIST || type == ATTR_STD))) {
196 if (le_o)
197 *le_o = NULL;
198 if (mi)
199 *mi = &ni->mi;
200
e8b8e97f 201 /* Look for required attribute in primary record. */
4342306f
KK
202 return mi_find_attr(&ni->mi, attr, type, name, name_len, NULL);
203 }
204
e8b8e97f 205 /* First look for list entry of required type. */
4342306f
KK
206 le = al_find_ex(ni, le_o ? *le_o : NULL, type, name, name_len, vcn);
207 if (!le)
208 return NULL;
209
210 if (le_o)
211 *le_o = le;
212
e8b8e97f 213 /* Load record that contains this attribute. */
4342306f
KK
214 if (ni_load_mi(ni, le, &m))
215 return NULL;
216
e8b8e97f 217 /* Look for required attribute. */
4342306f
KK
218 attr = mi_find_attr(m, NULL, type, name, name_len, &le->id);
219
220 if (!attr)
221 goto out;
222
223 if (!attr->non_res) {
224 if (vcn && *vcn)
225 goto out;
226 } else if (!vcn) {
227 if (attr->nres.svcn)
228 goto out;
229 } else if (le64_to_cpu(attr->nres.svcn) > *vcn ||
230 *vcn > le64_to_cpu(attr->nres.evcn)) {
231 goto out;
232 }
233
234 if (mi)
235 *mi = m;
236 return attr;
237
238out:
239 ntfs_set_state(ni->mi.sbi, NTFS_DIRTY_ERROR);
240 return NULL;
241}
242
243/*
e8b8e97f 244 * ni_enum_attr_ex - Enumerates attributes in ntfs_inode.
4342306f
KK
245 */
246struct ATTRIB *ni_enum_attr_ex(struct ntfs_inode *ni, struct ATTRIB *attr,
247 struct ATTR_LIST_ENTRY **le,
248 struct mft_inode **mi)
249{
250 struct mft_inode *mi2;
251 struct ATTR_LIST_ENTRY *le2;
252
253 /* Do we have an attribute list? */
254 if (!ni->attr_list.size) {
255 *le = NULL;
256 if (mi)
257 *mi = &ni->mi;
e8b8e97f 258 /* Enum attributes in primary record. */
4342306f
KK
259 return mi_enum_attr(&ni->mi, attr);
260 }
261
e8b8e97f 262 /* Get next list entry. */
4342306f
KK
263 le2 = *le = al_enumerate(ni, attr ? *le : NULL);
264 if (!le2)
265 return NULL;
266
e8b8e97f 267 /* Load record that contains the required attribute. */
4342306f
KK
268 if (ni_load_mi(ni, le2, &mi2))
269 return NULL;
270
271 if (mi)
272 *mi = mi2;
273
e8b8e97f 274 /* Find attribute in loaded record. */
4342306f
KK
275 return rec_find_attr_le(mi2, le2);
276}
277
278/*
e8b8e97f 279 * ni_load_attr - Load attribute that contains given VCN.
4342306f
KK
280 */
281struct ATTRIB *ni_load_attr(struct ntfs_inode *ni, enum ATTR_TYPE type,
282 const __le16 *name, u8 name_len, CLST vcn,
283 struct mft_inode **pmi)
284{
285 struct ATTR_LIST_ENTRY *le;
286 struct ATTRIB *attr;
287 struct mft_inode *mi;
288 struct ATTR_LIST_ENTRY *next;
289
290 if (!ni->attr_list.size) {
291 if (pmi)
292 *pmi = &ni->mi;
293 return mi_find_attr(&ni->mi, NULL, type, name, name_len, NULL);
294 }
295
296 le = al_find_ex(ni, NULL, type, name, name_len, NULL);
297 if (!le)
298 return NULL;
299
300 /*
e8b8e97f 301 * Unfortunately ATTR_LIST_ENTRY contains only start VCN.
4342306f 302 * So to find the ATTRIB segment that contains 'vcn' we should
e8b8e97f 303 * enumerate some entries.
4342306f
KK
304 */
305 if (vcn) {
306 for (;; le = next) {
307 next = al_find_ex(ni, le, type, name, name_len, NULL);
308 if (!next || le64_to_cpu(next->vcn) > vcn)
309 break;
310 }
311 }
312
313 if (ni_load_mi(ni, le, &mi))
314 return NULL;
315
316 if (pmi)
317 *pmi = mi;
318
319 attr = mi_find_attr(mi, NULL, type, name, name_len, &le->id);
320 if (!attr)
321 return NULL;
322
323 if (!attr->non_res)
324 return attr;
325
326 if (le64_to_cpu(attr->nres.svcn) <= vcn &&
327 vcn <= le64_to_cpu(attr->nres.evcn))
328 return attr;
329
330 return NULL;
331}
332
333/*
e8b8e97f 334 * ni_load_all_mi - Load all subrecords.
4342306f
KK
335 */
336int ni_load_all_mi(struct ntfs_inode *ni)
337{
338 int err;
339 struct ATTR_LIST_ENTRY *le;
340
341 if (!ni->attr_list.size)
342 return 0;
343
344 le = NULL;
345
346 while ((le = al_enumerate(ni, le))) {
347 CLST rno = ino_get(&le->ref);
348
349 if (rno == ni->mi.rno)
350 continue;
351
352 err = ni_load_mi_ex(ni, rno, NULL);
353 if (err)
354 return err;
355 }
356
357 return 0;
358}
359
360/*
e8b8e97f 361 * ni_add_subrecord - Allocate + format + attach a new subrecord.
4342306f
KK
362 */
363bool ni_add_subrecord(struct ntfs_inode *ni, CLST rno, struct mft_inode **mi)
364{
365 struct mft_inode *m;
366
195c52bd 367 m = kzalloc(sizeof(struct mft_inode), GFP_NOFS);
4342306f
KK
368 if (!m)
369 return false;
370
371 if (mi_format_new(m, ni->mi.sbi, rno, 0, ni->mi.rno == MFT_REC_MFT)) {
372 mi_put(m);
373 return false;
374 }
375
376 mi_get_ref(&ni->mi, &m->mrec->parent_ref);
377
378 ni_add_mi(ni, m);
379 *mi = m;
380 return true;
381}
382
383/*
e8b8e97f 384 * ni_remove_attr - Remove all attributes for the given type/name/id.
d3624466 385 */
4342306f
KK
386int ni_remove_attr(struct ntfs_inode *ni, enum ATTR_TYPE type,
387 const __le16 *name, size_t name_len, bool base_only,
388 const __le16 *id)
389{
390 int err;
391 struct ATTRIB *attr;
392 struct ATTR_LIST_ENTRY *le;
393 struct mft_inode *mi;
394 u32 type_in;
395 int diff;
396
397 if (base_only || type == ATTR_LIST || !ni->attr_list.size) {
398 attr = mi_find_attr(&ni->mi, NULL, type, name, name_len, id);
399 if (!attr)
400 return -ENOENT;
401
78ab59fe 402 mi_remove_attr(ni, &ni->mi, attr);
4342306f
KK
403 return 0;
404 }
405
406 type_in = le32_to_cpu(type);
407 le = NULL;
408
409 for (;;) {
410 le = al_enumerate(ni, le);
411 if (!le)
412 return 0;
413
414next_le2:
415 diff = le32_to_cpu(le->type) - type_in;
416 if (diff < 0)
417 continue;
418
419 if (diff > 0)
420 return 0;
421
422 if (le->name_len != name_len)
423 continue;
424
425 if (name_len &&
426 memcmp(le_name(le), name, name_len * sizeof(short)))
427 continue;
428
429 if (id && le->id != *id)
430 continue;
431 err = ni_load_mi(ni, le, &mi);
432 if (err)
433 return err;
434
435 al_remove_le(ni, le);
436
437 attr = mi_find_attr(mi, NULL, type, name, name_len, id);
438 if (!attr)
439 return -ENOENT;
440
78ab59fe 441 mi_remove_attr(ni, mi, attr);
4342306f
KK
442
443 if (PtrOffset(ni->attr_list.le, le) >= ni->attr_list.size)
444 return 0;
445 goto next_le2;
446 }
447}
448
449/*
e8b8e97f 450 * ni_ins_new_attr - Insert the attribute into record.
4342306f 451 *
e8b8e97f 452 * Return: Not full constructed attribute or NULL if not possible to create.
4342306f 453 */
78ab59fe
KK
454static struct ATTRIB *
455ni_ins_new_attr(struct ntfs_inode *ni, struct mft_inode *mi,
456 struct ATTR_LIST_ENTRY *le, enum ATTR_TYPE type,
457 const __le16 *name, u8 name_len, u32 asize, u16 name_off,
458 CLST svcn, struct ATTR_LIST_ENTRY **ins_le)
4342306f
KK
459{
460 int err;
461 struct ATTRIB *attr;
462 bool le_added = false;
463 struct MFT_REF ref;
464
465 mi_get_ref(mi, &ref);
466
467 if (type != ATTR_LIST && !le && ni->attr_list.size) {
468 err = al_add_le(ni, type, name, name_len, svcn, cpu_to_le16(-1),
469 &ref, &le);
470 if (err) {
e8b8e97f 471 /* No memory or no space. */
4342306f
KK
472 return NULL;
473 }
474 le_added = true;
475
476 /*
477 * al_add_le -> attr_set_size (list) -> ni_expand_list
478 * which moves some attributes out of primary record
479 * this means that name may point into moved memory
e8b8e97f 480 * reinit 'name' from le.
4342306f
KK
481 */
482 name = le->name;
483 }
484
485 attr = mi_insert_attr(mi, type, name, name_len, asize, name_off);
486 if (!attr) {
487 if (le_added)
488 al_remove_le(ni, le);
489 return NULL;
490 }
491
492 if (type == ATTR_LIST) {
e8b8e97f 493 /* Attr list is not in list entry array. */
4342306f
KK
494 goto out;
495 }
496
497 if (!le)
498 goto out;
499
e8b8e97f 500 /* Update ATTRIB Id and record reference. */
4342306f
KK
501 le->id = attr->id;
502 ni->attr_list.dirty = true;
503 le->ref = ref;
504
505out:
78ab59fe
KK
506 if (ins_le)
507 *ins_le = le;
4342306f
KK
508 return attr;
509}
510
511/*
e8b8e97f
KA
512 * ni_repack
513 *
514 * Random write access to sparsed or compressed file may result to
4342306f 515 * not optimized packed runs.
e8b8e97f 516 * Here is the place to optimize it.
4342306f
KK
517 */
518static int ni_repack(struct ntfs_inode *ni)
519{
520 int err = 0;
521 struct ntfs_sb_info *sbi = ni->mi.sbi;
522 struct mft_inode *mi, *mi_p = NULL;
523 struct ATTRIB *attr = NULL, *attr_p;
524 struct ATTR_LIST_ENTRY *le = NULL, *le_p;
525 CLST alloc = 0;
526 u8 cluster_bits = sbi->cluster_bits;
527 CLST svcn, evcn = 0, svcn_p, evcn_p, next_svcn;
528 u32 roff, rs = sbi->record_size;
529 struct runs_tree run;
530
531 run_init(&run);
532
533 while ((attr = ni_enum_attr_ex(ni, attr, &le, &mi))) {
534 if (!attr->non_res)
535 continue;
536
537 svcn = le64_to_cpu(attr->nres.svcn);
538 if (svcn != le64_to_cpu(le->vcn)) {
539 err = -EINVAL;
540 break;
541 }
542
543 if (!svcn) {
544 alloc = le64_to_cpu(attr->nres.alloc_size) >>
545 cluster_bits;
546 mi_p = NULL;
547 } else if (svcn != evcn + 1) {
548 err = -EINVAL;
549 break;
550 }
551
552 evcn = le64_to_cpu(attr->nres.evcn);
553
554 if (svcn > evcn + 1) {
555 err = -EINVAL;
556 break;
557 }
558
559 if (!mi_p) {
e8b8e97f 560 /* Do not try if not enogh free space. */
4342306f
KK
561 if (le32_to_cpu(mi->mrec->used) + 8 >= rs)
562 continue;
563
e8b8e97f 564 /* Do not try if last attribute segment. */
4342306f
KK
565 if (evcn + 1 == alloc)
566 continue;
567 run_close(&run);
568 }
569
570 roff = le16_to_cpu(attr->nres.run_off);
571 err = run_unpack(&run, sbi, ni->mi.rno, svcn, evcn, svcn,
572 Add2Ptr(attr, roff),
573 le32_to_cpu(attr->size) - roff);
574 if (err < 0)
575 break;
576
577 if (!mi_p) {
578 mi_p = mi;
579 attr_p = attr;
580 svcn_p = svcn;
581 evcn_p = evcn;
582 le_p = le;
583 err = 0;
584 continue;
585 }
586
587 /*
e8b8e97f
KA
588 * Run contains data from two records: mi_p and mi
589 * Try to pack in one.
4342306f
KK
590 */
591 err = mi_pack_runs(mi_p, attr_p, &run, evcn + 1 - svcn_p);
592 if (err)
593 break;
594
595 next_svcn = le64_to_cpu(attr_p->nres.evcn) + 1;
596
597 if (next_svcn >= evcn + 1) {
e8b8e97f 598 /* We can remove this attribute segment. */
4342306f 599 al_remove_le(ni, le);
78ab59fe 600 mi_remove_attr(NULL, mi, attr);
4342306f
KK
601 le = le_p;
602 continue;
603 }
604
605 attr->nres.svcn = le->vcn = cpu_to_le64(next_svcn);
606 mi->dirty = true;
607 ni->attr_list.dirty = true;
608
609 if (evcn + 1 == alloc) {
610 err = mi_pack_runs(mi, attr, &run,
611 evcn + 1 - next_svcn);
612 if (err)
613 break;
614 mi_p = NULL;
615 } else {
616 mi_p = mi;
617 attr_p = attr;
618 svcn_p = next_svcn;
619 evcn_p = evcn;
620 le_p = le;
621 run_truncate_head(&run, next_svcn);
622 }
623 }
624
625 if (err) {
626 ntfs_inode_warn(&ni->vfs_inode, "repack problem");
627 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
628
e8b8e97f 629 /* Pack loaded but not packed runs. */
4342306f
KK
630 if (mi_p)
631 mi_pack_runs(mi_p, attr_p, &run, evcn_p + 1 - svcn_p);
632 }
633
634 run_close(&run);
635 return err;
636}
637
638/*
639 * ni_try_remove_attr_list
640 *
641 * Can we remove attribute list?
e8b8e97f 642 * Check the case when primary record contains enough space for all attributes.
4342306f
KK
643 */
644static int ni_try_remove_attr_list(struct ntfs_inode *ni)
645{
646 int err = 0;
647 struct ntfs_sb_info *sbi = ni->mi.sbi;
648 struct ATTRIB *attr, *attr_list, *attr_ins;
649 struct ATTR_LIST_ENTRY *le;
650 struct mft_inode *mi;
651 u32 asize, free;
652 struct MFT_REF ref;
19d1b787 653 struct MFT_REC *mrec;
4342306f
KK
654 __le16 id;
655
656 if (!ni->attr_list.dirty)
657 return 0;
658
659 err = ni_repack(ni);
660 if (err)
661 return err;
662
663 attr_list = mi_find_attr(&ni->mi, NULL, ATTR_LIST, NULL, 0, NULL);
664 if (!attr_list)
665 return 0;
666
667 asize = le32_to_cpu(attr_list->size);
668
e8b8e97f 669 /* Free space in primary record without attribute list. */
4342306f
KK
670 free = sbi->record_size - le32_to_cpu(ni->mi.mrec->used) + asize;
671 mi_get_ref(&ni->mi, &ref);
672
673 le = NULL;
674 while ((le = al_enumerate(ni, le))) {
675 if (!memcmp(&le->ref, &ref, sizeof(ref)))
676 continue;
677
678 if (le->vcn)
679 return 0;
680
681 mi = ni_find_mi(ni, ino_get(&le->ref));
682 if (!mi)
683 return 0;
684
685 attr = mi_find_attr(mi, NULL, le->type, le_name(le),
686 le->name_len, &le->id);
687 if (!attr)
688 return 0;
689
690 asize = le32_to_cpu(attr->size);
691 if (asize > free)
692 return 0;
693
694 free -= asize;
695 }
696
19d1b787
KK
697 /* Make a copy of primary record to restore if error. */
698 mrec = kmemdup(ni->mi.mrec, sbi->record_size, GFP_NOFS);
699 if (!mrec)
700 return 0; /* Not critical. */
701
78ab59fe
KK
702 /* It seems that attribute list can be removed from primary record. */
703 mi_remove_attr(NULL, &ni->mi, attr_list);
4342306f
KK
704
705 /*
19d1b787
KK
706 * Repeat the cycle above and copy all attributes to primary record.
707 * Do not remove original attributes from subrecords!
4342306f
KK
708 * It should be success!
709 */
710 le = NULL;
711 while ((le = al_enumerate(ni, le))) {
712 if (!memcmp(&le->ref, &ref, sizeof(ref)))
713 continue;
714
715 mi = ni_find_mi(ni, ino_get(&le->ref));
8607954c
KK
716 if (!mi) {
717 /* Should never happened, 'cause already checked. */
19d1b787 718 goto out;
8607954c 719 }
4342306f
KK
720
721 attr = mi_find_attr(mi, NULL, le->type, le_name(le),
722 le->name_len, &le->id);
8607954c
KK
723 if (!attr) {
724 /* Should never happened, 'cause already checked. */
19d1b787 725 goto out;
8607954c 726 }
4342306f
KK
727 asize = le32_to_cpu(attr->size);
728
e8b8e97f 729 /* Insert into primary record. */
4342306f
KK
730 attr_ins = mi_insert_attr(&ni->mi, le->type, le_name(le),
731 le->name_len, asize,
732 le16_to_cpu(attr->name_off));
8607954c
KK
733 if (!attr_ins) {
734 /*
19d1b787 735 * No space in primary record (already checked).
8607954c 736 */
19d1b787 737 goto out;
8607954c 738 }
4342306f 739
e8b8e97f 740 /* Copy all except id. */
8607954c 741 id = attr_ins->id;
4342306f
KK
742 memcpy(attr_ins, attr, asize);
743 attr_ins->id = id;
19d1b787
KK
744 }
745
746 /*
747 * Repeat the cycle above and remove all attributes from subrecords.
748 */
749 le = NULL;
750 while ((le = al_enumerate(ni, le))) {
751 if (!memcmp(&le->ref, &ref, sizeof(ref)))
752 continue;
753
754 mi = ni_find_mi(ni, ino_get(&le->ref));
755 if (!mi)
756 continue;
757
758 attr = mi_find_attr(mi, NULL, le->type, le_name(le),
759 le->name_len, &le->id);
760 if (!attr)
761 continue;
4342306f 762
e8b8e97f 763 /* Remove from original record. */
78ab59fe 764 mi_remove_attr(NULL, mi, attr);
4342306f
KK
765 }
766
767 run_deallocate(sbi, &ni->attr_list.run, true);
768 run_close(&ni->attr_list.run);
769 ni->attr_list.size = 0;
195c52bd 770 kfree(ni->attr_list.le);
4342306f
KK
771 ni->attr_list.le = NULL;
772 ni->attr_list.dirty = false;
773
19d1b787
KK
774 kfree(mrec);
775 return 0;
776out:
777 /* Restore primary record. */
778 swap(mrec, ni->mi.mrec);
779 kfree(mrec);
4342306f
KK
780 return 0;
781}
782
783/*
e8b8e97f 784 * ni_create_attr_list - Generates an attribute list for this primary record.
d3624466 785 */
4342306f
KK
786int ni_create_attr_list(struct ntfs_inode *ni)
787{
788 struct ntfs_sb_info *sbi = ni->mi.sbi;
789 int err;
790 u32 lsize;
791 struct ATTRIB *attr;
792 struct ATTRIB *arr_move[7];
793 struct ATTR_LIST_ENTRY *le, *le_b[7];
794 struct MFT_REC *rec;
795 bool is_mft;
796 CLST rno = 0;
797 struct mft_inode *mi;
798 u32 free_b, nb, to_free, rs;
799 u16 sz;
800
801 is_mft = ni->mi.rno == MFT_REC_MFT;
802 rec = ni->mi.mrec;
803 rs = sbi->record_size;
804
805 /*
e8b8e97f
KA
806 * Skip estimating exact memory requirement.
807 * Looks like one record_size is always enough.
4342306f 808 */
195c52bd 809 le = kmalloc(al_aligned(rs), GFP_NOFS);
4342306f
KK
810 if (!le) {
811 err = -ENOMEM;
812 goto out;
813 }
814
815 mi_get_ref(&ni->mi, &le->ref);
816 ni->attr_list.le = le;
817
818 attr = NULL;
819 nb = 0;
820 free_b = 0;
821 attr = NULL;
822
823 for (; (attr = mi_enum_attr(&ni->mi, attr)); le = Add2Ptr(le, sz)) {
824 sz = le_size(attr->name_len);
825 le->type = attr->type;
826 le->size = cpu_to_le16(sz);
827 le->name_len = attr->name_len;
828 le->name_off = offsetof(struct ATTR_LIST_ENTRY, name);
829 le->vcn = 0;
830 if (le != ni->attr_list.le)
831 le->ref = ni->attr_list.le->ref;
832 le->id = attr->id;
833
834 if (attr->name_len)
835 memcpy(le->name, attr_name(attr),
836 sizeof(short) * attr->name_len);
837 else if (attr->type == ATTR_STD)
838 continue;
839 else if (attr->type == ATTR_LIST)
840 continue;
841 else if (is_mft && attr->type == ATTR_DATA)
842 continue;
843
844 if (!nb || nb < ARRAY_SIZE(arr_move)) {
845 le_b[nb] = le;
846 arr_move[nb++] = attr;
847 free_b += le32_to_cpu(attr->size);
848 }
849 }
850
851 lsize = PtrOffset(ni->attr_list.le, le);
852 ni->attr_list.size = lsize;
853
854 to_free = le32_to_cpu(rec->used) + lsize + SIZEOF_RESIDENT;
855 if (to_free <= rs) {
856 to_free = 0;
857 } else {
858 to_free -= rs;
859
860 if (to_free > free_b) {
861 err = -EINVAL;
862 goto out1;
863 }
864 }
865
e8b8e97f 866 /* Allocate child MFT. */
4342306f
KK
867 err = ntfs_look_free_mft(sbi, &rno, is_mft, ni, &mi);
868 if (err)
869 goto out1;
870
e8b8e97f 871 /* Call mi_remove_attr() in reverse order to keep pointers 'arr_move' valid. */
4342306f
KK
872 while (to_free > 0) {
873 struct ATTRIB *b = arr_move[--nb];
874 u32 asize = le32_to_cpu(b->size);
875 u16 name_off = le16_to_cpu(b->name_off);
876
877 attr = mi_insert_attr(mi, b->type, Add2Ptr(b, name_off),
878 b->name_len, asize, name_off);
879 WARN_ON(!attr);
880
881 mi_get_ref(mi, &le_b[nb]->ref);
882 le_b[nb]->id = attr->id;
883
e8b8e97f 884 /* Copy all except id. */
4342306f
KK
885 memcpy(attr, b, asize);
886 attr->id = le_b[nb]->id;
887
78ab59fe
KK
888 /* Remove from primary record. */
889 WARN_ON(!mi_remove_attr(NULL, &ni->mi, b));
4342306f
KK
890
891 if (to_free <= asize)
892 break;
893 to_free -= asize;
894 WARN_ON(!nb);
895 }
896
897 attr = mi_insert_attr(&ni->mi, ATTR_LIST, NULL, 0,
898 lsize + SIZEOF_RESIDENT, SIZEOF_RESIDENT);
899 WARN_ON(!attr);
900
901 attr->non_res = 0;
902 attr->flags = 0;
903 attr->res.data_size = cpu_to_le32(lsize);
904 attr->res.data_off = SIZEOF_RESIDENT_LE;
905 attr->res.flags = 0;
906 attr->res.res = 0;
907
908 memcpy(resident_data_ex(attr, lsize), ni->attr_list.le, lsize);
909
910 ni->attr_list.dirty = false;
911
912 mark_inode_dirty(&ni->vfs_inode);
913 goto out;
914
915out1:
195c52bd 916 kfree(ni->attr_list.le);
4342306f
KK
917 ni->attr_list.le = NULL;
918 ni->attr_list.size = 0;
919
920out:
921 return err;
922}
923
924/*
e8b8e97f 925 * ni_ins_attr_ext - Add an external attribute to the ntfs_inode.
4342306f
KK
926 */
927static int ni_ins_attr_ext(struct ntfs_inode *ni, struct ATTR_LIST_ENTRY *le,
928 enum ATTR_TYPE type, const __le16 *name, u8 name_len,
929 u32 asize, CLST svcn, u16 name_off, bool force_ext,
78ab59fe
KK
930 struct ATTRIB **ins_attr, struct mft_inode **ins_mi,
931 struct ATTR_LIST_ENTRY **ins_le)
4342306f
KK
932{
933 struct ATTRIB *attr;
934 struct mft_inode *mi;
935 CLST rno;
936 u64 vbo;
937 struct rb_node *node;
938 int err;
939 bool is_mft, is_mft_data;
940 struct ntfs_sb_info *sbi = ni->mi.sbi;
941
942 is_mft = ni->mi.rno == MFT_REC_MFT;
943 is_mft_data = is_mft && type == ATTR_DATA && !name_len;
944
945 if (asize > sbi->max_bytes_per_attr) {
946 err = -EINVAL;
947 goto out;
948 }
949
950 /*
e8b8e97f
KA
951 * Standard information and attr_list cannot be made external.
952 * The Log File cannot have any external attributes.
4342306f
KK
953 */
954 if (type == ATTR_STD || type == ATTR_LIST ||
955 ni->mi.rno == MFT_REC_LOG) {
956 err = -EINVAL;
957 goto out;
958 }
959
e8b8e97f 960 /* Create attribute list if it is not already existed. */
4342306f
KK
961 if (!ni->attr_list.size) {
962 err = ni_create_attr_list(ni);
963 if (err)
964 goto out;
965 }
966
967 vbo = is_mft_data ? ((u64)svcn << sbi->cluster_bits) : 0;
968
969 if (force_ext)
970 goto insert_ext;
971
972 /* Load all subrecords into memory. */
973 err = ni_load_all_mi(ni);
974 if (err)
975 goto out;
976
e8b8e97f 977 /* Check each of loaded subrecord. */
4342306f
KK
978 for (node = rb_first(&ni->mi_tree); node; node = rb_next(node)) {
979 mi = rb_entry(node, struct mft_inode, node);
980
981 if (is_mft_data &&
982 (mi_enum_attr(mi, NULL) ||
983 vbo <= ((u64)mi->rno << sbi->record_bits))) {
d3624466 984 /* We can't accept this record 'cause MFT's bootstrapping. */
4342306f
KK
985 continue;
986 }
987 if (is_mft &&
988 mi_find_attr(mi, NULL, ATTR_DATA, NULL, 0, NULL)) {
989 /*
990 * This child record already has a ATTR_DATA.
991 * So it can't accept any other records.
992 */
993 continue;
994 }
995
996 if ((type != ATTR_NAME || name_len) &&
997 mi_find_attr(mi, NULL, type, name, name_len, NULL)) {
e8b8e97f 998 /* Only indexed attributes can share same record. */
4342306f
KK
999 continue;
1000 }
1001
ee9d4810
KK
1002 /*
1003 * Do not try to insert this attribute
1004 * if there is no room in record.
1005 */
1006 if (le32_to_cpu(mi->mrec->used) + asize > sbi->record_size)
1007 continue;
1008
e8b8e97f 1009 /* Try to insert attribute into this subrecord. */
4342306f 1010 attr = ni_ins_new_attr(ni, mi, le, type, name, name_len, asize,
78ab59fe 1011 name_off, svcn, ins_le);
4342306f
KK
1012 if (!attr)
1013 continue;
1014
1015 if (ins_attr)
1016 *ins_attr = attr;
78ab59fe
KK
1017 if (ins_mi)
1018 *ins_mi = mi;
4342306f
KK
1019 return 0;
1020 }
1021
1022insert_ext:
e8b8e97f 1023 /* We have to allocate a new child subrecord. */
4342306f
KK
1024 err = ntfs_look_free_mft(sbi, &rno, is_mft_data, ni, &mi);
1025 if (err)
1026 goto out;
1027
1028 if (is_mft_data && vbo <= ((u64)rno << sbi->record_bits)) {
1029 err = -EINVAL;
1030 goto out1;
1031 }
1032
1033 attr = ni_ins_new_attr(ni, mi, le, type, name, name_len, asize,
78ab59fe 1034 name_off, svcn, ins_le);
4342306f
KK
1035 if (!attr)
1036 goto out2;
1037
1038 if (ins_attr)
1039 *ins_attr = attr;
1040 if (ins_mi)
1041 *ins_mi = mi;
1042
1043 return 0;
1044
1045out2:
1046 ni_remove_mi(ni, mi);
1047 mi_put(mi);
1048 err = -EINVAL;
1049
1050out1:
071100ea 1051 ntfs_mark_rec_free(sbi, rno, is_mft);
4342306f
KK
1052
1053out:
1054 return err;
1055}
1056
1057/*
e8b8e97f 1058 * ni_insert_attr - Insert an attribute into the file.
4342306f
KK
1059 *
1060 * If the primary record has room, it will just insert the attribute.
1061 * If not, it may make the attribute external.
1062 * For $MFT::Data it may make room for the attribute by
1063 * making other attributes external.
1064 *
1065 * NOTE:
1066 * The ATTR_LIST and ATTR_STD cannot be made external.
e8b8e97f
KA
1067 * This function does not fill new attribute full.
1068 * It only fills 'size'/'type'/'id'/'name_len' fields.
4342306f
KK
1069 */
1070static int ni_insert_attr(struct ntfs_inode *ni, enum ATTR_TYPE type,
1071 const __le16 *name, u8 name_len, u32 asize,
1072 u16 name_off, CLST svcn, struct ATTRIB **ins_attr,
78ab59fe
KK
1073 struct mft_inode **ins_mi,
1074 struct ATTR_LIST_ENTRY **ins_le)
4342306f
KK
1075{
1076 struct ntfs_sb_info *sbi = ni->mi.sbi;
1077 int err;
1078 struct ATTRIB *attr, *eattr;
1079 struct MFT_REC *rec;
1080 bool is_mft;
1081 struct ATTR_LIST_ENTRY *le;
1082 u32 list_reserve, max_free, free, used, t32;
1083 __le16 id;
1084 u16 t16;
1085
1086 is_mft = ni->mi.rno == MFT_REC_MFT;
1087 rec = ni->mi.mrec;
1088
1089 list_reserve = SIZEOF_NONRESIDENT + 3 * (1 + 2 * sizeof(u32));
1090 used = le32_to_cpu(rec->used);
1091 free = sbi->record_size - used;
1092
1093 if (is_mft && type != ATTR_LIST) {
e8b8e97f 1094 /* Reserve space for the ATTRIB list. */
4342306f
KK
1095 if (free < list_reserve)
1096 free = 0;
1097 else
1098 free -= list_reserve;
1099 }
1100
1101 if (asize <= free) {
1102 attr = ni_ins_new_attr(ni, &ni->mi, NULL, type, name, name_len,
78ab59fe 1103 asize, name_off, svcn, ins_le);
4342306f
KK
1104 if (attr) {
1105 if (ins_attr)
1106 *ins_attr = attr;
1107 if (ins_mi)
1108 *ins_mi = &ni->mi;
1109 err = 0;
1110 goto out;
1111 }
1112 }
1113
1114 if (!is_mft || type != ATTR_DATA || svcn) {
1115 /* This ATTRIB will be external. */
1116 err = ni_ins_attr_ext(ni, NULL, type, name, name_len, asize,
78ab59fe
KK
1117 svcn, name_off, false, ins_attr, ins_mi,
1118 ins_le);
4342306f
KK
1119 goto out;
1120 }
1121
1122 /*
e8b8e97f 1123 * Here we have: "is_mft && type == ATTR_DATA && !svcn"
4342306f
KK
1124 *
1125 * The first chunk of the $MFT::Data ATTRIB must be the base record.
1126 * Evict as many other attributes as possible.
1127 */
1128 max_free = free;
1129
d3624466 1130 /* Estimate the result of moving all possible attributes away. */
4342306f
KK
1131 attr = NULL;
1132
1133 while ((attr = mi_enum_attr(&ni->mi, attr))) {
1134 if (attr->type == ATTR_STD)
1135 continue;
1136 if (attr->type == ATTR_LIST)
1137 continue;
1138 max_free += le32_to_cpu(attr->size);
1139 }
1140
1141 if (max_free < asize + list_reserve) {
e8b8e97f 1142 /* Impossible to insert this attribute into primary record. */
4342306f
KK
1143 err = -EINVAL;
1144 goto out;
1145 }
1146
d3624466 1147 /* Start real attribute moving. */
4342306f
KK
1148 attr = NULL;
1149
1150 for (;;) {
1151 attr = mi_enum_attr(&ni->mi, attr);
1152 if (!attr) {
e8b8e97f 1153 /* We should never be here 'cause we have already check this case. */
4342306f
KK
1154 err = -EINVAL;
1155 goto out;
1156 }
1157
e8b8e97f 1158 /* Skip attributes that MUST be primary record. */
4342306f
KK
1159 if (attr->type == ATTR_STD || attr->type == ATTR_LIST)
1160 continue;
1161
1162 le = NULL;
1163 if (ni->attr_list.size) {
1164 le = al_find_le(ni, NULL, attr);
1165 if (!le) {
e8b8e97f 1166 /* Really this is a serious bug. */
4342306f
KK
1167 err = -EINVAL;
1168 goto out;
1169 }
1170 }
1171
1172 t32 = le32_to_cpu(attr->size);
1173 t16 = le16_to_cpu(attr->name_off);
1174 err = ni_ins_attr_ext(ni, le, attr->type, Add2Ptr(attr, t16),
1175 attr->name_len, t32, attr_svcn(attr), t16,
78ab59fe 1176 false, &eattr, NULL, NULL);
4342306f
KK
1177 if (err)
1178 return err;
1179
1180 id = eattr->id;
1181 memcpy(eattr, attr, t32);
1182 eattr->id = id;
1183
78ab59fe
KK
1184 /* Remove from primary record. */
1185 mi_remove_attr(NULL, &ni->mi, attr);
4342306f 1186
e8b8e97f 1187 /* attr now points to next attribute. */
4342306f
KK
1188 if (attr->type == ATTR_END)
1189 goto out;
1190 }
1191 while (asize + list_reserve > sbi->record_size - le32_to_cpu(rec->used))
1192 ;
1193
1194 attr = ni_ins_new_attr(ni, &ni->mi, NULL, type, name, name_len, asize,
78ab59fe 1195 name_off, svcn, ins_le);
4342306f
KK
1196 if (!attr) {
1197 err = -EINVAL;
1198 goto out;
1199 }
1200
1201 if (ins_attr)
1202 *ins_attr = attr;
1203 if (ins_mi)
1204 *ins_mi = &ni->mi;
1205
1206out:
1207 return err;
1208}
1209
e8b8e97f 1210/* ni_expand_mft_list - Split ATTR_DATA of $MFT. */
4342306f
KK
1211static int ni_expand_mft_list(struct ntfs_inode *ni)
1212{
1213 int err = 0;
1214 struct runs_tree *run = &ni->file.run;
1215 u32 asize, run_size, done = 0;
1216 struct ATTRIB *attr;
1217 struct rb_node *node;
1218 CLST mft_min, mft_new, svcn, evcn, plen;
1219 struct mft_inode *mi, *mi_min, *mi_new;
1220 struct ntfs_sb_info *sbi = ni->mi.sbi;
1221
e8b8e97f 1222 /* Find the nearest MFT. */
4342306f
KK
1223 mft_min = 0;
1224 mft_new = 0;
1225 mi_min = NULL;
1226
1227 for (node = rb_first(&ni->mi_tree); node; node = rb_next(node)) {
1228 mi = rb_entry(node, struct mft_inode, node);
1229
1230 attr = mi_enum_attr(mi, NULL);
1231
1232 if (!attr) {
1233 mft_min = mi->rno;
1234 mi_min = mi;
1235 break;
1236 }
1237 }
1238
1239 if (ntfs_look_free_mft(sbi, &mft_new, true, ni, &mi_new)) {
1240 mft_new = 0;
e8b8e97f 1241 /* Really this is not critical. */
4342306f
KK
1242 } else if (mft_min > mft_new) {
1243 mft_min = mft_new;
1244 mi_min = mi_new;
1245 } else {
071100ea 1246 ntfs_mark_rec_free(sbi, mft_new, true);
4342306f
KK
1247 mft_new = 0;
1248 ni_remove_mi(ni, mi_new);
1249 }
1250
1251 attr = mi_find_attr(&ni->mi, NULL, ATTR_DATA, NULL, 0, NULL);
1252 if (!attr) {
1253 err = -EINVAL;
1254 goto out;
1255 }
1256
1257 asize = le32_to_cpu(attr->size);
1258
1259 evcn = le64_to_cpu(attr->nres.evcn);
1260 svcn = bytes_to_cluster(sbi, (u64)(mft_min + 1) << sbi->record_bits);
1261 if (evcn + 1 >= svcn) {
1262 err = -EINVAL;
1263 goto out;
1264 }
1265
1266 /*
e8b8e97f 1267 * Split primary attribute [0 evcn] in two parts [0 svcn) + [svcn evcn].
4342306f 1268 *
e8b8e97f 1269 * Update first part of ATTR_DATA in 'primary MFT.
4342306f
KK
1270 */
1271 err = run_pack(run, 0, svcn, Add2Ptr(attr, SIZEOF_NONRESIDENT),
1272 asize - SIZEOF_NONRESIDENT, &plen);
1273 if (err < 0)
1274 goto out;
1275
fa3cacf5 1276 run_size = ALIGN(err, 8);
4342306f
KK
1277 err = 0;
1278
1279 if (plen < svcn) {
1280 err = -EINVAL;
1281 goto out;
1282 }
1283
1284 attr->nres.evcn = cpu_to_le64(svcn - 1);
1285 attr->size = cpu_to_le32(run_size + SIZEOF_NONRESIDENT);
e8b8e97f 1286 /* 'done' - How many bytes of primary MFT becomes free. */
4342306f
KK
1287 done = asize - run_size - SIZEOF_NONRESIDENT;
1288 le32_sub_cpu(&ni->mi.mrec->used, done);
1289
e8b8e97f 1290 /* Estimate the size of second part: run_buf=NULL. */
4342306f
KK
1291 err = run_pack(run, svcn, evcn + 1 - svcn, NULL, sbi->record_size,
1292 &plen);
1293 if (err < 0)
1294 goto out;
1295
fa3cacf5 1296 run_size = ALIGN(err, 8);
4342306f
KK
1297 err = 0;
1298
1299 if (plen < evcn + 1 - svcn) {
1300 err = -EINVAL;
1301 goto out;
1302 }
1303
1304 /*
e8b8e97f
KA
1305 * This function may implicitly call expand attr_list.
1306 * Insert second part of ATTR_DATA in 'mi_min'.
4342306f
KK
1307 */
1308 attr = ni_ins_new_attr(ni, mi_min, NULL, ATTR_DATA, NULL, 0,
1309 SIZEOF_NONRESIDENT + run_size,
78ab59fe 1310 SIZEOF_NONRESIDENT, svcn, NULL);
4342306f
KK
1311 if (!attr) {
1312 err = -EINVAL;
1313 goto out;
1314 }
1315
1316 attr->non_res = 1;
1317 attr->name_off = SIZEOF_NONRESIDENT_LE;
1318 attr->flags = 0;
1319
1320 run_pack(run, svcn, evcn + 1 - svcn, Add2Ptr(attr, SIZEOF_NONRESIDENT),
1321 run_size, &plen);
1322
1323 attr->nres.svcn = cpu_to_le64(svcn);
1324 attr->nres.evcn = cpu_to_le64(evcn);
1325 attr->nres.run_off = cpu_to_le16(SIZEOF_NONRESIDENT);
1326
1327out:
1328 if (mft_new) {
071100ea 1329 ntfs_mark_rec_free(sbi, mft_new, true);
4342306f
KK
1330 ni_remove_mi(ni, mi_new);
1331 }
1332
1333 return !err && !done ? -EOPNOTSUPP : err;
1334}
1335
1336/*
e8b8e97f 1337 * ni_expand_list - Move all possible attributes out of primary record.
4342306f
KK
1338 */
1339int ni_expand_list(struct ntfs_inode *ni)
1340{
1341 int err = 0;
1342 u32 asize, done = 0;
1343 struct ATTRIB *attr, *ins_attr;
1344 struct ATTR_LIST_ENTRY *le;
1345 bool is_mft = ni->mi.rno == MFT_REC_MFT;
1346 struct MFT_REF ref;
1347
1348 mi_get_ref(&ni->mi, &ref);
1349 le = NULL;
1350
1351 while ((le = al_enumerate(ni, le))) {
1352 if (le->type == ATTR_STD)
1353 continue;
1354
1355 if (memcmp(&ref, &le->ref, sizeof(struct MFT_REF)))
1356 continue;
1357
1358 if (is_mft && le->type == ATTR_DATA)
1359 continue;
1360
e8b8e97f 1361 /* Find attribute in primary record. */
4342306f
KK
1362 attr = rec_find_attr_le(&ni->mi, le);
1363 if (!attr) {
1364 err = -EINVAL;
1365 goto out;
1366 }
1367
1368 asize = le32_to_cpu(attr->size);
1369
e8b8e97f 1370 /* Always insert into new record to avoid collisions (deep recursive). */
4342306f
KK
1371 err = ni_ins_attr_ext(ni, le, attr->type, attr_name(attr),
1372 attr->name_len, asize, attr_svcn(attr),
1373 le16_to_cpu(attr->name_off), true,
78ab59fe 1374 &ins_attr, NULL, NULL);
4342306f
KK
1375
1376 if (err)
1377 goto out;
1378
1379 memcpy(ins_attr, attr, asize);
1380 ins_attr->id = le->id;
78ab59fe
KK
1381 /* Remove from primary record. */
1382 mi_remove_attr(NULL, &ni->mi, attr);
4342306f
KK
1383
1384 done += asize;
1385 goto out;
1386 }
1387
1388 if (!is_mft) {
e8b8e97f 1389 err = -EFBIG; /* Attr list is too big(?) */
4342306f
KK
1390 goto out;
1391 }
1392
e8b8e97f 1393 /* Split MFT data as much as possible. */
4342306f
KK
1394 err = ni_expand_mft_list(ni);
1395 if (err)
1396 goto out;
1397
1398out:
1399 return !err && !done ? -EOPNOTSUPP : err;
1400}
1401
1402/*
e8b8e97f 1403 * ni_insert_nonresident - Insert new nonresident attribute.
4342306f
KK
1404 */
1405int ni_insert_nonresident(struct ntfs_inode *ni, enum ATTR_TYPE type,
1406 const __le16 *name, u8 name_len,
1407 const struct runs_tree *run, CLST svcn, CLST len,
1408 __le16 flags, struct ATTRIB **new_attr,
c1e0ab37 1409 struct mft_inode **mi, struct ATTR_LIST_ENTRY **le)
4342306f
KK
1410{
1411 int err;
1412 CLST plen;
1413 struct ATTRIB *attr;
1414 bool is_ext =
1415 (flags & (ATTR_FLAG_SPARSED | ATTR_FLAG_COMPRESSED)) && !svcn;
fa3cacf5 1416 u32 name_size = ALIGN(name_len * sizeof(short), 8);
4342306f
KK
1417 u32 name_off = is_ext ? SIZEOF_NONRESIDENT_EX : SIZEOF_NONRESIDENT;
1418 u32 run_off = name_off + name_size;
1419 u32 run_size, asize;
1420 struct ntfs_sb_info *sbi = ni->mi.sbi;
1421
1422 err = run_pack(run, svcn, len, NULL, sbi->max_bytes_per_attr - run_off,
1423 &plen);
1424 if (err < 0)
1425 goto out;
1426
fa3cacf5 1427 run_size = ALIGN(err, 8);
4342306f
KK
1428
1429 if (plen < len) {
1430 err = -EINVAL;
1431 goto out;
1432 }
1433
1434 asize = run_off + run_size;
1435
1436 if (asize > sbi->max_bytes_per_attr) {
1437 err = -EINVAL;
1438 goto out;
1439 }
1440
1441 err = ni_insert_attr(ni, type, name, name_len, asize, name_off, svcn,
c1e0ab37 1442 &attr, mi, le);
4342306f
KK
1443
1444 if (err)
1445 goto out;
1446
1447 attr->non_res = 1;
1448 attr->name_off = cpu_to_le16(name_off);
1449 attr->flags = flags;
1450
1451 run_pack(run, svcn, len, Add2Ptr(attr, run_off), run_size, &plen);
1452
1453 attr->nres.svcn = cpu_to_le64(svcn);
1454 attr->nres.evcn = cpu_to_le64((u64)svcn + len - 1);
1455
1456 err = 0;
1457 if (new_attr)
1458 *new_attr = attr;
1459
1460 *(__le64 *)&attr->nres.run_off = cpu_to_le64(run_off);
1461
1462 attr->nres.alloc_size =
1463 svcn ? 0 : cpu_to_le64((u64)len << ni->mi.sbi->cluster_bits);
1464 attr->nres.data_size = attr->nres.alloc_size;
1465 attr->nres.valid_size = attr->nres.alloc_size;
1466
1467 if (is_ext) {
1468 if (flags & ATTR_FLAG_COMPRESSED)
1469 attr->nres.c_unit = COMPRESSION_UNIT;
1470 attr->nres.total_size = attr->nres.alloc_size;
1471 }
1472
1473out:
1474 return err;
1475}
1476
1477/*
e8b8e97f 1478 * ni_insert_resident - Inserts new resident attribute.
4342306f
KK
1479 */
1480int ni_insert_resident(struct ntfs_inode *ni, u32 data_size,
1481 enum ATTR_TYPE type, const __le16 *name, u8 name_len,
78ab59fe
KK
1482 struct ATTRIB **new_attr, struct mft_inode **mi,
1483 struct ATTR_LIST_ENTRY **le)
4342306f
KK
1484{
1485 int err;
fa3cacf5
KA
1486 u32 name_size = ALIGN(name_len * sizeof(short), 8);
1487 u32 asize = SIZEOF_RESIDENT + name_size + ALIGN(data_size, 8);
4342306f
KK
1488 struct ATTRIB *attr;
1489
1490 err = ni_insert_attr(ni, type, name, name_len, asize, SIZEOF_RESIDENT,
78ab59fe 1491 0, &attr, mi, le);
4342306f
KK
1492 if (err)
1493 return err;
1494
1495 attr->non_res = 0;
1496 attr->flags = 0;
1497
1498 attr->res.data_size = cpu_to_le32(data_size);
1499 attr->res.data_off = cpu_to_le16(SIZEOF_RESIDENT + name_size);
78ab59fe 1500 if (type == ATTR_NAME) {
4342306f 1501 attr->res.flags = RESIDENT_FLAG_INDEXED;
78ab59fe
KK
1502
1503 /* is_attr_indexed(attr)) == true */
7d95995a 1504 le16_add_cpu(&ni->mi.mrec->hard_links, 1);
78ab59fe
KK
1505 ni->mi.dirty = true;
1506 }
4342306f
KK
1507 attr->res.res = 0;
1508
1509 if (new_attr)
1510 *new_attr = attr;
1511
1512 return 0;
1513}
1514
1515/*
e8b8e97f 1516 * ni_remove_attr_le - Remove attribute from record.
4342306f 1517 */
78ab59fe
KK
1518void ni_remove_attr_le(struct ntfs_inode *ni, struct ATTRIB *attr,
1519 struct mft_inode *mi, struct ATTR_LIST_ENTRY *le)
4342306f 1520{
78ab59fe 1521 mi_remove_attr(ni, mi, attr);
4342306f
KK
1522
1523 if (le)
1524 al_remove_le(ni, le);
4342306f
KK
1525}
1526
1527/*
e8b8e97f 1528 * ni_delete_all - Remove all attributes and frees allocates space.
4342306f 1529 *
e8b8e97f 1530 * ntfs_evict_inode->ntfs_clear_inode->ni_delete_all (if no links).
4342306f
KK
1531 */
1532int ni_delete_all(struct ntfs_inode *ni)
1533{
1534 int err;
1535 struct ATTR_LIST_ENTRY *le = NULL;
1536 struct ATTRIB *attr = NULL;
1537 struct rb_node *node;
1538 u16 roff;
1539 u32 asize;
1540 CLST svcn, evcn;
1541 struct ntfs_sb_info *sbi = ni->mi.sbi;
1542 bool nt3 = is_ntfs3(sbi);
1543 struct MFT_REF ref;
1544
1545 while ((attr = ni_enum_attr_ex(ni, attr, &le, NULL))) {
1546 if (!nt3 || attr->name_len) {
1547 ;
1548 } else if (attr->type == ATTR_REPARSE) {
1549 mi_get_ref(&ni->mi, &ref);
1550 ntfs_remove_reparse(sbi, 0, &ref);
1551 } else if (attr->type == ATTR_ID && !attr->non_res &&
1552 le32_to_cpu(attr->res.data_size) >=
1553 sizeof(struct GUID)) {
1554 ntfs_objid_remove(sbi, resident_data(attr));
1555 }
1556
1557 if (!attr->non_res)
1558 continue;
1559
1560 svcn = le64_to_cpu(attr->nres.svcn);
1561 evcn = le64_to_cpu(attr->nres.evcn);
1562
1563 if (evcn + 1 <= svcn)
1564 continue;
1565
1566 asize = le32_to_cpu(attr->size);
1567 roff = le16_to_cpu(attr->nres.run_off);
1568
e8b8e97f 1569 /* run==1 means unpack and deallocate. */
4342306f
KK
1570 run_unpack_ex(RUN_DEALLOCATE, sbi, ni->mi.rno, svcn, evcn, svcn,
1571 Add2Ptr(attr, roff), asize - roff);
1572 }
1573
1574 if (ni->attr_list.size) {
1575 run_deallocate(ni->mi.sbi, &ni->attr_list.run, true);
1576 al_destroy(ni);
1577 }
1578
e8b8e97f 1579 /* Free all subrecords. */
4342306f
KK
1580 for (node = rb_first(&ni->mi_tree); node;) {
1581 struct rb_node *next = rb_next(node);
1582 struct mft_inode *mi = rb_entry(node, struct mft_inode, node);
1583
1584 clear_rec_inuse(mi->mrec);
1585 mi->dirty = true;
1586 mi_write(mi, 0);
1587
071100ea 1588 ntfs_mark_rec_free(sbi, mi->rno, false);
4342306f
KK
1589 ni_remove_mi(ni, mi);
1590 mi_put(mi);
1591 node = next;
1592 }
1593
d3624466 1594 /* Free base record. */
4342306f
KK
1595 clear_rec_inuse(ni->mi.mrec);
1596 ni->mi.dirty = true;
1597 err = mi_write(&ni->mi, 0);
1598
071100ea 1599 ntfs_mark_rec_free(sbi, ni->mi.rno, false);
4342306f
KK
1600
1601 return err;
1602}
1603
e8b8e97f 1604/* ni_fname_name
4342306f 1605 *
78ab59fe
KK
1606 * Return: File name attribute by its value.
1607 */
4342306f
KK
1608struct ATTR_FILE_NAME *ni_fname_name(struct ntfs_inode *ni,
1609 const struct cpu_str *uni,
1610 const struct MFT_REF *home_dir,
78ab59fe 1611 struct mft_inode **mi,
4342306f
KK
1612 struct ATTR_LIST_ENTRY **le)
1613{
1614 struct ATTRIB *attr = NULL;
1615 struct ATTR_FILE_NAME *fname;
1616
1617 *le = NULL;
1618
e8b8e97f 1619 /* Enumerate all names. */
4342306f 1620next:
78ab59fe 1621 attr = ni_find_attr(ni, attr, le, ATTR_NAME, NULL, 0, NULL, mi);
4342306f
KK
1622 if (!attr)
1623 return NULL;
1624
1625 fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
1626 if (!fname)
1627 goto next;
1628
1629 if (home_dir && memcmp(home_dir, &fname->home, sizeof(*home_dir)))
1630 goto next;
1631
1632 if (!uni)
1633 goto next;
1634
1635 if (uni->len != fname->name_len)
1636 goto next;
1637
1638 if (ntfs_cmp_names_cpu(uni, (struct le_str *)&fname->name_len, NULL,
1639 false))
1640 goto next;
1641
1642 return fname;
1643}
1644
1645/*
1646 * ni_fname_type
1647 *
e8b8e97f 1648 * Return: File name attribute with given type.
4342306f
KK
1649 */
1650struct ATTR_FILE_NAME *ni_fname_type(struct ntfs_inode *ni, u8 name_type,
78ab59fe 1651 struct mft_inode **mi,
4342306f
KK
1652 struct ATTR_LIST_ENTRY **le)
1653{
1654 struct ATTRIB *attr = NULL;
1655 struct ATTR_FILE_NAME *fname;
1656
1657 *le = NULL;
1658
4ca7fe57 1659 if (name_type == FILE_NAME_POSIX)
78ab59fe
KK
1660 return NULL;
1661
e8b8e97f 1662 /* Enumerate all names. */
4342306f 1663 for (;;) {
78ab59fe 1664 attr = ni_find_attr(ni, attr, le, ATTR_NAME, NULL, 0, NULL, mi);
4342306f
KK
1665 if (!attr)
1666 return NULL;
1667
1668 fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
1669 if (fname && name_type == fname->type)
1670 return fname;
1671 }
1672}
1673
1674/*
e8b8e97f
KA
1675 * ni_new_attr_flags
1676 *
1677 * Process compressed/sparsed in special way.
1678 * NOTE: You need to set ni->std_fa = new_fa
1679 * after this function to keep internal structures in consistency.
4342306f
KK
1680 */
1681int ni_new_attr_flags(struct ntfs_inode *ni, enum FILE_ATTRIBUTE new_fa)
1682{
1683 struct ATTRIB *attr;
1684 struct mft_inode *mi;
1685 __le16 new_aflags;
1686 u32 new_asize;
1687
1688 attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi);
1689 if (!attr)
1690 return -EINVAL;
1691
1692 new_aflags = attr->flags;
1693
1694 if (new_fa & FILE_ATTRIBUTE_SPARSE_FILE)
1695 new_aflags |= ATTR_FLAG_SPARSED;
1696 else
1697 new_aflags &= ~ATTR_FLAG_SPARSED;
1698
1699 if (new_fa & FILE_ATTRIBUTE_COMPRESSED)
1700 new_aflags |= ATTR_FLAG_COMPRESSED;
1701 else
1702 new_aflags &= ~ATTR_FLAG_COMPRESSED;
1703
1704 if (new_aflags == attr->flags)
1705 return 0;
1706
1707 if ((new_aflags & (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED)) ==
1708 (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED)) {
1709 ntfs_inode_warn(&ni->vfs_inode,
1710 "file can't be sparsed and compressed");
1711 return -EOPNOTSUPP;
1712 }
1713
1714 if (!attr->non_res)
1715 goto out;
1716
1717 if (attr->nres.data_size) {
1718 ntfs_inode_warn(
1719 &ni->vfs_inode,
1720 "one can change sparsed/compressed only for empty files");
1721 return -EOPNOTSUPP;
1722 }
1723
e8b8e97f 1724 /* Resize nonresident empty attribute in-place only. */
4342306f
KK
1725 new_asize = (new_aflags & (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED))
1726 ? (SIZEOF_NONRESIDENT_EX + 8)
1727 : (SIZEOF_NONRESIDENT + 8);
1728
1729 if (!mi_resize_attr(mi, attr, new_asize - le32_to_cpu(attr->size)))
1730 return -EOPNOTSUPP;
1731
1732 if (new_aflags & ATTR_FLAG_SPARSED) {
1733 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
e8b8e97f 1734 /* Windows uses 16 clusters per frame but supports one cluster per frame too. */
4342306f
KK
1735 attr->nres.c_unit = 0;
1736 ni->vfs_inode.i_mapping->a_ops = &ntfs_aops;
1737 } else if (new_aflags & ATTR_FLAG_COMPRESSED) {
1738 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
e8b8e97f 1739 /* The only allowed: 16 clusters per frame. */
4342306f
KK
1740 attr->nres.c_unit = NTFS_LZNT_CUNIT;
1741 ni->vfs_inode.i_mapping->a_ops = &ntfs_aops_cmpr;
1742 } else {
1743 attr->name_off = SIZEOF_NONRESIDENT_LE;
e8b8e97f 1744 /* Normal files. */
4342306f
KK
1745 attr->nres.c_unit = 0;
1746 ni->vfs_inode.i_mapping->a_ops = &ntfs_aops;
1747 }
1748 attr->nres.run_off = attr->name_off;
1749out:
1750 attr->flags = new_aflags;
1751 mi->dirty = true;
1752
1753 return 0;
1754}
1755
1756/*
1757 * ni_parse_reparse
1758 *
cd4c76ff 1759 * buffer - memory for reparse buffer header
4342306f
KK
1760 */
1761enum REPARSE_SIGN ni_parse_reparse(struct ntfs_inode *ni, struct ATTRIB *attr,
cd4c76ff 1762 struct REPARSE_DATA_BUFFER *buffer)
4342306f
KK
1763{
1764 const struct REPARSE_DATA_BUFFER *rp = NULL;
1765 u8 bits;
1766 u16 len;
1767 typeof(rp->CompressReparseBuffer) *cmpr;
1768
e8b8e97f 1769 /* Try to estimate reparse point. */
4342306f
KK
1770 if (!attr->non_res) {
1771 rp = resident_data_ex(attr, sizeof(struct REPARSE_DATA_BUFFER));
1772 } else if (le64_to_cpu(attr->nres.data_size) >=
1773 sizeof(struct REPARSE_DATA_BUFFER)) {
1774 struct runs_tree run;
1775
1776 run_init(&run);
1777
1778 if (!attr_load_runs_vcn(ni, ATTR_REPARSE, NULL, 0, &run, 0) &&
1779 !ntfs_read_run_nb(ni->mi.sbi, &run, 0, buffer,
1780 sizeof(struct REPARSE_DATA_BUFFER),
1781 NULL)) {
1782 rp = buffer;
1783 }
1784
1785 run_close(&run);
1786 }
1787
1788 if (!rp)
1789 return REPARSE_NONE;
1790
1791 len = le16_to_cpu(rp->ReparseDataLength);
1792 switch (rp->ReparseTag) {
1793 case (IO_REPARSE_TAG_MICROSOFT | IO_REPARSE_TAG_SYMBOLIC_LINK):
e8b8e97f 1794 break; /* Symbolic link. */
4342306f 1795 case IO_REPARSE_TAG_MOUNT_POINT:
e8b8e97f 1796 break; /* Mount points and junctions. */
4342306f
KK
1797 case IO_REPARSE_TAG_SYMLINK:
1798 break;
1799 case IO_REPARSE_TAG_COMPRESS:
1800 /*
24516d48
KA
1801 * WOF - Windows Overlay Filter - Used to compress files with
1802 * LZX/Xpress.
1803 *
1804 * Unlike native NTFS file compression, the Windows
1805 * Overlay Filter supports only read operations. This means
1806 * that it doesn't need to sector-align each compressed chunk,
1807 * so the compressed data can be packed more tightly together.
1808 * If you open the file for writing, the WOF just decompresses
4342306f
KK
1809 * the entire file, turning it back into a plain file.
1810 *
24516d48
KA
1811 * Ntfs3 driver decompresses the entire file only on write or
1812 * change size requests.
4342306f
KK
1813 */
1814
1815 cmpr = &rp->CompressReparseBuffer;
1816 if (len < sizeof(*cmpr) ||
1817 cmpr->WofVersion != WOF_CURRENT_VERSION ||
1818 cmpr->WofProvider != WOF_PROVIDER_SYSTEM ||
1819 cmpr->ProviderVer != WOF_PROVIDER_CURRENT_VERSION) {
1820 return REPARSE_NONE;
1821 }
1822
1823 switch (cmpr->CompressionFormat) {
1824 case WOF_COMPRESSION_XPRESS4K:
1825 bits = 0xc; // 4k
1826 break;
1827 case WOF_COMPRESSION_XPRESS8K:
1828 bits = 0xd; // 8k
1829 break;
1830 case WOF_COMPRESSION_XPRESS16K:
1831 bits = 0xe; // 16k
1832 break;
1833 case WOF_COMPRESSION_LZX32K:
1834 bits = 0xf; // 32k
1835 break;
1836 default:
1837 bits = 0x10; // 64k
1838 break;
1839 }
1840 ni_set_ext_compress_bits(ni, bits);
1841 return REPARSE_COMPRESSED;
1842
1843 case IO_REPARSE_TAG_DEDUP:
1844 ni->ni_flags |= NI_FLAG_DEDUPLICATED;
1845 return REPARSE_DEDUPLICATED;
1846
1847 default:
1848 if (rp->ReparseTag & IO_REPARSE_TAG_NAME_SURROGATE)
1849 break;
1850
1851 return REPARSE_NONE;
1852 }
1853
cd4c76ff
KK
1854 if (buffer != rp)
1855 memcpy(buffer, rp, sizeof(struct REPARSE_DATA_BUFFER));
1856
e8b8e97f 1857 /* Looks like normal symlink. */
4342306f
KK
1858 return REPARSE_LINK;
1859}
1860
1861/*
e8b8e97f
KA
1862 * ni_fiemap - Helper for file_fiemap().
1863 *
1864 * Assumed ni_lock.
1865 * TODO: Less aggressive locks.
4342306f
KK
1866 */
1867int ni_fiemap(struct ntfs_inode *ni, struct fiemap_extent_info *fieinfo,
1868 __u64 vbo, __u64 len)
1869{
1870 int err = 0;
1871 struct ntfs_sb_info *sbi = ni->mi.sbi;
1872 u8 cluster_bits = sbi->cluster_bits;
1873 struct runs_tree *run;
1874 struct rw_semaphore *run_lock;
1875 struct ATTRIB *attr;
1876 CLST vcn = vbo >> cluster_bits;
1877 CLST lcn, clen;
1878 u64 valid = ni->i_valid;
1879 u64 lbo, bytes;
1880 u64 end, alloc_size;
1881 size_t idx = -1;
1882 u32 flags;
1883 bool ok;
1884
1885 if (S_ISDIR(ni->vfs_inode.i_mode)) {
1886 run = &ni->dir.alloc_run;
1887 attr = ni_find_attr(ni, NULL, NULL, ATTR_ALLOC, I30_NAME,
1888 ARRAY_SIZE(I30_NAME), NULL, NULL);
1889 run_lock = &ni->dir.run_lock;
1890 } else {
1891 run = &ni->file.run;
1892 attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL,
1893 NULL);
1894 if (!attr) {
1895 err = -EINVAL;
1896 goto out;
1897 }
1898 if (is_attr_compressed(attr)) {
e8b8e97f 1899 /* Unfortunately cp -r incorrectly treats compressed clusters. */
4342306f
KK
1900 err = -EOPNOTSUPP;
1901 ntfs_inode_warn(
1902 &ni->vfs_inode,
1903 "fiemap is not supported for compressed file (cp -r)");
1904 goto out;
1905 }
1906 run_lock = &ni->file.run_lock;
1907 }
1908
1909 if (!attr || !attr->non_res) {
1910 err = fiemap_fill_next_extent(
1911 fieinfo, 0, 0,
1912 attr ? le32_to_cpu(attr->res.data_size) : 0,
1913 FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_LAST |
1914 FIEMAP_EXTENT_MERGED);
1915 goto out;
1916 }
1917
1918 end = vbo + len;
1919 alloc_size = le64_to_cpu(attr->nres.alloc_size);
1920 if (end > alloc_size)
1921 end = alloc_size;
1922
1923 down_read(run_lock);
1924
1925 while (vbo < end) {
1926 if (idx == -1) {
1927 ok = run_lookup_entry(run, vcn, &lcn, &clen, &idx);
1928 } else {
1929 CLST vcn_next = vcn;
1930
1931 ok = run_get_entry(run, ++idx, &vcn, &lcn, &clen) &&
1932 vcn == vcn_next;
1933 if (!ok)
1934 vcn = vcn_next;
1935 }
1936
1937 if (!ok) {
1938 up_read(run_lock);
1939 down_write(run_lock);
1940
1941 err = attr_load_runs_vcn(ni, attr->type,
1942 attr_name(attr),
1943 attr->name_len, run, vcn);
1944
1945 up_write(run_lock);
1946 down_read(run_lock);
1947
1948 if (err)
1949 break;
1950
1951 ok = run_lookup_entry(run, vcn, &lcn, &clen, &idx);
1952
1953 if (!ok) {
1954 err = -EINVAL;
1955 break;
1956 }
1957 }
1958
1959 if (!clen) {
1960 err = -EINVAL; // ?
1961 break;
1962 }
1963
1964 if (lcn == SPARSE_LCN) {
1965 vcn += clen;
1966 vbo = (u64)vcn << cluster_bits;
1967 continue;
1968 }
1969
1970 flags = FIEMAP_EXTENT_MERGED;
1971 if (S_ISDIR(ni->vfs_inode.i_mode)) {
1972 ;
1973 } else if (is_attr_compressed(attr)) {
1974 CLST clst_data;
1975
1976 err = attr_is_frame_compressed(
1977 ni, attr, vcn >> attr->nres.c_unit, &clst_data);
1978 if (err)
1979 break;
1980 if (clst_data < NTFS_LZNT_CLUSTERS)
1981 flags |= FIEMAP_EXTENT_ENCODED;
1982 } else if (is_attr_encrypted(attr)) {
1983 flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1984 }
1985
1986 vbo = (u64)vcn << cluster_bits;
1987 bytes = (u64)clen << cluster_bits;
1988 lbo = (u64)lcn << cluster_bits;
1989
1990 vcn += clen;
1991
3880f2b8 1992 if (vbo + bytes >= end)
4342306f 1993 bytes = end - vbo;
4342306f
KK
1994
1995 if (vbo + bytes <= valid) {
1996 ;
1997 } else if (vbo >= valid) {
1998 flags |= FIEMAP_EXTENT_UNWRITTEN;
1999 } else {
2000 /* vbo < valid && valid < vbo + bytes */
2001 u64 dlen = valid - vbo;
2002
3880f2b8
KK
2003 if (vbo + dlen >= end)
2004 flags |= FIEMAP_EXTENT_LAST;
2005
4342306f
KK
2006 err = fiemap_fill_next_extent(fieinfo, vbo, lbo, dlen,
2007 flags);
2008 if (err < 0)
2009 break;
2010 if (err == 1) {
2011 err = 0;
2012 break;
2013 }
2014
2015 vbo = valid;
2016 bytes -= dlen;
2017 if (!bytes)
2018 continue;
2019
2020 lbo += dlen;
2021 flags |= FIEMAP_EXTENT_UNWRITTEN;
2022 }
2023
3880f2b8
KK
2024 if (vbo + bytes >= end)
2025 flags |= FIEMAP_EXTENT_LAST;
2026
4342306f
KK
2027 err = fiemap_fill_next_extent(fieinfo, vbo, lbo, bytes, flags);
2028 if (err < 0)
2029 break;
2030 if (err == 1) {
2031 err = 0;
2032 break;
2033 }
2034
2035 vbo += bytes;
2036 }
2037
2038 up_read(run_lock);
2039
2040out:
2041 return err;
2042}
2043
2044/*
e8b8e97f
KA
2045 * ni_readpage_cmpr
2046 *
4342306f
KK
2047 * When decompressing, we typically obtain more than one page per reference.
2048 * We inject the additional pages into the page cache.
2049 */
2050int ni_readpage_cmpr(struct ntfs_inode *ni, struct page *page)
2051{
2052 int err;
2053 struct ntfs_sb_info *sbi = ni->mi.sbi;
2054 struct address_space *mapping = page->mapping;
2055 pgoff_t index = page->index;
2056 u64 frame_vbo, vbo = (u64)index << PAGE_SHIFT;
e8b8e97f 2057 struct page **pages = NULL; /* Array of at most 16 pages. stack? */
4342306f
KK
2058 u8 frame_bits;
2059 CLST frame;
2060 u32 i, idx, frame_size, pages_per_frame;
2061 gfp_t gfp_mask;
2062 struct page *pg;
2063
2064 if (vbo >= ni->vfs_inode.i_size) {
2065 SetPageUptodate(page);
2066 err = 0;
2067 goto out;
2068 }
2069
2070 if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
e8b8e97f 2071 /* Xpress or LZX. */
4342306f
KK
2072 frame_bits = ni_ext_compress_bits(ni);
2073 } else {
e8b8e97f 2074 /* LZNT compression. */
4342306f
KK
2075 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits;
2076 }
2077 frame_size = 1u << frame_bits;
2078 frame = vbo >> frame_bits;
2079 frame_vbo = (u64)frame << frame_bits;
2080 idx = (vbo - frame_vbo) >> PAGE_SHIFT;
2081
2082 pages_per_frame = frame_size >> PAGE_SHIFT;
345482bc 2083 pages = kcalloc(pages_per_frame, sizeof(struct page *), GFP_NOFS);
4342306f
KK
2084 if (!pages) {
2085 err = -ENOMEM;
2086 goto out;
2087 }
2088
2089 pages[idx] = page;
2090 index = frame_vbo >> PAGE_SHIFT;
2091 gfp_mask = mapping_gfp_mask(mapping);
2092
2093 for (i = 0; i < pages_per_frame; i++, index++) {
2094 if (i == idx)
2095 continue;
2096
2097 pg = find_or_create_page(mapping, index, gfp_mask);
2098 if (!pg) {
2099 err = -ENOMEM;
2100 goto out1;
2101 }
2102 pages[i] = pg;
2103 }
2104
2105 err = ni_read_frame(ni, frame_vbo, pages, pages_per_frame);
2106
2107out1:
2108 if (err)
2109 SetPageError(page);
2110
2111 for (i = 0; i < pages_per_frame; i++) {
2112 pg = pages[i];
2113 if (i == idx)
2114 continue;
2115 unlock_page(pg);
2116 put_page(pg);
2117 }
2118
2119out:
e8b8e97f 2120 /* At this point, err contains 0 or -EIO depending on the "critical" page. */
195c52bd 2121 kfree(pages);
4342306f
KK
2122 unlock_page(page);
2123
2124 return err;
2125}
2126
2127#ifdef CONFIG_NTFS3_LZX_XPRESS
2128/*
e8b8e97f
KA
2129 * ni_decompress_file - Decompress LZX/Xpress compressed file.
2130 *
2131 * Remove ATTR_DATA::WofCompressedData.
2132 * Remove ATTR_REPARSE.
4342306f
KK
2133 */
2134int ni_decompress_file(struct ntfs_inode *ni)
2135{
2136 struct ntfs_sb_info *sbi = ni->mi.sbi;
2137 struct inode *inode = &ni->vfs_inode;
2138 loff_t i_size = inode->i_size;
2139 struct address_space *mapping = inode->i_mapping;
2140 gfp_t gfp_mask = mapping_gfp_mask(mapping);
2141 struct page **pages = NULL;
2142 struct ATTR_LIST_ENTRY *le;
2143 struct ATTRIB *attr;
2144 CLST vcn, cend, lcn, clen, end;
2145 pgoff_t index;
2146 u64 vbo;
2147 u8 frame_bits;
2148 u32 i, frame_size, pages_per_frame, bytes;
2149 struct mft_inode *mi;
2150 int err;
2151
e8b8e97f 2152 /* Clusters for decompressed data. */
4342306f
KK
2153 cend = bytes_to_cluster(sbi, i_size);
2154
2155 if (!i_size)
2156 goto remove_wof;
2157
e8b8e97f 2158 /* Check in advance. */
4342306f
KK
2159 if (cend > wnd_zeroes(&sbi->used.bitmap)) {
2160 err = -ENOSPC;
2161 goto out;
2162 }
2163
2164 frame_bits = ni_ext_compress_bits(ni);
2165 frame_size = 1u << frame_bits;
2166 pages_per_frame = frame_size >> PAGE_SHIFT;
345482bc 2167 pages = kcalloc(pages_per_frame, sizeof(struct page *), GFP_NOFS);
4342306f
KK
2168 if (!pages) {
2169 err = -ENOMEM;
2170 goto out;
2171 }
2172
2173 /*
e8b8e97f 2174 * Step 1: Decompress data and copy to new allocated clusters.
4342306f
KK
2175 */
2176 index = 0;
2177 for (vbo = 0; vbo < i_size; vbo += bytes) {
2178 u32 nr_pages;
2179 bool new;
2180
2181 if (vbo + frame_size > i_size) {
2182 bytes = i_size - vbo;
2183 nr_pages = (bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
2184 } else {
2185 nr_pages = pages_per_frame;
2186 bytes = frame_size;
2187 }
2188
2189 end = bytes_to_cluster(sbi, vbo + bytes);
2190
2191 for (vcn = vbo >> sbi->cluster_bits; vcn < end; vcn += clen) {
2192 err = attr_data_get_block(ni, vcn, cend - vcn, &lcn,
2193 &clen, &new);
2194 if (err)
2195 goto out;
2196 }
2197
2198 for (i = 0; i < pages_per_frame; i++, index++) {
2199 struct page *pg;
2200
2201 pg = find_or_create_page(mapping, index, gfp_mask);
2202 if (!pg) {
2203 while (i--) {
2204 unlock_page(pages[i]);
2205 put_page(pages[i]);
2206 }
2207 err = -ENOMEM;
2208 goto out;
2209 }
2210 pages[i] = pg;
2211 }
2212
2213 err = ni_read_frame(ni, vbo, pages, pages_per_frame);
2214
2215 if (!err) {
2216 down_read(&ni->file.run_lock);
2217 err = ntfs_bio_pages(sbi, &ni->file.run, pages,
2218 nr_pages, vbo, bytes,
2219 REQ_OP_WRITE);
2220 up_read(&ni->file.run_lock);
2221 }
2222
2223 for (i = 0; i < pages_per_frame; i++) {
2224 unlock_page(pages[i]);
2225 put_page(pages[i]);
2226 }
2227
2228 if (err)
2229 goto out;
2230
2231 cond_resched();
2232 }
2233
2234remove_wof:
2235 /*
e8b8e97f
KA
2236 * Step 2: Deallocate attributes ATTR_DATA::WofCompressedData
2237 * and ATTR_REPARSE.
4342306f
KK
2238 */
2239 attr = NULL;
2240 le = NULL;
2241 while ((attr = ni_enum_attr_ex(ni, attr, &le, NULL))) {
2242 CLST svcn, evcn;
2243 u32 asize, roff;
2244
2245 if (attr->type == ATTR_REPARSE) {
2246 struct MFT_REF ref;
2247
2248 mi_get_ref(&ni->mi, &ref);
2249 ntfs_remove_reparse(sbi, 0, &ref);
2250 }
2251
2252 if (!attr->non_res)
2253 continue;
2254
2255 if (attr->type != ATTR_REPARSE &&
2256 (attr->type != ATTR_DATA ||
2257 attr->name_len != ARRAY_SIZE(WOF_NAME) ||
2258 memcmp(attr_name(attr), WOF_NAME, sizeof(WOF_NAME))))
2259 continue;
2260
2261 svcn = le64_to_cpu(attr->nres.svcn);
2262 evcn = le64_to_cpu(attr->nres.evcn);
2263
2264 if (evcn + 1 <= svcn)
2265 continue;
2266
2267 asize = le32_to_cpu(attr->size);
2268 roff = le16_to_cpu(attr->nres.run_off);
2269
e8b8e97f 2270 /*run==1 Means unpack and deallocate. */
4342306f
KK
2271 run_unpack_ex(RUN_DEALLOCATE, sbi, ni->mi.rno, svcn, evcn, svcn,
2272 Add2Ptr(attr, roff), asize - roff);
2273 }
2274
2275 /*
e8b8e97f 2276 * Step 3: Remove attribute ATTR_DATA::WofCompressedData.
4342306f
KK
2277 */
2278 err = ni_remove_attr(ni, ATTR_DATA, WOF_NAME, ARRAY_SIZE(WOF_NAME),
2279 false, NULL);
2280 if (err)
2281 goto out;
2282
2283 /*
e8b8e97f 2284 * Step 4: Remove ATTR_REPARSE.
4342306f
KK
2285 */
2286 err = ni_remove_attr(ni, ATTR_REPARSE, NULL, 0, false, NULL);
2287 if (err)
2288 goto out;
2289
2290 /*
e8b8e97f 2291 * Step 5: Remove sparse flag from data attribute.
4342306f
KK
2292 */
2293 attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi);
2294 if (!attr) {
2295 err = -EINVAL;
2296 goto out;
2297 }
2298
2299 if (attr->non_res && is_attr_sparsed(attr)) {
d3624466 2300 /* Sparsed attribute header is 8 bytes bigger than normal. */
4342306f
KK
2301 struct MFT_REC *rec = mi->mrec;
2302 u32 used = le32_to_cpu(rec->used);
2303 u32 asize = le32_to_cpu(attr->size);
2304 u16 roff = le16_to_cpu(attr->nres.run_off);
2305 char *rbuf = Add2Ptr(attr, roff);
2306
2307 memmove(rbuf - 8, rbuf, used - PtrOffset(rec, rbuf));
2308 attr->size = cpu_to_le32(asize - 8);
2309 attr->flags &= ~ATTR_FLAG_SPARSED;
2310 attr->nres.run_off = cpu_to_le16(roff - 8);
2311 attr->nres.c_unit = 0;
2312 rec->used = cpu_to_le32(used - 8);
2313 mi->dirty = true;
2314 ni->std_fa &= ~(FILE_ATTRIBUTE_SPARSE_FILE |
2315 FILE_ATTRIBUTE_REPARSE_POINT);
2316
2317 mark_inode_dirty(inode);
2318 }
2319
e8b8e97f 2320 /* Clear cached flag. */
4342306f
KK
2321 ni->ni_flags &= ~NI_FLAG_COMPRESSED_MASK;
2322 if (ni->file.offs_page) {
2323 put_page(ni->file.offs_page);
2324 ni->file.offs_page = NULL;
2325 }
2326 mapping->a_ops = &ntfs_aops;
2327
2328out:
195c52bd 2329 kfree(pages);
4342306f
KK
2330 if (err) {
2331 make_bad_inode(inode);
2332 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
2333 }
2334
2335 return err;
2336}
2337
e8b8e97f
KA
2338/*
2339 * decompress_lzx_xpress - External compression LZX/Xpress.
2340 */
4342306f
KK
2341static int decompress_lzx_xpress(struct ntfs_sb_info *sbi, const char *cmpr,
2342 size_t cmpr_size, void *unc, size_t unc_size,
2343 u32 frame_size)
2344{
2345 int err;
2346 void *ctx;
2347
2348 if (cmpr_size == unc_size) {
e8b8e97f 2349 /* Frame not compressed. */
4342306f
KK
2350 memcpy(unc, cmpr, unc_size);
2351 return 0;
2352 }
2353
2354 err = 0;
2355 if (frame_size == 0x8000) {
2356 mutex_lock(&sbi->compress.mtx_lzx);
e8b8e97f 2357 /* LZX: Frame compressed. */
4342306f
KK
2358 ctx = sbi->compress.lzx;
2359 if (!ctx) {
e8b8e97f 2360 /* Lazy initialize LZX decompress context. */
4342306f
KK
2361 ctx = lzx_allocate_decompressor();
2362 if (!ctx) {
2363 err = -ENOMEM;
2364 goto out1;
2365 }
2366
2367 sbi->compress.lzx = ctx;
2368 }
2369
2370 if (lzx_decompress(ctx, cmpr, cmpr_size, unc, unc_size)) {
e8b8e97f 2371 /* Treat all errors as "invalid argument". */
4342306f
KK
2372 err = -EINVAL;
2373 }
2374out1:
2375 mutex_unlock(&sbi->compress.mtx_lzx);
2376 } else {
e8b8e97f 2377 /* XPRESS: Frame compressed. */
4342306f
KK
2378 mutex_lock(&sbi->compress.mtx_xpress);
2379 ctx = sbi->compress.xpress;
2380 if (!ctx) {
d3624466 2381 /* Lazy initialize Xpress decompress context. */
4342306f
KK
2382 ctx = xpress_allocate_decompressor();
2383 if (!ctx) {
2384 err = -ENOMEM;
2385 goto out2;
2386 }
2387
2388 sbi->compress.xpress = ctx;
2389 }
2390
2391 if (xpress_decompress(ctx, cmpr, cmpr_size, unc, unc_size)) {
e8b8e97f 2392 /* Treat all errors as "invalid argument". */
4342306f
KK
2393 err = -EINVAL;
2394 }
2395out2:
2396 mutex_unlock(&sbi->compress.mtx_xpress);
2397 }
2398 return err;
2399}
2400#endif
2401
2402/*
2403 * ni_read_frame
2404 *
d3624466 2405 * Pages - Array of locked pages.
4342306f
KK
2406 */
2407int ni_read_frame(struct ntfs_inode *ni, u64 frame_vbo, struct page **pages,
2408 u32 pages_per_frame)
2409{
2410 int err;
2411 struct ntfs_sb_info *sbi = ni->mi.sbi;
2412 u8 cluster_bits = sbi->cluster_bits;
2413 char *frame_ondisk = NULL;
2414 char *frame_mem = NULL;
2415 struct page **pages_disk = NULL;
2416 struct ATTR_LIST_ENTRY *le = NULL;
2417 struct runs_tree *run = &ni->file.run;
2418 u64 valid_size = ni->i_valid;
2419 u64 vbo_disk;
2420 size_t unc_size;
2421 u32 frame_size, i, npages_disk, ondisk_size;
2422 struct page *pg;
2423 struct ATTRIB *attr;
2424 CLST frame, clst_data;
2425
2426 /*
e8b8e97f
KA
2427 * To simplify decompress algorithm do vmap for source
2428 * and target pages.
4342306f
KK
2429 */
2430 for (i = 0; i < pages_per_frame; i++)
2431 kmap(pages[i]);
2432
2433 frame_size = pages_per_frame << PAGE_SHIFT;
2434 frame_mem = vmap(pages, pages_per_frame, VM_MAP, PAGE_KERNEL);
2435 if (!frame_mem) {
2436 err = -ENOMEM;
2437 goto out;
2438 }
2439
2440 attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL, NULL);
2441 if (!attr) {
2442 err = -ENOENT;
2443 goto out1;
2444 }
2445
2446 if (!attr->non_res) {
2447 u32 data_size = le32_to_cpu(attr->res.data_size);
2448
2449 memset(frame_mem, 0, frame_size);
2450 if (frame_vbo < data_size) {
2451 ondisk_size = data_size - frame_vbo;
2452 memcpy(frame_mem, resident_data(attr) + frame_vbo,
2453 min(ondisk_size, frame_size));
2454 }
2455 err = 0;
2456 goto out1;
2457 }
2458
2459 if (frame_vbo >= valid_size) {
2460 memset(frame_mem, 0, frame_size);
2461 err = 0;
2462 goto out1;
2463 }
2464
2465 if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
2466#ifndef CONFIG_NTFS3_LZX_XPRESS
2467 err = -EOPNOTSUPP;
2468 goto out1;
2469#else
2470 u32 frame_bits = ni_ext_compress_bits(ni);
2471 u64 frame64 = frame_vbo >> frame_bits;
2472 u64 frames, vbo_data;
2473
2474 if (frame_size != (1u << frame_bits)) {
2475 err = -EINVAL;
2476 goto out1;
2477 }
2478 switch (frame_size) {
2479 case 0x1000:
2480 case 0x2000:
2481 case 0x4000:
2482 case 0x8000:
2483 break;
2484 default:
e8b8e97f 2485 /* Unknown compression. */
4342306f
KK
2486 err = -EOPNOTSUPP;
2487 goto out1;
2488 }
2489
2490 attr = ni_find_attr(ni, attr, &le, ATTR_DATA, WOF_NAME,
2491 ARRAY_SIZE(WOF_NAME), NULL, NULL);
2492 if (!attr) {
2493 ntfs_inode_err(
2494 &ni->vfs_inode,
2495 "external compressed file should contains data attribute \"WofCompressedData\"");
2496 err = -EINVAL;
2497 goto out1;
2498 }
2499
2500 if (!attr->non_res) {
2501 run = NULL;
2502 } else {
2503 run = run_alloc();
2504 if (!run) {
2505 err = -ENOMEM;
2506 goto out1;
2507 }
2508 }
2509
2510 frames = (ni->vfs_inode.i_size - 1) >> frame_bits;
2511
2512 err = attr_wof_frame_info(ni, attr, run, frame64, frames,
2513 frame_bits, &ondisk_size, &vbo_data);
2514 if (err)
2515 goto out2;
2516
2517 if (frame64 == frames) {
2518 unc_size = 1 + ((ni->vfs_inode.i_size - 1) &
2519 (frame_size - 1));
2520 ondisk_size = attr_size(attr) - vbo_data;
2521 } else {
2522 unc_size = frame_size;
2523 }
2524
2525 if (ondisk_size > frame_size) {
2526 err = -EINVAL;
2527 goto out2;
2528 }
2529
2530 if (!attr->non_res) {
2531 if (vbo_data + ondisk_size >
2532 le32_to_cpu(attr->res.data_size)) {
2533 err = -EINVAL;
2534 goto out1;
2535 }
2536
2537 err = decompress_lzx_xpress(
2538 sbi, Add2Ptr(resident_data(attr), vbo_data),
2539 ondisk_size, frame_mem, unc_size, frame_size);
2540 goto out1;
2541 }
2542 vbo_disk = vbo_data;
e8b8e97f 2543 /* Load all runs to read [vbo_disk-vbo_to). */
4342306f
KK
2544 err = attr_load_runs_range(ni, ATTR_DATA, WOF_NAME,
2545 ARRAY_SIZE(WOF_NAME), run, vbo_disk,
2546 vbo_data + ondisk_size);
2547 if (err)
2548 goto out2;
2549 npages_disk = (ondisk_size + (vbo_disk & (PAGE_SIZE - 1)) +
2550 PAGE_SIZE - 1) >>
2551 PAGE_SHIFT;
2552#endif
2553 } else if (is_attr_compressed(attr)) {
e8b8e97f 2554 /* LZNT compression. */
4342306f
KK
2555 if (sbi->cluster_size > NTFS_LZNT_MAX_CLUSTER) {
2556 err = -EOPNOTSUPP;
2557 goto out1;
2558 }
2559
2560 if (attr->nres.c_unit != NTFS_LZNT_CUNIT) {
2561 err = -EOPNOTSUPP;
2562 goto out1;
2563 }
2564
2565 down_write(&ni->file.run_lock);
2566 run_truncate_around(run, le64_to_cpu(attr->nres.svcn));
2567 frame = frame_vbo >> (cluster_bits + NTFS_LZNT_CUNIT);
2568 err = attr_is_frame_compressed(ni, attr, frame, &clst_data);
2569 up_write(&ni->file.run_lock);
2570 if (err)
2571 goto out1;
2572
2573 if (!clst_data) {
2574 memset(frame_mem, 0, frame_size);
2575 goto out1;
2576 }
2577
2578 frame_size = sbi->cluster_size << NTFS_LZNT_CUNIT;
2579 ondisk_size = clst_data << cluster_bits;
2580
2581 if (clst_data >= NTFS_LZNT_CLUSTERS) {
e8b8e97f 2582 /* Frame is not compressed. */
4342306f
KK
2583 down_read(&ni->file.run_lock);
2584 err = ntfs_bio_pages(sbi, run, pages, pages_per_frame,
2585 frame_vbo, ondisk_size,
2586 REQ_OP_READ);
2587 up_read(&ni->file.run_lock);
2588 goto out1;
2589 }
2590 vbo_disk = frame_vbo;
2591 npages_disk = (ondisk_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
2592 } else {
2593 __builtin_unreachable();
2594 err = -EINVAL;
2595 goto out1;
2596 }
2597
195c52bd 2598 pages_disk = kzalloc(npages_disk * sizeof(struct page *), GFP_NOFS);
4342306f
KK
2599 if (!pages_disk) {
2600 err = -ENOMEM;
2601 goto out2;
2602 }
2603
2604 for (i = 0; i < npages_disk; i++) {
2605 pg = alloc_page(GFP_KERNEL);
2606 if (!pg) {
2607 err = -ENOMEM;
2608 goto out3;
2609 }
2610 pages_disk[i] = pg;
2611 lock_page(pg);
2612 kmap(pg);
2613 }
2614
e8b8e97f 2615 /* Read 'ondisk_size' bytes from disk. */
4342306f
KK
2616 down_read(&ni->file.run_lock);
2617 err = ntfs_bio_pages(sbi, run, pages_disk, npages_disk, vbo_disk,
2618 ondisk_size, REQ_OP_READ);
2619 up_read(&ni->file.run_lock);
2620 if (err)
2621 goto out3;
2622
2623 /*
e8b8e97f 2624 * To simplify decompress algorithm do vmap for source and target pages.
4342306f
KK
2625 */
2626 frame_ondisk = vmap(pages_disk, npages_disk, VM_MAP, PAGE_KERNEL_RO);
2627 if (!frame_ondisk) {
2628 err = -ENOMEM;
2629 goto out3;
2630 }
2631
e8b8e97f 2632 /* Decompress: Frame_ondisk -> frame_mem. */
4342306f
KK
2633#ifdef CONFIG_NTFS3_LZX_XPRESS
2634 if (run != &ni->file.run) {
2635 /* LZX or XPRESS */
2636 err = decompress_lzx_xpress(
2637 sbi, frame_ondisk + (vbo_disk & (PAGE_SIZE - 1)),
2638 ondisk_size, frame_mem, unc_size, frame_size);
2639 } else
2640#endif
2641 {
e8b8e97f 2642 /* LZNT - Native NTFS compression. */
4342306f
KK
2643 unc_size = decompress_lznt(frame_ondisk, ondisk_size, frame_mem,
2644 frame_size);
2645 if ((ssize_t)unc_size < 0)
2646 err = unc_size;
2647 else if (!unc_size || unc_size > frame_size)
2648 err = -EINVAL;
2649 }
2650 if (!err && valid_size < frame_vbo + frame_size) {
2651 size_t ok = valid_size - frame_vbo;
2652
2653 memset(frame_mem + ok, 0, frame_size - ok);
2654 }
2655
2656 vunmap(frame_ondisk);
2657
2658out3:
2659 for (i = 0; i < npages_disk; i++) {
2660 pg = pages_disk[i];
2661 if (pg) {
2662 kunmap(pg);
2663 unlock_page(pg);
2664 put_page(pg);
2665 }
2666 }
195c52bd 2667 kfree(pages_disk);
4342306f
KK
2668
2669out2:
2670#ifdef CONFIG_NTFS3_LZX_XPRESS
2671 if (run != &ni->file.run)
2672 run_free(run);
2673#endif
2674out1:
2675 vunmap(frame_mem);
2676out:
2677 for (i = 0; i < pages_per_frame; i++) {
2678 pg = pages[i];
2679 kunmap(pg);
2680 ClearPageError(pg);
2681 SetPageUptodate(pg);
2682 }
2683
2684 return err;
2685}
2686
2687/*
2688 * ni_write_frame
2689 *
e8b8e97f 2690 * Pages - Array of locked pages.
4342306f
KK
2691 */
2692int ni_write_frame(struct ntfs_inode *ni, struct page **pages,
2693 u32 pages_per_frame)
2694{
2695 int err;
2696 struct ntfs_sb_info *sbi = ni->mi.sbi;
2697 u8 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits;
2698 u32 frame_size = sbi->cluster_size << NTFS_LZNT_CUNIT;
2699 u64 frame_vbo = (u64)pages[0]->index << PAGE_SHIFT;
2700 CLST frame = frame_vbo >> frame_bits;
2701 char *frame_ondisk = NULL;
2702 struct page **pages_disk = NULL;
2703 struct ATTR_LIST_ENTRY *le = NULL;
2704 char *frame_mem;
2705 struct ATTRIB *attr;
2706 struct mft_inode *mi;
2707 u32 i;
2708 struct page *pg;
2709 size_t compr_size, ondisk_size;
2710 struct lznt *lznt;
2711
2712 attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL, &mi);
2713 if (!attr) {
2714 err = -ENOENT;
2715 goto out;
2716 }
2717
2718 if (WARN_ON(!is_attr_compressed(attr))) {
2719 err = -EINVAL;
2720 goto out;
2721 }
2722
2723 if (sbi->cluster_size > NTFS_LZNT_MAX_CLUSTER) {
2724 err = -EOPNOTSUPP;
2725 goto out;
2726 }
2727
2728 if (!attr->non_res) {
2729 down_write(&ni->file.run_lock);
2730 err = attr_make_nonresident(ni, attr, le, mi,
2731 le32_to_cpu(attr->res.data_size),
2732 &ni->file.run, &attr, pages[0]);
2733 up_write(&ni->file.run_lock);
2734 if (err)
2735 goto out;
2736 }
2737
2738 if (attr->nres.c_unit != NTFS_LZNT_CUNIT) {
2739 err = -EOPNOTSUPP;
2740 goto out;
2741 }
2742
345482bc 2743 pages_disk = kcalloc(pages_per_frame, sizeof(struct page *), GFP_NOFS);
4342306f
KK
2744 if (!pages_disk) {
2745 err = -ENOMEM;
2746 goto out;
2747 }
2748
2749 for (i = 0; i < pages_per_frame; i++) {
2750 pg = alloc_page(GFP_KERNEL);
2751 if (!pg) {
2752 err = -ENOMEM;
2753 goto out1;
2754 }
2755 pages_disk[i] = pg;
2756 lock_page(pg);
2757 kmap(pg);
2758 }
2759
e8b8e97f 2760 /* To simplify compress algorithm do vmap for source and target pages. */
4342306f
KK
2761 frame_ondisk = vmap(pages_disk, pages_per_frame, VM_MAP, PAGE_KERNEL);
2762 if (!frame_ondisk) {
2763 err = -ENOMEM;
2764 goto out1;
2765 }
2766
2767 for (i = 0; i < pages_per_frame; i++)
2768 kmap(pages[i]);
2769
e8b8e97f 2770 /* Map in-memory frame for read-only. */
4342306f
KK
2771 frame_mem = vmap(pages, pages_per_frame, VM_MAP, PAGE_KERNEL_RO);
2772 if (!frame_mem) {
2773 err = -ENOMEM;
2774 goto out2;
2775 }
2776
2777 mutex_lock(&sbi->compress.mtx_lznt);
2778 lznt = NULL;
2779 if (!sbi->compress.lznt) {
2780 /*
e8b8e97f
KA
2781 * LZNT implements two levels of compression:
2782 * 0 - Standard compression
2783 * 1 - Best compression, requires a lot of cpu
4342306f
KK
2784 * use mount option?
2785 */
2786 lznt = get_lznt_ctx(0);
2787 if (!lznt) {
2788 mutex_unlock(&sbi->compress.mtx_lznt);
2789 err = -ENOMEM;
2790 goto out3;
2791 }
2792
2793 sbi->compress.lznt = lznt;
2794 lznt = NULL;
2795 }
2796
d3624466 2797 /* Compress: frame_mem -> frame_ondisk */
4342306f
KK
2798 compr_size = compress_lznt(frame_mem, frame_size, frame_ondisk,
2799 frame_size, sbi->compress.lznt);
2800 mutex_unlock(&sbi->compress.mtx_lznt);
195c52bd 2801 kfree(lznt);
4342306f
KK
2802
2803 if (compr_size + sbi->cluster_size > frame_size) {
e8b8e97f 2804 /* Frame is not compressed. */
4342306f
KK
2805 compr_size = frame_size;
2806 ondisk_size = frame_size;
2807 } else if (compr_size) {
e8b8e97f 2808 /* Frame is compressed. */
4342306f
KK
2809 ondisk_size = ntfs_up_cluster(sbi, compr_size);
2810 memset(frame_ondisk + compr_size, 0, ondisk_size - compr_size);
2811 } else {
e8b8e97f 2812 /* Frame is sparsed. */
4342306f
KK
2813 ondisk_size = 0;
2814 }
2815
2816 down_write(&ni->file.run_lock);
2817 run_truncate_around(&ni->file.run, le64_to_cpu(attr->nres.svcn));
2818 err = attr_allocate_frame(ni, frame, compr_size, ni->i_valid);
2819 up_write(&ni->file.run_lock);
2820 if (err)
2821 goto out2;
2822
2823 if (!ondisk_size)
2824 goto out2;
2825
2826 down_read(&ni->file.run_lock);
2827 err = ntfs_bio_pages(sbi, &ni->file.run,
2828 ondisk_size < frame_size ? pages_disk : pages,
2829 pages_per_frame, frame_vbo, ondisk_size,
2830 REQ_OP_WRITE);
2831 up_read(&ni->file.run_lock);
2832
2833out3:
2834 vunmap(frame_mem);
2835
2836out2:
2837 for (i = 0; i < pages_per_frame; i++)
2838 kunmap(pages[i]);
2839
2840 vunmap(frame_ondisk);
2841out1:
2842 for (i = 0; i < pages_per_frame; i++) {
2843 pg = pages_disk[i];
2844 if (pg) {
2845 kunmap(pg);
2846 unlock_page(pg);
2847 put_page(pg);
2848 }
2849 }
195c52bd 2850 kfree(pages_disk);
4342306f
KK
2851out:
2852 return err;
2853}
2854
78ab59fe
KK
2855/*
2856 * ni_remove_name - Removes name 'de' from MFT and from directory.
2857 * 'de2' and 'undo_step' are used to restore MFT/dir, if error occurs.
2858 */
2859int ni_remove_name(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
2860 struct NTFS_DE *de, struct NTFS_DE **de2, int *undo_step)
2861{
2862 int err;
2863 struct ntfs_sb_info *sbi = ni->mi.sbi;
2864 struct ATTR_FILE_NAME *de_name = (struct ATTR_FILE_NAME *)(de + 1);
2865 struct ATTR_FILE_NAME *fname;
2866 struct ATTR_LIST_ENTRY *le;
2867 struct mft_inode *mi;
2868 u16 de_key_size = le16_to_cpu(de->key_size);
2869 u8 name_type;
2870
2871 *undo_step = 0;
2872
2873 /* Find name in record. */
2874 mi_get_ref(&dir_ni->mi, &de_name->home);
2875
2876 fname = ni_fname_name(ni, (struct cpu_str *)&de_name->name_len,
2877 &de_name->home, &mi, &le);
2878 if (!fname)
2879 return -ENOENT;
2880
2881 memcpy(&de_name->dup, &fname->dup, sizeof(struct NTFS_DUP_INFO));
2882 name_type = paired_name(fname->type);
2883
2884 /* Mark ntfs as dirty. It will be cleared at umount. */
2885 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
2886
2887 /* Step 1: Remove name from directory. */
2888 err = indx_delete_entry(&dir_ni->dir, dir_ni, fname, de_key_size, sbi);
2889 if (err)
2890 return err;
2891
2892 /* Step 2: Remove name from MFT. */
2893 ni_remove_attr_le(ni, attr_from_name(fname), mi, le);
2894
2895 *undo_step = 2;
2896
2897 /* Get paired name. */
2898 fname = ni_fname_type(ni, name_type, &mi, &le);
2899 if (fname) {
2900 u16 de2_key_size = fname_full_size(fname);
2901
2902 *de2 = Add2Ptr(de, 1024);
2903 (*de2)->key_size = cpu_to_le16(de2_key_size);
2904
2905 memcpy(*de2 + 1, fname, de2_key_size);
2906
2907 /* Step 3: Remove paired name from directory. */
2908 err = indx_delete_entry(&dir_ni->dir, dir_ni, fname,
2909 de2_key_size, sbi);
2910 if (err)
2911 return err;
2912
2913 /* Step 4: Remove paired name from MFT. */
2914 ni_remove_attr_le(ni, attr_from_name(fname), mi, le);
2915
2916 *undo_step = 4;
2917 }
2918 return 0;
2919}
2920
2921/*
2922 * ni_remove_name_undo - Paired function for ni_remove_name.
2923 *
2924 * Return: True if ok
2925 */
2926bool ni_remove_name_undo(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
2927 struct NTFS_DE *de, struct NTFS_DE *de2, int undo_step)
2928{
2929 struct ntfs_sb_info *sbi = ni->mi.sbi;
2930 struct ATTRIB *attr;
2931 u16 de_key_size = de2 ? le16_to_cpu(de2->key_size) : 0;
2932
2933 switch (undo_step) {
2934 case 4:
2935 if (ni_insert_resident(ni, de_key_size, ATTR_NAME, NULL, 0,
2936 &attr, NULL, NULL)) {
2937 return false;
2938 }
2939 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), de2 + 1, de_key_size);
2940
2941 mi_get_ref(&ni->mi, &de2->ref);
2942 de2->size = cpu_to_le16(ALIGN(de_key_size, 8) +
2943 sizeof(struct NTFS_DE));
2944 de2->flags = 0;
2945 de2->res = 0;
2946
2947 if (indx_insert_entry(&dir_ni->dir, dir_ni, de2, sbi, NULL,
2948 1)) {
2949 return false;
2950 }
2951 fallthrough;
2952
2953 case 2:
2954 de_key_size = le16_to_cpu(de->key_size);
2955
2956 if (ni_insert_resident(ni, de_key_size, ATTR_NAME, NULL, 0,
2957 &attr, NULL, NULL)) {
2958 return false;
2959 }
2960
2961 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), de + 1, de_key_size);
2962 mi_get_ref(&ni->mi, &de->ref);
2963
2829e39e 2964 if (indx_insert_entry(&dir_ni->dir, dir_ni, de, sbi, NULL, 1))
78ab59fe 2965 return false;
78ab59fe
KK
2966 }
2967
2968 return true;
2969}
2970
2971/*
2972 * ni_add_name - Add new name in MFT and in directory.
2973 */
2974int ni_add_name(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
2975 struct NTFS_DE *de)
2976{
2977 int err;
2978 struct ATTRIB *attr;
2979 struct ATTR_LIST_ENTRY *le;
2980 struct mft_inode *mi;
2981 struct ATTR_FILE_NAME *de_name = (struct ATTR_FILE_NAME *)(de + 1);
2982 u16 de_key_size = le16_to_cpu(de->key_size);
2983
2984 mi_get_ref(&ni->mi, &de->ref);
2985 mi_get_ref(&dir_ni->mi, &de_name->home);
2986
2987 /* Insert new name in MFT. */
2988 err = ni_insert_resident(ni, de_key_size, ATTR_NAME, NULL, 0, &attr,
2989 &mi, &le);
2990 if (err)
2991 return err;
2992
2993 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), de_name, de_key_size);
2994
2995 /* Insert new name in directory. */
2996 err = indx_insert_entry(&dir_ni->dir, dir_ni, de, ni->mi.sbi, NULL, 0);
2997 if (err)
2998 ni_remove_attr_le(ni, attr, mi, le);
2999
3000 return err;
3001}
3002
3003/*
3004 * ni_rename - Remove one name and insert new name.
3005 */
3006int ni_rename(struct ntfs_inode *dir_ni, struct ntfs_inode *new_dir_ni,
3007 struct ntfs_inode *ni, struct NTFS_DE *de, struct NTFS_DE *new_de,
3008 bool *is_bad)
3009{
3010 int err;
3011 struct NTFS_DE *de2 = NULL;
3012 int undo = 0;
3013
3014 /*
3015 * There are two possible ways to rename:
3016 * 1) Add new name and remove old name.
3017 * 2) Remove old name and add new name.
3018 *
3019 * In most cases (not all!) adding new name in MFT and in directory can
3020 * allocate additional cluster(s).
3021 * Second way may result to bad inode if we can't add new name
3022 * and then can't restore (add) old name.
3023 */
3024
3025 /*
3026 * Way 1 - Add new + remove old.
3027 */
3028 err = ni_add_name(new_dir_ni, ni, new_de);
3029 if (!err) {
3030 err = ni_remove_name(dir_ni, ni, de, &de2, &undo);
3031 if (err && ni_remove_name(new_dir_ni, ni, new_de, &de2, &undo))
3032 *is_bad = true;
3033 }
3034
3035 /*
3036 * Way 2 - Remove old + add new.
3037 */
3038 /*
3039 * err = ni_remove_name(dir_ni, ni, de, &de2, &undo);
3040 * if (!err) {
3041 * err = ni_add_name(new_dir_ni, ni, new_de);
3042 * if (err && !ni_remove_name_undo(dir_ni, ni, de, de2, undo))
3043 * *is_bad = true;
3044 * }
3045 */
3046
3047 return err;
3048}
3049
3050/*
3051 * ni_is_dirty - Return: True if 'ni' requires ni_write_inode.
3052 */
3053bool ni_is_dirty(struct inode *inode)
3054{
3055 struct ntfs_inode *ni = ntfs_i(inode);
3056 struct rb_node *node;
3057
3058 if (ni->mi.dirty || ni->attr_list.dirty ||
3059 (ni->ni_flags & NI_FLAG_UPDATE_PARENT))
3060 return true;
3061
3062 for (node = rb_first(&ni->mi_tree); node; node = rb_next(node)) {
3063 if (rb_entry(node, struct mft_inode, node)->dirty)
3064 return true;
3065 }
3066
3067 return false;
3068}
3069
4342306f 3070/*
e8b8e97f
KA
3071 * ni_update_parent
3072 *
3073 * Update duplicate info of ATTR_FILE_NAME in MFT and in parent directories.
4342306f
KK
3074 */
3075static bool ni_update_parent(struct ntfs_inode *ni, struct NTFS_DUP_INFO *dup,
3076 int sync)
3077{
3078 struct ATTRIB *attr;
3079 struct mft_inode *mi;
3080 struct ATTR_LIST_ENTRY *le = NULL;
3081 struct ntfs_sb_info *sbi = ni->mi.sbi;
3082 struct super_block *sb = sbi->sb;
3083 bool re_dirty = false;
4342306f
KK
3084
3085 if (ni->mi.mrec->flags & RECORD_FLAG_DIR) {
3086 dup->fa |= FILE_ATTRIBUTE_DIRECTORY;
3087 attr = NULL;
3088 dup->alloc_size = 0;
3089 dup->data_size = 0;
3090 } else {
3091 dup->fa &= ~FILE_ATTRIBUTE_DIRECTORY;
3092
3093 attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL,
3094 &mi);
3095 if (!attr) {
3096 dup->alloc_size = dup->data_size = 0;
3097 } else if (!attr->non_res) {
3098 u32 data_size = le32_to_cpu(attr->res.data_size);
3099
fa3cacf5 3100 dup->alloc_size = cpu_to_le64(ALIGN(data_size, 8));
4342306f
KK
3101 dup->data_size = cpu_to_le64(data_size);
3102 } else {
3103 u64 new_valid = ni->i_valid;
3104 u64 data_size = le64_to_cpu(attr->nres.data_size);
3105 __le64 valid_le;
3106
3107 dup->alloc_size = is_attr_ext(attr)
3108 ? attr->nres.total_size
3109 : attr->nres.alloc_size;
3110 dup->data_size = attr->nres.data_size;
3111
3112 if (new_valid > data_size)
3113 new_valid = data_size;
3114
3115 valid_le = cpu_to_le64(new_valid);
3116 if (valid_le != attr->nres.valid_size) {
3117 attr->nres.valid_size = valid_le;
3118 mi->dirty = true;
3119 }
3120 }
3121 }
3122
e8b8e97f 3123 /* TODO: Fill reparse info. */
4342306f
KK
3124 dup->reparse = 0;
3125 dup->ea_size = 0;
3126
3127 if (ni->ni_flags & NI_FLAG_EA) {
3128 attr = ni_find_attr(ni, attr, &le, ATTR_EA_INFO, NULL, 0, NULL,
3129 NULL);
3130 if (attr) {
3131 const struct EA_INFO *info;
3132
3133 info = resident_data_ex(attr, sizeof(struct EA_INFO));
35afb70d
KK
3134 /* If ATTR_EA_INFO exists 'info' can't be NULL. */
3135 if (info)
3136 dup->ea_size = info->size_pack;
4342306f
KK
3137 }
3138 }
3139
3140 attr = NULL;
3141 le = NULL;
3142
3143 while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL,
3144 &mi))) {
3145 struct inode *dir;
3146 struct ATTR_FILE_NAME *fname;
3147
3148 fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
78ab59fe 3149 if (!fname || !memcmp(&fname->dup, dup, sizeof(fname->dup)))
4342306f
KK
3150 continue;
3151
e8b8e97f 3152 /* ntfs_iget5 may sleep. */
4342306f
KK
3153 dir = ntfs_iget5(sb, &fname->home, NULL);
3154 if (IS_ERR(dir)) {
3155 ntfs_inode_warn(
3156 &ni->vfs_inode,
3157 "failed to open parent directory r=%lx to update",
3158 (long)ino_get(&fname->home));
3159 continue;
3160 }
3161
3162 if (!is_bad_inode(dir)) {
3163 struct ntfs_inode *dir_ni = ntfs_i(dir);
3164
3165 if (!ni_trylock(dir_ni)) {
3166 re_dirty = true;
3167 } else {
3168 indx_update_dup(dir_ni, sbi, fname, dup, sync);
3169 ni_unlock(dir_ni);
78ab59fe
KK
3170 memcpy(&fname->dup, dup, sizeof(fname->dup));
3171 mi->dirty = true;
4342306f
KK
3172 }
3173 }
3174 iput(dir);
3175 }
3176
3177 return re_dirty;
3178}
3179
3180/*
e8b8e97f 3181 * ni_write_inode - Write MFT base record and all subrecords to disk.
4342306f
KK
3182 */
3183int ni_write_inode(struct inode *inode, int sync, const char *hint)
3184{
3185 int err = 0, err2;
3186 struct ntfs_inode *ni = ntfs_i(inode);
3187 struct super_block *sb = inode->i_sb;
3188 struct ntfs_sb_info *sbi = sb->s_fs_info;
3189 bool re_dirty = false;
3190 struct ATTR_STD_INFO *std;
3191 struct rb_node *node, *next;
3192 struct NTFS_DUP_INFO dup;
3193
3194 if (is_bad_inode(inode) || sb_rdonly(sb))
3195 return 0;
3196
3197 if (!ni_trylock(ni)) {
e8b8e97f 3198 /* 'ni' is under modification, skip for now. */
4342306f
KK
3199 mark_inode_dirty_sync(inode);
3200 return 0;
3201 }
3202
3203 if (is_rec_inuse(ni->mi.mrec) &&
3204 !(sbi->flags & NTFS_FLAGS_LOG_REPLAYING) && inode->i_nlink) {
3205 bool modified = false;
3206
e8b8e97f 3207 /* Update times in standard attribute. */
4342306f
KK
3208 std = ni_std(ni);
3209 if (!std) {
3210 err = -EINVAL;
3211 goto out;
3212 }
3213
3214 /* Update the access times if they have changed. */
3215 dup.m_time = kernel2nt(&inode->i_mtime);
3216 if (std->m_time != dup.m_time) {
3217 std->m_time = dup.m_time;
3218 modified = true;
3219 }
3220
3221 dup.c_time = kernel2nt(&inode->i_ctime);
3222 if (std->c_time != dup.c_time) {
3223 std->c_time = dup.c_time;
3224 modified = true;
3225 }
3226
3227 dup.a_time = kernel2nt(&inode->i_atime);
3228 if (std->a_time != dup.a_time) {
3229 std->a_time = dup.a_time;
3230 modified = true;
3231 }
3232
3233 dup.fa = ni->std_fa;
3234 if (std->fa != dup.fa) {
3235 std->fa = dup.fa;
3236 modified = true;
3237 }
3238
3239 if (modified)
3240 ni->mi.dirty = true;
3241
3242 if (!ntfs_is_meta_file(sbi, inode->i_ino) &&
78ab59fe
KK
3243 (modified || (ni->ni_flags & NI_FLAG_UPDATE_PARENT))
3244 /* Avoid __wait_on_freeing_inode(inode). */
3245 && (sb->s_flags & SB_ACTIVE)) {
4342306f 3246 dup.cr_time = std->cr_time;
e8b8e97f 3247 /* Not critical if this function fail. */
4342306f
KK
3248 re_dirty = ni_update_parent(ni, &dup, sync);
3249
3250 if (re_dirty)
3251 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
3252 else
3253 ni->ni_flags &= ~NI_FLAG_UPDATE_PARENT;
3254 }
3255
e8b8e97f 3256 /* Update attribute list. */
4342306f
KK
3257 if (ni->attr_list.size && ni->attr_list.dirty) {
3258 if (inode->i_ino != MFT_REC_MFT || sync) {
3259 err = ni_try_remove_attr_list(ni);
3260 if (err)
3261 goto out;
3262 }
3263
63544672 3264 err = al_update(ni, sync);
4342306f
KK
3265 if (err)
3266 goto out;
3267 }
3268 }
3269
3270 for (node = rb_first(&ni->mi_tree); node; node = next) {
3271 struct mft_inode *mi = rb_entry(node, struct mft_inode, node);
3272 bool is_empty;
3273
3274 next = rb_next(node);
3275
3276 if (!mi->dirty)
3277 continue;
3278
3279 is_empty = !mi_enum_attr(mi, NULL);
3280
3281 if (is_empty)
3282 clear_rec_inuse(mi->mrec);
3283
3284 err2 = mi_write(mi, sync);
3285 if (!err && err2)
3286 err = err2;
3287
3288 if (is_empty) {
071100ea 3289 ntfs_mark_rec_free(sbi, mi->rno, false);
4342306f
KK
3290 rb_erase(node, &ni->mi_tree);
3291 mi_put(mi);
3292 }
3293 }
3294
3295 if (ni->mi.dirty) {
3296 err2 = mi_write(&ni->mi, sync);
3297 if (!err && err2)
3298 err = err2;
3299 }
3300out:
3301 ni_unlock(ni);
3302
3303 if (err) {
3304 ntfs_err(sb, "%s r=%lx failed, %d.", hint, inode->i_ino, err);
3305 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
3306 return err;
3307 }
3308
78ab59fe 3309 if (re_dirty)
4342306f
KK
3310 mark_inode_dirty_sync(inode);
3311
3312 return 0;
3313}