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