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