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