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