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