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