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>
20 * Return: True if @le is valid.
22 static inline bool al_is_valid_le(const struct ntfs_inode *ni,
23 struct ATTR_LIST_ENTRY *le)
25 if (!le || !ni->attr_list.le || !ni->attr_list.size)
28 return PtrOffset(ni->attr_list.le, le) + le16_to_cpu(le->size) <=
32 void al_destroy(struct ntfs_inode *ni)
34 run_close(&ni->attr_list.run);
35 kfree(ni->attr_list.le);
36 ni->attr_list.le = NULL;
37 ni->attr_list.size = 0;
38 ni->attr_list.dirty = false;
44 * This method makes sure that the ATTRIB list, if present,
45 * has been properly set up.
47 int ntfs_load_attr_list(struct ntfs_inode *ni, struct ATTRIB *attr)
53 if (ni->attr_list.size)
57 lsize = le32_to_cpu(attr->res.data_size);
58 le = kmalloc(al_aligned(lsize), GFP_NOFS);
63 memcpy(le, resident_data(attr), lsize);
64 } else if (attr->nres.svcn) {
68 u16 run_off = le16_to_cpu(attr->nres.run_off);
70 lsize = le64_to_cpu(attr->nres.data_size);
72 run_init(&ni->attr_list.run);
74 err = run_unpack_ex(&ni->attr_list.run, ni->mi.sbi, ni->mi.rno,
75 0, le64_to_cpu(attr->nres.evcn), 0,
76 Add2Ptr(attr, run_off),
77 le32_to_cpu(attr->size) - run_off);
81 le = kmalloc(al_aligned(lsize), GFP_NOFS);
87 err = ntfs_read_run_nb(ni->mi.sbi, &ni->attr_list.run, 0, le,
93 ni->attr_list.size = lsize;
94 ni->attr_list.le = le;
99 ni->attr_list.le = le;
109 * * The next list le.
110 * * If @le is NULL then return the first le.
112 struct ATTR_LIST_ENTRY *al_enumerate(struct ntfs_inode *ni,
113 struct ATTR_LIST_ENTRY *le)
119 le = ni->attr_list.le;
121 sz = le16_to_cpu(le->size);
122 if (sz < sizeof(struct ATTR_LIST_ENTRY)) {
123 /* Impossible 'cause we should not return such le. */
126 le = Add2Ptr(le, sz);
129 /* Check boundary. */
130 off = PtrOffset(ni->attr_list.le, le);
131 if (off + sizeof(struct ATTR_LIST_ENTRY) > ni->attr_list.size) {
132 /* The regular end of list. */
136 sz = le16_to_cpu(le->size);
138 /* Check le for errors. */
139 if (sz < sizeof(struct ATTR_LIST_ENTRY) ||
140 off + sz > ni->attr_list.size ||
141 sz < le->name_off + le->name_len * sizeof(short)) {
151 * Find the first le in the list which matches type, name and VCN.
153 * Return: NULL if not found.
155 struct ATTR_LIST_ENTRY *al_find_le(struct ntfs_inode *ni,
156 struct ATTR_LIST_ENTRY *le,
157 const struct ATTRIB *attr)
159 CLST svcn = attr_svcn(attr);
161 return al_find_ex(ni, le, attr->type, attr_name(attr), attr->name_len,
168 * Find the first le in the list which matches type, name and VCN.
170 * Return: NULL if not found.
172 struct ATTR_LIST_ENTRY *al_find_ex(struct ntfs_inode *ni,
173 struct ATTR_LIST_ENTRY *le,
174 enum ATTR_TYPE type, const __le16 *name,
175 u8 name_len, const CLST *vcn)
177 struct ATTR_LIST_ENTRY *ret = NULL;
178 u32 type_in = le32_to_cpu(type);
180 while ((le = al_enumerate(ni, le))) {
182 int diff = le32_to_cpu(le->type) - type_in;
184 /* List entries are sorted by type, name and VCN. */
191 if (le->name_len != name_len)
194 le_vcn = le64_to_cpu(le->vcn);
197 * Compare entry names only for entry with vcn == 0.
199 diff = ntfs_cmp_names(le_name(le), name_len, name,
200 name_len, ni->mi.sbi->upcase,
225 * al_find_le_to_insert
227 * Find the first list entry which matches type, name and VCN.
229 static struct ATTR_LIST_ENTRY *al_find_le_to_insert(struct ntfs_inode *ni,
232 u8 name_len, CLST vcn)
234 struct ATTR_LIST_ENTRY *le = NULL, *prev;
235 u32 type_in = le32_to_cpu(type);
237 /* List entries are sorted by type, name and VCN. */
238 while ((le = al_enumerate(ni, prev = le))) {
239 int diff = le32_to_cpu(le->type) - type_in;
249 * Compare entry names only for entry with vcn == 0.
251 diff = ntfs_cmp_names(le_name(le), le->name_len, name,
252 name_len, ni->mi.sbi->upcase,
261 if (le64_to_cpu(le->vcn) >= vcn)
265 return prev ? Add2Ptr(prev, le16_to_cpu(prev->size)) : ni->attr_list.le;
271 * Add an "attribute list entry" to the list.
273 int al_add_le(struct ntfs_inode *ni, enum ATTR_TYPE type, const __le16 *name,
274 u8 name_len, CLST svcn, __le16 id, const struct MFT_REF *ref,
275 struct ATTR_LIST_ENTRY **new_le)
279 struct ATTR_LIST_ENTRY *le;
282 size_t asize, new_asize, old_size;
284 typeof(ni->attr_list) *al = &ni->attr_list;
287 * Compute the size of the new 'le'
289 sz = le_size(name_len);
291 new_size = old_size + sz;
292 asize = al_aligned(old_size);
293 new_asize = al_aligned(new_size);
295 /* Scan forward to the point at which the new 'le' should be inserted. */
296 le = al_find_le_to_insert(ni, type, name, name_len, svcn);
297 off = PtrOffset(al->le, le);
299 if (new_size > asize) {
300 void *ptr = kmalloc(new_asize, GFP_NOFS);
305 memcpy(ptr, al->le, off);
306 memcpy(Add2Ptr(ptr, off + sz), le, old_size - off);
307 le = Add2Ptr(ptr, off);
311 memmove(Add2Ptr(le, sz), le, old_size - off);
318 le->size = cpu_to_le16(sz);
319 le->name_len = name_len;
320 le->name_off = offsetof(struct ATTR_LIST_ENTRY, name);
321 le->vcn = cpu_to_le64(svcn);
324 memcpy(le->name, name, sizeof(short) * name_len);
326 err = attr_set_size(ni, ATTR_LIST, NULL, 0, &al->run, new_size,
327 &new_size, true, &attr);
329 /* Undo memmove above. */
330 memmove(le, Add2Ptr(le, sz), old_size - off);
337 if (attr && attr->non_res) {
338 err = ntfs_sb_write_run(ni->mi.sbi, &al->run, 0, al->le,
349 * al_remove_le - Remove @le from attribute list.
351 bool al_remove_le(struct ntfs_inode *ni, struct ATTR_LIST_ENTRY *le)
355 typeof(ni->attr_list) *al = &ni->attr_list;
357 if (!al_is_valid_le(ni, le))
360 /* Save on stack the size of 'le' */
361 size = le16_to_cpu(le->size);
362 off = PtrOffset(al->le, le);
364 memmove(le, Add2Ptr(le, size), al->size - (off + size));
373 * al_delete_le - Delete first le from the list which matches its parameters.
375 bool al_delete_le(struct ntfs_inode *ni, enum ATTR_TYPE type, CLST vcn,
376 const __le16 *name, size_t name_len,
377 const struct MFT_REF *ref)
380 struct ATTR_LIST_ENTRY *le;
382 typeof(ni->attr_list) *al = &ni->attr_list;
384 /* Scan forward to the first le that matches the input. */
385 le = al_find_ex(ni, NULL, type, name, name_len, &vcn);
389 off = PtrOffset(al->le, le);
394 if (le->type != type)
396 if (le->name_len != name_len)
398 if (name_len && ntfs_cmp_names(le_name(le), name_len, name, name_len,
399 ni->mi.sbi->upcase, true))
401 if (le64_to_cpu(le->vcn) != vcn)
405 * The caller specified a segment reference, so we have to
406 * scan through the matching entries until we find that segment
407 * reference or we run of matching entries.
409 if (ref && memcmp(ref, &le->ref, sizeof(*ref))) {
410 off += le16_to_cpu(le->size);
411 le = Add2Ptr(al->le, off);
415 /* Save on stack the size of 'le'. */
416 size = le16_to_cpu(le->size);
418 memmove(le, Add2Ptr(le, size), al->size - (off + size));
426 int al_update(struct ntfs_inode *ni)
430 typeof(ni->attr_list) *al = &ni->attr_list;
432 if (!al->dirty || !al->size)
436 * Attribute list increased on demand in al_add_le.
437 * Attribute list decreased here.
439 err = attr_set_size(ni, ATTR_LIST, NULL, 0, &al->run, al->size, NULL,
444 if (!attr->non_res) {
445 memcpy(resident_data(attr), al->le, al->size);
447 err = ntfs_sb_write_run(ni->mi.sbi, &al->run, 0, al->le,
452 attr->nres.valid_size = attr->nres.data_size;