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