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