1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Attribute list attribute handling code.
* Part of this file is based on code from the NTFS-3G.
*
* Copyright (c) 2004-2005 Anton Altaparmakov
* Copyright (c) 2004-2005 Yura Pakhuchiy
* Copyright (c) 2006 Szabolcs Szakacsits
* Copyright (c) 2025 LG Electronics Co., Ltd.
*/
#include "mft.h"
#include "attrib.h"
#include "attrlist.h"
#include "lcnalloc.h"
#define NTFS_MAX_ATTR_LIST_SIZE (256 * 1024)
/*
* ntfs_attrlist_need - check whether inode need attribute list
* @ni: opened ntfs inode for which perform check
*
* Check whether all are attributes belong to one MFT record, in that case
* attribute list is not needed.
*
* Return 1 if inode need attribute list, 0 if not, or -errno on error.
*/
int ntfs_attrlist_need(struct ntfs_inode *ni)
{
struct attr_list_entry *ale;
if (!ni) {
ntfs_debug("Invalid arguments.\n");
return -EINVAL;
}
ntfs_debug("Entering for inode 0x%llx.\n", (long long) ni->mft_no);
if (!NInoAttrList(ni)) {
ntfs_debug("Inode haven't got attribute list.\n");
return -EINVAL;
}
if (!ni->attr_list) {
ntfs_debug("Corrupt in-memory struct.\n");
return -EINVAL;
}
ale = (struct attr_list_entry *)ni->attr_list;
while ((u8 *)ale < ni->attr_list + ni->attr_list_size) {
if (MREF_LE(ale->mft_reference) != ni->mft_no)
return 1;
ale = (struct attr_list_entry *)((u8 *)ale + le16_to_cpu(ale->length));
}
return 0;
}
/*
* Repack the $MFT/$ATTRIBUTE_LIST data into one run.
*
* The mapping pairs for an $ATTRIBUTE_LIST must remain in the base MFT
* record. Once that record has no room left, extending a fragmented list
* can require one more mapping-pairs byte than the record can hold. There
* is no attribute that can legally be moved out in that state: $STANDARD_
* INFORMATION, $ATTRIBUTE_LIST, and the first $MFT/$DATA extent all have to
* stay in the base record. Move the list data to one contiguous run. The
* caller supplies the minimum allocation size so a recovery can use the
* smallest useful run while normal updates can still request the maximum
* legal list size as a reserve.
*/
static int ntfs_attrlist_repack(struct inode *attr_vi,
struct ntfs_inode *attr_ni, s64 min_alloc_size,
struct ntfs_inode *locked_ni)
{
struct ntfs_volume *vol = attr_ni->vol;
struct runlist_element *old_rl, *new_rl;
u8 *data = NULL;
s64 data_size, alloc_size, nr_clusters, written;
s64 old_alloc_size;
size_t old_rl_count, new_rl_count;
unsigned long flags;
int err, restore_err;
if (attr_ni->mft_no != FILE_MFT || !NInoNonResident(attr_ni) ||
min_alloc_size < 0)
return -EINVAL;
/* The buffered I/O below can reacquire the attribute runlist lock. */
if (attr_ni == locked_ni)
return -ENOSPC;
err = ntfs_attr_map_whole_runlist(attr_ni);
if (err)
return err;
data_size = attr_ni->data_size;
if (data_size < 0)
return -EIO;
if (data_size) {
data = kvmalloc(data_size, GFP_NOFS);
if (!data)
return -ENOMEM;
written = ntfs_inode_attr_pread(attr_vi, 0, data_size, data);
if (written != data_size) {
err = written < 0 ? (int)written : -EIO;
goto out_free_data;
}
}
old_alloc_size = attr_ni->allocated_size;
alloc_size = max_t(s64, old_alloc_size, min_alloc_size);
nr_clusters = ntfs_bytes_to_cluster(vol,
alloc_size + vol->cluster_size - 1);
if (nr_clusters <= 0) {
err = -EFBIG;
goto out_free_data;
}
/* A single run keeps the mapping pairs at the minimum size. */
new_rl = ntfs_cluster_alloc(vol, 0, nr_clusters, -1, DATA_ZONE,
true, true, false);
if (IS_ERR(new_rl)) {
err = PTR_ERR(new_rl);
goto out_free_data;
}
new_rl_count = 0;
if (new_rl->vcn == 0 && new_rl->length == nr_clusters &&
!new_rl[1].length)
new_rl_count = 2;
if (new_rl_count != 2) {
ntfs_cluster_free_from_rl(vol, new_rl);
kvfree(new_rl);
err = -ENOSPC;
goto out_free_data;
}
old_rl = attr_ni->runlist.rl;
old_rl_count = attr_ni->runlist.count;
down_write(&attr_ni->runlist.lock);
attr_ni->runlist.rl = new_rl;
attr_ni->runlist.count = new_rl_count;
up_write(&attr_ni->runlist.lock);
write_lock_irqsave(&attr_ni->size_lock, flags);
attr_ni->allocated_size = ntfs_cluster_to_bytes(vol, nr_clusters);
write_unlock_irqrestore(&attr_ni->size_lock, flags);
/* Populate the replacement extent before publishing its mapping pairs. */
if (data_size) {
written = ntfs_inode_attr_pwrite(attr_vi, 0, data_size, data, true);
if (written != data_size) {
err = written < 0 ? (int)written : -EIO;
goto restore_old_runlist;
}
}
err = ntfs_attr_update_mapping_pairs_locked(attr_ni, 0, locked_ni);
if (err)
goto restore_old_runlist;
/* The new mapping is now authoritative; release the old data runs. */
if (ntfs_cluster_free_from_rl(vol, old_rl)) {
ntfs_error(vol->sb,
"Failed to free old ATTRIBUTE_LIST extent: inode %#llx",
(long long)attr_ni->mft_no);
NVolSetErrors(vol);
}
kvfree(old_rl);
kvfree(data);
return 0;
restore_old_runlist:
down_write(&attr_ni->runlist.lock);
attr_ni->runlist.rl = old_rl;
attr_ni->runlist.count = old_rl_count;
up_write(&attr_ni->runlist.lock);
write_lock_irqsave(&attr_ni->size_lock, flags);
attr_ni->allocated_size = old_alloc_size;
write_unlock_irqrestore(&attr_ni->size_lock, flags);
restore_err = ntfs_attr_update_mapping_pairs_locked(
attr_ni, 0, locked_ni);
if (restore_err) {
ntfs_error(vol->sb, "Failed to restore ATTRIBUTE_LIST mapping pairs (%d)",
restore_err);
NVolSetErrors(vol);
}
ntfs_cluster_free_from_rl(vol, new_rl);
kvfree(new_rl);
err = err ? err : restore_err;
out_free_data:
kvfree(data);
return err;
}
int ntfs_attrlist_update_locked(struct ntfs_inode *base_ni,
struct ntfs_inode *locked_ni)
{
struct inode *attr_vi;
struct ntfs_inode *attr_ni;
s64 written;
int err, retry_err;
/*
* generic_shutdown_super() clears SB_ACTIVE before evicting cached
* inodes. Do not look up the attribute-list inode after SB_ACTIVE has
* been cleared; it may already be I_FREEING, and waiting on it can
* self-deadlock.
*/
if (!(VFS_I(base_ni)->i_sb->s_flags & SB_ACTIVE))
return -EIO;
attr_vi = ntfs_attr_iget(VFS_I(base_ni), AT_ATTRIBUTE_LIST, AT_UNNAMED, 0);
if (IS_ERR(attr_vi)) {
err = PTR_ERR(attr_vi);
return err;
}
attr_ni = NTFS_I(attr_vi);
/* Truncation and page-cache writes can reacquire this runlist lock. */
if (attr_ni == locked_ni) {
iput(attr_vi);
return -ENOSPC;
}
err = ntfs_attr_truncate_i_locked(
attr_ni, base_ni->attr_list_size, HOLES_NO, locked_ni);
if (err == -ENOSPC && attr_ni->mft_no == FILE_MFT &&
NInoNonResident(attr_ni)) {
retry_err = ntfs_attrlist_repack(attr_vi, attr_ni,
base_ni->attr_list_size, locked_ni);
if (retry_err) {
ntfs_error(base_ni->vol->sb, "Failed to repack attribute list");
iput(attr_vi);
return retry_err;
}
retry_err = ntfs_attr_truncate_i_locked(
attr_ni, base_ni->attr_list_size,
HOLES_NO, locked_ni);
if (retry_err) {
ntfs_error(base_ni->vol->sb,
"Failed to resize attribute list after repack");
iput(attr_vi);
return retry_err;
}
} else if (err) {
iput(attr_vi);
ntfs_error(base_ni->vol->sb,
"Failed to truncate attribute list of inode %#llx",
(long long)base_ni->mft_no);
return err;
}
/*
* Reserve the maximum legal list size while the MFT metadata area is
* still easy to allocate contiguously. This prevents a later list entry
* from needing another mapping-pairs byte in the full base MFT record.
* Failure to obtain the optional reserve must not reject the current
* metadata update; the repack retry above remains available if needed.
*/
if (base_ni->mft_no == FILE_MFT && NInoNonResident(attr_ni) &&
attr_ni->allocated_size < NTFS_MAX_ATTR_LIST_SIZE) {
retry_err = ntfs_attr_expand_locked(
attr_ni, base_ni->attr_list_size,
NTFS_MAX_ATTR_LIST_SIZE, locked_ni);
if (retry_err == -ENOSPC) {
retry_err = ntfs_attrlist_repack(
attr_vi, attr_ni,
NTFS_MAX_ATTR_LIST_SIZE, locked_ni);
if (retry_err == -ENOSPC)
retry_err = 0;
}
if (retry_err) {
ntfs_error(base_ni->vol->sb,
"Failed to reserve attribute list space");
iput(attr_vi);
return retry_err;
}
}
i_size_write(attr_vi, base_ni->attr_list_size);
if (NInoNonResident(attr_ni) && !NInoAttrListNonResident(base_ni))
NInoSetAttrListNonResident(base_ni);
written = ntfs_inode_attr_pwrite(attr_vi, 0, base_ni->attr_list_size,
base_ni->attr_list, false);
if (written != base_ni->attr_list_size) {
err = written < 0 ? (int)written : -EIO;
iput(attr_vi);
ntfs_error(base_ni->vol->sb,
"Failed to write attribute list of inode %#llx",
(long long)base_ni->mft_no);
return err;
}
NInoSetAttrListDirty(base_ni);
iput(attr_vi);
return 0;
}
int ntfs_attrlist_update(struct ntfs_inode *base_ni)
{
return ntfs_attrlist_update_locked(base_ni, NULL);
}
/*
* ntfs_attrlist_entry_add - add an attribute list attribute entry
* @ni: opened ntfs inode, which contains that attribute
* @attr: attribute record to add to attribute list
*
* Return 0 on success and -errno on error.
*/
int ntfs_attrlist_entry_add(struct ntfs_inode *ni, struct attr_record *attr)
{
struct attr_list_entry *ale;
__le64 mref;
struct ntfs_attr_search_ctx *ctx;
u8 *new_al;
int entry_len, entry_offset, err;
struct mft_record *ni_mrec;
u8 *old_al;
__le64 lowest_vcn;
if (!ni || !attr) {
ntfs_debug("Invalid arguments.\n");
return -EINVAL;
}
ntfs_debug("Entering for inode 0x%llx, attr 0x%x.\n",
ni->mft_no, (unsigned int) le32_to_cpu(attr->type));
ni_mrec = map_mft_record(ni);
if (IS_ERR(ni_mrec)) {
ntfs_debug("Failed to map mft record.\n");
return PTR_ERR(ni_mrec);
}
mref = MK_LE_MREF(ni->mft_no, le16_to_cpu(ni_mrec->sequence_number));
unmap_mft_record(ni);
if (ni->nr_extents == -1)
ni = ni->ext.base_ntfs_ino;
if (!NInoAttrList(ni)) {
ntfs_debug("Attribute list isn't present.\n");
return -ENOENT;
}
/* Determine size and allocate memory for new attribute list. */
entry_len = (sizeof(struct attr_list_entry) + sizeof(__le16) *
attr->name_length + 7) & ~7;
new_al = kvzalloc(ni->attr_list_size + entry_len, GFP_NOFS);
if (!new_al)
return -ENOMEM;
/* Find place for the new entry. */
ctx = ntfs_attr_get_search_ctx(ni, NULL);
if (!ctx) {
err = -ENOMEM;
ntfs_error(ni->vol->sb, "Failed to get search context");
goto err_out;
}
if (attr->non_resident)
lowest_vcn = attr->data.non_resident.lowest_vcn;
else
lowest_vcn = 0;
err = ntfs_attr_lookup(attr->type, (attr->name_length) ? (__le16 *)
((u8 *)attr + le16_to_cpu(attr->name_offset)) :
AT_UNNAMED, attr->name_length, CASE_SENSITIVE,
le64_to_cpu(lowest_vcn),
(attr->non_resident) ? NULL : ((u8 *)attr +
le16_to_cpu(attr->data.resident.value_offset)), (attr->non_resident) ?
0 : le32_to_cpu(attr->data.resident.value_length), ctx);
if (!err) {
/* Found some extent, check it to be before new extent. */
if (ctx->al_entry->lowest_vcn == lowest_vcn) {
err = -EEXIST;
ntfs_debug("Such attribute already present in the attribute list.\n");
ntfs_attr_put_search_ctx(ctx);
goto err_out;
}
/* Add new entry after this extent. */
ale = (struct attr_list_entry *)((u8 *)ctx->al_entry +
le16_to_cpu(ctx->al_entry->length));
} else {
/* Check for real errors. */
if (err != -ENOENT) {
ntfs_debug("Attribute lookup failed.\n");
ntfs_attr_put_search_ctx(ctx);
goto err_out;
}
/* No previous extents found. */
ale = ctx->al_entry;
}
/* Don't need it anymore, @ctx->al_entry points to @ni->attr_list. */
ntfs_attr_put_search_ctx(ctx);
/* Determine new entry offset. */
entry_offset = ((u8 *)ale - ni->attr_list);
/* Set pointer to new entry. */
ale = (struct attr_list_entry *)(new_al + entry_offset);
memset(ale, 0, entry_len);
/* Form new entry. */
ale->type = attr->type;
ale->length = cpu_to_le16(entry_len);
ale->name_length = attr->name_length;
ale->name_offset = offsetof(struct attr_list_entry, name);
if (attr->non_resident)
ale->lowest_vcn = attr->data.non_resident.lowest_vcn;
else
ale->lowest_vcn = 0;
ale->mft_reference = mref;
ale->instance = attr->instance;
memcpy(ale->name, (u8 *)attr + le16_to_cpu(attr->name_offset),
attr->name_length * sizeof(__le16));
/* Copy entries from old attribute list to new. */
memcpy(new_al, ni->attr_list, entry_offset);
memcpy(new_al + entry_offset + entry_len, ni->attr_list +
entry_offset, ni->attr_list_size - entry_offset);
/* Set new runlist. */
old_al = ni->attr_list;
ni->attr_list = new_al;
ni->attr_list_size = ni->attr_list_size + entry_len;
err = ntfs_attrlist_update(ni);
if (err) {
ni->attr_list = old_al;
ni->attr_list_size -= entry_len;
goto err_out;
}
kvfree(old_al);
return 0;
err_out:
kvfree(new_al);
return err;
}
/*
* ntfs_attrlist_entry_rm - remove an attribute list attribute entry
* @ctx: attribute search context describing the attribute list entry
*
* Remove the attribute list entry @ctx->al_entry from the attribute list.
*
* Return 0 on success and -errno on error.
*/
int ntfs_attrlist_entry_rm(struct ntfs_attr_search_ctx *ctx)
{
u8 *new_al;
int new_al_len;
struct ntfs_inode *base_ni;
struct attr_list_entry *ale;
if (!ctx || !ctx->ntfs_ino || !ctx->al_entry) {
ntfs_debug("Invalid arguments.\n");
return -EINVAL;
}
if (ctx->base_ntfs_ino)
base_ni = ctx->base_ntfs_ino;
else
base_ni = ctx->ntfs_ino;
ale = ctx->al_entry;
ntfs_debug("Entering for inode 0x%llx, attr 0x%x, lowest_vcn %lld.\n",
(long long)ctx->ntfs_ino->mft_no,
(unsigned int)le32_to_cpu(ctx->al_entry->type),
(long long)le64_to_cpu(ctx->al_entry->lowest_vcn));
if (!NInoAttrList(base_ni)) {
ntfs_debug("Attribute list isn't present.\n");
return -ENOENT;
}
/* Allocate memory for new attribute list. */
new_al_len = base_ni->attr_list_size - le16_to_cpu(ale->length);
new_al = kvzalloc(new_al_len, GFP_NOFS);
if (!new_al)
return -ENOMEM;
/* Copy entries from old attribute list to new. */
memcpy(new_al, base_ni->attr_list, (u8 *)ale - base_ni->attr_list);
memcpy(new_al + ((u8 *)ale - base_ni->attr_list), (u8 *)ale + le16_to_cpu(
ale->length), new_al_len - ((u8 *)ale - base_ni->attr_list));
/* Set new runlist. */
kvfree(base_ni->attr_list);
base_ni->attr_list = new_al;
base_ni->attr_list_size = new_al_len;
return ntfs_attrlist_update(base_ni);
}
|