1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
14 static inline int compare_attr(const struct ATTRIB *left, enum ATTR_TYPE type,
15 const __le16 *name, u8 name_len,
18 /* First, compare the type codes. */
19 int diff = le32_to_cpu(left->type) - le32_to_cpu(type);
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,
32 * Return: Unused attribute id that is less than mrec->next_attr_id.
34 static __le16 mi_new_attt_id(struct mft_inode *mi)
36 u16 free_id, max_id, t16;
37 struct MFT_REC *rec = mi->mrec;
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);
48 /* One record can store up to 1024/24 ~= 42 attributes. */
55 attr = mi_enum_attr(mi, attr);
57 rec->next_attr_id = cpu_to_le16(max_id + 1);
59 return cpu_to_le16(free_id);
62 t16 = le16_to_cpu(attr->id);
66 } else if (max_id < t16)
71 int mi_get(struct ntfs_sb_info *sbi, CLST rno, struct mft_inode **mi)
74 struct mft_inode *m = kzalloc(sizeof(struct mft_inode), GFP_NOFS);
79 err = mi_init(m, sbi, rno);
85 err = mi_read(m, false);
95 void mi_put(struct mft_inode *mi)
101 int mi_init(struct mft_inode *mi, struct ntfs_sb_info *sbi, CLST rno)
105 mi->mrec = kmalloc(sbi->record_size, GFP_NOFS);
113 * mi_read - Read MFT data.
115 int mi_read(struct mft_inode *mi, bool is_mft)
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;
126 if (is_mounted(sbi)) {
127 if (!is_mft && mft_ni) {
128 rw_lock = &mft_ni->file.run_lock;
133 err = ntfs_read_bh(sbi, run, vbo, &rec->rhdr, bpr, &mi->nb);
139 if (err == -E_NTFS_FIXUP) {
151 err = attr_load_runs_vcn(mft_ni, ATTR_DATA, NULL, 0, run,
152 vbo >> sbi->cluster_bits);
162 err = ntfs_read_bh(sbi, run, vbo, &rec->rhdr, bpr, &mi->nb);
166 if (err == -E_NTFS_FIXUP) {
174 /* Check field 'total' only here. */
175 if (le32_to_cpu(rec->total) != bpr) {
183 if (err == -E_NTFS_CORRUPT) {
184 ntfs_err(sbi->sb, "mft corrupted");
185 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
193 * mi_enum_attr - start/continue attributes enumeration in record.
195 * NOTE: mi->mrec - memory of size sbi->record_size
196 * here we sure that mi->mrec->total == sbi->record_size (see mi_read)
198 struct ATTRIB *mi_enum_attr(struct mft_inode *mi, struct ATTRIB *attr)
200 const struct MFT_REC *rec = mi->mrec;
201 u32 used = le32_to_cpu(rec->used);
202 u32 t32, off, asize, prev_type;
204 u64 data_size, alloc_size, tot_size;
207 u32 total = le32_to_cpu(rec->total);
209 off = le16_to_cpu(rec->attr_off);
214 if (off >= used || off < MFTRECORD_FIXUP_OFFSET_1 ||
215 !IS_ALIGNED(off, 4)) {
219 /* Skip non-resident records. */
220 if (!is_rec_inuse(rec))
224 attr = Add2Ptr(rec, off);
226 /* Check if input attr inside record. */
227 off = PtrOffset(rec, attr);
231 asize = le32_to_cpu(attr->size);
232 if (asize < SIZEOF_RESIDENT) {
233 /* Impossible 'cause we should not return such attribute. */
237 /* Overflow check. */
238 if (off + asize < off)
241 prev_type = le32_to_cpu(attr->type);
242 attr = Add2Ptr(attr, asize);
246 asize = le32_to_cpu(attr->size);
248 /* Can we use the first field (attr->type). */
249 if (off + 8 > used) {
250 static_assert(ALIGN(sizeof(enum ATTR_TYPE), 8) == 8);
254 if (attr->type == ATTR_END) {
255 /* End of enumeration. */
259 /* 0x100 is last known attribute for now. */
260 t32 = le32_to_cpu(attr->type);
261 if (!t32 || (t32 & 0xf) || (t32 > 0x100))
264 /* attributes in record must be ordered by type */
268 /* Check overflow and boundary. */
269 if (off + asize < off || off + asize > used)
272 /* Check size of attribute. */
273 if (!attr->non_res) {
274 /* Check resident fields. */
275 if (asize < SIZEOF_RESIDENT)
278 t16 = le16_to_cpu(attr->res.data_off);
282 if (t16 + le32_to_cpu(attr->res.data_size) > asize)
285 t32 = sizeof(short) * attr->name_len;
286 if (t32 && le16_to_cpu(attr->name_off) + t32 > t16)
292 /* Check nonresident fields. */
293 if (attr->non_res != 1)
296 t16 = le16_to_cpu(attr->nres.run_off);
300 t32 = sizeof(short) * attr->name_len;
301 if (t32 && le16_to_cpu(attr->name_off) + t32 > t16)
304 /* Check start/end vcn. */
305 if (le64_to_cpu(attr->nres.svcn) > le64_to_cpu(attr->nres.evcn) + 1)
308 data_size = le64_to_cpu(attr->nres.data_size);
309 if (le64_to_cpu(attr->nres.valid_size) > data_size)
312 alloc_size = le64_to_cpu(attr->nres.alloc_size);
313 if (data_size > alloc_size)
316 t32 = mi->sbi->cluster_mask;
317 if (alloc_size & t32)
320 if (!attr->nres.svcn && is_attr_ext(attr)) {
321 /* First segment of sparse/compressed attribute */
322 if (asize + 8 < SIZEOF_NONRESIDENT_EX)
325 tot_size = le64_to_cpu(attr->nres.total_size);
329 if (tot_size > alloc_size)
332 if (asize + 8 < SIZEOF_NONRESIDENT)
335 if (attr->nres.c_unit)
343 * mi_find_attr - Find the attribute by type and name and id.
345 struct ATTRIB *mi_find_attr(struct mft_inode *mi, struct ATTRIB *attr,
346 enum ATTR_TYPE type, const __le16 *name,
347 u8 name_len, const __le16 *id)
349 u32 type_in = le32_to_cpu(type);
353 attr = mi_enum_attr(mi, attr);
357 atype = le32_to_cpu(attr->type);
364 if (attr->name_len != name_len)
367 if (name_len && memcmp(attr_name(attr), name, name_len * sizeof(short)))
370 if (id && *id != attr->id)
376 int mi_write(struct mft_inode *mi, int wait)
380 struct ntfs_sb_info *sbi;
388 err = ntfs_write_bh(sbi, &rec->rhdr, &mi->nb, wait);
392 if (mi->rno < sbi->mft.recs_mirr)
393 sbi->flags |= NTFS_FLAGS_MFTMIRR;
400 int mi_format_new(struct mft_inode *mi, struct ntfs_sb_info *sbi, CLST rno,
401 __le16 flags, bool is_mft)
406 u64 vbo = (u64)rno << sbi->record_bits;
408 err = mi_init(mi, sbi, rno);
414 if (rno == MFT_REC_MFT) {
416 } else if (rno < MFT_REC_FREE) {
418 } else if (rno >= sbi->mft.used) {
420 } else if (mi_read(mi, is_mft)) {
422 } else if (rec->rhdr.sign == NTFS_FILE_SIGNATURE) {
423 /* Record is reused. Update its sequence number. */
424 seq = le16_to_cpu(rec->seq) + 1;
429 memcpy(rec, sbi->new_rec, sbi->record_size);
431 rec->seq = cpu_to_le16(seq);
432 rec->flags = RECORD_FLAG_IN_USE | flags;
433 if (MFTRECORD_FIXUP_OFFSET == MFTRECORD_FIXUP_OFFSET_3)
434 rec->mft_record = cpu_to_le32(rno);
439 struct ntfs_inode *ni = sbi->mft.ni;
442 if (is_mounted(sbi) && !is_mft) {
443 down_read(&ni->file.run_lock);
447 err = ntfs_get_bh(sbi, &ni->file.run, vbo, sbi->record_size,
450 up_read(&ni->file.run_lock);
457 * mi_insert_attr - Reserve space for new attribute.
459 * Return: Not full constructed attribute or NULL if not possible to create.
461 struct ATTRIB *mi_insert_attr(struct mft_inode *mi, enum ATTR_TYPE type,
462 const __le16 *name, u8 name_len, u32 asize,
468 struct MFT_REC *rec = mi->mrec;
469 struct ntfs_sb_info *sbi = mi->sbi;
470 u32 used = le32_to_cpu(rec->used);
471 const u16 *upcase = sbi->upcase;
473 /* Can we insert mi attribute? */
474 if (used + asize > sbi->record_size)
478 * Scan through the list of attributes to find the point
479 * at which we should insert it.
482 while ((attr = mi_enum_attr(mi, attr))) {
483 int diff = compare_attr(attr, type, name, name_len, upcase);
488 if (!diff && !is_attr_indexed(attr))
496 attr = Add2Ptr(rec, used - 8);
498 /* Insert before 'attr'. */
499 tail = used - PtrOffset(rec, attr);
502 id = mi_new_attt_id(mi);
504 memmove(Add2Ptr(attr, asize), attr, tail);
505 memset(attr, 0, asize);
508 attr->size = cpu_to_le32(asize);
509 attr->name_len = name_len;
510 attr->name_off = cpu_to_le16(name_off);
513 memmove(Add2Ptr(attr, name_off), name, name_len * sizeof(short));
514 rec->used = cpu_to_le32(used + asize);
522 * mi_remove_attr - Remove the attribute from record.
524 * NOTE: The source attr will point to next attribute.
526 bool mi_remove_attr(struct ntfs_inode *ni, struct mft_inode *mi,
529 struct MFT_REC *rec = mi->mrec;
530 u32 aoff = PtrOffset(rec, attr);
531 u32 used = le32_to_cpu(rec->used);
532 u32 asize = le32_to_cpu(attr->size);
534 if (aoff + asize > used)
537 if (ni && is_attr_indexed(attr)) {
538 le16_add_cpu(&ni->mi.mrec->hard_links, -1);
543 memmove(attr, Add2Ptr(attr, asize), used - aoff);
544 rec->used = cpu_to_le32(used);
550 /* bytes = "new attribute size" - "old attribute size" */
551 bool mi_resize_attr(struct mft_inode *mi, struct ATTRIB *attr, int bytes)
553 struct MFT_REC *rec = mi->mrec;
554 u32 aoff = PtrOffset(rec, attr);
555 u32 total, used = le32_to_cpu(rec->used);
556 u32 nsize, asize = le32_to_cpu(attr->size);
557 u32 rsize = le32_to_cpu(attr->res.data_size);
558 int tail = (int)(used - aoff - asize);
562 if (tail < 0 || aoff >= used)
568 total = le32_to_cpu(rec->total);
569 next = Add2Ptr(attr, asize);
572 dsize = ALIGN(bytes, 8);
573 if (used + dsize > total)
575 nsize = asize + dsize;
577 memmove(next + dsize, next, tail);
578 memset(next, 0, dsize);
582 dsize = ALIGN(-bytes, 8);
585 nsize = asize - dsize;
586 memmove(next - dsize, next, tail);
591 rec->used = cpu_to_le32(used);
592 attr->size = cpu_to_le32(nsize);
594 attr->res.data_size = cpu_to_le32(rsize);
601 * Pack runs in MFT record.
602 * If failed record is not changed.
604 int mi_pack_runs(struct mft_inode *mi, struct ATTRIB *attr,
605 struct runs_tree *run, CLST len)
608 struct ntfs_sb_info *sbi = mi->sbi;
611 struct MFT_REC *rec = mi->mrec;
612 CLST svcn = le64_to_cpu(attr->nres.svcn);
613 u32 used = le32_to_cpu(rec->used);
614 u32 aoff = PtrOffset(rec, attr);
615 u32 asize = le32_to_cpu(attr->size);
616 char *next = Add2Ptr(attr, asize);
617 u16 run_off = le16_to_cpu(attr->nres.run_off);
618 u32 run_size = asize - run_off;
619 u32 tail = used - aoff - asize;
620 u32 dsize = sbi->record_size - used;
622 /* Make a maximum gap in current record. */
623 memmove(next + dsize, next, tail);
625 /* Pack as much as possible. */
626 err = run_pack(run, svcn, len, Add2Ptr(attr, run_off), run_size + dsize,
629 memmove(next, next + dsize, tail);
633 new_run_size = ALIGN(err, 8);
635 memmove(next + new_run_size - run_size, next + dsize, tail);
637 attr->size = cpu_to_le32(asize + new_run_size - run_size);
638 attr->nres.evcn = cpu_to_le64(svcn + plen - 1);
639 rec->used = cpu_to_le32(used + new_run_size - run_size);