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