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