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