1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
17 * Return: True if @le is valid.
19 static inline bool al_is_valid_le(const struct ntfs_inode *ni,
20 struct ATTR_LIST_ENTRY *le)
22 if (!le || !ni->attr_list.le || !ni->attr_list.size)
25 return PtrOffset(ni->attr_list.le, le) + le16_to_cpu(le->size) <=
29 void al_destroy(struct ntfs_inode *ni)
31 run_close(&ni->attr_list.run);
32 kfree(ni->attr_list.le);
33 ni->attr_list.le = NULL;
34 ni->attr_list.size = 0;
35 ni->attr_list.dirty = false;
41 * This method makes sure that the ATTRIB list, if present,
42 * has been properly set up.
44 int ntfs_load_attr_list(struct ntfs_inode *ni, struct ATTRIB *attr)
50 if (ni->attr_list.size)
54 lsize = le32_to_cpu(attr->res.data_size);
55 le = kmalloc(al_aligned(lsize), GFP_NOFS);
60 memcpy(le, resident_data(attr), lsize);
61 } else if (attr->nres.svcn) {
65 u16 run_off = le16_to_cpu(attr->nres.run_off);
67 lsize = le64_to_cpu(attr->nres.data_size);
69 run_init(&ni->attr_list.run);
71 if (run_off > le32_to_cpu(attr->size)) {
76 err = run_unpack_ex(&ni->attr_list.run, ni->mi.sbi, ni->mi.rno,
77 0, le64_to_cpu(attr->nres.evcn), 0,
78 Add2Ptr(attr, run_off),
79 le32_to_cpu(attr->size) - run_off);
83 le = kmalloc(al_aligned(lsize), GFP_NOFS);
89 err = ntfs_read_run_nb(ni->mi.sbi, &ni->attr_list.run, 0, le,
95 ni->attr_list.size = lsize;
96 ni->attr_list.le = le;
101 ni->attr_list.le = le;
111 * * The next list le.
112 * * If @le is NULL then return the first le.
114 struct ATTR_LIST_ENTRY *al_enumerate(struct ntfs_inode *ni,
115 struct ATTR_LIST_ENTRY *le)
121 le = ni->attr_list.le;
123 sz = le16_to_cpu(le->size);
124 if (sz < sizeof(struct ATTR_LIST_ENTRY)) {
125 /* Impossible 'cause we should not return such le. */
128 le = Add2Ptr(le, sz);
131 /* Check boundary. */
132 off = PtrOffset(ni->attr_list.le, le);
133 if (off + sizeof(struct ATTR_LIST_ENTRY) > ni->attr_list.size) {
134 /* The regular end of list. */
138 sz = le16_to_cpu(le->size);
140 /* Check le for errors. */
141 if (sz < sizeof(struct ATTR_LIST_ENTRY) ||
142 off + sz > ni->attr_list.size ||
143 sz < le->name_off + le->name_len * sizeof(short)) {
153 * Find the first le in the list which matches type, name and VCN.
155 * Return: NULL if not found.
157 struct ATTR_LIST_ENTRY *al_find_le(struct ntfs_inode *ni,
158 struct ATTR_LIST_ENTRY *le,
159 const struct ATTRIB *attr)
161 CLST svcn = attr_svcn(attr);
163 return al_find_ex(ni, le, attr->type, attr_name(attr), attr->name_len,
170 * Find the first le in the list which matches type, name and VCN.
172 * Return: NULL if not found.
174 struct ATTR_LIST_ENTRY *al_find_ex(struct ntfs_inode *ni,
175 struct ATTR_LIST_ENTRY *le,
176 enum ATTR_TYPE type, const __le16 *name,
177 u8 name_len, const CLST *vcn)
179 struct ATTR_LIST_ENTRY *ret = NULL;
180 u32 type_in = le32_to_cpu(type);
182 while ((le = al_enumerate(ni, le))) {
184 int diff = le32_to_cpu(le->type) - type_in;
186 /* List entries are sorted by type, name and VCN. */
193 if (le->name_len != name_len)
196 le_vcn = le64_to_cpu(le->vcn);
199 * Compare entry names only for entry with vcn == 0.
201 diff = ntfs_cmp_names(le_name(le), name_len, name,
202 name_len, ni->mi.sbi->upcase,
227 * al_find_le_to_insert
229 * Find the first list entry which matches type, name and VCN.
231 static struct ATTR_LIST_ENTRY *al_find_le_to_insert(struct ntfs_inode *ni,
234 u8 name_len, CLST vcn)
236 struct ATTR_LIST_ENTRY *le = NULL, *prev;
237 u32 type_in = le32_to_cpu(type);
239 /* List entries are sorted by type, name and VCN. */
240 while ((le = al_enumerate(ni, prev = le))) {
241 int diff = le32_to_cpu(le->type) - type_in;
251 * Compare entry names only for entry with vcn == 0.
253 diff = ntfs_cmp_names(le_name(le), le->name_len, name,
254 name_len, ni->mi.sbi->upcase,
263 if (le64_to_cpu(le->vcn) >= vcn)
267 return prev ? Add2Ptr(prev, le16_to_cpu(prev->size)) : ni->attr_list.le;
273 * Add an "attribute list entry" to the list.
275 int al_add_le(struct ntfs_inode *ni, enum ATTR_TYPE type, const __le16 *name,
276 u8 name_len, CLST svcn, __le16 id, const struct MFT_REF *ref,
277 struct ATTR_LIST_ENTRY **new_le)
281 struct ATTR_LIST_ENTRY *le;
284 size_t asize, new_asize, old_size;
286 typeof(ni->attr_list) *al = &ni->attr_list;
289 * Compute the size of the new 'le'
291 sz = le_size(name_len);
293 new_size = old_size + sz;
294 asize = al_aligned(old_size);
295 new_asize = al_aligned(new_size);
297 /* Scan forward to the point at which the new 'le' should be inserted. */
298 le = al_find_le_to_insert(ni, type, name, name_len, svcn);
299 off = PtrOffset(al->le, le);
301 if (new_size > asize) {
302 void *ptr = kmalloc(new_asize, GFP_NOFS);
307 memcpy(ptr, al->le, off);
308 memcpy(Add2Ptr(ptr, off + sz), le, old_size - off);
309 le = Add2Ptr(ptr, off);
313 memmove(Add2Ptr(le, sz), le, old_size - off);
320 le->size = cpu_to_le16(sz);
321 le->name_len = name_len;
322 le->name_off = offsetof(struct ATTR_LIST_ENTRY, name);
323 le->vcn = cpu_to_le64(svcn);
326 memcpy(le->name, name, sizeof(short) * name_len);
328 err = attr_set_size(ni, ATTR_LIST, NULL, 0, &al->run, new_size,
329 &new_size, true, &attr);
331 /* Undo memmove above. */
332 memmove(le, Add2Ptr(le, sz), old_size - off);
339 if (attr && attr->non_res) {
340 err = ntfs_sb_write_run(ni->mi.sbi, &al->run, 0, al->le,
351 * al_remove_le - Remove @le from attribute list.
353 bool al_remove_le(struct ntfs_inode *ni, struct ATTR_LIST_ENTRY *le)
357 typeof(ni->attr_list) *al = &ni->attr_list;
359 if (!al_is_valid_le(ni, le))
362 /* Save on stack the size of 'le' */
363 size = le16_to_cpu(le->size);
364 off = PtrOffset(al->le, le);
366 memmove(le, Add2Ptr(le, size), al->size - (off + size));
375 * al_delete_le - Delete first le from the list which matches its parameters.
377 bool al_delete_le(struct ntfs_inode *ni, enum ATTR_TYPE type, CLST vcn,
378 const __le16 *name, size_t name_len,
379 const struct MFT_REF *ref)
382 struct ATTR_LIST_ENTRY *le;
384 typeof(ni->attr_list) *al = &ni->attr_list;
386 /* Scan forward to the first le that matches the input. */
387 le = al_find_ex(ni, NULL, type, name, name_len, &vcn);
391 off = PtrOffset(al->le, le);
396 if (le->type != type)
398 if (le->name_len != name_len)
400 if (name_len && ntfs_cmp_names(le_name(le), name_len, name, name_len,
401 ni->mi.sbi->upcase, true))
403 if (le64_to_cpu(le->vcn) != vcn)
407 * The caller specified a segment reference, so we have to
408 * scan through the matching entries until we find that segment
409 * reference or we run of matching entries.
411 if (ref && memcmp(ref, &le->ref, sizeof(*ref))) {
412 off += le16_to_cpu(le->size);
413 le = Add2Ptr(al->le, off);
417 /* Save on stack the size of 'le'. */
418 size = le16_to_cpu(le->size);
420 memmove(le, Add2Ptr(le, size), al->size - (off + size));
428 int al_update(struct ntfs_inode *ni, int sync)
432 typeof(ni->attr_list) *al = &ni->attr_list;
434 if (!al->dirty || !al->size)
438 * Attribute list increased on demand in al_add_le.
439 * Attribute list decreased here.
441 err = attr_set_size(ni, ATTR_LIST, NULL, 0, &al->run, al->size, NULL,
446 if (!attr->non_res) {
447 memcpy(resident_data(attr), al->le, al->size);
449 err = ntfs_sb_write_run(ni->mi.sbi, &al->run, 0, al->le,
454 attr->nres.valid_size = attr->nres.data_size;