1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
8 #include <linux/blkdev.h>
10 #include <linux/random.h>
11 #include <linux/slab.h>
23 #define MaxLogFileSize 0x100000000ull
24 #define DefaultLogPageSize 4096
25 #define MinLogRecordPages 0x30
28 struct NTFS_RECORD_HEADER rhdr; // 'RSTR'
29 __le32 sys_page_size; // 0x10: Page size of the system which initialized the log.
30 __le32 page_size; // 0x14: Log page size used for this log file.
31 __le16 ra_off; // 0x18:
32 __le16 minor_ver; // 0x1A:
33 __le16 major_ver; // 0x1C:
37 #define LFS_NO_CLIENT 0xffff
38 #define LFS_NO_CLIENT_LE cpu_to_le16(0xffff)
42 __le64 restart_lsn; // 0x08:
43 __le16 prev_client; // 0x10:
44 __le16 next_client; // 0x12:
45 __le16 seq_num; // 0x14:
47 __le32 name_bytes; // 0x1C: In bytes.
48 __le16 name[32]; // 0x20: Name of client.
51 static_assert(sizeof(struct CLIENT_REC) == 0x60);
53 /* Two copies of these will exist at the beginning of the log file */
55 __le64 current_lsn; // 0x00: Current logical end of log file.
56 __le16 log_clients; // 0x08: Maximum number of clients.
57 __le16 client_idx[2]; // 0x0A: Free/use index into the client record arrays.
58 __le16 flags; // 0x0E: See RESTART_SINGLE_PAGE_IO.
59 __le32 seq_num_bits; // 0x10: The number of bits in sequence number.
60 __le16 ra_len; // 0x14:
61 __le16 client_off; // 0x16:
62 __le64 l_size; // 0x18: Usable log file size.
63 __le32 last_lsn_data_len; // 0x20:
64 __le16 rec_hdr_len; // 0x24: Log page data offset.
65 __le16 data_off; // 0x26: Log page data length.
66 __le32 open_log_count; // 0x28:
67 __le32 align[5]; // 0x2C:
68 struct CLIENT_REC clients[]; // 0x40:
72 __le16 redo_op; // 0x00: NTFS_LOG_OPERATION
73 __le16 undo_op; // 0x02: NTFS_LOG_OPERATION
74 __le16 redo_off; // 0x04: Offset to Redo record.
75 __le16 redo_len; // 0x06: Redo length.
76 __le16 undo_off; // 0x08: Offset to Undo record.
77 __le16 undo_len; // 0x0A: Undo length.
78 __le16 target_attr; // 0x0C:
79 __le16 lcns_follow; // 0x0E:
80 __le16 record_off; // 0x10:
81 __le16 attr_off; // 0x12:
82 __le16 cluster_off; // 0x14:
83 __le16 reserved; // 0x16:
84 __le64 target_vcn; // 0x18:
85 __le64 page_lcns[]; // 0x20:
88 static_assert(sizeof(struct LOG_REC_HDR) == 0x20);
90 #define RESTART_ENTRY_ALLOCATED 0xFFFFFFFF
91 #define RESTART_ENTRY_ALLOCATED_LE cpu_to_le32(0xFFFFFFFF)
93 struct RESTART_TABLE {
94 __le16 size; // 0x00: In bytes
95 __le16 used; // 0x02: Entries
96 __le16 total; // 0x04: Entries
97 __le16 res[3]; // 0x06:
98 __le32 free_goal; // 0x0C:
99 __le32 first_free; // 0x10:
100 __le32 last_free; // 0x14:
104 static_assert(sizeof(struct RESTART_TABLE) == 0x18);
106 struct ATTR_NAME_ENTRY {
107 __le16 off; // Offset in the Open attribute Table.
112 struct OPEN_ATTR_ENRTY {
113 __le32 next; // 0x00: RESTART_ENTRY_ALLOCATED if allocated
114 __le32 bytes_per_index; // 0x04:
115 enum ATTR_TYPE type; // 0x08:
116 u8 is_dirty_pages; // 0x0C:
117 u8 is_attr_name; // 0x0B: Faked field to manage 'ptr'
118 u8 name_len; // 0x0C: Faked field to manage 'ptr'
120 struct MFT_REF ref; // 0x10: File Reference of file containing attribute
121 __le64 open_record_lsn; // 0x18:
125 /* 32 bit version of 'struct OPEN_ATTR_ENRTY' */
126 struct OPEN_ATTR_ENRTY_32 {
127 __le32 next; // 0x00: RESTART_ENTRY_ALLOCATED if allocated
129 struct MFT_REF ref; // 0x08:
130 __le64 open_record_lsn; // 0x10:
131 u8 is_dirty_pages; // 0x18:
132 u8 is_attr_name; // 0x19:
134 enum ATTR_TYPE type; // 0x1C:
135 u8 name_len; // 0x20: In wchar
137 __le32 AttributeName; // 0x24:
138 __le32 bytes_per_index; // 0x28:
141 #define SIZEOF_OPENATTRIBUTEENTRY0 0x2c
142 // static_assert( 0x2C == sizeof(struct OPEN_ATTR_ENRTY_32) );
143 static_assert(sizeof(struct OPEN_ATTR_ENRTY) < SIZEOF_OPENATTRIBUTEENTRY0);
146 * One entry exists in the Dirty Pages Table for each page which is dirty at
147 * the time the Restart Area is written.
149 struct DIR_PAGE_ENTRY {
150 __le32 next; // 0x00: RESTART_ENTRY_ALLOCATED if allocated
151 __le32 target_attr; // 0x04: Index into the Open attribute Table
152 __le32 transfer_len; // 0x08:
153 __le32 lcns_follow; // 0x0C:
154 __le64 vcn; // 0x10: Vcn of dirty page
155 __le64 oldest_lsn; // 0x18:
156 __le64 page_lcns[]; // 0x20:
159 static_assert(sizeof(struct DIR_PAGE_ENTRY) == 0x20);
161 /* 32 bit version of 'struct DIR_PAGE_ENTRY' */
162 struct DIR_PAGE_ENTRY_32 {
163 __le32 next; // 0x00: RESTART_ENTRY_ALLOCATED if allocated
164 __le32 target_attr; // 0x04: Index into the Open attribute Table
165 __le32 transfer_len; // 0x08:
166 __le32 lcns_follow; // 0x0C:
167 __le32 reserved; // 0x10:
168 __le32 vcn_low; // 0x14: Vcn of dirty page
169 __le32 vcn_hi; // 0x18: Vcn of dirty page
170 __le32 oldest_lsn_low; // 0x1C:
171 __le32 oldest_lsn_hi; // 0x1C:
172 __le32 page_lcns_low; // 0x24:
173 __le32 page_lcns_hi; // 0x24:
176 static_assert(offsetof(struct DIR_PAGE_ENTRY_32, vcn_low) == 0x14);
177 static_assert(sizeof(struct DIR_PAGE_ENTRY_32) == 0x2c);
179 enum transact_state {
180 TransactionUninitialized = 0,
186 struct TRANSACTION_ENTRY {
187 __le32 next; // 0x00: RESTART_ENTRY_ALLOCATED if allocated
188 u8 transact_state; // 0x04:
189 u8 reserved[3]; // 0x05:
190 __le64 first_lsn; // 0x08:
191 __le64 prev_lsn; // 0x10:
192 __le64 undo_next_lsn; // 0x18:
193 __le32 undo_records; // 0x20: Number of undo log records pending abort
194 __le32 undo_len; // 0x24: Total undo size
197 static_assert(sizeof(struct TRANSACTION_ENTRY) == 0x28);
199 struct NTFS_RESTART {
200 __le32 major_ver; // 0x00:
201 __le32 minor_ver; // 0x04:
202 __le64 check_point_start; // 0x08:
203 __le64 open_attr_table_lsn; // 0x10:
204 __le64 attr_names_lsn; // 0x18:
205 __le64 dirty_pages_table_lsn; // 0x20:
206 __le64 transact_table_lsn; // 0x28:
207 __le32 open_attr_len; // 0x30: In bytes
208 __le32 attr_names_len; // 0x34: In bytes
209 __le32 dirty_pages_len; // 0x38: In bytes
210 __le32 transact_table_len; // 0x3C: In bytes
213 static_assert(sizeof(struct NTFS_RESTART) == 0x40);
215 struct NEW_ATTRIBUTE_SIZES {
222 struct BITMAP_RANGE {
232 /* The following type defines the different log record types. */
233 #define LfsClientRecord cpu_to_le32(1)
234 #define LfsClientRestart cpu_to_le32(2)
236 /* This is used to uniquely identify a client for a particular log file. */
242 /* This is the header that begins every Log Record in the log file. */
243 struct LFS_RECORD_HDR {
244 __le64 this_lsn; // 0x00:
245 __le64 client_prev_lsn; // 0x08:
246 __le64 client_undo_next_lsn; // 0x10:
247 __le32 client_data_len; // 0x18:
248 struct CLIENT_ID client; // 0x1C: Owner of this log record.
249 __le32 record_type; // 0x20: LfsClientRecord or LfsClientRestart.
250 __le32 transact_id; // 0x24:
251 __le16 flags; // 0x28: LOG_RECORD_MULTI_PAGE
252 u8 align[6]; // 0x2A:
255 #define LOG_RECORD_MULTI_PAGE cpu_to_le16(1)
257 static_assert(sizeof(struct LFS_RECORD_HDR) == 0x30);
260 __le16 next_record_off; // 0x00: Offset of the free space in the page,
261 u8 align[6]; // 0x02:
262 __le64 last_end_lsn; // 0x08: lsn for the last log record which ends on the page,
265 static_assert(sizeof(struct LFS_RECORD) == 0x10);
267 struct RECORD_PAGE_HDR {
268 struct NTFS_RECORD_HEADER rhdr; // 'RCRD'
269 __le32 rflags; // 0x10: See LOG_PAGE_LOG_RECORD_END
270 __le16 page_count; // 0x14:
271 __le16 page_pos; // 0x16:
272 struct LFS_RECORD record_hdr; // 0x18:
273 __le16 fixups[10]; // 0x28:
274 __le32 file_off; // 0x3c: Used when major version >= 2
279 // Page contains the end of a log record.
280 #define LOG_PAGE_LOG_RECORD_END cpu_to_le32(0x00000001)
282 static inline bool is_log_record_end(const struct RECORD_PAGE_HDR *hdr)
284 return hdr->rflags & LOG_PAGE_LOG_RECORD_END;
287 static_assert(offsetof(struct RECORD_PAGE_HDR, file_off) == 0x3c);
290 * END of NTFS LOG structures
293 /* Define some tuning parameters to keep the restart tables a reasonable size. */
294 #define INITIAL_NUMBER_TRANSACTIONS 5
296 enum NTFS_LOG_OPERATION {
299 CompensationLogRecord = 0x01,
300 InitializeFileRecordSegment = 0x02,
301 DeallocateFileRecordSegment = 0x03,
302 WriteEndOfFileRecordSegment = 0x04,
303 CreateAttribute = 0x05,
304 DeleteAttribute = 0x06,
305 UpdateResidentValue = 0x07,
306 UpdateNonresidentValue = 0x08,
307 UpdateMappingPairs = 0x09,
308 DeleteDirtyClusters = 0x0A,
309 SetNewAttributeSizes = 0x0B,
310 AddIndexEntryRoot = 0x0C,
311 DeleteIndexEntryRoot = 0x0D,
312 AddIndexEntryAllocation = 0x0E,
313 DeleteIndexEntryAllocation = 0x0F,
314 WriteEndOfIndexBuffer = 0x10,
315 SetIndexEntryVcnRoot = 0x11,
316 SetIndexEntryVcnAllocation = 0x12,
317 UpdateFileNameRoot = 0x13,
318 UpdateFileNameAllocation = 0x14,
319 SetBitsInNonresidentBitMap = 0x15,
320 ClearBitsInNonresidentBitMap = 0x16,
322 EndTopLevelAction = 0x18,
323 PrepareTransaction = 0x19,
324 CommitTransaction = 0x1A,
325 ForgetTransaction = 0x1B,
326 OpenNonresidentAttribute = 0x1C,
327 OpenAttributeTableDump = 0x1D,
328 AttributeNamesDump = 0x1E,
329 DirtyPageTableDump = 0x1F,
330 TransactionTableDump = 0x20,
331 UpdateRecordDataRoot = 0x21,
332 UpdateRecordDataAllocation = 0x22,
334 UpdateRelativeDataInIndex =
335 0x23, // NtOfsRestartUpdateRelativeDataInIndex
336 UpdateRelativeDataInIndex2 = 0x24,
337 ZeroEndOfFileRecord = 0x25,
341 * Array for log records which require a target attribute.
342 * A true indicates that the corresponding restart operation
343 * requires a target attribute.
345 static const u8 AttributeRequired[] = {
346 0xFC, 0xFB, 0xFF, 0x10, 0x06,
349 static inline bool is_target_required(u16 op)
351 bool ret = op <= UpdateRecordDataAllocation &&
352 (AttributeRequired[op >> 3] >> (op & 7) & 1);
356 static inline bool can_skip_action(enum NTFS_LOG_OPERATION op)
360 case DeleteDirtyClusters:
362 case EndTopLevelAction:
363 case PrepareTransaction:
364 case CommitTransaction:
365 case ForgetTransaction:
366 case CompensationLogRecord:
367 case OpenNonresidentAttribute:
368 case OpenAttributeTableDump:
369 case AttributeNamesDump:
370 case DirtyPageTableDump:
371 case TransactionTableDump:
378 enum { lcb_ctx_undo_next, lcb_ctx_prev, lcb_ctx_next };
380 /* Bytes per restart table. */
381 static inline u32 bytes_per_rt(const struct RESTART_TABLE *rt)
383 return le16_to_cpu(rt->used) * le16_to_cpu(rt->size) +
384 sizeof(struct RESTART_TABLE);
387 /* Log record length. */
388 static inline u32 lrh_length(const struct LOG_REC_HDR *lr)
390 u16 t16 = le16_to_cpu(lr->lcns_follow);
392 return struct_size(lr, page_lcns, max_t(u16, 1, t16));
396 struct LFS_RECORD_HDR *lrh; // Log record header of the current lsn.
397 struct LOG_REC_HDR *log_rec;
398 u32 ctx_mode; // lcb_ctx_undo_next/lcb_ctx_prev/lcb_ctx_next
399 struct CLIENT_ID client;
400 bool alloc; // If true the we should deallocate 'log_rec'.
403 static void lcb_put(struct lcb *lcb)
411 /* Find the oldest lsn from active clients. */
412 static inline void oldest_client_lsn(const struct CLIENT_REC *ca,
413 __le16 next_client, u64 *oldest_lsn)
415 while (next_client != LFS_NO_CLIENT_LE) {
416 const struct CLIENT_REC *cr = ca + le16_to_cpu(next_client);
417 u64 lsn = le64_to_cpu(cr->oldest_lsn);
419 /* Ignore this block if it's oldest lsn is 0. */
420 if (lsn && lsn < *oldest_lsn)
423 next_client = cr->next_client;
427 static inline bool is_rst_page_hdr_valid(u32 file_off,
428 const struct RESTART_HDR *rhdr)
430 u32 sys_page = le32_to_cpu(rhdr->sys_page_size);
431 u32 page_size = le32_to_cpu(rhdr->page_size);
435 if (sys_page < SECTOR_SIZE || page_size < SECTOR_SIZE ||
436 sys_page & (sys_page - 1) || page_size & (page_size - 1)) {
440 /* Check that if the file offset isn't 0, it is the system page size. */
441 if (file_off && file_off != sys_page)
444 /* Check support version 1.1+. */
445 if (le16_to_cpu(rhdr->major_ver) <= 1 && !rhdr->minor_ver)
448 if (le16_to_cpu(rhdr->major_ver) > 2)
451 ro = le16_to_cpu(rhdr->ra_off);
452 if (!IS_ALIGNED(ro, 8) || ro > sys_page)
455 end_usa = ((sys_page >> SECTOR_SHIFT) + 1) * sizeof(short);
456 end_usa += le16_to_cpu(rhdr->rhdr.fix_off);
464 static inline bool is_rst_area_valid(const struct RESTART_HDR *rhdr)
466 const struct RESTART_AREA *ra;
468 u32 off, l_size, file_dat_bits, file_size_round;
469 u16 ro = le16_to_cpu(rhdr->ra_off);
470 u32 sys_page = le32_to_cpu(rhdr->sys_page_size);
472 if (ro + offsetof(struct RESTART_AREA, l_size) >
473 SECTOR_SIZE - sizeof(short))
476 ra = Add2Ptr(rhdr, ro);
477 cl = le16_to_cpu(ra->log_clients);
482 off = le16_to_cpu(ra->client_off);
484 if (!IS_ALIGNED(off, 8) || ro + off > SECTOR_SIZE - sizeof(short))
487 off += cl * sizeof(struct CLIENT_REC);
493 * Check the restart length field and whether the entire
494 * restart area is contained that length.
496 if (le16_to_cpu(rhdr->ra_off) + le16_to_cpu(ra->ra_len) > sys_page ||
497 off > le16_to_cpu(ra->ra_len)) {
502 * As a final check make sure that the use list and the free list
503 * are either empty or point to a valid client.
505 fl = le16_to_cpu(ra->client_idx[0]);
506 ul = le16_to_cpu(ra->client_idx[1]);
507 if ((fl != LFS_NO_CLIENT && fl >= cl) ||
508 (ul != LFS_NO_CLIENT && ul >= cl))
511 /* Make sure the sequence number bits match the log file size. */
512 l_size = le64_to_cpu(ra->l_size);
514 file_dat_bits = sizeof(u64) * 8 - le32_to_cpu(ra->seq_num_bits);
515 file_size_round = 1u << (file_dat_bits + 3);
516 if (file_size_round != l_size &&
517 (file_size_round < l_size || (file_size_round / 2) > l_size)) {
521 /* The log page data offset and record header length must be quad-aligned. */
522 if (!IS_ALIGNED(le16_to_cpu(ra->data_off), 8) ||
523 !IS_ALIGNED(le16_to_cpu(ra->rec_hdr_len), 8))
529 static inline bool is_client_area_valid(const struct RESTART_HDR *rhdr,
532 u16 ro = le16_to_cpu(rhdr->ra_off);
533 const struct RESTART_AREA *ra = Add2Ptr(rhdr, ro);
534 u16 ra_len = le16_to_cpu(ra->ra_len);
535 const struct CLIENT_REC *ca;
538 if (usa_error && ra_len + ro > SECTOR_SIZE - sizeof(short))
541 /* Find the start of the client array. */
542 ca = Add2Ptr(ra, le16_to_cpu(ra->client_off));
545 * Start with the free list.
546 * Check that all the clients are valid and that there isn't a cycle.
547 * Do the in-use list on the second pass.
549 for (i = 0; i < 2; i++) {
550 u16 client_idx = le16_to_cpu(ra->client_idx[i]);
551 bool first_client = true;
552 u16 clients = le16_to_cpu(ra->log_clients);
554 while (client_idx != LFS_NO_CLIENT) {
555 const struct CLIENT_REC *cr;
558 client_idx >= le16_to_cpu(ra->log_clients))
562 cr = ca + client_idx;
564 client_idx = le16_to_cpu(cr->next_client);
567 first_client = false;
568 if (cr->prev_client != LFS_NO_CLIENT_LE)
580 * Remove a client record from a client record list an restart area.
582 static inline void remove_client(struct CLIENT_REC *ca,
583 const struct CLIENT_REC *cr, __le16 *head)
585 if (cr->prev_client == LFS_NO_CLIENT_LE)
586 *head = cr->next_client;
588 ca[le16_to_cpu(cr->prev_client)].next_client = cr->next_client;
590 if (cr->next_client != LFS_NO_CLIENT_LE)
591 ca[le16_to_cpu(cr->next_client)].prev_client = cr->prev_client;
595 * add_client - Add a client record to the start of a list.
597 static inline void add_client(struct CLIENT_REC *ca, u16 index, __le16 *head)
599 struct CLIENT_REC *cr = ca + index;
601 cr->prev_client = LFS_NO_CLIENT_LE;
602 cr->next_client = *head;
604 if (*head != LFS_NO_CLIENT_LE)
605 ca[le16_to_cpu(*head)].prev_client = cpu_to_le16(index);
607 *head = cpu_to_le16(index);
610 static inline void *enum_rstbl(struct RESTART_TABLE *t, void *c)
614 u16 rsize = t ? le16_to_cpu(t->size) : 0;
619 e = Add2Ptr(t, sizeof(struct RESTART_TABLE));
621 e = Add2Ptr(c, rsize);
624 /* Loop until we hit the first one allocated, or the end of the list. */
625 for (bprt = bytes_per_rt(t); PtrOffset(t, e) < bprt;
626 e = Add2Ptr(e, rsize)) {
627 if (*e == RESTART_ENTRY_ALLOCATED_LE)
634 * find_dp - Search for a @vcn in Dirty Page Table.
636 static inline struct DIR_PAGE_ENTRY *find_dp(struct RESTART_TABLE *dptbl,
637 u32 target_attr, u64 vcn)
639 __le32 ta = cpu_to_le32(target_attr);
640 struct DIR_PAGE_ENTRY *dp = NULL;
642 while ((dp = enum_rstbl(dptbl, dp))) {
643 u64 dp_vcn = le64_to_cpu(dp->vcn);
645 if (dp->target_attr == ta && vcn >= dp_vcn &&
646 vcn < dp_vcn + le32_to_cpu(dp->lcns_follow)) {
653 static inline u32 norm_file_page(u32 page_size, u32 *l_size, bool use_default)
656 page_size = DefaultLogPageSize;
658 /* Round the file size down to a system page boundary. */
659 *l_size &= ~(page_size - 1);
661 /* File should contain at least 2 restart pages and MinLogRecordPages pages. */
662 if (*l_size < (MinLogRecordPages + 2) * page_size)
668 static bool check_log_rec(const struct LOG_REC_HDR *lr, u32 bytes, u32 tr,
669 u32 bytes_per_attr_entry)
673 if (bytes < sizeof(struct LOG_REC_HDR))
678 if ((tr - sizeof(struct RESTART_TABLE)) %
679 sizeof(struct TRANSACTION_ENTRY))
682 if (le16_to_cpu(lr->redo_off) & 7)
685 if (le16_to_cpu(lr->undo_off) & 7)
691 if (is_target_required(le16_to_cpu(lr->redo_op)))
694 if (is_target_required(le16_to_cpu(lr->undo_op)))
698 if (!lr->lcns_follow)
701 t16 = le16_to_cpu(lr->target_attr);
702 if ((t16 - sizeof(struct RESTART_TABLE)) % bytes_per_attr_entry)
706 if (bytes < lrh_length(lr))
712 static bool check_rstbl(const struct RESTART_TABLE *rt, size_t bytes)
716 u16 rsize = le16_to_cpu(rt->size);
717 u16 ne = le16_to_cpu(rt->used);
718 u32 ff = le32_to_cpu(rt->first_free);
719 u32 lf = le32_to_cpu(rt->last_free);
721 ts = rsize * ne + sizeof(struct RESTART_TABLE);
723 if (!rsize || rsize > bytes ||
724 rsize + sizeof(struct RESTART_TABLE) > bytes || bytes < ts ||
725 le16_to_cpu(rt->total) > ne || ff > ts || lf > ts ||
726 (ff && ff < sizeof(struct RESTART_TABLE)) ||
727 (lf && lf < sizeof(struct RESTART_TABLE))) {
732 * Verify each entry is either allocated or points
733 * to a valid offset the table.
735 for (i = 0; i < ne; i++) {
736 off = le32_to_cpu(*(__le32 *)Add2Ptr(
737 rt, i * rsize + sizeof(struct RESTART_TABLE)));
739 if (off != RESTART_ENTRY_ALLOCATED && off &&
740 (off < sizeof(struct RESTART_TABLE) ||
741 ((off - sizeof(struct RESTART_TABLE)) % rsize))) {
747 * Walk through the list headed by the first entry to make
748 * sure none of the entries are currently being used.
750 for (off = ff; off;) {
751 if (off == RESTART_ENTRY_ALLOCATED)
754 off = le32_to_cpu(*(__le32 *)Add2Ptr(rt, off));
761 * free_rsttbl_idx - Free a previously allocated index a Restart Table.
763 static inline void free_rsttbl_idx(struct RESTART_TABLE *rt, u32 off)
766 u32 lf = le32_to_cpu(rt->last_free);
767 __le32 off_le = cpu_to_le32(off);
769 e = Add2Ptr(rt, off);
771 if (off < le32_to_cpu(rt->free_goal)) {
773 rt->first_free = off_le;
775 rt->last_free = off_le;
778 *(__le32 *)Add2Ptr(rt, lf) = off_le;
780 rt->first_free = off_le;
782 rt->last_free = off_le;
786 le16_sub_cpu(&rt->total, 1);
789 static inline struct RESTART_TABLE *init_rsttbl(u16 esize, u16 used)
791 __le32 *e, *last_free;
793 u32 bytes = esize * used + sizeof(struct RESTART_TABLE);
794 u32 lf = sizeof(struct RESTART_TABLE) + (used - 1) * esize;
795 struct RESTART_TABLE *t = kzalloc(bytes, GFP_NOFS);
800 t->size = cpu_to_le16(esize);
801 t->used = cpu_to_le16(used);
802 t->free_goal = cpu_to_le32(~0u);
803 t->first_free = cpu_to_le32(sizeof(struct RESTART_TABLE));
804 t->last_free = cpu_to_le32(lf);
806 e = (__le32 *)(t + 1);
807 last_free = Add2Ptr(t, lf);
809 for (off = sizeof(struct RESTART_TABLE) + esize; e < last_free;
810 e = Add2Ptr(e, esize), off += esize) {
811 *e = cpu_to_le32(off);
816 static inline struct RESTART_TABLE *extend_rsttbl(struct RESTART_TABLE *tbl,
817 u32 add, u32 free_goal)
819 u16 esize = le16_to_cpu(tbl->size);
820 __le32 osize = cpu_to_le32(bytes_per_rt(tbl));
821 u32 used = le16_to_cpu(tbl->used);
822 struct RESTART_TABLE *rt;
824 rt = init_rsttbl(esize, used + add);
828 memcpy(rt + 1, tbl + 1, esize * used);
830 rt->free_goal = free_goal == ~0u ?
832 cpu_to_le32(sizeof(struct RESTART_TABLE) +
835 if (tbl->first_free) {
836 rt->first_free = tbl->first_free;
837 *(__le32 *)Add2Ptr(rt, le32_to_cpu(tbl->last_free)) = osize;
839 rt->first_free = osize;
842 rt->total = tbl->total;
851 * Allocate an index from within a previously initialized Restart Table.
853 static inline void *alloc_rsttbl_idx(struct RESTART_TABLE **tbl)
857 struct RESTART_TABLE *t = *tbl;
859 if (!t->first_free) {
860 *tbl = t = extend_rsttbl(t, 16, ~0u);
865 off = le32_to_cpu(t->first_free);
867 /* Dequeue this entry and zero it. */
872 memset(e, 0, le16_to_cpu(t->size));
874 *e = RESTART_ENTRY_ALLOCATED_LE;
876 /* If list is going empty, then we fix the last_free as well. */
880 le16_add_cpu(&t->total, 1);
882 return Add2Ptr(t, off);
886 * alloc_rsttbl_from_idx
888 * Allocate a specific index from within a previously initialized Restart Table.
890 static inline void *alloc_rsttbl_from_idx(struct RESTART_TABLE **tbl, u32 vbo)
894 struct RESTART_TABLE *rt = *tbl;
895 u32 bytes = bytes_per_rt(rt);
896 u16 esize = le16_to_cpu(rt->size);
898 /* If the entry is not the table, we will have to extend the table. */
901 * Extend the size by computing the number of entries between
902 * the existing size and the desired index and adding 1 to that.
904 u32 bytes2idx = vbo - bytes;
907 * There should always be an integral number of entries
908 * being added. Now extend the table.
910 *tbl = rt = extend_rsttbl(rt, bytes2idx / esize + 1, bytes);
915 /* See if the entry is already allocated, and just return if it is. */
916 e = Add2Ptr(rt, vbo);
918 if (*e == RESTART_ENTRY_ALLOCATED_LE)
922 * Walk through the table, looking for the entry we're
923 * interested and the previous entry.
925 off = le32_to_cpu(rt->first_free);
926 e = Add2Ptr(rt, off);
929 /* this is a match */
935 * Need to walk through the list looking for the predecessor
939 /* Remember the entry just found */
943 /* Should never run of entries. */
945 /* Lookup up the next entry the list. */
946 off = le32_to_cpu(*last_e);
947 e = Add2Ptr(rt, off);
949 /* If this is our match we are done. */
954 * If this was the last entry, we update that
957 if (le32_to_cpu(rt->last_free) == off)
958 rt->last_free = cpu_to_le32(last_off);
964 /* If the list is now empty, we fix the last_free as well. */
968 /* Zero this entry. */
970 *e = RESTART_ENTRY_ALLOCATED_LE;
972 le16_add_cpu(&rt->total, 1);
977 #define RESTART_SINGLE_PAGE_IO cpu_to_le16(0x0001)
979 #define NTFSLOG_WRAPPED 0x00000001
980 #define NTFSLOG_MULTIPLE_PAGE_IO 0x00000002
981 #define NTFSLOG_NO_LAST_LSN 0x00000004
982 #define NTFSLOG_REUSE_TAIL 0x00000010
983 #define NTFSLOG_NO_OLDEST_LSN 0x00000020
985 /* Helper struct to work with NTFS $LogFile. */
987 struct ntfs_inode *ni;
993 u32 page_mask; // page_size - 1
995 struct RECORD_PAGE_HDR *one_page_buf;
997 struct RESTART_TABLE *open_attr_tbl;
1007 u16 record_header_len;
1011 u32 seq_num_mask; /* (1 << file_data_bits) - 1 */
1013 struct RESTART_AREA *ra; /* In-memory image of the next restart area. */
1014 u32 ra_size; /* The usable size of the restart area. */
1017 * If true, then the in-memory restart area is to be written
1018 * to the first position on the disk.
1021 bool set_dirty; /* True if we need to set dirty flag. */
1029 u32 total_avail_pages;
1030 u32 total_undo_commit;
1031 u32 max_current_avail;
1038 u32 l_flags; /* See NTFSLOG_XXX */
1039 u32 current_openlog_count; /* On-disk value for open_log_count. */
1041 struct CLIENT_ID client_id;
1042 u32 client_undo_commit;
1045 static inline u32 lsn_to_vbo(struct ntfs_log *log, const u64 lsn)
1047 u32 vbo = (lsn << log->seq_num_bits) >> (log->seq_num_bits - 3);
1052 /* Compute the offset in the log file of the next log page. */
1053 static inline u32 next_page_off(struct ntfs_log *log, u32 off)
1055 off = (off & ~log->sys_page_mask) + log->page_size;
1056 return off >= log->l_size ? log->first_page : off;
1059 static inline u32 lsn_to_page_off(struct ntfs_log *log, u64 lsn)
1061 return (((u32)lsn) << 3) & log->page_mask;
1064 static inline u64 vbo_to_lsn(struct ntfs_log *log, u32 off, u64 Seq)
1066 return (off >> 3) + (Seq << log->file_data_bits);
1069 static inline bool is_lsn_in_file(struct ntfs_log *log, u64 lsn)
1071 return lsn >= log->oldest_lsn &&
1072 lsn <= le64_to_cpu(log->ra->current_lsn);
1075 static inline u32 hdr_file_off(struct ntfs_log *log,
1076 struct RECORD_PAGE_HDR *hdr)
1078 if (log->major_ver < 2)
1079 return le64_to_cpu(hdr->rhdr.lsn);
1081 return le32_to_cpu(hdr->file_off);
1084 static inline u64 base_lsn(struct ntfs_log *log,
1085 const struct RECORD_PAGE_HDR *hdr, u64 lsn)
1087 u64 h_lsn = le64_to_cpu(hdr->rhdr.lsn);
1088 u64 ret = (((h_lsn >> log->file_data_bits) +
1089 (lsn < (lsn_to_vbo(log, h_lsn) & ~log->page_mask) ? 1 : 0))
1090 << log->file_data_bits) +
1091 ((((is_log_record_end(hdr) &&
1092 h_lsn <= le64_to_cpu(hdr->record_hdr.last_end_lsn)) ?
1093 le16_to_cpu(hdr->record_hdr.next_record_off) :
1101 static inline bool verify_client_lsn(struct ntfs_log *log,
1102 const struct CLIENT_REC *client, u64 lsn)
1104 return lsn >= le64_to_cpu(client->oldest_lsn) &&
1105 lsn <= le64_to_cpu(log->ra->current_lsn) && lsn;
1108 struct restart_info {
1110 struct RESTART_HDR *r_page;
1112 bool chkdsk_was_run;
1118 static int read_log_page(struct ntfs_log *log, u32 vbo,
1119 struct RECORD_PAGE_HDR **buffer, bool *usa_error)
1122 u32 page_idx = vbo >> log->page_bits;
1123 u32 page_off = vbo & log->page_mask;
1124 u32 bytes = log->page_size - page_off;
1125 void *to_free = NULL;
1126 u32 page_vbo = page_idx << log->page_bits;
1127 struct RECORD_PAGE_HDR *page_buf;
1128 struct ntfs_inode *ni = log->ni;
1131 if (vbo >= log->l_size)
1135 to_free = kmalloc(log->page_size, GFP_NOFS);
1141 page_buf = page_off ? log->one_page_buf : *buffer;
1143 err = ntfs_read_run_nb(ni->mi.sbi, &ni->file.run, page_vbo, page_buf,
1144 log->page_size, NULL);
1148 if (page_buf->rhdr.sign != NTFS_FFFF_SIGNATURE)
1149 ntfs_fix_post_read(&page_buf->rhdr, PAGE_SIZE, false);
1151 if (page_buf != *buffer)
1152 memcpy(*buffer, Add2Ptr(page_buf, page_off), bytes);
1154 bBAAD = page_buf->rhdr.sign == NTFS_BAAD_SIGNATURE;
1158 /* Check that the update sequence array for this page is valid */
1159 /* If we don't allow errors, raise an error status */
1164 if (err && to_free) {
1175 * It walks through 512 blocks of the file looking for a valid
1176 * restart page header. It will stop the first time we find a
1177 * valid page header.
1179 static int log_read_rst(struct ntfs_log *log, u32 l_size, bool first,
1180 struct restart_info *info)
1183 struct RESTART_HDR *r_page = NULL;
1185 /* Determine which restart area we are looking for. */
1194 /* Loop continuously until we succeed. */
1195 for (; vbo < l_size; vbo = 2 * vbo + skip, skip = 0) {
1198 struct RESTART_AREA *ra;
1200 /* Read a page header at the current offset. */
1201 if (read_log_page(log, vbo, (struct RECORD_PAGE_HDR **)&r_page,
1203 /* Ignore any errors. */
1207 /* Exit if the signature is a log record page. */
1208 if (r_page->rhdr.sign == NTFS_RCRD_SIGNATURE) {
1209 info->initialized = true;
1213 brst = r_page->rhdr.sign == NTFS_RSTR_SIGNATURE;
1214 bchk = r_page->rhdr.sign == NTFS_CHKD_SIGNATURE;
1216 if (!bchk && !brst) {
1217 if (r_page->rhdr.sign != NTFS_FFFF_SIGNATURE) {
1219 * Remember if the signature does not
1220 * indicate uninitialized file.
1222 info->initialized = true;
1228 info->valid_page = false;
1229 info->initialized = true;
1232 /* Let's check the restart area if this is a valid page. */
1233 if (!is_rst_page_hdr_valid(vbo, r_page))
1235 ra = Add2Ptr(r_page, le16_to_cpu(r_page->ra_off));
1237 if (!is_rst_area_valid(r_page))
1241 * We have a valid restart page header and restart area.
1242 * If chkdsk was run or we have no clients then we have
1243 * no more checking to do.
1245 if (bchk || ra->client_idx[1] == LFS_NO_CLIENT_LE) {
1246 info->valid_page = true;
1250 if (is_client_area_valid(r_page, usa_error)) {
1251 info->valid_page = true;
1252 ra = Add2Ptr(r_page, le16_to_cpu(r_page->ra_off));
1257 * If chkdsk was run then update the caller's
1258 * values and return.
1260 if (r_page->rhdr.sign == NTFS_CHKD_SIGNATURE) {
1261 info->chkdsk_was_run = true;
1262 info->last_lsn = le64_to_cpu(r_page->rhdr.lsn);
1263 info->restart = true;
1264 info->r_page = r_page;
1269 * If we have a valid page then copy the values
1272 if (info->valid_page) {
1273 info->last_lsn = le64_to_cpu(ra->current_lsn);
1274 info->restart = true;
1275 info->r_page = r_page;
1286 * Ilog_init_pg_hdr - Init @log from restart page header.
1288 static void log_init_pg_hdr(struct ntfs_log *log, u32 sys_page_size,
1289 u32 page_size, u16 major_ver, u16 minor_ver)
1291 log->sys_page_size = sys_page_size;
1292 log->sys_page_mask = sys_page_size - 1;
1293 log->page_size = page_size;
1294 log->page_mask = page_size - 1;
1295 log->page_bits = blksize_bits(page_size);
1297 log->clst_per_page = log->page_size >> log->ni->mi.sbi->cluster_bits;
1298 if (!log->clst_per_page)
1299 log->clst_per_page = 1;
1301 log->first_page = major_ver >= 2 ?
1303 ((sys_page_size << 1) + (page_size << 1));
1304 log->major_ver = major_ver;
1305 log->minor_ver = minor_ver;
1309 * log_create - Init @log in cases when we don't have a restart area to use.
1311 static void log_create(struct ntfs_log *log, u32 l_size, const u64 last_lsn,
1312 u32 open_log_count, bool wrapped, bool use_multi_page)
1314 log->l_size = l_size;
1315 /* All file offsets must be quadword aligned. */
1316 log->file_data_bits = blksize_bits(l_size) - 3;
1317 log->seq_num_mask = (8 << log->file_data_bits) - 1;
1318 log->seq_num_bits = sizeof(u64) * 8 - log->file_data_bits;
1319 log->seq_num = (last_lsn >> log->file_data_bits) + 2;
1320 log->next_page = log->first_page;
1321 log->oldest_lsn = log->seq_num << log->file_data_bits;
1322 log->oldest_lsn_off = 0;
1323 log->last_lsn = log->oldest_lsn;
1325 log->l_flags |= NTFSLOG_NO_LAST_LSN | NTFSLOG_NO_OLDEST_LSN;
1327 /* Set the correct flags for the I/O and indicate if we have wrapped. */
1329 log->l_flags |= NTFSLOG_WRAPPED;
1332 log->l_flags |= NTFSLOG_MULTIPLE_PAGE_IO;
1334 /* Compute the log page values. */
1335 log->data_off = ALIGN(
1336 offsetof(struct RECORD_PAGE_HDR, fixups) +
1337 sizeof(short) * ((log->page_size >> SECTOR_SHIFT) + 1),
1339 log->data_size = log->page_size - log->data_off;
1340 log->record_header_len = sizeof(struct LFS_RECORD_HDR);
1342 /* Remember the different page sizes for reservation. */
1343 log->reserved = log->data_size - log->record_header_len;
1345 /* Compute the restart page values. */
1346 log->ra_off = ALIGN(
1347 offsetof(struct RESTART_HDR, fixups) +
1349 ((log->sys_page_size >> SECTOR_SHIFT) + 1),
1351 log->restart_size = log->sys_page_size - log->ra_off;
1352 log->ra_size = struct_size(log->ra, clients, 1);
1353 log->current_openlog_count = open_log_count;
1356 * The total available log file space is the number of
1357 * log file pages times the space available on each page.
1359 log->total_avail_pages = log->l_size - log->first_page;
1360 log->total_avail = log->total_avail_pages >> log->page_bits;
1363 * We assume that we can't use the end of the page less than
1364 * the file record size.
1365 * Then we won't need to reserve more than the caller asks for.
1367 log->max_current_avail = log->total_avail * log->reserved;
1368 log->total_avail = log->total_avail * log->data_size;
1369 log->current_avail = log->max_current_avail;
1373 * log_create_ra - Fill a restart area from the values stored in @log.
1375 static struct RESTART_AREA *log_create_ra(struct ntfs_log *log)
1377 struct CLIENT_REC *cr;
1378 struct RESTART_AREA *ra = kzalloc(log->restart_size, GFP_NOFS);
1383 ra->current_lsn = cpu_to_le64(log->last_lsn);
1384 ra->log_clients = cpu_to_le16(1);
1385 ra->client_idx[1] = LFS_NO_CLIENT_LE;
1386 if (log->l_flags & NTFSLOG_MULTIPLE_PAGE_IO)
1387 ra->flags = RESTART_SINGLE_PAGE_IO;
1388 ra->seq_num_bits = cpu_to_le32(log->seq_num_bits);
1389 ra->ra_len = cpu_to_le16(log->ra_size);
1390 ra->client_off = cpu_to_le16(offsetof(struct RESTART_AREA, clients));
1391 ra->l_size = cpu_to_le64(log->l_size);
1392 ra->rec_hdr_len = cpu_to_le16(log->record_header_len);
1393 ra->data_off = cpu_to_le16(log->data_off);
1394 ra->open_log_count = cpu_to_le32(log->current_openlog_count + 1);
1398 cr->prev_client = LFS_NO_CLIENT_LE;
1399 cr->next_client = LFS_NO_CLIENT_LE;
1404 static u32 final_log_off(struct ntfs_log *log, u64 lsn, u32 data_len)
1406 u32 base_vbo = lsn << 3;
1407 u32 final_log_off = (base_vbo & log->seq_num_mask) & ~log->page_mask;
1408 u32 page_off = base_vbo & log->page_mask;
1409 u32 tail = log->page_size - page_off;
1413 /* Add the length of the header. */
1414 data_len += log->record_header_len;
1417 * If this lsn is contained this log page we are done.
1418 * Otherwise we need to walk through several log pages.
1420 if (data_len > tail) {
1422 tail = log->data_size;
1423 page_off = log->data_off - 1;
1426 final_log_off = next_page_off(log, final_log_off);
1429 * We are done if the remaining bytes
1432 if (data_len <= tail)
1439 * We add the remaining bytes to our starting position on this page
1440 * and then add that value to the file offset of this log page.
1442 return final_log_off + data_len + page_off;
1445 static int next_log_lsn(struct ntfs_log *log, const struct LFS_RECORD_HDR *rh,
1449 u64 this_lsn = le64_to_cpu(rh->this_lsn);
1450 u32 vbo = lsn_to_vbo(log, this_lsn);
1452 final_log_off(log, this_lsn, le32_to_cpu(rh->client_data_len));
1453 u32 hdr_off = end & ~log->sys_page_mask;
1454 u64 seq = this_lsn >> log->file_data_bits;
1455 struct RECORD_PAGE_HDR *page = NULL;
1457 /* Remember if we wrapped. */
1461 /* Log page header for this page. */
1462 err = read_log_page(log, hdr_off, &page, NULL);
1467 * If the lsn we were given was not the last lsn on this page,
1468 * then the starting offset for the next lsn is on a quad word
1469 * boundary following the last file offset for the current lsn.
1470 * Otherwise the file offset is the start of the data on the next page.
1472 if (this_lsn == le64_to_cpu(page->rhdr.lsn)) {
1473 /* If we wrapped, we need to increment the sequence number. */
1474 hdr_off = next_page_off(log, hdr_off);
1475 if (hdr_off == log->first_page)
1478 vbo = hdr_off + log->data_off;
1480 vbo = ALIGN(end, 8);
1483 /* Compute the lsn based on the file offset and the sequence count. */
1484 *lsn = vbo_to_lsn(log, vbo, seq);
1487 * If this lsn is within the legal range for the file, we return true.
1488 * Otherwise false indicates that there are no more lsn's.
1490 if (!is_lsn_in_file(log, *lsn))
1499 * current_log_avail - Calculate the number of bytes available for log records.
1501 static u32 current_log_avail(struct ntfs_log *log)
1503 u32 oldest_off, next_free_off, free_bytes;
1505 if (log->l_flags & NTFSLOG_NO_LAST_LSN) {
1506 /* The entire file is available. */
1507 return log->max_current_avail;
1511 * If there is a last lsn the restart area then we know that we will
1512 * have to compute the free range.
1513 * If there is no oldest lsn then start at the first page of the file.
1515 oldest_off = (log->l_flags & NTFSLOG_NO_OLDEST_LSN) ?
1517 (log->oldest_lsn_off & ~log->sys_page_mask);
1520 * We will use the next log page offset to compute the next free page.
1521 * If we are going to reuse this page go to the next page.
1522 * If we are at the first page then use the end of the file.
1524 next_free_off = (log->l_flags & NTFSLOG_REUSE_TAIL) ?
1525 log->next_page + log->page_size :
1526 log->next_page == log->first_page ? log->l_size :
1529 /* If the two offsets are the same then there is no available space. */
1530 if (oldest_off == next_free_off)
1533 * If the free offset follows the oldest offset then subtract
1534 * this range from the total available pages.
1537 oldest_off < next_free_off ?
1538 log->total_avail_pages - (next_free_off - oldest_off) :
1539 oldest_off - next_free_off;
1541 free_bytes >>= log->page_bits;
1542 return free_bytes * log->reserved;
1545 static bool check_subseq_log_page(struct ntfs_log *log,
1546 const struct RECORD_PAGE_HDR *rp, u32 vbo,
1550 const struct NTFS_RECORD_HEADER *rhdr = &rp->rhdr;
1551 u64 lsn = le64_to_cpu(rhdr->lsn);
1553 if (rhdr->sign == NTFS_FFFF_SIGNATURE || !rhdr->sign)
1557 * If the last lsn on the page occurs was written after the page
1558 * that caused the original error then we have a fatal error.
1560 lsn_seq = lsn >> log->file_data_bits;
1563 * If the sequence number for the lsn the page is equal or greater
1564 * than lsn we expect, then this is a subsequent write.
1566 return lsn_seq >= seq ||
1567 (lsn_seq == seq - 1 && log->first_page == vbo &&
1568 vbo != (lsn_to_vbo(log, lsn) & ~log->page_mask));
1574 * Walks through the log pages for a file, searching for the
1575 * last log page written to the file.
1577 static int last_log_lsn(struct ntfs_log *log)
1580 bool usa_error = false;
1581 bool replace_page = false;
1582 bool reuse_page = log->l_flags & NTFSLOG_REUSE_TAIL;
1583 bool wrapped_file, wrapped;
1585 u32 page_cnt = 1, page_pos = 1;
1586 u32 page_off = 0, page_off1 = 0, saved_off = 0;
1587 u32 final_off, second_off, final_off_prev = 0, second_off_prev = 0;
1588 u32 first_file_off = 0, second_file_off = 0;
1589 u32 part_io_count = 0;
1591 u32 this_off, curpage_off, nextpage_off, remain_pages;
1593 u64 expected_seq, seq_base = 0, lsn_base = 0;
1594 u64 best_lsn, best_lsn1, best_lsn2;
1595 u64 lsn_cur, lsn1, lsn2;
1596 u64 last_ok_lsn = reuse_page ? log->last_lsn : 0;
1598 u16 cur_pos, best_page_pos;
1600 struct RECORD_PAGE_HDR *page = NULL;
1601 struct RECORD_PAGE_HDR *tst_page = NULL;
1602 struct RECORD_PAGE_HDR *first_tail = NULL;
1603 struct RECORD_PAGE_HDR *second_tail = NULL;
1604 struct RECORD_PAGE_HDR *tail_page = NULL;
1605 struct RECORD_PAGE_HDR *second_tail_prev = NULL;
1606 struct RECORD_PAGE_HDR *first_tail_prev = NULL;
1607 struct RECORD_PAGE_HDR *page_bufs = NULL;
1608 struct RECORD_PAGE_HDR *best_page;
1610 if (log->major_ver >= 2) {
1611 final_off = 0x02 * log->page_size;
1612 second_off = 0x12 * log->page_size;
1614 // 0x10 == 0x12 - 0x2
1615 page_bufs = kmalloc(log->page_size * 0x10, GFP_NOFS);
1619 second_off = log->first_page - log->page_size;
1620 final_off = second_off - log->page_size;
1624 /* Read second tail page (at pos 3/0x12000). */
1625 if (read_log_page(log, second_off, &second_tail, &usa_error) ||
1626 usa_error || second_tail->rhdr.sign != NTFS_RCRD_SIGNATURE) {
1629 second_file_off = 0;
1632 second_file_off = hdr_file_off(log, second_tail);
1633 lsn2 = le64_to_cpu(second_tail->record_hdr.last_end_lsn);
1636 /* Read first tail page (at pos 2/0x2000). */
1637 if (read_log_page(log, final_off, &first_tail, &usa_error) ||
1638 usa_error || first_tail->rhdr.sign != NTFS_RCRD_SIGNATURE) {
1644 first_file_off = hdr_file_off(log, first_tail);
1645 lsn1 = le64_to_cpu(first_tail->record_hdr.last_end_lsn);
1648 if (log->major_ver < 2) {
1651 first_tail_prev = first_tail;
1652 final_off_prev = first_file_off;
1653 second_tail_prev = second_tail;
1654 second_off_prev = second_file_off;
1657 if (!first_tail && !second_tail)
1660 if (first_tail && second_tail)
1661 best_page = lsn1 < lsn2 ? 1 : 0;
1662 else if (first_tail)
1667 page_off = best_page ? second_file_off : first_file_off;
1668 seq_base = (best_page ? lsn2 : lsn1) >> log->file_data_bits;
1672 best_lsn1 = first_tail ? base_lsn(log, first_tail, first_file_off) : 0;
1673 best_lsn2 = second_tail ? base_lsn(log, second_tail, second_file_off) :
1676 if (first_tail && second_tail) {
1677 if (best_lsn1 > best_lsn2) {
1678 best_lsn = best_lsn1;
1679 best_page = first_tail;
1680 this_off = first_file_off;
1682 best_lsn = best_lsn2;
1683 best_page = second_tail;
1684 this_off = second_file_off;
1686 } else if (first_tail) {
1687 best_lsn = best_lsn1;
1688 best_page = first_tail;
1689 this_off = first_file_off;
1690 } else if (second_tail) {
1691 best_lsn = best_lsn2;
1692 best_page = second_tail;
1693 this_off = second_file_off;
1698 best_page_pos = le16_to_cpu(best_page->page_pos);
1701 if (best_page_pos == page_pos) {
1702 seq_base = best_lsn >> log->file_data_bits;
1703 saved_off = page_off = le32_to_cpu(best_page->file_off);
1704 lsn_base = best_lsn;
1706 memmove(page_bufs, best_page, log->page_size);
1708 page_cnt = le16_to_cpu(best_page->page_count);
1714 } else if (seq_base == (best_lsn >> log->file_data_bits) &&
1715 saved_off + log->page_size == this_off &&
1716 lsn_base < best_lsn &&
1717 (page_pos != page_cnt || best_page_pos == page_pos ||
1718 best_page_pos == 1) &&
1719 (page_pos >= page_cnt || best_page_pos == page_pos)) {
1720 u16 bppc = le16_to_cpu(best_page->page_count);
1722 saved_off += log->page_size;
1723 lsn_base = best_lsn;
1725 memmove(Add2Ptr(page_bufs, tails * log->page_size), best_page,
1730 if (best_page_pos != bppc) {
1732 page_pos = best_page_pos;
1737 page_pos = page_cnt = 1;
1745 kfree(first_tail_prev);
1746 first_tail_prev = first_tail;
1747 final_off_prev = first_file_off;
1750 kfree(second_tail_prev);
1751 second_tail_prev = second_tail;
1752 second_off_prev = second_file_off;
1755 final_off += log->page_size;
1756 second_off += log->page_size;
1761 first_tail = first_tail_prev;
1762 final_off = final_off_prev;
1764 second_tail = second_tail_prev;
1765 second_off = second_off_prev;
1767 page_cnt = page_pos = 1;
1769 curpage_off = seq_base == log->seq_num ? min(log->next_page, page_off) :
1773 curpage_off == log->first_page &&
1774 !(log->l_flags & (NTFSLOG_NO_LAST_LSN | NTFSLOG_REUSE_TAIL));
1776 expected_seq = wrapped_file ? (log->seq_num + 1) : log->seq_num;
1778 nextpage_off = curpage_off;
1782 /* Read the next log page. */
1783 err = read_log_page(log, curpage_off, &page, &usa_error);
1785 /* Compute the next log page offset the file. */
1786 nextpage_off = next_page_off(log, curpage_off);
1787 wrapped = nextpage_off == log->first_page;
1790 struct RECORD_PAGE_HDR *cur_page =
1791 Add2Ptr(page_bufs, curpage_off - page_off);
1793 if (curpage_off == saved_off) {
1794 tail_page = cur_page;
1798 if (page_off > curpage_off || curpage_off >= saved_off)
1804 if (!err && !usa_error &&
1805 page->rhdr.sign == NTFS_RCRD_SIGNATURE &&
1806 cur_page->rhdr.lsn == page->rhdr.lsn &&
1807 cur_page->record_hdr.next_record_off ==
1808 page->record_hdr.next_record_off &&
1809 ((page_pos == page_cnt &&
1810 le16_to_cpu(page->page_pos) == 1) ||
1811 (page_pos != page_cnt &&
1812 le16_to_cpu(page->page_pos) == page_pos + 1 &&
1813 le16_to_cpu(page->page_count) == page_cnt))) {
1818 page_off1 = page_off;
1822 lsn_cur = le64_to_cpu(cur_page->rhdr.lsn);
1825 le64_to_cpu(cur_page->record_hdr.last_end_lsn) &&
1826 ((lsn_cur >> log->file_data_bits) +
1828 (lsn_to_vbo(log, lsn_cur) & ~log->page_mask)) ?
1830 0)) != expected_seq) {
1834 if (!is_log_record_end(cur_page)) {
1836 last_ok_lsn = lsn_cur;
1840 log->seq_num = expected_seq;
1841 log->l_flags &= ~NTFSLOG_NO_LAST_LSN;
1842 log->last_lsn = le64_to_cpu(cur_page->record_hdr.last_end_lsn);
1843 log->ra->current_lsn = cur_page->record_hdr.last_end_lsn;
1845 if (log->record_header_len <=
1847 le16_to_cpu(cur_page->record_hdr.next_record_off)) {
1848 log->l_flags |= NTFSLOG_REUSE_TAIL;
1849 log->next_page = curpage_off;
1851 log->l_flags &= ~NTFSLOG_REUSE_TAIL;
1852 log->next_page = nextpage_off;
1856 log->l_flags |= NTFSLOG_WRAPPED;
1858 last_ok_lsn = le64_to_cpu(cur_page->record_hdr.last_end_lsn);
1863 * If we are at the expected first page of a transfer check to see
1864 * if either tail copy is at this offset.
1865 * If this page is the last page of a transfer, check if we wrote
1866 * a subsequent tail copy.
1868 if (page_cnt == page_pos || page_cnt == page_pos + 1) {
1870 * Check if the offset matches either the first or second
1871 * tail copy. It is possible it will match both.
1873 if (curpage_off == final_off)
1874 tail_page = first_tail;
1877 * If we already matched on the first page then
1878 * check the ending lsn's.
1880 if (curpage_off == second_off) {
1883 le64_to_cpu(second_tail->record_hdr.last_end_lsn) >
1884 le64_to_cpu(first_tail->record_hdr
1886 tail_page = second_tail;
1893 /* We have a candidate for a tail copy. */
1894 lsn_cur = le64_to_cpu(tail_page->record_hdr.last_end_lsn);
1896 if (last_ok_lsn < lsn_cur) {
1898 * If the sequence number is not expected,
1899 * then don't use the tail copy.
1901 if (expected_seq != (lsn_cur >> log->file_data_bits))
1903 } else if (last_ok_lsn > lsn_cur) {
1905 * If the last lsn is greater than the one on
1906 * this page then forget this tail.
1913 *If we have an error on the current page,
1914 * we will break of this loop.
1916 if (err || usa_error)
1920 * Done if the last lsn on this page doesn't match the previous known
1921 * last lsn or the sequence number is not expected.
1923 lsn_cur = le64_to_cpu(page->rhdr.lsn);
1924 if (last_ok_lsn != lsn_cur &&
1925 expected_seq != (lsn_cur >> log->file_data_bits)) {
1930 * Check that the page position and page count values are correct.
1931 * If this is the first page of a transfer the position must be 1
1932 * and the count will be unknown.
1934 if (page_cnt == page_pos) {
1935 if (page->page_pos != cpu_to_le16(1) &&
1936 (!reuse_page || page->page_pos != page->page_count)) {
1938 * If the current page is the first page we are
1939 * looking at and we are reusing this page then
1940 * it can be either the first or last page of a
1941 * transfer. Otherwise it can only be the first.
1945 } else if (le16_to_cpu(page->page_count) != page_cnt ||
1946 le16_to_cpu(page->page_pos) != page_pos + 1) {
1948 * The page position better be 1 more than the last page
1949 * position and the page count better match.
1955 * We have a valid page the file and may have a valid page
1956 * the tail copy area.
1957 * If the tail page was written after the page the file then
1958 * break of the loop.
1961 le64_to_cpu(tail_page->record_hdr.last_end_lsn) > lsn_cur) {
1962 /* Remember if we will replace the page. */
1963 replace_page = true;
1969 if (is_log_record_end(page)) {
1971 * Since we have read this page we know the sequence number
1972 * is the same as our expected value.
1974 log->seq_num = expected_seq;
1975 log->last_lsn = le64_to_cpu(page->record_hdr.last_end_lsn);
1976 log->ra->current_lsn = page->record_hdr.last_end_lsn;
1977 log->l_flags &= ~NTFSLOG_NO_LAST_LSN;
1980 * If there is room on this page for another header then
1981 * remember we want to reuse the page.
1983 if (log->record_header_len <=
1985 le16_to_cpu(page->record_hdr.next_record_off)) {
1986 log->l_flags |= NTFSLOG_REUSE_TAIL;
1987 log->next_page = curpage_off;
1989 log->l_flags &= ~NTFSLOG_REUSE_TAIL;
1990 log->next_page = nextpage_off;
1993 /* Remember if we wrapped the log file. */
1995 log->l_flags |= NTFSLOG_WRAPPED;
1999 * Remember the last page count and position.
2000 * Also remember the last known lsn.
2002 page_cnt = le16_to_cpu(page->page_count);
2003 page_pos = le16_to_cpu(page->page_pos);
2004 last_ok_lsn = le64_to_cpu(page->rhdr.lsn);
2013 curpage_off = nextpage_off;
2021 log->seq_num = expected_seq;
2022 log->last_lsn = le64_to_cpu(tail_page->record_hdr.last_end_lsn);
2023 log->ra->current_lsn = tail_page->record_hdr.last_end_lsn;
2024 log->l_flags &= ~NTFSLOG_NO_LAST_LSN;
2026 if (log->page_size -
2028 tail_page->record_hdr.next_record_off) >=
2029 log->record_header_len) {
2030 log->l_flags |= NTFSLOG_REUSE_TAIL;
2031 log->next_page = curpage_off;
2033 log->l_flags &= ~NTFSLOG_REUSE_TAIL;
2034 log->next_page = nextpage_off;
2038 log->l_flags |= NTFSLOG_WRAPPED;
2041 /* Remember that the partial IO will start at the next page. */
2042 second_off = nextpage_off;
2045 * If the next page is the first page of the file then update
2046 * the sequence number for log records which begon the next page.
2052 * If we have a tail copy or are performing single page I/O we can
2053 * immediately look at the next page.
2055 if (replace_page || (log->ra->flags & RESTART_SINGLE_PAGE_IO)) {
2061 if (page_pos != page_cnt)
2064 * If the next page causes us to wrap to the beginning of the log
2065 * file then we know which page to check next.
2079 /* Walk through the file, reading log pages. */
2080 err = read_log_page(log, nextpage_off, &tst_page, &usa_error);
2083 * If we get a USA error then assume that we correctly found
2084 * the end of the original transfer.
2090 * If we were able to read the page, we examine it to see if it
2091 * is the same or different Io block.
2094 goto next_test_page_1;
2096 if (le16_to_cpu(tst_page->page_pos) == cur_pos &&
2097 check_subseq_log_page(log, tst_page, nextpage_off, expected_seq)) {
2098 page_cnt = le16_to_cpu(tst_page->page_count) + 1;
2099 page_pos = le16_to_cpu(tst_page->page_pos);
2107 nextpage_off = next_page_off(log, curpage_off);
2108 wrapped = nextpage_off == log->first_page;
2119 goto next_test_page;
2122 /* Skip over the remaining pages this transfer. */
2123 remain_pages = page_cnt - page_pos - 1;
2124 part_io_count += remain_pages;
2126 while (remain_pages--) {
2127 nextpage_off = next_page_off(log, curpage_off);
2128 wrapped = nextpage_off == log->first_page;
2134 /* Call our routine to check this log page. */
2138 err = read_log_page(log, nextpage_off, &tst_page, &usa_error);
2139 if (!err && !usa_error &&
2140 check_subseq_log_page(log, tst_page, nextpage_off, expected_seq)) {
2147 /* We have a valid file. */
2148 if (page_off1 || tail_page) {
2149 struct RECORD_PAGE_HDR *tmp_page;
2151 if (sb_rdonly(log->ni->mi.sbi->sb)) {
2157 tmp_page = Add2Ptr(page_bufs, page_off1 - page_off);
2158 tails -= (page_off1 - page_off) / log->page_size;
2162 tmp_page = tail_page;
2167 u64 off = hdr_file_off(log, tmp_page);
2170 page = kmalloc(log->page_size, GFP_NOFS);
2176 * Correct page and copy the data from this page
2177 * into it and flush it to disk.
2179 memcpy(page, tmp_page, log->page_size);
2181 /* Fill last flushed lsn value flush the page. */
2182 if (log->major_ver < 2)
2183 page->rhdr.lsn = page->record_hdr.last_end_lsn;
2187 page->page_pos = page->page_count = cpu_to_le16(1);
2189 ntfs_fix_pre_write(&page->rhdr, log->page_size);
2191 err = ntfs_sb_write_run(log->ni->mi.sbi,
2192 &log->ni->file.run, off, page,
2198 if (part_io_count && second_off == off) {
2199 second_off += log->page_size;
2203 tmp_page = Add2Ptr(tmp_page, log->page_size);
2207 if (part_io_count) {
2208 if (sb_rdonly(log->ni->mi.sbi->sb)) {
2225 * read_log_rec_buf - Copy a log record from the file to a buffer.
2227 * The log record may span several log pages and may even wrap the file.
2229 static int read_log_rec_buf(struct ntfs_log *log,
2230 const struct LFS_RECORD_HDR *rh, void *buffer)
2233 struct RECORD_PAGE_HDR *ph = NULL;
2234 u64 lsn = le64_to_cpu(rh->this_lsn);
2235 u32 vbo = lsn_to_vbo(log, lsn) & ~log->page_mask;
2236 u32 off = lsn_to_page_off(log, lsn) + log->record_header_len;
2237 u32 data_len = le32_to_cpu(rh->client_data_len);
2240 * While there are more bytes to transfer,
2241 * we continue to attempt to perform the read.
2245 u32 tail = log->page_size - off;
2247 if (tail >= data_len)
2252 err = read_log_page(log, vbo, &ph, &usa_error);
2257 * The last lsn on this page better be greater or equal
2258 * to the lsn we are copying.
2260 if (lsn > le64_to_cpu(ph->rhdr.lsn)) {
2265 memcpy(buffer, Add2Ptr(ph, off), tail);
2267 /* If there are no more bytes to transfer, we exit the loop. */
2269 if (!is_log_record_end(ph) ||
2270 lsn > le64_to_cpu(ph->record_hdr.last_end_lsn)) {
2277 if (ph->rhdr.lsn == ph->record_hdr.last_end_lsn ||
2278 lsn > le64_to_cpu(ph->rhdr.lsn)) {
2283 vbo = next_page_off(log, vbo);
2284 off = log->data_off;
2287 * Adjust our pointer the user's buffer to transfer
2288 * the next block to.
2290 buffer = Add2Ptr(buffer, tail);
2298 static int read_rst_area(struct ntfs_log *log, struct NTFS_RESTART **rst_,
2302 struct LFS_RECORD_HDR *rh = NULL;
2303 const struct CLIENT_REC *cr =
2304 Add2Ptr(log->ra, le16_to_cpu(log->ra->client_off));
2305 u64 lsnr, lsnc = le64_to_cpu(cr->restart_lsn);
2307 struct NTFS_RESTART *rst;
2312 /* If the client doesn't have a restart area, go ahead and exit now. */
2316 err = read_log_page(log, lsn_to_vbo(log, lsnc),
2317 (struct RECORD_PAGE_HDR **)&rh, NULL);
2322 lsnr = le64_to_cpu(rh->this_lsn);
2325 /* If the lsn values don't match, then the disk is corrupt. */
2331 len = le32_to_cpu(rh->client_data_len);
2338 if (len < sizeof(struct NTFS_RESTART)) {
2343 rst = kmalloc(len, GFP_NOFS);
2349 /* Copy the data into the 'rst' buffer. */
2350 err = read_log_rec_buf(log, rh, rst);
2364 static int find_log_rec(struct ntfs_log *log, u64 lsn, struct lcb *lcb)
2367 struct LFS_RECORD_HDR *rh = lcb->lrh;
2370 /* Read the record header for this lsn. */
2372 err = read_log_page(log, lsn_to_vbo(log, lsn),
2373 (struct RECORD_PAGE_HDR **)&rh, NULL);
2381 * If the lsn the log record doesn't match the desired
2382 * lsn then the disk is corrupt.
2384 if (lsn != le64_to_cpu(rh->this_lsn))
2387 len = le32_to_cpu(rh->client_data_len);
2390 * Check that the length field isn't greater than the total
2391 * available space the log file.
2393 rec_len = len + log->record_header_len;
2394 if (rec_len >= log->total_avail)
2398 * If the entire log record is on this log page,
2399 * put a pointer to the log record the context block.
2401 if (rh->flags & LOG_RECORD_MULTI_PAGE) {
2402 void *lr = kmalloc(len, GFP_NOFS);
2410 /* Copy the data into the buffer returned. */
2411 err = read_log_rec_buf(log, rh, lr);
2415 /* If beyond the end of the current page -> an error. */
2416 u32 page_off = lsn_to_page_off(log, lsn);
2418 if (page_off + len + log->record_header_len > log->page_size)
2421 lcb->log_rec = Add2Ptr(rh, sizeof(struct LFS_RECORD_HDR));
2429 * read_log_rec_lcb - Init the query operation.
2431 static int read_log_rec_lcb(struct ntfs_log *log, u64 lsn, u32 ctx_mode,
2435 const struct CLIENT_REC *cr;
2439 case lcb_ctx_undo_next:
2447 /* Check that the given lsn is the legal range for this client. */
2448 cr = Add2Ptr(log->ra, le16_to_cpu(log->ra->client_off));
2450 if (!verify_client_lsn(log, cr, lsn))
2453 lcb = kzalloc(sizeof(struct lcb), GFP_NOFS);
2456 lcb->client = log->client_id;
2457 lcb->ctx_mode = ctx_mode;
2459 /* Find the log record indicated by the given lsn. */
2460 err = find_log_rec(log, lsn, lcb);
2474 * find_client_next_lsn
2476 * Attempt to find the next lsn to return to a client based on the context mode.
2478 static int find_client_next_lsn(struct ntfs_log *log, struct lcb *lcb, u64 *lsn)
2482 struct LFS_RECORD_HDR *hdr;
2487 if (lcb_ctx_next != lcb->ctx_mode)
2488 goto check_undo_next;
2490 /* Loop as long as another lsn can be found. */
2494 err = next_log_lsn(log, hdr, ¤t_lsn);
2501 if (hdr != lcb->lrh)
2505 err = read_log_page(log, lsn_to_vbo(log, current_lsn),
2506 (struct RECORD_PAGE_HDR **)&hdr, NULL);
2510 if (memcmp(&hdr->client, &lcb->client,
2511 sizeof(struct CLIENT_ID))) {
2513 } else if (LfsClientRecord == hdr->record_type) {
2522 if (hdr != lcb->lrh)
2527 if (lcb_ctx_undo_next == lcb->ctx_mode)
2528 next_lsn = le64_to_cpu(hdr->client_undo_next_lsn);
2529 else if (lcb_ctx_prev == lcb->ctx_mode)
2530 next_lsn = le64_to_cpu(hdr->client_prev_lsn);
2537 if (!verify_client_lsn(
2538 log, Add2Ptr(log->ra, le16_to_cpu(log->ra->client_off)),
2543 err = read_log_page(log, lsn_to_vbo(log, next_lsn),
2544 (struct RECORD_PAGE_HDR **)&hdr, NULL);
2555 static int read_next_log_rec(struct ntfs_log *log, struct lcb *lcb, u64 *lsn)
2559 err = find_client_next_lsn(log, lcb, lsn);
2567 kfree(lcb->log_rec);
2569 lcb->log_rec = NULL;
2574 return find_log_rec(log, *lsn, lcb);
2577 bool check_index_header(const struct INDEX_HDR *hdr, size_t bytes)
2580 u32 min_de, de_off, used, total;
2581 const struct NTFS_DE *e;
2583 if (hdr_has_subnode(hdr)) {
2584 min_de = sizeof(struct NTFS_DE) + sizeof(u64);
2585 mask = NTFS_IE_HAS_SUBNODES;
2587 min_de = sizeof(struct NTFS_DE);
2591 de_off = le32_to_cpu(hdr->de_off);
2592 used = le32_to_cpu(hdr->used);
2593 total = le32_to_cpu(hdr->total);
2595 if (de_off > bytes - min_de || used > bytes || total > bytes ||
2596 de_off + min_de > used || used > total) {
2600 e = Add2Ptr(hdr, de_off);
2602 u16 esize = le16_to_cpu(e->size);
2603 struct NTFS_DE *next = Add2Ptr(e, esize);
2605 if (esize < min_de || PtrOffset(hdr, next) > used ||
2606 (e->flags & NTFS_IE_HAS_SUBNODES) != mask) {
2619 static inline bool check_index_buffer(const struct INDEX_BUFFER *ib, u32 bytes)
2622 const struct NTFS_RECORD_HEADER *r = &ib->rhdr;
2624 if (r->sign != NTFS_INDX_SIGNATURE)
2627 fo = (SECTOR_SIZE - ((bytes >> SECTOR_SHIFT) + 1) * sizeof(short));
2629 if (le16_to_cpu(r->fix_off) > fo)
2632 if ((le16_to_cpu(r->fix_num) - 1) * SECTOR_SIZE != bytes)
2635 return check_index_header(&ib->ihdr,
2636 bytes - offsetof(struct INDEX_BUFFER, ihdr));
2639 static inline bool check_index_root(const struct ATTRIB *attr,
2640 struct ntfs_sb_info *sbi)
2643 const struct INDEX_ROOT *root = resident_data(attr);
2644 u8 index_bits = le32_to_cpu(root->index_block_size) >=
2648 u8 block_clst = root->index_block_clst;
2650 if (le32_to_cpu(attr->res.data_size) < sizeof(struct INDEX_ROOT) ||
2651 (root->type != ATTR_NAME && root->type != ATTR_ZERO) ||
2652 (root->type == ATTR_NAME &&
2653 root->rule != NTFS_COLLATION_TYPE_FILENAME) ||
2654 (le32_to_cpu(root->index_block_size) !=
2655 (block_clst << index_bits)) ||
2656 (block_clst != 1 && block_clst != 2 && block_clst != 4 &&
2657 block_clst != 8 && block_clst != 0x10 && block_clst != 0x20 &&
2658 block_clst != 0x40 && block_clst != 0x80)) {
2662 ret = check_index_header(&root->ihdr,
2663 le32_to_cpu(attr->res.data_size) -
2664 offsetof(struct INDEX_ROOT, ihdr));
2668 static inline bool check_attr(const struct MFT_REC *rec,
2669 const struct ATTRIB *attr,
2670 struct ntfs_sb_info *sbi)
2672 u32 asize = le32_to_cpu(attr->size);
2674 u64 dsize, svcn, evcn;
2677 /* Check the fixed part of the attribute record header. */
2678 if (asize >= sbi->record_size ||
2679 asize + PtrOffset(rec, attr) >= sbi->record_size ||
2681 le16_to_cpu(attr->name_off) + attr->name_len * sizeof(short) >
2686 /* Check the attribute fields. */
2687 switch (attr->non_res) {
2689 rsize = le32_to_cpu(attr->res.data_size);
2690 if (rsize >= asize ||
2691 le16_to_cpu(attr->res.data_off) + rsize > asize) {
2697 dsize = le64_to_cpu(attr->nres.data_size);
2698 svcn = le64_to_cpu(attr->nres.svcn);
2699 evcn = le64_to_cpu(attr->nres.evcn);
2700 run_off = le16_to_cpu(attr->nres.run_off);
2702 if (svcn > evcn + 1 || run_off >= asize ||
2703 le64_to_cpu(attr->nres.valid_size) > dsize ||
2704 dsize > le64_to_cpu(attr->nres.alloc_size)) {
2708 if (run_off > asize)
2711 if (run_unpack(NULL, sbi, 0, svcn, evcn, svcn,
2712 Add2Ptr(attr, run_off), asize - run_off) < 0) {
2722 switch (attr->type) {
2724 if (fname_full_size(Add2Ptr(
2725 attr, le16_to_cpu(attr->res.data_off))) > asize) {
2731 return check_index_root(attr, sbi);
2734 if (rsize < sizeof(struct ATTR_STD_INFO5) &&
2735 rsize != sizeof(struct ATTR_STD_INFO)) {
2751 case ATTR_PROPERTYSET:
2752 case ATTR_LOGGED_UTILITY_STREAM:
2762 static inline bool check_file_record(const struct MFT_REC *rec,
2763 const struct MFT_REC *rec2,
2764 struct ntfs_sb_info *sbi)
2766 const struct ATTRIB *attr;
2767 u16 fo = le16_to_cpu(rec->rhdr.fix_off);
2768 u16 fn = le16_to_cpu(rec->rhdr.fix_num);
2769 u16 ao = le16_to_cpu(rec->attr_off);
2770 u32 rs = sbi->record_size;
2772 /* Check the file record header for consistency. */
2773 if (rec->rhdr.sign != NTFS_FILE_SIGNATURE ||
2774 fo > (SECTOR_SIZE - ((rs >> SECTOR_SHIFT) + 1) * sizeof(short)) ||
2775 (fn - 1) * SECTOR_SIZE != rs || ao < MFTRECORD_FIXUP_OFFSET_1 ||
2776 ao > sbi->record_size - SIZEOF_RESIDENT || !is_rec_inuse(rec) ||
2777 le32_to_cpu(rec->total) != rs) {
2781 /* Loop to check all of the attributes. */
2782 for (attr = Add2Ptr(rec, ao); attr->type != ATTR_END;
2783 attr = Add2Ptr(attr, le32_to_cpu(attr->size))) {
2784 if (check_attr(rec, attr, sbi))
2792 static inline int check_lsn(const struct NTFS_RECORD_HEADER *hdr,
2800 lsn = le64_to_cpu(hdr->lsn);
2802 if (hdr->sign == NTFS_HOLE_SIGNATURE)
2811 static inline bool check_if_attr(const struct MFT_REC *rec,
2812 const struct LOG_REC_HDR *lrh)
2814 u16 ro = le16_to_cpu(lrh->record_off);
2815 u16 o = le16_to_cpu(rec->attr_off);
2816 const struct ATTRIB *attr = Add2Ptr(rec, o);
2821 if (attr->type == ATTR_END)
2824 asize = le32_to_cpu(attr->size);
2829 attr = Add2Ptr(attr, asize);
2835 static inline bool check_if_index_root(const struct MFT_REC *rec,
2836 const struct LOG_REC_HDR *lrh)
2838 u16 ro = le16_to_cpu(lrh->record_off);
2839 u16 o = le16_to_cpu(rec->attr_off);
2840 const struct ATTRIB *attr = Add2Ptr(rec, o);
2845 if (attr->type == ATTR_END)
2848 asize = le32_to_cpu(attr->size);
2853 attr = Add2Ptr(attr, asize);
2856 return o == ro && attr->type == ATTR_ROOT;
2859 static inline bool check_if_root_index(const struct ATTRIB *attr,
2860 const struct INDEX_HDR *hdr,
2861 const struct LOG_REC_HDR *lrh)
2863 u16 ao = le16_to_cpu(lrh->attr_off);
2864 u32 de_off = le32_to_cpu(hdr->de_off);
2865 u32 o = PtrOffset(attr, hdr) + de_off;
2866 const struct NTFS_DE *e = Add2Ptr(hdr, de_off);
2867 u32 asize = le32_to_cpu(attr->size);
2875 esize = le16_to_cpu(e->size);
2880 e = Add2Ptr(e, esize);
2886 static inline bool check_if_alloc_index(const struct INDEX_HDR *hdr,
2889 u32 de_off = le32_to_cpu(hdr->de_off);
2890 u32 o = offsetof(struct INDEX_BUFFER, ihdr) + de_off;
2891 const struct NTFS_DE *e = Add2Ptr(hdr, de_off);
2892 u32 used = le32_to_cpu(hdr->used);
2894 while (o < attr_off) {
2900 esize = le16_to_cpu(e->size);
2906 e = Add2Ptr(e, esize);
2909 return o == attr_off;
2912 static inline void change_attr_size(struct MFT_REC *rec, struct ATTRIB *attr,
2915 u32 asize = le32_to_cpu(attr->size);
2916 int dsize = nsize - asize;
2917 u8 *next = Add2Ptr(attr, asize);
2918 u32 used = le32_to_cpu(rec->used);
2920 memmove(Add2Ptr(attr, nsize), next, used - PtrOffset(rec, next));
2922 rec->used = cpu_to_le32(used + dsize);
2923 attr->size = cpu_to_le32(nsize);
2927 struct ATTRIB *attr;
2928 struct runs_tree *run1;
2929 struct runs_tree run0;
2930 struct ntfs_inode *ni;
2937 * Return: 0 if 'attr' has the same type and name.
2939 static inline int cmp_type_and_name(const struct ATTRIB *a1,
2940 const struct ATTRIB *a2)
2942 return a1->type != a2->type || a1->name_len != a2->name_len ||
2943 (a1->name_len && memcmp(attr_name(a1), attr_name(a2),
2944 a1->name_len * sizeof(short)));
2947 static struct OpenAttr *find_loaded_attr(struct ntfs_log *log,
2948 const struct ATTRIB *attr, CLST rno)
2950 struct OPEN_ATTR_ENRTY *oe = NULL;
2952 while ((oe = enum_rstbl(log->open_attr_tbl, oe))) {
2953 struct OpenAttr *op_attr;
2955 if (ino_get(&oe->ref) != rno)
2958 op_attr = (struct OpenAttr *)oe->ptr;
2959 if (!cmp_type_and_name(op_attr->attr, attr))
2965 static struct ATTRIB *attr_create_nonres_log(struct ntfs_sb_info *sbi,
2966 enum ATTR_TYPE type, u64 size,
2967 const u16 *name, size_t name_len,
2970 struct ATTRIB *attr;
2971 u32 name_size = ALIGN(name_len * sizeof(short), 8);
2972 bool is_ext = flags & (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED);
2973 u32 asize = name_size +
2974 (is_ext ? SIZEOF_NONRESIDENT_EX : SIZEOF_NONRESIDENT);
2976 attr = kzalloc(asize, GFP_NOFS);
2981 attr->size = cpu_to_le32(asize);
2982 attr->flags = flags;
2984 attr->name_len = name_len;
2986 attr->nres.evcn = cpu_to_le64((u64)bytes_to_cluster(sbi, size) - 1);
2987 attr->nres.alloc_size = cpu_to_le64(ntfs_up_cluster(sbi, size));
2988 attr->nres.data_size = cpu_to_le64(size);
2989 attr->nres.valid_size = attr->nres.data_size;
2991 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
2992 if (is_attr_compressed(attr))
2993 attr->nres.c_unit = COMPRESSION_UNIT;
2995 attr->nres.run_off =
2996 cpu_to_le16(SIZEOF_NONRESIDENT_EX + name_size);
2997 memcpy(Add2Ptr(attr, SIZEOF_NONRESIDENT_EX), name,
2998 name_len * sizeof(short));
3000 attr->name_off = SIZEOF_NONRESIDENT_LE;
3001 attr->nres.run_off =
3002 cpu_to_le16(SIZEOF_NONRESIDENT + name_size);
3003 memcpy(Add2Ptr(attr, SIZEOF_NONRESIDENT), name,
3004 name_len * sizeof(short));
3011 * do_action - Common routine for the Redo and Undo Passes.
3012 * @rlsn: If it is NULL then undo.
3014 static int do_action(struct ntfs_log *log, struct OPEN_ATTR_ENRTY *oe,
3015 const struct LOG_REC_HDR *lrh, u32 op, void *data,
3016 u32 dlen, u32 rec_len, const u64 *rlsn)
3019 struct ntfs_sb_info *sbi = log->ni->mi.sbi;
3020 struct inode *inode = NULL, *inode_parent;
3021 struct mft_inode *mi = NULL, *mi2_child = NULL;
3022 CLST rno = 0, rno_base = 0;
3023 struct INDEX_BUFFER *ib = NULL;
3024 struct MFT_REC *rec = NULL;
3025 struct ATTRIB *attr = NULL, *attr2;
3026 struct INDEX_HDR *hdr;
3027 struct INDEX_ROOT *root;
3028 struct NTFS_DE *e, *e1, *e2;
3029 struct NEW_ATTRIBUTE_SIZES *new_sz;
3030 struct ATTR_FILE_NAME *fname;
3031 struct OpenAttr *oa, *oa2;
3032 u32 nsize, t32, asize, used, esize, off, bits;
3034 u32 record_size = sbi->record_size;
3036 u16 roff = le16_to_cpu(lrh->record_off);
3037 u16 aoff = le16_to_cpu(lrh->attr_off);
3039 u64 cbo = (u64)le16_to_cpu(lrh->cluster_off) << SECTOR_SHIFT;
3040 u64 tvo = le64_to_cpu(lrh->target_vcn) << sbi->cluster_bits;
3041 u64 vbo = cbo + tvo;
3042 void *buffer_le = NULL;
3044 bool a_dirty = false;
3049 /* Big switch to prepare. */
3051 /* ============================================================
3052 * Process MFT records, as described by the current log record.
3053 * ============================================================
3055 case InitializeFileRecordSegment:
3056 case DeallocateFileRecordSegment:
3057 case WriteEndOfFileRecordSegment:
3058 case CreateAttribute:
3059 case DeleteAttribute:
3060 case UpdateResidentValue:
3061 case UpdateMappingPairs:
3062 case SetNewAttributeSizes:
3063 case AddIndexEntryRoot:
3064 case DeleteIndexEntryRoot:
3065 case SetIndexEntryVcnRoot:
3066 case UpdateFileNameRoot:
3067 case UpdateRecordDataRoot:
3068 case ZeroEndOfFileRecord:
3069 rno = vbo >> sbi->record_bits;
3070 inode = ilookup(sbi->sb, rno);
3072 mi = &ntfs_i(inode)->mi;
3073 } else if (op == InitializeFileRecordSegment) {
3074 mi = kzalloc(sizeof(struct mft_inode), GFP_NOFS);
3077 err = mi_format_new(mi, sbi, rno, 0, false);
3081 /* Read from disk. */
3082 err = mi_get(sbi, rno, &mi);
3088 if (op == DeallocateFileRecordSegment)
3089 goto skip_load_parent;
3091 if (InitializeFileRecordSegment != op) {
3092 if (rec->rhdr.sign == NTFS_BAAD_SIGNATURE)
3094 if (!check_lsn(&rec->rhdr, rlsn))
3096 if (!check_file_record(rec, NULL, sbi))
3098 attr = Add2Ptr(rec, roff);
3101 if (is_rec_base(rec) || InitializeFileRecordSegment == op) {
3103 goto skip_load_parent;
3106 rno_base = ino_get(&rec->parent_ref);
3107 inode_parent = ntfs_iget5(sbi->sb, &rec->parent_ref, NULL);
3108 if (IS_ERR(inode_parent))
3109 goto skip_load_parent;
3111 if (is_bad_inode(inode_parent)) {
3113 goto skip_load_parent;
3116 if (ni_load_mi_ex(ntfs_i(inode_parent), rno, &mi2_child)) {
3119 if (mi2_child->mrec != mi->mrec)
3120 memcpy(mi2_child->mrec, mi->mrec,
3128 inode = inode_parent;
3130 rec = mi2_child->mrec;
3131 attr = Add2Ptr(rec, roff);
3135 inode_parent = NULL;
3139 * Process attributes, as described by the current log record.
3141 case UpdateNonresidentValue:
3142 case AddIndexEntryAllocation:
3143 case DeleteIndexEntryAllocation:
3144 case WriteEndOfIndexBuffer:
3145 case SetIndexEntryVcnAllocation:
3146 case UpdateFileNameAllocation:
3147 case SetBitsInNonresidentBitMap:
3148 case ClearBitsInNonresidentBitMap:
3149 case UpdateRecordDataAllocation:
3151 bytes = UpdateNonresidentValue == op ? dlen : 0;
3152 lco = (u64)le16_to_cpu(lrh->lcns_follow) << sbi->cluster_bits;
3154 if (attr->type == ATTR_ALLOC) {
3155 t32 = le32_to_cpu(oe->bytes_per_index);
3164 if (attr->type == ATTR_ALLOC)
3165 bytes = (bytes + 511) & ~511; // align
3167 buffer_le = kmalloc(bytes, GFP_NOFS);
3171 err = ntfs_read_run_nb(sbi, oa->run1, vbo, buffer_le, bytes,
3176 if (attr->type == ATTR_ALLOC && *(int *)buffer_le)
3177 ntfs_fix_post_read(buffer_le, bytes, false);
3184 /* Big switch to do operation. */
3186 case InitializeFileRecordSegment:
3187 if (roff + dlen > record_size)
3190 memcpy(Add2Ptr(rec, roff), data, dlen);
3194 case DeallocateFileRecordSegment:
3195 clear_rec_inuse(rec);
3196 le16_add_cpu(&rec->seq, 1);
3200 case WriteEndOfFileRecordSegment:
3201 attr2 = (struct ATTRIB *)data;
3202 if (!check_if_attr(rec, lrh) || roff + dlen > record_size)
3205 memmove(attr, attr2, dlen);
3206 rec->used = cpu_to_le32(ALIGN(roff + dlen, 8));
3211 case CreateAttribute:
3212 attr2 = (struct ATTRIB *)data;
3213 asize = le32_to_cpu(attr2->size);
3214 used = le32_to_cpu(rec->used);
3216 if (!check_if_attr(rec, lrh) || dlen < SIZEOF_RESIDENT ||
3217 !IS_ALIGNED(asize, 8) ||
3218 Add2Ptr(attr2, asize) > Add2Ptr(lrh, rec_len) ||
3219 dlen > record_size - used) {
3223 memmove(Add2Ptr(attr, asize), attr, used - roff);
3224 memcpy(attr, attr2, asize);
3226 rec->used = cpu_to_le32(used + asize);
3227 id = le16_to_cpu(rec->next_attr_id);
3228 id2 = le16_to_cpu(attr2->id);
3230 rec->next_attr_id = cpu_to_le16(id2 + 1);
3231 if (is_attr_indexed(attr))
3232 le16_add_cpu(&rec->hard_links, 1);
3234 oa2 = find_loaded_attr(log, attr, rno_base);
3236 void *p2 = kmemdup(attr, le32_to_cpu(attr->size),
3239 // run_close(oa2->run1);
3248 case DeleteAttribute:
3249 asize = le32_to_cpu(attr->size);
3250 used = le32_to_cpu(rec->used);
3252 if (!check_if_attr(rec, lrh))
3255 rec->used = cpu_to_le32(used - asize);
3256 if (is_attr_indexed(attr))
3257 le16_add_cpu(&rec->hard_links, -1);
3259 memmove(attr, Add2Ptr(attr, asize), used - asize - roff);
3264 case UpdateResidentValue:
3265 nsize = aoff + dlen;
3267 if (!check_if_attr(rec, lrh))
3270 asize = le32_to_cpu(attr->size);
3271 used = le32_to_cpu(rec->used);
3273 if (lrh->redo_len == lrh->undo_len) {
3279 if (nsize > asize && nsize - asize > record_size - used)
3282 nsize = ALIGN(nsize, 8);
3283 data_off = le16_to_cpu(attr->res.data_off);
3285 if (nsize < asize) {
3286 memmove(Add2Ptr(attr, aoff), data, dlen);
3287 data = NULL; // To skip below memmove().
3290 memmove(Add2Ptr(attr, nsize), Add2Ptr(attr, asize),
3291 used - le16_to_cpu(lrh->record_off) - asize);
3293 rec->used = cpu_to_le32(used + nsize - asize);
3294 attr->size = cpu_to_le32(nsize);
3295 attr->res.data_size = cpu_to_le32(aoff + dlen - data_off);
3299 memmove(Add2Ptr(attr, aoff), data, dlen);
3301 oa2 = find_loaded_attr(log, attr, rno_base);
3303 void *p2 = kmemdup(attr, le32_to_cpu(attr->size),
3306 // run_close(&oa2->run0);
3307 oa2->run1 = &oa2->run0;
3316 case UpdateMappingPairs:
3317 nsize = aoff + dlen;
3318 asize = le32_to_cpu(attr->size);
3319 used = le32_to_cpu(rec->used);
3321 if (!check_if_attr(rec, lrh) || !attr->non_res ||
3322 aoff < le16_to_cpu(attr->nres.run_off) || aoff > asize ||
3323 (nsize > asize && nsize - asize > record_size - used)) {
3327 nsize = ALIGN(nsize, 8);
3329 memmove(Add2Ptr(attr, nsize), Add2Ptr(attr, asize),
3330 used - le16_to_cpu(lrh->record_off) - asize);
3331 rec->used = cpu_to_le32(used + nsize - asize);
3332 attr->size = cpu_to_le32(nsize);
3333 memmove(Add2Ptr(attr, aoff), data, dlen);
3335 if (run_get_highest_vcn(le64_to_cpu(attr->nres.svcn),
3336 attr_run(attr), &t64)) {
3340 attr->nres.evcn = cpu_to_le64(t64);
3341 oa2 = find_loaded_attr(log, attr, rno_base);
3342 if (oa2 && oa2->attr->non_res)
3343 oa2->attr->nres.evcn = attr->nres.evcn;
3348 case SetNewAttributeSizes:
3350 if (!check_if_attr(rec, lrh) || !attr->non_res)
3353 attr->nres.alloc_size = new_sz->alloc_size;
3354 attr->nres.data_size = new_sz->data_size;
3355 attr->nres.valid_size = new_sz->valid_size;
3357 if (dlen >= sizeof(struct NEW_ATTRIBUTE_SIZES))
3358 attr->nres.total_size = new_sz->total_size;
3360 oa2 = find_loaded_attr(log, attr, rno_base);
3362 void *p2 = kmemdup(attr, le32_to_cpu(attr->size),
3372 case AddIndexEntryRoot:
3373 e = (struct NTFS_DE *)data;
3374 esize = le16_to_cpu(e->size);
3375 root = resident_data(attr);
3377 used = le32_to_cpu(hdr->used);
3379 if (!check_if_index_root(rec, lrh) ||
3380 !check_if_root_index(attr, hdr, lrh) ||
3381 Add2Ptr(data, esize) > Add2Ptr(lrh, rec_len) ||
3382 esize > le32_to_cpu(rec->total) - le32_to_cpu(rec->used)) {
3386 e1 = Add2Ptr(attr, le16_to_cpu(lrh->attr_off));
3388 change_attr_size(rec, attr, le32_to_cpu(attr->size) + esize);
3390 memmove(Add2Ptr(e1, esize), e1,
3391 PtrOffset(e1, Add2Ptr(hdr, used)));
3392 memmove(e1, e, esize);
3394 le32_add_cpu(&attr->res.data_size, esize);
3395 hdr->used = cpu_to_le32(used + esize);
3396 le32_add_cpu(&hdr->total, esize);
3401 case DeleteIndexEntryRoot:
3402 root = resident_data(attr);
3404 used = le32_to_cpu(hdr->used);
3406 if (!check_if_index_root(rec, lrh) ||
3407 !check_if_root_index(attr, hdr, lrh)) {
3411 e1 = Add2Ptr(attr, le16_to_cpu(lrh->attr_off));
3412 esize = le16_to_cpu(e1->size);
3413 e2 = Add2Ptr(e1, esize);
3415 memmove(e1, e2, PtrOffset(e2, Add2Ptr(hdr, used)));
3417 le32_sub_cpu(&attr->res.data_size, esize);
3418 hdr->used = cpu_to_le32(used - esize);
3419 le32_sub_cpu(&hdr->total, esize);
3421 change_attr_size(rec, attr, le32_to_cpu(attr->size) - esize);
3426 case SetIndexEntryVcnRoot:
3427 root = resident_data(attr);
3430 if (!check_if_index_root(rec, lrh) ||
3431 !check_if_root_index(attr, hdr, lrh)) {
3435 e = Add2Ptr(attr, le16_to_cpu(lrh->attr_off));
3437 de_set_vbn_le(e, *(__le64 *)data);
3441 case UpdateFileNameRoot:
3442 root = resident_data(attr);
3445 if (!check_if_index_root(rec, lrh) ||
3446 !check_if_root_index(attr, hdr, lrh)) {
3450 e = Add2Ptr(attr, le16_to_cpu(lrh->attr_off));
3451 fname = (struct ATTR_FILE_NAME *)(e + 1);
3452 memmove(&fname->dup, data, sizeof(fname->dup)); //
3456 case UpdateRecordDataRoot:
3457 root = resident_data(attr);
3460 if (!check_if_index_root(rec, lrh) ||
3461 !check_if_root_index(attr, hdr, lrh)) {
3465 e = Add2Ptr(attr, le16_to_cpu(lrh->attr_off));
3467 memmove(Add2Ptr(e, le16_to_cpu(e->view.data_off)), data, dlen);
3472 case ZeroEndOfFileRecord:
3473 if (roff + dlen > record_size)
3476 memset(attr, 0, dlen);
3480 case UpdateNonresidentValue:
3481 if (lco < cbo + roff + dlen)
3484 memcpy(Add2Ptr(buffer_le, roff), data, dlen);
3487 if (attr->type == ATTR_ALLOC)
3488 ntfs_fix_pre_write(buffer_le, bytes);
3491 case AddIndexEntryAllocation:
3492 ib = Add2Ptr(buffer_le, roff);
3495 esize = le16_to_cpu(e->size);
3496 e1 = Add2Ptr(ib, aoff);
3498 if (is_baad(&ib->rhdr))
3500 if (!check_lsn(&ib->rhdr, rlsn))
3503 used = le32_to_cpu(hdr->used);
3505 if (!check_index_buffer(ib, bytes) ||
3506 !check_if_alloc_index(hdr, aoff) ||
3507 Add2Ptr(e, esize) > Add2Ptr(lrh, rec_len) ||
3508 used + esize > le32_to_cpu(hdr->total)) {
3512 memmove(Add2Ptr(e1, esize), e1,
3513 PtrOffset(e1, Add2Ptr(hdr, used)));
3514 memcpy(e1, e, esize);
3516 hdr->used = cpu_to_le32(used + esize);
3520 ntfs_fix_pre_write(&ib->rhdr, bytes);
3523 case DeleteIndexEntryAllocation:
3524 ib = Add2Ptr(buffer_le, roff);
3526 e = Add2Ptr(ib, aoff);
3527 esize = le16_to_cpu(e->size);
3529 if (is_baad(&ib->rhdr))
3531 if (!check_lsn(&ib->rhdr, rlsn))
3534 if (!check_index_buffer(ib, bytes) ||
3535 !check_if_alloc_index(hdr, aoff)) {
3539 e1 = Add2Ptr(e, esize);
3541 used = le32_to_cpu(hdr->used);
3543 memmove(e, e1, PtrOffset(e1, Add2Ptr(hdr, used)));
3545 hdr->used = cpu_to_le32(used - nsize);
3549 ntfs_fix_pre_write(&ib->rhdr, bytes);
3552 case WriteEndOfIndexBuffer:
3553 ib = Add2Ptr(buffer_le, roff);
3555 e = Add2Ptr(ib, aoff);
3557 if (is_baad(&ib->rhdr))
3559 if (!check_lsn(&ib->rhdr, rlsn))
3561 if (!check_index_buffer(ib, bytes) ||
3562 !check_if_alloc_index(hdr, aoff) ||
3563 aoff + dlen > offsetof(struct INDEX_BUFFER, ihdr) +
3564 le32_to_cpu(hdr->total)) {
3568 hdr->used = cpu_to_le32(dlen + PtrOffset(hdr, e));
3569 memmove(e, data, dlen);
3572 ntfs_fix_pre_write(&ib->rhdr, bytes);
3575 case SetIndexEntryVcnAllocation:
3576 ib = Add2Ptr(buffer_le, roff);
3578 e = Add2Ptr(ib, aoff);
3580 if (is_baad(&ib->rhdr))
3583 if (!check_lsn(&ib->rhdr, rlsn))
3585 if (!check_index_buffer(ib, bytes) ||
3586 !check_if_alloc_index(hdr, aoff)) {
3590 de_set_vbn_le(e, *(__le64 *)data);
3593 ntfs_fix_pre_write(&ib->rhdr, bytes);
3596 case UpdateFileNameAllocation:
3597 ib = Add2Ptr(buffer_le, roff);
3599 e = Add2Ptr(ib, aoff);
3601 if (is_baad(&ib->rhdr))
3604 if (!check_lsn(&ib->rhdr, rlsn))
3606 if (!check_index_buffer(ib, bytes) ||
3607 !check_if_alloc_index(hdr, aoff)) {
3611 fname = (struct ATTR_FILE_NAME *)(e + 1);
3612 memmove(&fname->dup, data, sizeof(fname->dup));
3615 ntfs_fix_pre_write(&ib->rhdr, bytes);
3618 case SetBitsInNonresidentBitMap:
3619 off = le32_to_cpu(((struct BITMAP_RANGE *)data)->bitmap_off);
3620 bits = le32_to_cpu(((struct BITMAP_RANGE *)data)->bits);
3622 if (cbo + (off + 7) / 8 > lco ||
3623 cbo + ((off + bits + 7) / 8) > lco) {
3627 ntfs_bitmap_set_le(Add2Ptr(buffer_le, roff), off, bits);
3631 case ClearBitsInNonresidentBitMap:
3632 off = le32_to_cpu(((struct BITMAP_RANGE *)data)->bitmap_off);
3633 bits = le32_to_cpu(((struct BITMAP_RANGE *)data)->bits);
3635 if (cbo + (off + 7) / 8 > lco ||
3636 cbo + ((off + bits + 7) / 8) > lco) {
3640 ntfs_bitmap_clear_le(Add2Ptr(buffer_le, roff), off, bits);
3644 case UpdateRecordDataAllocation:
3645 ib = Add2Ptr(buffer_le, roff);
3647 e = Add2Ptr(ib, aoff);
3649 if (is_baad(&ib->rhdr))
3652 if (!check_lsn(&ib->rhdr, rlsn))
3654 if (!check_index_buffer(ib, bytes) ||
3655 !check_if_alloc_index(hdr, aoff)) {
3659 memmove(Add2Ptr(e, le16_to_cpu(e->view.data_off)), data, dlen);
3662 ntfs_fix_pre_write(&ib->rhdr, bytes);
3670 __le64 t64 = cpu_to_le64(*rlsn);
3673 rec->rhdr.lsn = t64;
3678 if (mi && mi->dirty) {
3679 err = mi_write(mi, 0);
3686 err = ntfs_sb_write_run(sbi, oa->run1, vbo, buffer_le, bytes,
3696 else if (mi != mi2_child)
3704 log->set_dirty = true;
3709 * log_replay - Replays log and empties it.
3711 * This function is called during mount operation.
3712 * It replays log and empties it.
3713 * Initialized is set false if logfile contains '-1'.
3715 int log_replay(struct ntfs_inode *ni, bool *initialized)
3718 struct ntfs_sb_info *sbi = ni->mi.sbi;
3719 struct ntfs_log *log;
3721 struct restart_info rst_info, rst_info2;
3722 u64 rec_lsn, ra_lsn, checkpt_lsn = 0, rlsn = 0;
3723 struct ATTR_NAME_ENTRY *attr_names = NULL;
3724 struct ATTR_NAME_ENTRY *ane;
3725 struct RESTART_TABLE *dptbl = NULL;
3726 struct RESTART_TABLE *trtbl = NULL;
3727 const struct RESTART_TABLE *rt;
3728 struct RESTART_TABLE *oatbl = NULL;
3729 struct inode *inode;
3730 struct OpenAttr *oa;
3731 struct ntfs_inode *ni_oe;
3732 struct ATTRIB *attr = NULL;
3733 u64 size, vcn, undo_next_lsn;
3734 CLST rno, lcn, lcn0, len0, clen;
3736 struct NTFS_RESTART *rst = NULL;
3737 struct lcb *lcb = NULL;
3738 struct OPEN_ATTR_ENRTY *oe;
3739 struct TRANSACTION_ENTRY *tr;
3740 struct DIR_PAGE_ENTRY *dp;
3741 u32 i, bytes_per_attr_entry;
3742 u32 l_size = ni->vfs_inode.i_size;
3743 u32 orig_file_size = l_size;
3744 u32 page_size, vbo, tail, off, dlen;
3745 u32 saved_len, rec_len, transact_id;
3746 bool use_second_page;
3747 struct RESTART_AREA *ra2, *ra = NULL;
3748 struct CLIENT_REC *ca, *cr;
3750 struct RESTART_HDR *rh;
3751 const struct LFS_RECORD_HDR *frh;
3752 const struct LOG_REC_HDR *lrh;
3754 bool is_ro = sb_rdonly(sbi->sb);
3759 /* Get the size of page. NOTE: To replay we can use default page. */
3760 #if PAGE_SIZE >= DefaultLogPageSize && PAGE_SIZE <= DefaultLogPageSize * 2
3761 page_size = norm_file_page(PAGE_SIZE, &l_size, true);
3763 page_size = norm_file_page(PAGE_SIZE, &l_size, false);
3768 log = kzalloc(sizeof(struct ntfs_log), GFP_NOFS);
3773 log->l_size = l_size;
3774 log->one_page_buf = kmalloc(page_size, GFP_NOFS);
3776 if (!log->one_page_buf) {
3781 log->page_size = page_size;
3782 log->page_mask = page_size - 1;
3783 log->page_bits = blksize_bits(page_size);
3785 /* Look for a restart area on the disk. */
3786 memset(&rst_info, 0, sizeof(struct restart_info));
3787 err = log_read_rst(log, l_size, true, &rst_info);
3791 /* remember 'initialized' */
3792 *initialized = rst_info.initialized;
3794 if (!rst_info.restart) {
3795 if (rst_info.initialized) {
3796 /* No restart area but the file is not initialized. */
3801 log_init_pg_hdr(log, page_size, page_size, 1, 1);
3802 log_create(log, l_size, 0, get_random_u32(), false, false);
3806 ra = log_create_ra(log);
3812 log->init_ra = true;
3818 * If the restart offset above wasn't zero then we won't
3819 * look for a second restart.
3822 goto check_restart_area;
3824 memset(&rst_info2, 0, sizeof(struct restart_info));
3825 err = log_read_rst(log, l_size, false, &rst_info2);
3829 /* Determine which restart area to use. */
3830 if (!rst_info2.restart || rst_info2.last_lsn <= rst_info.last_lsn)
3831 goto use_first_page;
3833 use_second_page = true;
3835 if (rst_info.chkdsk_was_run && page_size != rst_info.vbo) {
3836 struct RECORD_PAGE_HDR *sp = NULL;
3839 if (!read_log_page(log, page_size, &sp, &usa_error) &&
3840 sp->rhdr.sign == NTFS_CHKD_SIGNATURE) {
3841 use_second_page = false;
3846 if (use_second_page) {
3847 kfree(rst_info.r_page);
3848 memcpy(&rst_info, &rst_info2, sizeof(struct restart_info));
3849 rst_info2.r_page = NULL;
3853 kfree(rst_info2.r_page);
3857 * If the restart area is at offset 0, we want
3858 * to write the second restart area first.
3860 log->init_ra = !!rst_info.vbo;
3862 /* If we have a valid page then grab a pointer to the restart area. */
3863 ra2 = rst_info.valid_page ?
3864 Add2Ptr(rst_info.r_page,
3865 le16_to_cpu(rst_info.r_page->ra_off)) :
3868 if (rst_info.chkdsk_was_run ||
3869 (ra2 && ra2->client_idx[1] == LFS_NO_CLIENT_LE)) {
3870 bool wrapped = false;
3871 bool use_multi_page = false;
3874 /* Do some checks based on whether we have a valid log page. */
3875 if (!rst_info.valid_page) {
3876 open_log_count = get_random_u32();
3877 goto init_log_instance;
3879 open_log_count = le32_to_cpu(ra2->open_log_count);
3882 * If the restart page size isn't changing then we want to
3883 * check how much work we need to do.
3885 if (page_size != le32_to_cpu(rst_info.r_page->sys_page_size))
3886 goto init_log_instance;
3889 log_init_pg_hdr(log, page_size, page_size, 1, 1);
3891 log_create(log, l_size, rst_info.last_lsn, open_log_count,
3892 wrapped, use_multi_page);
3894 ra = log_create_ra(log);
3901 /* Put the restart areas and initialize
3902 * the log file as required.
3913 * If the log page or the system page sizes have changed, we can't
3914 * use the log file. We must use the system page size instead of the
3915 * default size if there is not a clean shutdown.
3917 t32 = le32_to_cpu(rst_info.r_page->sys_page_size);
3918 if (page_size != t32) {
3919 l_size = orig_file_size;
3921 norm_file_page(t32, &l_size, t32 == DefaultLogPageSize);
3924 if (page_size != t32 ||
3925 page_size != le32_to_cpu(rst_info.r_page->page_size)) {
3930 /* If the file size has shrunk then we won't mount it. */
3931 if (l_size < le64_to_cpu(ra2->l_size)) {
3936 log_init_pg_hdr(log, page_size, page_size,
3937 le16_to_cpu(rst_info.r_page->major_ver),
3938 le16_to_cpu(rst_info.r_page->minor_ver));
3940 log->l_size = le64_to_cpu(ra2->l_size);
3941 log->seq_num_bits = le32_to_cpu(ra2->seq_num_bits);
3942 log->file_data_bits = sizeof(u64) * 8 - log->seq_num_bits;
3943 log->seq_num_mask = (8 << log->file_data_bits) - 1;
3944 log->last_lsn = le64_to_cpu(ra2->current_lsn);
3945 log->seq_num = log->last_lsn >> log->file_data_bits;
3946 log->ra_off = le16_to_cpu(rst_info.r_page->ra_off);
3947 log->restart_size = log->sys_page_size - log->ra_off;
3948 log->record_header_len = le16_to_cpu(ra2->rec_hdr_len);
3949 log->ra_size = le16_to_cpu(ra2->ra_len);
3950 log->data_off = le16_to_cpu(ra2->data_off);
3951 log->data_size = log->page_size - log->data_off;
3952 log->reserved = log->data_size - log->record_header_len;
3954 vbo = lsn_to_vbo(log, log->last_lsn);
3956 if (vbo < log->first_page) {
3957 /* This is a pseudo lsn. */
3958 log->l_flags |= NTFSLOG_NO_LAST_LSN;
3959 log->next_page = log->first_page;
3963 /* Find the end of this log record. */
3964 off = final_log_off(log, log->last_lsn,
3965 le32_to_cpu(ra2->last_lsn_data_len));
3967 /* If we wrapped the file then increment the sequence number. */
3970 log->l_flags |= NTFSLOG_WRAPPED;
3973 /* Now compute the next log page to use. */
3974 vbo &= ~log->sys_page_mask;
3975 tail = log->page_size - (off & log->page_mask) - 1;
3978 *If we can fit another log record on the page,
3979 * move back a page the log file.
3981 if (tail >= log->record_header_len) {
3982 log->l_flags |= NTFSLOG_REUSE_TAIL;
3983 log->next_page = vbo;
3985 log->next_page = next_page_off(log, vbo);
3990 * Find the oldest client lsn. Use the last
3991 * flushed lsn as a starting point.
3993 log->oldest_lsn = log->last_lsn;
3994 oldest_client_lsn(Add2Ptr(ra2, le16_to_cpu(ra2->client_off)),
3995 ra2->client_idx[1], &log->oldest_lsn);
3996 log->oldest_lsn_off = lsn_to_vbo(log, log->oldest_lsn);
3998 if (log->oldest_lsn_off < log->first_page)
3999 log->l_flags |= NTFSLOG_NO_OLDEST_LSN;
4001 if (!(ra2->flags & RESTART_SINGLE_PAGE_IO))
4002 log->l_flags |= NTFSLOG_WRAPPED | NTFSLOG_MULTIPLE_PAGE_IO;
4004 log->current_openlog_count = le32_to_cpu(ra2->open_log_count);
4005 log->total_avail_pages = log->l_size - log->first_page;
4006 log->total_avail = log->total_avail_pages >> log->page_bits;
4007 log->max_current_avail = log->total_avail * log->reserved;
4008 log->total_avail = log->total_avail * log->data_size;
4010 log->current_avail = current_log_avail(log);
4012 ra = kzalloc(log->restart_size, GFP_NOFS);
4019 t16 = le16_to_cpu(ra2->client_off);
4020 if (t16 == offsetof(struct RESTART_AREA, clients)) {
4021 memcpy(ra, ra2, log->ra_size);
4023 memcpy(ra, ra2, offsetof(struct RESTART_AREA, clients));
4024 memcpy(ra->clients, Add2Ptr(ra2, t16),
4025 le16_to_cpu(ra2->ra_len) - t16);
4027 log->current_openlog_count = get_random_u32();
4028 ra->open_log_count = cpu_to_le32(log->current_openlog_count);
4029 log->ra_size = offsetof(struct RESTART_AREA, clients) +
4030 sizeof(struct CLIENT_REC);
4032 cpu_to_le16(offsetof(struct RESTART_AREA, clients));
4033 ra->ra_len = cpu_to_le16(log->ra_size);
4036 le32_add_cpu(&ra->open_log_count, 1);
4038 /* Now we need to walk through looking for the last lsn. */
4039 err = last_log_lsn(log);
4043 log->current_avail = current_log_avail(log);
4045 /* Remember which restart area to write first. */
4046 log->init_ra = rst_info.vbo;
4049 /* 1.0, 1.1, 2.0 log->major_ver/minor_ver - short values. */
4050 switch ((log->major_ver << 16) + log->minor_ver) {
4056 ntfs_warn(sbi->sb, "\x24LogFile version %d.%d is not supported",
4057 log->major_ver, log->minor_ver);
4059 log->set_dirty = true;
4063 /* One client "NTFS" per logfile. */
4064 ca = Add2Ptr(ra, le16_to_cpu(ra->client_off));
4066 for (client = ra->client_idx[1];; client = cr->next_client) {
4067 if (client == LFS_NO_CLIENT_LE) {
4068 /* Insert "NTFS" client LogFile. */
4069 client = ra->client_idx[0];
4070 if (client == LFS_NO_CLIENT_LE) {
4075 t16 = le16_to_cpu(client);
4078 remove_client(ca, cr, &ra->client_idx[0]);
4080 cr->restart_lsn = 0;
4081 cr->oldest_lsn = cpu_to_le64(log->oldest_lsn);
4082 cr->name_bytes = cpu_to_le32(8);
4083 cr->name[0] = cpu_to_le16('N');
4084 cr->name[1] = cpu_to_le16('T');
4085 cr->name[2] = cpu_to_le16('F');
4086 cr->name[3] = cpu_to_le16('S');
4088 add_client(ca, t16, &ra->client_idx[1]);
4092 cr = ca + le16_to_cpu(client);
4094 if (cpu_to_le32(8) == cr->name_bytes &&
4095 cpu_to_le16('N') == cr->name[0] &&
4096 cpu_to_le16('T') == cr->name[1] &&
4097 cpu_to_le16('F') == cr->name[2] &&
4098 cpu_to_le16('S') == cr->name[3])
4102 /* Update the client handle with the client block information. */
4103 log->client_id.seq_num = cr->seq_num;
4104 log->client_id.client_idx = client;
4106 err = read_rst_area(log, &rst, &ra_lsn);
4113 bytes_per_attr_entry = !rst->major_ver ? 0x2C : 0x28;
4115 checkpt_lsn = le64_to_cpu(rst->check_point_start);
4117 checkpt_lsn = ra_lsn;
4119 /* Allocate and Read the Transaction Table. */
4120 if (!rst->transact_table_len)
4121 goto check_dirty_page_table;
4123 t64 = le64_to_cpu(rst->transact_table_lsn);
4124 err = read_log_rec_lcb(log, t64, lcb_ctx_prev, &lcb);
4130 rec_len = le32_to_cpu(frh->client_data_len);
4132 if (!check_log_rec(lrh, rec_len, le32_to_cpu(frh->transact_id),
4133 bytes_per_attr_entry)) {
4138 t16 = le16_to_cpu(lrh->redo_off);
4140 rt = Add2Ptr(lrh, t16);
4141 t32 = rec_len - t16;
4143 /* Now check that this is a valid restart table. */
4144 if (!check_rstbl(rt, t32)) {
4149 trtbl = kmemdup(rt, t32, GFP_NOFS);
4158 check_dirty_page_table:
4159 /* The next record back should be the Dirty Pages Table. */
4160 if (!rst->dirty_pages_len)
4161 goto check_attribute_names;
4163 t64 = le64_to_cpu(rst->dirty_pages_table_lsn);
4164 err = read_log_rec_lcb(log, t64, lcb_ctx_prev, &lcb);
4170 rec_len = le32_to_cpu(frh->client_data_len);
4172 if (!check_log_rec(lrh, rec_len, le32_to_cpu(frh->transact_id),
4173 bytes_per_attr_entry)) {
4178 t16 = le16_to_cpu(lrh->redo_off);
4180 rt = Add2Ptr(lrh, t16);
4181 t32 = rec_len - t16;
4183 /* Now check that this is a valid restart table. */
4184 if (!check_rstbl(rt, t32)) {
4189 dptbl = kmemdup(rt, t32, GFP_NOFS);
4195 /* Convert Ra version '0' into version '1'. */
4200 while ((dp = enum_rstbl(dptbl, dp))) {
4201 struct DIR_PAGE_ENTRY_32 *dp0 = (struct DIR_PAGE_ENTRY_32 *)dp;
4202 // NOTE: Danger. Check for of boundary.
4203 memmove(&dp->vcn, &dp0->vcn_low,
4205 le32_to_cpu(dp->lcns_follow) * sizeof(u64));
4213 * Go through the table and remove the duplicates,
4214 * remembering the oldest lsn values.
4216 if (sbi->cluster_size <= log->page_size)
4217 goto trace_dp_table;
4220 while ((dp = enum_rstbl(dptbl, dp))) {
4221 struct DIR_PAGE_ENTRY *next = dp;
4223 while ((next = enum_rstbl(dptbl, next))) {
4224 if (next->target_attr == dp->target_attr &&
4225 next->vcn == dp->vcn) {
4226 if (le64_to_cpu(next->oldest_lsn) <
4227 le64_to_cpu(dp->oldest_lsn)) {
4228 dp->oldest_lsn = next->oldest_lsn;
4231 free_rsttbl_idx(dptbl, PtrOffset(dptbl, next));
4236 check_attribute_names:
4237 /* The next record should be the Attribute Names. */
4238 if (!rst->attr_names_len)
4239 goto check_attr_table;
4241 t64 = le64_to_cpu(rst->attr_names_lsn);
4242 err = read_log_rec_lcb(log, t64, lcb_ctx_prev, &lcb);
4248 rec_len = le32_to_cpu(frh->client_data_len);
4250 if (!check_log_rec(lrh, rec_len, le32_to_cpu(frh->transact_id),
4251 bytes_per_attr_entry)) {
4256 t32 = lrh_length(lrh);
4259 attr_names = kmemdup(Add2Ptr(lrh, t32), rec_len, GFP_NOFS);
4269 /* The next record should be the attribute Table. */
4270 if (!rst->open_attr_len)
4271 goto check_attribute_names2;
4273 t64 = le64_to_cpu(rst->open_attr_table_lsn);
4274 err = read_log_rec_lcb(log, t64, lcb_ctx_prev, &lcb);
4280 rec_len = le32_to_cpu(frh->client_data_len);
4282 if (!check_log_rec(lrh, rec_len, le32_to_cpu(frh->transact_id),
4283 bytes_per_attr_entry)) {
4288 t16 = le16_to_cpu(lrh->redo_off);
4290 rt = Add2Ptr(lrh, t16);
4291 t32 = rec_len - t16;
4293 if (!check_rstbl(rt, t32)) {
4298 oatbl = kmemdup(rt, t32, GFP_NOFS);
4304 log->open_attr_tbl = oatbl;
4306 /* Clear all of the Attr pointers. */
4308 while ((oe = enum_rstbl(oatbl, oe))) {
4309 if (!rst->major_ver) {
4310 struct OPEN_ATTR_ENRTY_32 oe0;
4312 /* Really 'oe' points to OPEN_ATTR_ENRTY_32. */
4313 memcpy(&oe0, oe, SIZEOF_OPENATTRIBUTEENTRY0);
4315 oe->bytes_per_index = oe0.bytes_per_index;
4316 oe->type = oe0.type;
4317 oe->is_dirty_pages = oe0.is_dirty_pages;
4320 oe->open_record_lsn = oe0.open_record_lsn;
4323 oe->is_attr_name = 0;
4330 check_attribute_names2:
4331 if (!rst->attr_names_len)
4332 goto trace_attribute_table;
4336 goto trace_attribute_table;
4338 /* TODO: Clear table on exit! */
4339 oe = Add2Ptr(oatbl, le16_to_cpu(ane->off));
4340 t16 = le16_to_cpu(ane->name_bytes);
4341 oe->name_len = t16 / sizeof(short);
4342 oe->ptr = ane->name;
4343 oe->is_attr_name = 2;
4344 ane = Add2Ptr(ane, sizeof(struct ATTR_NAME_ENTRY) + t16);
4347 trace_attribute_table:
4349 * If the checkpt_lsn is zero, then this is a freshly
4350 * formatted disk and we have no work to do.
4358 oatbl = init_rsttbl(bytes_per_attr_entry, 8);
4365 log->open_attr_tbl = oatbl;
4367 /* Start the analysis pass from the Checkpoint lsn. */
4368 rec_lsn = checkpt_lsn;
4370 /* Read the first lsn. */
4371 err = read_log_rec_lcb(log, checkpt_lsn, lcb_ctx_next, &lcb);
4375 /* Loop to read all subsequent records to the end of the log file. */
4376 next_log_record_analyze:
4377 err = read_next_log_rec(log, lcb, &rec_lsn);
4382 goto end_log_records_enumerate;
4385 transact_id = le32_to_cpu(frh->transact_id);
4386 rec_len = le32_to_cpu(frh->client_data_len);
4389 if (!check_log_rec(lrh, rec_len, transact_id, bytes_per_attr_entry)) {
4395 * The first lsn after the previous lsn remembered
4396 * the checkpoint is the first candidate for the rlsn.
4401 if (LfsClientRecord != frh->record_type)
4402 goto next_log_record_analyze;
4405 * Now update the Transaction Table for this transaction. If there
4406 * is no entry present or it is unallocated we allocate the entry.
4409 trtbl = init_rsttbl(sizeof(struct TRANSACTION_ENTRY),
4410 INITIAL_NUMBER_TRANSACTIONS);
4417 tr = Add2Ptr(trtbl, transact_id);
4419 if (transact_id >= bytes_per_rt(trtbl) ||
4420 tr->next != RESTART_ENTRY_ALLOCATED_LE) {
4421 tr = alloc_rsttbl_from_idx(&trtbl, transact_id);
4426 tr->transact_state = TransactionActive;
4427 tr->first_lsn = cpu_to_le64(rec_lsn);
4430 tr->prev_lsn = tr->undo_next_lsn = cpu_to_le64(rec_lsn);
4433 * If this is a compensation log record, then change
4434 * the undo_next_lsn to be the undo_next_lsn of this record.
4436 if (lrh->undo_op == cpu_to_le16(CompensationLogRecord))
4437 tr->undo_next_lsn = frh->client_undo_next_lsn;
4439 /* Dispatch to handle log record depending on type. */
4440 switch (le16_to_cpu(lrh->redo_op)) {
4441 case InitializeFileRecordSegment:
4442 case DeallocateFileRecordSegment:
4443 case WriteEndOfFileRecordSegment:
4444 case CreateAttribute:
4445 case DeleteAttribute:
4446 case UpdateResidentValue:
4447 case UpdateNonresidentValue:
4448 case UpdateMappingPairs:
4449 case SetNewAttributeSizes:
4450 case AddIndexEntryRoot:
4451 case DeleteIndexEntryRoot:
4452 case AddIndexEntryAllocation:
4453 case DeleteIndexEntryAllocation:
4454 case WriteEndOfIndexBuffer:
4455 case SetIndexEntryVcnRoot:
4456 case SetIndexEntryVcnAllocation:
4457 case UpdateFileNameRoot:
4458 case UpdateFileNameAllocation:
4459 case SetBitsInNonresidentBitMap:
4460 case ClearBitsInNonresidentBitMap:
4461 case UpdateRecordDataRoot:
4462 case UpdateRecordDataAllocation:
4463 case ZeroEndOfFileRecord:
4464 t16 = le16_to_cpu(lrh->target_attr);
4465 t64 = le64_to_cpu(lrh->target_vcn);
4466 dp = find_dp(dptbl, t16, t64);
4472 * Calculate the number of clusters per page the system
4473 * which wrote the checkpoint, possibly creating the table.
4476 t32 = (le16_to_cpu(dptbl->size) -
4477 sizeof(struct DIR_PAGE_ENTRY)) /
4480 t32 = log->clst_per_page;
4482 dptbl = init_rsttbl(struct_size(dp, page_lcns, t32),
4490 dp = alloc_rsttbl_idx(&dptbl);
4495 dp->target_attr = cpu_to_le32(t16);
4496 dp->transfer_len = cpu_to_le32(t32 << sbi->cluster_bits);
4497 dp->lcns_follow = cpu_to_le32(t32);
4498 dp->vcn = cpu_to_le64(t64 & ~((u64)t32 - 1));
4499 dp->oldest_lsn = cpu_to_le64(rec_lsn);
4503 * Copy the Lcns from the log record into the Dirty Page Entry.
4504 * TODO: For different page size support, must somehow make
4505 * whole routine a loop, case Lcns do not fit below.
4507 t16 = le16_to_cpu(lrh->lcns_follow);
4508 for (i = 0; i < t16; i++) {
4509 size_t j = (size_t)(le64_to_cpu(lrh->target_vcn) -
4510 le64_to_cpu(dp->vcn));
4511 dp->page_lcns[j + i] = lrh->page_lcns[i];
4514 goto next_log_record_analyze;
4516 case DeleteDirtyClusters: {
4518 le16_to_cpu(lrh->redo_len) / sizeof(struct LCN_RANGE);
4519 const struct LCN_RANGE *r =
4520 Add2Ptr(lrh, le16_to_cpu(lrh->redo_off));
4522 /* Loop through all of the Lcn ranges this log record. */
4523 for (i = 0; i < range_count; i++, r++) {
4524 u64 lcn0 = le64_to_cpu(r->lcn);
4525 u64 lcn_e = lcn0 + le64_to_cpu(r->len) - 1;
4528 while ((dp = enum_rstbl(dptbl, dp))) {
4531 t32 = le32_to_cpu(dp->lcns_follow);
4532 for (j = 0; j < t32; j++) {
4533 t64 = le64_to_cpu(dp->page_lcns[j]);
4534 if (t64 >= lcn0 && t64 <= lcn_e)
4535 dp->page_lcns[j] = 0;
4539 goto next_log_record_analyze;
4543 case OpenNonresidentAttribute:
4544 t16 = le16_to_cpu(lrh->target_attr);
4545 if (t16 >= bytes_per_rt(oatbl)) {
4547 * Compute how big the table needs to be.
4548 * Add 10 extra entries for some cushion.
4550 u32 new_e = t16 / le16_to_cpu(oatbl->size);
4552 new_e += 10 - le16_to_cpu(oatbl->used);
4554 oatbl = extend_rsttbl(oatbl, new_e, ~0u);
4555 log->open_attr_tbl = oatbl;
4562 /* Point to the entry being opened. */
4563 oe = alloc_rsttbl_from_idx(&oatbl, t16);
4564 log->open_attr_tbl = oatbl;
4570 /* Initialize this entry from the log record. */
4571 t16 = le16_to_cpu(lrh->redo_off);
4572 if (!rst->major_ver) {
4573 /* Convert version '0' into version '1'. */
4574 struct OPEN_ATTR_ENRTY_32 *oe0 = Add2Ptr(lrh, t16);
4576 oe->bytes_per_index = oe0->bytes_per_index;
4577 oe->type = oe0->type;
4578 oe->is_dirty_pages = oe0->is_dirty_pages;
4579 oe->name_len = 0; //oe0.name_len;
4581 oe->open_record_lsn = oe0->open_record_lsn;
4583 memcpy(oe, Add2Ptr(lrh, t16), bytes_per_attr_entry);
4586 t16 = le16_to_cpu(lrh->undo_len);
4588 oe->ptr = kmalloc(t16, GFP_NOFS);
4593 oe->name_len = t16 / sizeof(short);
4595 Add2Ptr(lrh, le16_to_cpu(lrh->undo_off)), t16);
4596 oe->is_attr_name = 1;
4599 oe->is_attr_name = 0;
4602 goto next_log_record_analyze;
4605 t16 = le16_to_cpu(lrh->target_attr);
4606 t64 = le64_to_cpu(lrh->target_vcn);
4607 dp = find_dp(dptbl, t16, t64);
4609 size_t j = le64_to_cpu(lrh->target_vcn) -
4610 le64_to_cpu(dp->vcn);
4611 if (dp->page_lcns[j])
4612 dp->page_lcns[j] = lrh->page_lcns[0];
4614 goto next_log_record_analyze;
4616 case EndTopLevelAction:
4617 tr = Add2Ptr(trtbl, transact_id);
4618 tr->prev_lsn = cpu_to_le64(rec_lsn);
4619 tr->undo_next_lsn = frh->client_undo_next_lsn;
4620 goto next_log_record_analyze;
4622 case PrepareTransaction:
4623 tr = Add2Ptr(trtbl, transact_id);
4624 tr->transact_state = TransactionPrepared;
4625 goto next_log_record_analyze;
4627 case CommitTransaction:
4628 tr = Add2Ptr(trtbl, transact_id);
4629 tr->transact_state = TransactionCommitted;
4630 goto next_log_record_analyze;
4632 case ForgetTransaction:
4633 free_rsttbl_idx(trtbl, transact_id);
4634 goto next_log_record_analyze;
4637 case OpenAttributeTableDump:
4638 case AttributeNamesDump:
4639 case DirtyPageTableDump:
4640 case TransactionTableDump:
4641 /* The following cases require no action the Analysis Pass. */
4642 goto next_log_record_analyze;
4646 * All codes will be explicitly handled.
4647 * If we see a code we do not expect, then we are trouble.
4649 goto next_log_record_analyze;
4652 end_log_records_enumerate:
4657 * Scan the Dirty Page Table and Transaction Table for
4658 * the lowest lsn, and return it as the Redo lsn.
4661 while ((dp = enum_rstbl(dptbl, dp))) {
4662 t64 = le64_to_cpu(dp->oldest_lsn);
4663 if (t64 && t64 < rlsn)
4668 while ((tr = enum_rstbl(trtbl, tr))) {
4669 t64 = le64_to_cpu(tr->first_lsn);
4670 if (t64 && t64 < rlsn)
4675 * Only proceed if the Dirty Page Table or Transaction
4676 * table are not empty.
4678 if ((!dptbl || !dptbl->total) && (!trtbl || !trtbl->total))
4681 sbi->flags |= NTFS_FLAGS_NEED_REPLAY;
4685 /* Reopen all of the attributes with dirty pages. */
4687 next_open_attribute:
4689 oe = enum_rstbl(oatbl, oe);
4693 goto next_dirty_page;
4696 oa = kzalloc(sizeof(struct OpenAttr), GFP_NOFS);
4702 inode = ntfs_iget5(sbi->sb, &oe->ref, NULL);
4706 if (is_bad_inode(inode)) {
4710 iput(&oa->ni->vfs_inode);
4714 attr = attr_create_nonres_log(sbi, oe->type, 0, oe->ptr,
4722 oa->run1 = &oa->run0;
4726 ni_oe = ntfs_i(inode);
4729 attr = ni_find_attr(ni_oe, NULL, NULL, oe->type, oe->ptr, oe->name_len,
4735 t32 = le32_to_cpu(attr->size);
4736 oa->attr = kmemdup(attr, t32, GFP_NOFS);
4740 if (!S_ISDIR(inode->i_mode)) {
4741 if (attr->type == ATTR_DATA && !attr->name_len) {
4742 oa->run1 = &ni_oe->file.run;
4746 if (attr->type == ATTR_ALLOC &&
4747 attr->name_len == ARRAY_SIZE(I30_NAME) &&
4748 !memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME))) {
4749 oa->run1 = &ni_oe->dir.alloc_run;
4754 if (attr->non_res) {
4755 u16 roff = le16_to_cpu(attr->nres.run_off);
4756 CLST svcn = le64_to_cpu(attr->nres.svcn);
4764 err = run_unpack(&oa->run0, sbi, inode->i_ino, svcn,
4765 le64_to_cpu(attr->nres.evcn), svcn,
4766 Add2Ptr(attr, roff), t32 - roff);
4774 oa->run1 = &oa->run0;
4778 if (oe->is_attr_name == 1)
4780 oe->is_attr_name = 0;
4782 oe->name_len = attr->name_len;
4784 goto next_open_attribute;
4787 * Now loop through the dirty page table to extract all of the Vcn/Lcn.
4788 * Mapping that we have, and insert it into the appropriate run.
4791 dp = enum_rstbl(dptbl, dp);
4795 oe = Add2Ptr(oatbl, le32_to_cpu(dp->target_attr));
4797 if (oe->next != RESTART_ENTRY_ALLOCATED_LE)
4798 goto next_dirty_page;
4802 goto next_dirty_page;
4805 next_dirty_page_vcn:
4807 if (i >= le32_to_cpu(dp->lcns_follow))
4808 goto next_dirty_page;
4810 vcn = le64_to_cpu(dp->vcn) + i;
4811 size = (vcn + 1) << sbi->cluster_bits;
4813 if (!dp->page_lcns[i])
4814 goto next_dirty_page_vcn;
4816 rno = ino_get(&oe->ref);
4817 if (rno <= MFT_REC_MIRR &&
4818 size < (MFT_REC_VOL + 1) * sbi->record_size &&
4819 oe->type == ATTR_DATA) {
4820 goto next_dirty_page_vcn;
4823 lcn = le64_to_cpu(dp->page_lcns[i]);
4825 if ((!run_lookup_entry(oa->run1, vcn, &lcn0, &len0, NULL) ||
4827 !run_add_entry(oa->run1, vcn, lcn, 1, false)) {
4832 if (size > le64_to_cpu(attr->nres.alloc_size)) {
4833 attr->nres.valid_size = attr->nres.data_size =
4834 attr->nres.alloc_size = cpu_to_le64(size);
4836 goto next_dirty_page_vcn;
4840 * Perform the Redo Pass, to restore all of the dirty pages to the same
4841 * contents that they had immediately before the crash. If the dirty
4842 * page table is empty, then we can skip the entire Redo Pass.
4844 if (!dptbl || !dptbl->total)
4845 goto do_undo_action;
4850 * Read the record at the Redo lsn, before falling
4851 * into common code to handle each record.
4853 err = read_log_rec_lcb(log, rlsn, lcb_ctx_next, &lcb);
4858 * Now loop to read all of our log records forwards, until
4859 * we hit the end of the file, cleaning up at the end.
4864 if (LfsClientRecord != frh->record_type)
4865 goto read_next_log_do_action;
4867 transact_id = le32_to_cpu(frh->transact_id);
4868 rec_len = le32_to_cpu(frh->client_data_len);
4871 if (!check_log_rec(lrh, rec_len, transact_id, bytes_per_attr_entry)) {
4876 /* Ignore log records that do not update pages. */
4877 if (lrh->lcns_follow)
4878 goto find_dirty_page;
4880 goto read_next_log_do_action;
4883 t16 = le16_to_cpu(lrh->target_attr);
4884 t64 = le64_to_cpu(lrh->target_vcn);
4885 dp = find_dp(dptbl, t16, t64);
4888 goto read_next_log_do_action;
4890 if (rec_lsn < le64_to_cpu(dp->oldest_lsn))
4891 goto read_next_log_do_action;
4893 t16 = le16_to_cpu(lrh->target_attr);
4894 if (t16 >= bytes_per_rt(oatbl)) {
4899 oe = Add2Ptr(oatbl, t16);
4901 if (oe->next != RESTART_ENTRY_ALLOCATED_LE) {
4914 vcn = le64_to_cpu(lrh->target_vcn);
4916 if (!run_lookup_entry(oa->run1, vcn, &lcn, NULL, NULL) ||
4917 lcn == SPARSE_LCN) {
4918 goto read_next_log_do_action;
4921 /* Point to the Redo data and get its length. */
4922 data = Add2Ptr(lrh, le16_to_cpu(lrh->redo_off));
4923 dlen = le16_to_cpu(lrh->redo_len);
4925 /* Shorten length by any Lcns which were deleted. */
4928 for (i = le16_to_cpu(lrh->lcns_follow); i; i--) {
4932 voff = le16_to_cpu(lrh->record_off) +
4933 le16_to_cpu(lrh->attr_off);
4934 voff += le16_to_cpu(lrh->cluster_off) << SECTOR_SHIFT;
4936 /* If the Vcn question is allocated, we can just get out. */
4937 j = le64_to_cpu(lrh->target_vcn) - le64_to_cpu(dp->vcn);
4938 if (dp->page_lcns[j + i - 1])
4945 * Calculate the allocated space left relative to the
4946 * log record Vcn, after removing this unallocated Vcn.
4948 alen = (i - 1) << sbi->cluster_bits;
4951 * If the update described this log record goes beyond
4952 * the allocated space, then we will have to reduce the length.
4956 else if (voff + dlen > alen)
4961 * If the resulting dlen from above is now zero,
4962 * we can skip this log record.
4964 if (!dlen && saved_len)
4965 goto read_next_log_do_action;
4967 t16 = le16_to_cpu(lrh->redo_op);
4968 if (can_skip_action(t16))
4969 goto read_next_log_do_action;
4971 /* Apply the Redo operation a common routine. */
4972 err = do_action(log, oe, lrh, t16, data, dlen, rec_len, &rec_lsn);
4976 /* Keep reading and looping back until end of file. */
4977 read_next_log_do_action:
4978 err = read_next_log_rec(log, lcb, &rec_lsn);
4979 if (!err && rec_lsn)
4980 goto do_action_next;
4986 /* Scan Transaction Table. */
4988 transaction_table_next:
4989 tr = enum_rstbl(trtbl, tr);
4991 goto undo_action_done;
4993 if (TransactionActive != tr->transact_state || !tr->undo_next_lsn) {
4994 free_rsttbl_idx(trtbl, PtrOffset(trtbl, tr));
4995 goto transaction_table_next;
4998 log->transaction_id = PtrOffset(trtbl, tr);
4999 undo_next_lsn = le64_to_cpu(tr->undo_next_lsn);
5002 * We only have to do anything if the transaction has
5003 * something its undo_next_lsn field.
5008 /* Read the first record to be undone by this transaction. */
5009 err = read_log_rec_lcb(log, undo_next_lsn, lcb_ctx_undo_next, &lcb);
5014 * Now loop to read all of our log records forwards,
5015 * until we hit the end of the file, cleaning up at the end.
5021 transact_id = le32_to_cpu(frh->transact_id);
5022 rec_len = le32_to_cpu(frh->client_data_len);
5024 if (!check_log_rec(lrh, rec_len, transact_id, bytes_per_attr_entry)) {
5029 if (lrh->undo_op == cpu_to_le16(Noop))
5030 goto read_next_log_undo_action;
5032 oe = Add2Ptr(oatbl, le16_to_cpu(lrh->target_attr));
5035 t16 = le16_to_cpu(lrh->lcns_follow);
5037 goto add_allocated_vcns;
5039 is_mapped = run_lookup_entry(oa->run1, le64_to_cpu(lrh->target_vcn),
5043 * If the mapping isn't already the table or the mapping
5044 * corresponds to a hole the mapping, we need to make sure
5045 * there is no partial page already memory.
5047 if (is_mapped && lcn != SPARSE_LCN && clen >= t16)
5048 goto add_allocated_vcns;
5050 vcn = le64_to_cpu(lrh->target_vcn);
5051 vcn &= ~(u64)(log->clst_per_page - 1);
5054 for (i = 0, vcn = le64_to_cpu(lrh->target_vcn),
5055 size = (vcn + 1) << sbi->cluster_bits;
5056 i < t16; i++, vcn += 1, size += sbi->cluster_size) {
5058 if (!attr->non_res) {
5059 if (size > le32_to_cpu(attr->res.data_size))
5060 attr->res.data_size = cpu_to_le32(size);
5062 if (size > le64_to_cpu(attr->nres.data_size))
5063 attr->nres.valid_size = attr->nres.data_size =
5064 attr->nres.alloc_size =
5069 t16 = le16_to_cpu(lrh->undo_op);
5070 if (can_skip_action(t16))
5071 goto read_next_log_undo_action;
5073 /* Point to the Redo data and get its length. */
5074 data = Add2Ptr(lrh, le16_to_cpu(lrh->undo_off));
5075 dlen = le16_to_cpu(lrh->undo_len);
5077 /* It is time to apply the undo action. */
5078 err = do_action(log, oe, lrh, t16, data, dlen, rec_len, NULL);
5080 read_next_log_undo_action:
5082 * Keep reading and looping back until we have read the
5083 * last record for this transaction.
5085 err = read_next_log_rec(log, lcb, &rec_lsn);
5090 goto undo_action_next;
5096 free_rsttbl_idx(trtbl, log->transaction_id);
5098 log->transaction_id = 0;
5100 goto transaction_table_next;
5104 ntfs_update_mftmirr(sbi, 0);
5106 sbi->flags &= ~NTFS_FLAGS_NEED_REPLAY;
5114 rh = kzalloc(log->page_size, GFP_NOFS);
5120 rh->rhdr.sign = NTFS_RSTR_SIGNATURE;
5121 rh->rhdr.fix_off = cpu_to_le16(offsetof(struct RESTART_HDR, fixups));
5122 t16 = (log->page_size >> SECTOR_SHIFT) + 1;
5123 rh->rhdr.fix_num = cpu_to_le16(t16);
5124 rh->sys_page_size = cpu_to_le32(log->page_size);
5125 rh->page_size = cpu_to_le32(log->page_size);
5127 t16 = ALIGN(offsetof(struct RESTART_HDR, fixups) + sizeof(short) * t16,
5129 rh->ra_off = cpu_to_le16(t16);
5130 rh->minor_ver = cpu_to_le16(1); // 0x1A:
5131 rh->major_ver = cpu_to_le16(1); // 0x1C:
5133 ra2 = Add2Ptr(rh, t16);
5134 memcpy(ra2, ra, sizeof(struct RESTART_AREA));
5136 ra2->client_idx[0] = 0;
5137 ra2->client_idx[1] = LFS_NO_CLIENT_LE;
5138 ra2->flags = cpu_to_le16(2);
5140 le32_add_cpu(&ra2->open_log_count, 1);
5142 ntfs_fix_pre_write(&rh->rhdr, log->page_size);
5144 err = ntfs_sb_write_run(sbi, &ni->file.run, 0, rh, log->page_size, 0);
5146 err = ntfs_sb_write_run(sbi, &log->ni->file.run, log->page_size,
5147 rh, log->page_size, 0);
5159 * Scan the Open Attribute Table to close all of
5160 * the open attributes.
5163 while ((oe = enum_rstbl(oatbl, oe))) {
5164 rno = ino_get(&oe->ref);
5166 if (oe->is_attr_name == 1) {
5172 if (oe->is_attr_name)
5179 run_close(&oa->run0);
5182 iput(&oa->ni->vfs_inode);
5190 kfree(rst_info.r_page);
5193 kfree(log->one_page_buf);
5196 sbi->flags |= NTFS_FLAGS_NEED_REPLAY;
5200 else if (log->set_dirty)
5201 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);