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