GNU Linux-libre 5.15.137-gnu
[releases.git] / fs / ntfs3 / record.c
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
14 static 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  */
34 static __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
71 int 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
95 void mi_put(struct mft_inode *mi)
96 {
97         mi_clear(mi);
98         kfree(mi);
99 }
100
101 int 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  */
115 int 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
173 ok:
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
182 out:
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
192 struct 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                 attr = Add2Ptr(attr, asize);
230                 off += asize;
231         }
232
233         asize = le32_to_cpu(attr->size);
234
235         /* Can we use the first field (attr->type). */
236         if (off + 8 > used) {
237                 static_assert(ALIGN(sizeof(enum ATTR_TYPE), 8) == 8);
238                 return NULL;
239         }
240
241         if (attr->type == ATTR_END) {
242                 /* End of enumeration. */
243                 return NULL;
244         }
245
246         /* 0x100 is last known attribute for now. */
247         t32 = le32_to_cpu(attr->type);
248         if ((t32 & 0xf) || (t32 > 0x100))
249                 return NULL;
250
251         /* Check overflow and boundary. */
252         if (off + asize < off || off + asize > used)
253                 return NULL;
254
255         /* Check size of attribute. */
256         if (!attr->non_res) {
257                 if (asize < SIZEOF_RESIDENT)
258                         return NULL;
259
260                 t16 = le16_to_cpu(attr->res.data_off);
261
262                 if (t16 > asize)
263                         return NULL;
264
265                 t32 = le32_to_cpu(attr->res.data_size);
266                 if (t16 + t32 > asize)
267                         return NULL;
268
269                 if (attr->name_len &&
270                     le16_to_cpu(attr->name_off) + sizeof(short) * attr->name_len > t16) {
271                         return NULL;
272                 }
273
274                 return attr;
275         }
276
277         /* Check some nonresident fields. */
278         if (attr->name_len &&
279             le16_to_cpu(attr->name_off) + sizeof(short) * attr->name_len >
280                     le16_to_cpu(attr->nres.run_off)) {
281                 return NULL;
282         }
283
284         if (attr->nres.svcn || !is_attr_ext(attr)) {
285                 if (asize + 8 < SIZEOF_NONRESIDENT)
286                         return NULL;
287
288                 if (attr->nres.c_unit)
289                         return NULL;
290         } else if (asize + 8 < SIZEOF_NONRESIDENT_EX)
291                 return NULL;
292
293         return attr;
294 }
295
296 /*
297  * mi_find_attr - Find the attribute by type and name and id.
298  */
299 struct ATTRIB *mi_find_attr(struct mft_inode *mi, struct ATTRIB *attr,
300                             enum ATTR_TYPE type, const __le16 *name,
301                             size_t name_len, const __le16 *id)
302 {
303         u32 type_in = le32_to_cpu(type);
304         u32 atype;
305
306 next_attr:
307         attr = mi_enum_attr(mi, attr);
308         if (!attr)
309                 return NULL;
310
311         atype = le32_to_cpu(attr->type);
312         if (atype > type_in)
313                 return NULL;
314
315         if (atype < type_in)
316                 goto next_attr;
317
318         if (attr->name_len != name_len)
319                 goto next_attr;
320
321         if (name_len && memcmp(attr_name(attr), name, name_len * sizeof(short)))
322                 goto next_attr;
323
324         if (id && *id != attr->id)
325                 goto next_attr;
326
327         return attr;
328 }
329
330 int mi_write(struct mft_inode *mi, int wait)
331 {
332         struct MFT_REC *rec;
333         int err;
334         struct ntfs_sb_info *sbi;
335
336         if (!mi->dirty)
337                 return 0;
338
339         sbi = mi->sbi;
340         rec = mi->mrec;
341
342         err = ntfs_write_bh(sbi, &rec->rhdr, &mi->nb, wait);
343         if (err)
344                 return err;
345
346         if (mi->rno < sbi->mft.recs_mirr)
347                 sbi->flags |= NTFS_FLAGS_MFTMIRR;
348
349         mi->dirty = false;
350
351         return 0;
352 }
353
354 int mi_format_new(struct mft_inode *mi, struct ntfs_sb_info *sbi, CLST rno,
355                   __le16 flags, bool is_mft)
356 {
357         int err;
358         u16 seq = 1;
359         struct MFT_REC *rec;
360         u64 vbo = (u64)rno << sbi->record_bits;
361
362         err = mi_init(mi, sbi, rno);
363         if (err)
364                 return err;
365
366         rec = mi->mrec;
367
368         if (rno == MFT_REC_MFT) {
369                 ;
370         } else if (rno < MFT_REC_FREE) {
371                 seq = rno;
372         } else if (rno >= sbi->mft.used) {
373                 ;
374         } else if (mi_read(mi, is_mft)) {
375                 ;
376         } else if (rec->rhdr.sign == NTFS_FILE_SIGNATURE) {
377                 /* Record is reused. Update its sequence number. */
378                 seq = le16_to_cpu(rec->seq) + 1;
379                 if (!seq)
380                         seq = 1;
381         }
382
383         memcpy(rec, sbi->new_rec, sbi->record_size);
384
385         rec->seq = cpu_to_le16(seq);
386         rec->flags = RECORD_FLAG_IN_USE | flags;
387
388         mi->dirty = true;
389
390         if (!mi->nb.nbufs) {
391                 struct ntfs_inode *ni = sbi->mft.ni;
392                 bool lock = false;
393
394                 if (is_mounted(sbi) && !is_mft) {
395                         down_read(&ni->file.run_lock);
396                         lock = true;
397                 }
398
399                 err = ntfs_get_bh(sbi, &ni->file.run, vbo, sbi->record_size,
400                                   &mi->nb);
401                 if (lock)
402                         up_read(&ni->file.run_lock);
403         }
404
405         return err;
406 }
407
408 /*
409  * mi_mark_free - Mark 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 - Reserve space for new attribute.
432  *
433  * Return: Not full constructed attribute or NULL if not possible to create.
434  */
435 struct ATTRIB *mi_insert_attr(struct mft_inode *mi, enum ATTR_TYPE type,
436                               const __le16 *name, u8 name_len, u32 asize,
437                               u16 name_off)
438 {
439         size_t tail;
440         struct ATTRIB *attr;
441         __le16 id;
442         struct MFT_REC *rec = mi->mrec;
443         struct ntfs_sb_info *sbi = mi->sbi;
444         u32 used = le32_to_cpu(rec->used);
445         const u16 *upcase = sbi->upcase;
446         int diff;
447
448         /* Can we insert mi attribute? */
449         if (used + asize > mi->sbi->record_size)
450                 return NULL;
451
452         /*
453          * Scan through the list of attributes to find the point
454          * at which we should insert it.
455          */
456         attr = NULL;
457         while ((attr = mi_enum_attr(mi, attr))) {
458                 diff = compare_attr(attr, type, name, name_len, upcase);
459                 if (diff > 0)
460                         break;
461                 if (diff < 0)
462                         continue;
463
464                 if (!is_attr_indexed(attr))
465                         return NULL;
466                 break;
467         }
468
469         if (!attr) {
470                 tail = 8; /* Not used, just to suppress warning. */
471                 attr = Add2Ptr(rec, used - 8);
472         } else {
473                 tail = used - PtrOffset(rec, attr);
474         }
475
476         id = mi_new_attt_id(mi);
477
478         memmove(Add2Ptr(attr, asize), attr, tail);
479         memset(attr, 0, asize);
480
481         attr->type = type;
482         attr->size = cpu_to_le32(asize);
483         attr->name_len = name_len;
484         attr->name_off = cpu_to_le16(name_off);
485         attr->id = id;
486
487         memmove(Add2Ptr(attr, name_off), name, name_len * sizeof(short));
488         rec->used = cpu_to_le32(used + asize);
489
490         mi->dirty = true;
491
492         return attr;
493 }
494
495 /*
496  * mi_remove_attr - Remove the attribute from record.
497  *
498  * NOTE: The source attr will point to next attribute.
499  */
500 bool mi_remove_attr(struct ntfs_inode *ni, struct mft_inode *mi,
501                     struct ATTRIB *attr)
502 {
503         struct MFT_REC *rec = mi->mrec;
504         u32 aoff = PtrOffset(rec, attr);
505         u32 used = le32_to_cpu(rec->used);
506         u32 asize = le32_to_cpu(attr->size);
507
508         if (aoff + asize > used)
509                 return false;
510
511         if (ni && is_attr_indexed(attr)) {
512                 le16_add_cpu(&ni->mi.mrec->hard_links, -1);
513                 ni->mi.dirty = true;
514         }
515
516         used -= asize;
517         memmove(attr, Add2Ptr(attr, asize), used - aoff);
518         rec->used = cpu_to_le32(used);
519         mi->dirty = true;
520
521         return true;
522 }
523
524 /* bytes = "new attribute size" - "old attribute size" */
525 bool mi_resize_attr(struct mft_inode *mi, struct ATTRIB *attr, int bytes)
526 {
527         struct MFT_REC *rec = mi->mrec;
528         u32 aoff = PtrOffset(rec, attr);
529         u32 total, used = le32_to_cpu(rec->used);
530         u32 nsize, asize = le32_to_cpu(attr->size);
531         u32 rsize = le32_to_cpu(attr->res.data_size);
532         int tail = (int)(used - aoff - asize);
533         int dsize;
534         char *next;
535
536         if (tail < 0 || aoff >= used)
537                 return false;
538
539         if (!bytes)
540                 return true;
541
542         total = le32_to_cpu(rec->total);
543         next = Add2Ptr(attr, asize);
544
545         if (bytes > 0) {
546                 dsize = ALIGN(bytes, 8);
547                 if (used + dsize > total)
548                         return false;
549                 nsize = asize + dsize;
550                 /* Move tail */
551                 memmove(next + dsize, next, tail);
552                 memset(next, 0, dsize);
553                 used += dsize;
554                 rsize += dsize;
555         } else {
556                 dsize = ALIGN(-bytes, 8);
557                 if (dsize > asize)
558                         return false;
559                 nsize = asize - dsize;
560                 memmove(next - dsize, next, tail);
561                 used -= dsize;
562                 rsize -= dsize;
563         }
564
565         rec->used = cpu_to_le32(used);
566         attr->size = cpu_to_le32(nsize);
567         if (!attr->non_res)
568                 attr->res.data_size = cpu_to_le32(rsize);
569         mi->dirty = true;
570
571         return true;
572 }
573
574 int mi_pack_runs(struct mft_inode *mi, struct ATTRIB *attr,
575                  struct runs_tree *run, CLST len)
576 {
577         int err = 0;
578         struct ntfs_sb_info *sbi = mi->sbi;
579         u32 new_run_size;
580         CLST plen;
581         struct MFT_REC *rec = mi->mrec;
582         CLST svcn = le64_to_cpu(attr->nres.svcn);
583         u32 used = le32_to_cpu(rec->used);
584         u32 aoff = PtrOffset(rec, attr);
585         u32 asize = le32_to_cpu(attr->size);
586         char *next = Add2Ptr(attr, asize);
587         u16 run_off = le16_to_cpu(attr->nres.run_off);
588         u32 run_size = asize - run_off;
589         u32 tail = used - aoff - asize;
590         u32 dsize = sbi->record_size - used;
591
592         /* Make a maximum gap in current record. */
593         memmove(next + dsize, next, tail);
594
595         /* Pack as much as possible. */
596         err = run_pack(run, svcn, len, Add2Ptr(attr, run_off), run_size + dsize,
597                        &plen);
598         if (err < 0) {
599                 memmove(next, next + dsize, tail);
600                 return err;
601         }
602
603         new_run_size = ALIGN(err, 8);
604
605         memmove(next + new_run_size - run_size, next + dsize, tail);
606
607         attr->size = cpu_to_le32(asize + new_run_size - run_size);
608         attr->nres.evcn = cpu_to_le64(svcn + plen - 1);
609         rec->used = cpu_to_le32(used + new_run_size - run_size);
610         mi->dirty = true;
611
612         return 0;
613 }