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/fs.h>
10 #include <linux/random.h>
11 #include <linux/slab.h>
12
13 #include "debug.h"
14 #include "ntfs.h"
15 #include "ntfs_fs.h"
16
17 /*
18 * LOG FILE structs
19 */
20
21 // clang-format off
22
23 #define MaxLogFileSize 0x100000000ull
24 #define DefaultLogPageSize 4096
25 #define MinLogRecordPages 0x30
26
27 struct RESTART_HDR {
28 struct NTFS_RECORD_HEADER rhdr; // 'RSTR'
29 __le32 sys_page_size; // 0x10: Page size of the system which initialized the log.
30 __le32 page_size; // 0x14: Log page size used for this log file.
31 __le16 ra_off; // 0x18:
32 __le16 minor_ver; // 0x1A:
33 __le16 major_ver; // 0x1C:
34 __le16 fixups[];
35 };
36
37 #define LFS_NO_CLIENT 0xffff
38 #define LFS_NO_CLIENT_LE cpu_to_le16(0xffff)
39
40 struct CLIENT_REC {
41 __le64 oldest_lsn;
42 __le64 restart_lsn; // 0x08:
43 __le16 prev_client; // 0x10:
44 __le16 next_client; // 0x12:
45 __le16 seq_num; // 0x14:
46 u8 align[6]; // 0x16:
47 __le32 name_bytes; // 0x1C: In bytes.
48 __le16 name[64]; // 0x20: Name of client.
49 };
50
51 static_assert(sizeof(struct CLIENT_REC) == 0xa0);
52
53 /* Two copies of these will exist at the beginning of the log file */
54 struct RESTART_AREA {
55 __le64 current_lsn; // 0x00: Current logical end of log file.
56 __le16 log_clients; // 0x08: Maximum number of clients.
57 __le16 client_idx[2]; // 0x0A: Free/use index into the client record arrays.
58 __le16 flags; // 0x0E: See RESTART_SINGLE_PAGE_IO.
59 __le32 seq_num_bits; // 0x10: The number of bits in sequence number.
60 __le16 ra_len; // 0x14:
61 __le16 client_off; // 0x16:
62 __le64 l_size; // 0x18: Usable log file size.
63 __le32 last_lsn_data_len; // 0x20:
64 __le16 rec_hdr_len; // 0x24: Log page data offset.
65 __le16 data_off; // 0x26: Log page data length.
66 __le32 open_log_count; // 0x28:
67 __le32 align[5]; // 0x2C:
68 struct CLIENT_REC clients[]; // 0x40:
69 };
70
71 struct LOG_REC_HDR {
72 __le16 redo_op; // 0x00: NTFS_LOG_OPERATION
73 __le16 undo_op; // 0x02: NTFS_LOG_OPERATION
74 __le16 redo_off; // 0x04: Offset to Redo record.
75 __le16 redo_len; // 0x06: Redo length.
76 __le16 undo_off; // 0x08: Offset to Undo record.
77 __le16 undo_len; // 0x0A: Undo length.
78 __le16 target_attr; // 0x0C:
79 __le16 lcns_follow; // 0x0E:
80 __le16 record_off; // 0x10:
81 __le16 attr_off; // 0x12:
82 __le16 cluster_off; // 0x14:
83 __le16 reserved; // 0x16:
84 __le64 target_vcn; // 0x18:
85 __le64 page_lcns[]; // 0x20:
86 };
87
88 static_assert(sizeof(struct LOG_REC_HDR) == 0x20);
89
90 #define RESTART_ENTRY_ALLOCATED 0xFFFFFFFF
91 #define RESTART_ENTRY_ALLOCATED_LE cpu_to_le32(0xFFFFFFFF)
92
93 struct RESTART_TABLE {
94 __le16 size; // 0x00: In bytes
95 __le16 used; // 0x02: Entries
96 __le16 total; // 0x04: Entries
97 __le16 res[3]; // 0x06:
98 __le32 free_goal; // 0x0C:
99 __le32 first_free; // 0x10:
100 __le32 last_free; // 0x14:
101
102 };
103
104 static_assert(sizeof(struct RESTART_TABLE) == 0x18);
105
106 struct ATTR_NAME_ENTRY {
107 __le16 off; // Offset in the Open attribute Table.
108 __le16 name_bytes;
109 __le16 name[];
110 };
111
112 struct OPEN_ATTR_ENRTY {
113 __le32 next; // 0x00: RESTART_ENTRY_ALLOCATED if allocated
114 __le32 bytes_per_index; // 0x04:
115 enum ATTR_TYPE type; // 0x08:
116 u8 is_dirty_pages; // 0x0C:
117 u8 is_attr_name; // 0x0B: Faked field to manage 'ptr'
118 u8 name_len; // 0x0C: Faked field to manage 'ptr'
119 u8 res;
120 struct MFT_REF ref; // 0x10: File Reference of file containing attribute
121 __le64 open_record_lsn; // 0x18:
122 void *ptr; // 0x20:
123 };
124
125 /* 32 bit version of 'struct OPEN_ATTR_ENRTY' */
126 struct OPEN_ATTR_ENRTY_32 {
127 __le32 next; // 0x00: RESTART_ENTRY_ALLOCATED if allocated
128 __le32 ptr; // 0x04:
129 struct MFT_REF ref; // 0x08:
130 __le64 open_record_lsn; // 0x10:
131 u8 is_dirty_pages; // 0x18:
132 u8 is_attr_name; // 0x19:
133 u8 res1[2];
134 enum ATTR_TYPE type; // 0x1C:
135 u8 name_len; // 0x20: In wchar
136 u8 res2[3];
137 __le32 AttributeName; // 0x24:
138 __le32 bytes_per_index; // 0x28:
139 };
140
141 #define SIZEOF_OPENATTRIBUTEENTRY0 0x2c
142 // static_assert( 0x2C == sizeof(struct OPEN_ATTR_ENRTY_32) );
143 static_assert(sizeof(struct OPEN_ATTR_ENRTY) < SIZEOF_OPENATTRIBUTEENTRY0);
144
145 /*
146 * One entry exists in the Dirty Pages Table for each page which is dirty at
147 * the time the Restart Area is written.
148 */
149 struct DIR_PAGE_ENTRY {
150 __le32 next; // 0x00: RESTART_ENTRY_ALLOCATED if allocated
151 __le32 target_attr; // 0x04: Index into the Open attribute Table
152 __le32 transfer_len; // 0x08:
153 __le32 lcns_follow; // 0x0C:
154 __le64 vcn; // 0x10: Vcn of dirty page
155 __le64 oldest_lsn; // 0x18:
156 __le64 page_lcns[]; // 0x20:
157 };
158
159 static_assert(sizeof(struct DIR_PAGE_ENTRY) == 0x20);
160
161 /* 32 bit version of 'struct DIR_PAGE_ENTRY' */
162 struct DIR_PAGE_ENTRY_32 {
163 __le32 next; // 0x00: RESTART_ENTRY_ALLOCATED if allocated
164 __le32 target_attr; // 0x04: Index into the Open attribute Table
165 __le32 transfer_len; // 0x08:
166 __le32 lcns_follow; // 0x0C:
167 __le32 reserved; // 0x10:
168 __le32 vcn_low; // 0x14: Vcn of dirty page
169 __le32 vcn_hi; // 0x18: Vcn of dirty page
170 __le32 oldest_lsn_low; // 0x1C:
171 __le32 oldest_lsn_hi; // 0x1C:
172 __le32 page_lcns_low; // 0x24:
173 __le32 page_lcns_hi; // 0x24:
174 };
175
176 static_assert(offsetof(struct DIR_PAGE_ENTRY_32, vcn_low) == 0x14);
177 static_assert(sizeof(struct DIR_PAGE_ENTRY_32) == 0x2c);
178
179 enum transact_state {
180 TransactionUninitialized = 0,
181 TransactionActive,
182 TransactionPrepared,
183 TransactionCommitted
184 };
185
186 struct TRANSACTION_ENTRY {
187 __le32 next; // 0x00: RESTART_ENTRY_ALLOCATED if allocated
188 u8 transact_state; // 0x04:
189 u8 reserved[3]; // 0x05:
190 __le64 first_lsn; // 0x08:
191 __le64 prev_lsn; // 0x10:
192 __le64 undo_next_lsn; // 0x18:
193 __le32 undo_records; // 0x20: Number of undo log records pending abort
194 __le32 undo_len; // 0x24: Total undo size
195 };
196
197 static_assert(sizeof(struct TRANSACTION_ENTRY) == 0x28);
198
199 struct NTFS_RESTART {
200 __le32 major_ver; // 0x00:
201 __le32 minor_ver; // 0x04:
202 __le64 check_point_start; // 0x08:
203 __le64 open_attr_table_lsn; // 0x10:
204 __le64 attr_names_lsn; // 0x18:
205 __le64 dirty_pages_table_lsn; // 0x20:
206 __le64 transact_table_lsn; // 0x28:
207 __le32 open_attr_len; // 0x30: In bytes
208 __le32 attr_names_len; // 0x34: In bytes
209 __le32 dirty_pages_len; // 0x38: In bytes
210 __le32 transact_table_len; // 0x3C: In bytes
211 };
212
213 static_assert(sizeof(struct NTFS_RESTART) == 0x40);
214
215 struct NEW_ATTRIBUTE_SIZES {
216 __le64 alloc_size;
217 __le64 valid_size;
218 __le64 data_size;
219 __le64 total_size;
220 };
221
222 struct BITMAP_RANGE {
223 __le32 bitmap_off;
224 __le32 bits;
225 };
226
227 struct LCN_RANGE {
228 __le64 lcn;
229 __le64 len;
230 };
231
232 /* The following type defines the different log record types. */
233 #define LfsClientRecord cpu_to_le32(1)
234 #define LfsClientRestart cpu_to_le32(2)
235
236 /* This is used to uniquely identify a client for a particular log file. */
237 struct CLIENT_ID {
238 __le16 seq_num;
239 __le16 client_idx;
240 };
241
242 /* This is the header that begins every Log Record in the log file. */
243 struct LFS_RECORD_HDR {
244 __le64 this_lsn; // 0x00:
245 __le64 client_prev_lsn; // 0x08:
246 __le64 client_undo_next_lsn; // 0x10:
247 __le32 client_data_len; // 0x18:
248 struct CLIENT_ID client; // 0x1C: Owner of this log record.
249 __le32 record_type; // 0x20: LfsClientRecord or LfsClientRestart.
250 __le32 transact_id; // 0x24:
251 __le16 flags; // 0x28: LOG_RECORD_MULTI_PAGE
252 u8 align[6]; // 0x2A:
253 };
254
255 #define LOG_RECORD_MULTI_PAGE cpu_to_le16(1)
256
257 static_assert(sizeof(struct LFS_RECORD_HDR) == 0x30);
258
259 struct LFS_RECORD {
260 __le16 next_record_off; // 0x00: Offset of the free space in the page,
261 u8 align[6]; // 0x02:
262 __le64 last_end_lsn; // 0x08: lsn for the last log record which ends on the page,
263 };
264
265 static_assert(sizeof(struct LFS_RECORD) == 0x10);
266
267 struct RECORD_PAGE_HDR {
268 struct NTFS_RECORD_HEADER rhdr; // 'RCRD'
269 __le32 rflags; // 0x10: See LOG_PAGE_LOG_RECORD_END
270 __le16 page_count; // 0x14:
271 __le16 page_pos; // 0x16:
272 struct LFS_RECORD record_hdr; // 0x18:
273 __le16 fixups[10]; // 0x28:
274 __le32 file_off; // 0x3c: Used when major version >= 2
275 };
276
277 // clang-format on
278
279 // Page contains the end of a log record.
280 #define LOG_PAGE_LOG_RECORD_END cpu_to_le32(0x00000001)
281
is_log_record_end(const struct RECORD_PAGE_HDR * hdr)282 static inline bool is_log_record_end(const struct RECORD_PAGE_HDR *hdr)
283 {
284 return hdr->rflags & LOG_PAGE_LOG_RECORD_END;
285 }
286
287 static_assert(offsetof(struct RECORD_PAGE_HDR, file_off) == 0x3c);
288
289 /*
290 * END of NTFS LOG structures
291 */
292
293 /* Define some tuning parameters to keep the restart tables a reasonable size. */
294 #define INITIAL_NUMBER_TRANSACTIONS 5
295
296 enum NTFS_LOG_OPERATION {
297
298 Noop = 0x00,
299 CompensationLogRecord = 0x01,
300 InitializeFileRecordSegment = 0x02,
301 DeallocateFileRecordSegment = 0x03,
302 WriteEndOfFileRecordSegment = 0x04,
303 CreateAttribute = 0x05,
304 DeleteAttribute = 0x06,
305 UpdateResidentValue = 0x07,
306 UpdateNonresidentValue = 0x08,
307 UpdateMappingPairs = 0x09,
308 DeleteDirtyClusters = 0x0A,
309 SetNewAttributeSizes = 0x0B,
310 AddIndexEntryRoot = 0x0C,
311 DeleteIndexEntryRoot = 0x0D,
312 AddIndexEntryAllocation = 0x0E,
313 DeleteIndexEntryAllocation = 0x0F,
314 WriteEndOfIndexBuffer = 0x10,
315 SetIndexEntryVcnRoot = 0x11,
316 SetIndexEntryVcnAllocation = 0x12,
317 UpdateFileNameRoot = 0x13,
318 UpdateFileNameAllocation = 0x14,
319 SetBitsInNonresidentBitMap = 0x15,
320 ClearBitsInNonresidentBitMap = 0x16,
321 HotFix = 0x17,
322 EndTopLevelAction = 0x18,
323 PrepareTransaction = 0x19,
324 CommitTransaction = 0x1A,
325 ForgetTransaction = 0x1B,
326 OpenNonresidentAttribute = 0x1C,
327 OpenAttributeTableDump = 0x1D,
328 AttributeNamesDump = 0x1E,
329 DirtyPageTableDump = 0x1F,
330 TransactionTableDump = 0x20,
331 UpdateRecordDataRoot = 0x21,
332 UpdateRecordDataAllocation = 0x22,
333
334 UpdateRelativeDataInIndex =
335 0x23, // NtOfsRestartUpdateRelativeDataInIndex
336 UpdateRelativeDataInIndex2 = 0x24,
337 ZeroEndOfFileRecord = 0x25,
338 };
339
340 /*
341 * Array for log records which require a target attribute.
342 * A true indicates that the corresponding restart operation
343 * requires a target attribute.
344 */
345 static const u8 AttributeRequired[] = {
346 0xFC, 0xFB, 0xFF, 0x10, 0x06,
347 };
348
is_target_required(u16 op)349 static inline bool is_target_required(u16 op)
350 {
351 bool ret = op <= UpdateRecordDataAllocation &&
352 (AttributeRequired[op >> 3] >> (op & 7) & 1);
353 return ret;
354 }
355
can_skip_action(enum NTFS_LOG_OPERATION op)356 static inline bool can_skip_action(enum NTFS_LOG_OPERATION op)
357 {
358 switch (op) {
359 case Noop:
360 case DeleteDirtyClusters:
361 case HotFix:
362 case EndTopLevelAction:
363 case PrepareTransaction:
364 case CommitTransaction:
365 case ForgetTransaction:
366 case CompensationLogRecord:
367 case OpenNonresidentAttribute:
368 case OpenAttributeTableDump:
369 case AttributeNamesDump:
370 case DirtyPageTableDump:
371 case TransactionTableDump:
372 return true;
373 default:
374 return false;
375 }
376 }
377
378 enum { lcb_ctx_undo_next, lcb_ctx_prev, lcb_ctx_next };
379
380 /* Bytes per restart table. */
bytes_per_rt(const struct RESTART_TABLE * rt)381 static inline u32 bytes_per_rt(const struct RESTART_TABLE *rt)
382 {
383 return le16_to_cpu(rt->used) * le16_to_cpu(rt->size) +
384 sizeof(struct RESTART_TABLE);
385 }
386
387 /* Log record length. */
lrh_length(const struct LOG_REC_HDR * lr)388 static inline u32 lrh_length(const struct LOG_REC_HDR *lr)
389 {
390 u16 t16 = le16_to_cpu(lr->lcns_follow);
391
392 return struct_size(lr, page_lcns, max_t(u16, 1, t16));
393 }
394
395 struct lcb {
396 struct LFS_RECORD_HDR *lrh; // Log record header of the current lsn.
397 struct LOG_REC_HDR *log_rec;
398 u32 ctx_mode; // lcb_ctx_undo_next/lcb_ctx_prev/lcb_ctx_next
399 struct CLIENT_ID client;
400 bool alloc; // If true the we should deallocate 'log_rec'.
401 };
402
lcb_put(struct lcb * lcb)403 static void lcb_put(struct lcb *lcb)
404 {
405 if (lcb->alloc)
406 kfree(lcb->log_rec);
407 kfree(lcb->lrh);
408 kfree(lcb);
409 }
410
411 /* Find the oldest lsn from active clients. */
oldest_client_lsn(const struct CLIENT_REC * ca,__le16 next_client,u64 * oldest_lsn)412 static inline void oldest_client_lsn(const struct CLIENT_REC *ca,
413 __le16 next_client, u64 *oldest_lsn)
414 {
415 while (next_client != LFS_NO_CLIENT_LE) {
416 const struct CLIENT_REC *cr = ca + le16_to_cpu(next_client);
417 u64 lsn = le64_to_cpu(cr->oldest_lsn);
418
419 /* Ignore this block if it's oldest lsn is 0. */
420 if (lsn && lsn < *oldest_lsn)
421 *oldest_lsn = lsn;
422
423 next_client = cr->next_client;
424 }
425 }
426
is_rst_page_hdr_valid(u32 file_off,const struct RESTART_HDR * rhdr)427 static inline bool is_rst_page_hdr_valid(u32 file_off,
428 const struct RESTART_HDR *rhdr)
429 {
430 u32 sys_page = le32_to_cpu(rhdr->sys_page_size);
431 u32 page_size = le32_to_cpu(rhdr->page_size);
432 u32 end_usa;
433 u16 ro;
434
435 if (sys_page < SECTOR_SIZE || page_size < SECTOR_SIZE ||
436 sys_page & (sys_page - 1) || page_size & (page_size - 1)) {
437 return false;
438 }
439
440 /* Check that if the file offset isn't 0, it is the system page size. */
441 if (file_off && file_off != sys_page)
442 return false;
443
444 /* Check support version 1.1+. */
445 if (le16_to_cpu(rhdr->major_ver) <= 1 && !rhdr->minor_ver)
446 return false;
447
448 if (le16_to_cpu(rhdr->major_ver) > 2)
449 return false;
450
451 ro = le16_to_cpu(rhdr->ra_off);
452 if (!IS_ALIGNED(ro, 8) || ro > sys_page)
453 return false;
454
455 end_usa = ((sys_page >> SECTOR_SHIFT) + 1) * sizeof(short);
456 end_usa += le16_to_cpu(rhdr->rhdr.fix_off);
457
458 if (ro < end_usa)
459 return false;
460
461 return true;
462 }
463
is_rst_area_valid(const struct RESTART_HDR * rhdr)464 static inline bool is_rst_area_valid(const struct RESTART_HDR *rhdr)
465 {
466 const struct RESTART_AREA *ra;
467 u16 cl, fl, ul;
468 u32 off, l_size, seq_bits;
469 u16 ro = le16_to_cpu(rhdr->ra_off);
470 u32 sys_page = le32_to_cpu(rhdr->sys_page_size);
471
472 if (ro + offsetof(struct RESTART_AREA, l_size) >
473 SECTOR_SIZE - sizeof(short))
474 return false;
475
476 ra = Add2Ptr(rhdr, ro);
477 cl = le16_to_cpu(ra->log_clients);
478
479 if (cl > 1)
480 return false;
481
482 off = le16_to_cpu(ra->client_off);
483
484 if (!IS_ALIGNED(off, 8) || ro + off > SECTOR_SIZE - sizeof(short))
485 return false;
486
487 off += cl * sizeof(struct CLIENT_REC);
488
489 if (off > sys_page)
490 return false;
491
492 /*
493 * Check the restart length field and whether the entire
494 * restart area is contained that length.
495 */
496 if (le16_to_cpu(rhdr->ra_off) + le16_to_cpu(ra->ra_len) > sys_page ||
497 off > le16_to_cpu(ra->ra_len)) {
498 return false;
499 }
500
501 /*
502 * As a final check make sure that the use list and the free list
503 * are either empty or point to a valid client.
504 */
505 fl = le16_to_cpu(ra->client_idx[0]);
506 ul = le16_to_cpu(ra->client_idx[1]);
507 if ((fl != LFS_NO_CLIENT && fl >= cl) ||
508 (ul != LFS_NO_CLIENT && ul >= cl))
509 return false;
510
511 /* Make sure the sequence number bits match the log file size. */
512 l_size = le64_to_cpu(ra->l_size);
513
514 seq_bits = sizeof(u64) * 8 + 3;
515 while (l_size) {
516 l_size >>= 1;
517 seq_bits -= 1;
518 }
519
520 if (seq_bits != le32_to_cpu(ra->seq_num_bits))
521 return false;
522
523 /* The log page data offset and record header length must be quad-aligned. */
524 if (!IS_ALIGNED(le16_to_cpu(ra->data_off), 8) ||
525 !IS_ALIGNED(le16_to_cpu(ra->rec_hdr_len), 8))
526 return false;
527
528 return true;
529 }
530
is_client_area_valid(const struct RESTART_HDR * rhdr,bool usa_error)531 static inline bool is_client_area_valid(const struct RESTART_HDR *rhdr,
532 bool usa_error)
533 {
534 u16 ro = le16_to_cpu(rhdr->ra_off);
535 const struct RESTART_AREA *ra = Add2Ptr(rhdr, ro);
536 u16 ra_len = le16_to_cpu(ra->ra_len);
537 const struct CLIENT_REC *ca;
538 u32 i;
539
540 if (usa_error && ra_len + ro > SECTOR_SIZE - sizeof(short))
541 return false;
542
543 /* Find the start of the client array. */
544 ca = Add2Ptr(ra, le16_to_cpu(ra->client_off));
545
546 /*
547 * Start with the free list.
548 * Check that all the clients are valid and that there isn't a cycle.
549 * Do the in-use list on the second pass.
550 */
551 for (i = 0; i < 2; i++) {
552 u16 client_idx = le16_to_cpu(ra->client_idx[i]);
553 bool first_client = true;
554 u16 clients = le16_to_cpu(ra->log_clients);
555
556 while (client_idx != LFS_NO_CLIENT) {
557 const struct CLIENT_REC *cr;
558
559 if (!clients ||
560 client_idx >= le16_to_cpu(ra->log_clients))
561 return false;
562
563 clients -= 1;
564 cr = ca + client_idx;
565
566 client_idx = le16_to_cpu(cr->next_client);
567
568 if (first_client) {
569 first_client = false;
570 if (cr->prev_client != LFS_NO_CLIENT_LE)
571 return false;
572 }
573 }
574 }
575
576 return true;
577 }
578
579 /*
580 * remove_client
581 *
582 * Remove a client record from a client record list an restart area.
583 */
remove_client(struct CLIENT_REC * ca,const struct CLIENT_REC * cr,__le16 * head)584 static inline void remove_client(struct CLIENT_REC *ca,
585 const struct CLIENT_REC *cr, __le16 *head)
586 {
587 if (cr->prev_client == LFS_NO_CLIENT_LE)
588 *head = cr->next_client;
589 else
590 ca[le16_to_cpu(cr->prev_client)].next_client = cr->next_client;
591
592 if (cr->next_client != LFS_NO_CLIENT_LE)
593 ca[le16_to_cpu(cr->next_client)].prev_client = cr->prev_client;
594 }
595
596 /*
597 * add_client - Add a client record to the start of a list.
598 */
add_client(struct CLIENT_REC * ca,u16 index,__le16 * head)599 static inline void add_client(struct CLIENT_REC *ca, u16 index, __le16 *head)
600 {
601 struct CLIENT_REC *cr = ca + index;
602
603 cr->prev_client = LFS_NO_CLIENT_LE;
604 cr->next_client = *head;
605
606 if (*head != LFS_NO_CLIENT_LE)
607 ca[le16_to_cpu(*head)].prev_client = cpu_to_le16(index);
608
609 *head = cpu_to_le16(index);
610 }
611
612 /*
613 * Enumerate restart table.
614 *
615 * @t - table to enumerate.
616 * @c - current enumerated element.
617 *
618 * enumeration starts with @c == NULL
619 * returns next element or NULL
620 */
enum_rstbl(struct RESTART_TABLE * t,void * c)621 static inline void *enum_rstbl(struct RESTART_TABLE *t, void *c)
622 {
623 __le32 *e;
624 u32 bprt;
625 u16 rsize;
626
627 if (!t)
628 return NULL;
629
630 rsize = le16_to_cpu(t->size);
631
632 if (!c) {
633 /* start enumeration. */
634 if (!t->total)
635 return NULL;
636 e = Add2Ptr(t, sizeof(struct RESTART_TABLE));
637 } else {
638 e = Add2Ptr(c, rsize);
639 }
640
641 /* Loop until we hit the first one allocated, or the end of the list. */
642 for (bprt = bytes_per_rt(t); PtrOffset(t, e) < bprt;
643 e = Add2Ptr(e, rsize)) {
644 if (*e == RESTART_ENTRY_ALLOCATED_LE)
645 return e;
646 }
647 return NULL;
648 }
649
650 /*
651 * dp_range_ok - true if [j, j + count) fits in a page_lcns[cap] array.
652 */
dp_range_ok(size_t j,u32 count,u32 cap)653 static inline bool dp_range_ok(size_t j, u32 count, u32 cap)
654 {
655 return j < cap && count <= cap - j;
656 }
657
658 /*
659 * find_dp - Search for a @vcn in Dirty Page Table.
660 */
find_dp(struct RESTART_TABLE * dptbl,u32 target_attr,u64 vcn)661 static inline struct DIR_PAGE_ENTRY *find_dp(struct RESTART_TABLE *dptbl,
662 u32 target_attr, u64 vcn)
663 {
664 __le32 ta = cpu_to_le32(target_attr);
665 struct DIR_PAGE_ENTRY *dp = NULL;
666
667 while ((dp = enum_rstbl(dptbl, dp))) {
668 u64 dp_vcn = le64_to_cpu(dp->vcn);
669
670 if (dp->target_attr == ta && vcn >= dp_vcn &&
671 vcn < dp_vcn + le32_to_cpu(dp->lcns_follow)) {
672 return dp;
673 }
674 }
675 return NULL;
676 }
677
norm_file_page(u32 page_size,u32 * l_size,bool use_default)678 static inline u32 norm_file_page(u32 page_size, u32 *l_size, bool use_default)
679 {
680 if (use_default)
681 page_size = DefaultLogPageSize;
682
683 /* Round the file size down to a system page boundary. */
684 *l_size &= ~(page_size - 1);
685
686 /* File should contain at least 2 restart pages and MinLogRecordPages pages. */
687 if (*l_size < (MinLogRecordPages + 2) * page_size)
688 return 0;
689
690 return page_size;
691 }
692
check_log_rec(const struct LOG_REC_HDR * lr,u32 bytes,u32 tr,u32 bytes_per_attr_entry)693 static bool check_log_rec(const struct LOG_REC_HDR *lr, u32 bytes, u32 tr,
694 u32 bytes_per_attr_entry)
695 {
696 u16 t16;
697
698 if (bytes < sizeof(struct LOG_REC_HDR))
699 return false;
700 if (!tr)
701 return false;
702
703 if ((tr - sizeof(struct RESTART_TABLE)) %
704 sizeof(struct TRANSACTION_ENTRY))
705 return false;
706
707 if (le16_to_cpu(lr->redo_off) & 7)
708 return false;
709
710 if (le16_to_cpu(lr->undo_off) & 7)
711 return false;
712
713 if (lr->target_attr)
714 goto check_lcns;
715
716 if (is_target_required(le16_to_cpu(lr->redo_op)))
717 return false;
718
719 if (is_target_required(le16_to_cpu(lr->undo_op)))
720 return false;
721
722 check_lcns:
723 if (!lr->lcns_follow)
724 goto check_length;
725
726 t16 = le16_to_cpu(lr->target_attr);
727 if ((t16 - sizeof(struct RESTART_TABLE)) % bytes_per_attr_entry)
728 return false;
729
730 check_length:
731 if (bytes < lrh_length(lr))
732 return false;
733
734 return true;
735 }
736
check_rstbl(const struct RESTART_TABLE * rt,size_t bytes)737 static bool check_rstbl(const struct RESTART_TABLE *rt, size_t bytes)
738 {
739 u32 ts;
740 u32 i, off;
741 u16 rsize = le16_to_cpu(rt->size);
742 u16 ne = le16_to_cpu(rt->used);
743 u32 ff = le32_to_cpu(rt->first_free);
744 u32 lf = le32_to_cpu(rt->last_free);
745
746 ts = rsize * ne + sizeof(struct RESTART_TABLE);
747
748 if (!rsize || rsize > bytes ||
749 rsize + sizeof(struct RESTART_TABLE) > bytes || bytes < ts ||
750 le16_to_cpu(rt->total) > ne || ff > ts - sizeof(__le32) ||
751 lf > ts - sizeof(__le32) ||
752 (ff && ff < sizeof(struct RESTART_TABLE)) ||
753 (lf && lf < sizeof(struct RESTART_TABLE))) {
754 return false;
755 }
756
757 /*
758 * Verify each entry is either allocated or points
759 * to a valid offset the table.
760 */
761 for (i = 0; i < ne; i++) {
762 off = le32_to_cpu(*(__le32 *)Add2Ptr(
763 rt, i * rsize + sizeof(struct RESTART_TABLE)));
764
765 if (off != RESTART_ENTRY_ALLOCATED && off &&
766 (off < sizeof(struct RESTART_TABLE) ||
767 ((off - sizeof(struct RESTART_TABLE)) % rsize))) {
768 return false;
769 }
770 }
771
772 /*
773 * Walk through the list headed by the first entry to make
774 * sure none of the entries are currently being used.
775 *
776 * Bound traversal by ne (rt->used) to defeat a crafted on-disk
777 * cycle in the free chain. Each entry in a legitimate free
778 * list is unique, so a chain that visits more than ne slots
779 * is malformed. Without this guard, an attacker-controlled
780 * RESTART_TABLE with a self-loop or A->B->A cycle whose
781 * offsets satisfy the existing alignment + in-bounds guards
782 * spins forever at mount time.
783 */
784 for (off = ff, i = 0; off; i++) {
785 if (i > ne)
786 return false;
787
788 if (off == RESTART_ENTRY_ALLOCATED)
789 return false;
790
791 off = le32_to_cpu(*(__le32 *)Add2Ptr(rt, off));
792
793 if (off > ts - sizeof(__le32))
794 return false;
795 }
796
797 return true;
798 }
799
check_dp_table(const struct RESTART_TABLE * dptbl)800 static bool check_dp_table(const struct RESTART_TABLE *dptbl)
801 {
802 u32 rsize = le16_to_cpu(dptbl->size);
803 struct DIR_PAGE_ENTRY *dp = NULL;
804
805 while ((dp = enum_rstbl((struct RESTART_TABLE *)dptbl, dp))) {
806 if (struct_size(dp, page_lcns, le32_to_cpu(dp->lcns_follow)) >
807 rsize)
808 return false;
809 }
810
811 return true;
812 }
813
814 /*
815 * free_rsttbl_idx - Free a previously allocated index a Restart Table.
816 */
free_rsttbl_idx(struct RESTART_TABLE * rt,u32 off)817 static inline void free_rsttbl_idx(struct RESTART_TABLE *rt, u32 off)
818 {
819 __le32 *e;
820 u32 lf = le32_to_cpu(rt->last_free);
821 __le32 off_le = cpu_to_le32(off);
822
823 e = Add2Ptr(rt, off);
824
825 if (off < le32_to_cpu(rt->free_goal)) {
826 *e = rt->first_free;
827 rt->first_free = off_le;
828 if (!lf)
829 rt->last_free = off_le;
830 } else {
831 if (lf)
832 *(__le32 *)Add2Ptr(rt, lf) = off_le;
833 else
834 rt->first_free = off_le;
835
836 rt->last_free = off_le;
837 *e = 0;
838 }
839
840 le16_sub_cpu(&rt->total, 1);
841 }
842
init_rsttbl(u16 esize,u16 used)843 static inline struct RESTART_TABLE *init_rsttbl(u16 esize, u16 used)
844 {
845 __le32 *e, *last_free;
846 u32 off;
847 u32 bytes = esize * used + sizeof(struct RESTART_TABLE);
848 u32 lf = sizeof(struct RESTART_TABLE) + (used - 1) * esize;
849 struct RESTART_TABLE *t = kzalloc(bytes, GFP_NOFS);
850
851 if (!t)
852 return NULL;
853
854 t->size = cpu_to_le16(esize);
855 t->used = cpu_to_le16(used);
856 t->free_goal = cpu_to_le32(~0u);
857 t->first_free = cpu_to_le32(sizeof(struct RESTART_TABLE));
858 t->last_free = cpu_to_le32(lf);
859
860 e = (__le32 *)(t + 1);
861 last_free = Add2Ptr(t, lf);
862
863 for (off = sizeof(struct RESTART_TABLE) + esize; e < last_free;
864 e = Add2Ptr(e, esize), off += esize) {
865 *e = cpu_to_le32(off);
866 }
867 return t;
868 }
869
extend_rsttbl(struct RESTART_TABLE * tbl,u32 add,u32 free_goal)870 static inline struct RESTART_TABLE *extend_rsttbl(struct RESTART_TABLE *tbl,
871 u32 add, u32 free_goal)
872 {
873 u16 esize = le16_to_cpu(tbl->size);
874 __le32 osize = cpu_to_le32(bytes_per_rt(tbl));
875 u32 used = le16_to_cpu(tbl->used);
876 struct RESTART_TABLE *rt;
877
878 if (used + add > U16_MAX)
879 return NULL;
880
881 rt = init_rsttbl(esize, used + add);
882 if (!rt)
883 return NULL;
884
885 memcpy(rt + 1, tbl + 1, esize * used);
886
887 rt->free_goal = free_goal == ~0u ?
888 cpu_to_le32(~0u) :
889 cpu_to_le32(sizeof(struct RESTART_TABLE) +
890 free_goal * esize);
891
892 if (tbl->first_free) {
893 rt->first_free = tbl->first_free;
894 *(__le32 *)Add2Ptr(rt, le32_to_cpu(tbl->last_free)) = osize;
895 } else {
896 rt->first_free = osize;
897 }
898
899 rt->total = tbl->total;
900
901 kfree(tbl);
902 return rt;
903 }
904
905 /*
906 * alloc_rsttbl_idx
907 *
908 * Allocate an index from within a previously initialized Restart Table.
909 */
alloc_rsttbl_idx(struct RESTART_TABLE ** tbl)910 static inline void *alloc_rsttbl_idx(struct RESTART_TABLE **tbl)
911 {
912 u32 off;
913 __le32 *e;
914 struct RESTART_TABLE *t = *tbl;
915
916 if (!t->first_free) {
917 *tbl = t = extend_rsttbl(t, 16, ~0u);
918 if (!t)
919 return NULL;
920 }
921
922 off = le32_to_cpu(t->first_free);
923
924 /* Dequeue this entry and zero it. */
925 e = Add2Ptr(t, off);
926
927 t->first_free = *e;
928
929 memset(e, 0, le16_to_cpu(t->size));
930
931 *e = RESTART_ENTRY_ALLOCATED_LE;
932
933 /* If list is going empty, then we fix the last_free as well. */
934 if (!t->first_free)
935 t->last_free = 0;
936
937 le16_add_cpu(&t->total, 1);
938
939 return Add2Ptr(t, off);
940 }
941
942 /*
943 * alloc_rsttbl_from_idx
944 *
945 * Allocate a specific index from within a previously initialized Restart Table.
946 */
alloc_rsttbl_from_idx(struct RESTART_TABLE ** tbl,u32 vbo)947 static inline void *alloc_rsttbl_from_idx(struct RESTART_TABLE **tbl, u32 vbo)
948 {
949 u32 off;
950 __le32 *e;
951 struct RESTART_TABLE *rt = *tbl;
952 u32 bytes = bytes_per_rt(rt);
953 u16 esize = le16_to_cpu(rt->size);
954
955 /* If the entry is not the table, we will have to extend the table. */
956 if (vbo >= bytes) {
957 /*
958 * Extend the size by computing the number of entries between
959 * the existing size and the desired index and adding 1 to that.
960 */
961 u32 bytes2idx = vbo - bytes;
962
963 /*
964 * There should always be an integral number of entries
965 * being added. Now extend the table.
966 */
967 *tbl = rt = extend_rsttbl(rt, bytes2idx / esize + 1, bytes);
968 if (!rt)
969 return NULL;
970 }
971
972 /* See if the entry is already allocated, and just return if it is. */
973 e = Add2Ptr(rt, vbo);
974
975 if (*e == RESTART_ENTRY_ALLOCATED_LE)
976 return e;
977
978 /*
979 * Walk through the table, looking for the entry we're
980 * interested and the previous entry.
981 */
982 off = le32_to_cpu(rt->first_free);
983 e = Add2Ptr(rt, off);
984
985 if (off == vbo) {
986 /* this is a match */
987 rt->first_free = *e;
988 goto skip_looking;
989 }
990
991 /*
992 * Need to walk through the list looking for the predecessor
993 * of our entry.
994 */
995 for (;;) {
996 /* Remember the entry just found */
997 u32 last_off = off;
998 __le32 *last_e = e;
999
1000 /* Should never run of entries. */
1001
1002 /* Lookup up the next entry the list. */
1003 off = le32_to_cpu(*last_e);
1004 e = Add2Ptr(rt, off);
1005
1006 /* If this is our match we are done. */
1007 if (off == vbo) {
1008 *last_e = *e;
1009
1010 /*
1011 * If this was the last entry, we update that
1012 * table as well.
1013 */
1014 if (le32_to_cpu(rt->last_free) == off)
1015 rt->last_free = cpu_to_le32(last_off);
1016 break;
1017 }
1018 }
1019
1020 skip_looking:
1021 /* If the list is now empty, we fix the last_free as well. */
1022 if (!rt->first_free)
1023 rt->last_free = 0;
1024
1025 /* Zero this entry. */
1026 memset(e, 0, esize);
1027 *e = RESTART_ENTRY_ALLOCATED_LE;
1028
1029 le16_add_cpu(&rt->total, 1);
1030
1031 return e;
1032 }
1033
1034 struct restart_info {
1035 u64 last_lsn;
1036 struct RESTART_HDR *r_page;
1037 u32 vbo;
1038 bool chkdsk_was_run;
1039 bool valid_page;
1040 bool initialized;
1041 bool restart;
1042 };
1043
1044 #define RESTART_SINGLE_PAGE_IO cpu_to_le16(0x0001)
1045
1046 #define NTFSLOG_WRAPPED 0x00000001
1047 #define NTFSLOG_MULTIPLE_PAGE_IO 0x00000002
1048 #define NTFSLOG_NO_LAST_LSN 0x00000004
1049 #define NTFSLOG_REUSE_TAIL 0x00000010
1050 #define NTFSLOG_NO_OLDEST_LSN 0x00000020
1051
1052 /* Helper struct to work with NTFS $LogFile. */
1053 struct ntfs_log {
1054 struct ntfs_inode *ni;
1055
1056 u32 l_size;
1057 u32 orig_file_size;
1058 u32 sys_page_size;
1059 u32 sys_page_mask;
1060 u32 page_size;
1061 u32 page_mask; // page_size - 1
1062 u8 page_bits;
1063 struct RECORD_PAGE_HDR *one_page_buf;
1064
1065 struct RESTART_TABLE *open_attr_tbl;
1066 u32 transaction_id;
1067 u32 clst_per_page;
1068
1069 u32 first_page;
1070 u32 next_page;
1071 u32 ra_off;
1072 u32 data_off;
1073 u32 restart_size;
1074 u32 data_size;
1075 u16 record_header_len;
1076 u64 seq_num;
1077 u32 seq_num_bits;
1078 u32 file_data_bits;
1079 u32 seq_num_mask; /* (1 << file_data_bits) - 1 */
1080
1081 struct RESTART_AREA *ra; /* In-memory image of the next restart area. */
1082 u32 ra_size; /* The usable size of the restart area. */
1083
1084 /*
1085 * If true, then the in-memory restart area is to be written
1086 * to the first position on the disk.
1087 */
1088 bool init_ra;
1089 bool set_dirty; /* True if we need to set dirty flag. */
1090
1091 u64 oldest_lsn;
1092
1093 u32 oldest_lsn_off;
1094 u64 last_lsn;
1095
1096 u32 total_avail;
1097 u32 total_avail_pages;
1098 u32 total_undo_commit;
1099 u32 max_current_avail;
1100 u32 current_avail;
1101 u32 reserved;
1102
1103 short major_ver;
1104 short minor_ver;
1105
1106 u32 l_flags; /* See NTFSLOG_XXX */
1107 u32 current_openlog_count; /* On-disk value for open_log_count. */
1108
1109 struct CLIENT_ID client_id;
1110 u32 client_undo_commit;
1111
1112 struct restart_info rst_info, rst_info2;
1113
1114 struct file_ra_state read_ahead;
1115 };
1116
lsn_to_vbo(struct ntfs_log * log,const u64 lsn)1117 static inline u32 lsn_to_vbo(struct ntfs_log *log, const u64 lsn)
1118 {
1119 u32 vbo = (lsn << log->seq_num_bits) >> (log->seq_num_bits - 3);
1120
1121 return vbo;
1122 }
1123
1124 /* Compute the offset in the log file of the next log page. */
next_page_off(struct ntfs_log * log,u32 off)1125 static inline u32 next_page_off(struct ntfs_log *log, u32 off)
1126 {
1127 off = (off & ~log->sys_page_mask) + log->page_size;
1128 return off >= log->l_size ? log->first_page : off;
1129 }
1130
lsn_to_page_off(struct ntfs_log * log,u64 lsn)1131 static inline u32 lsn_to_page_off(struct ntfs_log *log, u64 lsn)
1132 {
1133 return (((u32)lsn) << 3) & log->page_mask;
1134 }
1135
vbo_to_lsn(struct ntfs_log * log,u32 off,u64 Seq)1136 static inline u64 vbo_to_lsn(struct ntfs_log *log, u32 off, u64 Seq)
1137 {
1138 return (off >> 3) + (Seq << log->file_data_bits);
1139 }
1140
is_lsn_in_file(struct ntfs_log * log,u64 lsn)1141 static inline bool is_lsn_in_file(struct ntfs_log *log, u64 lsn)
1142 {
1143 return lsn >= log->oldest_lsn &&
1144 lsn <= le64_to_cpu(log->ra->current_lsn);
1145 }
1146
hdr_file_off(struct ntfs_log * log,struct RECORD_PAGE_HDR * hdr)1147 static inline u32 hdr_file_off(struct ntfs_log *log,
1148 struct RECORD_PAGE_HDR *hdr)
1149 {
1150 if (log->major_ver < 2)
1151 return le64_to_cpu(hdr->rhdr.lsn);
1152
1153 return le32_to_cpu(hdr->file_off);
1154 }
1155
base_lsn(struct ntfs_log * log,const struct RECORD_PAGE_HDR * hdr,u64 lsn)1156 static inline u64 base_lsn(struct ntfs_log *log,
1157 const struct RECORD_PAGE_HDR *hdr, u64 lsn)
1158 {
1159 u64 h_lsn = le64_to_cpu(hdr->rhdr.lsn);
1160 u64 ret = (((h_lsn >> log->file_data_bits) +
1161 (lsn < (lsn_to_vbo(log, h_lsn) & ~log->page_mask) ? 1 : 0))
1162 << log->file_data_bits) +
1163 ((((is_log_record_end(hdr) &&
1164 h_lsn <= le64_to_cpu(hdr->record_hdr.last_end_lsn)) ?
1165 le16_to_cpu(hdr->record_hdr.next_record_off) :
1166 log->page_size) +
1167 lsn) >>
1168 3);
1169
1170 return ret;
1171 }
1172
verify_client_lsn(struct ntfs_log * log,const struct CLIENT_REC * client,u64 lsn)1173 static inline bool verify_client_lsn(struct ntfs_log *log,
1174 const struct CLIENT_REC *client, u64 lsn)
1175 {
1176 return lsn >= le64_to_cpu(client->oldest_lsn) &&
1177 lsn <= le64_to_cpu(log->ra->current_lsn) && lsn;
1178 }
1179
read_log_page(struct ntfs_log * log,u32 vbo,struct RECORD_PAGE_HDR ** buffer,bool * usa_error)1180 static int read_log_page(struct ntfs_log *log, u32 vbo,
1181 struct RECORD_PAGE_HDR **buffer, bool *usa_error)
1182 {
1183 int err = 0;
1184 u32 page_idx = vbo >> log->page_bits;
1185 u32 page_off = vbo & log->page_mask;
1186 u32 bytes = log->page_size - page_off;
1187 void *to_free = NULL;
1188 u32 page_vbo = page_idx << log->page_bits;
1189 struct RECORD_PAGE_HDR *page_buf;
1190 struct ntfs_inode *ni = log->ni;
1191 bool bBAAD;
1192
1193 if (vbo >= log->l_size)
1194 return -EINVAL;
1195
1196 if (!*buffer) {
1197 to_free = kmalloc(log->page_size, GFP_NOFS);
1198 if (!to_free)
1199 return -ENOMEM;
1200 *buffer = to_free;
1201 }
1202
1203 page_buf = page_off ? log->one_page_buf : *buffer;
1204
1205 err = ntfs_read_run_nb_ra(ni->mi.sbi, &ni->file.run, page_vbo, page_buf,
1206 log->page_size, NULL, &log->read_ahead);
1207 if (err)
1208 goto out;
1209
1210 if (page_buf->rhdr.sign != NTFS_FFFF_SIGNATURE)
1211 ntfs_fix_post_read(&page_buf->rhdr, log->page_size, false);
1212
1213 if (page_buf != *buffer)
1214 memcpy(*buffer, Add2Ptr(page_buf, page_off), bytes);
1215
1216 bBAAD = page_buf->rhdr.sign == NTFS_BAAD_SIGNATURE;
1217
1218 if (usa_error)
1219 *usa_error = bBAAD;
1220 /* Check that the update sequence array for this page is valid */
1221 /* If we don't allow errors, raise an error status */
1222 else if (bBAAD)
1223 err = -EINVAL;
1224
1225 out:
1226 if (err && to_free) {
1227 kfree(to_free);
1228 *buffer = NULL;
1229 }
1230
1231 return err;
1232 }
1233
1234 /*
1235 * log_read_rst
1236 *
1237 * It walks through 512 blocks of the file looking for a valid
1238 * restart page header. It will stop the first time we find a
1239 * valid page header.
1240 */
log_read_rst(struct ntfs_log * log,bool first,struct restart_info * info)1241 static int log_read_rst(struct ntfs_log *log, bool first,
1242 struct restart_info *info)
1243 {
1244 u32 skip;
1245 u64 vbo;
1246 struct RESTART_HDR *r_page = NULL;
1247
1248 /* Determine which restart area we are looking for. */
1249 if (first) {
1250 vbo = 0;
1251 skip = 512;
1252 } else {
1253 vbo = 512;
1254 skip = 0;
1255 }
1256
1257 /* Loop continuously until we succeed. */
1258 for (; vbo < log->l_size; vbo = 2 * vbo + skip, skip = 0) {
1259 bool usa_error;
1260 bool brst, bchk;
1261 struct RESTART_AREA *ra;
1262
1263 /* Read a page header at the current offset. */
1264 if (read_log_page(log, vbo, (struct RECORD_PAGE_HDR **)&r_page,
1265 &usa_error)) {
1266 /* Ignore any errors. */
1267 continue;
1268 }
1269
1270 /* Exit if the signature is a log record page. */
1271 if (r_page->rhdr.sign == NTFS_RCRD_SIGNATURE) {
1272 info->initialized = true;
1273 break;
1274 }
1275
1276 brst = r_page->rhdr.sign == NTFS_RSTR_SIGNATURE;
1277 bchk = r_page->rhdr.sign == NTFS_CHKD_SIGNATURE;
1278
1279 if (!bchk && !brst) {
1280 if (r_page->rhdr.sign != NTFS_FFFF_SIGNATURE) {
1281 /*
1282 * Remember if the signature does not
1283 * indicate uninitialized file.
1284 */
1285 info->initialized = true;
1286 }
1287 continue;
1288 }
1289
1290 ra = NULL;
1291 info->valid_page = false;
1292 info->initialized = true;
1293 info->vbo = vbo;
1294
1295 /* Let's check the restart area if this is a valid page. */
1296 if (!is_rst_page_hdr_valid(vbo, r_page))
1297 goto check_result;
1298 ra = Add2Ptr(r_page, le16_to_cpu(r_page->ra_off));
1299
1300 if (!is_rst_area_valid(r_page))
1301 goto check_result;
1302
1303 /*
1304 * We have a valid restart page header and restart area.
1305 * If chkdsk was run or we have no clients then we have
1306 * no more checking to do.
1307 */
1308 if (bchk || ra->client_idx[1] == LFS_NO_CLIENT_LE) {
1309 info->valid_page = true;
1310 goto check_result;
1311 }
1312
1313 if (is_client_area_valid(r_page, usa_error)) {
1314 info->valid_page = true;
1315 ra = Add2Ptr(r_page, le16_to_cpu(r_page->ra_off));
1316 }
1317
1318 check_result:
1319 /*
1320 * If chkdsk was run then update the caller's
1321 * values and return.
1322 */
1323 if (r_page->rhdr.sign == NTFS_CHKD_SIGNATURE) {
1324 info->chkdsk_was_run = true;
1325 info->last_lsn = le64_to_cpu(r_page->rhdr.lsn);
1326 info->restart = true;
1327 info->r_page = r_page;
1328 return 0;
1329 }
1330
1331 /*
1332 * If we have a valid page then copy the values
1333 * we need from it.
1334 */
1335 if (info->valid_page) {
1336 info->last_lsn = le64_to_cpu(ra->current_lsn);
1337 info->restart = true;
1338 info->r_page = r_page;
1339 return 0;
1340 }
1341 }
1342
1343 kfree(r_page);
1344
1345 return 0;
1346 }
1347
1348 /*
1349 * Ilog_init_pg_hdr - Init @log from restart page header.
1350 */
log_init_pg_hdr(struct ntfs_log * log,u16 major_ver,u16 minor_ver)1351 static void log_init_pg_hdr(struct ntfs_log *log, u16 major_ver, u16 minor_ver)
1352 {
1353 log->sys_page_size = log->page_size;
1354 log->sys_page_mask = log->page_mask;
1355
1356 log->clst_per_page = log->page_size >> log->ni->mi.sbi->cluster_bits;
1357 if (!log->clst_per_page)
1358 log->clst_per_page = 1;
1359
1360 log->first_page = major_ver >= 2 ? 0x22 * log->page_size :
1361 4 * log->page_size;
1362 log->major_ver = major_ver;
1363 log->minor_ver = minor_ver;
1364 }
1365
1366 /*
1367 * log_create - Init @log in cases when we don't have a restart area to use.
1368 */
log_create(struct ntfs_log * log,const u64 last_lsn,u32 open_log_count,bool wrapped,bool use_multi_page)1369 static void log_create(struct ntfs_log *log, const u64 last_lsn,
1370 u32 open_log_count, bool wrapped, bool use_multi_page)
1371 {
1372 /* All file offsets must be quadword aligned. */
1373 log->file_data_bits = blksize_bits(log->l_size) - 3;
1374 log->seq_num_mask = (8 << log->file_data_bits) - 1;
1375 log->seq_num_bits = sizeof(u64) * 8 - log->file_data_bits;
1376 log->seq_num = (last_lsn >> log->file_data_bits) + 2;
1377 log->next_page = log->first_page;
1378 log->oldest_lsn = log->seq_num << log->file_data_bits;
1379 log->oldest_lsn_off = 0;
1380 log->last_lsn = log->oldest_lsn;
1381
1382 log->l_flags |= NTFSLOG_NO_LAST_LSN | NTFSLOG_NO_OLDEST_LSN;
1383
1384 /* Set the correct flags for the I/O and indicate if we have wrapped. */
1385 if (wrapped)
1386 log->l_flags |= NTFSLOG_WRAPPED;
1387
1388 if (use_multi_page)
1389 log->l_flags |= NTFSLOG_MULTIPLE_PAGE_IO;
1390
1391 /* Compute the log page values. */
1392 log->data_off = ALIGN(
1393 offsetof(struct RECORD_PAGE_HDR, fixups) +
1394 sizeof(short) * ((log->page_size >> SECTOR_SHIFT) + 1),
1395 8);
1396 log->data_size = log->page_size - log->data_off;
1397 log->record_header_len = sizeof(struct LFS_RECORD_HDR);
1398
1399 /* Remember the different page sizes for reservation. */
1400 log->reserved = log->data_size - log->record_header_len;
1401
1402 /* Compute the restart page values. */
1403 log->ra_off = ALIGN(
1404 offsetof(struct RESTART_HDR, fixups) +
1405 sizeof(short) *
1406 ((log->sys_page_size >> SECTOR_SHIFT) + 1),
1407 8);
1408 log->restart_size = log->sys_page_size - log->ra_off;
1409 log->ra_size = struct_size(log->ra, clients, 1);
1410 log->current_openlog_count = open_log_count;
1411
1412 /*
1413 * The total available log file space is the number of
1414 * log file pages times the space available on each page.
1415 */
1416 log->total_avail_pages = log->l_size - log->first_page;
1417 log->total_avail = log->total_avail_pages >> log->page_bits;
1418
1419 /*
1420 * We assume that we can't use the end of the page less than
1421 * the file record size.
1422 * Then we won't need to reserve more than the caller asks for.
1423 */
1424 log->max_current_avail = log->total_avail * log->reserved;
1425 log->total_avail = log->total_avail * log->data_size;
1426 log->current_avail = log->max_current_avail;
1427 }
1428
1429 /*
1430 * log_create_ra - Fill a restart area from the values stored in @log.
1431 */
log_create_ra(struct ntfs_log * log)1432 static struct RESTART_AREA *log_create_ra(struct ntfs_log *log)
1433 {
1434 struct CLIENT_REC *cr;
1435 struct RESTART_AREA *ra = kzalloc(log->restart_size, GFP_NOFS);
1436
1437 if (!ra)
1438 return NULL;
1439
1440 ra->current_lsn = cpu_to_le64(log->last_lsn);
1441 ra->log_clients = cpu_to_le16(1);
1442 ra->client_idx[1] = LFS_NO_CLIENT_LE;
1443 if (log->l_flags & NTFSLOG_MULTIPLE_PAGE_IO)
1444 ra->flags = RESTART_SINGLE_PAGE_IO;
1445 ra->seq_num_bits = cpu_to_le32(log->seq_num_bits);
1446 ra->ra_len = cpu_to_le16(log->ra_size);
1447 ra->client_off = cpu_to_le16(offsetof(struct RESTART_AREA, clients));
1448 ra->l_size = cpu_to_le64(log->l_size);
1449 ra->rec_hdr_len = cpu_to_le16(log->record_header_len);
1450 ra->data_off = cpu_to_le16(log->data_off);
1451 ra->open_log_count = cpu_to_le32(log->current_openlog_count + 1);
1452
1453 cr = ra->clients;
1454
1455 cr->prev_client = LFS_NO_CLIENT_LE;
1456 cr->next_client = LFS_NO_CLIENT_LE;
1457
1458 return ra;
1459 }
1460
final_log_off(struct ntfs_log * log,u64 lsn,u32 data_len)1461 static u32 final_log_off(struct ntfs_log *log, u64 lsn, u32 data_len)
1462 {
1463 u32 base_vbo = lsn << 3;
1464 u32 final_log_off = (base_vbo & log->seq_num_mask) & ~log->page_mask;
1465 u32 page_off = base_vbo & log->page_mask;
1466 u32 tail = log->page_size - page_off;
1467
1468 page_off -= 1;
1469
1470 /* Add the length of the header. */
1471 data_len += log->record_header_len;
1472
1473 /*
1474 * If this lsn is contained this log page we are done.
1475 * Otherwise we need to walk through several log pages.
1476 */
1477 if (data_len > tail) {
1478 data_len -= tail;
1479 tail = log->data_size;
1480 page_off = log->data_off - 1;
1481
1482 for (;;) {
1483 final_log_off = next_page_off(log, final_log_off);
1484
1485 /*
1486 * We are done if the remaining bytes
1487 * fit on this page.
1488 */
1489 if (data_len <= tail)
1490 break;
1491 data_len -= tail;
1492 }
1493 }
1494
1495 /*
1496 * We add the remaining bytes to our starting position on this page
1497 * and then add that value to the file offset of this log page.
1498 */
1499 return final_log_off + data_len + page_off;
1500 }
1501
next_log_lsn(struct ntfs_log * log,const struct LFS_RECORD_HDR * rh,u64 * lsn)1502 static int next_log_lsn(struct ntfs_log *log, const struct LFS_RECORD_HDR *rh,
1503 u64 *lsn)
1504 {
1505 int err;
1506 u64 this_lsn = le64_to_cpu(rh->this_lsn);
1507 u32 vbo = lsn_to_vbo(log, this_lsn);
1508 u32 end =
1509 final_log_off(log, this_lsn, le32_to_cpu(rh->client_data_len));
1510 u32 hdr_off = end & ~log->sys_page_mask;
1511 u64 seq = this_lsn >> log->file_data_bits;
1512 struct RECORD_PAGE_HDR *page = NULL;
1513
1514 /* Remember if we wrapped. */
1515 if (end <= vbo)
1516 seq += 1;
1517
1518 /* Log page header for this page. */
1519 err = read_log_page(log, hdr_off, &page, NULL);
1520 if (err)
1521 return err;
1522
1523 /*
1524 * If the lsn we were given was not the last lsn on this page,
1525 * then the starting offset for the next lsn is on a quad word
1526 * boundary following the last file offset for the current lsn.
1527 * Otherwise the file offset is the start of the data on the next page.
1528 */
1529 if (this_lsn == le64_to_cpu(page->rhdr.lsn)) {
1530 /* If we wrapped, we need to increment the sequence number. */
1531 hdr_off = next_page_off(log, hdr_off);
1532 if (hdr_off == log->first_page)
1533 seq += 1;
1534
1535 vbo = hdr_off + log->data_off;
1536 } else {
1537 vbo = ALIGN(end, 8);
1538 }
1539
1540 /* Compute the lsn based on the file offset and the sequence count. */
1541 *lsn = vbo_to_lsn(log, vbo, seq);
1542
1543 /*
1544 * If this lsn is within the legal range for the file, we return true.
1545 * Otherwise false indicates that there are no more lsn's.
1546 */
1547 if (!is_lsn_in_file(log, *lsn))
1548 *lsn = 0;
1549
1550 kfree(page);
1551
1552 return 0;
1553 }
1554
1555 /*
1556 * current_log_avail - Calculate the number of bytes available for log records.
1557 */
current_log_avail(struct ntfs_log * log)1558 static u32 current_log_avail(struct ntfs_log *log)
1559 {
1560 u32 oldest_off, next_free_off, free_bytes;
1561
1562 if (log->l_flags & NTFSLOG_NO_LAST_LSN) {
1563 /* The entire file is available. */
1564 return log->max_current_avail;
1565 }
1566
1567 /*
1568 * If there is a last lsn the restart area then we know that we will
1569 * have to compute the free range.
1570 * If there is no oldest lsn then start at the first page of the file.
1571 */
1572 oldest_off = (log->l_flags & NTFSLOG_NO_OLDEST_LSN) ?
1573 log->first_page :
1574 (log->oldest_lsn_off & ~log->sys_page_mask);
1575
1576 /*
1577 * We will use the next log page offset to compute the next free page.
1578 * If we are going to reuse this page go to the next page.
1579 * If we are at the first page then use the end of the file.
1580 */
1581 next_free_off = (log->l_flags & NTFSLOG_REUSE_TAIL) ?
1582 log->next_page + log->page_size :
1583 log->next_page == log->first_page ? log->l_size :
1584 log->next_page;
1585
1586 /* If the two offsets are the same then there is no available space. */
1587 if (oldest_off == next_free_off)
1588 return 0;
1589 /*
1590 * If the free offset follows the oldest offset then subtract
1591 * this range from the total available pages.
1592 */
1593 free_bytes =
1594 oldest_off < next_free_off ?
1595 log->total_avail_pages - (next_free_off - oldest_off) :
1596 oldest_off - next_free_off;
1597
1598 free_bytes >>= log->page_bits;
1599 return free_bytes * log->reserved;
1600 }
1601
check_subseq_log_page(struct ntfs_log * log,const struct RECORD_PAGE_HDR * rp,u32 vbo,u64 seq)1602 static bool check_subseq_log_page(struct ntfs_log *log,
1603 const struct RECORD_PAGE_HDR *rp, u32 vbo,
1604 u64 seq)
1605 {
1606 u64 lsn_seq;
1607 const struct NTFS_RECORD_HEADER *rhdr = &rp->rhdr;
1608 u64 lsn = le64_to_cpu(rhdr->lsn);
1609
1610 if (rhdr->sign == NTFS_FFFF_SIGNATURE || !rhdr->sign)
1611 return false;
1612
1613 /*
1614 * If the last lsn on the page occurs was written after the page
1615 * that caused the original error then we have a fatal error.
1616 */
1617 lsn_seq = lsn >> log->file_data_bits;
1618
1619 /*
1620 * If the sequence number for the lsn the page is equal or greater
1621 * than lsn we expect, then this is a subsequent write.
1622 */
1623 return lsn_seq >= seq ||
1624 (lsn_seq == seq - 1 && log->first_page == vbo &&
1625 vbo != (lsn_to_vbo(log, lsn) & ~log->page_mask));
1626 }
1627
1628 /*
1629 * last_log_lsn
1630 *
1631 * Walks through the log pages for a file, searching for the
1632 * last log page written to the file.
1633 */
last_log_lsn(struct ntfs_log * log)1634 static int last_log_lsn(struct ntfs_log *log)
1635 {
1636 int err;
1637 bool usa_error = false;
1638 bool replace_page = false;
1639 bool reuse_page = log->l_flags & NTFSLOG_REUSE_TAIL;
1640 bool wrapped_file, wrapped;
1641
1642 u32 page_cnt = 1, page_pos = 1;
1643 u32 page_off = 0, page_off1 = 0, saved_off = 0;
1644 u32 final_off, second_off, final_off_prev = 0, second_off_prev = 0;
1645 u32 first_file_off = 0, second_file_off = 0;
1646 u32 part_io_count = 0;
1647 u32 tails = 0;
1648 u32 this_off, curpage_off, nextpage_off, remain_pages;
1649
1650 u64 expected_seq, seq_base = 0, lsn_base = 0;
1651 u64 best_lsn, best_lsn1, best_lsn2;
1652 u64 lsn_cur, lsn1, lsn2;
1653 u64 last_ok_lsn = reuse_page ? log->last_lsn : 0;
1654
1655 u16 cur_pos, best_page_pos;
1656
1657 struct RECORD_PAGE_HDR *page = NULL;
1658 struct RECORD_PAGE_HDR *tst_page = NULL;
1659 struct RECORD_PAGE_HDR *first_tail = NULL;
1660 struct RECORD_PAGE_HDR *second_tail = NULL;
1661 struct RECORD_PAGE_HDR *tail_page = NULL;
1662 struct RECORD_PAGE_HDR *second_tail_prev = NULL;
1663 struct RECORD_PAGE_HDR *first_tail_prev = NULL;
1664 struct RECORD_PAGE_HDR *page_bufs = NULL;
1665 struct RECORD_PAGE_HDR *best_page;
1666
1667 if (log->major_ver >= 2) {
1668 final_off = 0x02 * log->page_size;
1669 second_off = 0x12 * log->page_size;
1670
1671 // 0x10 == 0x12 - 0x2
1672 page_bufs = kmalloc(log->page_size * 0x10, GFP_NOFS);
1673 if (!page_bufs)
1674 return -ENOMEM;
1675 } else {
1676 second_off = log->first_page - log->page_size;
1677 final_off = second_off - log->page_size;
1678 }
1679
1680 next_tail:
1681 /* Read second tail page (at pos 3/0x12000). */
1682 if (read_log_page(log, second_off, &second_tail, &usa_error) ||
1683 usa_error || second_tail->rhdr.sign != NTFS_RCRD_SIGNATURE) {
1684 kfree(second_tail);
1685 second_tail = NULL;
1686 second_file_off = 0;
1687 lsn2 = 0;
1688 } else {
1689 second_file_off = hdr_file_off(log, second_tail);
1690 lsn2 = le64_to_cpu(second_tail->record_hdr.last_end_lsn);
1691 }
1692
1693 /* Read first tail page (at pos 2/0x2000). */
1694 if (read_log_page(log, final_off, &first_tail, &usa_error) ||
1695 usa_error || first_tail->rhdr.sign != NTFS_RCRD_SIGNATURE) {
1696 kfree(first_tail);
1697 first_tail = NULL;
1698 first_file_off = 0;
1699 lsn1 = 0;
1700 } else {
1701 first_file_off = hdr_file_off(log, first_tail);
1702 lsn1 = le64_to_cpu(first_tail->record_hdr.last_end_lsn);
1703 }
1704
1705 if (log->major_ver < 2) {
1706 int best_page;
1707
1708 first_tail_prev = first_tail;
1709 final_off_prev = first_file_off;
1710 second_tail_prev = second_tail;
1711 second_off_prev = second_file_off;
1712 tails = 1;
1713
1714 if (!first_tail && !second_tail)
1715 goto tail_read;
1716
1717 if (first_tail && second_tail)
1718 best_page = lsn1 < lsn2 ? 1 : 0;
1719 else if (first_tail)
1720 best_page = 0;
1721 else
1722 best_page = 1;
1723
1724 page_off = best_page ? second_file_off : first_file_off;
1725 seq_base = (best_page ? lsn2 : lsn1) >> log->file_data_bits;
1726 goto tail_read;
1727 }
1728
1729 best_lsn1 = first_tail ? base_lsn(log, first_tail, first_file_off) : 0;
1730 best_lsn2 = second_tail ? base_lsn(log, second_tail, second_file_off) :
1731 0;
1732
1733 if (first_tail && second_tail) {
1734 if (best_lsn1 > best_lsn2) {
1735 best_lsn = best_lsn1;
1736 best_page = first_tail;
1737 this_off = first_file_off;
1738 } else {
1739 best_lsn = best_lsn2;
1740 best_page = second_tail;
1741 this_off = second_file_off;
1742 }
1743 } else if (first_tail) {
1744 best_lsn = best_lsn1;
1745 best_page = first_tail;
1746 this_off = first_file_off;
1747 } else if (second_tail) {
1748 best_lsn = best_lsn2;
1749 best_page = second_tail;
1750 this_off = second_file_off;
1751 } else {
1752 goto tail_read;
1753 }
1754
1755 best_page_pos = le16_to_cpu(best_page->page_pos);
1756
1757 if (!tails) {
1758 if (best_page_pos == page_pos) {
1759 seq_base = best_lsn >> log->file_data_bits;
1760 saved_off = page_off = le32_to_cpu(best_page->file_off);
1761 lsn_base = best_lsn;
1762
1763 memmove(page_bufs, best_page, log->page_size);
1764
1765 page_cnt = le16_to_cpu(best_page->page_count);
1766 if (page_cnt > 1)
1767 page_pos += 1;
1768
1769 tails = 1;
1770 }
1771 } else if (seq_base == (best_lsn >> log->file_data_bits) &&
1772 saved_off + log->page_size == this_off &&
1773 lsn_base < best_lsn &&
1774 (page_pos != page_cnt || best_page_pos == page_pos ||
1775 best_page_pos == 1) &&
1776 (page_pos >= page_cnt || best_page_pos == page_pos)) {
1777 u16 bppc = le16_to_cpu(best_page->page_count);
1778
1779 saved_off += log->page_size;
1780 lsn_base = best_lsn;
1781
1782 memmove(Add2Ptr(page_bufs, tails * log->page_size), best_page,
1783 log->page_size);
1784
1785 tails += 1;
1786
1787 if (best_page_pos != bppc) {
1788 page_cnt = bppc;
1789 page_pos = best_page_pos;
1790
1791 if (page_cnt > 1)
1792 page_pos += 1;
1793 } else {
1794 page_pos = page_cnt = 1;
1795 }
1796 } else {
1797 kfree(first_tail);
1798 kfree(second_tail);
1799 goto tail_read;
1800 }
1801
1802 kfree(first_tail_prev);
1803 first_tail_prev = first_tail;
1804 final_off_prev = first_file_off;
1805 first_tail = NULL;
1806
1807 kfree(second_tail_prev);
1808 second_tail_prev = second_tail;
1809 second_off_prev = second_file_off;
1810 second_tail = NULL;
1811
1812 final_off += log->page_size;
1813 second_off += log->page_size;
1814
1815 if (tails < 0x10)
1816 goto next_tail;
1817 tail_read:
1818 first_tail = first_tail_prev;
1819 final_off = final_off_prev;
1820
1821 second_tail = second_tail_prev;
1822 second_off = second_off_prev;
1823
1824 page_cnt = page_pos = 1;
1825
1826 curpage_off = seq_base == log->seq_num ? min(log->next_page, page_off) :
1827 log->next_page;
1828
1829 wrapped_file =
1830 curpage_off == log->first_page &&
1831 !(log->l_flags & (NTFSLOG_NO_LAST_LSN | NTFSLOG_REUSE_TAIL));
1832
1833 expected_seq = wrapped_file ? (log->seq_num + 1) : log->seq_num;
1834
1835 nextpage_off = curpage_off;
1836
1837 next_page:
1838 tail_page = NULL;
1839 /* Read the next log page. */
1840 err = read_log_page(log, curpage_off, &page, &usa_error);
1841
1842 /* Compute the next log page offset the file. */
1843 nextpage_off = next_page_off(log, curpage_off);
1844 wrapped = nextpage_off == log->first_page;
1845
1846 if (tails > 1) {
1847 struct RECORD_PAGE_HDR *cur_page =
1848 Add2Ptr(page_bufs, curpage_off - page_off);
1849
1850 if (curpage_off == saved_off) {
1851 tail_page = cur_page;
1852 goto use_tail_page;
1853 }
1854
1855 if (page_off > curpage_off || curpage_off >= saved_off)
1856 goto use_tail_page;
1857
1858 if (page_off1)
1859 goto use_cur_page;
1860
1861 if (!err && !usa_error &&
1862 page->rhdr.sign == NTFS_RCRD_SIGNATURE &&
1863 cur_page->rhdr.lsn == page->rhdr.lsn &&
1864 cur_page->record_hdr.next_record_off ==
1865 page->record_hdr.next_record_off &&
1866 ((page_pos == page_cnt &&
1867 le16_to_cpu(page->page_pos) == 1) ||
1868 (page_pos != page_cnt &&
1869 le16_to_cpu(page->page_pos) == page_pos + 1 &&
1870 le16_to_cpu(page->page_count) == page_cnt))) {
1871 cur_page = NULL;
1872 goto use_tail_page;
1873 }
1874
1875 page_off1 = page_off;
1876
1877 use_cur_page:
1878
1879 lsn_cur = le64_to_cpu(cur_page->rhdr.lsn);
1880
1881 if (last_ok_lsn !=
1882 le64_to_cpu(cur_page->record_hdr.last_end_lsn) &&
1883 ((lsn_cur >> log->file_data_bits) +
1884 ((curpage_off <
1885 (lsn_to_vbo(log, lsn_cur) & ~log->page_mask)) ?
1886 1 :
1887 0)) != expected_seq) {
1888 goto check_tail;
1889 }
1890
1891 if (!is_log_record_end(cur_page)) {
1892 tail_page = NULL;
1893 last_ok_lsn = lsn_cur;
1894 goto next_page_1;
1895 }
1896
1897 log->seq_num = expected_seq;
1898 log->l_flags &= ~NTFSLOG_NO_LAST_LSN;
1899 log->last_lsn = le64_to_cpu(cur_page->record_hdr.last_end_lsn);
1900 log->ra->current_lsn = cur_page->record_hdr.last_end_lsn;
1901
1902 if (log->record_header_len <=
1903 log->page_size -
1904 le16_to_cpu(cur_page->record_hdr.next_record_off)) {
1905 log->l_flags |= NTFSLOG_REUSE_TAIL;
1906 log->next_page = curpage_off;
1907 } else {
1908 log->l_flags &= ~NTFSLOG_REUSE_TAIL;
1909 log->next_page = nextpage_off;
1910 }
1911
1912 if (wrapped_file)
1913 log->l_flags |= NTFSLOG_WRAPPED;
1914
1915 last_ok_lsn = le64_to_cpu(cur_page->record_hdr.last_end_lsn);
1916 goto next_page_1;
1917 }
1918
1919 /*
1920 * If we are at the expected first page of a transfer check to see
1921 * if either tail copy is at this offset.
1922 * If this page is the last page of a transfer, check if we wrote
1923 * a subsequent tail copy.
1924 */
1925 if (page_cnt == page_pos || page_cnt == page_pos + 1) {
1926 /*
1927 * Check if the offset matches either the first or second
1928 * tail copy. It is possible it will match both.
1929 */
1930 if (curpage_off == final_off)
1931 tail_page = first_tail;
1932
1933 /*
1934 * If we already matched on the first page then
1935 * check the ending lsn's.
1936 */
1937 if (curpage_off == second_off) {
1938 if (!tail_page ||
1939 (second_tail &&
1940 le64_to_cpu(second_tail->record_hdr.last_end_lsn) >
1941 le64_to_cpu(first_tail->record_hdr
1942 .last_end_lsn))) {
1943 tail_page = second_tail;
1944 }
1945 }
1946 }
1947
1948 use_tail_page:
1949 if (tail_page) {
1950 /* We have a candidate for a tail copy. */
1951 lsn_cur = le64_to_cpu(tail_page->record_hdr.last_end_lsn);
1952
1953 if (last_ok_lsn < lsn_cur) {
1954 /*
1955 * If the sequence number is not expected,
1956 * then don't use the tail copy.
1957 */
1958 if (expected_seq != (lsn_cur >> log->file_data_bits))
1959 tail_page = NULL;
1960 } else if (last_ok_lsn > lsn_cur) {
1961 /*
1962 * If the last lsn is greater than the one on
1963 * this page then forget this tail.
1964 */
1965 tail_page = NULL;
1966 }
1967 }
1968
1969 /*
1970 *If we have an error on the current page,
1971 * we will break of this loop.
1972 */
1973 if (err || usa_error)
1974 goto check_tail;
1975
1976 /*
1977 * Done if the last lsn on this page doesn't match the previous known
1978 * last lsn or the sequence number is not expected.
1979 */
1980 lsn_cur = le64_to_cpu(page->rhdr.lsn);
1981 if (last_ok_lsn != lsn_cur &&
1982 expected_seq != (lsn_cur >> log->file_data_bits)) {
1983 goto check_tail;
1984 }
1985
1986 /*
1987 * Check that the page position and page count values are correct.
1988 * If this is the first page of a transfer the position must be 1
1989 * and the count will be unknown.
1990 */
1991 if (page_cnt == page_pos) {
1992 if (page->page_pos != cpu_to_le16(1) &&
1993 (!reuse_page || page->page_pos != page->page_count)) {
1994 /*
1995 * If the current page is the first page we are
1996 * looking at and we are reusing this page then
1997 * it can be either the first or last page of a
1998 * transfer. Otherwise it can only be the first.
1999 */
2000 goto check_tail;
2001 }
2002 } else if (le16_to_cpu(page->page_count) != page_cnt ||
2003 le16_to_cpu(page->page_pos) != page_pos + 1) {
2004 /*
2005 * The page position better be 1 more than the last page
2006 * position and the page count better match.
2007 */
2008 goto check_tail;
2009 }
2010
2011 /*
2012 * We have a valid page the file and may have a valid page
2013 * the tail copy area.
2014 * If the tail page was written after the page the file then
2015 * break of the loop.
2016 */
2017 if (tail_page &&
2018 le64_to_cpu(tail_page->record_hdr.last_end_lsn) > lsn_cur) {
2019 /* Remember if we will replace the page. */
2020 replace_page = true;
2021 goto check_tail;
2022 }
2023
2024 tail_page = NULL;
2025
2026 if (is_log_record_end(page)) {
2027 /*
2028 * Since we have read this page we know the sequence number
2029 * is the same as our expected value.
2030 */
2031 log->seq_num = expected_seq;
2032 log->last_lsn = le64_to_cpu(page->record_hdr.last_end_lsn);
2033 log->ra->current_lsn = page->record_hdr.last_end_lsn;
2034 log->l_flags &= ~NTFSLOG_NO_LAST_LSN;
2035
2036 /*
2037 * If there is room on this page for another header then
2038 * remember we want to reuse the page.
2039 */
2040 if (log->record_header_len <=
2041 log->page_size -
2042 le16_to_cpu(page->record_hdr.next_record_off)) {
2043 log->l_flags |= NTFSLOG_REUSE_TAIL;
2044 log->next_page = curpage_off;
2045 } else {
2046 log->l_flags &= ~NTFSLOG_REUSE_TAIL;
2047 log->next_page = nextpage_off;
2048 }
2049
2050 /* Remember if we wrapped the log file. */
2051 if (wrapped_file)
2052 log->l_flags |= NTFSLOG_WRAPPED;
2053 }
2054
2055 /*
2056 * Remember the last page count and position.
2057 * Also remember the last known lsn.
2058 */
2059 page_cnt = le16_to_cpu(page->page_count);
2060 page_pos = le16_to_cpu(page->page_pos);
2061 last_ok_lsn = le64_to_cpu(page->rhdr.lsn);
2062
2063 next_page_1:
2064
2065 if (wrapped) {
2066 expected_seq += 1;
2067 wrapped_file = 1;
2068 }
2069
2070 curpage_off = nextpage_off;
2071 kfree(page);
2072 page = NULL;
2073 reuse_page = 0;
2074 goto next_page;
2075
2076 check_tail:
2077 if (tail_page) {
2078 log->seq_num = expected_seq;
2079 log->last_lsn = le64_to_cpu(tail_page->record_hdr.last_end_lsn);
2080 log->ra->current_lsn = tail_page->record_hdr.last_end_lsn;
2081 log->l_flags &= ~NTFSLOG_NO_LAST_LSN;
2082
2083 if (log->page_size -
2084 le16_to_cpu(
2085 tail_page->record_hdr.next_record_off) >=
2086 log->record_header_len) {
2087 log->l_flags |= NTFSLOG_REUSE_TAIL;
2088 log->next_page = curpage_off;
2089 } else {
2090 log->l_flags &= ~NTFSLOG_REUSE_TAIL;
2091 log->next_page = nextpage_off;
2092 }
2093
2094 if (wrapped)
2095 log->l_flags |= NTFSLOG_WRAPPED;
2096 }
2097
2098 /* Remember that the partial IO will start at the next page. */
2099 second_off = nextpage_off;
2100
2101 /*
2102 * If the next page is the first page of the file then update
2103 * the sequence number for log records which begon the next page.
2104 */
2105 if (wrapped)
2106 expected_seq += 1;
2107
2108 /*
2109 * If we have a tail copy or are performing single page I/O we can
2110 * immediately look at the next page.
2111 */
2112 if (replace_page || (log->ra->flags & RESTART_SINGLE_PAGE_IO)) {
2113 page_cnt = 2;
2114 page_pos = 1;
2115 goto check_valid;
2116 }
2117
2118 if (page_pos != page_cnt)
2119 goto check_valid;
2120 /*
2121 * If the next page causes us to wrap to the beginning of the log
2122 * file then we know which page to check next.
2123 */
2124 if (wrapped) {
2125 page_cnt = 2;
2126 page_pos = 1;
2127 goto check_valid;
2128 }
2129
2130 cur_pos = 2;
2131
2132 next_test_page:
2133 kfree(tst_page);
2134 tst_page = NULL;
2135
2136 /* Walk through the file, reading log pages. */
2137 err = read_log_page(log, nextpage_off, &tst_page, &usa_error);
2138
2139 /*
2140 * If we get a USA error then assume that we correctly found
2141 * the end of the original transfer.
2142 */
2143 if (usa_error)
2144 goto file_is_valid;
2145
2146 /*
2147 * If we were able to read the page, we examine it to see if it
2148 * is the same or different Io block.
2149 */
2150 if (err)
2151 goto next_test_page_1;
2152
2153 if (le16_to_cpu(tst_page->page_pos) == cur_pos &&
2154 check_subseq_log_page(log, tst_page, nextpage_off, expected_seq)) {
2155 page_cnt = le16_to_cpu(tst_page->page_count) + 1;
2156 page_pos = le16_to_cpu(tst_page->page_pos);
2157 goto check_valid;
2158 } else {
2159 goto file_is_valid;
2160 }
2161
2162 next_test_page_1:
2163
2164 nextpage_off = next_page_off(log, curpage_off);
2165 wrapped = nextpage_off == log->first_page;
2166
2167 if (wrapped) {
2168 expected_seq += 1;
2169 page_cnt = 2;
2170 page_pos = 1;
2171 }
2172
2173 cur_pos += 1;
2174 part_io_count += 1;
2175 if (!wrapped)
2176 goto next_test_page;
2177
2178 check_valid:
2179 /* Skip over the remaining pages this transfer. */
2180 remain_pages = page_cnt - page_pos - 1;
2181 part_io_count += remain_pages;
2182
2183 while (remain_pages--) {
2184 nextpage_off = next_page_off(log, curpage_off);
2185 wrapped = nextpage_off == log->first_page;
2186
2187 if (wrapped)
2188 expected_seq += 1;
2189 }
2190
2191 /* Call our routine to check this log page. */
2192 kfree(tst_page);
2193 tst_page = NULL;
2194
2195 err = read_log_page(log, nextpage_off, &tst_page, &usa_error);
2196 if (!err && !usa_error &&
2197 check_subseq_log_page(log, tst_page, nextpage_off, expected_seq)) {
2198 err = -EINVAL;
2199 goto out;
2200 }
2201
2202 file_is_valid:
2203
2204 /* We have a valid file. */
2205 if (page_off1 || tail_page) {
2206 struct RECORD_PAGE_HDR *tmp_page;
2207
2208 if (sb_rdonly(log->ni->mi.sbi->sb)) {
2209 err = -EROFS;
2210 goto out;
2211 }
2212
2213 if (page_off1) {
2214 tmp_page = Add2Ptr(page_bufs, page_off1 - page_off);
2215 tails -= (page_off1 - page_off) / log->page_size;
2216 if (!tail_page)
2217 tails -= 1;
2218 } else {
2219 tmp_page = tail_page;
2220 tails = 1;
2221 }
2222
2223 while (tails--) {
2224 u64 off = hdr_file_off(log, tmp_page);
2225
2226 if (!page) {
2227 page = kmalloc(log->page_size, GFP_NOFS);
2228 if (!page) {
2229 err = -ENOMEM;
2230 goto out;
2231 }
2232 }
2233
2234 /*
2235 * Correct page and copy the data from this page
2236 * into it and flush it to disk.
2237 */
2238 memcpy(page, tmp_page, log->page_size);
2239
2240 /* Fill last flushed lsn value flush the page. */
2241 if (log->major_ver < 2)
2242 page->rhdr.lsn = page->record_hdr.last_end_lsn;
2243 else
2244 page->file_off = 0;
2245
2246 page->page_pos = page->page_count = cpu_to_le16(1);
2247
2248 ntfs_fix_pre_write(&page->rhdr, log->page_size);
2249
2250 err = ntfs_sb_write_run(log->ni->mi.sbi,
2251 &log->ni->file.run, off, page,
2252 log->page_size, 0);
2253
2254 if (err)
2255 goto out;
2256
2257 if (part_io_count && second_off == off) {
2258 second_off += log->page_size;
2259 part_io_count -= 1;
2260 }
2261
2262 tmp_page = Add2Ptr(tmp_page, log->page_size);
2263 }
2264 }
2265
2266 if (part_io_count) {
2267 if (sb_rdonly(log->ni->mi.sbi->sb)) {
2268 err = -EROFS;
2269 goto out;
2270 }
2271 }
2272
2273 out:
2274 kfree(second_tail);
2275 kfree(first_tail);
2276 kfree(page);
2277 kfree(tst_page);
2278 kfree(page_bufs);
2279
2280 return err;
2281 }
2282
2283 /*
2284 * read_log_rec_buf - Copy a log record from the file to a buffer.
2285 *
2286 * The log record may span several log pages and may even wrap the file.
2287 */
read_log_rec_buf(struct ntfs_log * log,const struct LFS_RECORD_HDR * rh,void * buffer)2288 static int read_log_rec_buf(struct ntfs_log *log,
2289 const struct LFS_RECORD_HDR *rh, void *buffer)
2290 {
2291 int err;
2292 struct RECORD_PAGE_HDR *ph = NULL;
2293 u64 lsn = le64_to_cpu(rh->this_lsn);
2294 u32 vbo = lsn_to_vbo(log, lsn) & ~log->page_mask;
2295 u32 off = lsn_to_page_off(log, lsn) + log->record_header_len;
2296 u32 data_len = le32_to_cpu(rh->client_data_len);
2297
2298 /*
2299 * While there are more bytes to transfer,
2300 * we continue to attempt to perform the read.
2301 */
2302 for (;;) {
2303 bool usa_error;
2304 u32 tail;
2305
2306 /* off comes from the on-disk restart area; bound it. */
2307 if (off > log->page_size) {
2308 err = -EINVAL;
2309 goto out;
2310 }
2311
2312 tail = log->page_size - off;
2313
2314 if (tail >= data_len)
2315 tail = data_len;
2316
2317 data_len -= tail;
2318
2319 err = read_log_page(log, vbo, &ph, &usa_error);
2320 if (err)
2321 goto out;
2322
2323 /*
2324 * The last lsn on this page better be greater or equal
2325 * to the lsn we are copying.
2326 */
2327 if (lsn > le64_to_cpu(ph->rhdr.lsn)) {
2328 err = -EINVAL;
2329 goto out;
2330 }
2331
2332 memcpy(buffer, Add2Ptr(ph, off), tail);
2333
2334 /* If there are no more bytes to transfer, we exit the loop. */
2335 if (!data_len) {
2336 if (!is_log_record_end(ph) ||
2337 lsn > le64_to_cpu(ph->record_hdr.last_end_lsn)) {
2338 err = -EINVAL;
2339 goto out;
2340 }
2341 break;
2342 }
2343
2344 if (ph->rhdr.lsn == ph->record_hdr.last_end_lsn ||
2345 lsn > le64_to_cpu(ph->rhdr.lsn)) {
2346 err = -EINVAL;
2347 goto out;
2348 }
2349
2350 vbo = next_page_off(log, vbo);
2351 off = log->data_off;
2352
2353 /*
2354 * Adjust our pointer the user's buffer to transfer
2355 * the next block to.
2356 */
2357 buffer = Add2Ptr(buffer, tail);
2358 }
2359
2360 out:
2361 kfree(ph);
2362 return err;
2363 }
2364
read_rst_area(struct ntfs_log * log,struct NTFS_RESTART ** rst_,u64 * lsn)2365 static int read_rst_area(struct ntfs_log *log, struct NTFS_RESTART **rst_,
2366 u64 *lsn)
2367 {
2368 int err;
2369 struct LFS_RECORD_HDR *rh = NULL;
2370 const struct CLIENT_REC *cr =
2371 Add2Ptr(log->ra, le16_to_cpu(log->ra->client_off));
2372 u64 lsnr, lsnc = le64_to_cpu(cr->restart_lsn);
2373 u32 len;
2374 struct NTFS_RESTART *rst;
2375
2376 *lsn = 0;
2377 *rst_ = NULL;
2378
2379 /* If the client doesn't have a restart area, go ahead and exit now. */
2380 if (!lsnc)
2381 return 0;
2382
2383 err = read_log_page(log, lsn_to_vbo(log, lsnc),
2384 (struct RECORD_PAGE_HDR **)&rh, NULL);
2385 if (err)
2386 return err;
2387
2388 rst = NULL;
2389 lsnr = le64_to_cpu(rh->this_lsn);
2390
2391 if (lsnc != lsnr) {
2392 /* If the lsn values don't match, then the disk is corrupt. */
2393 err = -EINVAL;
2394 goto out;
2395 }
2396
2397 *lsn = lsnr;
2398 len = le32_to_cpu(rh->client_data_len);
2399
2400 if (!len) {
2401 err = 0;
2402 goto out;
2403 }
2404
2405 if (len < sizeof(struct NTFS_RESTART)) {
2406 err = -EINVAL;
2407 goto out;
2408 }
2409
2410 rst = kmalloc(len, GFP_NOFS);
2411 if (!rst) {
2412 err = -ENOMEM;
2413 goto out;
2414 }
2415
2416 /* Copy the data into the 'rst' buffer. */
2417 err = read_log_rec_buf(log, rh, rst);
2418 if (err)
2419 goto out;
2420
2421 *rst_ = rst;
2422 rst = NULL;
2423
2424 out:
2425 kfree(rh);
2426 kfree(rst);
2427
2428 return err;
2429 }
2430
find_log_rec(struct ntfs_log * log,u64 lsn,struct lcb * lcb)2431 static int find_log_rec(struct ntfs_log *log, u64 lsn, struct lcb *lcb)
2432 {
2433 int err;
2434 struct LFS_RECORD_HDR *rh = lcb->lrh;
2435 u32 rec_len, len;
2436
2437 /* Read the record header for this lsn. */
2438 if (!rh) {
2439 err = read_log_page(log, lsn_to_vbo(log, lsn),
2440 (struct RECORD_PAGE_HDR **)&rh, NULL);
2441
2442 lcb->lrh = rh;
2443 if (err)
2444 return err;
2445 }
2446
2447 /*
2448 * If the lsn the log record doesn't match the desired
2449 * lsn then the disk is corrupt.
2450 */
2451 if (lsn != le64_to_cpu(rh->this_lsn))
2452 return -EINVAL;
2453
2454 len = le32_to_cpu(rh->client_data_len);
2455
2456 /*
2457 * Check that the length field isn't greater than the total
2458 * available space the log file.
2459 */
2460 rec_len = len + log->record_header_len;
2461 if (rec_len >= log->total_avail)
2462 return -EINVAL;
2463
2464 /*
2465 * If the entire log record is on this log page,
2466 * put a pointer to the log record the context block.
2467 */
2468 if (rh->flags & LOG_RECORD_MULTI_PAGE) {
2469 void *lr = kmalloc(len, GFP_NOFS);
2470
2471 if (!lr)
2472 return -ENOMEM;
2473
2474 lcb->log_rec = lr;
2475 lcb->alloc = true;
2476
2477 /* Copy the data into the buffer returned. */
2478 err = read_log_rec_buf(log, rh, lr);
2479 if (err)
2480 return err;
2481 } else {
2482 /* If beyond the end of the current page -> an error. */
2483 u32 page_off = lsn_to_page_off(log, lsn);
2484
2485 if (page_off + len + log->record_header_len > log->page_size)
2486 return -EINVAL;
2487
2488 lcb->log_rec = Add2Ptr(rh, sizeof(struct LFS_RECORD_HDR));
2489 lcb->alloc = false;
2490 }
2491
2492 return 0;
2493 }
2494
2495 /*
2496 * read_log_rec_lcb - Init the query operation.
2497 */
read_log_rec_lcb(struct ntfs_log * log,u64 lsn,u32 ctx_mode,struct lcb ** lcb_)2498 static int read_log_rec_lcb(struct ntfs_log *log, u64 lsn, u32 ctx_mode,
2499 struct lcb **lcb_)
2500 {
2501 int err;
2502 const struct CLIENT_REC *cr;
2503 struct lcb *lcb;
2504
2505 switch (ctx_mode) {
2506 case lcb_ctx_undo_next:
2507 case lcb_ctx_prev:
2508 case lcb_ctx_next:
2509 break;
2510 default:
2511 return -EINVAL;
2512 }
2513
2514 /* Check that the given lsn is the legal range for this client. */
2515 cr = Add2Ptr(log->ra, le16_to_cpu(log->ra->client_off));
2516
2517 if (!verify_client_lsn(log, cr, lsn))
2518 return -EINVAL;
2519
2520 lcb = kzalloc_obj(struct lcb, GFP_NOFS);
2521 if (!lcb)
2522 return -ENOMEM;
2523 lcb->client = log->client_id;
2524 lcb->ctx_mode = ctx_mode;
2525
2526 /* Find the log record indicated by the given lsn. */
2527 err = find_log_rec(log, lsn, lcb);
2528 if (err)
2529 goto out;
2530
2531 *lcb_ = lcb;
2532 return 0;
2533
2534 out:
2535 lcb_put(lcb);
2536 *lcb_ = NULL;
2537 return err;
2538 }
2539
2540 /*
2541 * find_client_next_lsn
2542 *
2543 * Attempt to find the next lsn to return to a client based on the context mode.
2544 */
find_client_next_lsn(struct ntfs_log * log,struct lcb * lcb,u64 * lsn)2545 static int find_client_next_lsn(struct ntfs_log *log, struct lcb *lcb, u64 *lsn)
2546 {
2547 int err;
2548 u64 next_lsn;
2549 struct LFS_RECORD_HDR *hdr;
2550
2551 hdr = lcb->lrh;
2552 *lsn = 0;
2553
2554 if (lcb_ctx_next != lcb->ctx_mode)
2555 goto check_undo_next;
2556
2557 /* Loop as long as another lsn can be found. */
2558 for (;;) {
2559 u64 current_lsn;
2560
2561 err = next_log_lsn(log, hdr, ¤t_lsn);
2562 if (err)
2563 goto out;
2564
2565 if (!current_lsn)
2566 break;
2567
2568 if (hdr != lcb->lrh)
2569 kfree(hdr);
2570
2571 hdr = NULL;
2572 err = read_log_page(log, lsn_to_vbo(log, current_lsn),
2573 (struct RECORD_PAGE_HDR **)&hdr, NULL);
2574 if (err)
2575 goto out;
2576
2577 if (memcmp(&hdr->client, &lcb->client,
2578 sizeof(struct CLIENT_ID))) {
2579 /*err = -EINVAL; */
2580 } else if (LfsClientRecord == hdr->record_type) {
2581 kfree(lcb->lrh);
2582 lcb->lrh = hdr;
2583 *lsn = current_lsn;
2584 return 0;
2585 }
2586 }
2587
2588 out:
2589 if (hdr != lcb->lrh)
2590 kfree(hdr);
2591 return err;
2592
2593 check_undo_next:
2594 if (lcb_ctx_undo_next == lcb->ctx_mode)
2595 next_lsn = le64_to_cpu(hdr->client_undo_next_lsn);
2596 else if (lcb_ctx_prev == lcb->ctx_mode)
2597 next_lsn = le64_to_cpu(hdr->client_prev_lsn);
2598 else
2599 return 0;
2600
2601 if (!next_lsn)
2602 return 0;
2603
2604 if (!verify_client_lsn(
2605 log, Add2Ptr(log->ra, le16_to_cpu(log->ra->client_off)),
2606 next_lsn))
2607 return 0;
2608
2609 hdr = NULL;
2610 err = read_log_page(log, lsn_to_vbo(log, next_lsn),
2611 (struct RECORD_PAGE_HDR **)&hdr, NULL);
2612 if (err)
2613 return err;
2614 kfree(lcb->lrh);
2615 lcb->lrh = hdr;
2616
2617 *lsn = next_lsn;
2618
2619 return 0;
2620 }
2621
read_next_log_rec(struct ntfs_log * log,struct lcb * lcb,u64 * lsn)2622 static int read_next_log_rec(struct ntfs_log *log, struct lcb *lcb, u64 *lsn)
2623 {
2624 int err;
2625
2626 err = find_client_next_lsn(log, lcb, lsn);
2627 if (err)
2628 return err;
2629
2630 if (!*lsn)
2631 return 0;
2632
2633 if (lcb->alloc)
2634 kfree(lcb->log_rec);
2635
2636 lcb->log_rec = NULL;
2637 lcb->alloc = false;
2638 kfree(lcb->lrh);
2639 lcb->lrh = NULL;
2640
2641 return find_log_rec(log, *lsn, lcb);
2642 }
2643
check_index_header(const struct INDEX_HDR * hdr,size_t bytes)2644 bool check_index_header(const struct INDEX_HDR *hdr, size_t bytes)
2645 {
2646 const bool has_subnode = hdr_has_subnode(hdr);
2647 __le16 mask;
2648 u32 min_de, de_off, used, total;
2649
2650 if (has_subnode) {
2651 min_de = sizeof(struct NTFS_DE) + sizeof(u64);
2652 mask = NTFS_IE_HAS_SUBNODES;
2653 } else {
2654 min_de = sizeof(struct NTFS_DE);
2655 mask = 0;
2656 }
2657
2658 de_off = le32_to_cpu(hdr->de_off);
2659 used = le32_to_cpu(hdr->used);
2660 total = le32_to_cpu(hdr->total);
2661
2662 if (de_off > bytes - min_de || used > bytes || total > bytes ||
2663 de_off + min_de > used || used > total) {
2664 return false;
2665 }
2666
2667 for (;;) {
2668 const struct NTFS_DE *e = Add2Ptr(hdr, de_off);
2669 u16 esize = le16_to_cpu(e->size);
2670 u16 key_size = le16_to_cpu(e->key_size);
2671 u16 data_size;
2672
2673 if (!IS_ALIGNED(esize, 8) || esize < min_de ||
2674 (e->flags & NTFS_IE_HAS_SUBNODES) != mask) {
2675 return false;
2676 }
2677
2678 if (size_add(de_off, esize) > used)
2679 return false;
2680
2681 if (de_is_last(e)) {
2682 if (key_size)
2683 return false;
2684 break;
2685 }
2686
2687 data_size = esize - min_de;
2688 if (key_size > data_size)
2689 return false;
2690
2691 de_off += esize;
2692 }
2693
2694 return true;
2695 }
2696
check_index_buffer(const struct INDEX_BUFFER * ib,u32 bytes)2697 static inline bool check_index_buffer(const struct INDEX_BUFFER *ib, u32 bytes)
2698 {
2699 u16 fo;
2700 const struct NTFS_RECORD_HEADER *r = &ib->rhdr;
2701
2702 if (r->sign != NTFS_INDX_SIGNATURE)
2703 return false;
2704
2705 fo = (SECTOR_SIZE - ((bytes >> SECTOR_SHIFT) + 1) * sizeof(short));
2706
2707 if (le16_to_cpu(r->fix_off) > fo)
2708 return false;
2709
2710 if ((le16_to_cpu(r->fix_num) - 1) * SECTOR_SIZE != bytes)
2711 return false;
2712
2713 return check_index_header(&ib->ihdr,
2714 bytes - offsetof(struct INDEX_BUFFER, ihdr));
2715 }
2716
check_index_root(const struct ATTRIB * attr,struct ntfs_sb_info * sbi)2717 static inline bool check_index_root(const struct ATTRIB *attr,
2718 struct ntfs_sb_info *sbi)
2719 {
2720 bool ret;
2721 const struct INDEX_ROOT *root = resident_data(attr);
2722 u8 index_bits = le32_to_cpu(root->index_block_size) >=
2723 sbi->cluster_size ?
2724 sbi->cluster_bits :
2725 SECTOR_SHIFT;
2726 u8 block_clst = root->index_block_clst;
2727
2728 if (le32_to_cpu(attr->res.data_size) < sizeof(struct INDEX_ROOT) ||
2729 (root->type != ATTR_NAME && root->type != ATTR_ZERO) ||
2730 (root->type == ATTR_NAME &&
2731 root->rule != NTFS_COLLATION_TYPE_FILENAME) ||
2732 (le32_to_cpu(root->index_block_size) !=
2733 (block_clst << index_bits)) ||
2734 (block_clst != 1 && block_clst != 2 && block_clst != 4 &&
2735 block_clst != 8 && block_clst != 0x10 && block_clst != 0x20 &&
2736 block_clst != 0x40 && block_clst != 0x80)) {
2737 return false;
2738 }
2739
2740 ret = check_index_header(&root->ihdr,
2741 le32_to_cpu(attr->res.data_size) -
2742 offsetof(struct INDEX_ROOT, ihdr));
2743 return ret;
2744 }
2745
check_attr(const struct MFT_REC * rec,const struct ATTRIB * attr,struct ntfs_sb_info * sbi)2746 static inline bool check_attr(const struct MFT_REC *rec,
2747 const struct ATTRIB *attr,
2748 struct ntfs_sb_info *sbi)
2749 {
2750 u32 asize = le32_to_cpu(attr->size);
2751 u32 rsize = 0;
2752 u64 dsize, svcn, evcn;
2753 u16 run_off;
2754
2755 /* Check the fixed part of the attribute record header. */
2756 if (asize >= sbi->record_size ||
2757 asize + PtrOffset(rec, attr) >= sbi->record_size ||
2758 (attr->name_len &&
2759 le16_to_cpu(attr->name_off) + attr->name_len * sizeof(short) >
2760 asize)) {
2761 return false;
2762 }
2763
2764 /* Check the attribute fields. */
2765 switch (attr->non_res) {
2766 case 0:
2767 rsize = le32_to_cpu(attr->res.data_size);
2768 if (rsize >= asize ||
2769 le16_to_cpu(attr->res.data_off) + rsize > asize) {
2770 return false;
2771 }
2772 break;
2773
2774 case 1:
2775 dsize = le64_to_cpu(attr->nres.data_size);
2776 svcn = le64_to_cpu(attr->nres.svcn);
2777 evcn = le64_to_cpu(attr->nres.evcn);
2778 run_off = le16_to_cpu(attr->nres.run_off);
2779
2780 if (svcn > evcn + 1 || run_off >= asize ||
2781 le64_to_cpu(attr->nres.valid_size) > dsize ||
2782 dsize > le64_to_cpu(attr->nres.alloc_size)) {
2783 return false;
2784 }
2785
2786 if (run_off > asize)
2787 return false;
2788
2789 if (run_unpack(NULL, sbi, 0, svcn, evcn, svcn,
2790 Add2Ptr(attr, run_off), asize - run_off) < 0) {
2791 return false;
2792 }
2793
2794 return true;
2795
2796 default:
2797 return false;
2798 }
2799
2800 switch (attr->type) {
2801 case ATTR_NAME:
2802 if (fname_full_size(Add2Ptr(
2803 attr, le16_to_cpu(attr->res.data_off))) > asize) {
2804 return false;
2805 }
2806 break;
2807
2808 case ATTR_ROOT:
2809 return check_index_root(attr, sbi);
2810
2811 case ATTR_STD:
2812 if (rsize < sizeof(struct ATTR_STD_INFO5) &&
2813 rsize != sizeof(struct ATTR_STD_INFO)) {
2814 return false;
2815 }
2816 break;
2817
2818 case ATTR_LIST:
2819 case ATTR_ID:
2820 case ATTR_SECURE:
2821 case ATTR_LABEL:
2822 case ATTR_VOL_INFO:
2823 case ATTR_DATA:
2824 case ATTR_ALLOC:
2825 case ATTR_BITMAP:
2826 case ATTR_REPARSE:
2827 case ATTR_EA_INFO:
2828 case ATTR_EA:
2829 case ATTR_PROPERTYSET:
2830 case ATTR_LOGGED_UTILITY_STREAM:
2831 break;
2832
2833 default:
2834 return false;
2835 }
2836
2837 return true;
2838 }
2839
check_file_record(const struct MFT_REC * rec,const struct MFT_REC * rec2,struct ntfs_sb_info * sbi)2840 static inline bool check_file_record(const struct MFT_REC *rec,
2841 const struct MFT_REC *rec2,
2842 struct ntfs_sb_info *sbi)
2843 {
2844 const struct ATTRIB *attr;
2845 u16 fo = le16_to_cpu(rec->rhdr.fix_off);
2846 u16 fn = le16_to_cpu(rec->rhdr.fix_num);
2847 u16 ao = le16_to_cpu(rec->attr_off);
2848 u32 rs = sbi->record_size;
2849 u32 used = le32_to_cpu(rec->used);
2850
2851 /* Check the file record header for consistency. */
2852 if (rec->rhdr.sign != NTFS_FILE_SIGNATURE ||
2853 fo > (SECTOR_SIZE - ((rs >> SECTOR_SHIFT) + 1) * sizeof(short)) ||
2854 (fn - 1) * SECTOR_SIZE != rs || ao < MFTRECORD_FIXUP_OFFSET_1 ||
2855 ao > sbi->record_size - SIZEOF_RESIDENT || !is_rec_inuse(rec) ||
2856 le32_to_cpu(rec->total) != rs || used > rs || used < ao) {
2857 return false;
2858 }
2859
2860 /* Loop to check all of the attributes. */
2861 for (attr = Add2Ptr(rec, ao); attr->type != ATTR_END;
2862 attr = Add2Ptr(attr, le32_to_cpu(attr->size))) {
2863 if (check_attr(rec, attr, sbi))
2864 continue;
2865 return false;
2866 }
2867
2868 /*
2869 * The do_action() handlers compute memmove lengths as
2870 * "rec->used - <offset of validated attr>", which underflows when
2871 * rec->used is smaller than the attribute walk reached. At this
2872 * point attr is the ATTR_END marker; rec->used must cover it.
2873 */
2874 if (used < PtrOffset(rec, attr) + sizeof(attr->type))
2875 return false;
2876
2877 return true;
2878 }
2879
check_lsn(const struct NTFS_RECORD_HEADER * hdr,const u64 * rlsn)2880 static inline int check_lsn(const struct NTFS_RECORD_HEADER *hdr,
2881 const u64 *rlsn)
2882 {
2883 u64 lsn;
2884
2885 if (!rlsn)
2886 return true;
2887
2888 lsn = le64_to_cpu(hdr->lsn);
2889
2890 if (hdr->sign == NTFS_HOLE_SIGNATURE)
2891 return false;
2892
2893 if (*rlsn > lsn)
2894 return true;
2895
2896 return false;
2897 }
2898
check_if_attr(const struct MFT_REC * rec,const struct LOG_REC_HDR * lrh)2899 static inline bool check_if_attr(const struct MFT_REC *rec,
2900 const struct LOG_REC_HDR *lrh)
2901 {
2902 u16 ro = le16_to_cpu(lrh->record_off);
2903 u16 o = le16_to_cpu(rec->attr_off);
2904 const struct ATTRIB *attr = Add2Ptr(rec, o);
2905
2906 while (o < ro) {
2907 u32 asize;
2908
2909 if (attr->type == ATTR_END)
2910 break;
2911
2912 asize = le32_to_cpu(attr->size);
2913 if (!asize)
2914 break;
2915
2916 o += asize;
2917 attr = Add2Ptr(attr, asize);
2918 }
2919
2920 return o == ro;
2921 }
2922
check_if_index_root(const struct MFT_REC * rec,const struct LOG_REC_HDR * lrh)2923 static inline bool check_if_index_root(const struct MFT_REC *rec,
2924 const struct LOG_REC_HDR *lrh)
2925 {
2926 u16 ro = le16_to_cpu(lrh->record_off);
2927 u16 o = le16_to_cpu(rec->attr_off);
2928 const struct ATTRIB *attr = Add2Ptr(rec, o);
2929
2930 while (o < ro) {
2931 u32 asize;
2932
2933 if (attr->type == ATTR_END)
2934 break;
2935
2936 asize = le32_to_cpu(attr->size);
2937 if (!asize)
2938 break;
2939
2940 o += asize;
2941 attr = Add2Ptr(attr, asize);
2942 }
2943
2944 return o == ro && attr->type == ATTR_ROOT;
2945 }
2946
check_if_root_index(const struct ATTRIB * attr,const struct INDEX_HDR * hdr,const struct LOG_REC_HDR * lrh)2947 static inline bool check_if_root_index(const struct ATTRIB *attr,
2948 const struct INDEX_HDR *hdr,
2949 const struct LOG_REC_HDR *lrh)
2950 {
2951 u16 ao = le16_to_cpu(lrh->attr_off);
2952 u32 de_off = le32_to_cpu(hdr->de_off);
2953 u32 o = PtrOffset(attr, hdr) + de_off;
2954 const struct NTFS_DE *e = Add2Ptr(hdr, de_off);
2955 u32 asize = le32_to_cpu(attr->size);
2956
2957 while (o < ao) {
2958 u16 esize;
2959
2960 if (o >= asize)
2961 break;
2962
2963 esize = le16_to_cpu(e->size);
2964 if (!esize)
2965 break;
2966
2967 o += esize;
2968 e = Add2Ptr(e, esize);
2969 }
2970
2971 return o == ao;
2972 }
2973
check_if_alloc_index(const struct INDEX_HDR * hdr,u32 attr_off)2974 static inline bool check_if_alloc_index(const struct INDEX_HDR *hdr,
2975 u32 attr_off)
2976 {
2977 u32 de_off = le32_to_cpu(hdr->de_off);
2978 u32 o = offsetof(struct INDEX_BUFFER, ihdr) + de_off;
2979 const struct NTFS_DE *e = Add2Ptr(hdr, de_off);
2980 u32 used = le32_to_cpu(hdr->used);
2981
2982 while (o < attr_off) {
2983 u16 esize;
2984
2985 if (de_off >= used)
2986 break;
2987
2988 esize = le16_to_cpu(e->size);
2989 if (!esize)
2990 break;
2991
2992 o += esize;
2993 de_off += esize;
2994 e = Add2Ptr(e, esize);
2995 }
2996
2997 return o == attr_off;
2998 }
2999
change_attr_size(struct MFT_REC * rec,struct ATTRIB * attr,u32 nsize)3000 static inline void change_attr_size(struct MFT_REC *rec, struct ATTRIB *attr,
3001 u32 nsize)
3002 {
3003 u32 asize = le32_to_cpu(attr->size);
3004 int dsize = nsize - asize;
3005 u8 *next = Add2Ptr(attr, asize);
3006 u32 used = le32_to_cpu(rec->used);
3007
3008 memmove(Add2Ptr(attr, nsize), next, used - PtrOffset(rec, next));
3009
3010 rec->used = cpu_to_le32(used + dsize);
3011 attr->size = cpu_to_le32(nsize);
3012 }
3013
3014 struct OpenAttr {
3015 struct ATTRIB *attr;
3016 struct runs_tree *run1;
3017 struct runs_tree run0;
3018 struct ntfs_inode *ni;
3019 // CLST rno;
3020 };
3021
3022 /*
3023 * cmp_type_and_name
3024 *
3025 * Return: 0 if 'attr' has the same type and name.
3026 */
cmp_type_and_name(const struct ATTRIB * a1,const struct ATTRIB * a2)3027 static inline int cmp_type_and_name(const struct ATTRIB *a1,
3028 const struct ATTRIB *a2)
3029 {
3030 return a1->type != a2->type || a1->name_len != a2->name_len ||
3031 (a1->name_len && memcmp(attr_name(a1), attr_name(a2),
3032 a1->name_len * sizeof(short)));
3033 }
3034
find_loaded_attr(struct ntfs_log * log,const struct ATTRIB * attr,CLST rno)3035 static struct OpenAttr *find_loaded_attr(struct ntfs_log *log,
3036 const struct ATTRIB *attr, CLST rno)
3037 {
3038 struct OPEN_ATTR_ENRTY *oe = NULL;
3039
3040 while ((oe = enum_rstbl(log->open_attr_tbl, oe))) {
3041 struct OpenAttr *op_attr;
3042
3043 if (ino_get(&oe->ref) != rno)
3044 continue;
3045
3046 op_attr = (struct OpenAttr *)oe->ptr;
3047 if (!cmp_type_and_name(op_attr->attr, attr))
3048 return op_attr;
3049 }
3050 return NULL;
3051 }
3052
attr_create_nonres_log(struct ntfs_sb_info * sbi,enum ATTR_TYPE type,u64 size,const u16 * name,size_t name_len,__le16 flags)3053 static struct ATTRIB *attr_create_nonres_log(struct ntfs_sb_info *sbi,
3054 enum ATTR_TYPE type, u64 size,
3055 const u16 *name, size_t name_len,
3056 __le16 flags)
3057 {
3058 struct ATTRIB *attr;
3059 u32 name_size = ALIGN(name_len * sizeof(short), 8);
3060 bool is_ext = flags & (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED);
3061 u32 asize = name_size +
3062 (is_ext ? SIZEOF_NONRESIDENT_EX : SIZEOF_NONRESIDENT);
3063
3064 attr = kzalloc(asize, GFP_NOFS);
3065 if (!attr)
3066 return NULL;
3067
3068 attr->type = type;
3069 attr->size = cpu_to_le32(asize);
3070 attr->flags = flags;
3071 attr->non_res = 1;
3072 attr->name_len = name_len;
3073
3074 attr->nres.evcn = cpu_to_le64((u64)bytes_to_cluster(sbi, size) - 1);
3075 attr->nres.alloc_size = cpu_to_le64(ntfs_up_cluster(sbi, size));
3076 attr->nres.data_size = cpu_to_le64(size);
3077 attr->nres.valid_size = attr->nres.data_size;
3078 if (is_ext) {
3079 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
3080 if (is_attr_compressed(attr))
3081 attr->nres.c_unit = NTFS_LZNT_CUNIT;
3082
3083 attr->nres.run_off =
3084 cpu_to_le16(SIZEOF_NONRESIDENT_EX + name_size);
3085 memcpy(Add2Ptr(attr, SIZEOF_NONRESIDENT_EX), name,
3086 name_len * sizeof(short));
3087 } else {
3088 attr->name_off = SIZEOF_NONRESIDENT_LE;
3089 attr->nres.run_off =
3090 cpu_to_le16(SIZEOF_NONRESIDENT + name_size);
3091 memcpy(Add2Ptr(attr, SIZEOF_NONRESIDENT), name,
3092 name_len * sizeof(short));
3093 }
3094
3095 return attr;
3096 }
3097
3098 /*
3099 * update_oa_attr - Synchronize OpenAttr's attribute pointer with modified attribute
3100 * @oa2: OpenAttr structure in memory that needs to be updated
3101 * @attr: Modified attribute from MFT record to duplicate
3102 *
3103 * Returns true on success, false on allocation failure.
3104 */
update_oa_attr(struct OpenAttr * oa2,struct ATTRIB * attr)3105 static bool update_oa_attr(struct OpenAttr *oa2, struct ATTRIB *attr)
3106 {
3107 void *p2;
3108
3109 p2 = kmemdup(attr, le32_to_cpu(attr->size), GFP_NOFS);
3110 if (p2) {
3111 kfree(oa2->attr);
3112 oa2->attr = p2;
3113 return true;
3114 }
3115 return false;
3116 }
3117
3118 /*
3119 * do_action - Common routine for the Redo and Undo Passes.
3120 * @rlsn: If it is NULL then undo.
3121 */
do_action(struct ntfs_log * log,struct OPEN_ATTR_ENRTY * oe,const struct LOG_REC_HDR * lrh,u32 op,void * data,u32 dlen,u32 rec_len,const u64 * rlsn)3122 static int do_action(struct ntfs_log *log, struct OPEN_ATTR_ENRTY *oe,
3123 const struct LOG_REC_HDR *lrh, u32 op, void *data,
3124 u32 dlen, u32 rec_len, const u64 *rlsn)
3125 {
3126 int err = 0;
3127 struct ntfs_sb_info *sbi = log->ni->mi.sbi;
3128 struct inode *inode = NULL, *inode_parent;
3129 struct mft_inode *mi = NULL, *mi2_child = NULL;
3130 CLST rno = 0, rno_base = 0;
3131 struct INDEX_BUFFER *ib = NULL;
3132 struct MFT_REC *rec = NULL;
3133 struct ATTRIB *attr = NULL, *attr2;
3134 struct INDEX_HDR *hdr;
3135 struct INDEX_ROOT *root;
3136 struct NTFS_DE *e, *e1, *e2;
3137 struct NEW_ATTRIBUTE_SIZES *new_sz;
3138 struct ATTR_FILE_NAME *fname;
3139 struct OpenAttr *oa, *oa2;
3140 u32 nsize, t32, asize, used, esize, off, bits;
3141 u16 id, id2;
3142 u32 record_size = sbi->record_size;
3143 u64 t64;
3144 u16 roff = le16_to_cpu(lrh->record_off);
3145 u16 aoff = le16_to_cpu(lrh->attr_off);
3146 u64 lco = 0;
3147 u64 cbo = (u64)le16_to_cpu(lrh->cluster_off) << SECTOR_SHIFT;
3148 u64 tvo = le64_to_cpu(lrh->target_vcn) << sbi->cluster_bits;
3149 u64 vbo = cbo + tvo;
3150 void *buffer_le = NULL;
3151 u32 bytes = 0;
3152 bool a_dirty = false;
3153 u16 data_off;
3154
3155 oa = oe->ptr;
3156
3157 /* Big switch to prepare. */
3158 switch (op) {
3159 /* ============================================================
3160 * Process MFT records, as described by the current log record.
3161 * ============================================================
3162 */
3163 case InitializeFileRecordSegment:
3164 case DeallocateFileRecordSegment:
3165 case WriteEndOfFileRecordSegment:
3166 case CreateAttribute:
3167 case DeleteAttribute:
3168 case UpdateResidentValue:
3169 case UpdateMappingPairs:
3170 case SetNewAttributeSizes:
3171 case AddIndexEntryRoot:
3172 case DeleteIndexEntryRoot:
3173 case SetIndexEntryVcnRoot:
3174 case UpdateFileNameRoot:
3175 case UpdateRecordDataRoot:
3176 case ZeroEndOfFileRecord:
3177 rno = vbo >> sbi->record_bits;
3178 inode = ilookup(sbi->sb, rno);
3179 if (inode) {
3180 mi = &ntfs_i(inode)->mi;
3181 } else {
3182 /* Read from disk. */
3183 err = mi_get(sbi, rno, &mi);
3184 if (err && op == InitializeFileRecordSegment) {
3185 mi = kzalloc_obj(struct mft_inode, GFP_NOFS);
3186 if (!mi)
3187 return -ENOMEM;
3188 err = mi_format_new(mi, sbi, rno, 0, false);
3189 }
3190 if (err)
3191 return err;
3192 }
3193 rec = mi->mrec;
3194
3195 if (op == DeallocateFileRecordSegment)
3196 goto skip_load_parent;
3197
3198 if (rec->rhdr.sign == NTFS_BAAD_SIGNATURE)
3199 goto dirty_vol;
3200 if (!check_lsn(&rec->rhdr, rlsn))
3201 goto out;
3202 if (!check_file_record(rec, NULL, sbi))
3203 goto dirty_vol;
3204 attr = Add2Ptr(rec, roff);
3205
3206 if (is_rec_base(rec) || InitializeFileRecordSegment == op) {
3207 rno_base = rno;
3208 goto skip_load_parent;
3209 }
3210
3211 rno_base = ino_get(&rec->parent_ref);
3212 inode_parent = ntfs_iget5(sbi->sb, &rec->parent_ref, NULL);
3213 if (IS_ERR(inode_parent))
3214 goto skip_load_parent;
3215
3216 if (is_bad_inode(inode_parent)) {
3217 iput(inode_parent);
3218 goto skip_load_parent;
3219 }
3220
3221 if (ni_load_mi_ex(ntfs_i(inode_parent), rno, &mi2_child)) {
3222 iput(inode_parent);
3223 } else {
3224 if (mi2_child->mrec != mi->mrec)
3225 memcpy(mi2_child->mrec, mi->mrec,
3226 sbi->record_size);
3227
3228 if (inode)
3229 iput(inode);
3230 else
3231 mi_put(mi);
3232
3233 inode = inode_parent;
3234 mi = mi2_child;
3235 rec = mi2_child->mrec;
3236 attr = Add2Ptr(rec, roff);
3237 }
3238
3239 skip_load_parent:
3240 inode_parent = NULL;
3241 break;
3242
3243 /*
3244 * Process attributes, as described by the current log record.
3245 */
3246 case UpdateNonresidentValue:
3247 case AddIndexEntryAllocation:
3248 case DeleteIndexEntryAllocation:
3249 case WriteEndOfIndexBuffer:
3250 case SetIndexEntryVcnAllocation:
3251 case UpdateFileNameAllocation:
3252 case SetBitsInNonresidentBitMap:
3253 case ClearBitsInNonresidentBitMap:
3254 case UpdateRecordDataAllocation:
3255 attr = oa->attr;
3256 bytes = UpdateNonresidentValue == op ? dlen : 0;
3257 lco = (u64)le16_to_cpu(lrh->lcns_follow) << sbi->cluster_bits;
3258
3259 if (attr->type == ATTR_ALLOC) {
3260 t32 = le32_to_cpu(oe->bytes_per_index);
3261 if (bytes < t32)
3262 bytes = t32;
3263 }
3264
3265 if (!bytes)
3266 bytes = lco - cbo;
3267
3268 bytes += roff;
3269 if (attr->type == ATTR_ALLOC)
3270 bytes = (bytes + 511) & ~511; // align
3271
3272 buffer_le = kmalloc(bytes, GFP_NOFS);
3273 if (!buffer_le)
3274 return -ENOMEM;
3275
3276 err = ntfs_read_run_nb(sbi, oa->run1, vbo, buffer_le, bytes,
3277 NULL);
3278 if (err)
3279 goto out;
3280
3281 if (attr->type == ATTR_ALLOC && *(int *)buffer_le)
3282 ntfs_fix_post_read(buffer_le, bytes, false);
3283 break;
3284
3285 default:
3286 WARN_ON(1);
3287 }
3288
3289 /* Big switch to do operation. */
3290 switch (op) {
3291 case InitializeFileRecordSegment:
3292 if (roff + dlen > record_size)
3293 goto dirty_vol;
3294
3295 memcpy(Add2Ptr(rec, roff), data, dlen);
3296 mi->dirty = true;
3297 break;
3298
3299 case DeallocateFileRecordSegment:
3300 clear_rec_inuse(rec);
3301 le16_add_cpu(&rec->seq, 1);
3302 mi->dirty = true;
3303 break;
3304
3305 case WriteEndOfFileRecordSegment:
3306 attr2 = (struct ATTRIB *)data;
3307 if (!check_if_attr(rec, lrh) || roff + dlen > record_size)
3308 goto dirty_vol;
3309
3310 memmove(attr, attr2, dlen);
3311 rec->used = cpu_to_le32(ALIGN(roff + dlen, 8));
3312
3313 mi->dirty = true;
3314 break;
3315
3316 case CreateAttribute:
3317 attr2 = (struct ATTRIB *)data;
3318 asize = le32_to_cpu(attr2->size);
3319 used = le32_to_cpu(rec->used);
3320
3321 if (!check_if_attr(rec, lrh) || dlen < SIZEOF_RESIDENT ||
3322 !IS_ALIGNED(asize, 8) ||
3323 Add2Ptr(attr2, asize) > Add2Ptr(lrh, rec_len) ||
3324 dlen > record_size - used) {
3325 goto dirty_vol;
3326 }
3327
3328 memmove(Add2Ptr(attr, asize), attr, used - roff);
3329 memcpy(attr, attr2, asize);
3330
3331 rec->used = cpu_to_le32(used + asize);
3332 id = le16_to_cpu(rec->next_attr_id);
3333 id2 = le16_to_cpu(attr2->id);
3334 if (id <= id2)
3335 rec->next_attr_id = cpu_to_le16(id2 + 1);
3336 if (is_attr_indexed(attr))
3337 le16_add_cpu(&rec->hard_links, 1);
3338
3339 oa2 = find_loaded_attr(log, attr, rno_base);
3340 if (oa2)
3341 update_oa_attr(oa2, attr);
3342
3343 mi->dirty = true;
3344 break;
3345
3346 case DeleteAttribute:
3347 asize = le32_to_cpu(attr->size);
3348 used = le32_to_cpu(rec->used);
3349
3350 if (!check_if_attr(rec, lrh))
3351 goto dirty_vol;
3352
3353 rec->used = cpu_to_le32(used - asize);
3354 if (is_attr_indexed(attr))
3355 le16_add_cpu(&rec->hard_links, -1);
3356
3357 memmove(attr, Add2Ptr(attr, asize), used - asize - roff);
3358
3359 mi->dirty = true;
3360 break;
3361
3362 case UpdateResidentValue:
3363 nsize = aoff + dlen;
3364
3365 if (!check_if_attr(rec, lrh))
3366 goto dirty_vol;
3367
3368 asize = le32_to_cpu(attr->size);
3369 used = le32_to_cpu(rec->used);
3370
3371 if (lrh->redo_len == lrh->undo_len) {
3372 if (nsize > asize)
3373 goto dirty_vol;
3374 goto move_data;
3375 }
3376
3377 if (nsize > asize && nsize - asize > record_size - used)
3378 goto dirty_vol;
3379
3380 nsize = ALIGN(nsize, 8);
3381 data_off = le16_to_cpu(attr->res.data_off);
3382
3383 /*
3384 * aoff comes from the on-disk lrh->attr_off. Forbid
3385 * writes that begin below the resident attribute's
3386 * data_off (which would overwrite the resident header),
3387 * and forbid aoff + dlen < data_off, which would make
3388 * the data_size assignment below underflow to ~4 GiB.
3389 */
3390 if (aoff < data_off || aoff + dlen < data_off ||
3391 aoff + dlen > asize)
3392 goto dirty_vol;
3393
3394 if (nsize < asize) {
3395 memmove(Add2Ptr(attr, aoff), data, dlen);
3396 data = NULL; // To skip below memmove().
3397 }
3398
3399 memmove(Add2Ptr(attr, nsize), Add2Ptr(attr, asize),
3400 used - le16_to_cpu(lrh->record_off) - asize);
3401
3402 rec->used = cpu_to_le32(used + nsize - asize);
3403 attr->size = cpu_to_le32(nsize);
3404 attr->res.data_size = cpu_to_le32(aoff + dlen - data_off);
3405
3406 move_data:
3407 if (data)
3408 memmove(Add2Ptr(attr, aoff), data, dlen);
3409
3410 oa2 = find_loaded_attr(log, attr, rno_base);
3411 if (oa2 && update_oa_attr(oa2, attr))
3412 oa2->run1 = &oa2->run0;
3413
3414 mi->dirty = true;
3415 break;
3416
3417 case UpdateMappingPairs:
3418 nsize = aoff + dlen;
3419 asize = le32_to_cpu(attr->size);
3420 used = le32_to_cpu(rec->used);
3421
3422 if (!check_if_attr(rec, lrh) || !attr->non_res ||
3423 aoff < le16_to_cpu(attr->nres.run_off) || aoff > asize ||
3424 (nsize > asize && nsize - asize > record_size - used)) {
3425 goto dirty_vol;
3426 }
3427
3428 nsize = ALIGN(nsize, 8);
3429
3430 memmove(Add2Ptr(attr, nsize), Add2Ptr(attr, asize),
3431 used - le16_to_cpu(lrh->record_off) - asize);
3432 rec->used = cpu_to_le32(used + nsize - asize);
3433 attr->size = cpu_to_le32(nsize);
3434 memmove(Add2Ptr(attr, aoff), data, dlen);
3435
3436 if (run_get_highest_vcn(le64_to_cpu(attr->nres.svcn),
3437 attr_run(attr),
3438 le32_to_cpu(attr->size) -
3439 le16_to_cpu(attr->nres.run_off),
3440 &t64)) {
3441 goto dirty_vol;
3442 }
3443
3444 attr->nres.evcn = cpu_to_le64(t64);
3445 oa2 = find_loaded_attr(log, attr, rno_base);
3446 if (oa2 && oa2->attr->non_res)
3447 oa2->attr->nres.evcn = attr->nres.evcn;
3448
3449 mi->dirty = true;
3450 break;
3451
3452 case SetNewAttributeSizes:
3453 new_sz = data;
3454 if (!check_if_attr(rec, lrh) || !attr->non_res)
3455 goto dirty_vol;
3456
3457 attr->nres.alloc_size = new_sz->alloc_size;
3458 attr->nres.data_size = new_sz->data_size;
3459 attr->nres.valid_size = new_sz->valid_size;
3460
3461 if (dlen >= sizeof(struct NEW_ATTRIBUTE_SIZES))
3462 attr->nres.total_size = new_sz->total_size;
3463
3464 oa2 = find_loaded_attr(log, attr, rno_base);
3465 if (oa2)
3466 update_oa_attr(oa2, attr);
3467
3468 mi->dirty = true;
3469 break;
3470
3471 case AddIndexEntryRoot:
3472 e = (struct NTFS_DE *)data;
3473 esize = le16_to_cpu(e->size);
3474 root = resident_data(attr);
3475 hdr = &root->ihdr;
3476 used = le32_to_cpu(hdr->used);
3477
3478 if (!check_if_index_root(rec, lrh) ||
3479 !check_if_root_index(attr, hdr, lrh) ||
3480 Add2Ptr(data, esize) > Add2Ptr(lrh, rec_len) ||
3481 esize > le32_to_cpu(rec->total) - le32_to_cpu(rec->used)) {
3482 goto dirty_vol;
3483 }
3484
3485 e1 = Add2Ptr(attr, le16_to_cpu(lrh->attr_off));
3486
3487 change_attr_size(rec, attr, le32_to_cpu(attr->size) + esize);
3488
3489 memmove(Add2Ptr(e1, esize), e1,
3490 PtrOffset(e1, Add2Ptr(hdr, used)));
3491 memmove(e1, e, esize);
3492
3493 le32_add_cpu(&attr->res.data_size, esize);
3494 hdr->used = cpu_to_le32(used + esize);
3495 le32_add_cpu(&hdr->total, esize);
3496
3497 mi->dirty = true;
3498 break;
3499
3500 case DeleteIndexEntryRoot:
3501 root = resident_data(attr);
3502 hdr = &root->ihdr;
3503 used = le32_to_cpu(hdr->used);
3504
3505 if (!check_if_index_root(rec, lrh) ||
3506 !check_if_root_index(attr, hdr, lrh)) {
3507 goto dirty_vol;
3508 }
3509
3510 e1 = Add2Ptr(attr, le16_to_cpu(lrh->attr_off));
3511 esize = le16_to_cpu(e1->size);
3512 if (PtrOffset(e1, Add2Ptr(hdr, used)) < esize)
3513 goto dirty_vol;
3514
3515 e2 = Add2Ptr(e1, esize);
3516
3517 memmove(e1, e2, PtrOffset(e2, Add2Ptr(hdr, used)));
3518
3519 le32_sub_cpu(&attr->res.data_size, esize);
3520 hdr->used = cpu_to_le32(used - esize);
3521 le32_sub_cpu(&hdr->total, esize);
3522
3523 change_attr_size(rec, attr, le32_to_cpu(attr->size) - esize);
3524
3525 mi->dirty = true;
3526 break;
3527
3528 case SetIndexEntryVcnRoot:
3529 root = resident_data(attr);
3530 hdr = &root->ihdr;
3531
3532 if (!check_if_index_root(rec, lrh) ||
3533 !check_if_root_index(attr, hdr, lrh)) {
3534 goto dirty_vol;
3535 }
3536
3537 e = Add2Ptr(attr, le16_to_cpu(lrh->attr_off));
3538
3539 de_set_vbn_le(e, *(__le64 *)data);
3540 mi->dirty = true;
3541 break;
3542
3543 case UpdateFileNameRoot:
3544 root = resident_data(attr);
3545 hdr = &root->ihdr;
3546
3547 if (!check_if_index_root(rec, lrh) ||
3548 !check_if_root_index(attr, hdr, lrh)) {
3549 goto dirty_vol;
3550 }
3551
3552 e = Add2Ptr(attr, le16_to_cpu(lrh->attr_off));
3553 fname = (struct ATTR_FILE_NAME *)(e + 1);
3554 memmove(&fname->dup, data, sizeof(fname->dup)); //
3555 mi->dirty = true;
3556 break;
3557
3558 case UpdateRecordDataRoot:
3559 root = resident_data(attr);
3560 hdr = &root->ihdr;
3561
3562 if (!check_if_index_root(rec, lrh) ||
3563 !check_if_root_index(attr, hdr, lrh)) {
3564 goto dirty_vol;
3565 }
3566
3567 e = Add2Ptr(attr, le16_to_cpu(lrh->attr_off));
3568
3569 /*
3570 * e->view.data_off and dlen come from the on-disk
3571 * INDEX_ROOT entry / LRH. The neighbouring read sites
3572 * (e.g. fs/ntfs3/index.c) check that
3573 * view.data_off + view.data_size <= e->size; mirror that
3574 * bound here so the memmove cannot reach past the entry.
3575 */
3576 if (le16_to_cpu(e->view.data_off) > le16_to_cpu(e->size) ||
3577 le16_to_cpu(e->view.data_off) + dlen > le16_to_cpu(e->size))
3578 goto dirty_vol;
3579
3580 memmove(Add2Ptr(e, le16_to_cpu(e->view.data_off)), data, dlen);
3581
3582 mi->dirty = true;
3583 break;
3584
3585 case ZeroEndOfFileRecord:
3586 if (roff + dlen > record_size)
3587 goto dirty_vol;
3588
3589 memset(attr, 0, dlen);
3590 mi->dirty = true;
3591 break;
3592
3593 case UpdateNonresidentValue:
3594 if (lco < cbo + roff + dlen)
3595 goto dirty_vol;
3596
3597 memcpy(Add2Ptr(buffer_le, roff), data, dlen);
3598
3599 a_dirty = true;
3600 if (attr->type == ATTR_ALLOC)
3601 ntfs_fix_pre_write(buffer_le, bytes);
3602 break;
3603
3604 case AddIndexEntryAllocation:
3605 ib = Add2Ptr(buffer_le, roff);
3606 hdr = &ib->ihdr;
3607 e = data;
3608 esize = le16_to_cpu(e->size);
3609 e1 = Add2Ptr(ib, aoff);
3610
3611 if (is_baad(&ib->rhdr))
3612 goto dirty_vol;
3613 if (!check_lsn(&ib->rhdr, rlsn))
3614 goto out;
3615
3616 used = le32_to_cpu(hdr->used);
3617
3618 if (!check_index_buffer(ib, bytes) ||
3619 !check_if_alloc_index(hdr, aoff) ||
3620 Add2Ptr(e, esize) > Add2Ptr(lrh, rec_len) ||
3621 used + esize > le32_to_cpu(hdr->total)) {
3622 goto dirty_vol;
3623 }
3624
3625 memmove(Add2Ptr(e1, esize), e1,
3626 PtrOffset(e1, Add2Ptr(hdr, used)));
3627 memcpy(e1, e, esize);
3628
3629 hdr->used = cpu_to_le32(used + esize);
3630
3631 a_dirty = true;
3632
3633 ntfs_fix_pre_write(&ib->rhdr, bytes);
3634 break;
3635
3636 case DeleteIndexEntryAllocation:
3637 ib = Add2Ptr(buffer_le, roff);
3638 hdr = &ib->ihdr;
3639 e = Add2Ptr(ib, aoff);
3640 esize = le16_to_cpu(e->size);
3641
3642 if (is_baad(&ib->rhdr))
3643 goto dirty_vol;
3644 if (!check_lsn(&ib->rhdr, rlsn))
3645 goto out;
3646
3647 if (!check_index_buffer(ib, bytes) ||
3648 !check_if_alloc_index(hdr, aoff)) {
3649 goto dirty_vol;
3650 }
3651
3652 e1 = Add2Ptr(e, esize);
3653 used = le32_to_cpu(hdr->used);
3654
3655 /*
3656 * Reject crafted entries whose e->size makes e + esize
3657 * point past the INDEX_HDR's used boundary. Without this,
3658 * PtrOffset(e1, hdr + used) underflows to a quasi-infinite
3659 * size_t when fed to the memmove() below.
3660 *
3661 * Also reject esize == 0: memmove(e, e, ...) is a no-op and
3662 * leaves hdr->used unchanged, masking the crafted entry.
3663 */
3664 if (!esize || Add2Ptr(e, esize) > Add2Ptr(hdr, used) ||
3665 PtrOffset(e1, Add2Ptr(hdr, used)) < esize)
3666 goto dirty_vol;
3667
3668 nsize = esize;
3669
3670 memmove(e, e1, PtrOffset(e1, Add2Ptr(hdr, used)));
3671
3672 hdr->used = cpu_to_le32(used - nsize);
3673
3674 a_dirty = true;
3675
3676 ntfs_fix_pre_write(&ib->rhdr, bytes);
3677 break;
3678
3679 case WriteEndOfIndexBuffer:
3680 ib = Add2Ptr(buffer_le, roff);
3681 hdr = &ib->ihdr;
3682 e = Add2Ptr(ib, aoff);
3683
3684 if (is_baad(&ib->rhdr))
3685 goto dirty_vol;
3686 if (!check_lsn(&ib->rhdr, rlsn))
3687 goto out;
3688 if (!check_index_buffer(ib, bytes) ||
3689 !check_if_alloc_index(hdr, aoff) ||
3690 aoff + dlen > offsetof(struct INDEX_BUFFER, ihdr) +
3691 le32_to_cpu(hdr->total)) {
3692 goto dirty_vol;
3693 }
3694
3695 hdr->used = cpu_to_le32(dlen + PtrOffset(hdr, e));
3696 memmove(e, data, dlen);
3697
3698 a_dirty = true;
3699 ntfs_fix_pre_write(&ib->rhdr, bytes);
3700 break;
3701
3702 case SetIndexEntryVcnAllocation:
3703 ib = Add2Ptr(buffer_le, roff);
3704 hdr = &ib->ihdr;
3705 e = Add2Ptr(ib, aoff);
3706
3707 if (is_baad(&ib->rhdr))
3708 goto dirty_vol;
3709
3710 if (!check_lsn(&ib->rhdr, rlsn))
3711 goto out;
3712 if (!check_index_buffer(ib, bytes) ||
3713 !check_if_alloc_index(hdr, aoff)) {
3714 goto dirty_vol;
3715 }
3716
3717 de_set_vbn_le(e, *(__le64 *)data);
3718
3719 a_dirty = true;
3720 ntfs_fix_pre_write(&ib->rhdr, bytes);
3721 break;
3722
3723 case UpdateFileNameAllocation:
3724 ib = Add2Ptr(buffer_le, roff);
3725 hdr = &ib->ihdr;
3726 e = Add2Ptr(ib, aoff);
3727
3728 if (is_baad(&ib->rhdr))
3729 goto dirty_vol;
3730
3731 if (!check_lsn(&ib->rhdr, rlsn))
3732 goto out;
3733 if (!check_index_buffer(ib, bytes) ||
3734 !check_if_alloc_index(hdr, aoff)) {
3735 goto dirty_vol;
3736 }
3737
3738 fname = (struct ATTR_FILE_NAME *)(e + 1);
3739 memmove(&fname->dup, data, sizeof(fname->dup));
3740
3741 a_dirty = true;
3742 ntfs_fix_pre_write(&ib->rhdr, bytes);
3743 break;
3744
3745 case SetBitsInNonresidentBitMap:
3746 off = le32_to_cpu(((struct BITMAP_RANGE *)data)->bitmap_off);
3747 bits = le32_to_cpu(((struct BITMAP_RANGE *)data)->bits);
3748
3749 if (cbo + (off + 7) / 8 > lco ||
3750 cbo + ((off + bits + 7) / 8) > lco) {
3751 goto dirty_vol;
3752 }
3753
3754 ntfs_bitmap_set_le(Add2Ptr(buffer_le, roff), off, bits);
3755 a_dirty = true;
3756 break;
3757
3758 case ClearBitsInNonresidentBitMap:
3759 off = le32_to_cpu(((struct BITMAP_RANGE *)data)->bitmap_off);
3760 bits = le32_to_cpu(((struct BITMAP_RANGE *)data)->bits);
3761
3762 if (cbo + (off + 7) / 8 > lco ||
3763 cbo + ((off + bits + 7) / 8) > lco) {
3764 goto dirty_vol;
3765 }
3766
3767 ntfs_bitmap_clear_le(Add2Ptr(buffer_le, roff), off, bits);
3768 a_dirty = true;
3769 break;
3770
3771 case UpdateRecordDataAllocation:
3772 ib = Add2Ptr(buffer_le, roff);
3773 hdr = &ib->ihdr;
3774 e = Add2Ptr(ib, aoff);
3775
3776 if (is_baad(&ib->rhdr))
3777 goto dirty_vol;
3778
3779 if (!check_lsn(&ib->rhdr, rlsn))
3780 goto out;
3781 if (!check_index_buffer(ib, bytes) ||
3782 !check_if_alloc_index(hdr, aoff)) {
3783 goto dirty_vol;
3784 }
3785
3786 /* See UpdateRecordDataRoot for the rationale. */
3787 if (le16_to_cpu(e->view.data_off) > le16_to_cpu(e->size) ||
3788 le16_to_cpu(e->view.data_off) + dlen > le16_to_cpu(e->size))
3789 goto dirty_vol;
3790
3791 memmove(Add2Ptr(e, le16_to_cpu(e->view.data_off)), data, dlen);
3792
3793 a_dirty = true;
3794 ntfs_fix_pre_write(&ib->rhdr, bytes);
3795 break;
3796
3797 default:
3798 WARN_ON(1);
3799 }
3800
3801 if (rlsn) {
3802 __le64 t64 = cpu_to_le64(*rlsn);
3803
3804 if (rec)
3805 rec->rhdr.lsn = t64;
3806 if (ib)
3807 ib->rhdr.lsn = t64;
3808 }
3809
3810 if (mi && mi->dirty) {
3811 err = mi_write(mi, 0);
3812 if (err)
3813 goto out;
3814 }
3815
3816 if (a_dirty) {
3817 attr = oa->attr;
3818 err = ntfs_sb_write_run(sbi, oa->run1, vbo, buffer_le, bytes,
3819 0);
3820 if (err)
3821 goto out;
3822 }
3823
3824 out:
3825
3826 if (inode)
3827 iput(inode);
3828 else if (mi != mi2_child)
3829 mi_put(mi);
3830
3831 kfree(buffer_le);
3832
3833 return err;
3834
3835 dirty_vol:
3836 log->set_dirty = true;
3837 goto out;
3838 }
3839
3840 /*
3841 * log_replay - Replays log and empties it.
3842 *
3843 * This function is called during mount operation.
3844 * It replays log and empties it.
3845 * Initialized is set false if logfile contains '-1'.
3846 */
log_replay(struct ntfs_inode * ni,bool * initialized)3847 int log_replay(struct ntfs_inode *ni, bool *initialized)
3848 {
3849 int err;
3850 struct ntfs_sb_info *sbi = ni->mi.sbi;
3851 struct ntfs_log *log;
3852
3853 u64 rec_lsn, checkpt_lsn = 0, rlsn = 0;
3854 struct ATTR_NAME_ENTRY *attr_names = NULL;
3855 u32 attr_names_bytes = 0;
3856 u32 oatbl_bytes = 0;
3857 struct RESTART_TABLE *dptbl = NULL;
3858 struct RESTART_TABLE *trtbl = NULL;
3859 const struct RESTART_TABLE *rt;
3860 struct RESTART_TABLE *oatbl = NULL;
3861 struct inode *inode;
3862 struct OpenAttr *oa;
3863 struct ntfs_inode *ni_oe;
3864 struct ATTRIB *attr = NULL;
3865 u64 size, vcn, undo_next_lsn;
3866 CLST rno, lcn, lcn0, len0, clen;
3867 void *data;
3868 struct NTFS_RESTART *rst = NULL;
3869 struct lcb *lcb = NULL;
3870 struct OPEN_ATTR_ENRTY *oe;
3871 struct ATTR_NAME_ENTRY *ane;
3872 struct TRANSACTION_ENTRY *tr;
3873 struct DIR_PAGE_ENTRY *dp;
3874 u32 i, bytes_per_attr_entry;
3875 u32 vbo, tail, off, dlen;
3876 u32 saved_len, rec_len, transact_id;
3877 bool use_second_page;
3878 struct RESTART_AREA *ra2, *ra = NULL;
3879 struct CLIENT_REC *ca, *cr;
3880 __le16 client;
3881 struct RESTART_HDR *rh;
3882 const struct LFS_RECORD_HDR *frh;
3883 const struct LOG_REC_HDR *lrh;
3884 bool is_mapped;
3885 bool is_ro = sb_rdonly(sbi->sb);
3886 u64 t64;
3887 u16 t16;
3888 u32 t32;
3889
3890 log = kzalloc_obj(struct ntfs_log, GFP_NOFS);
3891 if (!log)
3892 return -ENOMEM;
3893
3894 log->ni = ni;
3895 log->l_size = log->orig_file_size = ni->vfs_inode.i_size;
3896
3897 /* Get the size of page. NOTE: To replay we can use default page. */
3898 log->page_size = norm_file_page(PAGE_SIZE, &log->l_size, true);
3899 if (!log->page_size) {
3900 err = -EINVAL;
3901 goto out;
3902 }
3903
3904 log->one_page_buf = kmalloc(log->page_size, GFP_NOFS);
3905 if (!log->one_page_buf) {
3906 err = -ENOMEM;
3907 goto out;
3908 }
3909
3910 log->page_mask = log->page_size - 1;
3911 log->page_bits = blksize_bits(log->page_size);
3912
3913 /* Look for a restart area on the disk. */
3914 err = log_read_rst(log, true, &log->rst_info);
3915 if (err)
3916 goto out;
3917
3918 /* remember 'initialized' */
3919 *initialized = log->rst_info.initialized;
3920
3921 if (!log->rst_info.restart) {
3922 if (log->rst_info.initialized) {
3923 /* No restart area but the file is not initialized. */
3924 err = -EINVAL;
3925 goto out;
3926 }
3927
3928 log_init_pg_hdr(log, 1, 1);
3929 log_create(log, 0, get_random_u32(), false, false);
3930
3931 ra = log_create_ra(log);
3932 if (!ra) {
3933 err = -ENOMEM;
3934 goto out;
3935 }
3936 log->ra = ra;
3937 log->init_ra = true;
3938
3939 goto process_log;
3940 }
3941
3942 /*
3943 * If the restart offset above wasn't zero then we won't
3944 * look for a second restart.
3945 */
3946 if (log->rst_info.vbo)
3947 goto check_restart_area;
3948
3949 err = log_read_rst(log, false, &log->rst_info2);
3950 if (err)
3951 goto out;
3952
3953 /* Determine which restart area to use. */
3954 if (!log->rst_info2.restart ||
3955 log->rst_info2.last_lsn <= log->rst_info.last_lsn)
3956 goto use_first_page;
3957
3958 use_second_page = true;
3959
3960 if (log->rst_info.chkdsk_was_run &&
3961 log->page_size != log->rst_info.vbo) {
3962 struct RECORD_PAGE_HDR *sp = NULL;
3963 bool usa_error;
3964
3965 if (!read_log_page(log, log->page_size, &sp, &usa_error) &&
3966 sp->rhdr.sign == NTFS_CHKD_SIGNATURE) {
3967 use_second_page = false;
3968 }
3969 kfree(sp);
3970 }
3971
3972 if (use_second_page) {
3973 kfree(log->rst_info.r_page);
3974 memcpy(&log->rst_info, &log->rst_info2,
3975 sizeof(struct restart_info));
3976 log->rst_info2.r_page = NULL;
3977 }
3978
3979 use_first_page:
3980 kfree(log->rst_info2.r_page);
3981
3982 check_restart_area:
3983 /*
3984 * If the restart area is at offset 0, we want
3985 * to write the second restart area first.
3986 */
3987 log->init_ra = !!log->rst_info.vbo;
3988
3989 /* If we have a valid page then grab a pointer to the restart area. */
3990 ra2 = log->rst_info.valid_page ?
3991 Add2Ptr(log->rst_info.r_page,
3992 le16_to_cpu(log->rst_info.r_page->ra_off)) :
3993 NULL;
3994
3995 if (log->rst_info.chkdsk_was_run ||
3996 (ra2 && ra2->client_idx[1] == LFS_NO_CLIENT_LE)) {
3997 bool wrapped = false;
3998 bool use_multi_page = false;
3999 u32 open_log_count;
4000
4001 /* Do some checks based on whether we have a valid log page. */
4002 open_log_count = log->rst_info.valid_page ?
4003 le32_to_cpu(ra2->open_log_count) :
4004 get_random_u32();
4005
4006 log_init_pg_hdr(log, 1, 1);
4007
4008 log_create(log, log->rst_info.last_lsn, open_log_count, wrapped,
4009 use_multi_page);
4010
4011 ra = log_create_ra(log);
4012 if (!ra) {
4013 err = -ENOMEM;
4014 goto out;
4015 }
4016 log->ra = ra;
4017
4018 /* Put the restart areas and initialize
4019 * the log file as required.
4020 */
4021 goto process_log;
4022 }
4023
4024 if (!ra2) {
4025 err = -EINVAL;
4026 goto out;
4027 }
4028
4029 /*
4030 * If the log page or the system page sizes have changed, we can't
4031 * use the log file. We must use the system page size instead of the
4032 * default size if there is not a clean shutdown.
4033 */
4034 t32 = le32_to_cpu(log->rst_info.r_page->sys_page_size);
4035 if (log->page_size != t32) {
4036 u32 old_page_size = log->page_size;
4037
4038 log->l_size = log->orig_file_size;
4039 log->page_size = norm_file_page(t32, &log->l_size,
4040 t32 == DefaultLogPageSize);
4041
4042 /*
4043 * If the adopted on-disk page size is larger than the size used
4044 * to allocate one_page_buf above, grow the scratch buffer so a
4045 * later read_log_page() cannot overflow it.
4046 */
4047 if (log->page_size > old_page_size) {
4048 void *buf;
4049
4050 buf = krealloc(log->one_page_buf, log->page_size,
4051 GFP_NOFS);
4052 if (!buf) {
4053 err = -ENOMEM;
4054 goto out;
4055 }
4056 log->one_page_buf = buf;
4057 }
4058 }
4059
4060 if (log->page_size != t32 ||
4061 log->page_size != le32_to_cpu(log->rst_info.r_page->page_size)) {
4062 err = -EINVAL;
4063 goto out;
4064 }
4065
4066 log->page_mask = log->page_size - 1;
4067 log->page_bits = blksize_bits(log->page_size);
4068
4069 /* If the file size has shrunk then we won't mount it. */
4070 if (log->l_size < le64_to_cpu(ra2->l_size)) {
4071 err = -EINVAL;
4072 goto out;
4073 }
4074
4075 log_init_pg_hdr(log, le16_to_cpu(log->rst_info.r_page->major_ver),
4076 le16_to_cpu(log->rst_info.r_page->minor_ver));
4077
4078 log->l_size = le64_to_cpu(ra2->l_size);
4079 log->seq_num_bits = le32_to_cpu(ra2->seq_num_bits);
4080 log->file_data_bits = sizeof(u64) * 8 - log->seq_num_bits;
4081 log->seq_num_mask = (8 << log->file_data_bits) - 1;
4082 log->last_lsn = le64_to_cpu(ra2->current_lsn);
4083 log->seq_num = log->last_lsn >> log->file_data_bits;
4084 log->ra_off = le16_to_cpu(log->rst_info.r_page->ra_off);
4085 log->restart_size = log->sys_page_size - log->ra_off;
4086 log->record_header_len = le16_to_cpu(ra2->rec_hdr_len);
4087 log->ra_size = le16_to_cpu(ra2->ra_len);
4088 log->data_off = le16_to_cpu(ra2->data_off);
4089 log->data_size = log->page_size - log->data_off;
4090 log->reserved = log->data_size - log->record_header_len;
4091
4092 vbo = lsn_to_vbo(log, log->last_lsn);
4093
4094 if (vbo < log->first_page) {
4095 /* This is a pseudo lsn. */
4096 log->l_flags |= NTFSLOG_NO_LAST_LSN;
4097 log->next_page = log->first_page;
4098 goto find_oldest;
4099 }
4100
4101 /* Find the end of this log record. */
4102 off = final_log_off(log, log->last_lsn,
4103 le32_to_cpu(ra2->last_lsn_data_len));
4104
4105 /* If we wrapped the file then increment the sequence number. */
4106 if (off <= vbo) {
4107 log->seq_num += 1;
4108 log->l_flags |= NTFSLOG_WRAPPED;
4109 }
4110
4111 /* Now compute the next log page to use. */
4112 vbo &= ~log->sys_page_mask;
4113 tail = log->page_size - (off & log->page_mask) - 1;
4114
4115 /*
4116 *If we can fit another log record on the page,
4117 * move back a page the log file.
4118 */
4119 if (tail >= log->record_header_len) {
4120 log->l_flags |= NTFSLOG_REUSE_TAIL;
4121 log->next_page = vbo;
4122 } else {
4123 log->next_page = next_page_off(log, vbo);
4124 }
4125
4126 find_oldest:
4127 /*
4128 * Find the oldest client lsn. Use the last
4129 * flushed lsn as a starting point.
4130 */
4131 log->oldest_lsn = log->last_lsn;
4132 oldest_client_lsn(Add2Ptr(ra2, le16_to_cpu(ra2->client_off)),
4133 ra2->client_idx[1], &log->oldest_lsn);
4134 log->oldest_lsn_off = lsn_to_vbo(log, log->oldest_lsn);
4135
4136 if (log->oldest_lsn_off < log->first_page)
4137 log->l_flags |= NTFSLOG_NO_OLDEST_LSN;
4138
4139 if (!(ra2->flags & RESTART_SINGLE_PAGE_IO))
4140 log->l_flags |= NTFSLOG_WRAPPED | NTFSLOG_MULTIPLE_PAGE_IO;
4141
4142 log->current_openlog_count = le32_to_cpu(ra2->open_log_count);
4143 log->total_avail_pages = log->l_size - log->first_page;
4144 log->total_avail = log->total_avail_pages >> log->page_bits;
4145 log->max_current_avail = log->total_avail * log->reserved;
4146 log->total_avail = log->total_avail * log->data_size;
4147
4148 log->current_avail = current_log_avail(log);
4149
4150 ra = kzalloc(log->restart_size, GFP_NOFS);
4151 if (!ra) {
4152 err = -ENOMEM;
4153 goto out;
4154 }
4155 log->ra = ra;
4156
4157 t16 = le16_to_cpu(ra2->client_off);
4158 if (t16 == offsetof(struct RESTART_AREA, clients)) {
4159 memcpy(ra, ra2, log->ra_size);
4160 } else {
4161 memcpy(ra, ra2, offsetof(struct RESTART_AREA, clients));
4162 memcpy(ra->clients, Add2Ptr(ra2, t16),
4163 le16_to_cpu(ra2->ra_len) - t16);
4164
4165 log->current_openlog_count = get_random_u32();
4166 ra->open_log_count = cpu_to_le32(log->current_openlog_count);
4167 log->ra_size = offsetof(struct RESTART_AREA, clients) +
4168 sizeof(struct CLIENT_REC);
4169 ra->client_off =
4170 cpu_to_le16(offsetof(struct RESTART_AREA, clients));
4171 ra->ra_len = cpu_to_le16(log->ra_size);
4172 }
4173
4174 le32_add_cpu(&ra->open_log_count, 1);
4175
4176 /* Now we need to walk through looking for the last lsn. */
4177 err = last_log_lsn(log);
4178 if (err)
4179 goto out;
4180
4181 log->current_avail = current_log_avail(log);
4182
4183 /* Remember which restart area to write first. */
4184 log->init_ra = log->rst_info.vbo;
4185
4186 process_log:
4187 /* 1.0, 1.1, 2.0 log->major_ver/minor_ver - short values. */
4188 switch ((log->major_ver << 16) + log->minor_ver) {
4189 case 0x10000:
4190 case 0x10001:
4191 case 0x20000:
4192 break;
4193 default:
4194 ntfs_warn(sbi->sb, "\x24LogFile version %d.%d is not supported",
4195 log->major_ver, log->minor_ver);
4196 err = -EOPNOTSUPP;
4197 log->set_dirty = true;
4198 goto out;
4199 }
4200
4201 /* One client "NTFS" per logfile. */
4202 ca = Add2Ptr(ra, le16_to_cpu(ra->client_off));
4203
4204 for (client = ra->client_idx[1];; client = cr->next_client) {
4205 if (client == LFS_NO_CLIENT_LE) {
4206 /* Insert "NTFS" client LogFile. */
4207 client = ra->client_idx[0];
4208 if (client == LFS_NO_CLIENT_LE) {
4209 err = -EINVAL;
4210 goto out;
4211 }
4212
4213 t16 = le16_to_cpu(client);
4214 cr = ca + t16;
4215
4216 remove_client(ca, cr, &ra->client_idx[0]);
4217
4218 cr->restart_lsn = 0;
4219 cr->oldest_lsn = cpu_to_le64(log->oldest_lsn);
4220 cr->name_bytes = cpu_to_le32(8);
4221 cr->name[0] = cpu_to_le16('N');
4222 cr->name[1] = cpu_to_le16('T');
4223 cr->name[2] = cpu_to_le16('F');
4224 cr->name[3] = cpu_to_le16('S');
4225
4226 add_client(ca, t16, &ra->client_idx[1]);
4227 break;
4228 }
4229
4230 cr = ca + le16_to_cpu(client);
4231
4232 if (cpu_to_le32(8) == cr->name_bytes &&
4233 cpu_to_le16('N') == cr->name[0] &&
4234 cpu_to_le16('T') == cr->name[1] &&
4235 cpu_to_le16('F') == cr->name[2] &&
4236 cpu_to_le16('S') == cr->name[3])
4237 break;
4238 }
4239
4240 /* Update the client handle with the client block information. */
4241 log->client_id.seq_num = cr->seq_num;
4242 log->client_id.client_idx = client;
4243
4244 err = read_rst_area(log, &rst, &checkpt_lsn);
4245 if (err)
4246 goto out;
4247
4248 if (!rst)
4249 goto out;
4250
4251 bytes_per_attr_entry = !rst->major_ver ? 0x2C : 0x28;
4252
4253 if (rst->check_point_start)
4254 checkpt_lsn = le64_to_cpu(rst->check_point_start);
4255
4256 /* Allocate and Read the Transaction Table. */
4257 if (!rst->transact_table_len)
4258 goto check_dirty_page_table; /* reduce tab pressure. */
4259
4260 t64 = le64_to_cpu(rst->transact_table_lsn);
4261 err = read_log_rec_lcb(log, t64, lcb_ctx_prev, &lcb);
4262 if (err)
4263 goto out;
4264
4265 lrh = lcb->log_rec;
4266 frh = lcb->lrh;
4267 rec_len = le32_to_cpu(frh->client_data_len);
4268
4269 if (!check_log_rec(lrh, rec_len, le32_to_cpu(frh->transact_id),
4270 bytes_per_attr_entry)) {
4271 err = -EINVAL;
4272 goto out;
4273 }
4274
4275 t16 = le16_to_cpu(lrh->redo_off);
4276
4277 rt = Add2Ptr(lrh, t16);
4278 t32 = rec_len - t16;
4279
4280 /* Now check that this is a valid restart table. */
4281 if (!check_rstbl(rt, t32)) {
4282 err = -EINVAL;
4283 goto out;
4284 }
4285
4286 trtbl = kmemdup(rt, t32, GFP_NOFS);
4287 if (!trtbl) {
4288 err = -ENOMEM;
4289 goto out;
4290 }
4291
4292 lcb_put(lcb);
4293 lcb = NULL;
4294
4295 check_dirty_page_table:
4296 /* The next record back should be the Dirty Pages Table. */
4297 if (!rst->dirty_pages_len)
4298 goto check_attribute_names; /* reduce tab pressure. */
4299
4300 t64 = le64_to_cpu(rst->dirty_pages_table_lsn);
4301 err = read_log_rec_lcb(log, t64, lcb_ctx_prev, &lcb);
4302 if (err)
4303 goto out;
4304
4305 lrh = lcb->log_rec;
4306 frh = lcb->lrh;
4307 rec_len = le32_to_cpu(frh->client_data_len);
4308
4309 if (!check_log_rec(lrh, rec_len, le32_to_cpu(frh->transact_id),
4310 bytes_per_attr_entry)) {
4311 err = -EINVAL;
4312 goto out;
4313 }
4314
4315 t16 = le16_to_cpu(lrh->redo_off);
4316
4317 rt = Add2Ptr(lrh, t16);
4318 t32 = rec_len - t16;
4319
4320 /* Now check that this is a valid restart table. */
4321 if (!check_rstbl(rt, t32)) {
4322 err = -EINVAL;
4323 goto out;
4324 }
4325
4326 if (!check_dp_table(rt)) {
4327 err = -EINVAL;
4328 goto out;
4329 }
4330
4331 dptbl = kmemdup(rt, t32, GFP_NOFS);
4332 if (!dptbl) {
4333 err = -ENOMEM;
4334 goto out;
4335 }
4336
4337 /* Convert Ra version '0' into version '1'. */
4338 if (rst->major_ver)
4339 goto end_conv_1; /* reduce tab pressure. */
4340
4341 t16 = le16_to_cpu(dptbl->size);
4342 if (t16 < sizeof(struct DIR_PAGE_ENTRY)) {
4343 log->set_dirty = true;
4344 goto out;
4345 }
4346
4347 t32 = (t16 - sizeof(struct DIR_PAGE_ENTRY)) / sizeof(u64);
4348
4349 dp = NULL;
4350 while ((dp = enum_rstbl(dptbl, dp))) {
4351 struct DIR_PAGE_ENTRY_32 *dp0 = (struct DIR_PAGE_ENTRY_32 *)dp;
4352 u32 lcns = le32_to_cpu(dp->lcns_follow);
4353
4354 if (lcns > t32) {
4355 log->set_dirty = true;
4356 goto out;
4357 }
4358
4359 memmove(&dp->vcn, &dp0->vcn_low,
4360 2 * sizeof(u64) + lcns * sizeof(u64));
4361 }
4362
4363 end_conv_1:
4364 lcb_put(lcb);
4365 lcb = NULL;
4366
4367 /*
4368 * Go through the table and remove the duplicates,
4369 * remembering the oldest lsn values.
4370 */
4371 if (sbi->cluster_size <= log->page_size)
4372 goto trace_dp_table; /* reduce tab pressure. */
4373 dp = NULL;
4374 while ((dp = enum_rstbl(dptbl, dp))) {
4375 struct DIR_PAGE_ENTRY *next = dp;
4376
4377 while ((next = enum_rstbl(dptbl, next))) {
4378 if (next->target_attr == dp->target_attr &&
4379 next->vcn == dp->vcn) {
4380 if (le64_to_cpu(next->oldest_lsn) <
4381 le64_to_cpu(dp->oldest_lsn)) {
4382 dp->oldest_lsn = next->oldest_lsn;
4383 }
4384
4385 free_rsttbl_idx(dptbl, PtrOffset(dptbl, next));
4386 }
4387 }
4388 }
4389 trace_dp_table:
4390 check_attribute_names:
4391 /* The next record should be the Attribute Names. */
4392 if (!rst->attr_names_len)
4393 goto check_attr_table; /* reduce tab pressure. */
4394
4395 t64 = le64_to_cpu(rst->attr_names_lsn);
4396 err = read_log_rec_lcb(log, t64, lcb_ctx_prev, &lcb);
4397 if (err)
4398 goto out;
4399
4400 lrh = lcb->log_rec;
4401 frh = lcb->lrh;
4402 rec_len = le32_to_cpu(frh->client_data_len);
4403
4404 if (!check_log_rec(lrh, rec_len, le32_to_cpu(frh->transact_id),
4405 bytes_per_attr_entry)) {
4406 err = -EINVAL;
4407 goto out;
4408 }
4409
4410 t32 = lrh_length(lrh);
4411 attr_names_bytes = rec_len - t32;
4412
4413 attr_names = kmemdup(Add2Ptr(lrh, t32), attr_names_bytes, GFP_NOFS);
4414 if (!attr_names) {
4415 err = -ENOMEM;
4416 goto out;
4417 }
4418
4419 lcb_put(lcb);
4420 lcb = NULL;
4421
4422 check_attr_table:
4423 /* The next record should be the attribute Table. */
4424 if (!rst->open_attr_len)
4425 goto check_attribute_names2; /* reduce tab pressure. */
4426
4427 t64 = le64_to_cpu(rst->open_attr_table_lsn);
4428 err = read_log_rec_lcb(log, t64, lcb_ctx_prev, &lcb);
4429 if (err)
4430 goto out;
4431
4432 lrh = lcb->log_rec;
4433 frh = lcb->lrh;
4434 rec_len = le32_to_cpu(frh->client_data_len);
4435
4436 if (!check_log_rec(lrh, rec_len, le32_to_cpu(frh->transact_id),
4437 bytes_per_attr_entry)) {
4438 err = -EINVAL;
4439 goto out;
4440 }
4441
4442 t16 = le16_to_cpu(lrh->redo_off);
4443
4444 rt = Add2Ptr(lrh, t16);
4445 oatbl_bytes = rec_len - t16;
4446
4447 if (!check_rstbl(rt, oatbl_bytes)) {
4448 err = -EINVAL;
4449 goto out;
4450 }
4451
4452 oatbl = kmemdup(rt, oatbl_bytes, GFP_NOFS);
4453 if (!oatbl) {
4454 err = -ENOMEM;
4455 goto out;
4456 }
4457
4458 log->open_attr_tbl = oatbl;
4459
4460 /* Clear all of the Attr pointers. */
4461 oe = NULL;
4462 while ((oe = enum_rstbl(oatbl, oe))) {
4463 if (!rst->major_ver) {
4464 struct OPEN_ATTR_ENRTY_32 oe0;
4465
4466 /* Really 'oe' points to OPEN_ATTR_ENRTY_32. */
4467 memcpy(&oe0, oe, SIZEOF_OPENATTRIBUTEENTRY0);
4468
4469 oe->bytes_per_index = oe0.bytes_per_index;
4470 oe->type = oe0.type;
4471 oe->is_dirty_pages = oe0.is_dirty_pages;
4472 oe->name_len = 0;
4473 oe->ref = oe0.ref;
4474 oe->open_record_lsn = oe0.open_record_lsn;
4475 }
4476
4477 oe->is_attr_name = 0;
4478 oe->ptr = NULL;
4479 }
4480
4481 lcb_put(lcb);
4482 lcb = NULL;
4483
4484 check_attribute_names2:
4485 if (attr_names && oatbl) {
4486 off = 0;
4487 for (;;) {
4488 /* Check we can use attribute name entry 'ane'. */
4489 static_assert(sizeof(*ane) == 4);
4490 if (off + sizeof(*ane) > attr_names_bytes) {
4491 /* just ignore the rest. */
4492 break;
4493 }
4494
4495 ane = Add2Ptr(attr_names, off);
4496 t16 = le16_to_cpu(ane->off);
4497 if (!t16) {
4498 /* this is the only valid exit. */
4499 break;
4500 }
4501
4502 /* Check we can use open attribute entry 'oe'. */
4503 if (t16 + sizeof(*oe) > oatbl_bytes) {
4504 /* just ignore the rest. */
4505 break;
4506 }
4507
4508 /* TODO: Clear table on exit! */
4509 oe = Add2Ptr(oatbl, t16);
4510 t16 = le16_to_cpu(ane->name_bytes);
4511 off += t16 + sizeof(*ane);
4512 if (off > attr_names_bytes) {
4513 /* just ignore the rest. */
4514 break;
4515 }
4516 oe->name_len = t16 / sizeof(short);
4517 oe->ptr = ane->name;
4518 oe->is_attr_name = 2;
4519 }
4520 }
4521
4522 /*
4523 * If the checkpt_lsn is zero, then this is a freshly
4524 * formatted disk and we have no work to do.
4525 */
4526 if (!checkpt_lsn) {
4527 err = 0;
4528 goto out;
4529 }
4530
4531 if (!oatbl) {
4532 oatbl = init_rsttbl(bytes_per_attr_entry, 8);
4533 if (!oatbl) {
4534 err = -ENOMEM;
4535 goto out;
4536 }
4537 }
4538
4539 log->open_attr_tbl = oatbl;
4540
4541 /* Start the analysis pass from the Checkpoint lsn. */
4542 rec_lsn = checkpt_lsn;
4543
4544 /* Read the first lsn. */
4545 err = read_log_rec_lcb(log, checkpt_lsn, lcb_ctx_next, &lcb);
4546 if (err)
4547 goto out;
4548
4549 /* Loop to read all subsequent records to the end of the log file. */
4550 next_log_record_analyze:
4551 err = read_next_log_rec(log, lcb, &rec_lsn);
4552 if (err)
4553 goto out;
4554
4555 if (!rec_lsn)
4556 goto end_log_records_enumerate;
4557
4558 frh = lcb->lrh;
4559 transact_id = le32_to_cpu(frh->transact_id);
4560 rec_len = le32_to_cpu(frh->client_data_len);
4561 lrh = lcb->log_rec;
4562
4563 if (!check_log_rec(lrh, rec_len, transact_id, bytes_per_attr_entry)) {
4564 err = -EINVAL;
4565 goto out;
4566 }
4567
4568 /*
4569 * The first lsn after the previous lsn remembered
4570 * the checkpoint is the first candidate for the rlsn.
4571 */
4572 if (!rlsn)
4573 rlsn = rec_lsn;
4574
4575 if (LfsClientRecord != frh->record_type)
4576 goto next_log_record_analyze;
4577
4578 /*
4579 * Now update the Transaction Table for this transaction. If there
4580 * is no entry present or it is unallocated we allocate the entry.
4581 */
4582 if (!trtbl) {
4583 trtbl = init_rsttbl(sizeof(struct TRANSACTION_ENTRY),
4584 INITIAL_NUMBER_TRANSACTIONS);
4585 if (!trtbl) {
4586 err = -ENOMEM;
4587 goto out;
4588 }
4589 }
4590
4591 tr = Add2Ptr(trtbl, transact_id);
4592
4593 if (transact_id >= bytes_per_rt(trtbl) ||
4594 tr->next != RESTART_ENTRY_ALLOCATED_LE) {
4595 tr = alloc_rsttbl_from_idx(&trtbl, transact_id);
4596 if (!tr) {
4597 err = -ENOMEM;
4598 goto out;
4599 }
4600 tr->transact_state = TransactionActive;
4601 tr->first_lsn = cpu_to_le64(rec_lsn);
4602 }
4603
4604 tr->prev_lsn = tr->undo_next_lsn = cpu_to_le64(rec_lsn);
4605
4606 /*
4607 * If this is a compensation log record, then change
4608 * the undo_next_lsn to be the undo_next_lsn of this record.
4609 */
4610 if (lrh->undo_op == cpu_to_le16(CompensationLogRecord))
4611 tr->undo_next_lsn = frh->client_undo_next_lsn;
4612
4613 /* Dispatch to handle log record depending on type. */
4614 switch (le16_to_cpu(lrh->redo_op)) {
4615 case InitializeFileRecordSegment:
4616 case DeallocateFileRecordSegment:
4617 case WriteEndOfFileRecordSegment:
4618 case CreateAttribute:
4619 case DeleteAttribute:
4620 case UpdateResidentValue:
4621 case UpdateNonresidentValue:
4622 case UpdateMappingPairs:
4623 case SetNewAttributeSizes:
4624 case AddIndexEntryRoot:
4625 case DeleteIndexEntryRoot:
4626 case AddIndexEntryAllocation:
4627 case DeleteIndexEntryAllocation:
4628 case WriteEndOfIndexBuffer:
4629 case SetIndexEntryVcnRoot:
4630 case SetIndexEntryVcnAllocation:
4631 case UpdateFileNameRoot:
4632 case UpdateFileNameAllocation:
4633 case SetBitsInNonresidentBitMap:
4634 case ClearBitsInNonresidentBitMap:
4635 case UpdateRecordDataRoot:
4636 case UpdateRecordDataAllocation:
4637 case ZeroEndOfFileRecord:
4638 t16 = le16_to_cpu(lrh->target_attr);
4639 t64 = le64_to_cpu(lrh->target_vcn);
4640 dp = find_dp(dptbl, t16, t64);
4641
4642 if (dp)
4643 goto copy_lcns;
4644
4645 /*
4646 * Calculate the number of clusters per page the system
4647 * which wrote the checkpoint, possibly creating the table.
4648 */
4649 if (dptbl) {
4650 t32 = (le16_to_cpu(dptbl->size) -
4651 sizeof(struct DIR_PAGE_ENTRY)) /
4652 sizeof(u64);
4653 } else {
4654 t32 = log->clst_per_page;
4655 kfree(dptbl);
4656 dptbl = init_rsttbl(struct_size(dp, page_lcns, t32),
4657 32);
4658 if (!dptbl) {
4659 err = -ENOMEM;
4660 goto out;
4661 }
4662 }
4663
4664 dp = alloc_rsttbl_idx(&dptbl);
4665 if (!dp) {
4666 err = -ENOMEM;
4667 goto out;
4668 }
4669 dp->target_attr = cpu_to_le32(t16);
4670 dp->transfer_len = cpu_to_le32(t32 << sbi->cluster_bits);
4671 dp->lcns_follow = cpu_to_le32(t32);
4672 dp->vcn = cpu_to_le64(t64 & ~((u64)t32 - 1));
4673 dp->oldest_lsn = cpu_to_le64(rec_lsn);
4674
4675 copy_lcns:
4676 /*
4677 * Copy the Lcns from the log record into the Dirty Page Entry.
4678 * TODO: For different page size support, must somehow make
4679 * whole routine a loop, case Lcns do not fit below.
4680 */
4681 t16 = le16_to_cpu(lrh->lcns_follow);
4682 t32 = le32_to_cpu(dp->lcns_follow);
4683 if (le64_to_cpu(lrh->target_vcn) < le64_to_cpu(dp->vcn)) {
4684 err = -EINVAL;
4685 goto out;
4686 }
4687
4688 /*
4689 * find_dp() only validates that target_vcn is the first
4690 * cluster covered by dp. The walk through lrh->lcns_follow
4691 * further entries must stay within the allocated
4692 * dp->page_lcns[] array, which is sized by dp->lcns_follow.
4693 */
4694 if (le64_to_cpu(lrh->target_vcn) - le64_to_cpu(dp->vcn) + t16 >
4695 le32_to_cpu(dp->lcns_follow)) {
4696 err = -EINVAL;
4697 log->set_dirty = true;
4698 goto out;
4699 }
4700
4701 for (i = 0; i < t16; i++) {
4702 size_t j = (size_t)(le64_to_cpu(lrh->target_vcn) -
4703 le64_to_cpu(dp->vcn));
4704 if (j >= t32 || i >= t32 - j) {
4705 err = -EINVAL;
4706 goto out;
4707 }
4708 dp->page_lcns[j + i] = lrh->page_lcns[i];
4709 }
4710 goto next_log_record_analyze;
4711
4712 case DeleteDirtyClusters: {
4713 u32 range_count =
4714 le16_to_cpu(lrh->redo_len) / sizeof(struct LCN_RANGE);
4715 const struct LCN_RANGE *r =
4716 Add2Ptr(lrh, le16_to_cpu(lrh->redo_off));
4717
4718 /* Loop through all of the Lcn ranges this log record. */
4719 for (i = 0; i < range_count; i++, r++) {
4720 u64 lcn0 = le64_to_cpu(r->lcn);
4721 u64 lcn_e = lcn0 + le64_to_cpu(r->len) - 1;
4722
4723 dp = NULL;
4724 while ((dp = enum_rstbl(dptbl, dp))) {
4725 u32 j;
4726
4727 t32 = le32_to_cpu(dp->lcns_follow);
4728 for (j = 0; j < t32; j++) {
4729 t64 = le64_to_cpu(dp->page_lcns[j]);
4730 if (t64 >= lcn0 && t64 <= lcn_e)
4731 dp->page_lcns[j] = 0;
4732 }
4733 }
4734 }
4735 goto next_log_record_analyze;
4736 }
4737
4738 case OpenNonresidentAttribute:
4739 t16 = le16_to_cpu(lrh->target_attr);
4740 if (t16 >= bytes_per_rt(oatbl)) {
4741 /*
4742 * Compute how big the table needs to be.
4743 * Add 10 extra entries for some cushion.
4744 */
4745 u32 new_e = t16 / le16_to_cpu(oatbl->size);
4746
4747 new_e += 10 - le16_to_cpu(oatbl->used);
4748
4749 oatbl = extend_rsttbl(oatbl, new_e, ~0u);
4750 log->open_attr_tbl = oatbl;
4751 if (!oatbl) {
4752 err = -ENOMEM;
4753 goto out;
4754 }
4755 }
4756
4757 /* Point to the entry being opened. */
4758 oe = alloc_rsttbl_from_idx(&oatbl, t16);
4759 log->open_attr_tbl = oatbl;
4760 if (!oe) {
4761 err = -ENOMEM;
4762 goto out;
4763 }
4764
4765 /* Initialize this entry from the log record. */
4766 t16 = le16_to_cpu(lrh->redo_off);
4767 if (!rst->major_ver) {
4768 /* Convert version '0' into version '1'. */
4769 struct OPEN_ATTR_ENRTY_32 *oe0 = Add2Ptr(lrh, t16);
4770
4771 oe->bytes_per_index = oe0->bytes_per_index;
4772 oe->type = oe0->type;
4773 oe->is_dirty_pages = oe0->is_dirty_pages;
4774 oe->name_len = 0; //oe0.name_len;
4775 oe->ref = oe0->ref;
4776 oe->open_record_lsn = oe0->open_record_lsn;
4777 } else {
4778 memcpy(oe, Add2Ptr(lrh, t16), bytes_per_attr_entry);
4779 }
4780
4781 t16 = le16_to_cpu(lrh->undo_len);
4782 if (t16) {
4783 oe->ptr = kmalloc(t16, GFP_NOFS);
4784 if (!oe->ptr) {
4785 err = -ENOMEM;
4786 goto out;
4787 }
4788 oe->name_len = t16 / sizeof(short);
4789 memcpy(oe->ptr,
4790 Add2Ptr(lrh, le16_to_cpu(lrh->undo_off)), t16);
4791 oe->is_attr_name = 1;
4792 } else {
4793 oe->ptr = NULL;
4794 oe->is_attr_name = 0;
4795 }
4796
4797 goto next_log_record_analyze;
4798
4799 case HotFix:
4800 t16 = le16_to_cpu(lrh->target_attr);
4801 t64 = le64_to_cpu(lrh->target_vcn);
4802 dp = find_dp(dptbl, t16, t64);
4803 if (dp) {
4804 size_t j = le64_to_cpu(lrh->target_vcn) -
4805 le64_to_cpu(dp->vcn);
4806 if (dp->page_lcns[j])
4807 dp->page_lcns[j] = lrh->page_lcns[0];
4808 }
4809 goto next_log_record_analyze;
4810
4811 case EndTopLevelAction:
4812 tr = Add2Ptr(trtbl, transact_id);
4813 tr->prev_lsn = cpu_to_le64(rec_lsn);
4814 tr->undo_next_lsn = frh->client_undo_next_lsn;
4815 goto next_log_record_analyze;
4816
4817 case PrepareTransaction:
4818 tr = Add2Ptr(trtbl, transact_id);
4819 tr->transact_state = TransactionPrepared;
4820 goto next_log_record_analyze;
4821
4822 case CommitTransaction:
4823 tr = Add2Ptr(trtbl, transact_id);
4824 tr->transact_state = TransactionCommitted;
4825 goto next_log_record_analyze;
4826
4827 case ForgetTransaction:
4828 free_rsttbl_idx(trtbl, transact_id);
4829 goto next_log_record_analyze;
4830
4831 case Noop:
4832 case OpenAttributeTableDump:
4833 case AttributeNamesDump:
4834 case DirtyPageTableDump:
4835 case TransactionTableDump:
4836 /* The following cases require no action the Analysis Pass. */
4837 goto next_log_record_analyze;
4838
4839 default:
4840 /*
4841 * All codes will be explicitly handled.
4842 * If we see a code we do not expect, then we are trouble.
4843 */
4844 goto next_log_record_analyze;
4845 }
4846
4847 end_log_records_enumerate:
4848 lcb_put(lcb);
4849 lcb = NULL;
4850
4851 /*
4852 * Scan the Dirty Page Table and Transaction Table for
4853 * the lowest lsn, and return it as the Redo lsn.
4854 */
4855 dp = NULL;
4856 while ((dp = enum_rstbl(dptbl, dp))) {
4857 t64 = le64_to_cpu(dp->oldest_lsn);
4858 if (t64 && t64 < rlsn)
4859 rlsn = t64;
4860 }
4861
4862 tr = NULL;
4863 while ((tr = enum_rstbl(trtbl, tr))) {
4864 t64 = le64_to_cpu(tr->first_lsn);
4865 if (t64 && t64 < rlsn)
4866 rlsn = t64;
4867 }
4868
4869 /*
4870 * Only proceed if the Dirty Page Table or Transaction
4871 * table are not empty.
4872 */
4873 if ((!dptbl || !dptbl->total) && (!trtbl || !trtbl->total))
4874 goto end_replay;
4875
4876 sbi->flags |= NTFS_FLAGS_NEED_REPLAY;
4877 if (is_ro)
4878 goto out;
4879
4880 /* Reopen all of the attributes with dirty pages. */
4881 oe = NULL;
4882 next_open_attribute:
4883
4884 oe = enum_rstbl(oatbl, oe);
4885 if (!oe) {
4886 err = 0;
4887 dp = NULL;
4888 goto next_dirty_page;
4889 }
4890
4891 oa = kzalloc_obj(struct OpenAttr, GFP_NOFS);
4892 if (!oa) {
4893 err = -ENOMEM;
4894 goto out;
4895 }
4896
4897 inode = ntfs_iget5(sbi->sb, &oe->ref, NULL);
4898 if (IS_ERR(inode))
4899 goto fake_attr;
4900
4901 if (is_bad_inode(inode)) {
4902 iput(inode);
4903 fake_attr:
4904 if (oa->ni) {
4905 iput(&oa->ni->vfs_inode);
4906 oa->ni = NULL;
4907 }
4908
4909 attr = attr_create_nonres_log(sbi, oe->type, 0, oe->ptr,
4910 oe->name_len, 0);
4911 if (!attr) {
4912 kfree(oa);
4913 err = -ENOMEM;
4914 goto out;
4915 }
4916 oa->attr = attr;
4917 oa->run1 = &oa->run0;
4918 goto final_oe;
4919 }
4920
4921 ni_oe = ntfs_i(inode);
4922 oa->ni = ni_oe;
4923
4924 attr = ni_find_attr(ni_oe, NULL, NULL, oe->type, oe->ptr, oe->name_len,
4925 NULL, NULL);
4926
4927 if (!attr)
4928 goto fake_attr;
4929
4930 t32 = le32_to_cpu(attr->size);
4931 oa->attr = kmemdup(attr, t32, GFP_NOFS);
4932 if (!oa->attr)
4933 goto fake_attr;
4934
4935 if (!S_ISDIR(inode->i_mode)) {
4936 if (attr->type == ATTR_DATA && !attr->name_len) {
4937 oa->run1 = &ni_oe->file.run;
4938 goto final_oe;
4939 }
4940 } else {
4941 if (attr->type == ATTR_ALLOC &&
4942 attr->name_len == ARRAY_SIZE(I30_NAME) &&
4943 !memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME))) {
4944 oa->run1 = &ni_oe->dir.alloc_run;
4945 goto final_oe;
4946 }
4947 }
4948
4949 if (attr->non_res) {
4950 u16 roff = le16_to_cpu(attr->nres.run_off);
4951 CLST svcn = le64_to_cpu(attr->nres.svcn);
4952
4953 if (roff > t32) {
4954 kfree(oa->attr);
4955 oa->attr = NULL;
4956 goto fake_attr;
4957 }
4958
4959 err = run_unpack(&oa->run0, sbi, inode->i_ino, svcn,
4960 le64_to_cpu(attr->nres.evcn), svcn,
4961 Add2Ptr(attr, roff), t32 - roff);
4962 if (err < 0) {
4963 kfree(oa->attr);
4964 oa->attr = NULL;
4965 goto fake_attr;
4966 }
4967 err = 0;
4968 }
4969 oa->run1 = &oa->run0;
4970 attr = oa->attr;
4971
4972 final_oe:
4973 if (oe->is_attr_name == 1)
4974 kfree(oe->ptr);
4975 oe->is_attr_name = 0;
4976 oe->ptr = oa;
4977 oe->name_len = attr->name_len;
4978
4979 goto next_open_attribute;
4980
4981 /*
4982 * Now loop through the dirty page table to extract all of the Vcn/Lcn.
4983 * Mapping that we have, and insert it into the appropriate run.
4984 */
4985 next_dirty_page:
4986 dp = enum_rstbl(dptbl, dp);
4987 if (!dp)
4988 goto do_redo_1;
4989
4990 oe = Add2Ptr(oatbl, le32_to_cpu(dp->target_attr));
4991
4992 if (oe->next != RESTART_ENTRY_ALLOCATED_LE)
4993 goto next_dirty_page;
4994
4995 oa = oe->ptr;
4996 if (!oa)
4997 goto next_dirty_page;
4998
4999 i = -1;
5000 next_dirty_page_vcn:
5001 i += 1;
5002 if (i >= le32_to_cpu(dp->lcns_follow))
5003 goto next_dirty_page;
5004
5005 vcn = le64_to_cpu(dp->vcn) + i;
5006 size = (vcn + 1) << sbi->cluster_bits;
5007
5008 if (!dp->page_lcns[i])
5009 goto next_dirty_page_vcn;
5010
5011 rno = ino_get(&oe->ref);
5012 if (rno <= MFT_REC_MIRR &&
5013 size < (MFT_REC_VOL + 1) * sbi->record_size &&
5014 oe->type == ATTR_DATA) {
5015 goto next_dirty_page_vcn;
5016 }
5017
5018 lcn = le64_to_cpu(dp->page_lcns[i]);
5019
5020 if ((!run_lookup_entry(oa->run1, vcn, &lcn0, &len0, NULL) ||
5021 lcn0 != lcn) &&
5022 !run_add_entry(oa->run1, vcn, lcn, 1, false)) {
5023 err = -ENOMEM;
5024 goto out;
5025 }
5026 attr = oa->attr;
5027 if (size > le64_to_cpu(attr->nres.alloc_size)) {
5028 attr->nres.valid_size = attr->nres.data_size =
5029 attr->nres.alloc_size = cpu_to_le64(size);
5030 }
5031 goto next_dirty_page_vcn;
5032
5033 do_redo_1:
5034 /*
5035 * Perform the Redo Pass, to restore all of the dirty pages to the same
5036 * contents that they had immediately before the crash. If the dirty
5037 * page table is empty, then we can skip the entire Redo Pass.
5038 */
5039 if (!dptbl || !dptbl->total)
5040 goto do_undo_action;
5041
5042 rec_lsn = rlsn;
5043
5044 /*
5045 * Read the record at the Redo lsn, before falling
5046 * into common code to handle each record.
5047 */
5048 err = read_log_rec_lcb(log, rlsn, lcb_ctx_next, &lcb);
5049 if (err)
5050 goto out;
5051
5052 /*
5053 * Now loop to read all of our log records forwards, until
5054 * we hit the end of the file, cleaning up at the end.
5055 */
5056 do_action_next:
5057 frh = lcb->lrh;
5058
5059 if (LfsClientRecord != frh->record_type)
5060 goto read_next_log_do_action;
5061
5062 transact_id = le32_to_cpu(frh->transact_id);
5063 rec_len = le32_to_cpu(frh->client_data_len);
5064 lrh = lcb->log_rec;
5065
5066 if (!check_log_rec(lrh, rec_len, transact_id, bytes_per_attr_entry)) {
5067 err = -EINVAL;
5068 goto out;
5069 }
5070
5071 /* Ignore log records that do not update pages. */
5072 if (lrh->lcns_follow)
5073 goto find_dirty_page;
5074
5075 goto read_next_log_do_action;
5076
5077 find_dirty_page:
5078 t16 = le16_to_cpu(lrh->target_attr);
5079 t64 = le64_to_cpu(lrh->target_vcn);
5080 dp = find_dp(dptbl, t16, t64);
5081
5082 if (!dp)
5083 goto read_next_log_do_action;
5084
5085 if (rec_lsn < le64_to_cpu(dp->oldest_lsn))
5086 goto read_next_log_do_action;
5087
5088 t16 = le16_to_cpu(lrh->target_attr);
5089 if (t16 >= bytes_per_rt(oatbl)) {
5090 err = -EINVAL;
5091 goto out;
5092 }
5093
5094 oe = Add2Ptr(oatbl, t16);
5095
5096 if (oe->next != RESTART_ENTRY_ALLOCATED_LE) {
5097 err = -EINVAL;
5098 goto out;
5099 }
5100
5101 oa = oe->ptr;
5102
5103 if (!oa) {
5104 err = -EINVAL;
5105 goto out;
5106 }
5107 attr = oa->attr;
5108
5109 vcn = le64_to_cpu(lrh->target_vcn);
5110
5111 if (!run_lookup_entry(oa->run1, vcn, &lcn, NULL, NULL) ||
5112 lcn == SPARSE_LCN) {
5113 goto read_next_log_do_action;
5114 }
5115
5116 /* Point to the Redo data and get its length. */
5117 data = Add2Ptr(lrh, le16_to_cpu(lrh->redo_off));
5118 dlen = le16_to_cpu(lrh->redo_len);
5119
5120 /* Shorten length by any Lcns which were deleted. */
5121 saved_len = dlen;
5122
5123 if (!dp_range_ok(le64_to_cpu(lrh->target_vcn) - le64_to_cpu(dp->vcn),
5124 le16_to_cpu(lrh->lcns_follow),
5125 le32_to_cpu(dp->lcns_follow))) {
5126 err = -EINVAL;
5127 goto out;
5128 }
5129
5130 for (i = le16_to_cpu(lrh->lcns_follow); i; i--) {
5131 size_t j;
5132 u32 alen, voff;
5133
5134 voff = le16_to_cpu(lrh->record_off) +
5135 le16_to_cpu(lrh->attr_off);
5136 voff += le16_to_cpu(lrh->cluster_off) << SECTOR_SHIFT;
5137
5138 /* If the Vcn question is allocated, we can just get out. */
5139 j = le64_to_cpu(lrh->target_vcn) - le64_to_cpu(dp->vcn);
5140 if (dp->page_lcns[j + i - 1])
5141 break;
5142
5143 if (!saved_len)
5144 saved_len = 1;
5145
5146 /*
5147 * Calculate the allocated space left relative to the
5148 * log record Vcn, after removing this unallocated Vcn.
5149 */
5150 alen = (i - 1) << sbi->cluster_bits;
5151
5152 /*
5153 * If the update described this log record goes beyond
5154 * the allocated space, then we will have to reduce the length.
5155 */
5156 if (voff >= alen)
5157 dlen = 0;
5158 else if (voff + dlen > alen)
5159 dlen = alen - voff;
5160 }
5161
5162 /*
5163 * If the resulting dlen from above is now zero,
5164 * we can skip this log record.
5165 */
5166 if (!dlen && saved_len)
5167 goto read_next_log_do_action;
5168
5169 t16 = le16_to_cpu(lrh->redo_op);
5170 if (can_skip_action(t16))
5171 goto read_next_log_do_action;
5172
5173 /* Apply the Redo operation a common routine. */
5174 err = do_action(log, oe, lrh, t16, data, dlen, rec_len, &rec_lsn);
5175 if (err)
5176 goto out;
5177
5178 /* Keep reading and looping back until end of file. */
5179 read_next_log_do_action:
5180 err = read_next_log_rec(log, lcb, &rec_lsn);
5181 if (!err && rec_lsn)
5182 goto do_action_next;
5183
5184 lcb_put(lcb);
5185 lcb = NULL;
5186
5187 do_undo_action:
5188 /* Scan Transaction Table. */
5189 tr = NULL;
5190 transaction_table_next:
5191 tr = enum_rstbl(trtbl, tr);
5192 if (!tr)
5193 goto undo_action_done;
5194
5195 if (TransactionActive != tr->transact_state || !tr->undo_next_lsn) {
5196 free_rsttbl_idx(trtbl, PtrOffset(trtbl, tr));
5197 goto transaction_table_next;
5198 }
5199
5200 log->transaction_id = PtrOffset(trtbl, tr);
5201 undo_next_lsn = le64_to_cpu(tr->undo_next_lsn);
5202
5203 /*
5204 * We only have to do anything if the transaction has
5205 * something its undo_next_lsn field.
5206 */
5207 if (!undo_next_lsn)
5208 goto commit_undo;
5209
5210 /* Read the first record to be undone by this transaction. */
5211 err = read_log_rec_lcb(log, undo_next_lsn, lcb_ctx_undo_next, &lcb);
5212 if (err)
5213 goto out;
5214
5215 /*
5216 * Now loop to read all of our log records forwards,
5217 * until we hit the end of the file, cleaning up at the end.
5218 */
5219 undo_action_next:
5220
5221 lrh = lcb->log_rec;
5222 frh = lcb->lrh;
5223 transact_id = le32_to_cpu(frh->transact_id);
5224 rec_len = le32_to_cpu(frh->client_data_len);
5225
5226 if (!check_log_rec(lrh, rec_len, transact_id, bytes_per_attr_entry)) {
5227 err = -EINVAL;
5228 goto out;
5229 }
5230
5231 if (lrh->undo_op == cpu_to_le16(Noop))
5232 goto read_next_log_undo_action;
5233
5234 oe = Add2Ptr(oatbl, le16_to_cpu(lrh->target_attr));
5235 oa = oe->ptr;
5236
5237 t16 = le16_to_cpu(lrh->lcns_follow);
5238 if (!t16)
5239 goto add_allocated_vcns;
5240
5241 is_mapped = run_lookup_entry(oa->run1, le64_to_cpu(lrh->target_vcn),
5242 &lcn, &clen, NULL);
5243
5244 /*
5245 * If the mapping isn't already the table or the mapping
5246 * corresponds to a hole the mapping, we need to make sure
5247 * there is no partial page already memory.
5248 */
5249 if (is_mapped && lcn != SPARSE_LCN && clen >= t16)
5250 goto add_allocated_vcns;
5251
5252 vcn = le64_to_cpu(lrh->target_vcn);
5253 vcn &= ~(u64)(log->clst_per_page - 1);
5254
5255 add_allocated_vcns:
5256 for (i = 0, vcn = le64_to_cpu(lrh->target_vcn),
5257 size = (vcn + 1) << sbi->cluster_bits;
5258 i < t16; i++, vcn += 1, size += sbi->cluster_size) {
5259 attr = oa->attr;
5260 if (!attr->non_res) {
5261 if (size > le32_to_cpu(attr->res.data_size))
5262 attr->res.data_size = cpu_to_le32(size);
5263 } else {
5264 if (size > le64_to_cpu(attr->nres.data_size))
5265 attr->nres.valid_size = attr->nres.data_size =
5266 attr->nres.alloc_size =
5267 cpu_to_le64(size);
5268 }
5269 }
5270
5271 t16 = le16_to_cpu(lrh->undo_op);
5272 if (can_skip_action(t16))
5273 goto read_next_log_undo_action;
5274
5275 /* Point to the Redo data and get its length. */
5276 data = Add2Ptr(lrh, le16_to_cpu(lrh->undo_off));
5277 dlen = le16_to_cpu(lrh->undo_len);
5278
5279 /* It is time to apply the undo action. */
5280 err = do_action(log, oe, lrh, t16, data, dlen, rec_len, NULL);
5281
5282 read_next_log_undo_action:
5283 /*
5284 * Keep reading and looping back until we have read the
5285 * last record for this transaction.
5286 */
5287 err = read_next_log_rec(log, lcb, &rec_lsn);
5288 if (err)
5289 goto out;
5290
5291 if (rec_lsn)
5292 goto undo_action_next;
5293
5294 lcb_put(lcb);
5295 lcb = NULL;
5296
5297 commit_undo:
5298 free_rsttbl_idx(trtbl, log->transaction_id);
5299
5300 log->transaction_id = 0;
5301
5302 goto transaction_table_next;
5303
5304 undo_action_done:
5305
5306 ntfs_update_mftmirr(sbi);
5307
5308 sbi->flags &= ~NTFS_FLAGS_NEED_REPLAY;
5309
5310 end_replay:
5311
5312 err = 0;
5313 if (is_ro)
5314 goto out;
5315
5316 rh = kzalloc(log->page_size, GFP_NOFS);
5317 if (!rh) {
5318 err = -ENOMEM;
5319 goto out;
5320 }
5321
5322 rh->rhdr.sign = NTFS_RSTR_SIGNATURE;
5323 rh->rhdr.fix_off = cpu_to_le16(offsetof(struct RESTART_HDR, fixups));
5324 t16 = (log->page_size >> SECTOR_SHIFT) + 1;
5325 rh->rhdr.fix_num = cpu_to_le16(t16);
5326 rh->sys_page_size = cpu_to_le32(log->page_size);
5327 rh->page_size = cpu_to_le32(log->page_size);
5328
5329 t16 = ALIGN(offsetof(struct RESTART_HDR, fixups) + sizeof(short) * t16,
5330 8);
5331 rh->ra_off = cpu_to_le16(t16);
5332 rh->minor_ver = cpu_to_le16(1); // 0x1A:
5333 rh->major_ver = cpu_to_le16(1); // 0x1C:
5334
5335 ra2 = Add2Ptr(rh, t16);
5336 memcpy(ra2, ra, sizeof(struct RESTART_AREA));
5337
5338 ra2->client_idx[0] = 0;
5339 ra2->client_idx[1] = LFS_NO_CLIENT_LE;
5340 ra2->flags = cpu_to_le16(2);
5341
5342 le32_add_cpu(&ra2->open_log_count, 1);
5343
5344 ntfs_fix_pre_write(&rh->rhdr, log->page_size);
5345
5346 err = ntfs_sb_write_run(sbi, &ni->file.run, 0, rh, log->page_size, 0);
5347 if (!err)
5348 err = ntfs_sb_write_run(sbi, &log->ni->file.run, log->page_size,
5349 rh, log->page_size, 0);
5350
5351 kfree(rh);
5352 if (err)
5353 goto out;
5354
5355 out:
5356 kfree(rst);
5357 if (lcb)
5358 lcb_put(lcb);
5359
5360 /*
5361 * Scan the Open Attribute Table to close all of
5362 * the open attributes.
5363 */
5364 oe = NULL;
5365 while ((oe = enum_rstbl(oatbl, oe))) {
5366 rno = ino_get(&oe->ref);
5367
5368 if (oe->is_attr_name == 1) {
5369 kfree(oe->ptr);
5370 oe->ptr = NULL;
5371 continue;
5372 }
5373
5374 if (oe->is_attr_name)
5375 continue;
5376
5377 oa = oe->ptr;
5378 if (!oa)
5379 continue;
5380
5381 run_close(&oa->run0);
5382 kfree(oa->attr);
5383 if (oa->ni)
5384 iput(&oa->ni->vfs_inode);
5385 kfree(oa);
5386 }
5387
5388 kfree(trtbl);
5389 kfree(oatbl);
5390 kfree(dptbl);
5391 kfree(attr_names);
5392 kfree(log->rst_info.r_page);
5393
5394 kfree(ra);
5395 kfree(log->one_page_buf);
5396
5397 if (err)
5398 sbi->flags |= NTFS_FLAGS_NEED_REPLAY;
5399
5400 if (err == -EROFS)
5401 err = 0;
5402 else if (log->set_dirty)
5403 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
5404
5405 kfree(log);
5406
5407 return err;
5408 }
5409