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)) {
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, &mft_ni->file.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) {
186 struct ATTRIB *mi_enum_attr(struct mft_inode *mi, struct ATTRIB *attr)
188 const struct MFT_REC *rec = mi->mrec;
189 u32 used = le32_to_cpu(rec->used);
194 u32 total = le32_to_cpu(rec->total);
196 off = le16_to_cpu(rec->attr_off);
201 if (off >= used || off < MFTRECORD_FIXUP_OFFSET_1 ||
202 !IS_ALIGNED(off, 4)) {
206 /* Skip non-resident records. */
207 if (!is_rec_inuse(rec))
210 attr = Add2Ptr(rec, off);
212 /* Check if input attr inside record. */
213 off = PtrOffset(rec, attr);
217 asize = le32_to_cpu(attr->size);
218 if (asize < SIZEOF_RESIDENT) {
219 /* Impossible 'cause we should not return such attribute. */
223 attr = Add2Ptr(attr, asize);
227 asize = le32_to_cpu(attr->size);
229 /* Can we use the first field (attr->type). */
230 if (off + 8 > used) {
231 static_assert(ALIGN(sizeof(enum ATTR_TYPE), 8) == 8);
235 if (attr->type == ATTR_END) {
236 /* End of enumeration. */
240 /* 0x100 is last known attribute for now. */
241 t32 = le32_to_cpu(attr->type);
242 if ((t32 & 0xf) || (t32 > 0x100))
245 /* Check boundary. */
246 if (off + asize > used)
249 /* Check size of attribute. */
250 if (!attr->non_res) {
251 if (asize < SIZEOF_RESIDENT)
254 t16 = le16_to_cpu(attr->res.data_off);
259 t32 = le32_to_cpu(attr->res.data_size);
260 if (t16 + t32 > asize)
266 /* Check some nonresident fields. */
267 if (attr->name_len &&
268 le16_to_cpu(attr->name_off) + sizeof(short) * attr->name_len >
269 le16_to_cpu(attr->nres.run_off)) {
273 if (attr->nres.svcn || !is_attr_ext(attr)) {
274 if (asize + 8 < SIZEOF_NONRESIDENT)
277 if (attr->nres.c_unit)
279 } else if (asize + 8 < SIZEOF_NONRESIDENT_EX)
286 * mi_find_attr - Find the attribute by type and name and id.
288 struct ATTRIB *mi_find_attr(struct mft_inode *mi, struct ATTRIB *attr,
289 enum ATTR_TYPE type, const __le16 *name,
290 size_t name_len, const __le16 *id)
292 u32 type_in = le32_to_cpu(type);
296 attr = mi_enum_attr(mi, attr);
300 atype = le32_to_cpu(attr->type);
307 if (attr->name_len != name_len)
310 if (name_len && memcmp(attr_name(attr), name, name_len * sizeof(short)))
313 if (id && *id != attr->id)
319 int mi_write(struct mft_inode *mi, int wait)
323 struct ntfs_sb_info *sbi;
331 err = ntfs_write_bh(sbi, &rec->rhdr, &mi->nb, wait);
335 if (mi->rno < sbi->mft.recs_mirr)
336 sbi->flags |= NTFS_FLAGS_MFTMIRR;
343 int mi_format_new(struct mft_inode *mi, struct ntfs_sb_info *sbi, CLST rno,
344 __le16 flags, bool is_mft)
349 u64 vbo = (u64)rno << sbi->record_bits;
351 err = mi_init(mi, sbi, rno);
357 if (rno == MFT_REC_MFT) {
359 } else if (rno < MFT_REC_FREE) {
361 } else if (rno >= sbi->mft.used) {
363 } else if (mi_read(mi, is_mft)) {
365 } else if (rec->rhdr.sign == NTFS_FILE_SIGNATURE) {
366 /* Record is reused. Update its sequence number. */
367 seq = le16_to_cpu(rec->seq) + 1;
372 memcpy(rec, sbi->new_rec, sbi->record_size);
374 rec->seq = cpu_to_le16(seq);
375 rec->flags = RECORD_FLAG_IN_USE | flags;
380 struct ntfs_inode *ni = sbi->mft.ni;
383 if (is_mounted(sbi) && !is_mft) {
384 down_read(&ni->file.run_lock);
388 err = ntfs_get_bh(sbi, &ni->file.run, vbo, sbi->record_size,
391 up_read(&ni->file.run_lock);
398 * mi_mark_free - Mark record as unused and marks it as free in bitmap.
400 void mi_mark_free(struct mft_inode *mi)
403 struct ntfs_sb_info *sbi = mi->sbi;
405 if (rno >= MFT_REC_RESERVED && rno < MFT_REC_FREE) {
406 ntfs_clear_mft_tail(sbi, rno, rno + 1);
412 clear_rec_inuse(mi->mrec);
416 ntfs_mark_rec_free(sbi, rno);
420 * mi_insert_attr - Reserve space for new attribute.
422 * Return: Not full constructed attribute or NULL if not possible to create.
424 struct ATTRIB *mi_insert_attr(struct mft_inode *mi, enum ATTR_TYPE type,
425 const __le16 *name, u8 name_len, u32 asize,
431 struct MFT_REC *rec = mi->mrec;
432 struct ntfs_sb_info *sbi = mi->sbi;
433 u32 used = le32_to_cpu(rec->used);
434 const u16 *upcase = sbi->upcase;
437 /* Can we insert mi attribute? */
438 if (used + asize > mi->sbi->record_size)
442 * Scan through the list of attributes to find the point
443 * at which we should insert it.
446 while ((attr = mi_enum_attr(mi, attr))) {
447 diff = compare_attr(attr, type, name, name_len, upcase);
453 if (!is_attr_indexed(attr))
459 tail = 8; /* Not used, just to suppress warning. */
460 attr = Add2Ptr(rec, used - 8);
462 tail = used - PtrOffset(rec, attr);
465 id = mi_new_attt_id(mi);
467 memmove(Add2Ptr(attr, asize), attr, tail);
468 memset(attr, 0, asize);
471 attr->size = cpu_to_le32(asize);
472 attr->name_len = name_len;
473 attr->name_off = cpu_to_le16(name_off);
476 memmove(Add2Ptr(attr, name_off), name, name_len * sizeof(short));
477 rec->used = cpu_to_le32(used + asize);
485 * mi_remove_attr - Remove the attribute from record.
487 * NOTE: The source attr will point to next attribute.
489 bool mi_remove_attr(struct ntfs_inode *ni, struct mft_inode *mi,
492 struct MFT_REC *rec = mi->mrec;
493 u32 aoff = PtrOffset(rec, attr);
494 u32 used = le32_to_cpu(rec->used);
495 u32 asize = le32_to_cpu(attr->size);
497 if (aoff + asize > used)
500 if (ni && is_attr_indexed(attr)) {
501 le16_add_cpu(&ni->mi.mrec->hard_links, -1);
506 memmove(attr, Add2Ptr(attr, asize), used - aoff);
507 rec->used = cpu_to_le32(used);
513 /* bytes = "new attribute size" - "old attribute size" */
514 bool mi_resize_attr(struct mft_inode *mi, struct ATTRIB *attr, int bytes)
516 struct MFT_REC *rec = mi->mrec;
517 u32 aoff = PtrOffset(rec, attr);
518 u32 total, used = le32_to_cpu(rec->used);
519 u32 nsize, asize = le32_to_cpu(attr->size);
520 u32 rsize = le32_to_cpu(attr->res.data_size);
521 int tail = (int)(used - aoff - asize);
525 if (tail < 0 || aoff >= used)
531 total = le32_to_cpu(rec->total);
532 next = Add2Ptr(attr, asize);
535 dsize = ALIGN(bytes, 8);
536 if (used + dsize > total)
538 nsize = asize + dsize;
540 memmove(next + dsize, next, tail);
541 memset(next, 0, dsize);
545 dsize = ALIGN(-bytes, 8);
548 nsize = asize - dsize;
549 memmove(next - dsize, next, tail);
554 rec->used = cpu_to_le32(used);
555 attr->size = cpu_to_le32(nsize);
557 attr->res.data_size = cpu_to_le32(rsize);
563 int mi_pack_runs(struct mft_inode *mi, struct ATTRIB *attr,
564 struct runs_tree *run, CLST len)
567 struct ntfs_sb_info *sbi = mi->sbi;
570 struct MFT_REC *rec = mi->mrec;
571 CLST svcn = le64_to_cpu(attr->nres.svcn);
572 u32 used = le32_to_cpu(rec->used);
573 u32 aoff = PtrOffset(rec, attr);
574 u32 asize = le32_to_cpu(attr->size);
575 char *next = Add2Ptr(attr, asize);
576 u16 run_off = le16_to_cpu(attr->nres.run_off);
577 u32 run_size = asize - run_off;
578 u32 tail = used - aoff - asize;
579 u32 dsize = sbi->record_size - used;
581 /* Make a maximum gap in current record. */
582 memmove(next + dsize, next, tail);
584 /* Pack as much as possible. */
585 err = run_pack(run, svcn, len, Add2Ptr(attr, run_off), run_size + dsize,
588 memmove(next, next + dsize, tail);
592 new_run_size = ALIGN(err, 8);
594 memmove(next + new_run_size - run_size, next + dsize, tail);
596 attr->size = cpu_to_le32(asize + new_run_size - run_size);
597 attr->nres.evcn = cpu_to_le64(svcn + plen - 1);
598 rec->used = cpu_to_le32(used + new_run_size - run_size);