1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
8 #include <linux/blkdev.h>
9 #include <linux/buffer_head.h>
11 #include <linux/nls.h>
17 static inline int compare_attr(const struct ATTRIB *left, enum ATTR_TYPE type,
18 const __le16 *name, u8 name_len,
21 /* First, compare the type codes. */
22 int diff = le32_to_cpu(left->type) - le32_to_cpu(type);
27 /* They have the same type code, so we have to compare the names. */
28 return ntfs_cmp_names(attr_name(left), left->name_len, name, name_len,
35 * Return: Unused attribute id that is less than mrec->next_attr_id.
37 static __le16 mi_new_attt_id(struct mft_inode *mi)
39 u16 free_id, max_id, t16;
40 struct MFT_REC *rec = mi->mrec;
44 id = rec->next_attr_id;
45 free_id = le16_to_cpu(id);
46 if (free_id < 0x7FFF) {
47 rec->next_attr_id = cpu_to_le16(free_id + 1);
51 /* One record can store up to 1024/24 ~= 42 attributes. */
58 attr = mi_enum_attr(mi, attr);
60 rec->next_attr_id = cpu_to_le16(max_id + 1);
62 return cpu_to_le16(free_id);
65 t16 = le16_to_cpu(attr->id);
69 } else if (max_id < t16)
74 int mi_get(struct ntfs_sb_info *sbi, CLST rno, struct mft_inode **mi)
77 struct mft_inode *m = kzalloc(sizeof(struct mft_inode), GFP_NOFS);
82 err = mi_init(m, sbi, rno);
88 err = mi_read(m, false);
98 void mi_put(struct mft_inode *mi)
104 int mi_init(struct mft_inode *mi, struct ntfs_sb_info *sbi, CLST rno)
108 mi->mrec = kmalloc(sbi->record_size, GFP_NOFS);
116 * mi_read - Read MFT data.
118 int mi_read(struct mft_inode *mi, bool is_mft)
121 struct MFT_REC *rec = mi->mrec;
122 struct ntfs_sb_info *sbi = mi->sbi;
123 u32 bpr = sbi->record_size;
124 u64 vbo = (u64)mi->rno << sbi->record_bits;
125 struct ntfs_inode *mft_ni = sbi->mft.ni;
126 struct runs_tree *run = mft_ni ? &mft_ni->file.run : NULL;
127 struct rw_semaphore *rw_lock = NULL;
129 if (is_mounted(sbi)) {
131 rw_lock = &mft_ni->file.run_lock;
136 err = ntfs_read_bh(sbi, run, vbo, &rec->rhdr, bpr, &mi->nb);
142 if (err == -E_NTFS_FIXUP) {
154 err = attr_load_runs_vcn(mft_ni, ATTR_DATA, NULL, 0, &mft_ni->file.run,
155 vbo >> sbi->cluster_bits);
165 err = ntfs_read_bh(sbi, run, vbo, &rec->rhdr, bpr, &mi->nb);
169 if (err == -E_NTFS_FIXUP) {
177 /* Check field 'total' only here. */
178 if (le32_to_cpu(rec->total) != bpr) {
189 struct ATTRIB *mi_enum_attr(struct mft_inode *mi, struct ATTRIB *attr)
191 const struct MFT_REC *rec = mi->mrec;
192 u32 used = le32_to_cpu(rec->used);
197 u32 total = le32_to_cpu(rec->total);
199 off = le16_to_cpu(rec->attr_off);
204 if (off >= used || off < MFTRECORD_FIXUP_OFFSET_1 ||
205 !IS_ALIGNED(off, 4)) {
209 /* Skip non-resident records. */
210 if (!is_rec_inuse(rec))
213 attr = Add2Ptr(rec, off);
215 /* Check if input attr inside record. */
216 off = PtrOffset(rec, attr);
220 asize = le32_to_cpu(attr->size);
221 if (asize < SIZEOF_RESIDENT) {
222 /* Impossible 'cause we should not return such attribute. */
226 attr = Add2Ptr(attr, asize);
230 asize = le32_to_cpu(attr->size);
232 /* Can we use the first field (attr->type). */
233 if (off + 8 > used) {
234 static_assert(ALIGN(sizeof(enum ATTR_TYPE), 8) == 8);
238 if (attr->type == ATTR_END) {
239 /* End of enumeration. */
243 /* 0x100 is last known attribute for now. */
244 t32 = le32_to_cpu(attr->type);
245 if ((t32 & 0xf) || (t32 > 0x100))
248 /* Check boundary. */
249 if (off + asize > used)
252 /* Check size of attribute. */
253 if (!attr->non_res) {
254 if (asize < SIZEOF_RESIDENT)
257 t16 = le16_to_cpu(attr->res.data_off);
262 t32 = le32_to_cpu(attr->res.data_size);
263 if (t16 + t32 > asize)
269 /* Check some nonresident fields. */
270 if (attr->name_len &&
271 le16_to_cpu(attr->name_off) + sizeof(short) * attr->name_len >
272 le16_to_cpu(attr->nres.run_off)) {
276 if (attr->nres.svcn || !is_attr_ext(attr)) {
277 if (asize + 8 < SIZEOF_NONRESIDENT)
280 if (attr->nres.c_unit)
282 } else if (asize + 8 < SIZEOF_NONRESIDENT_EX)
289 * mi_find_attr - Find the attribute by type and name and id.
291 struct ATTRIB *mi_find_attr(struct mft_inode *mi, struct ATTRIB *attr,
292 enum ATTR_TYPE type, const __le16 *name,
293 size_t name_len, const __le16 *id)
295 u32 type_in = le32_to_cpu(type);
299 attr = mi_enum_attr(mi, attr);
303 atype = le32_to_cpu(attr->type);
310 if (attr->name_len != name_len)
313 if (name_len && memcmp(attr_name(attr), name, name_len * sizeof(short)))
316 if (id && *id != attr->id)
322 int mi_write(struct mft_inode *mi, int wait)
326 struct ntfs_sb_info *sbi;
334 err = ntfs_write_bh(sbi, &rec->rhdr, &mi->nb, wait);
338 if (mi->rno < sbi->mft.recs_mirr)
339 sbi->flags |= NTFS_FLAGS_MFTMIRR;
346 int mi_format_new(struct mft_inode *mi, struct ntfs_sb_info *sbi, CLST rno,
347 __le16 flags, bool is_mft)
352 u64 vbo = (u64)rno << sbi->record_bits;
354 err = mi_init(mi, sbi, rno);
360 if (rno == MFT_REC_MFT) {
362 } else if (rno < MFT_REC_FREE) {
364 } else if (rno >= sbi->mft.used) {
366 } else if (mi_read(mi, is_mft)) {
368 } else if (rec->rhdr.sign == NTFS_FILE_SIGNATURE) {
369 /* Record is reused. Update its sequence number. */
370 seq = le16_to_cpu(rec->seq) + 1;
375 memcpy(rec, sbi->new_rec, sbi->record_size);
377 rec->seq = cpu_to_le16(seq);
378 rec->flags = RECORD_FLAG_IN_USE | flags;
383 struct ntfs_inode *ni = sbi->mft.ni;
386 if (is_mounted(sbi) && !is_mft) {
387 down_read(&ni->file.run_lock);
391 err = ntfs_get_bh(sbi, &ni->file.run, vbo, sbi->record_size,
394 up_read(&ni->file.run_lock);
401 * mi_mark_free - Mark record as unused and marks it as free in bitmap.
403 void mi_mark_free(struct mft_inode *mi)
406 struct ntfs_sb_info *sbi = mi->sbi;
408 if (rno >= MFT_REC_RESERVED && rno < MFT_REC_FREE) {
409 ntfs_clear_mft_tail(sbi, rno, rno + 1);
415 clear_rec_inuse(mi->mrec);
419 ntfs_mark_rec_free(sbi, rno);
423 * mi_insert_attr - Reserve space for new attribute.
425 * Return: Not full constructed attribute or NULL if not possible to create.
427 struct ATTRIB *mi_insert_attr(struct mft_inode *mi, enum ATTR_TYPE type,
428 const __le16 *name, u8 name_len, u32 asize,
434 struct MFT_REC *rec = mi->mrec;
435 struct ntfs_sb_info *sbi = mi->sbi;
436 u32 used = le32_to_cpu(rec->used);
437 const u16 *upcase = sbi->upcase;
440 /* Can we insert mi attribute? */
441 if (used + asize > mi->sbi->record_size)
445 * Scan through the list of attributes to find the point
446 * at which we should insert it.
449 while ((attr = mi_enum_attr(mi, attr))) {
450 diff = compare_attr(attr, type, name, name_len, upcase);
456 if (!is_attr_indexed(attr))
462 tail = 8; /* Not used, just to suppress warning. */
463 attr = Add2Ptr(rec, used - 8);
465 tail = used - PtrOffset(rec, attr);
468 id = mi_new_attt_id(mi);
470 memmove(Add2Ptr(attr, asize), attr, tail);
471 memset(attr, 0, asize);
474 attr->size = cpu_to_le32(asize);
475 attr->name_len = name_len;
476 attr->name_off = cpu_to_le16(name_off);
479 memmove(Add2Ptr(attr, name_off), name, name_len * sizeof(short));
480 rec->used = cpu_to_le32(used + asize);
488 * mi_remove_attr - Remove the attribute from record.
490 * NOTE: The source attr will point to next attribute.
492 bool mi_remove_attr(struct ntfs_inode *ni, struct mft_inode *mi,
495 struct MFT_REC *rec = mi->mrec;
496 u32 aoff = PtrOffset(rec, attr);
497 u32 used = le32_to_cpu(rec->used);
498 u32 asize = le32_to_cpu(attr->size);
500 if (aoff + asize > used)
503 if (ni && is_attr_indexed(attr)) {
504 le16_add_cpu(&ni->mi.mrec->hard_links, -1);
509 memmove(attr, Add2Ptr(attr, asize), used - aoff);
510 rec->used = cpu_to_le32(used);
516 /* bytes = "new attribute size" - "old attribute size" */
517 bool mi_resize_attr(struct mft_inode *mi, struct ATTRIB *attr, int bytes)
519 struct MFT_REC *rec = mi->mrec;
520 u32 aoff = PtrOffset(rec, attr);
521 u32 total, used = le32_to_cpu(rec->used);
522 u32 nsize, asize = le32_to_cpu(attr->size);
523 u32 rsize = le32_to_cpu(attr->res.data_size);
524 int tail = (int)(used - aoff - asize);
528 if (tail < 0 || aoff >= used)
534 total = le32_to_cpu(rec->total);
535 next = Add2Ptr(attr, asize);
538 dsize = ALIGN(bytes, 8);
539 if (used + dsize > total)
541 nsize = asize + dsize;
543 memmove(next + dsize, next, tail);
544 memset(next, 0, dsize);
548 dsize = ALIGN(-bytes, 8);
551 nsize = asize - dsize;
552 memmove(next - dsize, next, tail);
557 rec->used = cpu_to_le32(used);
558 attr->size = cpu_to_le32(nsize);
560 attr->res.data_size = cpu_to_le32(rsize);
566 int mi_pack_runs(struct mft_inode *mi, struct ATTRIB *attr,
567 struct runs_tree *run, CLST len)
570 struct ntfs_sb_info *sbi = mi->sbi;
573 struct MFT_REC *rec = mi->mrec;
574 CLST svcn = le64_to_cpu(attr->nres.svcn);
575 u32 used = le32_to_cpu(rec->used);
576 u32 aoff = PtrOffset(rec, attr);
577 u32 asize = le32_to_cpu(attr->size);
578 char *next = Add2Ptr(attr, asize);
579 u16 run_off = le16_to_cpu(attr->nres.run_off);
580 u32 run_size = asize - run_off;
581 u32 tail = used - aoff - asize;
582 u32 dsize = sbi->record_size - used;
584 /* Make a maximum gap in current record. */
585 memmove(next + dsize, next, tail);
587 /* Pack as much as possible. */
588 err = run_pack(run, svcn, len, Add2Ptr(attr, run_off), run_size + dsize,
591 memmove(next, next + dsize, tail);
595 new_run_size = ALIGN(err, 8);
597 memmove(next + new_run_size - run_size, next + dsize, tail);
599 attr->size = cpu_to_le32(asize + new_run_size - run_size);
600 attr->nres.evcn = cpu_to_le64(svcn + plen - 1);
601 rec->used = cpu_to_le32(used + new_run_size - run_size);