Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2/*
3 *
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
5 *
6 */
7
8#include <linux/fs.h>
9
10#include "debug.h"
11#include "ntfs.h"
12#include "ntfs_fs.h"
13
14static inline int compare_attr(const struct ATTRIB *left, enum ATTR_TYPE type,
15 const __le16 *name, u8 name_len,
16 const u16 *upcase)
17{
18 /* First, compare the type codes. */
19 int diff = le32_to_cpu(left->type) - le32_to_cpu(type);
20
21 if (diff)
22 return diff;
23
24 /* They have the same type code, so we have to compare the names. */
25 return ntfs_cmp_names(attr_name(left), left->name_len, name, name_len,
26 upcase, true);
27}
28
29/*
30 * mi_new_attt_id
31 *
32 * Return: Unused attribute id that is less than mrec->next_attr_id.
33 */
34static __le16 mi_new_attt_id(struct mft_inode *mi)
35{
36 u16 free_id, max_id, t16;
37 struct MFT_REC *rec = mi->mrec;
38 struct ATTRIB *attr;
39 __le16 id;
40
41 id = rec->next_attr_id;
42 free_id = le16_to_cpu(id);
43 if (free_id < 0x7FFF) {
44 rec->next_attr_id = cpu_to_le16(free_id + 1);
45 return id;
46 }
47
48 /* One record can store up to 1024/24 ~= 42 attributes. */
49 free_id = 0;
50 max_id = 0;
51
52 attr = NULL;
53
54 for (;;) {
55 attr = mi_enum_attr(mi, attr);
56 if (!attr) {
57 rec->next_attr_id = cpu_to_le16(max_id + 1);
58 mi->dirty = true;
59 return cpu_to_le16(free_id);
60 }
61
62 t16 = le16_to_cpu(attr->id);
63 if (t16 == free_id) {
64 free_id += 1;
65 attr = NULL;
66 } else if (max_id < t16)
67 max_id = t16;
68 }
69}
70
71int mi_get(struct ntfs_sb_info *sbi, CLST rno, struct mft_inode **mi)
72{
73 int err;
74 struct mft_inode *m = kzalloc(sizeof(struct mft_inode), GFP_NOFS);
75
76 if (!m)
77 return -ENOMEM;
78
79 err = mi_init(m, sbi, rno);
80 if (err) {
81 kfree(m);
82 return err;
83 }
84
85 err = mi_read(m, false);
86 if (err) {
87 mi_put(m);
88 return err;
89 }
90
91 *mi = m;
92 return 0;
93}
94
95void mi_put(struct mft_inode *mi)
96{
97 mi_clear(mi);
98 kfree(mi);
99}
100
101int mi_init(struct mft_inode *mi, struct ntfs_sb_info *sbi, CLST rno)
102{
103 mi->sbi = sbi;
104 mi->rno = rno;
105 mi->mrec = kmalloc(sbi->record_size, GFP_NOFS);
106 if (!mi->mrec)
107 return -ENOMEM;
108
109 return 0;
110}
111
112/*
113 * mi_read - Read MFT data.
114 */
115int mi_read(struct mft_inode *mi, bool is_mft)
116{
117 int err;
118 struct MFT_REC *rec = mi->mrec;
119 struct ntfs_sb_info *sbi = mi->sbi;
120 u32 bpr = sbi->record_size;
121 u64 vbo = (u64)mi->rno << sbi->record_bits;
122 struct ntfs_inode *mft_ni = sbi->mft.ni;
123 struct runs_tree *run = mft_ni ? &mft_ni->file.run : NULL;
124 struct rw_semaphore *rw_lock = NULL;
125
126 if (is_mounted(sbi)) {
127 if (!is_mft && mft_ni) {
128 rw_lock = &mft_ni->file.run_lock;
129 down_read(rw_lock);
130 }
131 }
132
133 err = ntfs_read_bh(sbi, run, vbo, &rec->rhdr, bpr, &mi->nb);
134 if (rw_lock)
135 up_read(rw_lock);
136 if (!err)
137 goto ok;
138
139 if (err == -E_NTFS_FIXUP) {
140 mi->dirty = true;
141 goto ok;
142 }
143
144 if (err != -ENOENT)
145 goto out;
146
147 if (rw_lock) {
148 ni_lock(mft_ni);
149 down_write(rw_lock);
150 }
151 err = attr_load_runs_vcn(mft_ni, ATTR_DATA, NULL, 0, run,
152 vbo >> sbi->cluster_bits);
153 if (rw_lock) {
154 up_write(rw_lock);
155 ni_unlock(mft_ni);
156 }
157 if (err)
158 goto out;
159
160 if (rw_lock)
161 down_read(rw_lock);
162 err = ntfs_read_bh(sbi, run, vbo, &rec->rhdr, bpr, &mi->nb);
163 if (rw_lock)
164 up_read(rw_lock);
165
166 if (err == -E_NTFS_FIXUP) {
167 mi->dirty = true;
168 goto ok;
169 }
170 if (err)
171 goto out;
172
173ok:
174 /* Check field 'total' only here. */
175 if (le32_to_cpu(rec->total) != bpr) {
176 err = -EINVAL;
177 goto out;
178 }
179
180 return 0;
181
182out:
183 if (err == -E_NTFS_CORRUPT) {
184 ntfs_err(sbi->sb, "mft corrupted");
185 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
186 err = -EINVAL;
187 }
188
189 return err;
190}
191
192struct ATTRIB *mi_enum_attr(struct mft_inode *mi, struct ATTRIB *attr)
193{
194 const struct MFT_REC *rec = mi->mrec;
195 u32 used = le32_to_cpu(rec->used);
196 u32 t32, off, asize;
197 u16 t16;
198
199 if (!attr) {
200 u32 total = le32_to_cpu(rec->total);
201
202 off = le16_to_cpu(rec->attr_off);
203
204 if (used > total)
205 return NULL;
206
207 if (off >= used || off < MFTRECORD_FIXUP_OFFSET_1 ||
208 !IS_ALIGNED(off, 4)) {
209 return NULL;
210 }
211
212 /* Skip non-resident records. */
213 if (!is_rec_inuse(rec))
214 return NULL;
215
216 attr = Add2Ptr(rec, off);
217 } else {
218 /* Check if input attr inside record. */
219 off = PtrOffset(rec, attr);
220 if (off >= used)
221 return NULL;
222
223 asize = le32_to_cpu(attr->size);
224 if (asize < SIZEOF_RESIDENT) {
225 /* Impossible 'cause we should not return such attribute. */
226 return NULL;
227 }
228
229 if (off + asize < off) {
230 /* Overflow check. */
231 return NULL;
232 }
233
234 attr = Add2Ptr(attr, asize);
235 off += asize;
236 }
237
238 asize = le32_to_cpu(attr->size);
239
240 /* Can we use the first field (attr->type). */
241 if (off + 8 > used) {
242 static_assert(ALIGN(sizeof(enum ATTR_TYPE), 8) == 8);
243 return NULL;
244 }
245
246 if (attr->type == ATTR_END) {
247 /* End of enumeration. */
248 return NULL;
249 }
250
251 /* 0x100 is last known attribute for now. */
252 t32 = le32_to_cpu(attr->type);
253 if ((t32 & 0xf) || (t32 > 0x100))
254 return NULL;
255
256 /* Check overflow and boundary. */
257 if (off + asize < off || off + asize > used)
258 return NULL;
259
260 /* Check size of attribute. */
261 if (!attr->non_res) {
262 if (asize < SIZEOF_RESIDENT)
263 return NULL;
264
265 t16 = le16_to_cpu(attr->res.data_off);
266
267 if (t16 > asize)
268 return NULL;
269
270 t32 = le32_to_cpu(attr->res.data_size);
271 if (t16 + t32 > asize)
272 return NULL;
273
274 t32 = sizeof(short) * attr->name_len;
275 if (t32 && le16_to_cpu(attr->name_off) + t32 > t16)
276 return NULL;
277
278 return attr;
279 }
280
281 /* Check some nonresident fields. */
282 if (attr->name_len &&
283 le16_to_cpu(attr->name_off) + sizeof(short) * attr->name_len >
284 le16_to_cpu(attr->nres.run_off)) {
285 return NULL;
286 }
287
288 if (attr->nres.svcn || !is_attr_ext(attr)) {
289 if (asize + 8 < SIZEOF_NONRESIDENT)
290 return NULL;
291
292 if (attr->nres.c_unit)
293 return NULL;
294 } else if (asize + 8 < SIZEOF_NONRESIDENT_EX)
295 return NULL;
296
297 return attr;
298}
299
300/*
301 * mi_find_attr - Find the attribute by type and name and id.
302 */
303struct ATTRIB *mi_find_attr(struct mft_inode *mi, struct ATTRIB *attr,
304 enum ATTR_TYPE type, const __le16 *name,
305 u8 name_len, const __le16 *id)
306{
307 u32 type_in = le32_to_cpu(type);
308 u32 atype;
309
310next_attr:
311 attr = mi_enum_attr(mi, attr);
312 if (!attr)
313 return NULL;
314
315 atype = le32_to_cpu(attr->type);
316 if (atype > type_in)
317 return NULL;
318
319 if (atype < type_in)
320 goto next_attr;
321
322 if (attr->name_len != name_len)
323 goto next_attr;
324
325 if (name_len && memcmp(attr_name(attr), name, name_len * sizeof(short)))
326 goto next_attr;
327
328 if (id && *id != attr->id)
329 goto next_attr;
330
331 return attr;
332}
333
334int mi_write(struct mft_inode *mi, int wait)
335{
336 struct MFT_REC *rec;
337 int err;
338 struct ntfs_sb_info *sbi;
339
340 if (!mi->dirty)
341 return 0;
342
343 sbi = mi->sbi;
344 rec = mi->mrec;
345
346 err = ntfs_write_bh(sbi, &rec->rhdr, &mi->nb, wait);
347 if (err)
348 return err;
349
350 if (mi->rno < sbi->mft.recs_mirr)
351 sbi->flags |= NTFS_FLAGS_MFTMIRR;
352
353 mi->dirty = false;
354
355 return 0;
356}
357
358int mi_format_new(struct mft_inode *mi, struct ntfs_sb_info *sbi, CLST rno,
359 __le16 flags, bool is_mft)
360{
361 int err;
362 u16 seq = 1;
363 struct MFT_REC *rec;
364 u64 vbo = (u64)rno << sbi->record_bits;
365
366 err = mi_init(mi, sbi, rno);
367 if (err)
368 return err;
369
370 rec = mi->mrec;
371
372 if (rno == MFT_REC_MFT) {
373 ;
374 } else if (rno < MFT_REC_FREE) {
375 seq = rno;
376 } else if (rno >= sbi->mft.used) {
377 ;
378 } else if (mi_read(mi, is_mft)) {
379 ;
380 } else if (rec->rhdr.sign == NTFS_FILE_SIGNATURE) {
381 /* Record is reused. Update its sequence number. */
382 seq = le16_to_cpu(rec->seq) + 1;
383 if (!seq)
384 seq = 1;
385 }
386
387 memcpy(rec, sbi->new_rec, sbi->record_size);
388
389 rec->seq = cpu_to_le16(seq);
390 rec->flags = RECORD_FLAG_IN_USE | flags;
391 if (MFTRECORD_FIXUP_OFFSET == MFTRECORD_FIXUP_OFFSET_3)
392 rec->mft_record = cpu_to_le32(rno);
393
394 mi->dirty = true;
395
396 if (!mi->nb.nbufs) {
397 struct ntfs_inode *ni = sbi->mft.ni;
398 bool lock = false;
399
400 if (is_mounted(sbi) && !is_mft) {
401 down_read(&ni->file.run_lock);
402 lock = true;
403 }
404
405 err = ntfs_get_bh(sbi, &ni->file.run, vbo, sbi->record_size,
406 &mi->nb);
407 if (lock)
408 up_read(&ni->file.run_lock);
409 }
410
411 return err;
412}
413
414/*
415 * mi_insert_attr - Reserve space for new attribute.
416 *
417 * Return: Not full constructed attribute or NULL if not possible to create.
418 */
419struct ATTRIB *mi_insert_attr(struct mft_inode *mi, enum ATTR_TYPE type,
420 const __le16 *name, u8 name_len, u32 asize,
421 u16 name_off)
422{
423 size_t tail;
424 struct ATTRIB *attr;
425 __le16 id;
426 struct MFT_REC *rec = mi->mrec;
427 struct ntfs_sb_info *sbi = mi->sbi;
428 u32 used = le32_to_cpu(rec->used);
429 const u16 *upcase = sbi->upcase;
430
431 /* Can we insert mi attribute? */
432 if (used + asize > sbi->record_size)
433 return NULL;
434
435 /*
436 * Scan through the list of attributes to find the point
437 * at which we should insert it.
438 */
439 attr = NULL;
440 while ((attr = mi_enum_attr(mi, attr))) {
441 int diff = compare_attr(attr, type, name, name_len, upcase);
442
443 if (diff < 0)
444 continue;
445
446 if (!diff && !is_attr_indexed(attr))
447 return NULL;
448 break;
449 }
450
451 if (!attr) {
452 /* Append. */
453 tail = 8;
454 attr = Add2Ptr(rec, used - 8);
455 } else {
456 /* Insert before 'attr'. */
457 tail = used - PtrOffset(rec, attr);
458 }
459
460 id = mi_new_attt_id(mi);
461
462 memmove(Add2Ptr(attr, asize), attr, tail);
463 memset(attr, 0, asize);
464
465 attr->type = type;
466 attr->size = cpu_to_le32(asize);
467 attr->name_len = name_len;
468 attr->name_off = cpu_to_le16(name_off);
469 attr->id = id;
470
471 memmove(Add2Ptr(attr, name_off), name, name_len * sizeof(short));
472 rec->used = cpu_to_le32(used + asize);
473
474 mi->dirty = true;
475
476 return attr;
477}
478
479/*
480 * mi_remove_attr - Remove the attribute from record.
481 *
482 * NOTE: The source attr will point to next attribute.
483 */
484bool mi_remove_attr(struct ntfs_inode *ni, struct mft_inode *mi,
485 struct ATTRIB *attr)
486{
487 struct MFT_REC *rec = mi->mrec;
488 u32 aoff = PtrOffset(rec, attr);
489 u32 used = le32_to_cpu(rec->used);
490 u32 asize = le32_to_cpu(attr->size);
491
492 if (aoff + asize > used)
493 return false;
494
495 if (ni && is_attr_indexed(attr)) {
496 le16_add_cpu(&ni->mi.mrec->hard_links, -1);
497 ni->mi.dirty = true;
498 }
499
500 used -= asize;
501 memmove(attr, Add2Ptr(attr, asize), used - aoff);
502 rec->used = cpu_to_le32(used);
503 mi->dirty = true;
504
505 return true;
506}
507
508/* bytes = "new attribute size" - "old attribute size" */
509bool mi_resize_attr(struct mft_inode *mi, struct ATTRIB *attr, int bytes)
510{
511 struct MFT_REC *rec = mi->mrec;
512 u32 aoff = PtrOffset(rec, attr);
513 u32 total, used = le32_to_cpu(rec->used);
514 u32 nsize, asize = le32_to_cpu(attr->size);
515 u32 rsize = le32_to_cpu(attr->res.data_size);
516 int tail = (int)(used - aoff - asize);
517 int dsize;
518 char *next;
519
520 if (tail < 0 || aoff >= used)
521 return false;
522
523 if (!bytes)
524 return true;
525
526 total = le32_to_cpu(rec->total);
527 next = Add2Ptr(attr, asize);
528
529 if (bytes > 0) {
530 dsize = ALIGN(bytes, 8);
531 if (used + dsize > total)
532 return false;
533 nsize = asize + dsize;
534 /* Move tail */
535 memmove(next + dsize, next, tail);
536 memset(next, 0, dsize);
537 used += dsize;
538 rsize += dsize;
539 } else {
540 dsize = ALIGN(-bytes, 8);
541 if (dsize > asize)
542 return false;
543 nsize = asize - dsize;
544 memmove(next - dsize, next, tail);
545 used -= dsize;
546 rsize -= dsize;
547 }
548
549 rec->used = cpu_to_le32(used);
550 attr->size = cpu_to_le32(nsize);
551 if (!attr->non_res)
552 attr->res.data_size = cpu_to_le32(rsize);
553 mi->dirty = true;
554
555 return true;
556}
557
558/*
559 * Pack runs in MFT record.
560 * If failed record is not changed.
561 */
562int mi_pack_runs(struct mft_inode *mi, struct ATTRIB *attr,
563 struct runs_tree *run, CLST len)
564{
565 int err = 0;
566 struct ntfs_sb_info *sbi = mi->sbi;
567 u32 new_run_size;
568 CLST plen;
569 struct MFT_REC *rec = mi->mrec;
570 CLST svcn = le64_to_cpu(attr->nres.svcn);
571 u32 used = le32_to_cpu(rec->used);
572 u32 aoff = PtrOffset(rec, attr);
573 u32 asize = le32_to_cpu(attr->size);
574 char *next = Add2Ptr(attr, asize);
575 u16 run_off = le16_to_cpu(attr->nres.run_off);
576 u32 run_size = asize - run_off;
577 u32 tail = used - aoff - asize;
578 u32 dsize = sbi->record_size - used;
579
580 /* Make a maximum gap in current record. */
581 memmove(next + dsize, next, tail);
582
583 /* Pack as much as possible. */
584 err = run_pack(run, svcn, len, Add2Ptr(attr, run_off), run_size + dsize,
585 &plen);
586 if (err < 0) {
587 memmove(next, next + dsize, tail);
588 return err;
589 }
590
591 new_run_size = ALIGN(err, 8);
592
593 memmove(next + new_run_size - run_size, next + dsize, tail);
594
595 attr->size = cpu_to_le32(asize + new_run_size - run_size);
596 attr->nres.evcn = cpu_to_le64(svcn + plen - 1);
597 rec->used = cpu_to_le32(used + new_run_size - run_size);
598 mi->dirty = true;
599
600 return 0;
601}