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