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