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