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