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