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