Commit | Line | Data |
---|---|---|
4342306f KK |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * | |
4 | * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved. | |
5 | * | |
6 | */ | |
7 | ||
8 | #include <linux/blkdev.h> | |
9 | #include <linux/buffer_head.h> | |
10 | #include <linux/fiemap.h> | |
11 | #include <linux/fs.h> | |
12 | #include <linux/nls.h> | |
13 | #include <linux/vmalloc.h> | |
14 | ||
15 | #include "debug.h" | |
16 | #include "ntfs.h" | |
17 | #include "ntfs_fs.h" | |
18 | #ifdef CONFIG_NTFS3_LZX_XPRESS | |
19 | #include "lib/lib.h" | |
20 | #endif | |
21 | ||
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 | |
51 | * | |
52 | * finds mft_inode by record number | |
53 | */ | |
54 | static struct mft_inode *ni_find_mi(struct ntfs_inode *ni, CLST rno) | |
55 | { | |
56 | return ni_ins_mi(ni, &ni->mi_tree, rno, NULL); | |
57 | } | |
58 | ||
59 | /* | |
60 | * ni_add_mi | |
61 | * | |
62 | * adds new mft_inode into ntfs_inode | |
63 | */ | |
64 | static void ni_add_mi(struct ntfs_inode *ni, struct mft_inode *mi) | |
65 | { | |
66 | ni_ins_mi(ni, &ni->mi_tree, mi->rno, &mi->node); | |
67 | } | |
68 | ||
69 | /* | |
70 | * ni_remove_mi | |
71 | * | |
72 | * removes mft_inode from ntfs_inode | |
73 | */ | |
74 | void ni_remove_mi(struct ntfs_inode *ni, struct mft_inode *mi) | |
75 | { | |
76 | rb_erase(&mi->node, &ni->mi_tree); | |
77 | } | |
78 | ||
79 | /* | |
80 | * ni_std | |
81 | * | |
82 | * returns pointer into std_info from primary record | |
83 | */ | |
84 | struct ATTR_STD_INFO *ni_std(struct ntfs_inode *ni) | |
85 | { | |
86 | const struct ATTRIB *attr; | |
87 | ||
88 | attr = mi_find_attr(&ni->mi, NULL, ATTR_STD, NULL, 0, NULL); | |
89 | return attr ? resident_data_ex(attr, sizeof(struct ATTR_STD_INFO)) | |
90 | : NULL; | |
91 | } | |
92 | ||
93 | /* | |
94 | * ni_std5 | |
95 | * | |
96 | * returns pointer into std_info from primary record | |
97 | */ | |
98 | struct ATTR_STD_INFO5 *ni_std5(struct ntfs_inode *ni) | |
99 | { | |
100 | const struct ATTRIB *attr; | |
101 | ||
102 | attr = mi_find_attr(&ni->mi, NULL, ATTR_STD, NULL, 0, NULL); | |
103 | ||
104 | return attr ? resident_data_ex(attr, sizeof(struct ATTR_STD_INFO5)) | |
105 | : NULL; | |
106 | } | |
107 | ||
108 | /* | |
109 | * ni_clear | |
110 | * | |
111 | * clears resources allocated by ntfs_inode | |
112 | */ | |
113 | void ni_clear(struct ntfs_inode *ni) | |
114 | { | |
115 | struct rb_node *node; | |
116 | ||
117 | if (!ni->vfs_inode.i_nlink && is_rec_inuse(ni->mi.mrec)) | |
118 | ni_delete_all(ni); | |
119 | ||
120 | al_destroy(ni); | |
121 | ||
122 | for (node = rb_first(&ni->mi_tree); node;) { | |
123 | struct rb_node *next = rb_next(node); | |
124 | struct mft_inode *mi = rb_entry(node, struct mft_inode, node); | |
125 | ||
126 | rb_erase(node, &ni->mi_tree); | |
127 | mi_put(mi); | |
128 | node = next; | |
129 | } | |
130 | ||
131 | /* bad inode always has mode == S_IFREG */ | |
132 | if (ni->ni_flags & NI_FLAG_DIR) | |
133 | indx_clear(&ni->dir); | |
134 | else { | |
135 | run_close(&ni->file.run); | |
136 | #ifdef CONFIG_NTFS3_LZX_XPRESS | |
137 | if (ni->file.offs_page) { | |
138 | /* on-demand allocated page for offsets */ | |
139 | put_page(ni->file.offs_page); | |
140 | ni->file.offs_page = NULL; | |
141 | } | |
142 | #endif | |
143 | } | |
144 | ||
145 | mi_clear(&ni->mi); | |
146 | } | |
147 | ||
148 | /* | |
149 | * ni_load_mi_ex | |
150 | * | |
151 | * finds mft_inode by record number. | |
152 | */ | |
153 | int ni_load_mi_ex(struct ntfs_inode *ni, CLST rno, struct mft_inode **mi) | |
154 | { | |
155 | int err; | |
156 | struct mft_inode *r; | |
157 | ||
158 | r = ni_find_mi(ni, rno); | |
159 | if (r) | |
160 | goto out; | |
161 | ||
162 | err = mi_get(ni->mi.sbi, rno, &r); | |
163 | if (err) | |
164 | return err; | |
165 | ||
166 | ni_add_mi(ni, r); | |
167 | ||
168 | out: | |
169 | if (mi) | |
170 | *mi = r; | |
171 | return 0; | |
172 | } | |
173 | ||
174 | /* | |
175 | * ni_load_mi | |
176 | * | |
177 | * load mft_inode corresponded list_entry | |
178 | */ | |
179 | int ni_load_mi(struct ntfs_inode *ni, struct ATTR_LIST_ENTRY *le, | |
180 | struct mft_inode **mi) | |
181 | { | |
182 | CLST rno; | |
183 | ||
184 | if (!le) { | |
185 | *mi = &ni->mi; | |
186 | return 0; | |
187 | } | |
188 | ||
189 | rno = ino_get(&le->ref); | |
190 | if (rno == ni->mi.rno) { | |
191 | *mi = &ni->mi; | |
192 | return 0; | |
193 | } | |
194 | return ni_load_mi_ex(ni, rno, mi); | |
195 | } | |
196 | ||
197 | /* | |
198 | * ni_find_attr | |
199 | * | |
200 | * returns attribute and record this attribute belongs to | |
201 | */ | |
202 | struct ATTRIB *ni_find_attr(struct ntfs_inode *ni, struct ATTRIB *attr, | |
203 | struct ATTR_LIST_ENTRY **le_o, enum ATTR_TYPE type, | |
204 | const __le16 *name, u8 name_len, const CLST *vcn, | |
205 | struct mft_inode **mi) | |
206 | { | |
207 | struct ATTR_LIST_ENTRY *le; | |
208 | struct mft_inode *m; | |
209 | ||
210 | if (!ni->attr_list.size || | |
211 | (!name_len && (type == ATTR_LIST || type == ATTR_STD))) { | |
212 | if (le_o) | |
213 | *le_o = NULL; | |
214 | if (mi) | |
215 | *mi = &ni->mi; | |
216 | ||
217 | /* Look for required attribute in primary record */ | |
218 | return mi_find_attr(&ni->mi, attr, type, name, name_len, NULL); | |
219 | } | |
220 | ||
221 | /* first look for list entry of required type */ | |
222 | le = al_find_ex(ni, le_o ? *le_o : NULL, type, name, name_len, vcn); | |
223 | if (!le) | |
224 | return NULL; | |
225 | ||
226 | if (le_o) | |
227 | *le_o = le; | |
228 | ||
229 | /* Load record that contains this attribute */ | |
230 | if (ni_load_mi(ni, le, &m)) | |
231 | return NULL; | |
232 | ||
233 | /* Look for required attribute */ | |
234 | attr = mi_find_attr(m, NULL, type, name, name_len, &le->id); | |
235 | ||
236 | if (!attr) | |
237 | goto out; | |
238 | ||
239 | if (!attr->non_res) { | |
240 | if (vcn && *vcn) | |
241 | goto out; | |
242 | } else if (!vcn) { | |
243 | if (attr->nres.svcn) | |
244 | goto out; | |
245 | } else if (le64_to_cpu(attr->nres.svcn) > *vcn || | |
246 | *vcn > le64_to_cpu(attr->nres.evcn)) { | |
247 | goto out; | |
248 | } | |
249 | ||
250 | if (mi) | |
251 | *mi = m; | |
252 | return attr; | |
253 | ||
254 | out: | |
255 | ntfs_set_state(ni->mi.sbi, NTFS_DIRTY_ERROR); | |
256 | return NULL; | |
257 | } | |
258 | ||
259 | /* | |
260 | * ni_enum_attr_ex | |
261 | * | |
262 | * enumerates attributes in ntfs_inode | |
263 | */ | |
264 | struct ATTRIB *ni_enum_attr_ex(struct ntfs_inode *ni, struct ATTRIB *attr, | |
265 | struct ATTR_LIST_ENTRY **le, | |
266 | struct mft_inode **mi) | |
267 | { | |
268 | struct mft_inode *mi2; | |
269 | struct ATTR_LIST_ENTRY *le2; | |
270 | ||
271 | /* Do we have an attribute list? */ | |
272 | if (!ni->attr_list.size) { | |
273 | *le = NULL; | |
274 | if (mi) | |
275 | *mi = &ni->mi; | |
276 | /* Enum attributes in primary record */ | |
277 | return mi_enum_attr(&ni->mi, attr); | |
278 | } | |
279 | ||
280 | /* get next list entry */ | |
281 | le2 = *le = al_enumerate(ni, attr ? *le : NULL); | |
282 | if (!le2) | |
283 | return NULL; | |
284 | ||
285 | /* Load record that contains the required attribute */ | |
286 | if (ni_load_mi(ni, le2, &mi2)) | |
287 | return NULL; | |
288 | ||
289 | if (mi) | |
290 | *mi = mi2; | |
291 | ||
292 | /* Find attribute in loaded record */ | |
293 | return rec_find_attr_le(mi2, le2); | |
294 | } | |
295 | ||
296 | /* | |
297 | * ni_load_attr | |
298 | * | |
299 | * loads attribute that contains given vcn | |
300 | */ | |
301 | struct ATTRIB *ni_load_attr(struct ntfs_inode *ni, enum ATTR_TYPE type, | |
302 | const __le16 *name, u8 name_len, CLST vcn, | |
303 | struct mft_inode **pmi) | |
304 | { | |
305 | struct ATTR_LIST_ENTRY *le; | |
306 | struct ATTRIB *attr; | |
307 | struct mft_inode *mi; | |
308 | struct ATTR_LIST_ENTRY *next; | |
309 | ||
310 | if (!ni->attr_list.size) { | |
311 | if (pmi) | |
312 | *pmi = &ni->mi; | |
313 | return mi_find_attr(&ni->mi, NULL, type, name, name_len, NULL); | |
314 | } | |
315 | ||
316 | le = al_find_ex(ni, NULL, type, name, name_len, NULL); | |
317 | if (!le) | |
318 | return NULL; | |
319 | ||
320 | /* | |
321 | * Unfortunately ATTR_LIST_ENTRY contains only start vcn | |
322 | * So to find the ATTRIB segment that contains 'vcn' we should | |
323 | * enumerate some entries | |
324 | */ | |
325 | if (vcn) { | |
326 | for (;; le = next) { | |
327 | next = al_find_ex(ni, le, type, name, name_len, NULL); | |
328 | if (!next || le64_to_cpu(next->vcn) > vcn) | |
329 | break; | |
330 | } | |
331 | } | |
332 | ||
333 | if (ni_load_mi(ni, le, &mi)) | |
334 | return NULL; | |
335 | ||
336 | if (pmi) | |
337 | *pmi = mi; | |
338 | ||
339 | attr = mi_find_attr(mi, NULL, type, name, name_len, &le->id); | |
340 | if (!attr) | |
341 | return NULL; | |
342 | ||
343 | if (!attr->non_res) | |
344 | return attr; | |
345 | ||
346 | if (le64_to_cpu(attr->nres.svcn) <= vcn && | |
347 | vcn <= le64_to_cpu(attr->nres.evcn)) | |
348 | return attr; | |
349 | ||
350 | return NULL; | |
351 | } | |
352 | ||
353 | /* | |
354 | * ni_load_all_mi | |
355 | * | |
356 | * loads all subrecords | |
357 | */ | |
358 | int ni_load_all_mi(struct ntfs_inode *ni) | |
359 | { | |
360 | int err; | |
361 | struct ATTR_LIST_ENTRY *le; | |
362 | ||
363 | if (!ni->attr_list.size) | |
364 | return 0; | |
365 | ||
366 | le = NULL; | |
367 | ||
368 | while ((le = al_enumerate(ni, le))) { | |
369 | CLST rno = ino_get(&le->ref); | |
370 | ||
371 | if (rno == ni->mi.rno) | |
372 | continue; | |
373 | ||
374 | err = ni_load_mi_ex(ni, rno, NULL); | |
375 | if (err) | |
376 | return err; | |
377 | } | |
378 | ||
379 | return 0; | |
380 | } | |
381 | ||
382 | /* | |
383 | * ni_add_subrecord | |
384 | * | |
385 | * allocate + format + attach a new subrecord | |
386 | */ | |
387 | bool ni_add_subrecord(struct ntfs_inode *ni, CLST rno, struct mft_inode **mi) | |
388 | { | |
389 | struct mft_inode *m; | |
390 | ||
391 | m = ntfs_zalloc(sizeof(struct mft_inode)); | |
392 | if (!m) | |
393 | return false; | |
394 | ||
395 | if (mi_format_new(m, ni->mi.sbi, rno, 0, ni->mi.rno == MFT_REC_MFT)) { | |
396 | mi_put(m); | |
397 | return false; | |
398 | } | |
399 | ||
400 | mi_get_ref(&ni->mi, &m->mrec->parent_ref); | |
401 | ||
402 | ni_add_mi(ni, m); | |
403 | *mi = m; | |
404 | return true; | |
405 | } | |
406 | ||
407 | /* | |
408 | * ni_remove_attr | |
409 | * | |
410 | * removes all attributes for the given type/name/id | |
411 | */ | |
412 | int ni_remove_attr(struct ntfs_inode *ni, enum ATTR_TYPE type, | |
413 | const __le16 *name, size_t name_len, bool base_only, | |
414 | const __le16 *id) | |
415 | { | |
416 | int err; | |
417 | struct ATTRIB *attr; | |
418 | struct ATTR_LIST_ENTRY *le; | |
419 | struct mft_inode *mi; | |
420 | u32 type_in; | |
421 | int diff; | |
422 | ||
423 | if (base_only || type == ATTR_LIST || !ni->attr_list.size) { | |
424 | attr = mi_find_attr(&ni->mi, NULL, type, name, name_len, id); | |
425 | if (!attr) | |
426 | return -ENOENT; | |
427 | ||
428 | mi_remove_attr(&ni->mi, attr); | |
429 | return 0; | |
430 | } | |
431 | ||
432 | type_in = le32_to_cpu(type); | |
433 | le = NULL; | |
434 | ||
435 | for (;;) { | |
436 | le = al_enumerate(ni, le); | |
437 | if (!le) | |
438 | return 0; | |
439 | ||
440 | next_le2: | |
441 | diff = le32_to_cpu(le->type) - type_in; | |
442 | if (diff < 0) | |
443 | continue; | |
444 | ||
445 | if (diff > 0) | |
446 | return 0; | |
447 | ||
448 | if (le->name_len != name_len) | |
449 | continue; | |
450 | ||
451 | if (name_len && | |
452 | memcmp(le_name(le), name, name_len * sizeof(short))) | |
453 | continue; | |
454 | ||
455 | if (id && le->id != *id) | |
456 | continue; | |
457 | err = ni_load_mi(ni, le, &mi); | |
458 | if (err) | |
459 | return err; | |
460 | ||
461 | al_remove_le(ni, le); | |
462 | ||
463 | attr = mi_find_attr(mi, NULL, type, name, name_len, id); | |
464 | if (!attr) | |
465 | return -ENOENT; | |
466 | ||
467 | mi_remove_attr(mi, attr); | |
468 | ||
469 | if (PtrOffset(ni->attr_list.le, le) >= ni->attr_list.size) | |
470 | return 0; | |
471 | goto next_le2; | |
472 | } | |
473 | } | |
474 | ||
475 | /* | |
476 | * ni_ins_new_attr | |
477 | * | |
478 | * inserts the attribute into record | |
479 | * Returns not full constructed attribute or NULL if not possible to create | |
480 | */ | |
481 | static struct ATTRIB *ni_ins_new_attr(struct ntfs_inode *ni, | |
482 | struct mft_inode *mi, | |
483 | struct ATTR_LIST_ENTRY *le, | |
484 | enum ATTR_TYPE type, const __le16 *name, | |
485 | u8 name_len, u32 asize, u16 name_off, | |
486 | CLST svcn) | |
487 | { | |
488 | int err; | |
489 | struct ATTRIB *attr; | |
490 | bool le_added = false; | |
491 | struct MFT_REF ref; | |
492 | ||
493 | mi_get_ref(mi, &ref); | |
494 | ||
495 | if (type != ATTR_LIST && !le && ni->attr_list.size) { | |
496 | err = al_add_le(ni, type, name, name_len, svcn, cpu_to_le16(-1), | |
497 | &ref, &le); | |
498 | if (err) { | |
499 | /* no memory or no space */ | |
500 | return NULL; | |
501 | } | |
502 | le_added = true; | |
503 | ||
504 | /* | |
505 | * al_add_le -> attr_set_size (list) -> ni_expand_list | |
506 | * which moves some attributes out of primary record | |
507 | * this means that name may point into moved memory | |
508 | * reinit 'name' from le | |
509 | */ | |
510 | name = le->name; | |
511 | } | |
512 | ||
513 | attr = mi_insert_attr(mi, type, name, name_len, asize, name_off); | |
514 | if (!attr) { | |
515 | if (le_added) | |
516 | al_remove_le(ni, le); | |
517 | return NULL; | |
518 | } | |
519 | ||
520 | if (type == ATTR_LIST) { | |
521 | /*attr list is not in list entry array*/ | |
522 | goto out; | |
523 | } | |
524 | ||
525 | if (!le) | |
526 | goto out; | |
527 | ||
528 | /* Update ATTRIB Id and record reference */ | |
529 | le->id = attr->id; | |
530 | ni->attr_list.dirty = true; | |
531 | le->ref = ref; | |
532 | ||
533 | out: | |
534 | return attr; | |
535 | } | |
536 | ||
537 | /* | |
538 | * random write access to sparsed or compressed file may result to | |
539 | * not optimized packed runs. | |
540 | * Here it is the place to optimize it | |
541 | */ | |
542 | static int ni_repack(struct ntfs_inode *ni) | |
543 | { | |
544 | int err = 0; | |
545 | struct ntfs_sb_info *sbi = ni->mi.sbi; | |
546 | struct mft_inode *mi, *mi_p = NULL; | |
547 | struct ATTRIB *attr = NULL, *attr_p; | |
548 | struct ATTR_LIST_ENTRY *le = NULL, *le_p; | |
549 | CLST alloc = 0; | |
550 | u8 cluster_bits = sbi->cluster_bits; | |
551 | CLST svcn, evcn = 0, svcn_p, evcn_p, next_svcn; | |
552 | u32 roff, rs = sbi->record_size; | |
553 | struct runs_tree run; | |
554 | ||
555 | run_init(&run); | |
556 | ||
557 | while ((attr = ni_enum_attr_ex(ni, attr, &le, &mi))) { | |
558 | if (!attr->non_res) | |
559 | continue; | |
560 | ||
561 | svcn = le64_to_cpu(attr->nres.svcn); | |
562 | if (svcn != le64_to_cpu(le->vcn)) { | |
563 | err = -EINVAL; | |
564 | break; | |
565 | } | |
566 | ||
567 | if (!svcn) { | |
568 | alloc = le64_to_cpu(attr->nres.alloc_size) >> | |
569 | cluster_bits; | |
570 | mi_p = NULL; | |
571 | } else if (svcn != evcn + 1) { | |
572 | err = -EINVAL; | |
573 | break; | |
574 | } | |
575 | ||
576 | evcn = le64_to_cpu(attr->nres.evcn); | |
577 | ||
578 | if (svcn > evcn + 1) { | |
579 | err = -EINVAL; | |
580 | break; | |
581 | } | |
582 | ||
583 | if (!mi_p) { | |
584 | /* do not try if too little free space */ | |
585 | if (le32_to_cpu(mi->mrec->used) + 8 >= rs) | |
586 | continue; | |
587 | ||
588 | /* do not try if last attribute segment */ | |
589 | if (evcn + 1 == alloc) | |
590 | continue; | |
591 | run_close(&run); | |
592 | } | |
593 | ||
594 | roff = le16_to_cpu(attr->nres.run_off); | |
595 | err = run_unpack(&run, sbi, ni->mi.rno, svcn, evcn, svcn, | |
596 | Add2Ptr(attr, roff), | |
597 | le32_to_cpu(attr->size) - roff); | |
598 | if (err < 0) | |
599 | break; | |
600 | ||
601 | if (!mi_p) { | |
602 | mi_p = mi; | |
603 | attr_p = attr; | |
604 | svcn_p = svcn; | |
605 | evcn_p = evcn; | |
606 | le_p = le; | |
607 | err = 0; | |
608 | continue; | |
609 | } | |
610 | ||
611 | /* | |
612 | * run contains data from two records: mi_p and mi | |
613 | * try to pack in one | |
614 | */ | |
615 | err = mi_pack_runs(mi_p, attr_p, &run, evcn + 1 - svcn_p); | |
616 | if (err) | |
617 | break; | |
618 | ||
619 | next_svcn = le64_to_cpu(attr_p->nres.evcn) + 1; | |
620 | ||
621 | if (next_svcn >= evcn + 1) { | |
622 | /* we can remove this attribute segment */ | |
623 | al_remove_le(ni, le); | |
624 | mi_remove_attr(mi, attr); | |
625 | le = le_p; | |
626 | continue; | |
627 | } | |
628 | ||
629 | attr->nres.svcn = le->vcn = cpu_to_le64(next_svcn); | |
630 | mi->dirty = true; | |
631 | ni->attr_list.dirty = true; | |
632 | ||
633 | if (evcn + 1 == alloc) { | |
634 | err = mi_pack_runs(mi, attr, &run, | |
635 | evcn + 1 - next_svcn); | |
636 | if (err) | |
637 | break; | |
638 | mi_p = NULL; | |
639 | } else { | |
640 | mi_p = mi; | |
641 | attr_p = attr; | |
642 | svcn_p = next_svcn; | |
643 | evcn_p = evcn; | |
644 | le_p = le; | |
645 | run_truncate_head(&run, next_svcn); | |
646 | } | |
647 | } | |
648 | ||
649 | if (err) { | |
650 | ntfs_inode_warn(&ni->vfs_inode, "repack problem"); | |
651 | ntfs_set_state(sbi, NTFS_DIRTY_ERROR); | |
652 | ||
653 | /* Pack loaded but not packed runs */ | |
654 | if (mi_p) | |
655 | mi_pack_runs(mi_p, attr_p, &run, evcn_p + 1 - svcn_p); | |
656 | } | |
657 | ||
658 | run_close(&run); | |
659 | return err; | |
660 | } | |
661 | ||
662 | /* | |
663 | * ni_try_remove_attr_list | |
664 | * | |
665 | * Can we remove attribute list? | |
666 | * Check the case when primary record contains enough space for all attributes | |
667 | */ | |
668 | static int ni_try_remove_attr_list(struct ntfs_inode *ni) | |
669 | { | |
670 | int err = 0; | |
671 | struct ntfs_sb_info *sbi = ni->mi.sbi; | |
672 | struct ATTRIB *attr, *attr_list, *attr_ins; | |
673 | struct ATTR_LIST_ENTRY *le; | |
674 | struct mft_inode *mi; | |
675 | u32 asize, free; | |
676 | struct MFT_REF ref; | |
677 | __le16 id; | |
678 | ||
679 | if (!ni->attr_list.dirty) | |
680 | return 0; | |
681 | ||
682 | err = ni_repack(ni); | |
683 | if (err) | |
684 | return err; | |
685 | ||
686 | attr_list = mi_find_attr(&ni->mi, NULL, ATTR_LIST, NULL, 0, NULL); | |
687 | if (!attr_list) | |
688 | return 0; | |
689 | ||
690 | asize = le32_to_cpu(attr_list->size); | |
691 | ||
692 | /* free space in primary record without attribute list */ | |
693 | free = sbi->record_size - le32_to_cpu(ni->mi.mrec->used) + asize; | |
694 | mi_get_ref(&ni->mi, &ref); | |
695 | ||
696 | le = NULL; | |
697 | while ((le = al_enumerate(ni, le))) { | |
698 | if (!memcmp(&le->ref, &ref, sizeof(ref))) | |
699 | continue; | |
700 | ||
701 | if (le->vcn) | |
702 | return 0; | |
703 | ||
704 | mi = ni_find_mi(ni, ino_get(&le->ref)); | |
705 | if (!mi) | |
706 | return 0; | |
707 | ||
708 | attr = mi_find_attr(mi, NULL, le->type, le_name(le), | |
709 | le->name_len, &le->id); | |
710 | if (!attr) | |
711 | return 0; | |
712 | ||
713 | asize = le32_to_cpu(attr->size); | |
714 | if (asize > free) | |
715 | return 0; | |
716 | ||
717 | free -= asize; | |
718 | } | |
719 | ||
720 | /* Is seems that attribute list can be removed from primary record */ | |
721 | mi_remove_attr(&ni->mi, attr_list); | |
722 | ||
723 | /* | |
724 | * Repeat the cycle above and move all attributes to primary record. | |
725 | * It should be success! | |
726 | */ | |
727 | le = NULL; | |
728 | while ((le = al_enumerate(ni, le))) { | |
729 | if (!memcmp(&le->ref, &ref, sizeof(ref))) | |
730 | continue; | |
731 | ||
732 | mi = ni_find_mi(ni, ino_get(&le->ref)); | |
733 | ||
734 | attr = mi_find_attr(mi, NULL, le->type, le_name(le), | |
735 | le->name_len, &le->id); | |
736 | asize = le32_to_cpu(attr->size); | |
737 | ||
738 | /* insert into primary record */ | |
739 | attr_ins = mi_insert_attr(&ni->mi, le->type, le_name(le), | |
740 | le->name_len, asize, | |
741 | le16_to_cpu(attr->name_off)); | |
742 | id = attr_ins->id; | |
743 | ||
744 | /* copy all except id */ | |
745 | memcpy(attr_ins, attr, asize); | |
746 | attr_ins->id = id; | |
747 | ||
748 | /* remove from original record */ | |
749 | mi_remove_attr(mi, attr); | |
750 | } | |
751 | ||
752 | run_deallocate(sbi, &ni->attr_list.run, true); | |
753 | run_close(&ni->attr_list.run); | |
754 | ni->attr_list.size = 0; | |
755 | ntfs_free(ni->attr_list.le); | |
756 | ni->attr_list.le = NULL; | |
757 | ni->attr_list.dirty = false; | |
758 | ||
759 | return 0; | |
760 | } | |
761 | ||
762 | /* | |
763 | * ni_create_attr_list | |
764 | * | |
765 | * generates an attribute list for this primary record | |
766 | */ | |
767 | int ni_create_attr_list(struct ntfs_inode *ni) | |
768 | { | |
769 | struct ntfs_sb_info *sbi = ni->mi.sbi; | |
770 | int err; | |
771 | u32 lsize; | |
772 | struct ATTRIB *attr; | |
773 | struct ATTRIB *arr_move[7]; | |
774 | struct ATTR_LIST_ENTRY *le, *le_b[7]; | |
775 | struct MFT_REC *rec; | |
776 | bool is_mft; | |
777 | CLST rno = 0; | |
778 | struct mft_inode *mi; | |
779 | u32 free_b, nb, to_free, rs; | |
780 | u16 sz; | |
781 | ||
782 | is_mft = ni->mi.rno == MFT_REC_MFT; | |
783 | rec = ni->mi.mrec; | |
784 | rs = sbi->record_size; | |
785 | ||
786 | /* | |
787 | * Skip estimating exact memory requirement | |
788 | * Looks like one record_size is always enough | |
789 | */ | |
790 | le = ntfs_malloc(al_aligned(rs)); | |
791 | if (!le) { | |
792 | err = -ENOMEM; | |
793 | goto out; | |
794 | } | |
795 | ||
796 | mi_get_ref(&ni->mi, &le->ref); | |
797 | ni->attr_list.le = le; | |
798 | ||
799 | attr = NULL; | |
800 | nb = 0; | |
801 | free_b = 0; | |
802 | attr = NULL; | |
803 | ||
804 | for (; (attr = mi_enum_attr(&ni->mi, attr)); le = Add2Ptr(le, sz)) { | |
805 | sz = le_size(attr->name_len); | |
806 | le->type = attr->type; | |
807 | le->size = cpu_to_le16(sz); | |
808 | le->name_len = attr->name_len; | |
809 | le->name_off = offsetof(struct ATTR_LIST_ENTRY, name); | |
810 | le->vcn = 0; | |
811 | if (le != ni->attr_list.le) | |
812 | le->ref = ni->attr_list.le->ref; | |
813 | le->id = attr->id; | |
814 | ||
815 | if (attr->name_len) | |
816 | memcpy(le->name, attr_name(attr), | |
817 | sizeof(short) * attr->name_len); | |
818 | else if (attr->type == ATTR_STD) | |
819 | continue; | |
820 | else if (attr->type == ATTR_LIST) | |
821 | continue; | |
822 | else if (is_mft && attr->type == ATTR_DATA) | |
823 | continue; | |
824 | ||
825 | if (!nb || nb < ARRAY_SIZE(arr_move)) { | |
826 | le_b[nb] = le; | |
827 | arr_move[nb++] = attr; | |
828 | free_b += le32_to_cpu(attr->size); | |
829 | } | |
830 | } | |
831 | ||
832 | lsize = PtrOffset(ni->attr_list.le, le); | |
833 | ni->attr_list.size = lsize; | |
834 | ||
835 | to_free = le32_to_cpu(rec->used) + lsize + SIZEOF_RESIDENT; | |
836 | if (to_free <= rs) { | |
837 | to_free = 0; | |
838 | } else { | |
839 | to_free -= rs; | |
840 | ||
841 | if (to_free > free_b) { | |
842 | err = -EINVAL; | |
843 | goto out1; | |
844 | } | |
845 | } | |
846 | ||
847 | /* Allocate child mft. */ | |
848 | err = ntfs_look_free_mft(sbi, &rno, is_mft, ni, &mi); | |
849 | if (err) | |
850 | goto out1; | |
851 | ||
852 | /* Call 'mi_remove_attr' in reverse order to keep pointers 'arr_move' valid */ | |
853 | while (to_free > 0) { | |
854 | struct ATTRIB *b = arr_move[--nb]; | |
855 | u32 asize = le32_to_cpu(b->size); | |
856 | u16 name_off = le16_to_cpu(b->name_off); | |
857 | ||
858 | attr = mi_insert_attr(mi, b->type, Add2Ptr(b, name_off), | |
859 | b->name_len, asize, name_off); | |
860 | WARN_ON(!attr); | |
861 | ||
862 | mi_get_ref(mi, &le_b[nb]->ref); | |
863 | le_b[nb]->id = attr->id; | |
864 | ||
865 | /* copy all except id */ | |
866 | memcpy(attr, b, asize); | |
867 | attr->id = le_b[nb]->id; | |
868 | ||
869 | WARN_ON(!mi_remove_attr(&ni->mi, b)); | |
870 | ||
871 | if (to_free <= asize) | |
872 | break; | |
873 | to_free -= asize; | |
874 | WARN_ON(!nb); | |
875 | } | |
876 | ||
877 | attr = mi_insert_attr(&ni->mi, ATTR_LIST, NULL, 0, | |
878 | lsize + SIZEOF_RESIDENT, SIZEOF_RESIDENT); | |
879 | WARN_ON(!attr); | |
880 | ||
881 | attr->non_res = 0; | |
882 | attr->flags = 0; | |
883 | attr->res.data_size = cpu_to_le32(lsize); | |
884 | attr->res.data_off = SIZEOF_RESIDENT_LE; | |
885 | attr->res.flags = 0; | |
886 | attr->res.res = 0; | |
887 | ||
888 | memcpy(resident_data_ex(attr, lsize), ni->attr_list.le, lsize); | |
889 | ||
890 | ni->attr_list.dirty = false; | |
891 | ||
892 | mark_inode_dirty(&ni->vfs_inode); | |
893 | goto out; | |
894 | ||
895 | out1: | |
896 | ntfs_free(ni->attr_list.le); | |
897 | ni->attr_list.le = NULL; | |
898 | ni->attr_list.size = 0; | |
899 | ||
900 | out: | |
901 | return err; | |
902 | } | |
903 | ||
904 | /* | |
905 | * ni_ins_attr_ext | |
906 | * | |
907 | * This method adds an external attribute to the ntfs_inode. | |
908 | */ | |
909 | static int ni_ins_attr_ext(struct ntfs_inode *ni, struct ATTR_LIST_ENTRY *le, | |
910 | enum ATTR_TYPE type, const __le16 *name, u8 name_len, | |
911 | u32 asize, CLST svcn, u16 name_off, bool force_ext, | |
912 | struct ATTRIB **ins_attr, struct mft_inode **ins_mi) | |
913 | { | |
914 | struct ATTRIB *attr; | |
915 | struct mft_inode *mi; | |
916 | CLST rno; | |
917 | u64 vbo; | |
918 | struct rb_node *node; | |
919 | int err; | |
920 | bool is_mft, is_mft_data; | |
921 | struct ntfs_sb_info *sbi = ni->mi.sbi; | |
922 | ||
923 | is_mft = ni->mi.rno == MFT_REC_MFT; | |
924 | is_mft_data = is_mft && type == ATTR_DATA && !name_len; | |
925 | ||
926 | if (asize > sbi->max_bytes_per_attr) { | |
927 | err = -EINVAL; | |
928 | goto out; | |
929 | } | |
930 | ||
931 | /* | |
932 | * standard information and attr_list cannot be made external. | |
933 | * The Log File cannot have any external attributes | |
934 | */ | |
935 | if (type == ATTR_STD || type == ATTR_LIST || | |
936 | ni->mi.rno == MFT_REC_LOG) { | |
937 | err = -EINVAL; | |
938 | goto out; | |
939 | } | |
940 | ||
941 | /* Create attribute list if it is not already existed */ | |
942 | if (!ni->attr_list.size) { | |
943 | err = ni_create_attr_list(ni); | |
944 | if (err) | |
945 | goto out; | |
946 | } | |
947 | ||
948 | vbo = is_mft_data ? ((u64)svcn << sbi->cluster_bits) : 0; | |
949 | ||
950 | if (force_ext) | |
951 | goto insert_ext; | |
952 | ||
953 | /* Load all subrecords into memory. */ | |
954 | err = ni_load_all_mi(ni); | |
955 | if (err) | |
956 | goto out; | |
957 | ||
958 | /* Check each of loaded subrecord */ | |
959 | for (node = rb_first(&ni->mi_tree); node; node = rb_next(node)) { | |
960 | mi = rb_entry(node, struct mft_inode, node); | |
961 | ||
962 | if (is_mft_data && | |
963 | (mi_enum_attr(mi, NULL) || | |
964 | vbo <= ((u64)mi->rno << sbi->record_bits))) { | |
965 | /* We can't accept this record 'case MFT's bootstrapping */ | |
966 | continue; | |
967 | } | |
968 | if (is_mft && | |
969 | mi_find_attr(mi, NULL, ATTR_DATA, NULL, 0, NULL)) { | |
970 | /* | |
971 | * This child record already has a ATTR_DATA. | |
972 | * So it can't accept any other records. | |
973 | */ | |
974 | continue; | |
975 | } | |
976 | ||
977 | if ((type != ATTR_NAME || name_len) && | |
978 | mi_find_attr(mi, NULL, type, name, name_len, NULL)) { | |
979 | /* Only indexed attributes can share same record */ | |
980 | continue; | |
981 | } | |
982 | ||
983 | /* Try to insert attribute into this subrecord */ | |
984 | attr = ni_ins_new_attr(ni, mi, le, type, name, name_len, asize, | |
985 | name_off, svcn); | |
986 | if (!attr) | |
987 | continue; | |
988 | ||
989 | if (ins_attr) | |
990 | *ins_attr = attr; | |
991 | return 0; | |
992 | } | |
993 | ||
994 | insert_ext: | |
995 | /* We have to allocate a new child subrecord*/ | |
996 | err = ntfs_look_free_mft(sbi, &rno, is_mft_data, ni, &mi); | |
997 | if (err) | |
998 | goto out; | |
999 | ||
1000 | if (is_mft_data && vbo <= ((u64)rno << sbi->record_bits)) { | |
1001 | err = -EINVAL; | |
1002 | goto out1; | |
1003 | } | |
1004 | ||
1005 | attr = ni_ins_new_attr(ni, mi, le, type, name, name_len, asize, | |
1006 | name_off, svcn); | |
1007 | if (!attr) | |
1008 | goto out2; | |
1009 | ||
1010 | if (ins_attr) | |
1011 | *ins_attr = attr; | |
1012 | if (ins_mi) | |
1013 | *ins_mi = mi; | |
1014 | ||
1015 | return 0; | |
1016 | ||
1017 | out2: | |
1018 | ni_remove_mi(ni, mi); | |
1019 | mi_put(mi); | |
1020 | err = -EINVAL; | |
1021 | ||
1022 | out1: | |
1023 | ntfs_mark_rec_free(sbi, rno); | |
1024 | ||
1025 | out: | |
1026 | return err; | |
1027 | } | |
1028 | ||
1029 | /* | |
1030 | * ni_insert_attr | |
1031 | * | |
1032 | * inserts an attribute into the file. | |
1033 | * | |
1034 | * If the primary record has room, it will just insert the attribute. | |
1035 | * If not, it may make the attribute external. | |
1036 | * For $MFT::Data it may make room for the attribute by | |
1037 | * making other attributes external. | |
1038 | * | |
1039 | * NOTE: | |
1040 | * The ATTR_LIST and ATTR_STD cannot be made external. | |
1041 | * This function does not fill new attribute full | |
1042 | * It only fills 'size'/'type'/'id'/'name_len' fields | |
1043 | */ | |
1044 | static int ni_insert_attr(struct ntfs_inode *ni, enum ATTR_TYPE type, | |
1045 | const __le16 *name, u8 name_len, u32 asize, | |
1046 | u16 name_off, CLST svcn, struct ATTRIB **ins_attr, | |
1047 | struct mft_inode **ins_mi) | |
1048 | { | |
1049 | struct ntfs_sb_info *sbi = ni->mi.sbi; | |
1050 | int err; | |
1051 | struct ATTRIB *attr, *eattr; | |
1052 | struct MFT_REC *rec; | |
1053 | bool is_mft; | |
1054 | struct ATTR_LIST_ENTRY *le; | |
1055 | u32 list_reserve, max_free, free, used, t32; | |
1056 | __le16 id; | |
1057 | u16 t16; | |
1058 | ||
1059 | is_mft = ni->mi.rno == MFT_REC_MFT; | |
1060 | rec = ni->mi.mrec; | |
1061 | ||
1062 | list_reserve = SIZEOF_NONRESIDENT + 3 * (1 + 2 * sizeof(u32)); | |
1063 | used = le32_to_cpu(rec->used); | |
1064 | free = sbi->record_size - used; | |
1065 | ||
1066 | if (is_mft && type != ATTR_LIST) { | |
1067 | /* Reserve space for the ATTRIB List. */ | |
1068 | if (free < list_reserve) | |
1069 | free = 0; | |
1070 | else | |
1071 | free -= list_reserve; | |
1072 | } | |
1073 | ||
1074 | if (asize <= free) { | |
1075 | attr = ni_ins_new_attr(ni, &ni->mi, NULL, type, name, name_len, | |
1076 | asize, name_off, svcn); | |
1077 | if (attr) { | |
1078 | if (ins_attr) | |
1079 | *ins_attr = attr; | |
1080 | if (ins_mi) | |
1081 | *ins_mi = &ni->mi; | |
1082 | err = 0; | |
1083 | goto out; | |
1084 | } | |
1085 | } | |
1086 | ||
1087 | if (!is_mft || type != ATTR_DATA || svcn) { | |
1088 | /* This ATTRIB will be external. */ | |
1089 | err = ni_ins_attr_ext(ni, NULL, type, name, name_len, asize, | |
1090 | svcn, name_off, false, ins_attr, ins_mi); | |
1091 | goto out; | |
1092 | } | |
1093 | ||
1094 | /* | |
1095 | * Here we have: "is_mft && type == ATTR_DATA && !svcn | |
1096 | * | |
1097 | * The first chunk of the $MFT::Data ATTRIB must be the base record. | |
1098 | * Evict as many other attributes as possible. | |
1099 | */ | |
1100 | max_free = free; | |
1101 | ||
1102 | /* Estimate the result of moving all possible attributes away.*/ | |
1103 | attr = NULL; | |
1104 | ||
1105 | while ((attr = mi_enum_attr(&ni->mi, attr))) { | |
1106 | if (attr->type == ATTR_STD) | |
1107 | continue; | |
1108 | if (attr->type == ATTR_LIST) | |
1109 | continue; | |
1110 | max_free += le32_to_cpu(attr->size); | |
1111 | } | |
1112 | ||
1113 | if (max_free < asize + list_reserve) { | |
1114 | /* Impossible to insert this attribute into primary record */ | |
1115 | err = -EINVAL; | |
1116 | goto out; | |
1117 | } | |
1118 | ||
1119 | /* Start real attribute moving */ | |
1120 | attr = NULL; | |
1121 | ||
1122 | for (;;) { | |
1123 | attr = mi_enum_attr(&ni->mi, attr); | |
1124 | if (!attr) { | |
1125 | /* We should never be here 'cause we have already check this case */ | |
1126 | err = -EINVAL; | |
1127 | goto out; | |
1128 | } | |
1129 | ||
1130 | /* Skip attributes that MUST be primary record */ | |
1131 | if (attr->type == ATTR_STD || attr->type == ATTR_LIST) | |
1132 | continue; | |
1133 | ||
1134 | le = NULL; | |
1135 | if (ni->attr_list.size) { | |
1136 | le = al_find_le(ni, NULL, attr); | |
1137 | if (!le) { | |
1138 | /* Really this is a serious bug */ | |
1139 | err = -EINVAL; | |
1140 | goto out; | |
1141 | } | |
1142 | } | |
1143 | ||
1144 | t32 = le32_to_cpu(attr->size); | |
1145 | t16 = le16_to_cpu(attr->name_off); | |
1146 | err = ni_ins_attr_ext(ni, le, attr->type, Add2Ptr(attr, t16), | |
1147 | attr->name_len, t32, attr_svcn(attr), t16, | |
1148 | false, &eattr, NULL); | |
1149 | if (err) | |
1150 | return err; | |
1151 | ||
1152 | id = eattr->id; | |
1153 | memcpy(eattr, attr, t32); | |
1154 | eattr->id = id; | |
1155 | ||
1156 | /* remove attrib from primary record */ | |
1157 | mi_remove_attr(&ni->mi, attr); | |
1158 | ||
1159 | /* attr now points to next attribute */ | |
1160 | if (attr->type == ATTR_END) | |
1161 | goto out; | |
1162 | } | |
1163 | while (asize + list_reserve > sbi->record_size - le32_to_cpu(rec->used)) | |
1164 | ; | |
1165 | ||
1166 | attr = ni_ins_new_attr(ni, &ni->mi, NULL, type, name, name_len, asize, | |
1167 | name_off, svcn); | |
1168 | if (!attr) { | |
1169 | err = -EINVAL; | |
1170 | goto out; | |
1171 | } | |
1172 | ||
1173 | if (ins_attr) | |
1174 | *ins_attr = attr; | |
1175 | if (ins_mi) | |
1176 | *ins_mi = &ni->mi; | |
1177 | ||
1178 | out: | |
1179 | return err; | |
1180 | } | |
1181 | ||
1182 | /* | |
1183 | * ni_expand_mft_list | |
1184 | * | |
1185 | * This method splits ATTR_DATA of $MFT | |
1186 | */ | |
1187 | static int ni_expand_mft_list(struct ntfs_inode *ni) | |
1188 | { | |
1189 | int err = 0; | |
1190 | struct runs_tree *run = &ni->file.run; | |
1191 | u32 asize, run_size, done = 0; | |
1192 | struct ATTRIB *attr; | |
1193 | struct rb_node *node; | |
1194 | CLST mft_min, mft_new, svcn, evcn, plen; | |
1195 | struct mft_inode *mi, *mi_min, *mi_new; | |
1196 | struct ntfs_sb_info *sbi = ni->mi.sbi; | |
1197 | ||
1198 | /* Find the nearest Mft */ | |
1199 | mft_min = 0; | |
1200 | mft_new = 0; | |
1201 | mi_min = NULL; | |
1202 | ||
1203 | for (node = rb_first(&ni->mi_tree); node; node = rb_next(node)) { | |
1204 | mi = rb_entry(node, struct mft_inode, node); | |
1205 | ||
1206 | attr = mi_enum_attr(mi, NULL); | |
1207 | ||
1208 | if (!attr) { | |
1209 | mft_min = mi->rno; | |
1210 | mi_min = mi; | |
1211 | break; | |
1212 | } | |
1213 | } | |
1214 | ||
1215 | if (ntfs_look_free_mft(sbi, &mft_new, true, ni, &mi_new)) { | |
1216 | mft_new = 0; | |
1217 | // really this is not critical | |
1218 | } else if (mft_min > mft_new) { | |
1219 | mft_min = mft_new; | |
1220 | mi_min = mi_new; | |
1221 | } else { | |
1222 | ntfs_mark_rec_free(sbi, mft_new); | |
1223 | mft_new = 0; | |
1224 | ni_remove_mi(ni, mi_new); | |
1225 | } | |
1226 | ||
1227 | attr = mi_find_attr(&ni->mi, NULL, ATTR_DATA, NULL, 0, NULL); | |
1228 | if (!attr) { | |
1229 | err = -EINVAL; | |
1230 | goto out; | |
1231 | } | |
1232 | ||
1233 | asize = le32_to_cpu(attr->size); | |
1234 | ||
1235 | evcn = le64_to_cpu(attr->nres.evcn); | |
1236 | svcn = bytes_to_cluster(sbi, (u64)(mft_min + 1) << sbi->record_bits); | |
1237 | if (evcn + 1 >= svcn) { | |
1238 | err = -EINVAL; | |
1239 | goto out; | |
1240 | } | |
1241 | ||
1242 | /* | |
1243 | * split primary attribute [0 evcn] in two parts [0 svcn) + [svcn evcn] | |
1244 | * | |
1245 | * Update first part of ATTR_DATA in 'primary MFT | |
1246 | */ | |
1247 | err = run_pack(run, 0, svcn, Add2Ptr(attr, SIZEOF_NONRESIDENT), | |
1248 | asize - SIZEOF_NONRESIDENT, &plen); | |
1249 | if (err < 0) | |
1250 | goto out; | |
1251 | ||
1252 | run_size = QuadAlign(err); | |
1253 | err = 0; | |
1254 | ||
1255 | if (plen < svcn) { | |
1256 | err = -EINVAL; | |
1257 | goto out; | |
1258 | } | |
1259 | ||
1260 | attr->nres.evcn = cpu_to_le64(svcn - 1); | |
1261 | attr->size = cpu_to_le32(run_size + SIZEOF_NONRESIDENT); | |
1262 | /* 'done' - how many bytes of primary MFT becomes free */ | |
1263 | done = asize - run_size - SIZEOF_NONRESIDENT; | |
1264 | le32_sub_cpu(&ni->mi.mrec->used, done); | |
1265 | ||
1266 | /* Estimate the size of second part: run_buf=NULL */ | |
1267 | err = run_pack(run, svcn, evcn + 1 - svcn, NULL, sbi->record_size, | |
1268 | &plen); | |
1269 | if (err < 0) | |
1270 | goto out; | |
1271 | ||
1272 | run_size = QuadAlign(err); | |
1273 | err = 0; | |
1274 | ||
1275 | if (plen < evcn + 1 - svcn) { | |
1276 | err = -EINVAL; | |
1277 | goto out; | |
1278 | } | |
1279 | ||
1280 | /* | |
1281 | * This function may implicitly call expand attr_list | |
1282 | * Insert second part of ATTR_DATA in 'mi_min' | |
1283 | */ | |
1284 | attr = ni_ins_new_attr(ni, mi_min, NULL, ATTR_DATA, NULL, 0, | |
1285 | SIZEOF_NONRESIDENT + run_size, | |
1286 | SIZEOF_NONRESIDENT, svcn); | |
1287 | if (!attr) { | |
1288 | err = -EINVAL; | |
1289 | goto out; | |
1290 | } | |
1291 | ||
1292 | attr->non_res = 1; | |
1293 | attr->name_off = SIZEOF_NONRESIDENT_LE; | |
1294 | attr->flags = 0; | |
1295 | ||
1296 | run_pack(run, svcn, evcn + 1 - svcn, Add2Ptr(attr, SIZEOF_NONRESIDENT), | |
1297 | run_size, &plen); | |
1298 | ||
1299 | attr->nres.svcn = cpu_to_le64(svcn); | |
1300 | attr->nres.evcn = cpu_to_le64(evcn); | |
1301 | attr->nres.run_off = cpu_to_le16(SIZEOF_NONRESIDENT); | |
1302 | ||
1303 | out: | |
1304 | if (mft_new) { | |
1305 | ntfs_mark_rec_free(sbi, mft_new); | |
1306 | ni_remove_mi(ni, mi_new); | |
1307 | } | |
1308 | ||
1309 | return !err && !done ? -EOPNOTSUPP : err; | |
1310 | } | |
1311 | ||
1312 | /* | |
1313 | * ni_expand_list | |
1314 | * | |
1315 | * This method moves all possible attributes out of primary record | |
1316 | */ | |
1317 | int ni_expand_list(struct ntfs_inode *ni) | |
1318 | { | |
1319 | int err = 0; | |
1320 | u32 asize, done = 0; | |
1321 | struct ATTRIB *attr, *ins_attr; | |
1322 | struct ATTR_LIST_ENTRY *le; | |
1323 | bool is_mft = ni->mi.rno == MFT_REC_MFT; | |
1324 | struct MFT_REF ref; | |
1325 | ||
1326 | mi_get_ref(&ni->mi, &ref); | |
1327 | le = NULL; | |
1328 | ||
1329 | while ((le = al_enumerate(ni, le))) { | |
1330 | if (le->type == ATTR_STD) | |
1331 | continue; | |
1332 | ||
1333 | if (memcmp(&ref, &le->ref, sizeof(struct MFT_REF))) | |
1334 | continue; | |
1335 | ||
1336 | if (is_mft && le->type == ATTR_DATA) | |
1337 | continue; | |
1338 | ||
1339 | /* Find attribute in primary record */ | |
1340 | attr = rec_find_attr_le(&ni->mi, le); | |
1341 | if (!attr) { | |
1342 | err = -EINVAL; | |
1343 | goto out; | |
1344 | } | |
1345 | ||
1346 | asize = le32_to_cpu(attr->size); | |
1347 | ||
1348 | /* Always insert into new record to avoid collisions (deep recursive) */ | |
1349 | err = ni_ins_attr_ext(ni, le, attr->type, attr_name(attr), | |
1350 | attr->name_len, asize, attr_svcn(attr), | |
1351 | le16_to_cpu(attr->name_off), true, | |
1352 | &ins_attr, NULL); | |
1353 | ||
1354 | if (err) | |
1355 | goto out; | |
1356 | ||
1357 | memcpy(ins_attr, attr, asize); | |
1358 | ins_attr->id = le->id; | |
1359 | mi_remove_attr(&ni->mi, attr); | |
1360 | ||
1361 | done += asize; | |
1362 | goto out; | |
1363 | } | |
1364 | ||
1365 | if (!is_mft) { | |
1366 | err = -EFBIG; /* attr list is too big(?) */ | |
1367 | goto out; | |
1368 | } | |
1369 | ||
1370 | /* split mft data as much as possible */ | |
1371 | err = ni_expand_mft_list(ni); | |
1372 | if (err) | |
1373 | goto out; | |
1374 | ||
1375 | out: | |
1376 | return !err && !done ? -EOPNOTSUPP : err; | |
1377 | } | |
1378 | ||
1379 | /* | |
1380 | * ni_insert_nonresident | |
1381 | * | |
1382 | * inserts new nonresident attribute | |
1383 | */ | |
1384 | int ni_insert_nonresident(struct ntfs_inode *ni, enum ATTR_TYPE type, | |
1385 | const __le16 *name, u8 name_len, | |
1386 | const struct runs_tree *run, CLST svcn, CLST len, | |
1387 | __le16 flags, struct ATTRIB **new_attr, | |
1388 | struct mft_inode **mi) | |
1389 | { | |
1390 | int err; | |
1391 | CLST plen; | |
1392 | struct ATTRIB *attr; | |
1393 | bool is_ext = | |
1394 | (flags & (ATTR_FLAG_SPARSED | ATTR_FLAG_COMPRESSED)) && !svcn; | |
1395 | u32 name_size = QuadAlign(name_len * sizeof(short)); | |
1396 | u32 name_off = is_ext ? SIZEOF_NONRESIDENT_EX : SIZEOF_NONRESIDENT; | |
1397 | u32 run_off = name_off + name_size; | |
1398 | u32 run_size, asize; | |
1399 | struct ntfs_sb_info *sbi = ni->mi.sbi; | |
1400 | ||
1401 | err = run_pack(run, svcn, len, NULL, sbi->max_bytes_per_attr - run_off, | |
1402 | &plen); | |
1403 | if (err < 0) | |
1404 | goto out; | |
1405 | ||
1406 | run_size = QuadAlign(err); | |
1407 | ||
1408 | if (plen < len) { | |
1409 | err = -EINVAL; | |
1410 | goto out; | |
1411 | } | |
1412 | ||
1413 | asize = run_off + run_size; | |
1414 | ||
1415 | if (asize > sbi->max_bytes_per_attr) { | |
1416 | err = -EINVAL; | |
1417 | goto out; | |
1418 | } | |
1419 | ||
1420 | err = ni_insert_attr(ni, type, name, name_len, asize, name_off, svcn, | |
1421 | &attr, mi); | |
1422 | ||
1423 | if (err) | |
1424 | goto out; | |
1425 | ||
1426 | attr->non_res = 1; | |
1427 | attr->name_off = cpu_to_le16(name_off); | |
1428 | attr->flags = flags; | |
1429 | ||
1430 | run_pack(run, svcn, len, Add2Ptr(attr, run_off), run_size, &plen); | |
1431 | ||
1432 | attr->nres.svcn = cpu_to_le64(svcn); | |
1433 | attr->nres.evcn = cpu_to_le64((u64)svcn + len - 1); | |
1434 | ||
1435 | err = 0; | |
1436 | if (new_attr) | |
1437 | *new_attr = attr; | |
1438 | ||
1439 | *(__le64 *)&attr->nres.run_off = cpu_to_le64(run_off); | |
1440 | ||
1441 | attr->nres.alloc_size = | |
1442 | svcn ? 0 : cpu_to_le64((u64)len << ni->mi.sbi->cluster_bits); | |
1443 | attr->nres.data_size = attr->nres.alloc_size; | |
1444 | attr->nres.valid_size = attr->nres.alloc_size; | |
1445 | ||
1446 | if (is_ext) { | |
1447 | if (flags & ATTR_FLAG_COMPRESSED) | |
1448 | attr->nres.c_unit = COMPRESSION_UNIT; | |
1449 | attr->nres.total_size = attr->nres.alloc_size; | |
1450 | } | |
1451 | ||
1452 | out: | |
1453 | return err; | |
1454 | } | |
1455 | ||
1456 | /* | |
1457 | * ni_insert_resident | |
1458 | * | |
1459 | * inserts new resident attribute | |
1460 | */ | |
1461 | int ni_insert_resident(struct ntfs_inode *ni, u32 data_size, | |
1462 | enum ATTR_TYPE type, const __le16 *name, u8 name_len, | |
1463 | struct ATTRIB **new_attr, struct mft_inode **mi) | |
1464 | { | |
1465 | int err; | |
1466 | u32 name_size = QuadAlign(name_len * sizeof(short)); | |
1467 | u32 asize = SIZEOF_RESIDENT + name_size + QuadAlign(data_size); | |
1468 | struct ATTRIB *attr; | |
1469 | ||
1470 | err = ni_insert_attr(ni, type, name, name_len, asize, SIZEOF_RESIDENT, | |
1471 | 0, &attr, mi); | |
1472 | if (err) | |
1473 | return err; | |
1474 | ||
1475 | attr->non_res = 0; | |
1476 | attr->flags = 0; | |
1477 | ||
1478 | attr->res.data_size = cpu_to_le32(data_size); | |
1479 | attr->res.data_off = cpu_to_le16(SIZEOF_RESIDENT + name_size); | |
1480 | if (type == ATTR_NAME) | |
1481 | attr->res.flags = RESIDENT_FLAG_INDEXED; | |
1482 | attr->res.res = 0; | |
1483 | ||
1484 | if (new_attr) | |
1485 | *new_attr = attr; | |
1486 | ||
1487 | return 0; | |
1488 | } | |
1489 | ||
1490 | /* | |
1491 | * ni_remove_attr_le | |
1492 | * | |
1493 | * removes attribute from record | |
1494 | */ | |
1495 | int ni_remove_attr_le(struct ntfs_inode *ni, struct ATTRIB *attr, | |
1496 | struct ATTR_LIST_ENTRY *le) | |
1497 | { | |
1498 | int err; | |
1499 | struct mft_inode *mi; | |
1500 | ||
1501 | err = ni_load_mi(ni, le, &mi); | |
1502 | if (err) | |
1503 | return err; | |
1504 | ||
1505 | mi_remove_attr(mi, attr); | |
1506 | ||
1507 | if (le) | |
1508 | al_remove_le(ni, le); | |
1509 | ||
1510 | return 0; | |
1511 | } | |
1512 | ||
1513 | /* | |
1514 | * ni_delete_all | |
1515 | * | |
1516 | * removes all attributes and frees allocates space | |
1517 | * ntfs_evict_inode->ntfs_clear_inode->ni_delete_all (if no links) | |
1518 | */ | |
1519 | int ni_delete_all(struct ntfs_inode *ni) | |
1520 | { | |
1521 | int err; | |
1522 | struct ATTR_LIST_ENTRY *le = NULL; | |
1523 | struct ATTRIB *attr = NULL; | |
1524 | struct rb_node *node; | |
1525 | u16 roff; | |
1526 | u32 asize; | |
1527 | CLST svcn, evcn; | |
1528 | struct ntfs_sb_info *sbi = ni->mi.sbi; | |
1529 | bool nt3 = is_ntfs3(sbi); | |
1530 | struct MFT_REF ref; | |
1531 | ||
1532 | while ((attr = ni_enum_attr_ex(ni, attr, &le, NULL))) { | |
1533 | if (!nt3 || attr->name_len) { | |
1534 | ; | |
1535 | } else if (attr->type == ATTR_REPARSE) { | |
1536 | mi_get_ref(&ni->mi, &ref); | |
1537 | ntfs_remove_reparse(sbi, 0, &ref); | |
1538 | } else if (attr->type == ATTR_ID && !attr->non_res && | |
1539 | le32_to_cpu(attr->res.data_size) >= | |
1540 | sizeof(struct GUID)) { | |
1541 | ntfs_objid_remove(sbi, resident_data(attr)); | |
1542 | } | |
1543 | ||
1544 | if (!attr->non_res) | |
1545 | continue; | |
1546 | ||
1547 | svcn = le64_to_cpu(attr->nres.svcn); | |
1548 | evcn = le64_to_cpu(attr->nres.evcn); | |
1549 | ||
1550 | if (evcn + 1 <= svcn) | |
1551 | continue; | |
1552 | ||
1553 | asize = le32_to_cpu(attr->size); | |
1554 | roff = le16_to_cpu(attr->nres.run_off); | |
1555 | ||
1556 | /*run==1 means unpack and deallocate*/ | |
1557 | run_unpack_ex(RUN_DEALLOCATE, sbi, ni->mi.rno, svcn, evcn, svcn, | |
1558 | Add2Ptr(attr, roff), asize - roff); | |
1559 | } | |
1560 | ||
1561 | if (ni->attr_list.size) { | |
1562 | run_deallocate(ni->mi.sbi, &ni->attr_list.run, true); | |
1563 | al_destroy(ni); | |
1564 | } | |
1565 | ||
1566 | /* Free all subrecords */ | |
1567 | for (node = rb_first(&ni->mi_tree); node;) { | |
1568 | struct rb_node *next = rb_next(node); | |
1569 | struct mft_inode *mi = rb_entry(node, struct mft_inode, node); | |
1570 | ||
1571 | clear_rec_inuse(mi->mrec); | |
1572 | mi->dirty = true; | |
1573 | mi_write(mi, 0); | |
1574 | ||
1575 | ntfs_mark_rec_free(sbi, mi->rno); | |
1576 | ni_remove_mi(ni, mi); | |
1577 | mi_put(mi); | |
1578 | node = next; | |
1579 | } | |
1580 | ||
1581 | // Free base record | |
1582 | clear_rec_inuse(ni->mi.mrec); | |
1583 | ni->mi.dirty = true; | |
1584 | err = mi_write(&ni->mi, 0); | |
1585 | ||
1586 | ntfs_mark_rec_free(sbi, ni->mi.rno); | |
1587 | ||
1588 | return err; | |
1589 | } | |
1590 | ||
1591 | /* | |
1592 | * ni_fname_name | |
1593 | * | |
1594 | * returns file name attribute by its value | |
1595 | */ | |
1596 | struct ATTR_FILE_NAME *ni_fname_name(struct ntfs_inode *ni, | |
1597 | const struct cpu_str *uni, | |
1598 | const struct MFT_REF *home_dir, | |
1599 | struct ATTR_LIST_ENTRY **le) | |
1600 | { | |
1601 | struct ATTRIB *attr = NULL; | |
1602 | struct ATTR_FILE_NAME *fname; | |
1603 | ||
1604 | *le = NULL; | |
1605 | ||
1606 | /* Enumerate all names */ | |
1607 | next: | |
1608 | attr = ni_find_attr(ni, attr, le, ATTR_NAME, NULL, 0, NULL, NULL); | |
1609 | if (!attr) | |
1610 | return NULL; | |
1611 | ||
1612 | fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME); | |
1613 | if (!fname) | |
1614 | goto next; | |
1615 | ||
1616 | if (home_dir && memcmp(home_dir, &fname->home, sizeof(*home_dir))) | |
1617 | goto next; | |
1618 | ||
1619 | if (!uni) | |
1620 | goto next; | |
1621 | ||
1622 | if (uni->len != fname->name_len) | |
1623 | goto next; | |
1624 | ||
1625 | if (ntfs_cmp_names_cpu(uni, (struct le_str *)&fname->name_len, NULL, | |
1626 | false)) | |
1627 | goto next; | |
1628 | ||
1629 | return fname; | |
1630 | } | |
1631 | ||
1632 | /* | |
1633 | * ni_fname_type | |
1634 | * | |
1635 | * returns file name attribute with given type | |
1636 | */ | |
1637 | struct ATTR_FILE_NAME *ni_fname_type(struct ntfs_inode *ni, u8 name_type, | |
1638 | struct ATTR_LIST_ENTRY **le) | |
1639 | { | |
1640 | struct ATTRIB *attr = NULL; | |
1641 | struct ATTR_FILE_NAME *fname; | |
1642 | ||
1643 | *le = NULL; | |
1644 | ||
1645 | /* Enumerate all names */ | |
1646 | for (;;) { | |
1647 | attr = ni_find_attr(ni, attr, le, ATTR_NAME, NULL, 0, NULL, | |
1648 | NULL); | |
1649 | if (!attr) | |
1650 | return NULL; | |
1651 | ||
1652 | fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME); | |
1653 | if (fname && name_type == fname->type) | |
1654 | return fname; | |
1655 | } | |
1656 | } | |
1657 | ||
1658 | /* | |
1659 | * Process compressed/sparsed in special way | |
1660 | * NOTE: you need to set ni->std_fa = new_fa | |
1661 | * after this function to keep internal structures in consistency | |
1662 | */ | |
1663 | int ni_new_attr_flags(struct ntfs_inode *ni, enum FILE_ATTRIBUTE new_fa) | |
1664 | { | |
1665 | struct ATTRIB *attr; | |
1666 | struct mft_inode *mi; | |
1667 | __le16 new_aflags; | |
1668 | u32 new_asize; | |
1669 | ||
1670 | attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi); | |
1671 | if (!attr) | |
1672 | return -EINVAL; | |
1673 | ||
1674 | new_aflags = attr->flags; | |
1675 | ||
1676 | if (new_fa & FILE_ATTRIBUTE_SPARSE_FILE) | |
1677 | new_aflags |= ATTR_FLAG_SPARSED; | |
1678 | else | |
1679 | new_aflags &= ~ATTR_FLAG_SPARSED; | |
1680 | ||
1681 | if (new_fa & FILE_ATTRIBUTE_COMPRESSED) | |
1682 | new_aflags |= ATTR_FLAG_COMPRESSED; | |
1683 | else | |
1684 | new_aflags &= ~ATTR_FLAG_COMPRESSED; | |
1685 | ||
1686 | if (new_aflags == attr->flags) | |
1687 | return 0; | |
1688 | ||
1689 | if ((new_aflags & (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED)) == | |
1690 | (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED)) { | |
1691 | ntfs_inode_warn(&ni->vfs_inode, | |
1692 | "file can't be sparsed and compressed"); | |
1693 | return -EOPNOTSUPP; | |
1694 | } | |
1695 | ||
1696 | if (!attr->non_res) | |
1697 | goto out; | |
1698 | ||
1699 | if (attr->nres.data_size) { | |
1700 | ntfs_inode_warn( | |
1701 | &ni->vfs_inode, | |
1702 | "one can change sparsed/compressed only for empty files"); | |
1703 | return -EOPNOTSUPP; | |
1704 | } | |
1705 | ||
1706 | /* resize nonresident empty attribute in-place only*/ | |
1707 | new_asize = (new_aflags & (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED)) | |
1708 | ? (SIZEOF_NONRESIDENT_EX + 8) | |
1709 | : (SIZEOF_NONRESIDENT + 8); | |
1710 | ||
1711 | if (!mi_resize_attr(mi, attr, new_asize - le32_to_cpu(attr->size))) | |
1712 | return -EOPNOTSUPP; | |
1713 | ||
1714 | if (new_aflags & ATTR_FLAG_SPARSED) { | |
1715 | attr->name_off = SIZEOF_NONRESIDENT_EX_LE; | |
1716 | /* windows uses 16 clusters per frame but supports one cluster per frame too*/ | |
1717 | attr->nres.c_unit = 0; | |
1718 | ni->vfs_inode.i_mapping->a_ops = &ntfs_aops; | |
1719 | } else if (new_aflags & ATTR_FLAG_COMPRESSED) { | |
1720 | attr->name_off = SIZEOF_NONRESIDENT_EX_LE; | |
1721 | /* the only allowed: 16 clusters per frame */ | |
1722 | attr->nres.c_unit = NTFS_LZNT_CUNIT; | |
1723 | ni->vfs_inode.i_mapping->a_ops = &ntfs_aops_cmpr; | |
1724 | } else { | |
1725 | attr->name_off = SIZEOF_NONRESIDENT_LE; | |
1726 | /* normal files */ | |
1727 | attr->nres.c_unit = 0; | |
1728 | ni->vfs_inode.i_mapping->a_ops = &ntfs_aops; | |
1729 | } | |
1730 | attr->nres.run_off = attr->name_off; | |
1731 | out: | |
1732 | attr->flags = new_aflags; | |
1733 | mi->dirty = true; | |
1734 | ||
1735 | return 0; | |
1736 | } | |
1737 | ||
1738 | /* | |
1739 | * ni_parse_reparse | |
1740 | * | |
1741 | * buffer is at least 24 bytes | |
1742 | */ | |
1743 | enum REPARSE_SIGN ni_parse_reparse(struct ntfs_inode *ni, struct ATTRIB *attr, | |
1744 | void *buffer) | |
1745 | { | |
1746 | const struct REPARSE_DATA_BUFFER *rp = NULL; | |
1747 | u8 bits; | |
1748 | u16 len; | |
1749 | typeof(rp->CompressReparseBuffer) *cmpr; | |
1750 | ||
1751 | static_assert(sizeof(struct REPARSE_DATA_BUFFER) <= 24); | |
1752 | ||
1753 | /* Try to estimate reparse point */ | |
1754 | if (!attr->non_res) { | |
1755 | rp = resident_data_ex(attr, sizeof(struct REPARSE_DATA_BUFFER)); | |
1756 | } else if (le64_to_cpu(attr->nres.data_size) >= | |
1757 | sizeof(struct REPARSE_DATA_BUFFER)) { | |
1758 | struct runs_tree run; | |
1759 | ||
1760 | run_init(&run); | |
1761 | ||
1762 | if (!attr_load_runs_vcn(ni, ATTR_REPARSE, NULL, 0, &run, 0) && | |
1763 | !ntfs_read_run_nb(ni->mi.sbi, &run, 0, buffer, | |
1764 | sizeof(struct REPARSE_DATA_BUFFER), | |
1765 | NULL)) { | |
1766 | rp = buffer; | |
1767 | } | |
1768 | ||
1769 | run_close(&run); | |
1770 | } | |
1771 | ||
1772 | if (!rp) | |
1773 | return REPARSE_NONE; | |
1774 | ||
1775 | len = le16_to_cpu(rp->ReparseDataLength); | |
1776 | switch (rp->ReparseTag) { | |
1777 | case (IO_REPARSE_TAG_MICROSOFT | IO_REPARSE_TAG_SYMBOLIC_LINK): | |
1778 | break; /* Symbolic link */ | |
1779 | case IO_REPARSE_TAG_MOUNT_POINT: | |
1780 | break; /* Mount points and junctions */ | |
1781 | case IO_REPARSE_TAG_SYMLINK: | |
1782 | break; | |
1783 | case IO_REPARSE_TAG_COMPRESS: | |
1784 | /* | |
1785 | * WOF - Windows Overlay Filter - used to compress files with lzx/xpress | |
1786 | * Unlike native NTFS file compression, the Windows Overlay Filter supports | |
1787 |