Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

ntfs: update attrib operations

Overhaul the attribute operations to support write access, including
full attribute list management for handling multiple MFT records, and
compressed writes.

Acked-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Hyunchul Lee <hyc.lee@gmail.com>
Signed-off-by: Namjae Jeon <linkinjeon@kernel.org>

+5280 -1563
+4152 -1351
fs/ntfs/attrib.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0-or-later 2 2 /* 3 - * attrib.c - NTFS attribute operations. Part of the Linux-NTFS project. 3 + * NTFS attribute operations. 4 4 * 5 5 * Copyright (c) 2001-2012 Anton Altaparmakov and Tuxera Inc. 6 6 * Copyright (c) 2002 Richard Russon 7 + * Copyright (c) 2025 LG Electronics Co., Ltd. 8 + * 9 + * Part of this file is based on code from the NTFS-3G. 10 + * and is copyrighted by the respective authors below: 11 + * Copyright (c) 2000-2010 Anton Altaparmakov 12 + * Copyright (c) 2002-2005 Richard Russon 13 + * Copyright (c) 2002-2008 Szabolcs Szakacsits 14 + * Copyright (c) 2004-2007 Yura Pakhuchiy 15 + * Copyright (c) 2007-2021 Jean-Pierre Andre 16 + * Copyright (c) 2010 Erik Larsson 7 17 */ 8 18 9 - #include <linux/buffer_head.h> 10 - #include <linux/sched.h> 11 - #include <linux/slab.h> 12 - #include <linux/swap.h> 13 19 #include <linux/writeback.h> 20 + #include <linux/iomap.h> 14 21 15 22 #include "attrib.h" 16 - #include "debug.h" 17 - #include "layout.h" 23 + #include "attrlist.h" 18 24 #include "lcnalloc.h" 19 - #include "malloc.h" 25 + #include "debug.h" 20 26 #include "mft.h" 21 27 #include "ntfs.h" 22 - #include "types.h" 28 + #include "iomap.h" 23 29 24 - /** 30 + __le16 AT_UNNAMED[] = { cpu_to_le16('\0') }; 31 + 32 + /* 25 33 * ntfs_map_runlist_nolock - map (a part of) a runlist of an ntfs inode 26 34 * @ni: ntfs inode for which to map (part of) a runlist 27 35 * @vcn: map runlist part containing this vcn ··· 51 43 * ntfs_map_runlist_nolock(), you will probably want to do: 52 44 * m = ctx->mrec; 53 45 * a = ctx->attr; 54 - * Assuming you cache ctx->attr in a variable @a of type ATTR_RECORD * and that 55 - * you cache ctx->mrec in a variable @m of type MFT_RECORD *. 46 + * Assuming you cache ctx->attr in a variable @a of type struct attr_record * 47 + * and that you cache ctx->mrec in a variable @m of type struct mft_record *. 56 48 * 57 49 * Return 0 on success and -errno on error. There is one special error code 58 50 * which is not an error as such. This is -ENOENT. It means that @vcn is out ··· 75 67 * - If @ctx is not NULL, the base mft record must be mapped on entry 76 68 * and it will be left mapped on return. 77 69 */ 78 - int ntfs_map_runlist_nolock(ntfs_inode *ni, VCN vcn, ntfs_attr_search_ctx *ctx) 70 + int ntfs_map_runlist_nolock(struct ntfs_inode *ni, s64 vcn, struct ntfs_attr_search_ctx *ctx) 79 71 { 80 - VCN end_vcn; 72 + s64 end_vcn; 81 73 unsigned long flags; 82 - ntfs_inode *base_ni; 83 - MFT_RECORD *m; 84 - ATTR_RECORD *a; 85 - runlist_element *rl; 86 - struct page *put_this_page = NULL; 74 + struct ntfs_inode *base_ni; 75 + struct mft_record *m; 76 + struct attr_record *a; 77 + struct runlist_element *rl; 78 + struct folio *put_this_folio = NULL; 87 79 int err = 0; 88 - bool ctx_is_temporary, ctx_needs_reset; 89 - ntfs_attr_search_ctx old_ctx = { NULL, }; 80 + bool ctx_is_temporary = false, ctx_needs_reset; 81 + struct ntfs_attr_search_ctx old_ctx = { NULL, }; 82 + size_t new_rl_count; 90 83 91 84 ntfs_debug("Mapping runlist part containing vcn 0x%llx.", 92 85 (unsigned long long)vcn); ··· 106 97 goto err_out; 107 98 } 108 99 } else { 109 - VCN allocated_size_vcn; 100 + s64 allocated_size_vcn; 110 101 111 - BUG_ON(IS_ERR(ctx->mrec)); 102 + WARN_ON(IS_ERR(ctx->mrec)); 112 103 a = ctx->attr; 113 - BUG_ON(!a->non_resident); 114 - ctx_is_temporary = false; 115 - end_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn); 104 + if (!a->non_resident) { 105 + err = -EIO; 106 + goto err_out; 107 + } 108 + end_vcn = le64_to_cpu(a->data.non_resident.highest_vcn); 116 109 read_lock_irqsave(&ni->size_lock, flags); 117 - allocated_size_vcn = ni->allocated_size >> 118 - ni->vol->cluster_size_bits; 110 + allocated_size_vcn = 111 + ntfs_bytes_to_cluster(ni->vol, ni->allocated_size); 119 112 read_unlock_irqrestore(&ni->size_lock, flags); 120 113 if (!a->data.non_resident.lowest_vcn && end_vcn <= 0) 121 114 end_vcn = allocated_size_vcn - 1; ··· 130 119 */ 131 120 if (vcn >= allocated_size_vcn || (a->type == ni->type && 132 121 a->name_length == ni->name_len && 133 - !memcmp((u8*)a + le16_to_cpu(a->name_offset), 122 + !memcmp((u8 *)a + le16_to_cpu(a->name_offset), 134 123 ni->name, ni->name_len) && 135 - sle64_to_cpu(a->data.non_resident.lowest_vcn) 124 + le64_to_cpu(a->data.non_resident.lowest_vcn) 136 125 <= vcn && end_vcn >= vcn)) 137 126 ctx_needs_reset = false; 138 127 else { ··· 148 137 */ 149 138 if (old_ctx.base_ntfs_ino && old_ctx.ntfs_ino != 150 139 old_ctx.base_ntfs_ino) { 151 - put_this_page = old_ctx.ntfs_ino->page; 152 - get_page(put_this_page); 140 + put_this_folio = old_ctx.ntfs_ino->folio; 141 + folio_get(put_this_folio); 153 142 } 154 143 /* 155 144 * Reinitialize the search context so we can lookup the ··· 167 156 err = -EIO; 168 157 goto err_out; 169 158 } 170 - BUG_ON(!ctx->attr->non_resident); 159 + WARN_ON(!ctx->attr->non_resident); 171 160 } 172 161 a = ctx->attr; 173 162 /* ··· 176 165 * we then try to map the already mapped runlist fragment and 177 166 * ntfs_mapping_pairs_decompress() fails. 178 167 */ 179 - end_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn) + 1; 168 + end_vcn = le64_to_cpu(a->data.non_resident.highest_vcn) + 1; 180 169 if (unlikely(vcn && vcn >= end_vcn)) { 181 170 err = -ENOENT; 182 171 goto err_out; 183 172 } 184 - rl = ntfs_mapping_pairs_decompress(ni->vol, a, ni->runlist.rl); 173 + rl = ntfs_mapping_pairs_decompress(ni->vol, a, &ni->runlist, &new_rl_count); 185 174 if (IS_ERR(rl)) 186 175 err = PTR_ERR(rl); 187 - else 176 + else { 188 177 ni->runlist.rl = rl; 178 + ni->runlist.count = new_rl_count; 179 + } 189 180 err_out: 190 181 if (ctx_is_temporary) { 191 182 if (likely(ctx)) ··· 216 203 ctx->base_ntfs_ino) { 217 204 unmap_extent_mft_record(ctx->ntfs_ino); 218 205 ctx->mrec = ctx->base_mrec; 219 - BUG_ON(!ctx->mrec); 206 + WARN_ON(!ctx->mrec); 220 207 } 221 208 /* 222 209 * If the old mapped inode is not the base 223 210 * inode, map it. 224 211 */ 225 212 if (old_ctx.base_ntfs_ino && 226 - old_ctx.ntfs_ino != 227 - old_ctx.base_ntfs_ino) { 213 + old_ctx.ntfs_ino != old_ctx.base_ntfs_ino) { 228 214 retry_map: 229 - ctx->mrec = map_mft_record( 230 - old_ctx.ntfs_ino); 215 + ctx->mrec = map_mft_record(old_ctx.ntfs_ino); 231 216 /* 232 217 * Something bad has happened. If out 233 218 * of memory retry till it succeeds. ··· 237 226 * search context safely. 238 227 */ 239 228 if (IS_ERR(ctx->mrec)) { 240 - if (PTR_ERR(ctx->mrec) == 241 - -ENOMEM) { 229 + if (PTR_ERR(ctx->mrec) == -ENOMEM) { 242 230 schedule(); 243 231 goto retry_map; 244 232 } else 245 233 old_ctx.ntfs_ino = 246 - old_ctx. 247 - base_ntfs_ino; 234 + old_ctx.base_ntfs_ino; 248 235 } 249 236 } 250 237 } 251 238 /* Update the changed pointers in the saved context. */ 252 239 if (ctx->mrec != old_ctx.mrec) { 253 240 if (!IS_ERR(ctx->mrec)) 254 - old_ctx.attr = (ATTR_RECORD*)( 255 - (u8*)ctx->mrec + 256 - ((u8*)old_ctx.attr - 257 - (u8*)old_ctx.mrec)); 241 + old_ctx.attr = (struct attr_record *)( 242 + (u8 *)ctx->mrec + 243 + ((u8 *)old_ctx.attr - 244 + (u8 *)old_ctx.mrec)); 258 245 old_ctx.mrec = ctx->mrec; 259 246 } 260 247 } ··· 269 260 * immediately and mark the volume dirty for chkdsk to pick up 270 261 * the pieces anyway. 271 262 */ 272 - if (put_this_page) 273 - put_page(put_this_page); 263 + if (put_this_folio) 264 + folio_put(put_this_folio); 274 265 } 275 266 return err; 276 267 } 277 268 278 - /** 269 + /* 279 270 * ntfs_map_runlist - map (a part of) a runlist of an ntfs inode 280 271 * @ni: ntfs inode for which to map (part of) a runlist 281 272 * @vcn: map runlist part containing this vcn ··· 290 281 * - This function takes the runlist lock for writing and may modify 291 282 * the runlist. 292 283 */ 293 - int ntfs_map_runlist(ntfs_inode *ni, VCN vcn) 284 + int ntfs_map_runlist(struct ntfs_inode *ni, s64 vcn) 294 285 { 295 286 int err = 0; 296 287 ··· 303 294 return err; 304 295 } 305 296 306 - /** 297 + struct runlist_element *ntfs_attr_vcn_to_rl(struct ntfs_inode *ni, s64 vcn, s64 *lcn) 298 + { 299 + struct runlist_element *rl = ni->runlist.rl; 300 + int err; 301 + bool is_retry = false; 302 + 303 + if (!rl) { 304 + err = ntfs_attr_map_whole_runlist(ni); 305 + if (err) 306 + return ERR_PTR(-ENOENT); 307 + rl = ni->runlist.rl; 308 + } 309 + 310 + remap_rl: 311 + /* Seek to element containing target vcn. */ 312 + while (rl->length && rl[1].vcn <= vcn) 313 + rl++; 314 + *lcn = ntfs_rl_vcn_to_lcn(rl, vcn); 315 + 316 + if (*lcn <= LCN_RL_NOT_MAPPED && is_retry == false) { 317 + is_retry = true; 318 + if (!ntfs_map_runlist_nolock(ni, vcn, NULL)) { 319 + rl = ni->runlist.rl; 320 + goto remap_rl; 321 + } 322 + } 323 + 324 + return rl; 325 + } 326 + 327 + /* 307 328 * ntfs_attr_vcn_to_lcn_nolock - convert a vcn into a lcn given an ntfs inode 308 329 * @ni: ntfs inode of the attribute whose runlist to search 309 330 * @vcn: vcn to convert ··· 363 324 * the lock may be dropped inside the function so you cannot rely on 364 325 * the runlist still being the same when this function returns. 365 326 */ 366 - LCN ntfs_attr_vcn_to_lcn_nolock(ntfs_inode *ni, const VCN vcn, 327 + s64 ntfs_attr_vcn_to_lcn_nolock(struct ntfs_inode *ni, const s64 vcn, 367 328 const bool write_locked) 368 329 { 369 - LCN lcn; 330 + s64 lcn; 370 331 unsigned long flags; 371 332 bool is_retry = false; 372 333 373 - BUG_ON(!ni); 374 334 ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, %s_locked.", 375 335 ni->mft_no, (unsigned long long)vcn, 376 336 write_locked ? "write" : "read"); 377 - BUG_ON(!NInoNonResident(ni)); 378 - BUG_ON(vcn < 0); 379 337 if (!ni->runlist.rl) { 380 338 read_lock_irqsave(&ni->size_lock, flags); 381 339 if (!ni->allocated_size) { ··· 426 390 return lcn; 427 391 } 428 392 429 - /** 393 + struct runlist_element *__ntfs_attr_find_vcn_nolock(struct runlist *runlist, const s64 vcn) 394 + { 395 + size_t lower_idx, upper_idx, idx; 396 + struct runlist_element *run; 397 + int rh = runlist->rl_hint; 398 + 399 + if (runlist->count <= 1) 400 + return ERR_PTR(-ENOENT); 401 + 402 + if (runlist->count - 1 > rh && runlist->rl[rh].vcn <= vcn) { 403 + if (vcn < runlist->rl[rh].vcn + runlist->rl[rh].length) 404 + return &runlist->rl[rh]; 405 + if (runlist->count - 2 == rh) 406 + return ERR_PTR(-ENOENT); 407 + 408 + lower_idx = rh + 1; 409 + } else { 410 + run = &runlist->rl[0]; 411 + if (vcn < run->vcn) 412 + return ERR_PTR(-ENOENT); 413 + else if (vcn < run->vcn + run->length) { 414 + runlist->rl_hint = 0; 415 + return run; 416 + } 417 + 418 + lower_idx = 1; 419 + } 420 + 421 + run = &runlist->rl[runlist->count - 2]; 422 + if (vcn >= run->vcn && vcn < run->vcn + run->length) { 423 + runlist->rl_hint = runlist->count - 2; 424 + return run; 425 + } 426 + if (vcn >= run->vcn + run->length) 427 + return ERR_PTR(-ENOENT); 428 + 429 + upper_idx = runlist->count - 2; 430 + 431 + while (lower_idx <= upper_idx) { 432 + idx = (lower_idx + upper_idx) >> 1; 433 + run = &runlist->rl[idx]; 434 + 435 + if (vcn < run->vcn) 436 + upper_idx = idx - 1; 437 + else if (vcn >= run->vcn + run->length) 438 + lower_idx = idx + 1; 439 + else { 440 + runlist->rl_hint = idx; 441 + return run; 442 + } 443 + } 444 + 445 + return ERR_PTR(-ENOENT); 446 + } 447 + 448 + /* 430 449 * ntfs_attr_find_vcn_nolock - find a vcn in the runlist of an ntfs inode 431 450 * @ni: ntfs inode describing the runlist to search 432 451 * @vcn: vcn to find ··· 507 416 * ntfs_attr_find_vcn_nolock(), you will probably want to do: 508 417 * m = ctx->mrec; 509 418 * a = ctx->attr; 510 - * Assuming you cache ctx->attr in a variable @a of type ATTR_RECORD * and that 511 - * you cache ctx->mrec in a variable @m of type MFT_RECORD *. 419 + * Assuming you cache ctx->attr in a variable @a of type attr_record * and that 420 + * you cache ctx->mrec in a variable @m of type struct mft_record *. 512 421 * Note you need to distinguish between the lcn of the returned runlist element 513 422 * being >= 0 and LCN_HOLE. In the later case you have to return zeroes on 514 423 * read and allocate clusters on write. 515 - * 516 - * Return the runlist element containing the @vcn on success and 517 - * ERR_PTR(-errno) on error. You need to test the return value with IS_ERR() 518 - * to decide if the return is success or failure and PTR_ERR() to get to the 519 - * error code if IS_ERR() is true. 520 - * 521 - * The possible error return codes are: 522 - * -ENOENT - No such vcn in the runlist, i.e. @vcn is out of bounds. 523 - * -ENOMEM - Not enough memory to map runlist. 524 - * -EIO - Critical error (runlist/file is corrupt, i/o error, etc). 525 - * 526 - * WARNING: If @ctx is supplied, regardless of whether success or failure is 527 - * returned, you need to check IS_ERR(@ctx->mrec) and if 'true' the @ctx 528 - * is no longer valid, i.e. you need to either call 529 - * ntfs_attr_reinit_search_ctx() or ntfs_attr_put_search_ctx() on it. 530 - * In that case PTR_ERR(@ctx->mrec) will give you the error code for 531 - * why the mapping of the old inode failed. 532 - * 533 - * Locking: - The runlist described by @ni must be locked for writing on entry 534 - * and is locked on return. Note the runlist may be modified when 535 - * needed runlist fragments need to be mapped. 536 - * - If @ctx is NULL, the base mft record of @ni must not be mapped on 537 - * entry and it will be left unmapped on return. 538 - * - If @ctx is not NULL, the base mft record must be mapped on entry 539 - * and it will be left mapped on return. 540 424 */ 541 - runlist_element *ntfs_attr_find_vcn_nolock(ntfs_inode *ni, const VCN vcn, 542 - ntfs_attr_search_ctx *ctx) 425 + struct runlist_element *ntfs_attr_find_vcn_nolock(struct ntfs_inode *ni, const s64 vcn, 426 + struct ntfs_attr_search_ctx *ctx) 543 427 { 544 428 unsigned long flags; 545 - runlist_element *rl; 429 + struct runlist_element *rl; 546 430 int err = 0; 547 431 bool is_retry = false; 548 432 549 - BUG_ON(!ni); 550 433 ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, with%s ctx.", 551 434 ni->mft_no, (unsigned long long)vcn, ctx ? "" : "out"); 552 - BUG_ON(!NInoNonResident(ni)); 553 - BUG_ON(vcn < 0); 554 435 if (!ni->runlist.rl) { 555 436 read_lock_irqsave(&ni->size_lock, flags); 556 437 if (!ni->allocated_size) { ··· 531 468 } 532 469 read_unlock_irqrestore(&ni->size_lock, flags); 533 470 } 471 + 534 472 retry_remap: 535 473 rl = ni->runlist.rl; 536 474 if (likely(rl && vcn >= rl[0].vcn)) { 537 - while (likely(rl->length)) { 538 - if (unlikely(vcn < rl[1].vcn)) { 539 - if (likely(rl->lcn >= LCN_HOLE)) { 540 - ntfs_debug("Done."); 541 - return rl; 542 - } 543 - break; 544 - } 545 - rl++; 546 - } 547 - if (likely(rl->lcn != LCN_RL_NOT_MAPPED)) { 548 - if (likely(rl->lcn == LCN_ENOENT)) 549 - err = -ENOENT; 550 - else 551 - err = -EIO; 552 - } 475 + rl = __ntfs_attr_find_vcn_nolock(&ni->runlist, vcn); 476 + if (IS_ERR(rl)) 477 + err = PTR_ERR(rl); 478 + else if (rl->lcn >= LCN_HOLE) 479 + return rl; 480 + else if (rl->lcn <= LCN_ENOENT) 481 + err = -EIO; 553 482 } 554 483 if (!err && !is_retry) { 555 484 /* 556 485 * If the search context is invalid we cannot map the unmapped 557 486 * region. 558 487 */ 559 - if (IS_ERR(ctx->mrec)) 488 + if (ctx && IS_ERR(ctx->mrec)) 560 489 err = PTR_ERR(ctx->mrec); 561 490 else { 562 491 /* ··· 570 515 return ERR_PTR(err); 571 516 } 572 517 573 - /** 518 + /* 574 519 * ntfs_attr_find - find (next) attribute in mft record 575 520 * @type: attribute type to find 576 521 * @name: attribute name to find (optional, i.e. NULL means don't care) ··· 627 572 * Warning: Never use @val when looking for attribute types which can be 628 573 * non-resident as this most likely will result in a crash! 629 574 */ 630 - static int ntfs_attr_find(const ATTR_TYPE type, const ntfschar *name, 631 - const u32 name_len, const IGNORE_CASE_BOOL ic, 632 - const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx) 575 + static int ntfs_attr_find(const __le32 type, const __le16 *name, 576 + const u32 name_len, const u32 ic, 577 + const u8 *val, const u32 val_len, struct ntfs_attr_search_ctx *ctx) 633 578 { 634 - ATTR_RECORD *a; 635 - ntfs_volume *vol = ctx->ntfs_ino->vol; 636 - ntfschar *upcase = vol->upcase; 579 + struct attr_record *a; 580 + struct ntfs_volume *vol = ctx->ntfs_ino->vol; 581 + __le16 *upcase = vol->upcase; 637 582 u32 upcase_len = vol->upcase_len; 583 + unsigned int space; 638 584 639 585 /* 640 586 * Iterate over attributes in mft record starting at @ctx->attr, or the ··· 645 589 a = ctx->attr; 646 590 ctx->is_first = false; 647 591 } else 648 - a = (ATTR_RECORD*)((u8*)ctx->attr + 592 + a = (struct attr_record *)((u8 *)ctx->attr + 649 593 le32_to_cpu(ctx->attr->length)); 650 - for (;; a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length))) { 651 - u8 *mrec_end = (u8 *)ctx->mrec + 652 - le32_to_cpu(ctx->mrec->bytes_allocated); 653 - u8 *name_end; 654 - 655 - /* check whether ATTR_RECORD wrap */ 656 - if ((u8 *)a < (u8 *)ctx->mrec) 594 + for (;; a = (struct attr_record *)((u8 *)a + le32_to_cpu(a->length))) { 595 + if ((u8 *)a < (u8 *)ctx->mrec || (u8 *)a > (u8 *)ctx->mrec + 596 + le32_to_cpu(ctx->mrec->bytes_allocated)) 657 597 break; 658 598 659 - /* check whether Attribute Record Header is within bounds */ 660 - if ((u8 *)a > mrec_end || 661 - (u8 *)a + sizeof(ATTR_RECORD) > mrec_end) 662 - break; 663 - 664 - /* check whether ATTR_RECORD's name is within bounds */ 665 - name_end = (u8 *)a + le16_to_cpu(a->name_offset) + 666 - a->name_length * sizeof(ntfschar); 667 - if (name_end > mrec_end) 599 + space = le32_to_cpu(ctx->mrec->bytes_in_use) - ((u8 *)a - (u8 *)ctx->mrec); 600 + if ((space < offsetof(struct attr_record, data.resident.reserved) + 1 || 601 + space < le32_to_cpu(a->length)) && (space < 4 || a->type != AT_END)) 668 602 break; 669 603 670 604 ctx->attr = a; 671 - if (unlikely(le32_to_cpu(a->type) > le32_to_cpu(type) || 672 - a->type == AT_END)) 605 + if (((type != AT_UNUSED) && (le32_to_cpu(a->type) > le32_to_cpu(type))) || 606 + a->type == AT_END) 673 607 return -ENOENT; 674 608 if (unlikely(!a->length)) 675 609 break; 676 - 677 - /* check whether ATTR_RECORD's length wrap */ 678 - if ((u8 *)a + le32_to_cpu(a->length) < (u8 *)a) 679 - break; 680 - /* check whether ATTR_RECORD's length is within bounds */ 681 - if ((u8 *)a + le32_to_cpu(a->length) > mrec_end) 682 - break; 683 - 610 + if (type == AT_UNUSED) 611 + return 0; 684 612 if (a->type != type) 685 613 continue; 686 614 /* 687 615 * If @name is present, compare the two names. If @name is 688 616 * missing, assume we want an unnamed attribute. 689 617 */ 690 - if (!name) { 618 + if (!name || name == AT_UNNAMED) { 691 619 /* The search failed if the found attribute is named. */ 692 620 if (a->name_length) 693 621 return -ENOENT; 694 - } else if (!ntfs_are_names_equal(name, name_len, 695 - (ntfschar*)((u8*)a + le16_to_cpu(a->name_offset)), 696 - a->name_length, ic, upcase, upcase_len)) { 697 - register int rc; 622 + } else { 623 + if (a->name_length && ((le16_to_cpu(a->name_offset) + 624 + a->name_length * sizeof(__le16)) > 625 + le32_to_cpu(a->length))) { 626 + ntfs_error(vol->sb, "Corrupt attribute name in MFT record %lld\n", 627 + (long long)ctx->ntfs_ino->mft_no); 628 + break; 629 + } 698 630 699 - rc = ntfs_collate_names(name, name_len, 700 - (ntfschar*)((u8*)a + 701 - le16_to_cpu(a->name_offset)), 702 - a->name_length, 1, IGNORE_CASE, 703 - upcase, upcase_len); 704 - /* 705 - * If @name collates before a->name, there is no 706 - * matching attribute. 707 - */ 708 - if (rc == -1) 709 - return -ENOENT; 710 - /* If the strings are not equal, continue search. */ 711 - if (rc) 712 - continue; 713 - rc = ntfs_collate_names(name, name_len, 714 - (ntfschar*)((u8*)a + 715 - le16_to_cpu(a->name_offset)), 716 - a->name_length, 1, CASE_SENSITIVE, 717 - upcase, upcase_len); 718 - if (rc == -1) 719 - return -ENOENT; 720 - if (rc) 721 - continue; 631 + if (!ntfs_are_names_equal(name, name_len, 632 + (__le16 *)((u8 *)a + le16_to_cpu(a->name_offset)), 633 + a->name_length, ic, upcase, upcase_len)) { 634 + register int rc; 635 + 636 + rc = ntfs_collate_names(name, name_len, 637 + (__le16 *)((u8 *)a + le16_to_cpu(a->name_offset)), 638 + a->name_length, 1, IGNORE_CASE, 639 + upcase, upcase_len); 640 + /* 641 + * If @name collates before a->name, there is no 642 + * matching attribute. 643 + */ 644 + if (rc == -1) 645 + return -ENOENT; 646 + /* If the strings are not equal, continue search. */ 647 + if (rc) 648 + continue; 649 + rc = ntfs_collate_names(name, name_len, 650 + (__le16 *)((u8 *)a + le16_to_cpu(a->name_offset)), 651 + a->name_length, 1, CASE_SENSITIVE, 652 + upcase, upcase_len); 653 + if (rc == -1) 654 + return -ENOENT; 655 + if (rc) 656 + continue; 657 + } 722 658 } 723 659 /* 724 660 * The names match or @name not present and attribute is ··· 723 675 else { 724 676 register int rc; 725 677 726 - rc = memcmp(val, (u8*)a + le16_to_cpu( 678 + rc = memcmp(val, (u8 *)a + le16_to_cpu( 727 679 a->data.resident.value_offset), 728 680 min_t(u32, val_len, le32_to_cpu( 729 681 a->data.resident.value_length))); ··· 734 686 if (!rc) { 735 687 register u32 avl; 736 688 737 - avl = le32_to_cpu( 738 - a->data.resident.value_length); 689 + avl = le32_to_cpu(a->data.resident.value_length); 739 690 if (val_len == avl) 740 691 return 0; 741 692 if (val_len < avl) ··· 748 701 return -EIO; 749 702 } 750 703 751 - /** 752 - * load_attribute_list - load an attribute list into memory 753 - * @vol: ntfs volume from which to read 754 - * @runlist: runlist of the attribute list 755 - * @al_start: destination buffer 756 - * @size: size of the destination buffer in bytes 757 - * @initialized_size: initialized size of the attribute list 758 - * 759 - * Walk the runlist @runlist and load all clusters from it copying them into 760 - * the linear buffer @al. The maximum number of bytes copied to @al is @size 761 - * bytes. Note, @size does not need to be a multiple of the cluster size. If 762 - * @initialized_size is less than @size, the region in @al between 763 - * @initialized_size and @size will be zeroed and not read from disk. 764 - * 765 - * Return 0 on success or -errno on error. 766 - */ 767 - int load_attribute_list(ntfs_volume *vol, runlist *runlist, u8 *al_start, 768 - const s64 size, const s64 initialized_size) 704 + void ntfs_attr_name_free(unsigned char **name) 769 705 { 770 - LCN lcn; 771 - u8 *al = al_start; 772 - u8 *al_end = al + initialized_size; 773 - runlist_element *rl; 774 - struct buffer_head *bh; 775 - struct super_block *sb; 776 - unsigned long block_size; 777 - unsigned long block, max_block; 778 - int err = 0; 779 - unsigned char block_size_bits; 780 - 781 - ntfs_debug("Entering."); 782 - if (!vol || !runlist || !al || size <= 0 || initialized_size < 0 || 783 - initialized_size > size) 784 - return -EINVAL; 785 - if (!initialized_size) { 786 - memset(al, 0, size); 787 - return 0; 706 + if (*name) { 707 + kfree(*name); 708 + *name = NULL; 788 709 } 789 - sb = vol->sb; 790 - block_size = sb->s_blocksize; 791 - block_size_bits = sb->s_blocksize_bits; 792 - down_read(&runlist->lock); 793 - rl = runlist->rl; 794 - if (!rl) { 795 - ntfs_error(sb, "Cannot read attribute list since runlist is " 796 - "missing."); 797 - goto err_out; 798 - } 799 - /* Read all clusters specified by the runlist one run at a time. */ 800 - while (rl->length) { 801 - lcn = ntfs_rl_vcn_to_lcn(rl, rl->vcn); 802 - ntfs_debug("Reading vcn = 0x%llx, lcn = 0x%llx.", 803 - (unsigned long long)rl->vcn, 804 - (unsigned long long)lcn); 805 - /* The attribute list cannot be sparse. */ 806 - if (lcn < 0) { 807 - ntfs_error(sb, "ntfs_rl_vcn_to_lcn() failed. Cannot " 808 - "read attribute list."); 809 - goto err_out; 810 - } 811 - block = lcn << vol->cluster_size_bits >> block_size_bits; 812 - /* Read the run from device in chunks of block_size bytes. */ 813 - max_block = block + (rl->length << vol->cluster_size_bits >> 814 - block_size_bits); 815 - ntfs_debug("max_block = 0x%lx.", max_block); 816 - do { 817 - ntfs_debug("Reading block = 0x%lx.", block); 818 - bh = sb_bread(sb, block); 819 - if (!bh) { 820 - ntfs_error(sb, "sb_bread() failed. Cannot " 821 - "read attribute list."); 822 - goto err_out; 823 - } 824 - if (al + block_size >= al_end) 825 - goto do_final; 826 - memcpy(al, bh->b_data, block_size); 827 - brelse(bh); 828 - al += block_size; 829 - } while (++block < max_block); 830 - rl++; 831 - } 832 - if (initialized_size < size) { 833 - initialize: 834 - memset(al_start + initialized_size, 0, size - initialized_size); 835 - } 836 - done: 837 - up_read(&runlist->lock); 838 - return err; 839 - do_final: 840 - if (al < al_end) { 841 - /* 842 - * Partial block. 843 - * 844 - * Note: The attribute list can be smaller than its allocation 845 - * by multiple clusters. This has been encountered by at least 846 - * two people running Windows XP, thus we cannot do any 847 - * truncation sanity checking here. (AIA) 848 - */ 849 - memcpy(al, bh->b_data, al_end - al); 850 - brelse(bh); 851 - if (initialized_size < size) 852 - goto initialize; 853 - goto done; 854 - } 855 - brelse(bh); 856 - /* Real overflow! */ 857 - ntfs_error(sb, "Attribute list buffer overflow. Read attribute list " 858 - "is truncated."); 859 - err_out: 860 - err = -EIO; 861 - goto done; 862 710 } 863 711 864 - /** 712 + char *ntfs_attr_name_get(const struct ntfs_volume *vol, const __le16 *uname, 713 + const int uname_len) 714 + { 715 + unsigned char *name = NULL; 716 + int name_len; 717 + 718 + name_len = ntfs_ucstonls(vol, uname, uname_len, &name, 0); 719 + if (name_len < 0) { 720 + ntfs_error(vol->sb, "ntfs_ucstonls error"); 721 + /* This function when returns -1, memory for name might 722 + * be allocated. So lets free this memory. 723 + */ 724 + ntfs_attr_name_free(&name); 725 + return NULL; 726 + 727 + } else if (name_len > 0) 728 + return name; 729 + 730 + ntfs_attr_name_free(&name); 731 + return NULL; 732 + } 733 + 734 + int load_attribute_list(struct ntfs_inode *base_ni, u8 *al_start, const s64 size) 735 + { 736 + struct inode *attr_vi = NULL; 737 + u8 *al; 738 + struct attr_list_entry *ale; 739 + 740 + if (!al_start || size <= 0) 741 + return -EINVAL; 742 + 743 + attr_vi = ntfs_attr_iget(VFS_I(base_ni), AT_ATTRIBUTE_LIST, AT_UNNAMED, 0); 744 + if (IS_ERR(attr_vi)) { 745 + ntfs_error(base_ni->vol->sb, 746 + "Failed to open an inode for Attribute list, mft = %ld", 747 + base_ni->mft_no); 748 + return PTR_ERR(attr_vi); 749 + } 750 + 751 + if (ntfs_inode_attr_pread(attr_vi, 0, size, al_start) != size) { 752 + iput(attr_vi); 753 + ntfs_error(base_ni->vol->sb, 754 + "Failed to read attribute list, mft = %ld", 755 + base_ni->mft_no); 756 + return -EIO; 757 + } 758 + iput(attr_vi); 759 + 760 + for (al = al_start; al < al_start + size; al += le16_to_cpu(ale->length)) { 761 + ale = (struct attr_list_entry *)al; 762 + if (ale->name_offset != sizeof(struct attr_list_entry)) 763 + break; 764 + if (le16_to_cpu(ale->length) <= ale->name_offset + ale->name_length || 765 + al + le16_to_cpu(ale->length) > al_start + size) 766 + break; 767 + if (ale->type == AT_UNUSED) 768 + break; 769 + if (MSEQNO_LE(ale->mft_reference) == 0) 770 + break; 771 + } 772 + if (al != al_start + size) { 773 + ntfs_error(base_ni->vol->sb, "Corrupt attribute list, mft = %ld", 774 + base_ni->mft_no); 775 + return -EIO; 776 + } 777 + return 0; 778 + } 779 + 780 + /* 865 781 * ntfs_external_attr_find - find an attribute in the attribute list of an inode 866 782 * @type: attribute type to find 867 783 * @name: attribute name to find (optional, i.e. NULL means don't care) ··· 874 864 * On actual error, ntfs_external_attr_find() returns -EIO. In this case 875 865 * @ctx->attr is undefined and in particular do not rely on it not changing. 876 866 */ 877 - static int ntfs_external_attr_find(const ATTR_TYPE type, 878 - const ntfschar *name, const u32 name_len, 879 - const IGNORE_CASE_BOOL ic, const VCN lowest_vcn, 880 - const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx) 867 + static int ntfs_external_attr_find(const __le32 type, 868 + const __le16 *name, const u32 name_len, 869 + const u32 ic, const s64 lowest_vcn, 870 + const u8 *val, const u32 val_len, struct ntfs_attr_search_ctx *ctx) 881 871 { 882 - ntfs_inode *base_ni, *ni; 883 - ntfs_volume *vol; 884 - ATTR_LIST_ENTRY *al_entry, *next_al_entry; 872 + struct ntfs_inode *base_ni = ctx->base_ntfs_ino, *ni = ctx->ntfs_ino; 873 + struct ntfs_volume *vol; 874 + struct attr_list_entry *al_entry, *next_al_entry; 885 875 u8 *al_start, *al_end; 886 - ATTR_RECORD *a; 887 - ntfschar *al_name; 876 + struct attr_record *a; 877 + __le16 *al_name; 888 878 u32 al_name_len; 879 + bool is_first_search = false; 889 880 int err = 0; 890 881 static const char *es = " Unmount and run chkdsk."; 891 882 892 - ni = ctx->ntfs_ino; 893 - base_ni = ctx->base_ntfs_ino; 894 883 ntfs_debug("Entering for inode 0x%lx, type 0x%x.", ni->mft_no, type); 895 884 if (!base_ni) { 896 885 /* First call happens with the base mft record. */ 897 886 base_ni = ctx->base_ntfs_ino = ctx->ntfs_ino; 898 887 ctx->base_mrec = ctx->mrec; 888 + ctx->mapped_base_mrec = ctx->mapped_mrec; 899 889 } 900 890 if (ni == base_ni) 901 891 ctx->base_attr = ctx->attr; ··· 904 894 vol = base_ni->vol; 905 895 al_start = base_ni->attr_list; 906 896 al_end = al_start + base_ni->attr_list_size; 907 - if (!ctx->al_entry) 908 - ctx->al_entry = (ATTR_LIST_ENTRY*)al_start; 897 + if (!ctx->al_entry) { 898 + ctx->al_entry = (struct attr_list_entry *)al_start; 899 + is_first_search = true; 900 + } 909 901 /* 910 902 * Iterate over entries in attribute list starting at @ctx->al_entry, 911 903 * or the entry following that, if @ctx->is_first is 'true'. ··· 915 903 if (ctx->is_first) { 916 904 al_entry = ctx->al_entry; 917 905 ctx->is_first = false; 918 - } else 919 - al_entry = (ATTR_LIST_ENTRY*)((u8*)ctx->al_entry + 906 + /* 907 + * If an enumeration and the first attribute is higher than 908 + * the attribute list itself, need to return the attribute list 909 + * attribute. 910 + */ 911 + if ((type == AT_UNUSED) && is_first_search && 912 + le32_to_cpu(al_entry->type) > 913 + le32_to_cpu(AT_ATTRIBUTE_LIST)) 914 + goto find_attr_list_attr; 915 + } else { 916 + /* Check for small entry */ 917 + if (((al_end - (u8 *)ctx->al_entry) < 918 + (long)offsetof(struct attr_list_entry, name)) || 919 + (le16_to_cpu(ctx->al_entry->length) & 7) || 920 + (le16_to_cpu(ctx->al_entry->length) < offsetof(struct attr_list_entry, name))) 921 + goto corrupt; 922 + 923 + al_entry = (struct attr_list_entry *)((u8 *)ctx->al_entry + 920 924 le16_to_cpu(ctx->al_entry->length)); 925 + 926 + if ((u8 *)al_entry == al_end) 927 + goto not_found; 928 + 929 + /* Preliminary check for small entry */ 930 + if ((al_end - (u8 *)al_entry) < 931 + (long)offsetof(struct attr_list_entry, name)) 932 + goto corrupt; 933 + 934 + /* 935 + * If this is an enumeration and the attribute list attribute 936 + * is the next one in the enumeration sequence, just return the 937 + * attribute list attribute from the base mft record as it is 938 + * not listed in the attribute list itself. 939 + */ 940 + if ((type == AT_UNUSED) && le32_to_cpu(ctx->al_entry->type) < 941 + le32_to_cpu(AT_ATTRIBUTE_LIST) && 942 + le32_to_cpu(al_entry->type) > 943 + le32_to_cpu(AT_ATTRIBUTE_LIST)) { 944 + find_attr_list_attr: 945 + 946 + /* Check for bogus calls. */ 947 + if (name || name_len || val || val_len || lowest_vcn) 948 + return -EINVAL; 949 + 950 + /* We want the base record. */ 951 + if (ctx->ntfs_ino != base_ni) 952 + unmap_mft_record(ctx->ntfs_ino); 953 + ctx->ntfs_ino = base_ni; 954 + ctx->mapped_mrec = ctx->mapped_base_mrec; 955 + ctx->mrec = ctx->base_mrec; 956 + ctx->is_first = true; 957 + 958 + /* Sanity checks are performed elsewhere. */ 959 + ctx->attr = (struct attr_record *)((u8 *)ctx->mrec + 960 + le16_to_cpu(ctx->mrec->attrs_offset)); 961 + 962 + /* Find the attribute list attribute. */ 963 + err = ntfs_attr_find(AT_ATTRIBUTE_LIST, NULL, 0, 964 + IGNORE_CASE, NULL, 0, ctx); 965 + 966 + /* 967 + * Setup the search context so the correct 968 + * attribute is returned next time round. 969 + */ 970 + ctx->al_entry = al_entry; 971 + ctx->is_first = true; 972 + 973 + /* Got it. Done. */ 974 + if (!err) 975 + return 0; 976 + 977 + /* Error! If other than not found return it. */ 978 + if (err != -ENOENT) 979 + return err; 980 + 981 + /* Not found?!? Absurd! */ 982 + ntfs_error(ctx->ntfs_ino->vol->sb, "Attribute list wasn't found"); 983 + return -EIO; 984 + } 985 + } 921 986 for (;; al_entry = next_al_entry) { 922 987 /* Out of bounds check. */ 923 - if ((u8*)al_entry < base_ni->attr_list || 924 - (u8*)al_entry > al_end) 988 + if ((u8 *)al_entry < base_ni->attr_list || 989 + (u8 *)al_entry > al_end) 925 990 break; /* Inode is corrupt. */ 926 991 ctx->al_entry = al_entry; 927 992 /* Catch the end of the attribute list. */ 928 - if ((u8*)al_entry == al_end) 993 + if ((u8 *)al_entry == al_end) 929 994 goto not_found; 930 - if (!al_entry->length) 931 - break; 932 - if ((u8*)al_entry + 6 > al_end || (u8*)al_entry + 933 - le16_to_cpu(al_entry->length) > al_end) 934 - break; 935 - next_al_entry = (ATTR_LIST_ENTRY*)((u8*)al_entry + 995 + 996 + if ((((u8 *)al_entry + offsetof(struct attr_list_entry, name)) > al_end) || 997 + ((u8 *)al_entry + le16_to_cpu(al_entry->length) > al_end) || 998 + (le16_to_cpu(al_entry->length) & 7) || 999 + (le16_to_cpu(al_entry->length) < 1000 + offsetof(struct attr_list_entry, name_length)) || 1001 + (al_entry->name_length && ((u8 *)al_entry + al_entry->name_offset + 1002 + al_entry->name_length * sizeof(__le16)) > al_end)) 1003 + break; /* corrupt */ 1004 + 1005 + next_al_entry = (struct attr_list_entry *)((u8 *)al_entry + 936 1006 le16_to_cpu(al_entry->length)); 937 - if (le32_to_cpu(al_entry->type) > le32_to_cpu(type)) 938 - goto not_found; 939 - if (type != al_entry->type) 940 - continue; 1007 + if (type != AT_UNUSED) { 1008 + if (le32_to_cpu(al_entry->type) > le32_to_cpu(type)) 1009 + goto not_found; 1010 + if (type != al_entry->type) 1011 + continue; 1012 + } 941 1013 /* 942 1014 * If @name is present, compare the two names. If @name is 943 1015 * missing, assume we want an unnamed attribute. 944 1016 */ 945 1017 al_name_len = al_entry->name_length; 946 - al_name = (ntfschar*)((u8*)al_entry + al_entry->name_offset); 947 - if (!name) { 1018 + al_name = (__le16 *)((u8 *)al_entry + al_entry->name_offset); 1019 + 1020 + /* 1021 + * If !@type we want the attribute represented by this 1022 + * attribute list entry. 1023 + */ 1024 + if (type == AT_UNUSED) 1025 + goto is_enumeration; 1026 + 1027 + if (!name || name == AT_UNNAMED) { 948 1028 if (al_name_len) 949 1029 goto not_found; 950 1030 } else if (!ntfs_are_names_equal(al_name, al_name_len, name, ··· 1055 951 /* If the strings are not equal, continue search. */ 1056 952 if (rc) 1057 953 continue; 1058 - /* 1059 - * FIXME: Reverse engineering showed 0, IGNORE_CASE but 1060 - * that is inconsistent with ntfs_attr_find(). The 1061 - * subsequent rc checks were also different. Perhaps I 1062 - * made a mistake in one of the two. Need to recheck 1063 - * which is correct or at least see what is going on... 1064 - * (AIA) 1065 - */ 954 + 1066 955 rc = ntfs_collate_names(name, name_len, al_name, 1067 956 al_name_len, 1, CASE_SENSITIVE, 1068 957 vol->upcase, vol->upcase_len); ··· 1070 973 * next attribute list entry still fits @lowest_vcn. Otherwise 1071 974 * we have reached the right one or the search has failed. 1072 975 */ 1073 - if (lowest_vcn && (u8*)next_al_entry >= al_start && 1074 - (u8*)next_al_entry + 6 < al_end && 1075 - (u8*)next_al_entry + le16_to_cpu( 1076 - next_al_entry->length) <= al_end && 1077 - sle64_to_cpu(next_al_entry->lowest_vcn) <= 1078 - lowest_vcn && 1079 - next_al_entry->type == al_entry->type && 1080 - next_al_entry->name_length == al_name_len && 1081 - ntfs_are_names_equal((ntfschar*)((u8*) 976 + if (lowest_vcn && (u8 *)next_al_entry >= al_start && 977 + (u8 *)next_al_entry + 6 < al_end && 978 + (u8 *)next_al_entry + le16_to_cpu( 979 + next_al_entry->length) <= al_end && 980 + le64_to_cpu(next_al_entry->lowest_vcn) <= 981 + lowest_vcn && 982 + next_al_entry->type == al_entry->type && 983 + next_al_entry->name_length == al_name_len && 984 + ntfs_are_names_equal((__le16 *)((u8 *) 1082 985 next_al_entry + 1083 986 next_al_entry->name_offset), 1084 987 next_al_entry->name_length, 1085 988 al_name, al_name_len, CASE_SENSITIVE, 1086 989 vol->upcase, vol->upcase_len)) 1087 990 continue; 991 + 992 + is_enumeration: 1088 993 if (MREF_LE(al_entry->mft_reference) == ni->mft_no) { 1089 994 if (MSEQNO_LE(al_entry->mft_reference) != ni->seq_no) { 1090 - ntfs_error(vol->sb, "Found stale mft " 1091 - "reference in attribute list " 1092 - "of base inode 0x%lx.%s", 1093 - base_ni->mft_no, es); 995 + ntfs_error(vol->sb, 996 + "Found stale mft reference in attribute list of base inode 0x%lx.%s", 997 + base_ni->mft_no, es); 1094 998 err = -EIO; 1095 999 break; 1096 1000 } ··· 1104 1006 base_ni->mft_no) { 1105 1007 ni = ctx->ntfs_ino = base_ni; 1106 1008 ctx->mrec = ctx->base_mrec; 1009 + ctx->mapped_mrec = ctx->mapped_base_mrec; 1107 1010 } else { 1108 1011 /* We want an extent record. */ 1109 1012 ctx->mrec = map_extent_mft_record(base_ni, 1110 1013 le64_to_cpu( 1111 1014 al_entry->mft_reference), &ni); 1112 1015 if (IS_ERR(ctx->mrec)) { 1113 - ntfs_error(vol->sb, "Failed to map " 1114 - "extent mft record " 1115 - "0x%lx of base inode " 1116 - "0x%lx.%s", 1117 - MREF_LE(al_entry-> 1118 - mft_reference), 1016 + ntfs_error(vol->sb, 1017 + "Failed to map extent mft record 0x%lx of base inode 0x%lx.%s", 1018 + MREF_LE(al_entry->mft_reference), 1119 1019 base_ni->mft_no, es); 1120 1020 err = PTR_ERR(ctx->mrec); 1121 1021 if (err == -ENOENT) ··· 1123 1027 break; 1124 1028 } 1125 1029 ctx->ntfs_ino = ni; 1030 + ctx->mapped_mrec = true; 1031 + 1126 1032 } 1127 - ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec + 1128 - le16_to_cpu(ctx->mrec->attrs_offset)); 1129 1033 } 1034 + a = ctx->attr = (struct attr_record *)((u8 *)ctx->mrec + 1035 + le16_to_cpu(ctx->mrec->attrs_offset)); 1130 1036 /* 1131 1037 * ctx->vfs_ino, ctx->mrec, and ctx->attr now point to the 1132 1038 * mft record containing the attribute represented by the ··· 1144 1046 * entry above, the comparison can now be optimized. So it is 1145 1047 * worth re-implementing a simplified ntfs_attr_find() here. 1146 1048 */ 1147 - a = ctx->attr; 1148 1049 /* 1149 1050 * Use a manual loop so we can still use break and continue 1150 1051 * with the same meanings as above. 1151 1052 */ 1152 1053 do_next_attr_loop: 1153 - if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec + 1054 + if ((u8 *)a < (u8 *)ctx->mrec || (u8 *)a > (u8 *)ctx->mrec + 1154 1055 le32_to_cpu(ctx->mrec->bytes_allocated)) 1155 1056 break; 1156 1057 if (a->type == AT_END) 1157 - break; 1058 + continue; 1158 1059 if (!a->length) 1159 1060 break; 1160 1061 if (al_entry->instance != a->instance) ··· 1165 1068 */ 1166 1069 if (al_entry->type != a->type) 1167 1070 break; 1168 - if (!ntfs_are_names_equal((ntfschar*)((u8*)a + 1071 + if (!ntfs_are_names_equal((__le16 *)((u8 *)a + 1169 1072 le16_to_cpu(a->name_offset)), a->name_length, 1170 1073 al_name, al_name_len, CASE_SENSITIVE, 1171 1074 vol->upcase, vol->upcase_len)) ··· 1175 1078 * If no @val specified or @val specified and it matches, we 1176 1079 * have found it! 1177 1080 */ 1178 - if (!val || (!a->non_resident && le32_to_cpu( 1081 + if ((type == AT_UNUSED) || !val || (!a->non_resident && le32_to_cpu( 1179 1082 a->data.resident.value_length) == val_len && 1180 - !memcmp((u8*)a + 1083 + !memcmp((u8 *)a + 1181 1084 le16_to_cpu(a->data.resident.value_offset), 1182 1085 val, val_len))) { 1183 1086 ntfs_debug("Done, found."); ··· 1185 1088 } 1186 1089 do_next_attr: 1187 1090 /* Proceed to the next attribute in the current mft record. */ 1188 - a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length)); 1091 + a = (struct attr_record *)((u8 *)a + le32_to_cpu(a->length)); 1189 1092 goto do_next_attr_loop; 1190 1093 } 1191 - if (!err) { 1192 - ntfs_error(vol->sb, "Base inode 0x%lx contains corrupt " 1193 - "attribute list attribute.%s", base_ni->mft_no, 1194 - es); 1195 - err = -EIO; 1196 - } 1094 + 1095 + corrupt: 1197 1096 if (ni != base_ni) { 1198 1097 if (ni) 1199 1098 unmap_extent_mft_record(ni); 1200 1099 ctx->ntfs_ino = base_ni; 1201 1100 ctx->mrec = ctx->base_mrec; 1202 1101 ctx->attr = ctx->base_attr; 1102 + ctx->mapped_mrec = ctx->mapped_base_mrec; 1203 1103 } 1104 + 1105 + if (!err) { 1106 + ntfs_error(vol->sb, 1107 + "Base inode 0x%lx contains corrupt attribute list attribute.%s", 1108 + base_ni->mft_no, es); 1109 + err = -EIO; 1110 + } 1111 + 1204 1112 if (err != -ENOMEM) 1205 1113 NVolSetErrors(vol); 1206 1114 return err; ··· 1214 1112 * If we were looking for AT_END, we reset the search context @ctx and 1215 1113 * use ntfs_attr_find() to seek to the end of the base mft record. 1216 1114 */ 1217 - if (type == AT_END) { 1115 + if (type == AT_UNUSED || type == AT_END) { 1218 1116 ntfs_attr_reinit_search_ctx(ctx); 1219 1117 return ntfs_attr_find(AT_END, name, name_len, ic, val, val_len, 1220 1118 ctx); ··· 1235 1133 if (ni != base_ni) 1236 1134 unmap_extent_mft_record(ni); 1237 1135 ctx->mrec = ctx->base_mrec; 1238 - ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec + 1136 + ctx->attr = (struct attr_record *)((u8 *)ctx->mrec + 1239 1137 le16_to_cpu(ctx->mrec->attrs_offset)); 1240 1138 ctx->is_first = true; 1241 1139 ctx->ntfs_ino = base_ni; 1242 1140 ctx->base_ntfs_ino = NULL; 1243 1141 ctx->base_mrec = NULL; 1244 1142 ctx->base_attr = NULL; 1143 + ctx->mapped_mrec = ctx->mapped_base_mrec; 1245 1144 /* 1246 1145 * In case there are multiple matches in the base mft record, need to 1247 1146 * keep enumerating until we get an attribute not found response (or ··· 1258 1155 return err; 1259 1156 } 1260 1157 1261 - /** 1158 + /* 1262 1159 * ntfs_attr_lookup - find an attribute in an ntfs inode 1263 1160 * @type: attribute type to find 1264 1161 * @name: attribute name to find (optional, i.e. NULL means don't care) ··· 1293 1190 * collates just after the attribute list entry of the attribute being searched 1294 1191 * for, i.e. if one wants to add the attribute to the mft record this is the 1295 1192 * correct place to insert its attribute list entry into. 1296 - * 1297 - * When -errno != -ENOENT, an error occurred during the lookup. @ctx->attr is 1298 - * then undefined and in particular you should not rely on it not changing. 1299 1193 */ 1300 - int ntfs_attr_lookup(const ATTR_TYPE type, const ntfschar *name, 1301 - const u32 name_len, const IGNORE_CASE_BOOL ic, 1302 - const VCN lowest_vcn, const u8 *val, const u32 val_len, 1303 - ntfs_attr_search_ctx *ctx) 1194 + int ntfs_attr_lookup(const __le32 type, const __le16 *name, 1195 + const u32 name_len, const u32 ic, 1196 + const s64 lowest_vcn, const u8 *val, const u32 val_len, 1197 + struct ntfs_attr_search_ctx *ctx) 1304 1198 { 1305 - ntfs_inode *base_ni; 1199 + struct ntfs_inode *base_ni; 1306 1200 1307 1201 ntfs_debug("Entering."); 1308 - BUG_ON(IS_ERR(ctx->mrec)); 1309 1202 if (ctx->base_ntfs_ino) 1310 1203 base_ni = ctx->base_ntfs_ino; 1311 1204 else 1312 1205 base_ni = ctx->ntfs_ino; 1313 1206 /* Sanity check, just for debugging really. */ 1314 - BUG_ON(!base_ni); 1315 - if (!NInoAttrList(base_ni) || type == AT_ATTRIBUTE_LIST) 1207 + if (!base_ni || !NInoAttrList(base_ni) || type == AT_ATTRIBUTE_LIST) 1316 1208 return ntfs_attr_find(type, name, name_len, ic, val, val_len, 1317 1209 ctx); 1318 1210 return ntfs_external_attr_find(type, name, name_len, ic, lowest_vcn, ··· 1316 1218 1317 1219 /** 1318 1220 * ntfs_attr_init_search_ctx - initialize an attribute search context 1319 - * @ctx: attribute search context to initialize 1320 - * @ni: ntfs inode with which to initialize the search context 1321 - * @mrec: mft record with which to initialize the search context 1221 + * @ctx: attribute search context to initialize 1222 + * @ni: ntfs inode with which to initialize the search context 1223 + * @mrec: mft record with which to initialize the search context 1322 1224 * 1323 1225 * Initialize the attribute search context @ctx with @ni and @mrec. 1324 1226 */ 1325 - static inline void ntfs_attr_init_search_ctx(ntfs_attr_search_ctx *ctx, 1326 - ntfs_inode *ni, MFT_RECORD *mrec) 1227 + static bool ntfs_attr_init_search_ctx(struct ntfs_attr_search_ctx *ctx, 1228 + struct ntfs_inode *ni, struct mft_record *mrec) 1327 1229 { 1328 - *ctx = (ntfs_attr_search_ctx) { 1329 - .mrec = mrec, 1330 - /* Sanity checks are performed elsewhere. */ 1331 - .attr = (ATTR_RECORD*)((u8*)mrec + 1332 - le16_to_cpu(mrec->attrs_offset)), 1333 - .is_first = true, 1334 - .ntfs_ino = ni, 1335 - }; 1230 + if (!mrec) { 1231 + mrec = map_mft_record(ni); 1232 + if (IS_ERR(mrec)) 1233 + return false; 1234 + ctx->mapped_mrec = true; 1235 + } else { 1236 + ctx->mapped_mrec = false; 1237 + } 1238 + 1239 + ctx->mrec = mrec; 1240 + /* Sanity checks are performed elsewhere. */ 1241 + ctx->attr = (struct attr_record *)((u8 *)mrec + le16_to_cpu(mrec->attrs_offset)); 1242 + ctx->is_first = true; 1243 + ctx->ntfs_ino = ni; 1244 + ctx->al_entry = NULL; 1245 + ctx->base_ntfs_ino = NULL; 1246 + ctx->base_mrec = NULL; 1247 + ctx->base_attr = NULL; 1248 + ctx->mapped_base_mrec = false; 1249 + return true; 1336 1250 } 1337 1251 1338 - /** 1252 + /* 1339 1253 * ntfs_attr_reinit_search_ctx - reinitialize an attribute search context 1340 1254 * @ctx: attribute search context to reinitialize 1341 1255 * ··· 1357 1247 * This is used when a search for a new attribute is being started to reset 1358 1248 * the search context to the beginning. 1359 1249 */ 1360 - void ntfs_attr_reinit_search_ctx(ntfs_attr_search_ctx *ctx) 1250 + void ntfs_attr_reinit_search_ctx(struct ntfs_attr_search_ctx *ctx) 1361 1251 { 1252 + bool mapped_mrec; 1253 + 1362 1254 if (likely(!ctx->base_ntfs_ino)) { 1363 1255 /* No attribute list. */ 1364 1256 ctx->is_first = true; 1365 1257 /* Sanity checks are performed elsewhere. */ 1366 - ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec + 1258 + ctx->attr = (struct attr_record *)((u8 *)ctx->mrec + 1367 1259 le16_to_cpu(ctx->mrec->attrs_offset)); 1368 1260 /* 1369 1261 * This needs resetting due to ntfs_external_attr_find() which ··· 1374 1262 ctx->al_entry = NULL; 1375 1263 return; 1376 1264 } /* Attribute list. */ 1377 - if (ctx->ntfs_ino != ctx->base_ntfs_ino) 1265 + if (ctx->ntfs_ino != ctx->base_ntfs_ino && ctx->ntfs_ino) 1378 1266 unmap_extent_mft_record(ctx->ntfs_ino); 1267 + 1268 + mapped_mrec = ctx->mapped_base_mrec; 1379 1269 ntfs_attr_init_search_ctx(ctx, ctx->base_ntfs_ino, ctx->base_mrec); 1380 - return; 1270 + ctx->mapped_mrec = mapped_mrec; 1381 1271 } 1382 1272 1383 - /** 1273 + /* 1384 1274 * ntfs_attr_get_search_ctx - allocate/initialize a new attribute search context 1385 1275 * @ni: ntfs inode with which to initialize the search context 1386 1276 * @mrec: mft record with which to initialize the search context ··· 1390 1276 * Allocate a new attribute search context, initialize it with @ni and @mrec, 1391 1277 * and return it. Return NULL if allocation failed. 1392 1278 */ 1393 - ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(ntfs_inode *ni, MFT_RECORD *mrec) 1279 + struct ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(struct ntfs_inode *ni, 1280 + struct mft_record *mrec) 1394 1281 { 1395 - ntfs_attr_search_ctx *ctx; 1282 + struct ntfs_attr_search_ctx *ctx; 1283 + bool init; 1396 1284 1397 1285 ctx = kmem_cache_alloc(ntfs_attr_ctx_cache, GFP_NOFS); 1398 - if (ctx) 1399 - ntfs_attr_init_search_ctx(ctx, ni, mrec); 1286 + if (ctx) { 1287 + init = ntfs_attr_init_search_ctx(ctx, ni, mrec); 1288 + if (init == false) { 1289 + kmem_cache_free(ntfs_attr_ctx_cache, ctx); 1290 + ctx = NULL; 1291 + } 1292 + } 1293 + 1400 1294 return ctx; 1401 1295 } 1402 1296 1403 - /** 1297 + /* 1404 1298 * ntfs_attr_put_search_ctx - release an attribute search context 1405 1299 * @ctx: attribute search context to free 1406 1300 * 1407 1301 * Release the attribute search context @ctx, unmapping an associated extent 1408 1302 * mft record if present. 1409 1303 */ 1410 - void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx) 1304 + void ntfs_attr_put_search_ctx(struct ntfs_attr_search_ctx *ctx) 1411 1305 { 1412 - if (ctx->base_ntfs_ino && ctx->ntfs_ino != ctx->base_ntfs_ino) 1413 - unmap_extent_mft_record(ctx->ntfs_ino); 1306 + if (ctx->mapped_mrec) 1307 + unmap_mft_record(ctx->ntfs_ino); 1308 + 1309 + if (ctx->mapped_base_mrec && ctx->base_ntfs_ino && 1310 + ctx->ntfs_ino != ctx->base_ntfs_ino) 1311 + unmap_extent_mft_record(ctx->base_ntfs_ino); 1414 1312 kmem_cache_free(ntfs_attr_ctx_cache, ctx); 1415 - return; 1416 1313 } 1417 1314 1418 - #ifdef NTFS_RW 1419 - 1420 - /** 1315 + /* 1421 1316 * ntfs_attr_find_in_attrdef - find an attribute in the $AttrDef system file 1422 1317 * @vol: ntfs volume to which the attribute belongs 1423 1318 * @type: attribute type which to find ··· 1436 1313 * 1437 1314 * Return the attribute type definition record if found and NULL if not found. 1438 1315 */ 1439 - static ATTR_DEF *ntfs_attr_find_in_attrdef(const ntfs_volume *vol, 1440 - const ATTR_TYPE type) 1316 + static struct attr_def *ntfs_attr_find_in_attrdef(const struct ntfs_volume *vol, 1317 + const __le32 type) 1441 1318 { 1442 - ATTR_DEF *ad; 1319 + struct attr_def *ad; 1443 1320 1444 - BUG_ON(!vol->attrdef); 1445 - BUG_ON(!type); 1446 - for (ad = vol->attrdef; (u8*)ad - (u8*)vol->attrdef < 1321 + WARN_ON(!type); 1322 + for (ad = vol->attrdef; (u8 *)ad - (u8 *)vol->attrdef < 1447 1323 vol->attrdef_size && ad->type; ++ad) { 1448 1324 /* We have not found it yet, carry on searching. */ 1449 1325 if (likely(le32_to_cpu(ad->type) < le32_to_cpu(type))) ··· 1459 1337 return NULL; 1460 1338 } 1461 1339 1462 - /** 1340 + /* 1463 1341 * ntfs_attr_size_bounds_check - check a size of an attribute type for validity 1464 1342 * @vol: ntfs volume to which the attribute belongs 1465 1343 * @type: attribute type which to check ··· 1467 1345 * 1468 1346 * Check whether the @size in bytes is valid for an attribute of @type on the 1469 1347 * ntfs volume @vol. This information is obtained from $AttrDef system file. 1470 - * 1471 - * Return 0 if valid, -ERANGE if not valid, or -ENOENT if the attribute is not 1472 - * listed in $AttrDef. 1473 1348 */ 1474 - int ntfs_attr_size_bounds_check(const ntfs_volume *vol, const ATTR_TYPE type, 1349 + int ntfs_attr_size_bounds_check(const struct ntfs_volume *vol, const __le32 type, 1475 1350 const s64 size) 1476 1351 { 1477 - ATTR_DEF *ad; 1352 + struct attr_def *ad; 1478 1353 1479 - BUG_ON(size < 0); 1354 + if (size < 0) 1355 + return -EINVAL; 1356 + 1480 1357 /* 1481 1358 * $ATTRIBUTE_LIST has a maximum size of 256kiB, but this is not 1482 1359 * listed in $AttrDef. ··· 1487 1366 if (unlikely(!ad)) 1488 1367 return -ENOENT; 1489 1368 /* Do the bounds check. */ 1490 - if (((sle64_to_cpu(ad->min_size) > 0) && 1491 - size < sle64_to_cpu(ad->min_size)) || 1492 - ((sle64_to_cpu(ad->max_size) > 0) && size > 1493 - sle64_to_cpu(ad->max_size))) 1369 + if (((le64_to_cpu(ad->min_size) > 0) && 1370 + size < le64_to_cpu(ad->min_size)) || 1371 + ((le64_to_cpu(ad->max_size) > 0) && size > 1372 + le64_to_cpu(ad->max_size))) 1494 1373 return -ERANGE; 1495 1374 return 0; 1496 1375 } 1497 1376 1498 - /** 1377 + /* 1499 1378 * ntfs_attr_can_be_non_resident - check if an attribute can be non-resident 1500 1379 * @vol: ntfs volume to which the attribute belongs 1501 1380 * @type: attribute type which to check 1502 1381 * 1503 1382 * Check whether the attribute of @type on the ntfs volume @vol is allowed to 1504 1383 * be non-resident. This information is obtained from $AttrDef system file. 1505 - * 1506 - * Return 0 if the attribute is allowed to be non-resident, -EPERM if not, and 1507 - * -ENOENT if the attribute is not listed in $AttrDef. 1508 1384 */ 1509 - int ntfs_attr_can_be_non_resident(const ntfs_volume *vol, const ATTR_TYPE type) 1385 + static int ntfs_attr_can_be_non_resident(const struct ntfs_volume *vol, 1386 + const __le32 type) 1510 1387 { 1511 - ATTR_DEF *ad; 1388 + struct attr_def *ad; 1512 1389 1513 1390 /* Find the attribute definition record in $AttrDef. */ 1514 1391 ad = ntfs_attr_find_in_attrdef(vol, type); ··· 1518 1399 return 0; 1519 1400 } 1520 1401 1521 - /** 1402 + /* 1522 1403 * ntfs_attr_can_be_resident - check if an attribute can be resident 1523 1404 * @vol: ntfs volume to which the attribute belongs 1524 1405 * @type: attribute type which to check ··· 1536 1417 * check for this here as we do not know which inode's $Bitmap is 1537 1418 * being asked about so the caller needs to special case this. 1538 1419 */ 1539 - int ntfs_attr_can_be_resident(const ntfs_volume *vol, const ATTR_TYPE type) 1420 + int ntfs_attr_can_be_resident(const struct ntfs_volume *vol, const __le32 type) 1540 1421 { 1541 1422 if (type == AT_INDEX_ALLOCATION) 1542 1423 return -EPERM; 1543 1424 return 0; 1544 1425 } 1545 1426 1546 - /** 1427 + /* 1547 1428 * ntfs_attr_record_resize - resize an attribute record 1548 1429 * @m: mft record containing attribute record 1549 1430 * @a: attribute record to resize ··· 1551 1432 * 1552 1433 * Resize the attribute record @a, i.e. the resident part of the attribute, in 1553 1434 * the mft record @m to @new_size bytes. 1554 - * 1555 - * Return 0 on success and -errno on error. The following error codes are 1556 - * defined: 1557 - * -ENOSPC - Not enough space in the mft record @m to perform the resize. 1558 - * 1559 - * Note: On error, no modifications have been performed whatsoever. 1560 - * 1561 - * Warning: If you make a record smaller without having copied all the data you 1562 - * are interested in the data may be overwritten. 1563 1435 */ 1564 - int ntfs_attr_record_resize(MFT_RECORD *m, ATTR_RECORD *a, u32 new_size) 1436 + int ntfs_attr_record_resize(struct mft_record *m, struct attr_record *a, u32 new_size) 1565 1437 { 1566 - ntfs_debug("Entering for new_size %u.", new_size); 1438 + u32 old_size, alloc_size, attr_size; 1439 + 1440 + old_size = le32_to_cpu(m->bytes_in_use); 1441 + alloc_size = le32_to_cpu(m->bytes_allocated); 1442 + attr_size = le32_to_cpu(a->length); 1443 + 1444 + ntfs_debug("Sizes: old=%u alloc=%u attr=%u new=%u\n", 1445 + (unsigned int)old_size, (unsigned int)alloc_size, 1446 + (unsigned int)attr_size, (unsigned int)new_size); 1447 + 1567 1448 /* Align to 8 bytes if it is not already done. */ 1568 1449 if (new_size & 7) 1569 1450 new_size = (new_size + 7) & ~7; 1570 1451 /* If the actual attribute length has changed, move things around. */ 1571 - if (new_size != le32_to_cpu(a->length)) { 1452 + if (new_size != attr_size) { 1572 1453 u32 new_muse = le32_to_cpu(m->bytes_in_use) - 1573 - le32_to_cpu(a->length) + new_size; 1454 + attr_size + new_size; 1574 1455 /* Not enough space in this mft record. */ 1575 1456 if (new_muse > le32_to_cpu(m->bytes_allocated)) 1576 1457 return -ENOSPC; 1458 + 1459 + if (a->type == AT_INDEX_ROOT && new_size > attr_size && 1460 + new_muse + 120 > alloc_size && old_size + 120 <= alloc_size) { 1461 + ntfs_debug("Too big struct index_root (%u > %u)\n", 1462 + new_muse, alloc_size); 1463 + return -ENOSPC; 1464 + } 1465 + 1577 1466 /* Move attributes following @a to their new location. */ 1578 - memmove((u8*)a + new_size, (u8*)a + le32_to_cpu(a->length), 1579 - le32_to_cpu(m->bytes_in_use) - ((u8*)a - 1580 - (u8*)m) - le32_to_cpu(a->length)); 1467 + memmove((u8 *)a + new_size, (u8 *)a + le32_to_cpu(a->length), 1468 + le32_to_cpu(m->bytes_in_use) - ((u8 *)a - 1469 + (u8 *)m) - attr_size); 1581 1470 /* Adjust @m to reflect the change in used space. */ 1582 1471 m->bytes_in_use = cpu_to_le32(new_muse); 1583 1472 /* Adjust @a to reflect the new size. */ 1584 - if (new_size >= offsetof(ATTR_REC, length) + sizeof(a->length)) 1473 + if (new_size >= offsetof(struct attr_record, length) + sizeof(a->length)) 1585 1474 a->length = cpu_to_le32(new_size); 1586 1475 } 1587 1476 return 0; 1588 1477 } 1589 1478 1590 - /** 1479 + /* 1591 1480 * ntfs_resident_attr_value_resize - resize the value of a resident attribute 1592 1481 * @m: mft record containing attribute record 1593 1482 * @a: attribute record whose value to resize ··· 1603 1476 * 1604 1477 * Resize the value of the attribute @a in the mft record @m to @new_size bytes. 1605 1478 * If the value is made bigger, the newly allocated space is cleared. 1606 - * 1607 - * Return 0 on success and -errno on error. The following error codes are 1608 - * defined: 1609 - * -ENOSPC - Not enough space in the mft record @m to perform the resize. 1610 - * 1611 - * Note: On error, no modifications have been performed whatsoever. 1612 - * 1613 - * Warning: If you make a record smaller without having copied all the data you 1614 - * are interested in the data may be overwritten. 1615 1479 */ 1616 - int ntfs_resident_attr_value_resize(MFT_RECORD *m, ATTR_RECORD *a, 1480 + int ntfs_resident_attr_value_resize(struct mft_record *m, struct attr_record *a, 1617 1481 const u32 new_size) 1618 1482 { 1619 1483 u32 old_size; ··· 1619 1501 */ 1620 1502 old_size = le32_to_cpu(a->data.resident.value_length); 1621 1503 if (new_size > old_size) 1622 - memset((u8*)a + le16_to_cpu(a->data.resident.value_offset) + 1504 + memset((u8 *)a + le16_to_cpu(a->data.resident.value_offset) + 1623 1505 old_size, 0, new_size - old_size); 1624 1506 /* Finally update the length of the attribute value. */ 1625 1507 a->data.resident.value_length = cpu_to_le32(new_size); 1626 1508 return 0; 1627 1509 } 1628 1510 1629 - /** 1511 + /* 1630 1512 * ntfs_attr_make_non_resident - convert a resident to a non-resident attribute 1631 1513 * @ni: ntfs inode describing the attribute to convert 1632 1514 * @data_size: size of the resident data to copy to the non-resident attribute ··· 1639 1521 * always know it. The reason we cannot simply read the size from the vfs 1640 1522 * inode i_size is that this is not necessarily uptodate. This happens when 1641 1523 * ntfs_attr_make_non_resident() is called in the ->truncate call path(s). 1642 - * 1643 - * Return 0 on success and -errno on error. The following error return codes 1644 - * are defined: 1645 - * -EPERM - The attribute is not allowed to be non-resident. 1646 - * -ENOMEM - Not enough memory. 1647 - * -ENOSPC - Not enough disk space. 1648 - * -EINVAL - Attribute not defined on the volume. 1649 - * -EIO - I/o error or other error. 1650 - * Note that -ENOSPC is also returned in the case that there is not enough 1651 - * space in the mft record to do the conversion. This can happen when the mft 1652 - * record is already very full. The caller is responsible for trying to make 1653 - * space in the mft record and trying again. FIXME: Do we need a separate 1654 - * error return code for this kind of -ENOSPC or is it always worth trying 1655 - * again in case the attribute may then fit in a resident state so no need to 1656 - * make it non-resident at all? Ho-hum... (AIA) 1657 - * 1658 - * NOTE to self: No changes in the attribute list are required to move from 1659 - * a resident to a non-resident attribute. 1660 - * 1661 - * Locking: - The caller must hold i_mutex on the inode. 1662 1524 */ 1663 - int ntfs_attr_make_non_resident(ntfs_inode *ni, const u32 data_size) 1525 + int ntfs_attr_make_non_resident(struct ntfs_inode *ni, const u32 data_size) 1664 1526 { 1665 1527 s64 new_size; 1666 1528 struct inode *vi = VFS_I(ni); 1667 - ntfs_volume *vol = ni->vol; 1668 - ntfs_inode *base_ni; 1669 - MFT_RECORD *m; 1670 - ATTR_RECORD *a; 1671 - ntfs_attr_search_ctx *ctx; 1672 - struct page *page; 1673 - runlist_element *rl; 1674 - u8 *kaddr; 1529 + struct ntfs_volume *vol = ni->vol; 1530 + struct ntfs_inode *base_ni; 1531 + struct mft_record *m; 1532 + struct attr_record *a; 1533 + struct ntfs_attr_search_ctx *ctx; 1534 + struct folio *folio; 1535 + struct runlist_element *rl; 1675 1536 unsigned long flags; 1676 1537 int mp_size, mp_ofs, name_ofs, arec_size, err, err2; 1677 1538 u32 attr_size; 1678 1539 u8 old_res_attr_flags; 1679 1540 1541 + if (NInoNonResident(ni)) { 1542 + ntfs_warning(vol->sb, 1543 + "Trying to make non-resident attribute non-resident. Aborting...\n"); 1544 + return -EINVAL; 1545 + } 1546 + 1680 1547 /* Check that the attribute is allowed to be non-resident. */ 1681 1548 err = ntfs_attr_can_be_non_resident(vol, ni->type); 1682 1549 if (unlikely(err)) { 1683 1550 if (err == -EPERM) 1684 - ntfs_debug("Attribute is not allowed to be " 1685 - "non-resident."); 1551 + ntfs_debug("Attribute is not allowed to be non-resident."); 1686 1552 else 1687 - ntfs_debug("Attribute not defined on the NTFS " 1688 - "volume!"); 1553 + ntfs_debug("Attribute not defined on the NTFS volume!"); 1689 1554 return err; 1690 1555 } 1691 - /* 1692 - * FIXME: Compressed and encrypted attributes are not supported when 1693 - * writing and we should never have gotten here for them. 1694 - */ 1695 - BUG_ON(NInoCompressed(ni)); 1696 - BUG_ON(NInoEncrypted(ni)); 1697 - /* 1698 - * The size needs to be aligned to a cluster boundary for allocation 1699 - * purposes. 1700 - */ 1701 - new_size = (data_size + vol->cluster_size - 1) & 1702 - ~(vol->cluster_size - 1); 1703 - if (new_size > 0) { 1704 - /* 1705 - * Will need the page later and since the page lock nests 1706 - * outside all ntfs locks, we need to get the page now. 1707 - */ 1708 - page = find_or_create_page(vi->i_mapping, 0, 1709 - mapping_gfp_mask(vi->i_mapping)); 1710 - if (unlikely(!page)) 1711 - return -ENOMEM; 1712 - /* Start by allocating clusters to hold the attribute value. */ 1713 - rl = ntfs_cluster_alloc(vol, 0, new_size >> 1714 - vol->cluster_size_bits, -1, DATA_ZONE, true); 1715 - if (IS_ERR(rl)) { 1716 - err = PTR_ERR(rl); 1717 - ntfs_debug("Failed to allocate cluster%s, error code " 1718 - "%i.", (new_size >> 1719 - vol->cluster_size_bits) > 1 ? "s" : "", 1720 - err); 1721 - goto page_err_out; 1722 - } 1723 - } else { 1724 - rl = NULL; 1725 - page = NULL; 1726 - } 1727 - /* Determine the size of the mapping pairs array. */ 1728 - mp_size = ntfs_get_size_for_mapping_pairs(vol, rl, 0, -1); 1729 - if (unlikely(mp_size < 0)) { 1730 - err = mp_size; 1731 - ntfs_debug("Failed to get size for mapping pairs array, error " 1732 - "code %i.", err); 1733 - goto rl_err_out; 1734 - } 1735 - down_write(&ni->runlist.lock); 1556 + 1557 + if (NInoEncrypted(ni)) 1558 + return -EIO; 1559 + 1736 1560 if (!NInoAttr(ni)) 1737 1561 base_ni = ni; 1738 1562 else ··· 1700 1640 } 1701 1641 m = ctx->mrec; 1702 1642 a = ctx->attr; 1703 - BUG_ON(NInoNonResident(ni)); 1704 - BUG_ON(a->non_resident); 1643 + 1644 + /* 1645 + * The size needs to be aligned to a cluster boundary for allocation 1646 + * purposes. 1647 + */ 1648 + new_size = (data_size + vol->cluster_size - 1) & 1649 + ~(vol->cluster_size - 1); 1650 + if (new_size > 0) { 1651 + if ((a->flags & ATTR_COMPRESSION_MASK) == ATTR_IS_COMPRESSED) { 1652 + /* must allocate full compression blocks */ 1653 + new_size = 1654 + ((new_size - 1) | 1655 + ((1L << (STANDARD_COMPRESSION_UNIT + 1656 + vol->cluster_size_bits)) - 1)) + 1; 1657 + } 1658 + 1659 + /* 1660 + * Will need folio later and since folio lock nests 1661 + * outside all ntfs locks, we need to get the folio now. 1662 + */ 1663 + folio = __filemap_get_folio(vi->i_mapping, 0, 1664 + FGP_CREAT | FGP_LOCK, 1665 + mapping_gfp_mask(vi->i_mapping)); 1666 + if (IS_ERR(folio)) { 1667 + err = -ENOMEM; 1668 + goto err_out; 1669 + } 1670 + 1671 + /* Start by allocating clusters to hold the attribute value. */ 1672 + rl = ntfs_cluster_alloc(vol, 0, 1673 + ntfs_bytes_to_cluster(vol, new_size), 1674 + -1, DATA_ZONE, true, false, false); 1675 + if (IS_ERR(rl)) { 1676 + err = PTR_ERR(rl); 1677 + ntfs_debug("Failed to allocate cluster%s, error code %i.", 1678 + ntfs_bytes_to_cluster(vol, new_size) > 1 ? "s" : "", 1679 + err); 1680 + goto folio_err_out; 1681 + } 1682 + } else { 1683 + rl = NULL; 1684 + folio = NULL; 1685 + } 1686 + 1687 + down_write(&ni->runlist.lock); 1688 + /* Determine the size of the mapping pairs array. */ 1689 + mp_size = ntfs_get_size_for_mapping_pairs(vol, rl, 0, -1, -1); 1690 + if (unlikely(mp_size < 0)) { 1691 + err = mp_size; 1692 + ntfs_debug("Failed to get size for mapping pairs array, error code %i.\n", err); 1693 + goto rl_err_out; 1694 + } 1695 + 1696 + if (NInoNonResident(ni) || a->non_resident) { 1697 + err = -EIO; 1698 + goto rl_err_out; 1699 + } 1700 + 1705 1701 /* 1706 1702 * Calculate new offsets for the name and the mapping pairs array. 1707 1703 */ 1708 1704 if (NInoSparse(ni) || NInoCompressed(ni)) 1709 - name_ofs = (offsetof(ATTR_REC, 1705 + name_ofs = (offsetof(struct attr_record, 1710 1706 data.non_resident.compressed_size) + 1711 1707 sizeof(a->data.non_resident.compressed_size) + 1712 1708 7) & ~7; 1713 1709 else 1714 - name_ofs = (offsetof(ATTR_REC, 1710 + name_ofs = (offsetof(struct attr_record, 1715 1711 data.non_resident.compressed_size) + 7) & ~7; 1716 - mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7; 1712 + mp_ofs = (name_ofs + a->name_length * sizeof(__le16) + 7) & ~7; 1717 1713 /* 1718 1714 * Determine the size of the resident part of the now non-resident 1719 1715 * attribute record. 1720 1716 */ 1721 1717 arec_size = (mp_ofs + mp_size + 7) & ~7; 1722 1718 /* 1723 - * If the page is not uptodate bring it uptodate by copying from the 1719 + * If the folio is not uptodate bring it uptodate by copying from the 1724 1720 * attribute value. 1725 1721 */ 1726 1722 attr_size = le32_to_cpu(a->data.resident.value_length); 1727 - BUG_ON(attr_size != data_size); 1728 - if (page && !PageUptodate(page)) { 1729 - kaddr = kmap_atomic(page); 1730 - memcpy(kaddr, (u8*)a + 1723 + WARN_ON(attr_size != data_size); 1724 + if (folio && !folio_test_uptodate(folio)) { 1725 + folio_fill_tail(folio, 0, (u8 *)a + 1731 1726 le16_to_cpu(a->data.resident.value_offset), 1732 1727 attr_size); 1733 - memset(kaddr + attr_size, 0, PAGE_SIZE - attr_size); 1734 - kunmap_atomic(kaddr); 1735 - flush_dcache_page(page); 1736 - SetPageUptodate(page); 1728 + folio_mark_uptodate(folio); 1737 1729 } 1730 + 1738 1731 /* Backup the attribute flag. */ 1739 1732 old_res_attr_flags = a->data.resident.flags; 1740 1733 /* Resize the resident part of the attribute record. */ 1741 1734 err = ntfs_attr_record_resize(m, a, arec_size); 1742 1735 if (unlikely(err)) 1743 - goto err_out; 1736 + goto rl_err_out; 1737 + 1744 1738 /* 1745 1739 * Convert the resident part of the attribute record to describe a 1746 1740 * non-resident attribute. ··· 1802 1688 a->non_resident = 1; 1803 1689 /* Move the attribute name if it exists and update the offset. */ 1804 1690 if (a->name_length) 1805 - memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset), 1806 - a->name_length * sizeof(ntfschar)); 1691 + memmove((u8 *)a + name_ofs, (u8 *)a + le16_to_cpu(a->name_offset), 1692 + a->name_length * sizeof(__le16)); 1807 1693 a->name_offset = cpu_to_le16(name_ofs); 1808 1694 /* Setup the fields specific to non-resident attributes. */ 1809 1695 a->data.non_resident.lowest_vcn = 0; 1810 - a->data.non_resident.highest_vcn = cpu_to_sle64((new_size - 1) >> 1811 - vol->cluster_size_bits); 1696 + a->data.non_resident.highest_vcn = 1697 + cpu_to_le64(ntfs_bytes_to_cluster(vol, new_size - 1)); 1812 1698 a->data.non_resident.mapping_pairs_offset = cpu_to_le16(mp_ofs); 1813 1699 memset(&a->data.non_resident.reserved, 0, 1814 1700 sizeof(a->data.non_resident.reserved)); 1815 - a->data.non_resident.allocated_size = cpu_to_sle64(new_size); 1701 + a->data.non_resident.allocated_size = cpu_to_le64(new_size); 1816 1702 a->data.non_resident.data_size = 1817 1703 a->data.non_resident.initialized_size = 1818 - cpu_to_sle64(attr_size); 1704 + cpu_to_le64(attr_size); 1819 1705 if (NInoSparse(ni) || NInoCompressed(ni)) { 1820 1706 a->data.non_resident.compression_unit = 0; 1821 1707 if (NInoCompressed(ni) || vol->major_ver < 3) ··· 1825 1711 } else 1826 1712 a->data.non_resident.compression_unit = 0; 1827 1713 /* Generate the mapping pairs array into the attribute record. */ 1828 - err = ntfs_mapping_pairs_build(vol, (u8*)a + mp_ofs, 1829 - arec_size - mp_ofs, rl, 0, -1, NULL); 1714 + err = ntfs_mapping_pairs_build(vol, (u8 *)a + mp_ofs, 1715 + arec_size - mp_ofs, rl, 0, -1, NULL, NULL, NULL); 1830 1716 if (unlikely(err)) { 1831 - ntfs_debug("Failed to build mapping pairs, error code %i.", 1717 + ntfs_error(vol->sb, "Failed to build mapping pairs, error code %i.", 1832 1718 err); 1833 1719 goto undo_err_out; 1834 1720 } 1721 + 1835 1722 /* Setup the in-memory attribute structure to be non-resident. */ 1836 1723 ni->runlist.rl = rl; 1724 + if (rl) { 1725 + for (ni->runlist.count = 1; rl->length != 0; rl++) 1726 + ni->runlist.count++; 1727 + } else 1728 + ni->runlist.count = 0; 1837 1729 write_lock_irqsave(&ni->size_lock, flags); 1838 1730 ni->allocated_size = new_size; 1839 1731 if (NInoSparse(ni) || NInoCompressed(ni)) { 1840 1732 ni->itype.compressed.size = ni->allocated_size; 1841 1733 if (a->data.non_resident.compression_unit) { 1842 - ni->itype.compressed.block_size = 1U << (a->data. 1843 - non_resident.compression_unit + 1844 - vol->cluster_size_bits); 1734 + ni->itype.compressed.block_size = 1U << 1735 + (a->data.non_resident.compression_unit + 1736 + vol->cluster_size_bits); 1845 1737 ni->itype.compressed.block_size_bits = 1846 1738 ffs(ni->itype.compressed.block_size) - 1847 1739 1; ··· 1869 1749 * this switch, which is another reason to do this last. 1870 1750 */ 1871 1751 NInoSetNonResident(ni); 1752 + NInoSetFullyMapped(ni); 1872 1753 /* Mark the mft record dirty, so it gets written back. */ 1873 - flush_dcache_mft_record_page(ctx->ntfs_ino); 1874 1754 mark_mft_record_dirty(ctx->ntfs_ino); 1875 1755 ntfs_attr_put_search_ctx(ctx); 1876 1756 unmap_mft_record(base_ni); 1877 1757 up_write(&ni->runlist.lock); 1878 - if (page) { 1879 - set_page_dirty(page); 1880 - unlock_page(page); 1881 - put_page(page); 1758 + if (folio) { 1759 + iomap_dirty_folio(vi->i_mapping, folio); 1760 + folio_unlock(folio); 1761 + folio_put(folio); 1882 1762 } 1883 1763 ntfs_debug("Done."); 1884 1764 return 0; ··· 1886 1766 /* Convert the attribute back into a resident attribute. */ 1887 1767 a->non_resident = 0; 1888 1768 /* Move the attribute name if it exists and update the offset. */ 1889 - name_ofs = (offsetof(ATTR_RECORD, data.resident.reserved) + 1769 + name_ofs = (offsetof(struct attr_record, data.resident.reserved) + 1890 1770 sizeof(a->data.resident.reserved) + 7) & ~7; 1891 1771 if (a->name_length) 1892 - memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset), 1893 - a->name_length * sizeof(ntfschar)); 1894 - mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7; 1772 + memmove((u8 *)a + name_ofs, (u8 *)a + le16_to_cpu(a->name_offset), 1773 + a->name_length * sizeof(__le16)); 1774 + mp_ofs = (name_ofs + a->name_length * sizeof(__le16) + 7) & ~7; 1895 1775 a->name_offset = cpu_to_le16(name_ofs); 1896 1776 arec_size = (mp_ofs + attr_size + 7) & ~7; 1897 1777 /* Resize the resident part of the attribute record. */ ··· 1902 1782 * could happen in theory), but deal with it as well as we can. 1903 1783 * If the old size is too small, truncate the attribute, 1904 1784 * otherwise simply give it a larger allocated size. 1905 - * FIXME: Should check whether chkdsk complains when the 1906 - * allocated size is much bigger than the resident value size. 1907 1785 */ 1908 1786 arec_size = le32_to_cpu(a->length); 1909 1787 if ((mp_ofs + attr_size) > arec_size) { 1910 1788 err2 = attr_size; 1911 1789 attr_size = arec_size - mp_ofs; 1912 - ntfs_error(vol->sb, "Failed to undo partial resident " 1913 - "to non-resident attribute " 1914 - "conversion. Truncating inode 0x%lx, " 1915 - "attribute type 0x%x from %i bytes to " 1916 - "%i bytes to maintain metadata " 1917 - "consistency. THIS MEANS YOU ARE " 1918 - "LOSING %i BYTES DATA FROM THIS %s.", 1790 + ntfs_error(vol->sb, 1791 + "Failed to undo partial resident to non-resident attribute conversion. Truncating inode 0x%lx, attribute type 0x%x from %i bytes to %i bytes to maintain metadata consistency. THIS MEANS YOU ARE LOSING %i BYTES DATA FROM THIS %s.", 1919 1792 vi->i_ino, 1920 - (unsigned)le32_to_cpu(ni->type), 1793 + (unsigned int)le32_to_cpu(ni->type), 1921 1794 err2, attr_size, err2 - attr_size, 1922 1795 ((ni->type == AT_DATA) && 1923 - !ni->name_len) ? "FILE": "ATTRIBUTE"); 1796 + !ni->name_len) ? "FILE" : "ATTRIBUTE"); 1924 1797 write_lock_irqsave(&ni->size_lock, flags); 1925 1798 ni->initialized_size = attr_size; 1926 1799 i_size_write(vi, attr_size); ··· 1926 1813 a->data.resident.flags = old_res_attr_flags; 1927 1814 memset(&a->data.resident.reserved, 0, 1928 1815 sizeof(a->data.resident.reserved)); 1929 - /* Copy the data from the page back to the attribute value. */ 1930 - if (page) { 1931 - kaddr = kmap_atomic(page); 1932 - memcpy((u8*)a + mp_ofs, kaddr, attr_size); 1933 - kunmap_atomic(kaddr); 1934 - } 1816 + /* Copy the data from folio back to the attribute value. */ 1817 + if (folio) 1818 + memcpy_from_folio((u8 *)a + mp_ofs, folio, 0, attr_size); 1935 1819 /* Setup the allocated size in the ntfs inode in case it changed. */ 1936 1820 write_lock_irqsave(&ni->size_lock, flags); 1937 1821 ni->allocated_size = arec_size - mp_ofs; 1938 1822 write_unlock_irqrestore(&ni->size_lock, flags); 1939 1823 /* Mark the mft record dirty, so it gets written back. */ 1940 - flush_dcache_mft_record_page(ctx->ntfs_ino); 1941 1824 mark_mft_record_dirty(ctx->ntfs_ino); 1825 + rl_err_out: 1826 + up_write(&ni->runlist.lock); 1827 + if (rl) { 1828 + if (ntfs_cluster_free_from_rl(vol, rl) < 0) { 1829 + ntfs_error(vol->sb, 1830 + "Failed to release allocated cluster(s) in error code path. Run chkdsk to recover the lost cluster(s)."); 1831 + NVolSetErrors(vol); 1832 + } 1833 + kvfree(rl); 1834 + folio_err_out: 1835 + folio_unlock(folio); 1836 + folio_put(folio); 1837 + } 1942 1838 err_out: 1943 1839 if (ctx) 1944 1840 ntfs_attr_put_search_ctx(ctx); 1945 1841 if (m) 1946 1842 unmap_mft_record(base_ni); 1947 1843 ni->runlist.rl = NULL; 1948 - up_write(&ni->runlist.lock); 1949 - rl_err_out: 1950 - if (rl) { 1951 - if (ntfs_cluster_free_from_rl(vol, rl) < 0) { 1952 - ntfs_error(vol->sb, "Failed to release allocated " 1953 - "cluster(s) in error code path. Run " 1954 - "chkdsk to recover the lost " 1955 - "cluster(s)."); 1956 - NVolSetErrors(vol); 1957 - } 1958 - ntfs_free(rl); 1959 - page_err_out: 1960 - unlock_page(page); 1961 - put_page(page); 1962 - } 1844 + 1963 1845 if (err == -EINVAL) 1964 1846 err = -EIO; 1965 1847 return err; 1966 1848 } 1967 1849 1968 - /** 1969 - * ntfs_attr_extend_allocation - extend the allocated space of an attribute 1970 - * @ni: ntfs inode of the attribute whose allocation to extend 1971 - * @new_alloc_size: new size in bytes to which to extend the allocation to 1972 - * @new_data_size: new size in bytes to which to extend the data to 1973 - * @data_start: beginning of region which is required to be non-sparse 1974 - * 1975 - * Extend the allocated space of an attribute described by the ntfs inode @ni 1976 - * to @new_alloc_size bytes. If @data_start is -1, the whole extension may be 1977 - * implemented as a hole in the file (as long as both the volume and the ntfs 1978 - * inode @ni have sparse support enabled). If @data_start is >= 0, then the 1979 - * region between the old allocated size and @data_start - 1 may be made sparse 1980 - * but the regions between @data_start and @new_alloc_size must be backed by 1981 - * actual clusters. 1982 - * 1983 - * If @new_data_size is -1, it is ignored. If it is >= 0, then the data size 1984 - * of the attribute is extended to @new_data_size. Note that the i_size of the 1985 - * vfs inode is not updated. Only the data size in the base attribute record 1986 - * is updated. The caller has to update i_size separately if this is required. 1987 - * WARNING: It is a BUG() for @new_data_size to be smaller than the old data 1988 - * size as well as for @new_data_size to be greater than @new_alloc_size. 1989 - * 1990 - * For resident attributes this involves resizing the attribute record and if 1991 - * necessary moving it and/or other attributes into extent mft records and/or 1992 - * converting the attribute to a non-resident attribute which in turn involves 1993 - * extending the allocation of a non-resident attribute as described below. 1994 - * 1995 - * For non-resident attributes this involves allocating clusters in the data 1996 - * zone on the volume (except for regions that are being made sparse) and 1997 - * extending the run list to describe the allocated clusters as well as 1998 - * updating the mapping pairs array of the attribute. This in turn involves 1999 - * resizing the attribute record and if necessary moving it and/or other 2000 - * attributes into extent mft records and/or splitting the attribute record 2001 - * into multiple extent attribute records. 2002 - * 2003 - * Also, the attribute list attribute is updated if present and in some of the 2004 - * above cases (the ones where extent mft records/attributes come into play), 2005 - * an attribute list attribute is created if not already present. 2006 - * 2007 - * Return the new allocated size on success and -errno on error. In the case 2008 - * that an error is encountered but a partial extension at least up to 2009 - * @data_start (if present) is possible, the allocation is partially extended 2010 - * and this is returned. This means the caller must check the returned size to 2011 - * determine if the extension was partial. If @data_start is -1 then partial 2012 - * allocations are not performed. 2013 - * 2014 - * WARNING: Do not call ntfs_attr_extend_allocation() for $MFT/$DATA. 2015 - * 2016 - * Locking: This function takes the runlist lock of @ni for writing as well as 2017 - * locking the mft record of the base ntfs inode. These locks are maintained 2018 - * throughout execution of the function. These locks are required so that the 2019 - * attribute can be resized safely and so that it can for example be converted 2020 - * from resident to non-resident safely. 2021 - * 2022 - * TODO: At present attribute list attribute handling is not implemented. 2023 - * 2024 - * TODO: At present it is not safe to call this function for anything other 2025 - * than the $DATA attribute(s) of an uncompressed and unencrypted file. 2026 - */ 2027 - s64 ntfs_attr_extend_allocation(ntfs_inode *ni, s64 new_alloc_size, 2028 - const s64 new_data_size, const s64 data_start) 2029 - { 2030 - VCN vcn; 2031 - s64 ll, allocated_size, start = data_start; 2032 - struct inode *vi = VFS_I(ni); 2033 - ntfs_volume *vol = ni->vol; 2034 - ntfs_inode *base_ni; 2035 - MFT_RECORD *m; 2036 - ATTR_RECORD *a; 2037 - ntfs_attr_search_ctx *ctx; 2038 - runlist_element *rl, *rl2; 2039 - unsigned long flags; 2040 - int err, mp_size; 2041 - u32 attr_len = 0; /* Silence stupid gcc warning. */ 2042 - bool mp_rebuilt; 2043 - 2044 - #ifdef DEBUG 2045 - read_lock_irqsave(&ni->size_lock, flags); 2046 - allocated_size = ni->allocated_size; 2047 - read_unlock_irqrestore(&ni->size_lock, flags); 2048 - ntfs_debug("Entering for i_ino 0x%lx, attribute type 0x%x, " 2049 - "old_allocated_size 0x%llx, " 2050 - "new_allocated_size 0x%llx, new_data_size 0x%llx, " 2051 - "data_start 0x%llx.", vi->i_ino, 2052 - (unsigned)le32_to_cpu(ni->type), 2053 - (unsigned long long)allocated_size, 2054 - (unsigned long long)new_alloc_size, 2055 - (unsigned long long)new_data_size, 2056 - (unsigned long long)start); 2057 - #endif 2058 - retry_extend: 2059 - /* 2060 - * For non-resident attributes, @start and @new_size need to be aligned 2061 - * to cluster boundaries for allocation purposes. 2062 - */ 2063 - if (NInoNonResident(ni)) { 2064 - if (start > 0) 2065 - start &= ~(s64)vol->cluster_size_mask; 2066 - new_alloc_size = (new_alloc_size + vol->cluster_size - 1) & 2067 - ~(s64)vol->cluster_size_mask; 2068 - } 2069 - BUG_ON(new_data_size >= 0 && new_data_size > new_alloc_size); 2070 - /* Check if new size is allowed in $AttrDef. */ 2071 - err = ntfs_attr_size_bounds_check(vol, ni->type, new_alloc_size); 2072 - if (unlikely(err)) { 2073 - /* Only emit errors when the write will fail completely. */ 2074 - read_lock_irqsave(&ni->size_lock, flags); 2075 - allocated_size = ni->allocated_size; 2076 - read_unlock_irqrestore(&ni->size_lock, flags); 2077 - if (start < 0 || start >= allocated_size) { 2078 - if (err == -ERANGE) { 2079 - ntfs_error(vol->sb, "Cannot extend allocation " 2080 - "of inode 0x%lx, attribute " 2081 - "type 0x%x, because the new " 2082 - "allocation would exceed the " 2083 - "maximum allowed size for " 2084 - "this attribute type.", 2085 - vi->i_ino, (unsigned) 2086 - le32_to_cpu(ni->type)); 2087 - } else { 2088 - ntfs_error(vol->sb, "Cannot extend allocation " 2089 - "of inode 0x%lx, attribute " 2090 - "type 0x%x, because this " 2091 - "attribute type is not " 2092 - "defined on the NTFS volume. " 2093 - "Possible corruption! You " 2094 - "should run chkdsk!", 2095 - vi->i_ino, (unsigned) 2096 - le32_to_cpu(ni->type)); 2097 - } 2098 - } 2099 - /* Translate error code to be POSIX conformant for write(2). */ 2100 - if (err == -ERANGE) 2101 - err = -EFBIG; 2102 - else 2103 - err = -EIO; 2104 - return err; 2105 - } 2106 - if (!NInoAttr(ni)) 2107 - base_ni = ni; 2108 - else 2109 - base_ni = ni->ext.base_ntfs_ino; 2110 - /* 2111 - * We will be modifying both the runlist (if non-resident) and the mft 2112 - * record so lock them both down. 2113 - */ 2114 - down_write(&ni->runlist.lock); 2115 - m = map_mft_record(base_ni); 2116 - if (IS_ERR(m)) { 2117 - err = PTR_ERR(m); 2118 - m = NULL; 2119 - ctx = NULL; 2120 - goto err_out; 2121 - } 2122 - ctx = ntfs_attr_get_search_ctx(base_ni, m); 2123 - if (unlikely(!ctx)) { 2124 - err = -ENOMEM; 2125 - goto err_out; 2126 - } 2127 - read_lock_irqsave(&ni->size_lock, flags); 2128 - allocated_size = ni->allocated_size; 2129 - read_unlock_irqrestore(&ni->size_lock, flags); 2130 - /* 2131 - * If non-resident, seek to the last extent. If resident, there is 2132 - * only one extent, so seek to that. 2133 - */ 2134 - vcn = NInoNonResident(ni) ? allocated_size >> vol->cluster_size_bits : 2135 - 0; 2136 - /* 2137 - * Abort if someone did the work whilst we waited for the locks. If we 2138 - * just converted the attribute from resident to non-resident it is 2139 - * likely that exactly this has happened already. We cannot quite 2140 - * abort if we need to update the data size. 2141 - */ 2142 - if (unlikely(new_alloc_size <= allocated_size)) { 2143 - ntfs_debug("Allocated size already exceeds requested size."); 2144 - new_alloc_size = allocated_size; 2145 - if (new_data_size < 0) 2146 - goto done; 2147 - /* 2148 - * We want the first attribute extent so that we can update the 2149 - * data size. 2150 - */ 2151 - vcn = 0; 2152 - } 2153 - err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 2154 - CASE_SENSITIVE, vcn, NULL, 0, ctx); 2155 - if (unlikely(err)) { 2156 - if (err == -ENOENT) 2157 - err = -EIO; 2158 - goto err_out; 2159 - } 2160 - m = ctx->mrec; 2161 - a = ctx->attr; 2162 - /* Use goto to reduce indentation. */ 2163 - if (a->non_resident) 2164 - goto do_non_resident_extend; 2165 - BUG_ON(NInoNonResident(ni)); 2166 - /* The total length of the attribute value. */ 2167 - attr_len = le32_to_cpu(a->data.resident.value_length); 2168 - /* 2169 - * Extend the attribute record to be able to store the new attribute 2170 - * size. ntfs_attr_record_resize() will not do anything if the size is 2171 - * not changing. 2172 - */ 2173 - if (new_alloc_size < vol->mft_record_size && 2174 - !ntfs_attr_record_resize(m, a, 2175 - le16_to_cpu(a->data.resident.value_offset) + 2176 - new_alloc_size)) { 2177 - /* The resize succeeded! */ 2178 - write_lock_irqsave(&ni->size_lock, flags); 2179 - ni->allocated_size = le32_to_cpu(a->length) - 2180 - le16_to_cpu(a->data.resident.value_offset); 2181 - write_unlock_irqrestore(&ni->size_lock, flags); 2182 - if (new_data_size >= 0) { 2183 - BUG_ON(new_data_size < attr_len); 2184 - a->data.resident.value_length = 2185 - cpu_to_le32((u32)new_data_size); 2186 - } 2187 - goto flush_done; 2188 - } 2189 - /* 2190 - * We have to drop all the locks so we can call 2191 - * ntfs_attr_make_non_resident(). This could be optimised by try- 2192 - * locking the first page cache page and only if that fails dropping 2193 - * the locks, locking the page, and redoing all the locking and 2194 - * lookups. While this would be a huge optimisation, it is not worth 2195 - * it as this is definitely a slow code path. 2196 - */ 2197 - ntfs_attr_put_search_ctx(ctx); 2198 - unmap_mft_record(base_ni); 2199 - up_write(&ni->runlist.lock); 2200 - /* 2201 - * Not enough space in the mft record, try to make the attribute 2202 - * non-resident and if successful restart the extension process. 2203 - */ 2204 - err = ntfs_attr_make_non_resident(ni, attr_len); 2205 - if (likely(!err)) 2206 - goto retry_extend; 2207 - /* 2208 - * Could not make non-resident. If this is due to this not being 2209 - * permitted for this attribute type or there not being enough space, 2210 - * try to make other attributes non-resident. Otherwise fail. 2211 - */ 2212 - if (unlikely(err != -EPERM && err != -ENOSPC)) { 2213 - /* Only emit errors when the write will fail completely. */ 2214 - read_lock_irqsave(&ni->size_lock, flags); 2215 - allocated_size = ni->allocated_size; 2216 - read_unlock_irqrestore(&ni->size_lock, flags); 2217 - if (start < 0 || start >= allocated_size) 2218 - ntfs_error(vol->sb, "Cannot extend allocation of " 2219 - "inode 0x%lx, attribute type 0x%x, " 2220 - "because the conversion from resident " 2221 - "to non-resident attribute failed " 2222 - "with error code %i.", vi->i_ino, 2223 - (unsigned)le32_to_cpu(ni->type), err); 2224 - if (err != -ENOMEM) 2225 - err = -EIO; 2226 - goto conv_err_out; 2227 - } 2228 - /* TODO: Not implemented from here, abort. */ 2229 - read_lock_irqsave(&ni->size_lock, flags); 2230 - allocated_size = ni->allocated_size; 2231 - read_unlock_irqrestore(&ni->size_lock, flags); 2232 - if (start < 0 || start >= allocated_size) { 2233 - if (err == -ENOSPC) 2234 - ntfs_error(vol->sb, "Not enough space in the mft " 2235 - "record/on disk for the non-resident " 2236 - "attribute value. This case is not " 2237 - "implemented yet."); 2238 - else /* if (err == -EPERM) */ 2239 - ntfs_error(vol->sb, "This attribute type may not be " 2240 - "non-resident. This case is not " 2241 - "implemented yet."); 2242 - } 2243 - err = -EOPNOTSUPP; 2244 - goto conv_err_out; 2245 - #if 0 2246 - // TODO: Attempt to make other attributes non-resident. 2247 - if (!err) 2248 - goto do_resident_extend; 2249 - /* 2250 - * Both the attribute list attribute and the standard information 2251 - * attribute must remain in the base inode. Thus, if this is one of 2252 - * these attributes, we have to try to move other attributes out into 2253 - * extent mft records instead. 2254 - */ 2255 - if (ni->type == AT_ATTRIBUTE_LIST || 2256 - ni->type == AT_STANDARD_INFORMATION) { 2257 - // TODO: Attempt to move other attributes into extent mft 2258 - // records. 2259 - err = -EOPNOTSUPP; 2260 - if (!err) 2261 - goto do_resident_extend; 2262 - goto err_out; 2263 - } 2264 - // TODO: Attempt to move this attribute to an extent mft record, but 2265 - // only if it is not already the only attribute in an mft record in 2266 - // which case there would be nothing to gain. 2267 - err = -EOPNOTSUPP; 2268 - if (!err) 2269 - goto do_resident_extend; 2270 - /* There is nothing we can do to make enough space. )-: */ 2271 - goto err_out; 2272 - #endif 2273 - do_non_resident_extend: 2274 - BUG_ON(!NInoNonResident(ni)); 2275 - if (new_alloc_size == allocated_size) { 2276 - BUG_ON(vcn); 2277 - goto alloc_done; 2278 - } 2279 - /* 2280 - * If the data starts after the end of the old allocation, this is a 2281 - * $DATA attribute and sparse attributes are enabled on the volume and 2282 - * for this inode, then create a sparse region between the old 2283 - * allocated size and the start of the data. Otherwise simply proceed 2284 - * with filling the whole space between the old allocated size and the 2285 - * new allocated size with clusters. 2286 - */ 2287 - if ((start >= 0 && start <= allocated_size) || ni->type != AT_DATA || 2288 - !NVolSparseEnabled(vol) || NInoSparseDisabled(ni)) 2289 - goto skip_sparse; 2290 - // TODO: This is not implemented yet. We just fill in with real 2291 - // clusters for now... 2292 - ntfs_debug("Inserting holes is not-implemented yet. Falling back to " 2293 - "allocating real clusters instead."); 2294 - skip_sparse: 2295 - rl = ni->runlist.rl; 2296 - if (likely(rl)) { 2297 - /* Seek to the end of the runlist. */ 2298 - while (rl->length) 2299 - rl++; 2300 - } 2301 - /* If this attribute extent is not mapped, map it now. */ 2302 - if (unlikely(!rl || rl->lcn == LCN_RL_NOT_MAPPED || 2303 - (rl->lcn == LCN_ENOENT && rl > ni->runlist.rl && 2304 - (rl-1)->lcn == LCN_RL_NOT_MAPPED))) { 2305 - if (!rl && !allocated_size) 2306 - goto first_alloc; 2307 - rl = ntfs_mapping_pairs_decompress(vol, a, ni->runlist.rl); 2308 - if (IS_ERR(rl)) { 2309 - err = PTR_ERR(rl); 2310 - if (start < 0 || start >= allocated_size) 2311 - ntfs_error(vol->sb, "Cannot extend allocation " 2312 - "of inode 0x%lx, attribute " 2313 - "type 0x%x, because the " 2314 - "mapping of a runlist " 2315 - "fragment failed with error " 2316 - "code %i.", vi->i_ino, 2317 - (unsigned)le32_to_cpu(ni->type), 2318 - err); 2319 - if (err != -ENOMEM) 2320 - err = -EIO; 2321 - goto err_out; 2322 - } 2323 - ni->runlist.rl = rl; 2324 - /* Seek to the end of the runlist. */ 2325 - while (rl->length) 2326 - rl++; 2327 - } 2328 - /* 2329 - * We now know the runlist of the last extent is mapped and @rl is at 2330 - * the end of the runlist. We want to begin allocating clusters 2331 - * starting at the last allocated cluster to reduce fragmentation. If 2332 - * there are no valid LCNs in the attribute we let the cluster 2333 - * allocator choose the starting cluster. 2334 - */ 2335 - /* If the last LCN is a hole or simillar seek back to last real LCN. */ 2336 - while (rl->lcn < 0 && rl > ni->runlist.rl) 2337 - rl--; 2338 - first_alloc: 2339 - // FIXME: Need to implement partial allocations so at least part of the 2340 - // write can be performed when start >= 0. (Needed for POSIX write(2) 2341 - // conformance.) 2342 - rl2 = ntfs_cluster_alloc(vol, allocated_size >> vol->cluster_size_bits, 2343 - (new_alloc_size - allocated_size) >> 2344 - vol->cluster_size_bits, (rl && (rl->lcn >= 0)) ? 2345 - rl->lcn + rl->length : -1, DATA_ZONE, true); 2346 - if (IS_ERR(rl2)) { 2347 - err = PTR_ERR(rl2); 2348 - if (start < 0 || start >= allocated_size) 2349 - ntfs_error(vol->sb, "Cannot extend allocation of " 2350 - "inode 0x%lx, attribute type 0x%x, " 2351 - "because the allocation of clusters " 2352 - "failed with error code %i.", vi->i_ino, 2353 - (unsigned)le32_to_cpu(ni->type), err); 2354 - if (err != -ENOMEM && err != -ENOSPC) 2355 - err = -EIO; 2356 - goto err_out; 2357 - } 2358 - rl = ntfs_runlists_merge(ni->runlist.rl, rl2); 2359 - if (IS_ERR(rl)) { 2360 - err = PTR_ERR(rl); 2361 - if (start < 0 || start >= allocated_size) 2362 - ntfs_error(vol->sb, "Cannot extend allocation of " 2363 - "inode 0x%lx, attribute type 0x%x, " 2364 - "because the runlist merge failed " 2365 - "with error code %i.", vi->i_ino, 2366 - (unsigned)le32_to_cpu(ni->type), err); 2367 - if (err != -ENOMEM) 2368 - err = -EIO; 2369 - if (ntfs_cluster_free_from_rl(vol, rl2)) { 2370 - ntfs_error(vol->sb, "Failed to release allocated " 2371 - "cluster(s) in error code path. Run " 2372 - "chkdsk to recover the lost " 2373 - "cluster(s)."); 2374 - NVolSetErrors(vol); 2375 - } 2376 - ntfs_free(rl2); 2377 - goto err_out; 2378 - } 2379 - ni->runlist.rl = rl; 2380 - ntfs_debug("Allocated 0x%llx clusters.", (long long)(new_alloc_size - 2381 - allocated_size) >> vol->cluster_size_bits); 2382 - /* Find the runlist element with which the attribute extent starts. */ 2383 - ll = sle64_to_cpu(a->data.non_resident.lowest_vcn); 2384 - rl2 = ntfs_rl_find_vcn_nolock(rl, ll); 2385 - BUG_ON(!rl2); 2386 - BUG_ON(!rl2->length); 2387 - BUG_ON(rl2->lcn < LCN_HOLE); 2388 - mp_rebuilt = false; 2389 - /* Get the size for the new mapping pairs array for this extent. */ 2390 - mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, -1); 2391 - if (unlikely(mp_size <= 0)) { 2392 - err = mp_size; 2393 - if (start < 0 || start >= allocated_size) 2394 - ntfs_error(vol->sb, "Cannot extend allocation of " 2395 - "inode 0x%lx, attribute type 0x%x, " 2396 - "because determining the size for the " 2397 - "mapping pairs failed with error code " 2398 - "%i.", vi->i_ino, 2399 - (unsigned)le32_to_cpu(ni->type), err); 2400 - err = -EIO; 2401 - goto undo_alloc; 2402 - } 2403 - /* Extend the attribute record to fit the bigger mapping pairs array. */ 2404 - attr_len = le32_to_cpu(a->length); 2405 - err = ntfs_attr_record_resize(m, a, mp_size + 2406 - le16_to_cpu(a->data.non_resident.mapping_pairs_offset)); 2407 - if (unlikely(err)) { 2408 - BUG_ON(err != -ENOSPC); 2409 - // TODO: Deal with this by moving this extent to a new mft 2410 - // record or by starting a new extent in a new mft record, 2411 - // possibly by extending this extent partially and filling it 2412 - // and creating a new extent for the remainder, or by making 2413 - // other attributes non-resident and/or by moving other 2414 - // attributes out of this mft record. 2415 - if (start < 0 || start >= allocated_size) 2416 - ntfs_error(vol->sb, "Not enough space in the mft " 2417 - "record for the extended attribute " 2418 - "record. This case is not " 2419 - "implemented yet."); 2420 - err = -EOPNOTSUPP; 2421 - goto undo_alloc; 2422 - } 2423 - mp_rebuilt = true; 2424 - /* Generate the mapping pairs array directly into the attr record. */ 2425 - err = ntfs_mapping_pairs_build(vol, (u8*)a + 2426 - le16_to_cpu(a->data.non_resident.mapping_pairs_offset), 2427 - mp_size, rl2, ll, -1, NULL); 2428 - if (unlikely(err)) { 2429 - if (start < 0 || start >= allocated_size) 2430 - ntfs_error(vol->sb, "Cannot extend allocation of " 2431 - "inode 0x%lx, attribute type 0x%x, " 2432 - "because building the mapping pairs " 2433 - "failed with error code %i.", vi->i_ino, 2434 - (unsigned)le32_to_cpu(ni->type), err); 2435 - err = -EIO; 2436 - goto undo_alloc; 2437 - } 2438 - /* Update the highest_vcn. */ 2439 - a->data.non_resident.highest_vcn = cpu_to_sle64((new_alloc_size >> 2440 - vol->cluster_size_bits) - 1); 2441 - /* 2442 - * We now have extended the allocated size of the attribute. Reflect 2443 - * this in the ntfs_inode structure and the attribute record. 2444 - */ 2445 - if (a->data.non_resident.lowest_vcn) { 2446 - /* 2447 - * We are not in the first attribute extent, switch to it, but 2448 - * first ensure the changes will make it to disk later. 2449 - */ 2450 - flush_dcache_mft_record_page(ctx->ntfs_ino); 2451 - mark_mft_record_dirty(ctx->ntfs_ino); 2452 - ntfs_attr_reinit_search_ctx(ctx); 2453 - err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 2454 - CASE_SENSITIVE, 0, NULL, 0, ctx); 2455 - if (unlikely(err)) 2456 - goto restore_undo_alloc; 2457 - /* @m is not used any more so no need to set it. */ 2458 - a = ctx->attr; 2459 - } 2460 - write_lock_irqsave(&ni->size_lock, flags); 2461 - ni->allocated_size = new_alloc_size; 2462 - a->data.non_resident.allocated_size = cpu_to_sle64(new_alloc_size); 2463 - /* 2464 - * FIXME: This would fail if @ni is a directory, $MFT, or an index, 2465 - * since those can have sparse/compressed set. For example can be 2466 - * set compressed even though it is not compressed itself and in that 2467 - * case the bit means that files are to be created compressed in the 2468 - * directory... At present this is ok as this code is only called for 2469 - * regular files, and only for their $DATA attribute(s). 2470 - * FIXME: The calculation is wrong if we created a hole above. For now 2471 - * it does not matter as we never create holes. 2472 - */ 2473 - if (NInoSparse(ni) || NInoCompressed(ni)) { 2474 - ni->itype.compressed.size += new_alloc_size - allocated_size; 2475 - a->data.non_resident.compressed_size = 2476 - cpu_to_sle64(ni->itype.compressed.size); 2477 - vi->i_blocks = ni->itype.compressed.size >> 9; 2478 - } else 2479 - vi->i_blocks = new_alloc_size >> 9; 2480 - write_unlock_irqrestore(&ni->size_lock, flags); 2481 - alloc_done: 2482 - if (new_data_size >= 0) { 2483 - BUG_ON(new_data_size < 2484 - sle64_to_cpu(a->data.non_resident.data_size)); 2485 - a->data.non_resident.data_size = cpu_to_sle64(new_data_size); 2486 - } 2487 - flush_done: 2488 - /* Ensure the changes make it to disk. */ 2489 - flush_dcache_mft_record_page(ctx->ntfs_ino); 2490 - mark_mft_record_dirty(ctx->ntfs_ino); 2491 - done: 2492 - ntfs_attr_put_search_ctx(ctx); 2493 - unmap_mft_record(base_ni); 2494 - up_write(&ni->runlist.lock); 2495 - ntfs_debug("Done, new_allocated_size 0x%llx.", 2496 - (unsigned long long)new_alloc_size); 2497 - return new_alloc_size; 2498 - restore_undo_alloc: 2499 - if (start < 0 || start >= allocated_size) 2500 - ntfs_error(vol->sb, "Cannot complete extension of allocation " 2501 - "of inode 0x%lx, attribute type 0x%x, because " 2502 - "lookup of first attribute extent failed with " 2503 - "error code %i.", vi->i_ino, 2504 - (unsigned)le32_to_cpu(ni->type), err); 2505 - if (err == -ENOENT) 2506 - err = -EIO; 2507 - ntfs_attr_reinit_search_ctx(ctx); 2508 - if (ntfs_attr_lookup(ni->type, ni->name, ni->name_len, CASE_SENSITIVE, 2509 - allocated_size >> vol->cluster_size_bits, NULL, 0, 2510 - ctx)) { 2511 - ntfs_error(vol->sb, "Failed to find last attribute extent of " 2512 - "attribute in error code path. Run chkdsk to " 2513 - "recover."); 2514 - write_lock_irqsave(&ni->size_lock, flags); 2515 - ni->allocated_size = new_alloc_size; 2516 - /* 2517 - * FIXME: This would fail if @ni is a directory... See above. 2518 - * FIXME: The calculation is wrong if we created a hole above. 2519 - * For now it does not matter as we never create holes. 2520 - */ 2521 - if (NInoSparse(ni) || NInoCompressed(ni)) { 2522 - ni->itype.compressed.size += new_alloc_size - 2523 - allocated_size; 2524 - vi->i_blocks = ni->itype.compressed.size >> 9; 2525 - } else 2526 - vi->i_blocks = new_alloc_size >> 9; 2527 - write_unlock_irqrestore(&ni->size_lock, flags); 2528 - ntfs_attr_put_search_ctx(ctx); 2529 - unmap_mft_record(base_ni); 2530 - up_write(&ni->runlist.lock); 2531 - /* 2532 - * The only thing that is now wrong is the allocated size of the 2533 - * base attribute extent which chkdsk should be able to fix. 2534 - */ 2535 - NVolSetErrors(vol); 2536 - return err; 2537 - } 2538 - ctx->attr->data.non_resident.highest_vcn = cpu_to_sle64( 2539 - (allocated_size >> vol->cluster_size_bits) - 1); 2540 - undo_alloc: 2541 - ll = allocated_size >> vol->cluster_size_bits; 2542 - if (ntfs_cluster_free(ni, ll, -1, ctx) < 0) { 2543 - ntfs_error(vol->sb, "Failed to release allocated cluster(s) " 2544 - "in error code path. Run chkdsk to recover " 2545 - "the lost cluster(s)."); 2546 - NVolSetErrors(vol); 2547 - } 2548 - m = ctx->mrec; 2549 - a = ctx->attr; 2550 - /* 2551 - * If the runlist truncation fails and/or the search context is no 2552 - * longer valid, we cannot resize the attribute record or build the 2553 - * mapping pairs array thus we mark the inode bad so that no access to 2554 - * the freed clusters can happen. 2555 - */ 2556 - if (ntfs_rl_truncate_nolock(vol, &ni->runlist, ll) || IS_ERR(m)) { 2557 - ntfs_error(vol->sb, "Failed to %s in error code path. Run " 2558 - "chkdsk to recover.", IS_ERR(m) ? 2559 - "restore attribute search context" : 2560 - "truncate attribute runlist"); 2561 - NVolSetErrors(vol); 2562 - } else if (mp_rebuilt) { 2563 - if (ntfs_attr_record_resize(m, a, attr_len)) { 2564 - ntfs_error(vol->sb, "Failed to restore attribute " 2565 - "record in error code path. Run " 2566 - "chkdsk to recover."); 2567 - NVolSetErrors(vol); 2568 - } else /* if (success) */ { 2569 - if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu( 2570 - a->data.non_resident. 2571 - mapping_pairs_offset), attr_len - 2572 - le16_to_cpu(a->data.non_resident. 2573 - mapping_pairs_offset), rl2, ll, -1, 2574 - NULL)) { 2575 - ntfs_error(vol->sb, "Failed to restore " 2576 - "mapping pairs array in error " 2577 - "code path. Run chkdsk to " 2578 - "recover."); 2579 - NVolSetErrors(vol); 2580 - } 2581 - flush_dcache_mft_record_page(ctx->ntfs_ino); 2582 - mark_mft_record_dirty(ctx->ntfs_ino); 2583 - } 2584 - } 2585 - err_out: 2586 - if (ctx) 2587 - ntfs_attr_put_search_ctx(ctx); 2588 - if (m) 2589 - unmap_mft_record(base_ni); 2590 - up_write(&ni->runlist.lock); 2591 - conv_err_out: 2592 - ntfs_debug("Failed. Returning error code %i.", err); 2593 - return err; 2594 - } 2595 - 2596 - /** 1850 + /* 2597 1851 * ntfs_attr_set - fill (a part of) an attribute with a byte 2598 1852 * @ni: ntfs inode describing the attribute to fill 2599 1853 * @ofs: offset inside the attribute at which to start to fill ··· 1971 2491 * byte offset @ofs inside the attribute with the constant byte @val. 1972 2492 * 1973 2493 * This function is effectively like memset() applied to an ntfs attribute. 1974 - * Note this function actually only operates on the page cache pages belonging 2494 + * Note thie function actually only operates on the page cache pages belonging 1975 2495 * to the ntfs attribute and it marks them dirty after doing the memset(). 1976 2496 * Thus it relies on the vm dirty page write code paths to cause the modified 1977 2497 * pages to be written to the mft record/disk. 1978 - * 1979 - * Return 0 on success and -errno on error. An error code of -ESPIPE means 1980 - * that @ofs + @cnt were outside the end of the attribute and no write was 1981 - * performed. 1982 2498 */ 1983 - int ntfs_attr_set(ntfs_inode *ni, const s64 ofs, const s64 cnt, const u8 val) 2499 + int ntfs_attr_set(struct ntfs_inode *ni, s64 ofs, s64 cnt, const u8 val) 1984 2500 { 1985 - ntfs_volume *vol = ni->vol; 1986 - struct address_space *mapping; 1987 - struct page *page; 1988 - u8 *kaddr; 1989 - pgoff_t idx, end; 1990 - unsigned start_ofs, end_ofs, size; 2501 + struct address_space *mapping = VFS_I(ni)->i_mapping; 2502 + struct folio *folio; 2503 + pgoff_t index; 2504 + u8 *addr; 2505 + unsigned long offset; 2506 + size_t attr_len; 2507 + int ret = 0; 1991 2508 1992 - ntfs_debug("Entering for ofs 0x%llx, cnt 0x%llx, val 0x%hx.", 1993 - (long long)ofs, (long long)cnt, val); 1994 - BUG_ON(ofs < 0); 1995 - BUG_ON(cnt < 0); 1996 - if (!cnt) 1997 - goto done; 1998 - /* 1999 - * FIXME: Compressed and encrypted attributes are not supported when 2000 - * writing and we should never have gotten here for them. 2001 - */ 2002 - BUG_ON(NInoCompressed(ni)); 2003 - BUG_ON(NInoEncrypted(ni)); 2004 - mapping = VFS_I(ni)->i_mapping; 2005 - /* Work out the starting index and page offset. */ 2006 - idx = ofs >> PAGE_SHIFT; 2007 - start_ofs = ofs & ~PAGE_MASK; 2008 - /* Work out the ending index and page offset. */ 2009 - end = ofs + cnt; 2010 - end_ofs = end & ~PAGE_MASK; 2011 - /* If the end is outside the inode size return -ESPIPE. */ 2012 - if (unlikely(end > i_size_read(VFS_I(ni)))) { 2013 - ntfs_error(vol->sb, "Request exceeds end of attribute."); 2014 - return -ESPIPE; 2015 - } 2016 - end >>= PAGE_SHIFT; 2017 - /* If there is a first partial page, need to do it the slow way. */ 2018 - if (start_ofs) { 2019 - page = read_mapping_page(mapping, idx, NULL); 2020 - if (IS_ERR(page)) { 2021 - ntfs_error(vol->sb, "Failed to read first partial " 2022 - "page (error, index 0x%lx).", idx); 2023 - return PTR_ERR(page); 2509 + index = ofs >> PAGE_SHIFT; 2510 + while (cnt) { 2511 + folio = read_mapping_folio(mapping, index, NULL); 2512 + if (IS_ERR(folio)) { 2513 + ret = PTR_ERR(folio); 2514 + ntfs_error(VFS_I(ni)->i_sb, "Failed to read a page %lu for attr %#x: %ld", 2515 + index, ni->type, PTR_ERR(folio)); 2516 + break; 2024 2517 } 2025 - /* 2026 - * If the last page is the same as the first page, need to 2027 - * limit the write to the end offset. 2028 - */ 2029 - size = PAGE_SIZE; 2030 - if (idx == end) 2031 - size = end_ofs; 2032 - kaddr = kmap_atomic(page); 2033 - memset(kaddr + start_ofs, val, size - start_ofs); 2034 - flush_dcache_page(page); 2035 - kunmap_atomic(kaddr); 2036 - set_page_dirty(page); 2037 - put_page(page); 2038 - balance_dirty_pages_ratelimited(mapping); 2039 - cond_resched(); 2040 - if (idx == end) 2041 - goto done; 2042 - idx++; 2043 - } 2044 - /* Do the whole pages the fast way. */ 2045 - for (; idx < end; idx++) { 2046 - /* Find or create the current page. (The page is locked.) */ 2047 - page = grab_cache_page(mapping, idx); 2048 - if (unlikely(!page)) { 2049 - ntfs_error(vol->sb, "Insufficient memory to grab " 2050 - "page (index 0x%lx).", idx); 2051 - return -ENOMEM; 2052 - } 2053 - kaddr = kmap_atomic(page); 2054 - memset(kaddr, val, PAGE_SIZE); 2055 - flush_dcache_page(page); 2056 - kunmap_atomic(kaddr); 2057 - /* 2058 - * If the page has buffers, mark them uptodate since buffer 2059 - * state and not page state is definitive in 2.6 kernels. 2060 - */ 2061 - if (page_has_buffers(page)) { 2062 - struct buffer_head *bh, *head; 2063 2518 2064 - bh = head = page_buffers(page); 2065 - do { 2066 - set_buffer_uptodate(bh); 2067 - } while ((bh = bh->b_this_page) != head); 2068 - } 2069 - /* Now that buffers are uptodate, set the page uptodate, too. */ 2070 - SetPageUptodate(page); 2071 - /* 2072 - * Set the page and all its buffers dirty and mark the inode 2073 - * dirty, too. The VM will write the page later on. 2074 - */ 2075 - set_page_dirty(page); 2076 - /* Finally unlock and release the page. */ 2077 - unlock_page(page); 2078 - put_page(page); 2079 - balance_dirty_pages_ratelimited(mapping); 2519 + offset = offset_in_folio(folio, ofs); 2520 + attr_len = min_t(size_t, (size_t)cnt, folio_size(folio) - offset); 2521 + 2522 + folio_lock(folio); 2523 + addr = kmap_local_folio(folio, offset); 2524 + memset(addr, val, attr_len); 2525 + kunmap_local(addr); 2526 + 2527 + folio_mark_dirty(folio); 2528 + folio_unlock(folio); 2529 + folio_put(folio); 2530 + 2531 + ofs += attr_len; 2532 + cnt -= attr_len; 2533 + index++; 2080 2534 cond_resched(); 2081 2535 } 2082 - /* If there is a last partial page, need to do it the slow way. */ 2083 - if (end_ofs) { 2084 - page = read_mapping_page(mapping, idx, NULL); 2085 - if (IS_ERR(page)) { 2086 - ntfs_error(vol->sb, "Failed to read last partial page " 2087 - "(error, index 0x%lx).", idx); 2088 - return PTR_ERR(page); 2089 - } 2090 - kaddr = kmap_atomic(page); 2091 - memset(kaddr, val, end_ofs); 2092 - flush_dcache_page(page); 2093 - kunmap_atomic(kaddr); 2094 - set_page_dirty(page); 2095 - put_page(page); 2096 - balance_dirty_pages_ratelimited(mapping); 2097 - cond_resched(); 2536 + 2537 + return ret; 2538 + } 2539 + 2540 + int ntfs_attr_set_initialized_size(struct ntfs_inode *ni, loff_t new_size) 2541 + { 2542 + struct ntfs_attr_search_ctx *ctx; 2543 + int err = 0; 2544 + 2545 + if (!NInoNonResident(ni)) 2546 + return -EINVAL; 2547 + 2548 + ctx = ntfs_attr_get_search_ctx(ni, NULL); 2549 + if (!ctx) 2550 + return -ENOMEM; 2551 + 2552 + err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 2553 + CASE_SENSITIVE, 0, NULL, 0, ctx); 2554 + if (err) 2555 + goto out_ctx; 2556 + 2557 + ctx->attr->data.non_resident.initialized_size = cpu_to_le64(new_size); 2558 + ni->initialized_size = new_size; 2559 + mark_mft_record_dirty(ctx->ntfs_ino); 2560 + out_ctx: 2561 + ntfs_attr_put_search_ctx(ctx); 2562 + return err; 2563 + } 2564 + 2565 + /* 2566 + * ntfs_make_room_for_attr - make room for an attribute inside an mft record 2567 + * @m: mft record 2568 + * @pos: position at which to make space 2569 + * @size: byte size to make available at this position 2570 + * 2571 + * @pos points to the attribute in front of which we want to make space. 2572 + */ 2573 + static int ntfs_make_room_for_attr(struct mft_record *m, u8 *pos, u32 size) 2574 + { 2575 + u32 biu; 2576 + 2577 + ntfs_debug("Entering for pos 0x%x, size %u.\n", 2578 + (int)(pos - (u8 *)m), (unsigned int) size); 2579 + 2580 + /* Make size 8-byte alignment. */ 2581 + size = (size + 7) & ~7; 2582 + 2583 + /* Rigorous consistency checks. */ 2584 + if (!m || !pos || pos < (u8 *)m) { 2585 + pr_err("%s: pos=%p m=%p", __func__, pos, m); 2586 + return -EINVAL; 2098 2587 } 2099 - done: 2100 - ntfs_debug("Done."); 2588 + 2589 + /* The -8 is for the attribute terminator. */ 2590 + if (pos - (u8 *)m > (int)le32_to_cpu(m->bytes_in_use) - 8) 2591 + return -EINVAL; 2592 + /* Nothing to do. */ 2593 + if (!size) 2594 + return 0; 2595 + 2596 + biu = le32_to_cpu(m->bytes_in_use); 2597 + /* Do we have enough space? */ 2598 + if (biu + size > le32_to_cpu(m->bytes_allocated) || 2599 + pos + size > (u8 *)m + le32_to_cpu(m->bytes_allocated)) { 2600 + ntfs_debug("No enough space in the MFT record\n"); 2601 + return -ENOSPC; 2602 + } 2603 + /* Move everything after pos to pos + size. */ 2604 + memmove(pos + size, pos, biu - (pos - (u8 *)m)); 2605 + /* Update mft record. */ 2606 + m->bytes_in_use = cpu_to_le32(biu + size); 2101 2607 return 0; 2102 2608 } 2103 2609 2104 - #endif /* NTFS_RW */ 2610 + /* 2611 + * ntfs_resident_attr_record_add - add resident attribute to inode 2612 + * @ni: opened ntfs inode to which MFT record add attribute 2613 + * @type: type of the new attribute 2614 + * @name: name of the new attribute 2615 + * @name_len: name length of the new attribute 2616 + * @val: value of the new attribute 2617 + * @size: size of new attribute (length of @val, if @val != NULL) 2618 + * @flags: flags of the new attribute 2619 + */ 2620 + int ntfs_resident_attr_record_add(struct ntfs_inode *ni, __le32 type, 2621 + __le16 *name, u8 name_len, u8 *val, u32 size, 2622 + __le16 flags) 2623 + { 2624 + struct ntfs_attr_search_ctx *ctx; 2625 + u32 length; 2626 + struct attr_record *a; 2627 + struct mft_record *m; 2628 + int err, offset; 2629 + struct ntfs_inode *base_ni; 2630 + 2631 + ntfs_debug("Entering for inode 0x%llx, attr 0x%x, flags 0x%x.\n", 2632 + (long long) ni->mft_no, (unsigned int) le32_to_cpu(type), 2633 + (unsigned int) le16_to_cpu(flags)); 2634 + 2635 + if (!ni || (!name && name_len)) 2636 + return -EINVAL; 2637 + 2638 + err = ntfs_attr_can_be_resident(ni->vol, type); 2639 + if (err) { 2640 + if (err == -EPERM) 2641 + ntfs_debug("Attribute can't be resident.\n"); 2642 + else 2643 + ntfs_debug("ntfs_attr_can_be_resident failed.\n"); 2644 + return err; 2645 + } 2646 + 2647 + /* Locate place where record should be. */ 2648 + ctx = ntfs_attr_get_search_ctx(ni, NULL); 2649 + if (!ctx) { 2650 + ntfs_error(ni->vol->sb, "%s: Failed to get search context", 2651 + __func__); 2652 + return -ENOMEM; 2653 + } 2654 + /* 2655 + * Use ntfs_attr_find instead of ntfs_attr_lookup to find place for 2656 + * attribute in @ni->mrec, not any extent inode in case if @ni is base 2657 + * file record. 2658 + */ 2659 + err = ntfs_attr_find(type, name, name_len, CASE_SENSITIVE, val, size, ctx); 2660 + if (!err) { 2661 + err = -EEXIST; 2662 + ntfs_debug("Attribute already present.\n"); 2663 + goto put_err_out; 2664 + } 2665 + if (err != -ENOENT) { 2666 + err = -EIO; 2667 + goto put_err_out; 2668 + } 2669 + a = ctx->attr; 2670 + m = ctx->mrec; 2671 + 2672 + /* Make room for attribute. */ 2673 + length = offsetof(struct attr_record, data.resident.reserved) + 2674 + sizeof(a->data.resident.reserved) + 2675 + ((name_len * sizeof(__le16) + 7) & ~7) + 2676 + ((size + 7) & ~7); 2677 + err = ntfs_make_room_for_attr(ctx->mrec, (u8 *) ctx->attr, length); 2678 + if (err) { 2679 + ntfs_debug("Failed to make room for attribute.\n"); 2680 + goto put_err_out; 2681 + } 2682 + 2683 + /* Setup record fields. */ 2684 + offset = ((u8 *)a - (u8 *)m); 2685 + a->type = type; 2686 + a->length = cpu_to_le32(length); 2687 + a->non_resident = 0; 2688 + a->name_length = name_len; 2689 + a->name_offset = 2690 + name_len ? cpu_to_le16((offsetof(struct attr_record, data.resident.reserved) + 2691 + sizeof(a->data.resident.reserved))) : cpu_to_le16(0); 2692 + 2693 + a->flags = flags; 2694 + a->instance = m->next_attr_instance; 2695 + a->data.resident.value_length = cpu_to_le32(size); 2696 + a->data.resident.value_offset = cpu_to_le16(length - ((size + 7) & ~7)); 2697 + if (val) 2698 + memcpy((u8 *)a + le16_to_cpu(a->data.resident.value_offset), val, size); 2699 + else 2700 + memset((u8 *)a + le16_to_cpu(a->data.resident.value_offset), 0, size); 2701 + if (type == AT_FILE_NAME) 2702 + a->data.resident.flags = RESIDENT_ATTR_IS_INDEXED; 2703 + else 2704 + a->data.resident.flags = 0; 2705 + if (name_len) 2706 + memcpy((u8 *)a + le16_to_cpu(a->name_offset), 2707 + name, sizeof(__le16) * name_len); 2708 + m->next_attr_instance = 2709 + cpu_to_le16((le16_to_cpu(m->next_attr_instance) + 1) & 0xffff); 2710 + if (ni->nr_extents == -1) 2711 + base_ni = ni->ext.base_ntfs_ino; 2712 + else 2713 + base_ni = ni; 2714 + if (type != AT_ATTRIBUTE_LIST && NInoAttrList(base_ni)) { 2715 + err = ntfs_attrlist_entry_add(ni, a); 2716 + if (err) { 2717 + ntfs_attr_record_resize(m, a, 0); 2718 + mark_mft_record_dirty(ctx->ntfs_ino); 2719 + ntfs_debug("Failed add attribute entry to ATTRIBUTE_LIST.\n"); 2720 + goto put_err_out; 2721 + } 2722 + } 2723 + mark_mft_record_dirty(ni); 2724 + ntfs_attr_put_search_ctx(ctx); 2725 + return offset; 2726 + put_err_out: 2727 + ntfs_attr_put_search_ctx(ctx); 2728 + return -EIO; 2729 + } 2730 + 2731 + /* 2732 + * ntfs_non_resident_attr_record_add - add extent of non-resident attribute 2733 + * @ni: opened ntfs inode to which MFT record add attribute 2734 + * @type: type of the new attribute extent 2735 + * @name: name of the new attribute extent 2736 + * @name_len: name length of the new attribute extent 2737 + * @lowest_vcn: lowest vcn of the new attribute extent 2738 + * @dataruns_size: dataruns size of the new attribute extent 2739 + * @flags: flags of the new attribute extent 2740 + */ 2741 + static int ntfs_non_resident_attr_record_add(struct ntfs_inode *ni, __le32 type, 2742 + __le16 *name, u8 name_len, s64 lowest_vcn, int dataruns_size, 2743 + __le16 flags) 2744 + { 2745 + struct ntfs_attr_search_ctx *ctx; 2746 + u32 length; 2747 + struct attr_record *a; 2748 + struct mft_record *m; 2749 + struct ntfs_inode *base_ni; 2750 + int err, offset; 2751 + 2752 + ntfs_debug("Entering for inode 0x%llx, attr 0x%x, lowest_vcn %lld, dataruns_size %d, flags 0x%x.\n", 2753 + (long long) ni->mft_no, (unsigned int) le32_to_cpu(type), 2754 + (long long) lowest_vcn, dataruns_size, 2755 + (unsigned int) le16_to_cpu(flags)); 2756 + 2757 + if (!ni || dataruns_size <= 0 || (!name && name_len)) 2758 + return -EINVAL; 2759 + 2760 + err = ntfs_attr_can_be_non_resident(ni->vol, type); 2761 + if (err) { 2762 + if (err == -EPERM) 2763 + pr_err("Attribute can't be non resident"); 2764 + else 2765 + pr_err("ntfs_attr_can_be_non_resident failed"); 2766 + return err; 2767 + } 2768 + 2769 + /* Locate place where record should be. */ 2770 + ctx = ntfs_attr_get_search_ctx(ni, NULL); 2771 + if (!ctx) { 2772 + pr_err("%s: Failed to get search context", __func__); 2773 + return -ENOMEM; 2774 + } 2775 + /* 2776 + * Use ntfs_attr_find instead of ntfs_attr_lookup to find place for 2777 + * attribute in @ni->mrec, not any extent inode in case if @ni is base 2778 + * file record. 2779 + */ 2780 + err = ntfs_attr_find(type, name, name_len, CASE_SENSITIVE, NULL, 0, ctx); 2781 + if (!err) { 2782 + err = -EEXIST; 2783 + pr_err("Attribute 0x%x already present", type); 2784 + goto put_err_out; 2785 + } 2786 + if (err != -ENOENT) { 2787 + pr_err("ntfs_attr_find failed"); 2788 + err = -EIO; 2789 + goto put_err_out; 2790 + } 2791 + a = ctx->attr; 2792 + m = ctx->mrec; 2793 + 2794 + /* Make room for attribute. */ 2795 + dataruns_size = (dataruns_size + 7) & ~7; 2796 + length = offsetof(struct attr_record, data.non_resident.compressed_size) + 2797 + ((sizeof(__le16) * name_len + 7) & ~7) + dataruns_size + 2798 + ((flags & (ATTR_IS_COMPRESSED | ATTR_IS_SPARSE)) ? 2799 + sizeof(a->data.non_resident.compressed_size) : 0); 2800 + err = ntfs_make_room_for_attr(ctx->mrec, (u8 *) ctx->attr, length); 2801 + if (err) { 2802 + pr_err("Failed to make room for attribute"); 2803 + goto put_err_out; 2804 + } 2805 + 2806 + /* Setup record fields. */ 2807 + a->type = type; 2808 + a->length = cpu_to_le32(length); 2809 + a->non_resident = 1; 2810 + a->name_length = name_len; 2811 + a->name_offset = cpu_to_le16(offsetof(struct attr_record, 2812 + data.non_resident.compressed_size) + 2813 + ((flags & (ATTR_IS_COMPRESSED | ATTR_IS_SPARSE)) ? 2814 + sizeof(a->data.non_resident.compressed_size) : 0)); 2815 + a->flags = flags; 2816 + a->instance = m->next_attr_instance; 2817 + a->data.non_resident.lowest_vcn = cpu_to_le64(lowest_vcn); 2818 + a->data.non_resident.mapping_pairs_offset = cpu_to_le16(length - dataruns_size); 2819 + a->data.non_resident.compression_unit = 2820 + (flags & ATTR_IS_COMPRESSED) ? STANDARD_COMPRESSION_UNIT : 0; 2821 + /* If @lowest_vcn == 0, than setup empty attribute. */ 2822 + if (!lowest_vcn) { 2823 + a->data.non_resident.highest_vcn = cpu_to_le64(-1); 2824 + a->data.non_resident.allocated_size = 0; 2825 + a->data.non_resident.data_size = 0; 2826 + a->data.non_resident.initialized_size = 0; 2827 + /* Set empty mapping pairs. */ 2828 + *((u8 *)a + le16_to_cpu(a->data.non_resident.mapping_pairs_offset)) = 0; 2829 + } 2830 + if (name_len) 2831 + memcpy((u8 *)a + le16_to_cpu(a->name_offset), 2832 + name, sizeof(__le16) * name_len); 2833 + m->next_attr_instance = 2834 + cpu_to_le16((le16_to_cpu(m->next_attr_instance) + 1) & 0xffff); 2835 + if (ni->nr_extents == -1) 2836 + base_ni = ni->ext.base_ntfs_ino; 2837 + else 2838 + base_ni = ni; 2839 + if (type != AT_ATTRIBUTE_LIST && NInoAttrList(base_ni)) { 2840 + err = ntfs_attrlist_entry_add(ni, a); 2841 + if (err) { 2842 + pr_err("Failed add attr entry to attrlist"); 2843 + ntfs_attr_record_resize(m, a, 0); 2844 + goto put_err_out; 2845 + } 2846 + } 2847 + mark_mft_record_dirty(ni); 2848 + /* 2849 + * Locate offset from start of the MFT record where new attribute is 2850 + * placed. We need relookup it, because record maybe moved during 2851 + * update of attribute list. 2852 + */ 2853 + ntfs_attr_reinit_search_ctx(ctx); 2854 + err = ntfs_attr_lookup(type, name, name_len, CASE_SENSITIVE, 2855 + lowest_vcn, NULL, 0, ctx); 2856 + if (err) { 2857 + pr_err("%s: attribute lookup failed", __func__); 2858 + ntfs_attr_put_search_ctx(ctx); 2859 + return err; 2860 + 2861 + } 2862 + offset = (u8 *)ctx->attr - (u8 *)ctx->mrec; 2863 + ntfs_attr_put_search_ctx(ctx); 2864 + return offset; 2865 + put_err_out: 2866 + ntfs_attr_put_search_ctx(ctx); 2867 + return -1; 2868 + } 2869 + 2870 + /* 2871 + * ntfs_attr_record_rm - remove attribute extent 2872 + * @ctx: search context describing the attribute which should be removed 2873 + * 2874 + * If this function succeed, user should reinit search context if he/she wants 2875 + * use it anymore. 2876 + */ 2877 + int ntfs_attr_record_rm(struct ntfs_attr_search_ctx *ctx) 2878 + { 2879 + struct ntfs_inode *base_ni, *ni; 2880 + __le32 type; 2881 + int err; 2882 + 2883 + if (!ctx || !ctx->ntfs_ino || !ctx->mrec || !ctx->attr) 2884 + return -EINVAL; 2885 + 2886 + ntfs_debug("Entering for inode 0x%llx, attr 0x%x.\n", 2887 + (long long) ctx->ntfs_ino->mft_no, 2888 + (unsigned int) le32_to_cpu(ctx->attr->type)); 2889 + type = ctx->attr->type; 2890 + ni = ctx->ntfs_ino; 2891 + if (ctx->base_ntfs_ino) 2892 + base_ni = ctx->base_ntfs_ino; 2893 + else 2894 + base_ni = ctx->ntfs_ino; 2895 + 2896 + /* Remove attribute itself. */ 2897 + if (ntfs_attr_record_resize(ctx->mrec, ctx->attr, 0)) { 2898 + ntfs_debug("Couldn't remove attribute record. Bug or damaged MFT record.\n"); 2899 + return -EIO; 2900 + } 2901 + mark_mft_record_dirty(ni); 2902 + 2903 + /* 2904 + * Remove record from $ATTRIBUTE_LIST if present and we don't want 2905 + * delete $ATTRIBUTE_LIST itself. 2906 + */ 2907 + if (NInoAttrList(base_ni) && type != AT_ATTRIBUTE_LIST) { 2908 + err = ntfs_attrlist_entry_rm(ctx); 2909 + if (err) { 2910 + ntfs_debug("Couldn't delete record from $ATTRIBUTE_LIST.\n"); 2911 + return err; 2912 + } 2913 + } 2914 + 2915 + /* Post $ATTRIBUTE_LIST delete setup. */ 2916 + if (type == AT_ATTRIBUTE_LIST) { 2917 + if (NInoAttrList(base_ni) && base_ni->attr_list) 2918 + kvfree(base_ni->attr_list); 2919 + base_ni->attr_list = NULL; 2920 + NInoClearAttrList(base_ni); 2921 + } 2922 + 2923 + /* Free MFT record, if it doesn't contain attributes. */ 2924 + if (le32_to_cpu(ctx->mrec->bytes_in_use) - 2925 + le16_to_cpu(ctx->mrec->attrs_offset) == 8) { 2926 + if (ntfs_mft_record_free(ni->vol, ni)) { 2927 + ntfs_debug("Couldn't free MFT record.\n"); 2928 + return -EIO; 2929 + } 2930 + /* Remove done if we freed base inode. */ 2931 + if (ni == base_ni) 2932 + return 0; 2933 + ntfs_inode_close(ni); 2934 + ctx->ntfs_ino = ni = NULL; 2935 + } 2936 + 2937 + if (type == AT_ATTRIBUTE_LIST || !NInoAttrList(base_ni)) 2938 + return 0; 2939 + 2940 + /* Remove attribute list if we don't need it any more. */ 2941 + if (!ntfs_attrlist_need(base_ni)) { 2942 + struct ntfs_attr na; 2943 + struct inode *attr_vi; 2944 + 2945 + ntfs_attr_reinit_search_ctx(ctx); 2946 + if (ntfs_attr_lookup(AT_ATTRIBUTE_LIST, NULL, 0, CASE_SENSITIVE, 2947 + 0, NULL, 0, ctx)) { 2948 + ntfs_debug("Couldn't find attribute list. Succeed anyway.\n"); 2949 + return 0; 2950 + } 2951 + /* Deallocate clusters. */ 2952 + if (ctx->attr->non_resident) { 2953 + struct runlist_element *al_rl; 2954 + size_t new_rl_count; 2955 + 2956 + al_rl = ntfs_mapping_pairs_decompress(base_ni->vol, 2957 + ctx->attr, NULL, &new_rl_count); 2958 + if (IS_ERR(al_rl)) { 2959 + ntfs_debug("Couldn't decompress attribute list runlist. Succeed anyway.\n"); 2960 + return 0; 2961 + } 2962 + if (ntfs_cluster_free_from_rl(base_ni->vol, al_rl)) 2963 + ntfs_debug("Leaking clusters! Run chkdsk. Couldn't free clusters from attribute list runlist.\n"); 2964 + kvfree(al_rl); 2965 + } 2966 + /* Remove attribute record itself. */ 2967 + if (ntfs_attr_record_rm(ctx)) { 2968 + ntfs_debug("Couldn't remove attribute list. Succeed anyway.\n"); 2969 + return 0; 2970 + } 2971 + 2972 + na.mft_no = VFS_I(base_ni)->i_ino; 2973 + na.type = AT_ATTRIBUTE_LIST; 2974 + na.name = NULL; 2975 + na.name_len = 0; 2976 + 2977 + attr_vi = ilookup5(VFS_I(base_ni)->i_sb, VFS_I(base_ni)->i_ino, 2978 + ntfs_test_inode, &na); 2979 + if (attr_vi) { 2980 + clear_nlink(attr_vi); 2981 + iput(attr_vi); 2982 + } 2983 + 2984 + } 2985 + return 0; 2986 + } 2987 + 2988 + /* 2989 + * ntfs_attr_add - add attribute to inode 2990 + * @ni: opened ntfs inode to which add attribute 2991 + * @type: type of the new attribute 2992 + * @name: name in unicode of the new attribute 2993 + * @name_len: name length in unicode characters of the new attribute 2994 + * @val: value of new attribute 2995 + * @size: size of the new attribute / length of @val (if specified) 2996 + * 2997 + * @val should always be specified for always resident attributes (eg. FILE_NAME 2998 + * attribute), for attributes that can become non-resident @val can be NULL 2999 + * (eg. DATA attribute). @size can be specified even if @val is NULL, in this 3000 + * case data size will be equal to @size and initialized size will be equal 3001 + * to 0. 3002 + * 3003 + * If inode haven't got enough space to add attribute, add attribute to one of 3004 + * it extents, if no extents present or no one of them have enough space, than 3005 + * allocate new extent and add attribute to it. 3006 + * 3007 + * If on one of this steps attribute list is needed but not present, than it is 3008 + * added transparently to caller. So, this function should not be called with 3009 + * @type == AT_ATTRIBUTE_LIST, if you really need to add attribute list call 3010 + * ntfs_inode_add_attrlist instead. 3011 + * 3012 + * On success return 0. On error return -1 with errno set to the error code. 3013 + */ 3014 + int ntfs_attr_add(struct ntfs_inode *ni, __le32 type, 3015 + __le16 *name, u8 name_len, u8 *val, s64 size) 3016 + { 3017 + struct super_block *sb; 3018 + u32 attr_rec_size; 3019 + int err, i, offset; 3020 + bool is_resident; 3021 + bool can_be_non_resident = false; 3022 + struct ntfs_inode *attr_ni; 3023 + struct inode *attr_vi; 3024 + struct mft_record *ni_mrec; 3025 + 3026 + if (!ni || size < 0 || type == AT_ATTRIBUTE_LIST) 3027 + return -EINVAL; 3028 + 3029 + ntfs_debug("Entering for inode 0x%llx, attr %x, size %lld.\n", 3030 + (long long) ni->mft_no, type, size); 3031 + 3032 + if (ni->nr_extents == -1) 3033 + ni = ni->ext.base_ntfs_ino; 3034 + 3035 + /* Check the attribute type and the size. */ 3036 + err = ntfs_attr_size_bounds_check(ni->vol, type, size); 3037 + if (err) { 3038 + if (err == -ENOENT) 3039 + err = -EIO; 3040 + return err; 3041 + } 3042 + 3043 + sb = ni->vol->sb; 3044 + /* Sanity checks for always resident attributes. */ 3045 + err = ntfs_attr_can_be_non_resident(ni->vol, type); 3046 + if (err) { 3047 + if (err != -EPERM) { 3048 + ntfs_error(sb, "ntfs_attr_can_be_non_resident failed"); 3049 + goto err_out; 3050 + } 3051 + /* @val is mandatory. */ 3052 + if (!val) { 3053 + ntfs_error(sb, 3054 + "val is mandatory for always resident attributes"); 3055 + return -EINVAL; 3056 + } 3057 + if (size > ni->vol->mft_record_size) { 3058 + ntfs_error(sb, "Attribute is too big"); 3059 + return -ERANGE; 3060 + } 3061 + } else 3062 + can_be_non_resident = true; 3063 + 3064 + /* 3065 + * Determine resident or not will be new attribute. We add 8 to size in 3066 + * non resident case for mapping pairs. 3067 + */ 3068 + err = ntfs_attr_can_be_resident(ni->vol, type); 3069 + if (!err) { 3070 + is_resident = true; 3071 + } else { 3072 + if (err != -EPERM) { 3073 + ntfs_error(sb, "ntfs_attr_can_be_resident failed"); 3074 + goto err_out; 3075 + } 3076 + is_resident = false; 3077 + } 3078 + 3079 + /* Calculate attribute record size. */ 3080 + if (is_resident) 3081 + attr_rec_size = offsetof(struct attr_record, data.resident.reserved) + 3082 + 1 + 3083 + ((name_len * sizeof(__le16) + 7) & ~7) + 3084 + ((size + 7) & ~7); 3085 + else 3086 + attr_rec_size = offsetof(struct attr_record, data.non_resident.compressed_size) + 3087 + ((name_len * sizeof(__le16) + 7) & ~7) + 8; 3088 + 3089 + /* 3090 + * If we have enough free space for the new attribute in the base MFT 3091 + * record, then add attribute to it. 3092 + */ 3093 + retry: 3094 + ni_mrec = map_mft_record(ni); 3095 + if (IS_ERR(ni_mrec)) { 3096 + err = -EIO; 3097 + goto err_out; 3098 + } 3099 + 3100 + if (le32_to_cpu(ni_mrec->bytes_allocated) - 3101 + le32_to_cpu(ni_mrec->bytes_in_use) >= attr_rec_size) { 3102 + attr_ni = ni; 3103 + unmap_mft_record(ni); 3104 + goto add_attr_record; 3105 + } 3106 + unmap_mft_record(ni); 3107 + 3108 + /* Try to add to extent inodes. */ 3109 + err = ntfs_inode_attach_all_extents(ni); 3110 + if (err) { 3111 + ntfs_error(sb, "Failed to attach all extents to inode"); 3112 + goto err_out; 3113 + } 3114 + 3115 + for (i = 0; i < ni->nr_extents; i++) { 3116 + attr_ni = ni->ext.extent_ntfs_inos[i]; 3117 + ni_mrec = map_mft_record(attr_ni); 3118 + if (IS_ERR(ni_mrec)) { 3119 + err = -EIO; 3120 + goto err_out; 3121 + } 3122 + 3123 + if (le32_to_cpu(ni_mrec->bytes_allocated) - 3124 + le32_to_cpu(ni_mrec->bytes_in_use) >= 3125 + attr_rec_size) { 3126 + unmap_mft_record(attr_ni); 3127 + goto add_attr_record; 3128 + } 3129 + unmap_mft_record(attr_ni); 3130 + } 3131 + 3132 + /* There is no extent that contain enough space for new attribute. */ 3133 + if (!NInoAttrList(ni)) { 3134 + /* Add attribute list not present, add it and retry. */ 3135 + err = ntfs_inode_add_attrlist(ni); 3136 + if (err) { 3137 + ntfs_error(sb, "Failed to add attribute list"); 3138 + goto err_out; 3139 + } 3140 + goto retry; 3141 + } 3142 + 3143 + attr_ni = NULL; 3144 + /* Allocate new extent. */ 3145 + err = ntfs_mft_record_alloc(ni->vol, 0, &attr_ni, ni, NULL); 3146 + if (err) { 3147 + ntfs_error(sb, "Failed to allocate extent record"); 3148 + goto err_out; 3149 + } 3150 + unmap_mft_record(attr_ni); 3151 + 3152 + add_attr_record: 3153 + if (is_resident) { 3154 + /* Add resident attribute. */ 3155 + offset = ntfs_resident_attr_record_add(attr_ni, type, name, 3156 + name_len, val, size, 0); 3157 + if (offset < 0) { 3158 + if (offset == -ENOSPC && can_be_non_resident) 3159 + goto add_non_resident; 3160 + err = offset; 3161 + ntfs_error(sb, "Failed to add resident attribute"); 3162 + goto free_err_out; 3163 + } 3164 + return 0; 3165 + } 3166 + 3167 + add_non_resident: 3168 + /* Add non resident attribute. */ 3169 + offset = ntfs_non_resident_attr_record_add(attr_ni, type, name, 3170 + name_len, 0, 8, 0); 3171 + if (offset < 0) { 3172 + err = offset; 3173 + ntfs_error(sb, "Failed to add non resident attribute"); 3174 + goto free_err_out; 3175 + } 3176 + 3177 + /* If @size == 0, we are done. */ 3178 + if (!size) 3179 + return 0; 3180 + 3181 + /* Open new attribute and resize it. */ 3182 + attr_vi = ntfs_attr_iget(VFS_I(ni), type, name, name_len); 3183 + if (IS_ERR(attr_vi)) { 3184 + ntfs_error(sb, "Failed to open just added attribute"); 3185 + goto rm_attr_err_out; 3186 + } 3187 + attr_ni = NTFS_I(attr_vi); 3188 + 3189 + /* Resize and set attribute value. */ 3190 + if (ntfs_attr_truncate(attr_ni, size) || 3191 + (val && (ntfs_inode_attr_pwrite(attr_vi, 0, size, val, false) != size))) { 3192 + err = -EIO; 3193 + ntfs_error(sb, "Failed to initialize just added attribute"); 3194 + if (ntfs_attr_rm(attr_ni)) 3195 + ntfs_error(sb, "Failed to remove just added attribute"); 3196 + iput(attr_vi); 3197 + goto err_out; 3198 + } 3199 + iput(attr_vi); 3200 + return 0; 3201 + 3202 + rm_attr_err_out: 3203 + /* Remove just added attribute. */ 3204 + ni_mrec = map_mft_record(attr_ni); 3205 + if (!IS_ERR(ni_mrec)) { 3206 + if (ntfs_attr_record_resize(ni_mrec, 3207 + (struct attr_record *)((u8 *)ni_mrec + offset), 0)) 3208 + ntfs_error(sb, "Failed to remove just added attribute #2"); 3209 + unmap_mft_record(attr_ni); 3210 + } else 3211 + pr_err("EIO when try to remove new added attr\n"); 3212 + 3213 + free_err_out: 3214 + /* Free MFT record, if it doesn't contain attributes. */ 3215 + ni_mrec = map_mft_record(attr_ni); 3216 + if (!IS_ERR(ni_mrec)) { 3217 + int attr_size; 3218 + 3219 + attr_size = le32_to_cpu(ni_mrec->bytes_in_use) - 3220 + le16_to_cpu(ni_mrec->attrs_offset); 3221 + unmap_mft_record(attr_ni); 3222 + if (attr_size == 8) { 3223 + if (ntfs_mft_record_free(attr_ni->vol, attr_ni)) 3224 + ntfs_error(sb, "Failed to free MFT record"); 3225 + if (attr_ni->nr_extents < 0) 3226 + ntfs_inode_close(attr_ni); 3227 + } 3228 + } else 3229 + pr_err("EIO when testing mft record is free-able\n"); 3230 + 3231 + err_out: 3232 + return err; 3233 + } 3234 + 3235 + /* 3236 + * __ntfs_attr_init - primary initialization of an ntfs attribute structure 3237 + * @ni: ntfs attribute inode to initialize 3238 + * @ni: ntfs inode with which to initialize the ntfs attribute 3239 + * @type: attribute type 3240 + * @name: attribute name in little endian Unicode or NULL 3241 + * @name_len: length of attribute @name in Unicode characters (if @name given) 3242 + * 3243 + * Initialize the ntfs attribute @na with @ni, @type, @name, and @name_len. 3244 + */ 3245 + static void __ntfs_attr_init(struct ntfs_inode *ni, 3246 + const __le32 type, __le16 *name, const u32 name_len) 3247 + { 3248 + ni->runlist.rl = NULL; 3249 + ni->type = type; 3250 + ni->name = name; 3251 + if (name) 3252 + ni->name_len = name_len; 3253 + else 3254 + ni->name_len = 0; 3255 + } 3256 + 3257 + /* 3258 + * ntfs_attr_init - initialize an ntfs_attr with data sizes and status 3259 + * @ni: ntfs inode to initialize 3260 + * @non_resident: true if attribute is non-resident 3261 + * @compressed: true if attribute is compressed 3262 + * @encrypted: true if attribute is encrypted 3263 + * @sparse: true if attribute is sparse 3264 + * @allocated_size: allocated size of the attribute 3265 + * @data_size: actual data size of the attribute 3266 + * @initialized_size: initialized size of the attribute 3267 + * @compressed_size: compressed size (if compressed or sparse) 3268 + * @compression_unit: compression unit size (log2 of clusters) 3269 + * 3270 + * Final initialization for an ntfs attribute. 3271 + */ 3272 + static void ntfs_attr_init(struct ntfs_inode *ni, const bool non_resident, 3273 + const bool compressed, const bool encrypted, const bool sparse, 3274 + const s64 allocated_size, const s64 data_size, 3275 + const s64 initialized_size, const s64 compressed_size, 3276 + const u8 compression_unit) 3277 + { 3278 + if (non_resident) 3279 + NInoSetNonResident(ni); 3280 + if (compressed) { 3281 + NInoSetCompressed(ni); 3282 + ni->flags |= FILE_ATTR_COMPRESSED; 3283 + } 3284 + if (encrypted) { 3285 + NInoSetEncrypted(ni); 3286 + ni->flags |= FILE_ATTR_ENCRYPTED; 3287 + } 3288 + if (sparse) { 3289 + NInoSetSparse(ni); 3290 + ni->flags |= FILE_ATTR_SPARSE_FILE; 3291 + } 3292 + ni->allocated_size = allocated_size; 3293 + ni->data_size = data_size; 3294 + ni->initialized_size = initialized_size; 3295 + if (compressed || sparse) { 3296 + struct ntfs_volume *vol = ni->vol; 3297 + 3298 + ni->itype.compressed.size = compressed_size; 3299 + ni->itype.compressed.block_clusters = 1 << compression_unit; 3300 + ni->itype.compressed.block_size = 1 << (compression_unit + 3301 + vol->cluster_size_bits); 3302 + ni->itype.compressed.block_size_bits = ffs( 3303 + ni->itype.compressed.block_size) - 1; 3304 + } 3305 + } 3306 + 3307 + /* 3308 + * ntfs_attr_open - open an ntfs attribute for access 3309 + * @ni: open ntfs inode in which the ntfs attribute resides 3310 + * @type: attribute type 3311 + * @name: attribute name in little endian Unicode or AT_UNNAMED or NULL 3312 + * @name_len: length of attribute @name in Unicode characters (if @name given) 3313 + */ 3314 + int ntfs_attr_open(struct ntfs_inode *ni, const __le32 type, 3315 + __le16 *name, u32 name_len) 3316 + { 3317 + struct ntfs_attr_search_ctx *ctx; 3318 + __le16 *newname = NULL; 3319 + struct attr_record *a; 3320 + bool cs; 3321 + struct ntfs_inode *base_ni; 3322 + int err; 3323 + 3324 + ntfs_debug("Entering for inode %lld, attr 0x%x.\n", 3325 + (unsigned long long)ni->mft_no, type); 3326 + 3327 + if (!ni || !ni->vol) 3328 + return -EINVAL; 3329 + 3330 + if (NInoAttr(ni)) 3331 + base_ni = ni->ext.base_ntfs_ino; 3332 + else 3333 + base_ni = ni; 3334 + 3335 + if (name && name != AT_UNNAMED && name != I30) { 3336 + name = ntfs_ucsndup(name, name_len); 3337 + if (!name) { 3338 + err = -ENOMEM; 3339 + goto err_out; 3340 + } 3341 + newname = name; 3342 + } 3343 + 3344 + ctx = ntfs_attr_get_search_ctx(base_ni, NULL); 3345 + if (!ctx) { 3346 + err = -ENOMEM; 3347 + pr_err("%s: Failed to get search context", __func__); 3348 + goto err_out; 3349 + } 3350 + 3351 + err = ntfs_attr_lookup(type, name, name_len, 0, 0, NULL, 0, ctx); 3352 + if (err) 3353 + goto put_err_out; 3354 + 3355 + a = ctx->attr; 3356 + 3357 + if (!name) { 3358 + if (a->name_length) { 3359 + name = ntfs_ucsndup((__le16 *)((u8 *)a + le16_to_cpu(a->name_offset)), 3360 + a->name_length); 3361 + if (!name) 3362 + goto put_err_out; 3363 + newname = name; 3364 + name_len = a->name_length; 3365 + } else { 3366 + name = AT_UNNAMED; 3367 + name_len = 0; 3368 + } 3369 + } 3370 + 3371 + __ntfs_attr_init(ni, type, name, name_len); 3372 + 3373 + /* 3374 + * Wipe the flags in case they are not zero for an attribute list 3375 + * attribute. Windows does not complain about invalid flags and chkdsk 3376 + * does not detect or fix them so we need to cope with it, too. 3377 + */ 3378 + if (type == AT_ATTRIBUTE_LIST) 3379 + a->flags = 0; 3380 + 3381 + if ((type == AT_DATA) && 3382 + (a->non_resident ? !a->data.non_resident.initialized_size : 3383 + !a->data.resident.value_length)) { 3384 + /* 3385 + * Define/redefine the compression state if stream is 3386 + * empty, based on the compression mark on parent 3387 + * directory (for unnamed data streams) or on current 3388 + * inode (for named data streams). The compression mark 3389 + * may change any time, the compression state can only 3390 + * change when stream is wiped out. 3391 + * 3392 + * Also prevent compression on NTFS version < 3.0 3393 + * or cluster size > 4K or compression is disabled 3394 + */ 3395 + a->flags &= ~ATTR_COMPRESSION_MASK; 3396 + if (NInoCompressed(ni) 3397 + && (ni->vol->major_ver >= 3) 3398 + && NVolCompression(ni->vol) 3399 + && (ni->vol->cluster_size <= MAX_COMPRESSION_CLUSTER_SIZE)) 3400 + a->flags |= ATTR_IS_COMPRESSED; 3401 + } 3402 + 3403 + cs = a->flags & (ATTR_IS_COMPRESSED | ATTR_IS_SPARSE); 3404 + 3405 + if (ni->type == AT_DATA && ni->name == AT_UNNAMED && 3406 + ((!(a->flags & ATTR_IS_COMPRESSED) != !NInoCompressed(ni)) || 3407 + (!(a->flags & ATTR_IS_SPARSE) != !NInoSparse(ni)) || 3408 + (!(a->flags & ATTR_IS_ENCRYPTED) != !NInoEncrypted(ni)))) { 3409 + err = -EIO; 3410 + pr_err("Inode %lld has corrupt attribute flags (0x%x <> 0x%x)\n", 3411 + (unsigned long long)ni->mft_no, 3412 + a->flags, ni->flags); 3413 + goto put_err_out; 3414 + } 3415 + 3416 + if (a->non_resident) { 3417 + if (((a->flags & ATTR_COMPRESSION_MASK) || a->data.non_resident.compression_unit) && 3418 + (ni->vol->major_ver < 3)) { 3419 + err = -EIO; 3420 + pr_err("Compressed inode %lld not allowed on NTFS %d.%d\n", 3421 + (unsigned long long)ni->mft_no, 3422 + ni->vol->major_ver, 3423 + ni->vol->major_ver); 3424 + goto put_err_out; 3425 + } 3426 + 3427 + if ((a->flags & ATTR_IS_COMPRESSED) && !a->data.non_resident.compression_unit) { 3428 + err = -EIO; 3429 + pr_err("Compressed inode %lld attr 0x%x has no compression unit\n", 3430 + (unsigned long long)ni->mft_no, type); 3431 + goto put_err_out; 3432 + } 3433 + if ((a->flags & ATTR_COMPRESSION_MASK) && 3434 + (a->data.non_resident.compression_unit != STANDARD_COMPRESSION_UNIT)) { 3435 + err = -EIO; 3436 + pr_err("Compressed inode %lld attr 0x%lx has an unsupported compression unit %d\n", 3437 + (unsigned long long)ni->mft_no, 3438 + (long)le32_to_cpu(type), 3439 + (int)a->data.non_resident.compression_unit); 3440 + goto put_err_out; 3441 + } 3442 + ntfs_attr_init(ni, true, a->flags & ATTR_IS_COMPRESSED, 3443 + a->flags & ATTR_IS_ENCRYPTED, 3444 + a->flags & ATTR_IS_SPARSE, 3445 + le64_to_cpu(a->data.non_resident.allocated_size), 3446 + le64_to_cpu(a->data.non_resident.data_size), 3447 + le64_to_cpu(a->data.non_resident.initialized_size), 3448 + cs ? le64_to_cpu(a->data.non_resident.compressed_size) : 0, 3449 + cs ? a->data.non_resident.compression_unit : 0); 3450 + } else { 3451 + s64 l = le32_to_cpu(a->data.resident.value_length); 3452 + 3453 + ntfs_attr_init(ni, false, a->flags & ATTR_IS_COMPRESSED, 3454 + a->flags & ATTR_IS_ENCRYPTED, 3455 + a->flags & ATTR_IS_SPARSE, (l + 7) & ~7, l, l, 3456 + cs ? (l + 7) & ~7 : 0, 0); 3457 + } 3458 + ntfs_attr_put_search_ctx(ctx); 3459 + out: 3460 + ntfs_debug("\n"); 3461 + return err; 3462 + 3463 + put_err_out: 3464 + ntfs_attr_put_search_ctx(ctx); 3465 + err_out: 3466 + kfree(newname); 3467 + goto out; 3468 + } 3469 + 3470 + /* 3471 + * ntfs_attr_close - free an ntfs attribute structure 3472 + * @ni: ntfs inode to free 3473 + * 3474 + * Release all memory associated with the ntfs attribute @na and then release 3475 + * @na itself. 3476 + */ 3477 + void ntfs_attr_close(struct ntfs_inode *ni) 3478 + { 3479 + if (NInoNonResident(ni) && ni->runlist.rl) 3480 + kvfree(ni->runlist.rl); 3481 + /* Don't release if using an internal constant. */ 3482 + if (ni->name != AT_UNNAMED && ni->name != I30) 3483 + kfree(ni->name); 3484 + } 3485 + 3486 + /* 3487 + * ntfs_attr_map_whole_runlist - map the whole runlist of an ntfs attribute 3488 + * @ni: ntfs inode for which to map the runlist 3489 + * 3490 + * Map the whole runlist of the ntfs attribute @na. For an attribute made up 3491 + * of only one attribute extent this is the same as calling 3492 + * ntfs_map_runlist(ni, 0) but for an attribute with multiple extents this 3493 + * will map the runlist fragments from each of the extents thus giving access 3494 + * to the entirety of the disk allocation of an attribute. 3495 + */ 3496 + int ntfs_attr_map_whole_runlist(struct ntfs_inode *ni) 3497 + { 3498 + s64 next_vcn, last_vcn, highest_vcn; 3499 + struct ntfs_attr_search_ctx *ctx; 3500 + struct ntfs_volume *vol = ni->vol; 3501 + struct super_block *sb = vol->sb; 3502 + struct attr_record *a; 3503 + int err; 3504 + struct ntfs_inode *base_ni; 3505 + int not_mapped; 3506 + size_t new_rl_count; 3507 + 3508 + ntfs_debug("Entering for inode 0x%llx, attr 0x%x.\n", 3509 + (unsigned long long)ni->mft_no, ni->type); 3510 + 3511 + if (NInoFullyMapped(ni) && ni->runlist.rl) 3512 + return 0; 3513 + 3514 + if (NInoAttr(ni)) 3515 + base_ni = ni->ext.base_ntfs_ino; 3516 + else 3517 + base_ni = ni; 3518 + 3519 + ctx = ntfs_attr_get_search_ctx(base_ni, NULL); 3520 + if (!ctx) { 3521 + ntfs_error(sb, "%s: Failed to get search context", __func__); 3522 + return -ENOMEM; 3523 + } 3524 + 3525 + /* Map all attribute extents one by one. */ 3526 + next_vcn = last_vcn = highest_vcn = 0; 3527 + a = NULL; 3528 + while (1) { 3529 + struct runlist_element *rl; 3530 + 3531 + not_mapped = 0; 3532 + if (ntfs_rl_vcn_to_lcn(ni->runlist.rl, next_vcn) == LCN_RL_NOT_MAPPED) 3533 + not_mapped = 1; 3534 + 3535 + err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 3536 + CASE_SENSITIVE, next_vcn, NULL, 0, ctx); 3537 + if (err) 3538 + break; 3539 + 3540 + a = ctx->attr; 3541 + 3542 + if (not_mapped) { 3543 + /* Decode the runlist. */ 3544 + rl = ntfs_mapping_pairs_decompress(ni->vol, a, &ni->runlist, 3545 + &new_rl_count); 3546 + if (IS_ERR(rl)) { 3547 + err = PTR_ERR(rl); 3548 + goto err_out; 3549 + } 3550 + ni->runlist.rl = rl; 3551 + ni->runlist.count = new_rl_count; 3552 + } 3553 + 3554 + /* Are we in the first extent? */ 3555 + if (!next_vcn) { 3556 + if (a->data.non_resident.lowest_vcn) { 3557 + err = -EIO; 3558 + ntfs_error(sb, 3559 + "First extent of inode %llu attribute has non-zero lowest_vcn", 3560 + (unsigned long long)ni->mft_no); 3561 + goto err_out; 3562 + } 3563 + /* Get the last vcn in the attribute. */ 3564 + last_vcn = ntfs_bytes_to_cluster(vol, 3565 + le64_to_cpu(a->data.non_resident.allocated_size)); 3566 + } 3567 + 3568 + /* Get the lowest vcn for the next extent. */ 3569 + highest_vcn = le64_to_cpu(a->data.non_resident.highest_vcn); 3570 + next_vcn = highest_vcn + 1; 3571 + 3572 + /* Only one extent or error, which we catch below. */ 3573 + if (next_vcn <= 0) { 3574 + err = -ENOENT; 3575 + break; 3576 + } 3577 + 3578 + /* Avoid endless loops due to corruption. */ 3579 + if (next_vcn < le64_to_cpu(a->data.non_resident.lowest_vcn)) { 3580 + err = -EIO; 3581 + ntfs_error(sb, "Inode %llu has corrupt attribute list", 3582 + (unsigned long long)ni->mft_no); 3583 + goto err_out; 3584 + } 3585 + } 3586 + if (!a) { 3587 + ntfs_error(sb, "Couldn't find attribute for runlist mapping"); 3588 + goto err_out; 3589 + } 3590 + if (not_mapped && highest_vcn && highest_vcn != last_vcn - 1) { 3591 + err = -EIO; 3592 + ntfs_error(sb, 3593 + "Failed to load full runlist: inode: %llu highest_vcn: 0x%llx last_vcn: 0x%llx", 3594 + (unsigned long long)ni->mft_no, 3595 + (long long)highest_vcn, (long long)last_vcn); 3596 + goto err_out; 3597 + } 3598 + ntfs_attr_put_search_ctx(ctx); 3599 + if (err == -ENOENT) { 3600 + NInoSetFullyMapped(ni); 3601 + return 0; 3602 + } 3603 + 3604 + return err; 3605 + 3606 + err_out: 3607 + ntfs_attr_put_search_ctx(ctx); 3608 + return err; 3609 + } 3610 + 3611 + /* 3612 + * ntfs_attr_record_move_to - move attribute record to target inode 3613 + * @ctx: attribute search context describing the attribute record 3614 + * @ni: opened ntfs inode to which move attribute record 3615 + */ 3616 + int ntfs_attr_record_move_to(struct ntfs_attr_search_ctx *ctx, struct ntfs_inode *ni) 3617 + { 3618 + struct ntfs_attr_search_ctx *nctx; 3619 + struct attr_record *a; 3620 + int err; 3621 + struct mft_record *ni_mrec; 3622 + struct super_block *sb; 3623 + 3624 + if (!ctx || !ctx->attr || !ctx->ntfs_ino || !ni) { 3625 + ntfs_debug("Invalid arguments passed.\n"); 3626 + return -EINVAL; 3627 + } 3628 + 3629 + sb = ni->vol->sb; 3630 + ntfs_debug("Entering for ctx->attr->type 0x%x, ctx->ntfs_ino->mft_no 0x%llx, ni->mft_no 0x%llx.\n", 3631 + (unsigned int) le32_to_cpu(ctx->attr->type), 3632 + (long long) ctx->ntfs_ino->mft_no, 3633 + (long long) ni->mft_no); 3634 + 3635 + if (ctx->ntfs_ino == ni) 3636 + return 0; 3637 + 3638 + if (!ctx->al_entry) { 3639 + ntfs_debug("Inode should contain attribute list to use this function.\n"); 3640 + return -EINVAL; 3641 + } 3642 + 3643 + /* Find place in MFT record where attribute will be moved. */ 3644 + a = ctx->attr; 3645 + nctx = ntfs_attr_get_search_ctx(ni, NULL); 3646 + if (!nctx) { 3647 + ntfs_error(sb, "%s: Failed to get search context", __func__); 3648 + return -ENOMEM; 3649 + } 3650 + 3651 + /* 3652 + * Use ntfs_attr_find instead of ntfs_attr_lookup to find place for 3653 + * attribute in @ni->mrec, not any extent inode in case if @ni is base 3654 + * file record. 3655 + */ 3656 + err = ntfs_attr_find(a->type, (__le16 *)((u8 *)a + le16_to_cpu(a->name_offset)), 3657 + a->name_length, CASE_SENSITIVE, NULL, 3658 + 0, nctx); 3659 + if (!err) { 3660 + ntfs_debug("Attribute of such type, with same name already present in this MFT record.\n"); 3661 + err = -EEXIST; 3662 + goto put_err_out; 3663 + } 3664 + if (err != -ENOENT) { 3665 + ntfs_debug("Attribute lookup failed.\n"); 3666 + goto put_err_out; 3667 + } 3668 + 3669 + /* Make space and move attribute. */ 3670 + ni_mrec = map_mft_record(ni); 3671 + if (IS_ERR(ni_mrec)) { 3672 + err = -EIO; 3673 + goto put_err_out; 3674 + } 3675 + 3676 + err = ntfs_make_room_for_attr(ni_mrec, (u8 *) nctx->attr, 3677 + le32_to_cpu(a->length)); 3678 + if (err) { 3679 + ntfs_debug("Couldn't make space for attribute.\n"); 3680 + unmap_mft_record(ni); 3681 + goto put_err_out; 3682 + } 3683 + memcpy(nctx->attr, a, le32_to_cpu(a->length)); 3684 + nctx->attr->instance = nctx->mrec->next_attr_instance; 3685 + nctx->mrec->next_attr_instance = 3686 + cpu_to_le16((le16_to_cpu(nctx->mrec->next_attr_instance) + 1) & 0xffff); 3687 + ntfs_attr_record_resize(ctx->mrec, a, 0); 3688 + mark_mft_record_dirty(ctx->ntfs_ino); 3689 + mark_mft_record_dirty(ni); 3690 + 3691 + /* Update attribute list. */ 3692 + ctx->al_entry->mft_reference = 3693 + MK_LE_MREF(ni->mft_no, le16_to_cpu(ni_mrec->sequence_number)); 3694 + ctx->al_entry->instance = nctx->attr->instance; 3695 + unmap_mft_record(ni); 3696 + put_err_out: 3697 + ntfs_attr_put_search_ctx(nctx); 3698 + return err; 3699 + } 3700 + 3701 + /* 3702 + * ntfs_attr_record_move_away - move away attribute record from it's mft record 3703 + * @ctx: attribute search context describing the attribute record 3704 + * @extra: minimum amount of free space in the new holder of record 3705 + */ 3706 + int ntfs_attr_record_move_away(struct ntfs_attr_search_ctx *ctx, int extra) 3707 + { 3708 + struct ntfs_inode *base_ni, *ni = NULL; 3709 + struct mft_record *m; 3710 + int i, err; 3711 + struct super_block *sb; 3712 + 3713 + if (!ctx || !ctx->attr || !ctx->ntfs_ino || extra < 0) 3714 + return -EINVAL; 3715 + 3716 + ntfs_debug("Entering for attr 0x%x, inode %llu\n", 3717 + (unsigned int) le32_to_cpu(ctx->attr->type), 3718 + (unsigned long long)ctx->ntfs_ino->mft_no); 3719 + 3720 + if (ctx->ntfs_ino->nr_extents == -1) 3721 + base_ni = ctx->base_ntfs_ino; 3722 + else 3723 + base_ni = ctx->ntfs_ino; 3724 + 3725 + sb = ctx->ntfs_ino->vol->sb; 3726 + if (!NInoAttrList(base_ni)) { 3727 + ntfs_error(sb, "Inode %llu has no attrlist", 3728 + (unsigned long long)base_ni->mft_no); 3729 + return -EINVAL; 3730 + } 3731 + 3732 + err = ntfs_inode_attach_all_extents(ctx->ntfs_ino); 3733 + if (err) { 3734 + ntfs_error(sb, "Couldn't attach extents, inode=%llu", 3735 + (unsigned long long)base_ni->mft_no); 3736 + return err; 3737 + } 3738 + 3739 + mutex_lock(&base_ni->extent_lock); 3740 + /* Walk through all extents and try to move attribute to them. */ 3741 + for (i = 0; i < base_ni->nr_extents; i++) { 3742 + ni = base_ni->ext.extent_ntfs_inos[i]; 3743 + 3744 + if (ctx->ntfs_ino->mft_no == ni->mft_no) 3745 + continue; 3746 + m = map_mft_record(ni); 3747 + if (IS_ERR(m)) { 3748 + ntfs_error(sb, "Can not map mft record for mft_no %lld", 3749 + (unsigned long long)ni->mft_no); 3750 + mutex_unlock(&base_ni->extent_lock); 3751 + return -EIO; 3752 + } 3753 + if (le32_to_cpu(m->bytes_allocated) - 3754 + le32_to_cpu(m->bytes_in_use) < le32_to_cpu(ctx->attr->length) + extra) { 3755 + unmap_mft_record(ni); 3756 + continue; 3757 + } 3758 + unmap_mft_record(ni); 3759 + 3760 + /* 3761 + * ntfs_attr_record_move_to can fail if extent with other lowest 3762 + * s64 already present in inode we trying move record to. So, 3763 + * do not return error. 3764 + */ 3765 + if (!ntfs_attr_record_move_to(ctx, ni)) { 3766 + mutex_unlock(&base_ni->extent_lock); 3767 + return 0; 3768 + } 3769 + } 3770 + mutex_unlock(&base_ni->extent_lock); 3771 + 3772 + /* 3773 + * Failed to move attribute to one of the current extents, so allocate 3774 + * new extent and move attribute to it. 3775 + */ 3776 + ni = NULL; 3777 + err = ntfs_mft_record_alloc(base_ni->vol, 0, &ni, base_ni, NULL); 3778 + if (err) { 3779 + ntfs_error(sb, "Couldn't allocate MFT record, err : %d", err); 3780 + return err; 3781 + } 3782 + unmap_mft_record(ni); 3783 + 3784 + err = ntfs_attr_record_move_to(ctx, ni); 3785 + if (err) 3786 + ntfs_error(sb, "Couldn't move attribute to MFT record"); 3787 + 3788 + return err; 3789 + } 3790 + 3791 + /* 3792 + * If we are in the first extent, then set/clean sparse bit, 3793 + * update allocated and compressed size. 3794 + */ 3795 + static int ntfs_attr_update_meta(struct attr_record *a, struct ntfs_inode *ni, 3796 + struct mft_record *m, struct ntfs_attr_search_ctx *ctx) 3797 + { 3798 + int sparse, err = 0; 3799 + struct ntfs_inode *base_ni; 3800 + struct super_block *sb = ni->vol->sb; 3801 + 3802 + ntfs_debug("Entering for inode 0x%llx, attr 0x%x\n", 3803 + (unsigned long long)ni->mft_no, ni->type); 3804 + 3805 + if (NInoAttr(ni)) 3806 + base_ni = ni->ext.base_ntfs_ino; 3807 + else 3808 + base_ni = ni; 3809 + 3810 + if (a->data.non_resident.lowest_vcn) 3811 + goto out; 3812 + 3813 + a->data.non_resident.allocated_size = cpu_to_le64(ni->allocated_size); 3814 + 3815 + sparse = ntfs_rl_sparse(ni->runlist.rl); 3816 + if (sparse < 0) { 3817 + err = -EIO; 3818 + goto out; 3819 + } 3820 + 3821 + /* Attribute become sparse. */ 3822 + if (sparse && !(a->flags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED))) { 3823 + /* 3824 + * Move attribute to another mft record, if attribute is too 3825 + * small to add compressed_size field to it and we have no 3826 + * free space in the current mft record. 3827 + */ 3828 + if ((le32_to_cpu(a->length) - 3829 + le16_to_cpu(a->data.non_resident.mapping_pairs_offset) == 8) && 3830 + !(le32_to_cpu(m->bytes_allocated) - le32_to_cpu(m->bytes_in_use))) { 3831 + 3832 + if (!NInoAttrList(base_ni)) { 3833 + err = ntfs_inode_add_attrlist(base_ni); 3834 + if (err) 3835 + goto out; 3836 + err = -EAGAIN; 3837 + goto out; 3838 + } 3839 + err = ntfs_attr_record_move_away(ctx, 8); 3840 + if (err) { 3841 + ntfs_error(sb, "Failed to move attribute"); 3842 + goto out; 3843 + } 3844 + 3845 + err = ntfs_attrlist_update(base_ni); 3846 + if (err) 3847 + goto out; 3848 + err = -EAGAIN; 3849 + goto out; 3850 + } 3851 + if (!(le32_to_cpu(a->length) - 3852 + le16_to_cpu(a->data.non_resident.mapping_pairs_offset))) { 3853 + err = -EIO; 3854 + ntfs_error(sb, "Mapping pairs space is 0"); 3855 + goto out; 3856 + } 3857 + 3858 + NInoSetSparse(ni); 3859 + ni->flags |= FILE_ATTR_SPARSE_FILE; 3860 + a->flags |= ATTR_IS_SPARSE; 3861 + a->data.non_resident.compression_unit = 0; 3862 + 3863 + memmove((u8 *)a + le16_to_cpu(a->name_offset) + 8, 3864 + (u8 *)a + le16_to_cpu(a->name_offset), 3865 + a->name_length * sizeof(__le16)); 3866 + 3867 + a->name_offset = cpu_to_le16(le16_to_cpu(a->name_offset) + 8); 3868 + 3869 + a->data.non_resident.mapping_pairs_offset = 3870 + cpu_to_le16(le16_to_cpu(a->data.non_resident.mapping_pairs_offset) + 8); 3871 + } 3872 + 3873 + /* Attribute no longer sparse. */ 3874 + if (!sparse && (a->flags & ATTR_IS_SPARSE) && 3875 + !(a->flags & ATTR_IS_COMPRESSED)) { 3876 + NInoClearSparse(ni); 3877 + ni->flags &= ~FILE_ATTR_SPARSE_FILE; 3878 + a->flags &= ~ATTR_IS_SPARSE; 3879 + a->data.non_resident.compression_unit = 0; 3880 + 3881 + memmove((u8 *)a + le16_to_cpu(a->name_offset) - 8, 3882 + (u8 *)a + le16_to_cpu(a->name_offset), 3883 + a->name_length * sizeof(__le16)); 3884 + 3885 + if (le16_to_cpu(a->name_offset) >= 8) 3886 + a->name_offset = cpu_to_le16(le16_to_cpu(a->name_offset) - 8); 3887 + 3888 + a->data.non_resident.mapping_pairs_offset = 3889 + cpu_to_le16(le16_to_cpu(a->data.non_resident.mapping_pairs_offset) - 8); 3890 + } 3891 + 3892 + /* Update compressed size if required. */ 3893 + if (NInoFullyMapped(ni) && (sparse || NInoCompressed(ni))) { 3894 + s64 new_compr_size; 3895 + 3896 + new_compr_size = ntfs_rl_get_compressed_size(ni->vol, ni->runlist.rl); 3897 + if (new_compr_size < 0) { 3898 + err = new_compr_size; 3899 + goto out; 3900 + } 3901 + 3902 + ni->itype.compressed.size = new_compr_size; 3903 + a->data.non_resident.compressed_size = cpu_to_le64(new_compr_size); 3904 + } 3905 + 3906 + if (NInoSparse(ni) || NInoCompressed(ni)) 3907 + VFS_I(base_ni)->i_blocks = ni->itype.compressed.size >> 9; 3908 + else 3909 + VFS_I(base_ni)->i_blocks = ni->allocated_size >> 9; 3910 + /* 3911 + * Set FILE_NAME dirty flag, to update sparse bit and 3912 + * allocated size in the index. 3913 + */ 3914 + if (ni->type == AT_DATA && ni->name == AT_UNNAMED) 3915 + NInoSetFileNameDirty(ni); 3916 + out: 3917 + return err; 3918 + } 3919 + 3920 + #define NTFS_VCN_DELETE_MARK -2 3921 + /* 3922 + * ntfs_attr_update_mapping_pairs - update mapping pairs for ntfs attribute 3923 + * @ni: non-resident ntfs inode for which we need update 3924 + * @from_vcn: update runlist starting this VCN 3925 + * 3926 + * Build mapping pairs from @na->rl and write them to the disk. Also, this 3927 + * function updates sparse bit, allocated and compressed size (allocates/frees 3928 + * space for this field if required). 3929 + * 3930 + * @na->allocated_size should be set to correct value for the new runlist before 3931 + * call to this function. Vice-versa @na->compressed_size will be calculated and 3932 + * set to correct value during this function. 3933 + */ 3934 + int ntfs_attr_update_mapping_pairs(struct ntfs_inode *ni, s64 from_vcn) 3935 + { 3936 + struct ntfs_attr_search_ctx *ctx; 3937 + struct ntfs_inode *base_ni; 3938 + struct mft_record *m; 3939 + struct attr_record *a; 3940 + s64 stop_vcn; 3941 + int err = 0, mp_size, cur_max_mp_size, exp_max_mp_size; 3942 + bool finished_build; 3943 + bool first_updated = false; 3944 + struct super_block *sb; 3945 + struct runlist_element *start_rl; 3946 + unsigned int de_cluster_count = 0; 3947 + 3948 + retry: 3949 + if (!ni || !ni->runlist.rl) 3950 + return -EINVAL; 3951 + 3952 + ntfs_debug("Entering for inode %llu, attr 0x%x\n", 3953 + (unsigned long long)ni->mft_no, ni->type); 3954 + 3955 + sb = ni->vol->sb; 3956 + if (!NInoNonResident(ni)) { 3957 + ntfs_error(sb, "%s: resident attribute", __func__); 3958 + return -EINVAL; 3959 + } 3960 + 3961 + if (ni->nr_extents == -1) 3962 + base_ni = ni->ext.base_ntfs_ino; 3963 + else 3964 + base_ni = ni; 3965 + 3966 + ctx = ntfs_attr_get_search_ctx(base_ni, NULL); 3967 + if (!ctx) { 3968 + ntfs_error(sb, "%s: Failed to get search context", __func__); 3969 + return -ENOMEM; 3970 + } 3971 + 3972 + /* Fill attribute records with new mapping pairs. */ 3973 + stop_vcn = 0; 3974 + finished_build = false; 3975 + start_rl = ni->runlist.rl; 3976 + while (!(err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 3977 + CASE_SENSITIVE, from_vcn, NULL, 0, ctx))) { 3978 + unsigned int de_cnt = 0; 3979 + 3980 + a = ctx->attr; 3981 + m = ctx->mrec; 3982 + if (!a->data.non_resident.lowest_vcn) 3983 + first_updated = true; 3984 + 3985 + /* 3986 + * If runlist is updating not from the beginning, then set 3987 + * @stop_vcn properly, i.e. to the lowest vcn of record that 3988 + * contain @from_vcn. Also we do not need @from_vcn anymore, 3989 + * set it to 0 to make ntfs_attr_lookup enumerate attributes. 3990 + */ 3991 + if (from_vcn) { 3992 + s64 first_lcn; 3993 + 3994 + stop_vcn = le64_to_cpu(a->data.non_resident.lowest_vcn); 3995 + from_vcn = 0; 3996 + /* 3997 + * Check whether the first run we need to update is 3998 + * the last run in runlist, if so, then deallocate 3999 + * all attrubute extents starting this one. 4000 + */ 4001 + first_lcn = ntfs_rl_vcn_to_lcn(ni->runlist.rl, stop_vcn); 4002 + if (first_lcn == LCN_EINVAL) { 4003 + err = -EIO; 4004 + ntfs_error(sb, "Bad runlist"); 4005 + goto put_err_out; 4006 + } 4007 + if (first_lcn == LCN_ENOENT || 4008 + first_lcn == LCN_RL_NOT_MAPPED) 4009 + finished_build = true; 4010 + } 4011 + 4012 + /* 4013 + * Check whether we finished mapping pairs build, if so mark 4014 + * extent as need to delete (by setting highest vcn to 4015 + * NTFS_VCN_DELETE_MARK (-2), we shall check it later and 4016 + * delete extent) and continue search. 4017 + */ 4018 + if (finished_build) { 4019 + ntfs_debug("Mark attr 0x%x for delete in inode 0x%lx.\n", 4020 + (unsigned int)le32_to_cpu(a->type), ctx->ntfs_ino->mft_no); 4021 + a->data.non_resident.highest_vcn = cpu_to_le64(NTFS_VCN_DELETE_MARK); 4022 + mark_mft_record_dirty(ctx->ntfs_ino); 4023 + continue; 4024 + } 4025 + 4026 + err = ntfs_attr_update_meta(a, ni, m, ctx); 4027 + if (err < 0) { 4028 + if (err == -EAGAIN) { 4029 + ntfs_attr_put_search_ctx(ctx); 4030 + goto retry; 4031 + } 4032 + goto put_err_out; 4033 + } 4034 + 4035 + /* 4036 + * Determine maximum possible length of mapping pairs, 4037 + * if we shall *not* expand space for mapping pairs. 4038 + */ 4039 + cur_max_mp_size = le32_to_cpu(a->length) - 4040 + le16_to_cpu(a->data.non_resident.mapping_pairs_offset); 4041 + /* 4042 + * Determine maximum possible length of mapping pairs in the 4043 + * current mft record, if we shall expand space for mapping 4044 + * pairs. 4045 + */ 4046 + exp_max_mp_size = le32_to_cpu(m->bytes_allocated) - 4047 + le32_to_cpu(m->bytes_in_use) + cur_max_mp_size; 4048 + 4049 + /* Get the size for the rest of mapping pairs array. */ 4050 + mp_size = ntfs_get_size_for_mapping_pairs(ni->vol, start_rl, 4051 + stop_vcn, -1, exp_max_mp_size); 4052 + if (mp_size <= 0) { 4053 + err = mp_size; 4054 + ntfs_error(sb, "%s: get MP size failed", __func__); 4055 + goto put_err_out; 4056 + } 4057 + /* Test mapping pairs for fitting in the current mft record. */ 4058 + if (mp_size > exp_max_mp_size) { 4059 + /* 4060 + * Mapping pairs of $ATTRIBUTE_LIST attribute must fit 4061 + * in the base mft record. Try to move out other 4062 + * attributes and try again. 4063 + */ 4064 + if (ni->type == AT_ATTRIBUTE_LIST) { 4065 + ntfs_attr_put_search_ctx(ctx); 4066 + if (ntfs_inode_free_space(base_ni, mp_size - 4067 + cur_max_mp_size)) { 4068 + ntfs_debug("Attribute list is too big. Defragment the volume\n"); 4069 + return -ENOSPC; 4070 + } 4071 + if (ntfs_attrlist_update(base_ni)) 4072 + return -EIO; 4073 + goto retry; 4074 + } 4075 + 4076 + /* Add attribute list if it isn't present, and retry. */ 4077 + if (!NInoAttrList(base_ni)) { 4078 + ntfs_attr_put_search_ctx(ctx); 4079 + if (ntfs_inode_add_attrlist(base_ni)) { 4080 + ntfs_error(sb, "Can not add attrlist"); 4081 + return -EIO; 4082 + } 4083 + goto retry; 4084 + } 4085 + 4086 + /* 4087 + * Set mapping pairs size to maximum possible for this 4088 + * mft record. We shall write the rest of mapping pairs 4089 + * to another MFT records. 4090 + */ 4091 + mp_size = exp_max_mp_size; 4092 + } 4093 + 4094 + /* Change space for mapping pairs if we need it. */ 4095 + if (((mp_size + 7) & ~7) != cur_max_mp_size) { 4096 + if (ntfs_attr_record_resize(m, a, 4097 + le16_to_cpu(a->data.non_resident.mapping_pairs_offset) + 4098 + mp_size)) { 4099 + err = -EIO; 4100 + ntfs_error(sb, "Failed to resize attribute"); 4101 + goto put_err_out; 4102 + } 4103 + } 4104 + 4105 + /* Update lowest vcn. */ 4106 + a->data.non_resident.lowest_vcn = cpu_to_le64(stop_vcn); 4107 + mark_mft_record_dirty(ctx->ntfs_ino); 4108 + if ((ctx->ntfs_ino->nr_extents == -1 || NInoAttrList(ctx->ntfs_ino)) && 4109 + ctx->attr->type != AT_ATTRIBUTE_LIST) { 4110 + ctx->al_entry->lowest_vcn = cpu_to_le64(stop_vcn); 4111 + err = ntfs_attrlist_update(base_ni); 4112 + if (err) 4113 + goto put_err_out; 4114 + } 4115 + 4116 + /* 4117 + * Generate the new mapping pairs array directly into the 4118 + * correct destination, i.e. the attribute record itself. 4119 + */ 4120 + err = ntfs_mapping_pairs_build(ni->vol, 4121 + (u8 *)a + le16_to_cpu(a->data.non_resident.mapping_pairs_offset), 4122 + mp_size, start_rl, stop_vcn, -1, &stop_vcn, &start_rl, &de_cnt); 4123 + if (!err) 4124 + finished_build = true; 4125 + if (!finished_build && err != -ENOSPC) { 4126 + ntfs_error(sb, "Failed to build mapping pairs"); 4127 + goto put_err_out; 4128 + } 4129 + a->data.non_resident.highest_vcn = cpu_to_le64(stop_vcn - 1); 4130 + mark_mft_record_dirty(ctx->ntfs_ino); 4131 + de_cluster_count += de_cnt; 4132 + } 4133 + 4134 + /* Check whether error occurred. */ 4135 + if (err && err != -ENOENT) { 4136 + ntfs_error(sb, "%s: Attribute lookup failed", __func__); 4137 + goto put_err_out; 4138 + } 4139 + 4140 + /* 4141 + * If the base extent was skipped in the above process, 4142 + * we still may have to update the sizes. 4143 + */ 4144 + if (!first_updated) { 4145 + ntfs_attr_reinit_search_ctx(ctx); 4146 + err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 4147 + CASE_SENSITIVE, 0, NULL, 0, ctx); 4148 + if (!err) { 4149 + a = ctx->attr; 4150 + a->data.non_resident.allocated_size = cpu_to_le64(ni->allocated_size); 4151 + if (NInoCompressed(ni) || NInoSparse(ni)) 4152 + a->data.non_resident.compressed_size = 4153 + cpu_to_le64(ni->itype.compressed.size); 4154 + /* Updating sizes taints the extent holding the attr */ 4155 + if (ni->type == AT_DATA && ni->name == AT_UNNAMED) 4156 + NInoSetFileNameDirty(ni); 4157 + mark_mft_record_dirty(ctx->ntfs_ino); 4158 + } else { 4159 + ntfs_error(sb, "Failed to update sizes in base extent\n"); 4160 + goto put_err_out; 4161 + } 4162 + } 4163 + 4164 + /* Deallocate not used attribute extents and return with success. */ 4165 + if (finished_build) { 4166 + ntfs_attr_reinit_search_ctx(ctx); 4167 + ntfs_debug("Deallocate marked extents.\n"); 4168 + while (!(err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 4169 + CASE_SENSITIVE, 0, NULL, 0, ctx))) { 4170 + if (le64_to_cpu(ctx->attr->data.non_resident.highest_vcn) != 4171 + NTFS_VCN_DELETE_MARK) 4172 + continue; 4173 + /* Remove unused attribute record. */ 4174 + err = ntfs_attr_record_rm(ctx); 4175 + if (err) { 4176 + ntfs_error(sb, "Could not remove unused attr"); 4177 + goto put_err_out; 4178 + } 4179 + ntfs_attr_reinit_search_ctx(ctx); 4180 + } 4181 + if (err && err != -ENOENT) { 4182 + ntfs_error(sb, "%s: Attr lookup failed", __func__); 4183 + goto put_err_out; 4184 + } 4185 + ntfs_debug("Deallocate done.\n"); 4186 + ntfs_attr_put_search_ctx(ctx); 4187 + goto out; 4188 + } 4189 + ntfs_attr_put_search_ctx(ctx); 4190 + ctx = NULL; 4191 + 4192 + /* Allocate new MFT records for the rest of mapping pairs. */ 4193 + while (1) { 4194 + struct ntfs_inode *ext_ni = NULL; 4195 + unsigned int de_cnt = 0; 4196 + 4197 + /* Allocate new mft record. */ 4198 + err = ntfs_mft_record_alloc(ni->vol, 0, &ext_ni, base_ni, NULL); 4199 + if (err) { 4200 + ntfs_error(sb, "Failed to allocate extent record"); 4201 + goto put_err_out; 4202 + } 4203 + unmap_mft_record(ext_ni); 4204 + 4205 + m = map_mft_record(ext_ni); 4206 + if (IS_ERR(m)) { 4207 + ntfs_error(sb, "Could not map new MFT record"); 4208 + if (ntfs_mft_record_free(ni->vol, ext_ni)) 4209 + ntfs_error(sb, "Could not free MFT record"); 4210 + ntfs_inode_close(ext_ni); 4211 + err = -ENOMEM; 4212 + ext_ni = NULL; 4213 + goto put_err_out; 4214 + } 4215 + /* 4216 + * If mapping size exceed available space, set them to 4217 + * possible maximum. 4218 + */ 4219 + cur_max_mp_size = le32_to_cpu(m->bytes_allocated) - 4220 + le32_to_cpu(m->bytes_in_use) - 4221 + (sizeof(struct attr_record) + 4222 + ((NInoCompressed(ni) || NInoSparse(ni)) ? 4223 + sizeof(a->data.non_resident.compressed_size) : 0)) - 4224 + ((sizeof(__le16) * ni->name_len + 7) & ~7); 4225 + 4226 + /* Calculate size of rest mapping pairs. */ 4227 + mp_size = ntfs_get_size_for_mapping_pairs(ni->vol, 4228 + start_rl, stop_vcn, -1, cur_max_mp_size); 4229 + if (mp_size <= 0) { 4230 + unmap_mft_record(ext_ni); 4231 + ntfs_inode_close(ext_ni); 4232 + err = mp_size; 4233 + ntfs_error(sb, "%s: get mp size failed", __func__); 4234 + goto put_err_out; 4235 + } 4236 + 4237 + if (mp_size > cur_max_mp_size) 4238 + mp_size = cur_max_mp_size; 4239 + /* Add attribute extent to new record. */ 4240 + err = ntfs_non_resident_attr_record_add(ext_ni, ni->type, 4241 + ni->name, ni->name_len, stop_vcn, mp_size, 0); 4242 + if (err < 0) { 4243 + ntfs_error(sb, "Could not add attribute extent"); 4244 + unmap_mft_record(ext_ni); 4245 + if (ntfs_mft_record_free(ni->vol, ext_ni)) 4246 + ntfs_error(sb, "Could not free MFT record"); 4247 + ntfs_inode_close(ext_ni); 4248 + goto put_err_out; 4249 + } 4250 + a = (struct attr_record *)((u8 *)m + err); 4251 + 4252 + err = ntfs_mapping_pairs_build(ni->vol, (u8 *)a + 4253 + le16_to_cpu(a->data.non_resident.mapping_pairs_offset), 4254 + mp_size, start_rl, stop_vcn, -1, &stop_vcn, &start_rl, 4255 + &de_cnt); 4256 + if (err < 0 && err != -ENOSPC) { 4257 + ntfs_error(sb, "Failed to build MP"); 4258 + unmap_mft_record(ext_ni); 4259 + if (ntfs_mft_record_free(ni->vol, ext_ni)) 4260 + ntfs_error(sb, "Couldn't free MFT record"); 4261 + goto put_err_out; 4262 + } 4263 + a->data.non_resident.highest_vcn = cpu_to_le64(stop_vcn - 1); 4264 + mark_mft_record_dirty(ext_ni); 4265 + unmap_mft_record(ext_ni); 4266 + 4267 + de_cluster_count += de_cnt; 4268 + /* All mapping pairs has been written. */ 4269 + if (!err) 4270 + break; 4271 + } 4272 + out: 4273 + if (from_vcn == 0) 4274 + ni->i_dealloc_clusters = de_cluster_count; 4275 + return 0; 4276 + 4277 + put_err_out: 4278 + if (ctx) 4279 + ntfs_attr_put_search_ctx(ctx); 4280 + return err; 4281 + } 4282 + 4283 + /* 4284 + * ntfs_attr_make_resident - convert a non-resident to a resident attribute 4285 + * @ni: open ntfs attribute to make resident 4286 + * @ctx: ntfs search context describing the attribute 4287 + * 4288 + * Convert a non-resident ntfs attribute to a resident one. 4289 + */ 4290 + static int ntfs_attr_make_resident(struct ntfs_inode *ni, struct ntfs_attr_search_ctx *ctx) 4291 + { 4292 + struct ntfs_volume *vol = ni->vol; 4293 + struct super_block *sb = vol->sb; 4294 + struct attr_record *a = ctx->attr; 4295 + int name_ofs, val_ofs, err; 4296 + s64 arec_size; 4297 + 4298 + ntfs_debug("Entering for inode 0x%llx, attr 0x%x.\n", 4299 + (unsigned long long)ni->mft_no, ni->type); 4300 + 4301 + /* Should be called for the first extent of the attribute. */ 4302 + if (le64_to_cpu(a->data.non_resident.lowest_vcn)) { 4303 + ntfs_debug("Eeek! Should be called for the first extent of the attribute. Aborting...\n"); 4304 + return -EINVAL; 4305 + } 4306 + 4307 + /* Some preliminary sanity checking. */ 4308 + if (!NInoNonResident(ni)) { 4309 + ntfs_debug("Eeek! Trying to make resident attribute resident. Aborting...\n"); 4310 + return -EINVAL; 4311 + } 4312 + 4313 + /* Make sure this is not $MFT/$BITMAP or Windows will not boot! */ 4314 + if (ni->type == AT_BITMAP && ni->mft_no == FILE_MFT) 4315 + return -EPERM; 4316 + 4317 + /* Check that the attribute is allowed to be resident. */ 4318 + err = ntfs_attr_can_be_resident(vol, ni->type); 4319 + if (err) 4320 + return err; 4321 + 4322 + if (NInoCompressed(ni) || NInoEncrypted(ni)) { 4323 + ntfs_debug("Making compressed or encrypted files resident is not implemented yet.\n"); 4324 + return -EOPNOTSUPP; 4325 + } 4326 + 4327 + /* Work out offsets into and size of the resident attribute. */ 4328 + name_ofs = 24; /* = sizeof(resident_struct attr_record); */ 4329 + val_ofs = (name_ofs + a->name_length * sizeof(__le16) + 7) & ~7; 4330 + arec_size = (val_ofs + ni->data_size + 7) & ~7; 4331 + 4332 + /* Sanity check the size before we start modifying the attribute. */ 4333 + if (le32_to_cpu(ctx->mrec->bytes_in_use) - le32_to_cpu(a->length) + 4334 + arec_size > le32_to_cpu(ctx->mrec->bytes_allocated)) { 4335 + ntfs_debug("Not enough space to make attribute resident\n"); 4336 + return -ENOSPC; 4337 + } 4338 + 4339 + /* Read and cache the whole runlist if not already done. */ 4340 + err = ntfs_attr_map_whole_runlist(ni); 4341 + if (err) 4342 + return err; 4343 + 4344 + /* Move the attribute name if it exists and update the offset. */ 4345 + if (a->name_length) { 4346 + memmove((u8 *)a + name_ofs, (u8 *)a + le16_to_cpu(a->name_offset), 4347 + a->name_length * sizeof(__le16)); 4348 + } 4349 + a->name_offset = cpu_to_le16(name_ofs); 4350 + 4351 + /* Resize the resident part of the attribute record. */ 4352 + if (ntfs_attr_record_resize(ctx->mrec, a, arec_size) < 0) { 4353 + /* 4354 + * Bug, because ntfs_attr_record_resize should not fail (we 4355 + * already checked that attribute fits MFT record). 4356 + */ 4357 + ntfs_error(ctx->ntfs_ino->vol->sb, "BUG! Failed to resize attribute record. "); 4358 + return -EIO; 4359 + } 4360 + 4361 + /* Convert the attribute record to describe a resident attribute. */ 4362 + a->non_resident = 0; 4363 + a->flags = 0; 4364 + a->data.resident.value_length = cpu_to_le32(ni->data_size); 4365 + a->data.resident.value_offset = cpu_to_le16(val_ofs); 4366 + /* 4367 + * File names cannot be non-resident so we would never see this here 4368 + * but at least it serves as a reminder that there may be attributes 4369 + * for which we do need to set this flag. (AIA) 4370 + */ 4371 + if (a->type == AT_FILE_NAME) 4372 + a->data.resident.flags = RESIDENT_ATTR_IS_INDEXED; 4373 + else 4374 + a->data.resident.flags = 0; 4375 + a->data.resident.reserved = 0; 4376 + 4377 + /* 4378 + * Deallocate clusters from the runlist. 4379 + * 4380 + * NOTE: We can use ntfs_cluster_free() because we have already mapped 4381 + * the whole run list and thus it doesn't matter that the attribute 4382 + * record is in a transiently corrupted state at this moment in time. 4383 + */ 4384 + err = ntfs_cluster_free(ni, 0, -1, ctx); 4385 + if (err) { 4386 + ntfs_error(sb, "Eeek! Failed to release allocated clusters"); 4387 + ntfs_debug("Ignoring error and leaving behind wasted clusters.\n"); 4388 + } 4389 + 4390 + /* Throw away the now unused runlist. */ 4391 + kvfree(ni->runlist.rl); 4392 + ni->runlist.rl = NULL; 4393 + ni->runlist.count = 0; 4394 + /* Update in-memory struct ntfs_attr. */ 4395 + NInoClearNonResident(ni); 4396 + NInoClearCompressed(ni); 4397 + ni->flags &= ~FILE_ATTR_COMPRESSED; 4398 + NInoClearSparse(ni); 4399 + ni->flags &= ~FILE_ATTR_SPARSE_FILE; 4400 + NInoClearEncrypted(ni); 4401 + ni->flags &= ~FILE_ATTR_ENCRYPTED; 4402 + ni->initialized_size = ni->data_size; 4403 + ni->allocated_size = ni->itype.compressed.size = (ni->data_size + 7) & ~7; 4404 + ni->itype.compressed.block_size = 0; 4405 + ni->itype.compressed.block_size_bits = ni->itype.compressed.block_clusters = 0; 4406 + return 0; 4407 + } 4408 + 4409 + /* 4410 + * ntfs_non_resident_attr_shrink - shrink a non-resident, open ntfs attribute 4411 + * @ni: non-resident ntfs attribute to shrink 4412 + * @newsize: new size (in bytes) to which to shrink the attribute 4413 + * 4414 + * Reduce the size of a non-resident, open ntfs attribute @na to @newsize bytes. 4415 + */ 4416 + static int ntfs_non_resident_attr_shrink(struct ntfs_inode *ni, const s64 newsize) 4417 + { 4418 + struct ntfs_volume *vol; 4419 + struct ntfs_attr_search_ctx *ctx; 4420 + s64 first_free_vcn; 4421 + s64 nr_freed_clusters; 4422 + int err; 4423 + struct ntfs_inode *base_ni; 4424 + 4425 + ntfs_debug("Inode 0x%llx attr 0x%x new size %lld\n", 4426 + (unsigned long long)ni->mft_no, ni->type, (long long)newsize); 4427 + 4428 + vol = ni->vol; 4429 + 4430 + if (NInoAttr(ni)) 4431 + base_ni = ni->ext.base_ntfs_ino; 4432 + else 4433 + base_ni = ni; 4434 + 4435 + /* 4436 + * Check the attribute type and the corresponding minimum size 4437 + * against @newsize and fail if @newsize is too small. 4438 + */ 4439 + err = ntfs_attr_size_bounds_check(vol, ni->type, newsize); 4440 + if (err) { 4441 + if (err == -ERANGE) 4442 + ntfs_debug("Eeek! Size bounds check failed. Aborting...\n"); 4443 + else if (err == -ENOENT) 4444 + err = -EIO; 4445 + return err; 4446 + } 4447 + 4448 + /* The first cluster outside the new allocation. */ 4449 + if (NInoCompressed(ni)) 4450 + /* 4451 + * For compressed files we must keep full compressions blocks, 4452 + * but currently we do not decompress/recompress the last 4453 + * block to truncate the data, so we may leave more allocated 4454 + * clusters than really needed. 4455 + */ 4456 + first_free_vcn = ntfs_bytes_to_cluster(vol, 4457 + ((newsize - 1) | (ni->itype.compressed.block_size - 1)) + 1); 4458 + else 4459 + first_free_vcn = 4460 + ntfs_bytes_to_cluster(vol, newsize + vol->cluster_size - 1); 4461 + 4462 + if (first_free_vcn < 0) 4463 + return -EINVAL; 4464 + /* 4465 + * Compare the new allocation with the old one and only deallocate 4466 + * clusters if there is a change. 4467 + */ 4468 + if (ntfs_bytes_to_cluster(vol, ni->allocated_size) != first_free_vcn) { 4469 + struct ntfs_attr_search_ctx *ctx; 4470 + 4471 + err = ntfs_attr_map_whole_runlist(ni); 4472 + if (err) { 4473 + ntfs_debug("Eeek! ntfs_attr_map_whole_runlist failed.\n"); 4474 + return err; 4475 + } 4476 + 4477 + ctx = ntfs_attr_get_search_ctx(ni, NULL); 4478 + if (!ctx) { 4479 + ntfs_error(vol->sb, "%s: Failed to get search context", __func__); 4480 + return -ENOMEM; 4481 + } 4482 + 4483 + /* Deallocate all clusters starting with the first free one. */ 4484 + nr_freed_clusters = ntfs_cluster_free(ni, first_free_vcn, -1, ctx); 4485 + if (nr_freed_clusters < 0) { 4486 + ntfs_debug("Eeek! Freeing of clusters failed. Aborting...\n"); 4487 + ntfs_attr_put_search_ctx(ctx); 4488 + return (int)nr_freed_clusters; 4489 + } 4490 + ntfs_attr_put_search_ctx(ctx); 4491 + 4492 + /* Truncate the runlist itself. */ 4493 + if (ntfs_rl_truncate_nolock(vol, &ni->runlist, first_free_vcn)) { 4494 + /* 4495 + * Failed to truncate the runlist, so just throw it 4496 + * away, it will be mapped afresh on next use. 4497 + */ 4498 + kvfree(ni->runlist.rl); 4499 + ni->runlist.rl = NULL; 4500 + ntfs_error(vol->sb, "Eeek! Run list truncation failed.\n"); 4501 + return -EIO; 4502 + } 4503 + 4504 + /* Prepare to mapping pairs update. */ 4505 + ni->allocated_size = ntfs_cluster_to_bytes(vol, first_free_vcn); 4506 + 4507 + if (NInoSparse(ni) || NInoCompressed(ni)) { 4508 + if (nr_freed_clusters) { 4509 + ni->itype.compressed.size -= 4510 + ntfs_cluster_to_bytes(vol, nr_freed_clusters); 4511 + VFS_I(base_ni)->i_blocks = ni->itype.compressed.size >> 9; 4512 + } 4513 + } else 4514 + VFS_I(base_ni)->i_blocks = ni->allocated_size >> 9; 4515 + 4516 + /* Write mapping pairs for new runlist. */ 4517 + err = ntfs_attr_update_mapping_pairs(ni, 0 /*first_free_vcn*/); 4518 + if (err) { 4519 + ntfs_debug("Eeek! Mapping pairs update failed. Leaving inconstant metadata. Run chkdsk.\n"); 4520 + return err; 4521 + } 4522 + } 4523 + 4524 + /* Get the first attribute record. */ 4525 + ctx = ntfs_attr_get_search_ctx(base_ni, NULL); 4526 + if (!ctx) { 4527 + ntfs_error(vol->sb, "%s: Failed to get search context", __func__); 4528 + return -ENOMEM; 4529 + } 4530 + 4531 + err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, CASE_SENSITIVE, 4532 + 0, NULL, 0, ctx); 4533 + if (err) { 4534 + if (err == -ENOENT) 4535 + err = -EIO; 4536 + ntfs_debug("Eeek! Lookup of first attribute extent failed. Leaving inconstant metadata.\n"); 4537 + goto put_err_out; 4538 + } 4539 + 4540 + /* Update data and initialized size. */ 4541 + ni->data_size = newsize; 4542 + ctx->attr->data.non_resident.data_size = cpu_to_le64(newsize); 4543 + if (newsize < ni->initialized_size) { 4544 + ni->initialized_size = newsize; 4545 + ctx->attr->data.non_resident.initialized_size = cpu_to_le64(newsize); 4546 + } 4547 + /* Update data size in the index. */ 4548 + if (ni->type == AT_DATA && ni->name == AT_UNNAMED) 4549 + NInoSetFileNameDirty(ni); 4550 + 4551 + /* If the attribute now has zero size, make it resident. */ 4552 + if (!newsize && !NInoEncrypted(ni) && !NInoCompressed(ni)) { 4553 + err = ntfs_attr_make_resident(ni, ctx); 4554 + if (err) { 4555 + /* If couldn't make resident, just continue. */ 4556 + if (err != -EPERM) 4557 + ntfs_error(ni->vol->sb, 4558 + "Failed to make attribute resident. Leaving as is...\n"); 4559 + } 4560 + } 4561 + 4562 + /* Set the inode dirty so it is written out later. */ 4563 + mark_mft_record_dirty(ctx->ntfs_ino); 4564 + /* Done! */ 4565 + ntfs_attr_put_search_ctx(ctx); 4566 + return 0; 4567 + put_err_out: 4568 + ntfs_attr_put_search_ctx(ctx); 4569 + return err; 4570 + } 4571 + 4572 + /* 4573 + * ntfs_non_resident_attr_expand - expand a non-resident, open ntfs attribute 4574 + * @ni: non-resident ntfs attribute to expand 4575 + * @prealloc_size: preallocation size (in bytes) to which to expand the attribute 4576 + * @newsize: new size (in bytes) to which to expand the attribute 4577 + * @holes: how to create a hole if expanding 4578 + * @need_lock: whether mrec lock is needed or not 4579 + * 4580 + * Expand the size of a non-resident, open ntfs attribute @na to @newsize bytes, 4581 + * by allocating new clusters. 4582 + */ 4583 + static int ntfs_non_resident_attr_expand(struct ntfs_inode *ni, const s64 newsize, 4584 + const s64 prealloc_size, unsigned int holes, bool need_lock) 4585 + { 4586 + s64 lcn_seek_from; 4587 + s64 first_free_vcn; 4588 + struct ntfs_volume *vol; 4589 + struct ntfs_attr_search_ctx *ctx = NULL; 4590 + struct runlist_element *rl, *rln; 4591 + s64 org_alloc_size, org_compressed_size; 4592 + int err, err2; 4593 + struct ntfs_inode *base_ni; 4594 + struct super_block *sb = ni->vol->sb; 4595 + size_t new_rl_count; 4596 + 4597 + ntfs_debug("Inode 0x%llx, attr 0x%x, new size %lld old size %lld\n", 4598 + (unsigned long long)ni->mft_no, ni->type, 4599 + (long long)newsize, (long long)ni->data_size); 4600 + 4601 + vol = ni->vol; 4602 + 4603 + if (NInoAttr(ni)) 4604 + base_ni = ni->ext.base_ntfs_ino; 4605 + else 4606 + base_ni = ni; 4607 + 4608 + /* 4609 + * Check the attribute type and the corresponding maximum size 4610 + * against @newsize and fail if @newsize is too big. 4611 + */ 4612 + err = ntfs_attr_size_bounds_check(vol, ni->type, newsize); 4613 + if (err < 0) { 4614 + ntfs_error(sb, "%s: bounds check failed", __func__); 4615 + return err; 4616 + } 4617 + 4618 + /* Save for future use. */ 4619 + org_alloc_size = ni->allocated_size; 4620 + org_compressed_size = ni->itype.compressed.size; 4621 + 4622 + /* The first cluster outside the new allocation. */ 4623 + if (prealloc_size) 4624 + first_free_vcn = 4625 + ntfs_bytes_to_cluster(vol, prealloc_size + vol->cluster_size - 1); 4626 + else 4627 + first_free_vcn = 4628 + ntfs_bytes_to_cluster(vol, newsize + vol->cluster_size - 1); 4629 + if (first_free_vcn < 0) 4630 + return -EFBIG; 4631 + 4632 + /* 4633 + * Compare the new allocation with the old one and only allocate 4634 + * clusters if there is a change. 4635 + */ 4636 + if (ntfs_bytes_to_cluster(vol, ni->allocated_size) < first_free_vcn) { 4637 + err = ntfs_attr_map_whole_runlist(ni); 4638 + if (err) { 4639 + ntfs_error(sb, "ntfs_attr_map_whole_runlist failed"); 4640 + return err; 4641 + } 4642 + 4643 + /* 4644 + * If we extend $DATA attribute on NTFS 3+ volume, we can add 4645 + * sparse runs instead of real allocation of clusters. 4646 + */ 4647 + if ((ni->type == AT_DATA && (vol->major_ver >= 3 || !NInoSparseDisabled(ni))) && 4648 + (holes != HOLES_NO)) { 4649 + if (NInoCompressed(ni)) { 4650 + int last = 0, i = 0; 4651 + s64 alloc_size; 4652 + u64 more_entries = round_up(first_free_vcn - 4653 + ntfs_bytes_to_cluster(vol, ni->allocated_size), 4654 + ni->itype.compressed.block_clusters); 4655 + 4656 + do_div(more_entries, ni->itype.compressed.block_clusters); 4657 + 4658 + while (ni->runlist.rl[last].length) 4659 + last++; 4660 + 4661 + rl = ntfs_rl_realloc(ni->runlist.rl, last + 1, 4662 + last + more_entries + 1); 4663 + if (IS_ERR(rl)) { 4664 + err = -ENOMEM; 4665 + goto put_err_out; 4666 + } 4667 + 4668 + alloc_size = ni->allocated_size; 4669 + while (i++ < more_entries) { 4670 + rl[last].vcn = ntfs_bytes_to_cluster(vol, 4671 + round_up(alloc_size, vol->cluster_size)); 4672 + rl[last].length = ni->itype.compressed.block_clusters - 4673 + (rl[last].vcn & 4674 + (ni->itype.compressed.block_clusters - 1)); 4675 + rl[last].lcn = LCN_HOLE; 4676 + last++; 4677 + alloc_size += ni->itype.compressed.block_size; 4678 + } 4679 + 4680 + rl[last].vcn = first_free_vcn; 4681 + rl[last].lcn = LCN_ENOENT; 4682 + rl[last].length = 0; 4683 + 4684 + ni->runlist.rl = rl; 4685 + ni->runlist.count += more_entries; 4686 + } else { 4687 + rl = kmalloc(sizeof(struct runlist_element) * 2, GFP_NOFS); 4688 + if (!rl) { 4689 + err = -ENOMEM; 4690 + goto put_err_out; 4691 + } 4692 + 4693 + rl[0].vcn = ntfs_bytes_to_cluster(vol, ni->allocated_size); 4694 + rl[0].lcn = LCN_HOLE; 4695 + rl[0].length = first_free_vcn - 4696 + ntfs_bytes_to_cluster(vol, ni->allocated_size); 4697 + rl[1].vcn = first_free_vcn; 4698 + rl[1].lcn = LCN_ENOENT; 4699 + rl[1].length = 0; 4700 + } 4701 + } else { 4702 + /* 4703 + * Determine first after last LCN of attribute. 4704 + * We will start seek clusters from this LCN to avoid 4705 + * fragmentation. If there are no valid LCNs in the 4706 + * attribute let the cluster allocator choose the 4707 + * starting LCN. 4708 + */ 4709 + lcn_seek_from = -1; 4710 + if (ni->runlist.rl->length) { 4711 + /* Seek to the last run list element. */ 4712 + for (rl = ni->runlist.rl; (rl + 1)->length; rl++) 4713 + ; 4714 + /* 4715 + * If the last LCN is a hole or similar seek 4716 + * back to last valid LCN. 4717 + */ 4718 + while (rl->lcn < 0 && rl != ni->runlist.rl) 4719 + rl--; 4720 + /* 4721 + * Only set lcn_seek_from it the LCN is valid. 4722 + */ 4723 + if (rl->lcn >= 0) 4724 + lcn_seek_from = rl->lcn + rl->length; 4725 + } 4726 + 4727 + rl = ntfs_cluster_alloc(vol, 4728 + ntfs_bytes_to_cluster(vol, ni->allocated_size), 4729 + first_free_vcn - 4730 + ntfs_bytes_to_cluster(vol, ni->allocated_size), 4731 + lcn_seek_from, DATA_ZONE, false, false, false); 4732 + if (IS_ERR(rl)) { 4733 + ntfs_debug("Cluster allocation failed (%lld)", 4734 + (long long)first_free_vcn - 4735 + ntfs_bytes_to_cluster(vol, ni->allocated_size)); 4736 + return PTR_ERR(rl); 4737 + } 4738 + } 4739 + 4740 + if (!NInoCompressed(ni)) { 4741 + /* Append new clusters to attribute runlist. */ 4742 + rln = ntfs_runlists_merge(&ni->runlist, rl, 0, &new_rl_count); 4743 + if (IS_ERR(rln)) { 4744 + /* Failed, free just allocated clusters. */ 4745 + ntfs_error(sb, "Run list merge failed"); 4746 + ntfs_cluster_free_from_rl(vol, rl); 4747 + kvfree(rl); 4748 + return -EIO; 4749 + } 4750 + ni->runlist.rl = rln; 4751 + ni->runlist.count = new_rl_count; 4752 + } 4753 + 4754 + /* Prepare to mapping pairs update. */ 4755 + ni->allocated_size = ntfs_cluster_to_bytes(vol, first_free_vcn); 4756 + err = ntfs_attr_update_mapping_pairs(ni, 0); 4757 + if (err) { 4758 + ntfs_debug("Mapping pairs update failed"); 4759 + goto rollback; 4760 + } 4761 + } 4762 + 4763 + ctx = ntfs_attr_get_search_ctx(base_ni, NULL); 4764 + if (!ctx) { 4765 + err = -ENOMEM; 4766 + if (ni->allocated_size == org_alloc_size) 4767 + return err; 4768 + goto rollback; 4769 + } 4770 + 4771 + err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, CASE_SENSITIVE, 4772 + 0, NULL, 0, ctx); 4773 + if (err) { 4774 + if (err == -ENOENT) 4775 + err = -EIO; 4776 + if (ni->allocated_size != org_alloc_size) 4777 + goto rollback; 4778 + goto put_err_out; 4779 + } 4780 + 4781 + /* Update data size. */ 4782 + ni->data_size = newsize; 4783 + ctx->attr->data.non_resident.data_size = cpu_to_le64(newsize); 4784 + /* Update data size in the index. */ 4785 + if (ni->type == AT_DATA && ni->name == AT_UNNAMED) 4786 + NInoSetFileNameDirty(ni); 4787 + /* Set the inode dirty so it is written out later. */ 4788 + mark_mft_record_dirty(ctx->ntfs_ino); 4789 + /* Done! */ 4790 + ntfs_attr_put_search_ctx(ctx); 4791 + return 0; 4792 + rollback: 4793 + /* Free allocated clusters. */ 4794 + err2 = ntfs_cluster_free(ni, ntfs_bytes_to_cluster(vol, org_alloc_size), 4795 + -1, ctx); 4796 + if (err2) 4797 + ntfs_debug("Leaking clusters"); 4798 + 4799 + /* Now, truncate the runlist itself. */ 4800 + if (need_lock) 4801 + down_write(&ni->runlist.lock); 4802 + err2 = ntfs_rl_truncate_nolock(vol, &ni->runlist, 4803 + ntfs_bytes_to_cluster(vol, org_alloc_size)); 4804 + if (need_lock) 4805 + up_write(&ni->runlist.lock); 4806 + if (err2) { 4807 + /* 4808 + * Failed to truncate the runlist, so just throw it away, it 4809 + * will be mapped afresh on next use. 4810 + */ 4811 + kvfree(ni->runlist.rl); 4812 + ni->runlist.rl = NULL; 4813 + ntfs_error(sb, "Couldn't truncate runlist. Rollback failed"); 4814 + } else { 4815 + /* Prepare to mapping pairs update. */ 4816 + ni->allocated_size = org_alloc_size; 4817 + /* Restore mapping pairs. */ 4818 + if (need_lock) 4819 + down_read(&ni->runlist.lock); 4820 + if (ntfs_attr_update_mapping_pairs(ni, 0)) 4821 + ntfs_error(sb, "Failed to restore old mapping pairs"); 4822 + if (need_lock) 4823 + up_read(&ni->runlist.lock); 4824 + 4825 + if (NInoSparse(ni) || NInoCompressed(ni)) { 4826 + ni->itype.compressed.size = org_compressed_size; 4827 + VFS_I(base_ni)->i_blocks = ni->itype.compressed.size >> 9; 4828 + } else 4829 + VFS_I(base_ni)->i_blocks = ni->allocated_size >> 9; 4830 + } 4831 + if (ctx) 4832 + ntfs_attr_put_search_ctx(ctx); 4833 + return err; 4834 + put_err_out: 4835 + if (ctx) 4836 + ntfs_attr_put_search_ctx(ctx); 4837 + return err; 4838 + } 4839 + 4840 + /* 4841 + * ntfs_resident_attr_resize - resize a resident, open ntfs attribute 4842 + * @attr_ni: resident ntfs inode to resize 4843 + * @newsize: new size (in bytes) to which to resize the attribute 4844 + * @prealloc_size: preallocation size (in bytes) to which to resize the attribute 4845 + * @holes: flags indicating how to handle holes 4846 + * 4847 + * Change the size of a resident, open ntfs attribute @na to @newsize bytes. 4848 + */ 4849 + static int ntfs_resident_attr_resize(struct ntfs_inode *attr_ni, const s64 newsize, 4850 + const s64 prealloc_size, unsigned int holes) 4851 + { 4852 + struct ntfs_attr_search_ctx *ctx; 4853 + struct ntfs_volume *vol = attr_ni->vol; 4854 + struct super_block *sb = vol->sb; 4855 + int err = -EIO; 4856 + struct ntfs_inode *base_ni, *ext_ni = NULL; 4857 + 4858 + attr_resize_again: 4859 + ntfs_debug("Inode 0x%llx attr 0x%x new size %lld\n", 4860 + (unsigned long long)attr_ni->mft_no, attr_ni->type, 4861 + (long long)newsize); 4862 + 4863 + if (NInoAttr(attr_ni)) 4864 + base_ni = attr_ni->ext.base_ntfs_ino; 4865 + else 4866 + base_ni = attr_ni; 4867 + 4868 + /* Get the attribute record that needs modification. */ 4869 + ctx = ntfs_attr_get_search_ctx(base_ni, NULL); 4870 + if (!ctx) { 4871 + ntfs_error(sb, "%s: Failed to get search context", __func__); 4872 + return -ENOMEM; 4873 + } 4874 + 4875 + err = ntfs_attr_lookup(attr_ni->type, attr_ni->name, attr_ni->name_len, 4876 + 0, 0, NULL, 0, ctx); 4877 + if (err) { 4878 + ntfs_error(sb, "ntfs_attr_lookup failed"); 4879 + goto put_err_out; 4880 + } 4881 + 4882 + /* 4883 + * Check the attribute type and the corresponding minimum and maximum 4884 + * sizes against @newsize and fail if @newsize is out of bounds. 4885 + */ 4886 + err = ntfs_attr_size_bounds_check(vol, attr_ni->type, newsize); 4887 + if (err) { 4888 + if (err == -ENOENT) 4889 + err = -EIO; 4890 + ntfs_debug("%s: bounds check failed", __func__); 4891 + goto put_err_out; 4892 + } 4893 + /* 4894 + * If @newsize is bigger than the mft record we need to make the 4895 + * attribute non-resident if the attribute type supports it. If it is 4896 + * smaller we can go ahead and attempt the resize. 4897 + */ 4898 + if (newsize < vol->mft_record_size) { 4899 + /* Perform the resize of the attribute record. */ 4900 + err = ntfs_resident_attr_value_resize(ctx->mrec, ctx->attr, 4901 + newsize); 4902 + if (!err) { 4903 + /* Update attribute size everywhere. */ 4904 + attr_ni->data_size = attr_ni->initialized_size = newsize; 4905 + attr_ni->allocated_size = (newsize + 7) & ~7; 4906 + if (NInoCompressed(attr_ni) || NInoSparse(attr_ni)) 4907 + attr_ni->itype.compressed.size = attr_ni->allocated_size; 4908 + if (attr_ni->type == AT_DATA && attr_ni->name == AT_UNNAMED) 4909 + NInoSetFileNameDirty(attr_ni); 4910 + goto resize_done; 4911 + } 4912 + 4913 + /* Prefer AT_INDEX_ALLOCATION instead of AT_ATTRIBUTE_LIST */ 4914 + if (err == -ENOSPC && ctx->attr->type == AT_INDEX_ROOT) 4915 + goto put_err_out; 4916 + 4917 + } 4918 + /* There is not enough space in the mft record to perform the resize. */ 4919 + 4920 + /* Make the attribute non-resident if possible. */ 4921 + err = ntfs_attr_make_non_resident(attr_ni, 4922 + le32_to_cpu(ctx->attr->data.resident.value_length)); 4923 + if (!err) { 4924 + mark_mft_record_dirty(ctx->ntfs_ino); 4925 + ntfs_attr_put_search_ctx(ctx); 4926 + /* Resize non-resident attribute */ 4927 + return ntfs_non_resident_attr_expand(attr_ni, newsize, prealloc_size, holes, true); 4928 + } else if (err != -ENOSPC && err != -EPERM) { 4929 + ntfs_error(sb, "Failed to make attribute non-resident"); 4930 + goto put_err_out; 4931 + } 4932 + 4933 + /* Try to make other attributes non-resident and retry each time. */ 4934 + ntfs_attr_reinit_search_ctx(ctx); 4935 + while (!(err = ntfs_attr_lookup(AT_UNUSED, NULL, 0, 0, 0, NULL, 0, ctx))) { 4936 + struct inode *tvi; 4937 + struct attr_record *a; 4938 + 4939 + a = ctx->attr; 4940 + if (a->non_resident || a->type == AT_ATTRIBUTE_LIST) 4941 + continue; 4942 + 4943 + if (ntfs_attr_can_be_non_resident(vol, a->type)) 4944 + continue; 4945 + 4946 + /* 4947 + * Check out whether convert is reasonable. Assume that mapping 4948 + * pairs will take 8 bytes. 4949 + */ 4950 + if (le32_to_cpu(a->length) <= (sizeof(struct attr_record) - sizeof(s64)) + 4951 + ((a->name_length * sizeof(__le16) + 7) & ~7) + 8) 4952 + continue; 4953 + 4954 + if (a->type == AT_DATA) 4955 + tvi = ntfs_iget(sb, base_ni->mft_no); 4956 + else 4957 + tvi = ntfs_attr_iget(VFS_I(base_ni), a->type, 4958 + (__le16 *)((u8 *)a + le16_to_cpu(a->name_offset)), 4959 + a->name_length); 4960 + if (IS_ERR(tvi)) { 4961 + ntfs_error(sb, "Couldn't open attribute"); 4962 + continue; 4963 + } 4964 + 4965 + if (ntfs_attr_make_non_resident(NTFS_I(tvi), 4966 + le32_to_cpu(ctx->attr->data.resident.value_length))) { 4967 + iput(tvi); 4968 + continue; 4969 + } 4970 + 4971 + mark_mft_record_dirty(ctx->ntfs_ino); 4972 + iput(tvi); 4973 + ntfs_attr_put_search_ctx(ctx); 4974 + goto attr_resize_again; 4975 + } 4976 + 4977 + /* Check whether error occurred. */ 4978 + if (err != -ENOENT) { 4979 + ntfs_error(sb, "%s: Attribute lookup failed 1", __func__); 4980 + goto put_err_out; 4981 + } 4982 + 4983 + /* 4984 + * The standard information and attribute list attributes can't be 4985 + * moved out from the base MFT record, so try to move out others. 4986 + */ 4987 + if (attr_ni->type == AT_STANDARD_INFORMATION || 4988 + attr_ni->type == AT_ATTRIBUTE_LIST) { 4989 + ntfs_attr_put_search_ctx(ctx); 4990 + 4991 + if (!NInoAttrList(base_ni)) { 4992 + err = ntfs_inode_add_attrlist(base_ni); 4993 + if (err) 4994 + return err; 4995 + } 4996 + 4997 + err = ntfs_inode_free_space(base_ni, sizeof(struct attr_record)); 4998 + if (err) { 4999 + err = -ENOSPC; 5000 + ntfs_error(sb, 5001 + "Couldn't free space in the MFT record to make attribute list non resident"); 5002 + return err; 5003 + } 5004 + err = ntfs_attrlist_update(base_ni); 5005 + if (err) 5006 + return err; 5007 + goto attr_resize_again; 5008 + } 5009 + 5010 + /* 5011 + * Move the attribute to a new mft record, creating an attribute list 5012 + * attribute or modifying it if it is already present. 5013 + */ 5014 + 5015 + /* Point search context back to attribute which we need resize. */ 5016 + ntfs_attr_reinit_search_ctx(ctx); 5017 + err = ntfs_attr_lookup(attr_ni->type, attr_ni->name, attr_ni->name_len, 5018 + CASE_SENSITIVE, 0, NULL, 0, ctx); 5019 + if (err) { 5020 + ntfs_error(sb, "%s: Attribute lookup failed 2", __func__); 5021 + goto put_err_out; 5022 + } 5023 + 5024 + /* 5025 + * Check whether attribute is already single in this MFT record. 5026 + * 8 added for the attribute terminator. 5027 + */ 5028 + if (le32_to_cpu(ctx->mrec->bytes_in_use) == 5029 + le16_to_cpu(ctx->mrec->attrs_offset) + le32_to_cpu(ctx->attr->length) + 8) { 5030 + err = -ENOSPC; 5031 + ntfs_debug("MFT record is filled with one attribute\n"); 5032 + goto put_err_out; 5033 + } 5034 + 5035 + /* Add attribute list if not present. */ 5036 + if (!NInoAttrList(base_ni)) { 5037 + ntfs_attr_put_search_ctx(ctx); 5038 + err = ntfs_inode_add_attrlist(base_ni); 5039 + if (err) 5040 + return err; 5041 + goto attr_resize_again; 5042 + } 5043 + 5044 + /* Allocate new mft record. */ 5045 + err = ntfs_mft_record_alloc(base_ni->vol, 0, &ext_ni, base_ni, NULL); 5046 + if (err) { 5047 + ntfs_error(sb, "Couldn't allocate MFT record"); 5048 + goto put_err_out; 5049 + } 5050 + unmap_mft_record(ext_ni); 5051 + 5052 + /* Move attribute to it. */ 5053 + err = ntfs_attr_record_move_to(ctx, ext_ni); 5054 + if (err) { 5055 + ntfs_error(sb, "Couldn't move attribute to new MFT record"); 5056 + err = -ENOMEM; 5057 + goto put_err_out; 5058 + } 5059 + 5060 + err = ntfs_attrlist_update(base_ni); 5061 + if (err < 0) 5062 + goto put_err_out; 5063 + 5064 + ntfs_attr_put_search_ctx(ctx); 5065 + /* Try to perform resize once again. */ 5066 + goto attr_resize_again; 5067 + 5068 + resize_done: 5069 + /* 5070 + * Set the inode (and its base inode if it exists) dirty so it is 5071 + * written out later. 5072 + */ 5073 + mark_mft_record_dirty(ctx->ntfs_ino); 5074 + ntfs_attr_put_search_ctx(ctx); 5075 + return 0; 5076 + 5077 + put_err_out: 5078 + ntfs_attr_put_search_ctx(ctx); 5079 + return err; 5080 + } 5081 + 5082 + int __ntfs_attr_truncate_vfs(struct ntfs_inode *ni, const s64 newsize, 5083 + const s64 i_size) 5084 + { 5085 + int err = 0; 5086 + 5087 + if (newsize < 0 || 5088 + (ni->mft_no == FILE_MFT && ni->type == AT_DATA)) { 5089 + ntfs_debug("Invalid arguments passed.\n"); 5090 + return -EINVAL; 5091 + } 5092 + 5093 + ntfs_debug("Entering for inode 0x%llx, attr 0x%x, size %lld\n", 5094 + (unsigned long long)ni->mft_no, ni->type, newsize); 5095 + 5096 + if (NInoNonResident(ni)) { 5097 + if (newsize > i_size) { 5098 + down_write(&ni->runlist.lock); 5099 + err = ntfs_non_resident_attr_expand(ni, newsize, 0, 5100 + NVolDisableSparse(ni->vol) ? 5101 + HOLES_NO : HOLES_OK, 5102 + false); 5103 + up_write(&ni->runlist.lock); 5104 + } else 5105 + err = ntfs_non_resident_attr_shrink(ni, newsize); 5106 + } else 5107 + err = ntfs_resident_attr_resize(ni, newsize, 0, 5108 + NVolDisableSparse(ni->vol) ? 5109 + HOLES_NO : HOLES_OK); 5110 + ntfs_debug("Return status %d\n", err); 5111 + return err; 5112 + } 5113 + 5114 + int ntfs_attr_expand(struct ntfs_inode *ni, const s64 newsize, const s64 prealloc_size) 5115 + { 5116 + int err = 0; 5117 + 5118 + if (newsize < 0 || 5119 + (ni->mft_no == FILE_MFT && ni->type == AT_DATA)) { 5120 + ntfs_debug("Invalid arguments passed.\n"); 5121 + return -EINVAL; 5122 + } 5123 + 5124 + ntfs_debug("Entering for inode 0x%llx, attr 0x%x, size %lld\n", 5125 + (unsigned long long)ni->mft_no, ni->type, newsize); 5126 + 5127 + if (ni->data_size == newsize) { 5128 + ntfs_debug("Size is already ok\n"); 5129 + return 0; 5130 + } 5131 + 5132 + /* 5133 + * Encrypted attributes are not supported. We return access denied, 5134 + * which is what Windows NT4 does, too. 5135 + */ 5136 + if (NInoEncrypted(ni)) { 5137 + pr_err("Failed to truncate encrypted attribute"); 5138 + return -EACCES; 5139 + } 5140 + 5141 + if (NInoNonResident(ni)) { 5142 + if (newsize > ni->data_size) 5143 + err = ntfs_non_resident_attr_expand(ni, newsize, prealloc_size, 5144 + NVolDisableSparse(ni->vol) ? 5145 + HOLES_NO : HOLES_OK, true); 5146 + } else 5147 + err = ntfs_resident_attr_resize(ni, newsize, prealloc_size, 5148 + NVolDisableSparse(ni->vol) ? 5149 + HOLES_NO : HOLES_OK); 5150 + if (!err) 5151 + i_size_write(VFS_I(ni), newsize); 5152 + ntfs_debug("Return status %d\n", err); 5153 + return err; 5154 + } 5155 + 5156 + /* 5157 + * ntfs_attr_truncate_i - resize an ntfs attribute 5158 + * @ni: open ntfs inode to resize 5159 + * @newsize: new size (in bytes) to which to resize the attribute 5160 + * @holes: how to create a hole if expanding 5161 + * 5162 + * Change the size of an open ntfs attribute @na to @newsize bytes. If the 5163 + * attribute is made bigger and the attribute is resident the newly 5164 + * "allocated" space is cleared and if the attribute is non-resident the 5165 + * newly allocated space is marked as not initialised and no real allocation 5166 + * on disk is performed. 5167 + */ 5168 + int ntfs_attr_truncate_i(struct ntfs_inode *ni, const s64 newsize, unsigned int holes) 5169 + { 5170 + int err; 5171 + 5172 + if (newsize < 0 || 5173 + (ni->mft_no == FILE_MFT && ni->type == AT_DATA)) { 5174 + ntfs_debug("Invalid arguments passed.\n"); 5175 + return -EINVAL; 5176 + } 5177 + 5178 + ntfs_debug("Entering for inode 0x%llx, attr 0x%x, size %lld\n", 5179 + (unsigned long long)ni->mft_no, ni->type, newsize); 5180 + 5181 + if (ni->data_size == newsize) { 5182 + ntfs_debug("Size is already ok\n"); 5183 + return 0; 5184 + } 5185 + 5186 + /* 5187 + * Encrypted attributes are not supported. We return access denied, 5188 + * which is what Windows NT4 does, too. 5189 + */ 5190 + if (NInoEncrypted(ni)) { 5191 + pr_err("Failed to truncate encrypted attribute"); 5192 + return -EACCES; 5193 + } 5194 + 5195 + if (NInoCompressed(ni)) { 5196 + pr_err("Failed to truncate compressed attribute"); 5197 + return -EOPNOTSUPP; 5198 + } 5199 + 5200 + if (NInoNonResident(ni)) { 5201 + if (newsize > ni->data_size) 5202 + err = ntfs_non_resident_attr_expand(ni, newsize, 0, holes, true); 5203 + else 5204 + err = ntfs_non_resident_attr_shrink(ni, newsize); 5205 + } else 5206 + err = ntfs_resident_attr_resize(ni, newsize, 0, holes); 5207 + ntfs_debug("Return status %d\n", err); 5208 + return err; 5209 + } 5210 + 5211 + /* 5212 + * Resize an attribute, creating a hole if relevant 5213 + */ 5214 + int ntfs_attr_truncate(struct ntfs_inode *ni, const s64 newsize) 5215 + { 5216 + return ntfs_attr_truncate_i(ni, newsize, 5217 + NVolDisableSparse(ni->vol) ? 5218 + HOLES_NO : HOLES_OK); 5219 + } 5220 + 5221 + int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start, 5222 + s64 *lcn_count, s64 max_clu_count, bool *balloc, bool update_mp, 5223 + bool skip_holes) 5224 + { 5225 + struct ntfs_volume *vol = ni->vol; 5226 + struct ntfs_attr_search_ctx *ctx; 5227 + struct runlist_element *rl, *rlc; 5228 + s64 vcn = vcn_start, lcn, clu_count; 5229 + s64 lcn_seek_from = -1; 5230 + int err = 0; 5231 + size_t new_rl_count; 5232 + 5233 + err = ntfs_attr_map_whole_runlist(ni); 5234 + if (err) 5235 + return err; 5236 + 5237 + if (NInoAttr(ni)) 5238 + ctx = ntfs_attr_get_search_ctx(ni->ext.base_ntfs_ino, NULL); 5239 + else 5240 + ctx = ntfs_attr_get_search_ctx(ni, NULL); 5241 + if (!ctx) { 5242 + ntfs_error(vol->sb, "%s: Failed to get search context", __func__); 5243 + return -ENOMEM; 5244 + } 5245 + 5246 + err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 5247 + CASE_SENSITIVE, vcn, NULL, 0, ctx); 5248 + if (err) { 5249 + ntfs_error(vol->sb, 5250 + "ntfs_attr_lookup failed, ntfs inode(mft_no : %ld) type : 0x%x, err : %d", 5251 + ni->mft_no, ni->type, err); 5252 + goto out; 5253 + } 5254 + 5255 + rl = ntfs_attr_find_vcn_nolock(ni, vcn, ctx); 5256 + if (IS_ERR(rl)) { 5257 + ntfs_error(vol->sb, "Failed to find run after mapping runlist."); 5258 + err = PTR_ERR(rl); 5259 + goto out; 5260 + } 5261 + 5262 + lcn = ntfs_rl_vcn_to_lcn(rl, vcn); 5263 + clu_count = min(max_clu_count, rl->length - (vcn - rl->vcn)); 5264 + if (lcn >= LCN_HOLE) { 5265 + if (lcn > LCN_DELALLOC || 5266 + (lcn == LCN_HOLE && skip_holes)) { 5267 + *lcn_start = lcn; 5268 + *lcn_count = clu_count; 5269 + *balloc = false; 5270 + goto out; 5271 + } 5272 + } else { 5273 + WARN_ON(lcn == LCN_RL_NOT_MAPPED); 5274 + if (lcn == LCN_ENOENT) 5275 + err = -ENOENT; 5276 + else 5277 + err = -EIO; 5278 + goto out; 5279 + } 5280 + 5281 + /* Search backwards to find the best lcn to start seek from. */ 5282 + rlc = rl; 5283 + while (rlc->vcn) { 5284 + rlc--; 5285 + if (rlc->lcn >= 0) { 5286 + /* 5287 + * avoid fragmenting a compressed file 5288 + * Windows does not do that, and that may 5289 + * not be desirable for files which can 5290 + * be updated 5291 + */ 5292 + if (NInoCompressed(ni)) 5293 + lcn_seek_from = rlc->lcn + rlc->length; 5294 + else 5295 + lcn_seek_from = rlc->lcn + (vcn - rlc->vcn); 5296 + break; 5297 + } 5298 + } 5299 + 5300 + if (lcn_seek_from == -1) { 5301 + /* Backwards search failed, search forwards. */ 5302 + rlc = rl; 5303 + while (rlc->length) { 5304 + rlc++; 5305 + if (rlc->lcn >= 0) { 5306 + lcn_seek_from = rlc->lcn - (rlc->vcn - vcn); 5307 + if (lcn_seek_from < -1) 5308 + lcn_seek_from = -1; 5309 + break; 5310 + } 5311 + } 5312 + } 5313 + 5314 + rlc = ntfs_cluster_alloc(vol, vcn, clu_count, lcn_seek_from, DATA_ZONE, 5315 + false, true, true); 5316 + if (IS_ERR(rlc)) { 5317 + err = PTR_ERR(rlc); 5318 + goto out; 5319 + } 5320 + 5321 + WARN_ON(rlc->vcn != vcn); 5322 + lcn = rlc->lcn; 5323 + clu_count = rlc->length; 5324 + 5325 + rl = ntfs_runlists_merge(&ni->runlist, rlc, 0, &new_rl_count); 5326 + if (IS_ERR(rl)) { 5327 + ntfs_error(vol->sb, "Failed to merge runlists"); 5328 + err = PTR_ERR(rl); 5329 + if (ntfs_cluster_free_from_rl(vol, rlc)) 5330 + ntfs_error(vol->sb, "Failed to free hot clusters."); 5331 + kvfree(rlc); 5332 + goto out; 5333 + } 5334 + ni->runlist.rl = rl; 5335 + ni->runlist.count = new_rl_count; 5336 + 5337 + if (!update_mp) { 5338 + u64 free = atomic64_read(&vol->free_clusters) * 100; 5339 + 5340 + do_div(free, vol->nr_clusters); 5341 + if (free <= 5) 5342 + update_mp = true; 5343 + } 5344 + 5345 + if (update_mp) { 5346 + ntfs_attr_reinit_search_ctx(ctx); 5347 + err = ntfs_attr_update_mapping_pairs(ni, 0); 5348 + if (err) { 5349 + int err2; 5350 + 5351 + err2 = ntfs_cluster_free(ni, vcn, clu_count, ctx); 5352 + if (err2 < 0) 5353 + ntfs_error(vol->sb, 5354 + "Failed to free cluster allocation. Leaving inconstant metadata.\n"); 5355 + goto out; 5356 + } 5357 + } else { 5358 + VFS_I(ni)->i_blocks += clu_count << (vol->cluster_size_bits - 9); 5359 + NInoSetRunlistDirty(ni); 5360 + mark_mft_record_dirty(ni); 5361 + } 5362 + 5363 + *lcn_start = lcn; 5364 + *lcn_count = clu_count; 5365 + *balloc = true; 5366 + out: 5367 + ntfs_attr_put_search_ctx(ctx); 5368 + return err; 5369 + } 5370 + 5371 + /* 5372 + * ntfs_attr_rm - remove attribute from ntfs inode 5373 + * @ni: opened ntfs attribute to delete 5374 + * 5375 + * Remove attribute and all it's extents from ntfs inode. If attribute was non 5376 + * resident also free all clusters allocated by attribute. 5377 + */ 5378 + int ntfs_attr_rm(struct ntfs_inode *ni) 5379 + { 5380 + struct ntfs_attr_search_ctx *ctx; 5381 + int err = 0, ret = 0; 5382 + struct ntfs_inode *base_ni; 5383 + struct super_block *sb = ni->vol->sb; 5384 + 5385 + if (NInoAttr(ni)) 5386 + base_ni = ni->ext.base_ntfs_ino; 5387 + else 5388 + base_ni = ni; 5389 + 5390 + ntfs_debug("Entering for inode 0x%llx, attr 0x%x.\n", 5391 + (long long) ni->mft_no, ni->type); 5392 + 5393 + /* Free cluster allocation. */ 5394 + if (NInoNonResident(ni)) { 5395 + struct ntfs_attr_search_ctx *ctx; 5396 + 5397 + err = ntfs_attr_map_whole_runlist(ni); 5398 + if (err) 5399 + return err; 5400 + ctx = ntfs_attr_get_search_ctx(ni, NULL); 5401 + if (!ctx) { 5402 + ntfs_error(sb, "%s: Failed to get search context", __func__); 5403 + return -ENOMEM; 5404 + } 5405 + 5406 + ret = ntfs_cluster_free(ni, 0, -1, ctx); 5407 + if (ret < 0) 5408 + ntfs_error(sb, 5409 + "Failed to free cluster allocation. Leaving inconstant metadata.\n"); 5410 + ntfs_attr_put_search_ctx(ctx); 5411 + } 5412 + 5413 + /* Search for attribute extents and remove them all. */ 5414 + ctx = ntfs_attr_get_search_ctx(base_ni, NULL); 5415 + if (!ctx) { 5416 + ntfs_error(sb, "%s: Failed to get search context", __func__); 5417 + return -ENOMEM; 5418 + } 5419 + while (!(err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 5420 + CASE_SENSITIVE, 0, NULL, 0, ctx))) { 5421 + err = ntfs_attr_record_rm(ctx); 5422 + if (err) { 5423 + ntfs_error(sb, 5424 + "Failed to remove attribute extent. Leaving inconstant metadata.\n"); 5425 + ret = err; 5426 + } 5427 + ntfs_attr_reinit_search_ctx(ctx); 5428 + } 5429 + ntfs_attr_put_search_ctx(ctx); 5430 + if (err != -ENOENT) { 5431 + ntfs_error(sb, "Attribute lookup failed. Probably leaving inconstant metadata.\n"); 5432 + ret = err; 5433 + } 5434 + 5435 + return ret; 5436 + } 5437 + 5438 + int ntfs_attr_exist(struct ntfs_inode *ni, const __le32 type, __le16 *name, 5439 + u32 name_len) 5440 + { 5441 + struct ntfs_attr_search_ctx *ctx; 5442 + int ret; 5443 + 5444 + ntfs_debug("Entering\n"); 5445 + 5446 + ctx = ntfs_attr_get_search_ctx(ni, NULL); 5447 + if (!ctx) { 5448 + ntfs_error(ni->vol->sb, "%s: Failed to get search context", 5449 + __func__); 5450 + return 0; 5451 + } 5452 + 5453 + ret = ntfs_attr_lookup(type, name, name_len, CASE_SENSITIVE, 5454 + 0, NULL, 0, ctx); 5455 + ntfs_attr_put_search_ctx(ctx); 5456 + 5457 + return !ret; 5458 + } 5459 + 5460 + int ntfs_attr_remove(struct ntfs_inode *ni, const __le32 type, __le16 *name, 5461 + u32 name_len) 5462 + { 5463 + struct super_block *sb; 5464 + int err; 5465 + struct inode *attr_vi; 5466 + struct ntfs_inode *attr_ni; 5467 + 5468 + ntfs_debug("Entering\n"); 5469 + 5470 + sb = ni->vol->sb; 5471 + if (!ni) { 5472 + ntfs_error(sb, "NULL inode pointer\n"); 5473 + return -EINVAL; 5474 + } 5475 + 5476 + attr_vi = ntfs_attr_iget(VFS_I(ni), type, name, name_len); 5477 + if (IS_ERR(attr_vi)) { 5478 + err = PTR_ERR(attr_vi); 5479 + ntfs_error(sb, "Failed to open attribute 0x%02x of inode 0x%llx", 5480 + type, (unsigned long long)ni->mft_no); 5481 + return err; 5482 + } 5483 + attr_ni = NTFS_I(attr_vi); 5484 + 5485 + err = ntfs_attr_rm(attr_ni); 5486 + if (err) 5487 + ntfs_error(sb, "Failed to remove attribute 0x%02x of inode 0x%llx", 5488 + type, (unsigned long long)ni->mft_no); 5489 + iput(attr_vi); 5490 + return err; 5491 + } 5492 + 5493 + /* 5494 + * ntfs_attr_readall - read the entire data from an ntfs attribute 5495 + * @ni: open ntfs inode in which the ntfs attribute resides 5496 + * @type: attribute type 5497 + * @name: attribute name in little endian Unicode or AT_UNNAMED or NULL 5498 + * @name_len: length of attribute @name in Unicode characters (if @name given) 5499 + * @data_size: if non-NULL then store here the data size 5500 + * 5501 + * This function will read the entire content of an ntfs attribute. 5502 + * If @name is AT_UNNAMED then look specifically for an unnamed attribute. 5503 + * If @name is NULL then the attribute could be either named or not. 5504 + * In both those cases @name_len is not used at all. 5505 + * 5506 + * On success a buffer is allocated with the content of the attribute 5507 + * and which needs to be freed when it's not needed anymore. If the 5508 + * @data_size parameter is non-NULL then the data size is set there. 5509 + */ 5510 + void *ntfs_attr_readall(struct ntfs_inode *ni, const __le32 type, 5511 + __le16 *name, u32 name_len, s64 *data_size) 5512 + { 5513 + struct ntfs_inode *bmp_ni; 5514 + struct inode *bmp_vi; 5515 + void *data, *ret = NULL; 5516 + s64 size; 5517 + struct super_block *sb = ni->vol->sb; 5518 + 5519 + ntfs_debug("Entering\n"); 5520 + 5521 + bmp_vi = ntfs_attr_iget(VFS_I(ni), type, name, name_len); 5522 + if (IS_ERR(bmp_vi)) { 5523 + ntfs_debug("ntfs_attr_iget failed"); 5524 + goto err_exit; 5525 + } 5526 + bmp_ni = NTFS_I(bmp_vi); 5527 + 5528 + data = kvmalloc(bmp_ni->data_size, GFP_NOFS); 5529 + if (!data) 5530 + goto out; 5531 + 5532 + size = ntfs_inode_attr_pread(VFS_I(bmp_ni), 0, bmp_ni->data_size, 5533 + (u8 *)data); 5534 + if (size != bmp_ni->data_size) { 5535 + ntfs_error(sb, "ntfs_attr_pread failed"); 5536 + kvfree(data); 5537 + goto out; 5538 + } 5539 + ret = data; 5540 + if (data_size) 5541 + *data_size = size; 5542 + out: 5543 + iput(bmp_vi); 5544 + err_exit: 5545 + ntfs_debug("\n"); 5546 + return ret; 5547 + } 5548 + 5549 + int ntfs_non_resident_attr_insert_range(struct ntfs_inode *ni, s64 start_vcn, s64 len) 5550 + { 5551 + struct ntfs_volume *vol = ni->vol; 5552 + struct runlist_element *hole_rl, *rl; 5553 + struct ntfs_attr_search_ctx *ctx; 5554 + int ret; 5555 + size_t new_rl_count; 5556 + 5557 + if (NInoAttr(ni) || ni->type != AT_DATA) 5558 + return -EOPNOTSUPP; 5559 + if (start_vcn > ntfs_bytes_to_cluster(vol, ni->allocated_size)) 5560 + return -EINVAL; 5561 + 5562 + hole_rl = kmalloc(sizeof(*hole_rl) * 2, GFP_NOFS); 5563 + if (!hole_rl) 5564 + return -ENOMEM; 5565 + hole_rl[0].vcn = start_vcn; 5566 + hole_rl[0].lcn = LCN_HOLE; 5567 + hole_rl[0].length = len; 5568 + hole_rl[1].vcn = start_vcn + len; 5569 + hole_rl[1].lcn = LCN_ENOENT; 5570 + hole_rl[1].length = 0; 5571 + 5572 + down_write(&ni->runlist.lock); 5573 + ret = ntfs_attr_map_whole_runlist(ni); 5574 + if (ret) { 5575 + up_write(&ni->runlist.lock); 5576 + return ret; 5577 + } 5578 + 5579 + rl = ntfs_rl_find_vcn_nolock(ni->runlist.rl, start_vcn); 5580 + if (!rl) { 5581 + up_write(&ni->runlist.lock); 5582 + kfree(hole_rl); 5583 + return -EIO; 5584 + } 5585 + 5586 + rl = ntfs_rl_insert_range(ni->runlist.rl, (int)ni->runlist.count, 5587 + hole_rl, 1, &new_rl_count); 5588 + if (IS_ERR(rl)) { 5589 + up_write(&ni->runlist.lock); 5590 + kfree(hole_rl); 5591 + return PTR_ERR(rl); 5592 + } 5593 + ni->runlist.rl = rl; 5594 + ni->runlist.count = new_rl_count; 5595 + 5596 + ni->allocated_size += ntfs_cluster_to_bytes(vol, len); 5597 + ni->data_size += ntfs_cluster_to_bytes(vol, len); 5598 + if (ntfs_cluster_to_bytes(vol, start_vcn) < ni->initialized_size) 5599 + ni->initialized_size += ntfs_cluster_to_bytes(vol, len); 5600 + ret = ntfs_attr_update_mapping_pairs(ni, 0); 5601 + up_write(&ni->runlist.lock); 5602 + if (ret) 5603 + return ret; 5604 + 5605 + ctx = ntfs_attr_get_search_ctx(ni, NULL); 5606 + if (!ctx) { 5607 + ret = -ENOMEM; 5608 + return ret; 5609 + } 5610 + 5611 + ret = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, CASE_SENSITIVE, 5612 + 0, NULL, 0, ctx); 5613 + if (ret) { 5614 + ntfs_attr_put_search_ctx(ctx); 5615 + return ret; 5616 + } 5617 + 5618 + ctx->attr->data.non_resident.data_size = cpu_to_le64(ni->data_size); 5619 + ctx->attr->data.non_resident.initialized_size = cpu_to_le64(ni->initialized_size); 5620 + if (ni->type == AT_DATA && ni->name == AT_UNNAMED) 5621 + NInoSetFileNameDirty(ni); 5622 + mark_mft_record_dirty(ctx->ntfs_ino); 5623 + ntfs_attr_put_search_ctx(ctx); 5624 + return ret; 5625 + } 5626 + 5627 + int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start_vcn, s64 len) 5628 + { 5629 + struct ntfs_volume *vol = ni->vol; 5630 + struct runlist_element *punch_rl, *rl; 5631 + struct ntfs_attr_search_ctx *ctx = NULL; 5632 + s64 end_vcn; 5633 + int dst_cnt; 5634 + int ret; 5635 + size_t new_rl_cnt; 5636 + 5637 + if (NInoAttr(ni) || ni->type != AT_DATA) 5638 + return -EOPNOTSUPP; 5639 + 5640 + end_vcn = ntfs_bytes_to_cluster(vol, ni->allocated_size); 5641 + if (start_vcn >= end_vcn) 5642 + return -EINVAL; 5643 + 5644 + down_write(&ni->runlist.lock); 5645 + ret = ntfs_attr_map_whole_runlist(ni); 5646 + if (ret) 5647 + return ret; 5648 + 5649 + len = min(len, end_vcn - start_vcn); 5650 + for (rl = ni->runlist.rl, dst_cnt = 0; rl && rl->length; rl++) 5651 + dst_cnt++; 5652 + rl = ntfs_rl_find_vcn_nolock(ni->runlist.rl, start_vcn); 5653 + if (!rl) { 5654 + up_write(&ni->runlist.lock); 5655 + return -EIO; 5656 + } 5657 + 5658 + rl = ntfs_rl_collapse_range(ni->runlist.rl, dst_cnt + 1, 5659 + start_vcn, len, &punch_rl, &new_rl_cnt); 5660 + if (IS_ERR(rl)) { 5661 + up_write(&ni->runlist.lock); 5662 + return PTR_ERR(rl); 5663 + } 5664 + ni->runlist.rl = rl; 5665 + ni->runlist.count = new_rl_cnt; 5666 + 5667 + ni->allocated_size -= ntfs_cluster_to_bytes(vol, len); 5668 + if (ni->data_size > ntfs_cluster_to_bytes(vol, start_vcn)) { 5669 + if (ni->data_size > ntfs_cluster_to_bytes(vol, (start_vcn + len))) 5670 + ni->data_size -= ntfs_cluster_to_bytes(vol, len); 5671 + else 5672 + ni->data_size = ntfs_cluster_to_bytes(vol, start_vcn); 5673 + } 5674 + if (ni->initialized_size > ntfs_cluster_to_bytes(vol, start_vcn)) { 5675 + if (ni->initialized_size > 5676 + ntfs_cluster_to_bytes(vol, start_vcn + len)) 5677 + ni->initialized_size -= ntfs_cluster_to_bytes(vol, len); 5678 + else 5679 + ni->initialized_size = ntfs_cluster_to_bytes(vol, start_vcn); 5680 + } 5681 + 5682 + if (ni->allocated_size > 0) { 5683 + ret = ntfs_attr_update_mapping_pairs(ni, 0); 5684 + if (ret) { 5685 + up_write(&ni->runlist.lock); 5686 + goto out_rl; 5687 + } 5688 + } 5689 + up_write(&ni->runlist.lock); 5690 + 5691 + ctx = ntfs_attr_get_search_ctx(ni, NULL); 5692 + if (!ctx) { 5693 + ret = -ENOMEM; 5694 + goto out_rl; 5695 + } 5696 + 5697 + ret = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, CASE_SENSITIVE, 5698 + 0, NULL, 0, ctx); 5699 + if (ret) 5700 + goto out_ctx; 5701 + 5702 + ctx->attr->data.non_resident.data_size = cpu_to_le64(ni->data_size); 5703 + ctx->attr->data.non_resident.initialized_size = cpu_to_le64(ni->initialized_size); 5704 + if (ni->allocated_size == 0) 5705 + ntfs_attr_make_resident(ni, ctx); 5706 + mark_mft_record_dirty(ctx->ntfs_ino); 5707 + 5708 + ret = ntfs_cluster_free_from_rl(vol, punch_rl); 5709 + if (ret) 5710 + ntfs_error(vol->sb, "Freeing of clusters failed"); 5711 + out_ctx: 5712 + if (ctx) 5713 + ntfs_attr_put_search_ctx(ctx); 5714 + out_rl: 5715 + kvfree(punch_rl); 5716 + mark_mft_record_dirty(ni); 5717 + return ret; 5718 + } 5719 + 5720 + int ntfs_non_resident_attr_punch_hole(struct ntfs_inode *ni, s64 start_vcn, s64 len) 5721 + { 5722 + struct ntfs_volume *vol = ni->vol; 5723 + struct runlist_element *punch_rl, *rl; 5724 + s64 end_vcn; 5725 + int dst_cnt; 5726 + int ret; 5727 + size_t new_rl_count; 5728 + 5729 + if (NInoAttr(ni) || ni->type != AT_DATA) 5730 + return -EOPNOTSUPP; 5731 + 5732 + end_vcn = ntfs_bytes_to_cluster(vol, ni->allocated_size); 5733 + if (start_vcn >= end_vcn) 5734 + return -EINVAL; 5735 + 5736 + down_write(&ni->runlist.lock); 5737 + ret = ntfs_attr_map_whole_runlist(ni); 5738 + if (ret) { 5739 + up_write(&ni->runlist.lock); 5740 + return ret; 5741 + } 5742 + 5743 + len = min(len, end_vcn - start_vcn + 1); 5744 + for (rl = ni->runlist.rl, dst_cnt = 0; rl && rl->length; rl++) 5745 + dst_cnt++; 5746 + rl = ntfs_rl_find_vcn_nolock(ni->runlist.rl, start_vcn); 5747 + if (!rl) { 5748 + up_write(&ni->runlist.lock); 5749 + return -EIO; 5750 + } 5751 + 5752 + rl = ntfs_rl_punch_hole(ni->runlist.rl, dst_cnt + 1, 5753 + start_vcn, len, &punch_rl, &new_rl_count); 5754 + if (IS_ERR(rl)) { 5755 + up_write(&ni->runlist.lock); 5756 + return PTR_ERR(rl); 5757 + } 5758 + ni->runlist.rl = rl; 5759 + ni->runlist.count = new_rl_count; 5760 + 5761 + ret = ntfs_attr_update_mapping_pairs(ni, 0); 5762 + up_write(&ni->runlist.lock); 5763 + if (ret) { 5764 + kvfree(punch_rl); 5765 + return ret; 5766 + } 5767 + 5768 + ret = ntfs_cluster_free_from_rl(vol, punch_rl); 5769 + if (ret) 5770 + ntfs_error(vol->sb, "Freeing of clusters failed"); 5771 + 5772 + kvfree(punch_rl); 5773 + mark_mft_record_dirty(ni); 5774 + return ret; 5775 + } 5776 + 5777 + int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bool keep_size) 5778 + { 5779 + struct ntfs_volume *vol = ni->vol; 5780 + struct mft_record *mrec; 5781 + struct ntfs_attr_search_ctx *ctx; 5782 + s64 old_data_size; 5783 + s64 vcn_start, vcn_end, vcn_uninit, vcn, try_alloc_cnt; 5784 + s64 lcn, alloc_cnt; 5785 + int err = 0; 5786 + struct runlist_element *rl; 5787 + bool balloc; 5788 + 5789 + if (NInoAttr(ni) || ni->type != AT_DATA) 5790 + return -EINVAL; 5791 + 5792 + if (NInoNonResident(ni) && !NInoFullyMapped(ni)) { 5793 + down_write(&ni->runlist.lock); 5794 + err = ntfs_attr_map_whole_runlist(ni); 5795 + up_write(&ni->runlist.lock); 5796 + if (err) 5797 + return err; 5798 + } 5799 + 5800 + mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 5801 + mrec = map_mft_record(ni); 5802 + if (IS_ERR(mrec)) { 5803 + mutex_unlock(&ni->mrec_lock); 5804 + return PTR_ERR(mrec); 5805 + } 5806 + 5807 + ctx = ntfs_attr_get_search_ctx(ni, mrec); 5808 + if (!ctx) { 5809 + err = -ENOMEM; 5810 + goto out_unmap; 5811 + } 5812 + 5813 + err = ntfs_attr_lookup(AT_DATA, AT_UNNAMED, 0, 0, 0, NULL, 0, ctx); 5814 + if (err) { 5815 + err = -EIO; 5816 + goto out_unmap; 5817 + } 5818 + 5819 + old_data_size = ni->data_size; 5820 + if (start + byte_len > ni->data_size) { 5821 + err = ntfs_attr_truncate(ni, start + byte_len); 5822 + if (err) 5823 + goto out_unmap; 5824 + if (keep_size) { 5825 + ntfs_attr_reinit_search_ctx(ctx); 5826 + err = ntfs_attr_lookup(AT_DATA, AT_UNNAMED, 0, 0, 0, NULL, 0, ctx); 5827 + if (err) { 5828 + err = -EIO; 5829 + goto out_unmap; 5830 + } 5831 + ni->data_size = old_data_size; 5832 + if (NInoNonResident(ni)) 5833 + ctx->attr->data.non_resident.data_size = 5834 + cpu_to_le64(old_data_size); 5835 + else 5836 + ctx->attr->data.resident.value_length = 5837 + cpu_to_le32((u32)old_data_size); 5838 + mark_mft_record_dirty(ni); 5839 + } 5840 + } 5841 + 5842 + ntfs_attr_put_search_ctx(ctx); 5843 + unmap_mft_record(ni); 5844 + mutex_unlock(&ni->mrec_lock); 5845 + 5846 + if (!NInoNonResident(ni)) 5847 + goto out; 5848 + 5849 + vcn_start = (s64)ntfs_bytes_to_cluster(vol, start); 5850 + vcn_end = (s64)ntfs_bytes_to_cluster(vol, 5851 + round_up(start + byte_len, vol->cluster_size)); 5852 + vcn_uninit = (s64)ntfs_bytes_to_cluster(vol, 5853 + round_up(ni->initialized_size, vol->cluster_size)); 5854 + vcn_uninit = min_t(s64, vcn_uninit, vcn_end); 5855 + 5856 + /* 5857 + * we have to allocate clusters for holes and delayed within initialized_size, 5858 + * and zero out the clusters only for the holes. 5859 + */ 5860 + vcn = vcn_start; 5861 + while (vcn < vcn_uninit) { 5862 + down_read(&ni->runlist.lock); 5863 + rl = ntfs_attr_find_vcn_nolock(ni, vcn, NULL); 5864 + up_read(&ni->runlist.lock); 5865 + if (IS_ERR(rl)) { 5866 + err = PTR_ERR(rl); 5867 + goto out; 5868 + } 5869 + 5870 + if (rl->lcn > 0) { 5871 + vcn += rl->length - (vcn - rl->vcn); 5872 + } else if (rl->lcn == LCN_DELALLOC || rl->lcn == LCN_HOLE) { 5873 + try_alloc_cnt = min(rl->length - (vcn - rl->vcn), 5874 + vcn_uninit - vcn); 5875 + 5876 + if (rl->lcn == LCN_DELALLOC) { 5877 + vcn += try_alloc_cnt; 5878 + continue; 5879 + } 5880 + 5881 + while (try_alloc_cnt > 0) { 5882 + mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 5883 + down_write(&ni->runlist.lock); 5884 + err = ntfs_attr_map_cluster(ni, vcn, &lcn, &alloc_cnt, 5885 + try_alloc_cnt, &balloc, false, false); 5886 + up_write(&ni->runlist.lock); 5887 + mutex_unlock(&ni->mrec_lock); 5888 + if (err) 5889 + goto out; 5890 + 5891 + err = ntfs_dio_zero_range(VFS_I(ni), 5892 + lcn << vol->cluster_size_bits, 5893 + alloc_cnt << vol->cluster_size_bits); 5894 + if (err > 0) 5895 + goto out; 5896 + 5897 + if (signal_pending(current)) 5898 + goto out; 5899 + 5900 + vcn += alloc_cnt; 5901 + try_alloc_cnt -= alloc_cnt; 5902 + } 5903 + } else { 5904 + err = -EIO; 5905 + goto out; 5906 + } 5907 + } 5908 + 5909 + /* allocate clusters outside of initialized_size */ 5910 + try_alloc_cnt = vcn_end - vcn; 5911 + while (try_alloc_cnt > 0) { 5912 + mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 5913 + down_write(&ni->runlist.lock); 5914 + err = ntfs_attr_map_cluster(ni, vcn, &lcn, &alloc_cnt, 5915 + try_alloc_cnt, &balloc, false, false); 5916 + up_write(&ni->runlist.lock); 5917 + mutex_unlock(&ni->mrec_lock); 5918 + if (err || signal_pending(current)) 5919 + goto out; 5920 + 5921 + vcn += alloc_cnt; 5922 + try_alloc_cnt -= alloc_cnt; 5923 + cond_resched(); 5924 + } 5925 + 5926 + if (NInoRunlistDirty(ni)) { 5927 + mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 5928 + down_write(&ni->runlist.lock); 5929 + err = ntfs_attr_update_mapping_pairs(ni, 0); 5930 + if (err) 5931 + ntfs_error(ni->vol->sb, "Updating mapping pairs failed"); 5932 + else 5933 + NInoClearRunlistDirty(ni); 5934 + up_write(&ni->runlist.lock); 5935 + mutex_unlock(&ni->mrec_lock); 5936 + } 5937 + return err; 5938 + out_unmap: 5939 + if (ctx) 5940 + ntfs_attr_put_search_ctx(ctx); 5941 + unmap_mft_record(ni); 5942 + mutex_unlock(&ni->mrec_lock); 5943 + out: 5944 + return err >= 0 ? 0 : err; 5945 + }
+289
fs/ntfs/attrlist.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Attribute list attribute handling code. 4 + * Part of this file is based on code from the NTFS-3G. 5 + * 6 + * Copyright (c) 2004-2005 Anton Altaparmakov 7 + * Copyright (c) 2004-2005 Yura Pakhuchiy 8 + * Copyright (c) 2006 Szabolcs Szakacsits 9 + * Copyright (c) 2025 LG Electronics Co., Ltd. 10 + */ 11 + 12 + #include "mft.h" 13 + #include "attrib.h" 14 + #include "attrlist.h" 15 + 16 + /* 17 + * ntfs_attrlist_need - check whether inode need attribute list 18 + * @ni: opened ntfs inode for which perform check 19 + * 20 + * Check whether all are attributes belong to one MFT record, in that case 21 + * attribute list is not needed. 22 + * 23 + * Return 1 if inode need attribute list, 0 if not, or -errno on error. 24 + */ 25 + int ntfs_attrlist_need(struct ntfs_inode *ni) 26 + { 27 + struct attr_list_entry *ale; 28 + 29 + if (!ni) { 30 + ntfs_debug("Invalid arguments.\n"); 31 + return -EINVAL; 32 + } 33 + ntfs_debug("Entering for inode 0x%llx.\n", (long long) ni->mft_no); 34 + 35 + if (!NInoAttrList(ni)) { 36 + ntfs_debug("Inode haven't got attribute list.\n"); 37 + return -EINVAL; 38 + } 39 + 40 + if (!ni->attr_list) { 41 + ntfs_debug("Corrupt in-memory struct.\n"); 42 + return -EINVAL; 43 + } 44 + 45 + ale = (struct attr_list_entry *)ni->attr_list; 46 + while ((u8 *)ale < ni->attr_list + ni->attr_list_size) { 47 + if (MREF_LE(ale->mft_reference) != ni->mft_no) 48 + return 1; 49 + ale = (struct attr_list_entry *)((u8 *)ale + le16_to_cpu(ale->length)); 50 + } 51 + return 0; 52 + } 53 + 54 + int ntfs_attrlist_update(struct ntfs_inode *base_ni) 55 + { 56 + struct inode *attr_vi; 57 + struct ntfs_inode *attr_ni; 58 + int err; 59 + 60 + attr_vi = ntfs_attr_iget(VFS_I(base_ni), AT_ATTRIBUTE_LIST, AT_UNNAMED, 0); 61 + if (IS_ERR(attr_vi)) { 62 + err = PTR_ERR(attr_vi); 63 + return err; 64 + } 65 + attr_ni = NTFS_I(attr_vi); 66 + 67 + err = ntfs_attr_truncate_i(attr_ni, base_ni->attr_list_size, HOLES_NO); 68 + if (err == -ENOSPC && attr_ni->mft_no == FILE_MFT) { 69 + err = ntfs_attr_truncate(attr_ni, 0); 70 + if (err || ntfs_attr_truncate_i(attr_ni, base_ni->attr_list_size, HOLES_NO) != 0) { 71 + iput(attr_vi); 72 + ntfs_error(base_ni->vol->sb, 73 + "Failed to truncate attribute list of inode %#llx", 74 + (long long)base_ni->mft_no); 75 + return -EIO; 76 + } 77 + } else if (err) { 78 + iput(attr_vi); 79 + ntfs_error(base_ni->vol->sb, 80 + "Failed to truncate attribute list of inode %#llx", 81 + (long long)base_ni->mft_no); 82 + return -EIO; 83 + } 84 + 85 + i_size_write(attr_vi, base_ni->attr_list_size); 86 + 87 + if (NInoNonResident(attr_ni) && !NInoAttrListNonResident(base_ni)) 88 + NInoSetAttrListNonResident(base_ni); 89 + 90 + if (ntfs_inode_attr_pwrite(attr_vi, 0, base_ni->attr_list_size, 91 + base_ni->attr_list, false) != 92 + base_ni->attr_list_size) { 93 + iput(attr_vi); 94 + ntfs_error(base_ni->vol->sb, 95 + "Failed to write attribute list of inode %#llx", 96 + (long long)base_ni->mft_no); 97 + return -EIO; 98 + } 99 + 100 + NInoSetAttrListDirty(base_ni); 101 + iput(attr_vi); 102 + return 0; 103 + } 104 + 105 + /* 106 + * ntfs_attrlist_entry_add - add an attribute list attribute entry 107 + * @ni: opened ntfs inode, which contains that attribute 108 + * @attr: attribute record to add to attribute list 109 + * 110 + * Return 0 on success and -errno on error. 111 + */ 112 + int ntfs_attrlist_entry_add(struct ntfs_inode *ni, struct attr_record *attr) 113 + { 114 + struct attr_list_entry *ale; 115 + __le64 mref; 116 + struct ntfs_attr_search_ctx *ctx; 117 + u8 *new_al; 118 + int entry_len, entry_offset, err; 119 + struct mft_record *ni_mrec; 120 + u8 *old_al; 121 + 122 + ntfs_debug("Entering for inode 0x%llx, attr 0x%x.\n", 123 + (long long) ni->mft_no, 124 + (unsigned int) le32_to_cpu(attr->type)); 125 + 126 + if (!ni || !attr) { 127 + ntfs_debug("Invalid arguments.\n"); 128 + return -EINVAL; 129 + } 130 + 131 + ni_mrec = map_mft_record(ni); 132 + if (IS_ERR(ni_mrec)) { 133 + ntfs_debug("Invalid arguments.\n"); 134 + return -EIO; 135 + } 136 + 137 + mref = MK_LE_MREF(ni->mft_no, le16_to_cpu(ni_mrec->sequence_number)); 138 + unmap_mft_record(ni); 139 + 140 + if (ni->nr_extents == -1) 141 + ni = ni->ext.base_ntfs_ino; 142 + 143 + if (!NInoAttrList(ni)) { 144 + ntfs_debug("Attribute list isn't present.\n"); 145 + return -ENOENT; 146 + } 147 + 148 + /* Determine size and allocate memory for new attribute list. */ 149 + entry_len = (sizeof(struct attr_list_entry) + sizeof(__le16) * 150 + attr->name_length + 7) & ~7; 151 + new_al = kvzalloc(ni->attr_list_size + entry_len, GFP_NOFS); 152 + if (!new_al) 153 + return -ENOMEM; 154 + 155 + /* Find place for the new entry. */ 156 + ctx = ntfs_attr_get_search_ctx(ni, NULL); 157 + if (!ctx) { 158 + err = -ENOMEM; 159 + ntfs_error(ni->vol->sb, "Failed to get search context"); 160 + goto err_out; 161 + } 162 + 163 + err = ntfs_attr_lookup(attr->type, (attr->name_length) ? (__le16 *) 164 + ((u8 *)attr + le16_to_cpu(attr->name_offset)) : 165 + AT_UNNAMED, attr->name_length, CASE_SENSITIVE, 166 + (attr->non_resident) ? le64_to_cpu(attr->data.non_resident.lowest_vcn) : 167 + 0, (attr->non_resident) ? NULL : ((u8 *)attr + 168 + le16_to_cpu(attr->data.resident.value_offset)), (attr->non_resident) ? 169 + 0 : le32_to_cpu(attr->data.resident.value_length), ctx); 170 + if (!err) { 171 + /* Found some extent, check it to be before new extent. */ 172 + if (ctx->al_entry->lowest_vcn == attr->data.non_resident.lowest_vcn) { 173 + err = -EEXIST; 174 + ntfs_debug("Such attribute already present in the attribute list.\n"); 175 + ntfs_attr_put_search_ctx(ctx); 176 + goto err_out; 177 + } 178 + /* Add new entry after this extent. */ 179 + ale = (struct attr_list_entry *)((u8 *)ctx->al_entry + 180 + le16_to_cpu(ctx->al_entry->length)); 181 + } else { 182 + /* Check for real errors. */ 183 + if (err != -ENOENT) { 184 + ntfs_debug("Attribute lookup failed.\n"); 185 + ntfs_attr_put_search_ctx(ctx); 186 + goto err_out; 187 + } 188 + /* No previous extents found. */ 189 + ale = ctx->al_entry; 190 + } 191 + /* Don't need it anymore, @ctx->al_entry points to @ni->attr_list. */ 192 + ntfs_attr_put_search_ctx(ctx); 193 + 194 + /* Determine new entry offset. */ 195 + entry_offset = ((u8 *)ale - ni->attr_list); 196 + /* Set pointer to new entry. */ 197 + ale = (struct attr_list_entry *)(new_al + entry_offset); 198 + memset(ale, 0, entry_len); 199 + /* Form new entry. */ 200 + ale->type = attr->type; 201 + ale->length = cpu_to_le16(entry_len); 202 + ale->name_length = attr->name_length; 203 + ale->name_offset = offsetof(struct attr_list_entry, name); 204 + if (attr->non_resident) 205 + ale->lowest_vcn = attr->data.non_resident.lowest_vcn; 206 + else 207 + ale->lowest_vcn = 0; 208 + ale->mft_reference = mref; 209 + ale->instance = attr->instance; 210 + memcpy(ale->name, (u8 *)attr + le16_to_cpu(attr->name_offset), 211 + attr->name_length * sizeof(__le16)); 212 + 213 + /* Copy entries from old attribute list to new. */ 214 + memcpy(new_al, ni->attr_list, entry_offset); 215 + memcpy(new_al + entry_offset + entry_len, ni->attr_list + 216 + entry_offset, ni->attr_list_size - entry_offset); 217 + 218 + /* Set new runlist. */ 219 + old_al = ni->attr_list; 220 + ni->attr_list = new_al; 221 + ni->attr_list_size = ni->attr_list_size + entry_len; 222 + 223 + err = ntfs_attrlist_update(ni); 224 + if (err) { 225 + ni->attr_list = old_al; 226 + ni->attr_list_size -= entry_len; 227 + goto err_out; 228 + } 229 + kvfree(old_al); 230 + return 0; 231 + err_out: 232 + kvfree(new_al); 233 + return err; 234 + } 235 + 236 + /* 237 + * ntfs_attrlist_entry_rm - remove an attribute list attribute entry 238 + * @ctx: attribute search context describing the attribute list entry 239 + * 240 + * Remove the attribute list entry @ctx->al_entry from the attribute list. 241 + * 242 + * Return 0 on success and -errno on error. 243 + */ 244 + int ntfs_attrlist_entry_rm(struct ntfs_attr_search_ctx *ctx) 245 + { 246 + u8 *new_al; 247 + int new_al_len; 248 + struct ntfs_inode *base_ni; 249 + struct attr_list_entry *ale; 250 + 251 + if (!ctx || !ctx->ntfs_ino || !ctx->al_entry) { 252 + ntfs_debug("Invalid arguments.\n"); 253 + return -EINVAL; 254 + } 255 + 256 + if (ctx->base_ntfs_ino) 257 + base_ni = ctx->base_ntfs_ino; 258 + else 259 + base_ni = ctx->ntfs_ino; 260 + ale = ctx->al_entry; 261 + 262 + ntfs_debug("Entering for inode 0x%llx, attr 0x%x, lowest_vcn %lld.\n", 263 + (long long)ctx->ntfs_ino->mft_no, 264 + (unsigned int)le32_to_cpu(ctx->al_entry->type), 265 + (long long)le64_to_cpu(ctx->al_entry->lowest_vcn)); 266 + 267 + if (!NInoAttrList(base_ni)) { 268 + ntfs_debug("Attribute list isn't present.\n"); 269 + return -ENOENT; 270 + } 271 + 272 + /* Allocate memory for new attribute list. */ 273 + new_al_len = base_ni->attr_list_size - le16_to_cpu(ale->length); 274 + new_al = kvzalloc(new_al_len, GFP_NOFS); 275 + if (!new_al) 276 + return -ENOMEM; 277 + 278 + /* Copy entries from old attribute list to new. */ 279 + memcpy(new_al, base_ni->attr_list, (u8 *)ale - base_ni->attr_list); 280 + memcpy(new_al + ((u8 *)ale - base_ni->attr_list), (u8 *)ale + le16_to_cpu( 281 + ale->length), new_al_len - ((u8 *)ale - base_ni->attr_list)); 282 + 283 + /* Set new runlist. */ 284 + kvfree(base_ni->attr_list); 285 + base_ni->attr_list = new_al; 286 + base_ni->attr_list_size = new_al_len; 287 + 288 + return ntfs_attrlist_update(base_ni); 289 + }
+839 -212
fs/ntfs/compress.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0-or-later 2 2 /* 3 - * compress.c - NTFS kernel compressed attributes handling. 4 - * Part of the Linux-NTFS project. 3 + * NTFS kernel compressed attributes handling. 5 4 * 6 5 * Copyright (c) 2001-2004 Anton Altaparmakov 7 6 * Copyright (c) 2002 Richard Russon 7 + * Copyright (c) 2025 LG Electronics Co., Ltd. 8 + * 9 + * Part of this file is based on code from the NTFS-3G. 10 + * and is copyrighted by the respective authors below: 11 + * Copyright (c) 2004-2005 Anton Altaparmakov 12 + * Copyright (c) 2004-2006 Szabolcs Szakacsits 13 + * Copyright (c) 2005 Yura Pakhuchiy 14 + * Copyright (c) 2009-2014 Jean-Pierre Andre 15 + * Copyright (c) 2014 Eric Biggers 8 16 */ 9 17 10 18 #include <linux/fs.h> 11 - #include <linux/buffer_head.h> 12 19 #include <linux/blkdev.h> 13 20 #include <linux/vmalloc.h> 14 21 #include <linux/slab.h> ··· 24 17 #include "inode.h" 25 18 #include "debug.h" 26 19 #include "ntfs.h" 20 + #include "lcnalloc.h" 21 + #include "mft.h" 27 22 28 - /** 29 - * ntfs_compression_constants - enum of constants used in the compression code 23 + /* 24 + * Constants used in the compression code 30 25 */ 31 - typedef enum { 26 + enum { 32 27 /* Token types and access mask. */ 33 28 NTFS_SYMBOL_TOKEN = 0, 34 29 NTFS_PHRASE_TOKEN = 1, ··· 48 39 * initializing the compression buffer. 49 40 */ 50 41 NTFS_MAX_CB_SIZE = 64 * 1024, 51 - } ntfs_compression_constants; 42 + }; 52 43 53 44 /* 54 45 * ntfs_compression_buffer - one buffer for the decompression engine ··· 56 47 static u8 *ntfs_compression_buffer; 57 48 58 49 /* 59 - * ntfs_cb_lock - spinlock which protects ntfs_compression_buffer 50 + * ntfs_cb_lock - mutex lock which protects ntfs_compression_buffer 60 51 */ 61 - static DEFINE_SPINLOCK(ntfs_cb_lock); 52 + static DEFINE_MUTEX(ntfs_cb_lock); 62 53 63 - /** 54 + /* 64 55 * allocate_compression_buffers - allocate the decompression buffers 65 56 * 66 57 * Caller has to hold the ntfs_lock mutex. ··· 69 60 */ 70 61 int allocate_compression_buffers(void) 71 62 { 72 - BUG_ON(ntfs_compression_buffer); 63 + if (ntfs_compression_buffer) 64 + return 0; 73 65 74 66 ntfs_compression_buffer = vmalloc(NTFS_MAX_CB_SIZE); 75 67 if (!ntfs_compression_buffer) ··· 78 68 return 0; 79 69 } 80 70 81 - /** 71 + /* 82 72 * free_compression_buffers - free the decompression buffers 83 73 * 84 74 * Caller has to hold the ntfs_lock mutex. 85 75 */ 86 76 void free_compression_buffers(void) 87 77 { 88 - BUG_ON(!ntfs_compression_buffer); 78 + mutex_lock(&ntfs_cb_lock); 79 + if (!ntfs_compression_buffer) { 80 + mutex_unlock(&ntfs_cb_lock); 81 + return; 82 + } 83 + 89 84 vfree(ntfs_compression_buffer); 90 85 ntfs_compression_buffer = NULL; 86 + mutex_unlock(&ntfs_cb_lock); 91 87 } 92 88 93 - /** 89 + /* 94 90 * zero_partial_compressed_page - zero out of bounds compressed page region 91 + * @page: page to zero 92 + * @initialized_size: initialized size of the attribute 95 93 */ 96 94 static void zero_partial_compressed_page(struct page *page, 97 95 const s64 initialized_size) ··· 108 90 unsigned int kp_ofs; 109 91 110 92 ntfs_debug("Zeroing page region outside initialized size."); 111 - if (((s64)page->index << PAGE_SHIFT) >= initialized_size) { 93 + if (((s64)page->__folio_index << PAGE_SHIFT) >= initialized_size) { 112 94 clear_page(kp); 113 95 return; 114 96 } 115 97 kp_ofs = initialized_size & ~PAGE_MASK; 116 98 memset(kp + kp_ofs, 0, PAGE_SIZE - kp_ofs); 117 - return; 118 99 } 119 100 120 - /** 101 + /* 121 102 * handle_bounds_compressed_page - test for&handle out of bounds compressed page 103 + * @page: page to check and handle 104 + * @i_size: file size 105 + * @initialized_size: initialized size of the attribute 122 106 */ 123 107 static inline void handle_bounds_compressed_page(struct page *page, 124 108 const loff_t i_size, const s64 initialized_size) 125 109 { 126 - if ((page->index >= (initialized_size >> PAGE_SHIFT)) && 110 + if ((page->__folio_index >= (initialized_size >> PAGE_SHIFT)) && 127 111 (initialized_size < i_size)) 128 112 zero_partial_compressed_page(page, initialized_size); 129 - return; 130 113 } 131 114 132 - /** 115 + /* 133 116 * ntfs_decompress - decompress a compression block into an array of pages 134 117 * @dest_pages: destination array of pages 135 118 * @completed_pages: scratch space to track completed pages ··· 180 161 */ 181 162 u8 *cb_end = cb_start + cb_size; /* End of cb. */ 182 163 u8 *cb = cb_start; /* Current position in cb. */ 183 - u8 *cb_sb_start; /* Beginning of the current sb in the cb. */ 164 + u8 *cb_sb_start = cb; /* Beginning of the current sb in the cb. */ 184 165 u8 *cb_sb_end; /* End of current sb / beginning of next sb. */ 185 166 186 167 /* Variables for uncompressed data / destination. */ 187 168 struct page *dp; /* Current destination page being worked on. */ 188 169 u8 *dp_addr; /* Current pointer into dp. */ 189 170 u8 *dp_sb_start; /* Start of current sub-block in dp. */ 190 - u8 *dp_sb_end; /* End of current sb in dp (dp_sb_start + 191 - NTFS_SB_SIZE). */ 171 + u8 *dp_sb_end; /* End of current sb in dp (dp_sb_start + NTFS_SB_SIZE). */ 192 172 u16 do_sb_start; /* @dest_ofs when starting this sub-block. */ 193 - u16 do_sb_end; /* @dest_ofs of end of this sb (do_sb_start + 194 - NTFS_SB_SIZE). */ 173 + u16 do_sb_end; /* @dest_ofs of end of this sb (do_sb_start + NTFS_SB_SIZE). */ 195 174 196 175 /* Variables for tag and token parsing. */ 197 176 u8 tag; /* Current tag. */ ··· 209 192 * position in the compression block is one byte before its end so the 210 193 * first two checks do not detect it. 211 194 */ 212 - if (cb == cb_end || !le16_to_cpup((le16*)cb) || 195 + if (cb == cb_end || !le16_to_cpup((__le16 *)cb) || 213 196 (*dest_index == dest_max_index && 214 197 *dest_ofs == dest_max_ofs)) { 215 198 int i; ··· 218 201 err = 0; 219 202 return_error: 220 203 /* We can sleep from now on, so we drop lock. */ 221 - spin_unlock(&ntfs_cb_lock); 204 + mutex_unlock(&ntfs_cb_lock); 222 205 /* Second stage: finalize completed pages. */ 223 206 if (nr_completed_pages > 0) { 224 207 for (i = 0; i < nr_completed_pages; i++) { ··· 232 215 handle_bounds_compressed_page(dp, i_size, 233 216 initialized_size); 234 217 flush_dcache_page(dp); 235 - kunmap(dp); 218 + kunmap_local(page_address(dp)); 236 219 SetPageUptodate(dp); 237 220 unlock_page(dp); 238 221 if (di == xpage) ··· 259 242 260 243 /* Setup the current sub-block source pointers and validate range. */ 261 244 cb_sb_start = cb; 262 - cb_sb_end = cb_sb_start + (le16_to_cpup((le16*)cb) & NTFS_SB_SIZE_MASK) 245 + cb_sb_end = cb_sb_start + (le16_to_cpup((__le16 *)cb) & NTFS_SB_SIZE_MASK) 263 246 + 3; 264 247 if (cb_sb_end > cb_end) 265 248 goto return_overflow; ··· 278 261 } 279 262 280 263 /* We have a valid destination page. Setup the destination pointers. */ 281 - dp_addr = (u8*)page_address(dp) + do_sb_start; 264 + dp_addr = (u8 *)page_address(dp) + do_sb_start; 282 265 283 266 /* Now, we are ready to process the current sub-block (sb). */ 284 - if (!(le16_to_cpup((le16*)cb) & NTFS_SB_IS_COMPRESSED)) { 267 + if (!(le16_to_cpup((__le16 *)cb) & NTFS_SB_IS_COMPRESSED)) { 285 268 ntfs_debug("Found uncompressed sub-block."); 286 269 /* This sb is not compressed, just copy it into destination. */ 287 270 ··· 298 281 299 282 /* Advance destination position to next sub-block. */ 300 283 *dest_ofs += NTFS_SB_SIZE; 301 - if (!(*dest_ofs &= ~PAGE_MASK)) { 284 + *dest_ofs &= ~PAGE_MASK; 285 + if (!(*dest_ofs)) { 302 286 finalize_page: 303 287 /* 304 288 * First stage: add current page index to array of ··· 326 308 if (dp_addr < dp_sb_end) { 327 309 int nr_bytes = do_sb_end - *dest_ofs; 328 310 329 - ntfs_debug("Filling incomplete sub-block with " 330 - "zeroes."); 311 + ntfs_debug("Filling incomplete sub-block with zeroes."); 331 312 /* Zero remainder and update destination position. */ 332 313 memset(dp_addr, 0, nr_bytes); 333 314 *dest_ofs += nr_bytes; 334 315 } 335 316 /* We have finished the current sub-block. */ 336 - if (!(*dest_ofs &= ~PAGE_MASK)) 317 + *dest_ofs &= ~PAGE_MASK; 318 + if (!(*dest_ofs)) 337 319 goto finalize_page; 338 320 goto do_next_sb; 339 321 } ··· 347 329 348 330 /* Parse the eight tokens described by the tag. */ 349 331 for (token = 0; token < 8; token++, tag >>= 1) { 350 - u16 lg, pt, length, max_non_overlap; 351 332 register u16 i; 333 + u16 lg, pt, length, max_non_overlap; 352 334 u8 *dp_back_addr; 353 335 354 336 /* Check if we are done / still in range. */ ··· 387 369 lg++; 388 370 389 371 /* Get the phrase token into i. */ 390 - pt = le16_to_cpup((le16*)cb); 372 + pt = le16_to_cpup((__le16 *)cb); 391 373 392 374 /* 393 375 * Calculate starting position of the byte sequence in ··· 442 424 goto return_error; 443 425 } 444 426 445 - /** 427 + /* 446 428 * ntfs_read_compressed_block - read a compressed block into the page cache 447 - * @page: locked page in the compression block(s) we need to read 429 + * @folio: locked folio in the compression block(s) we need to read 448 430 * 449 431 * When we are called the page has already been verified to be locked and the 450 432 * attribute is known to be non-resident, not encrypted, but compressed. ··· 459 441 * Warning: We have to be careful what we do about existing pages. They might 460 442 * have been written to so that we would lose data if we were to just overwrite 461 443 * them with the out-of-date uncompressed data. 462 - * 463 - * FIXME: For PAGE_SIZE > cb_size we are not doing the Right Thing(TM) at 464 - * the end of the file I think. We need to detect this case and zero the out 465 - * of bounds remainder of the page in question and mark it as handled. At the 466 - * moment we would just return -EIO on such a page. This bug will only become 467 - * apparent if pages are above 8kiB and the NTFS volume only uses 512 byte 468 - * clusters so is probably not going to be seen by anyone. Still this should 469 - * be fixed. (AIA) 470 - * 471 - * FIXME: Again for PAGE_SIZE > cb_size we are screwing up both in 472 - * handling sparse and compressed cbs. (AIA) 473 - * 474 - * FIXME: At the moment we don't do any zeroing out in the case that 475 - * initialized_size is less than data_size. This should be safe because of the 476 - * nature of the compression algorithm used. Just in case we check and output 477 - * an error message in read inode if the two sizes are not equal for a 478 - * compressed file. (AIA) 479 444 */ 480 - int ntfs_read_compressed_block(struct page *page) 445 + int ntfs_read_compressed_block(struct folio *folio) 481 446 { 447 + struct page *page = &folio->page; 482 448 loff_t i_size; 483 449 s64 initialized_size; 484 450 struct address_space *mapping = page->mapping; 485 - ntfs_inode *ni = NTFS_I(mapping->host); 486 - ntfs_volume *vol = ni->vol; 451 + struct ntfs_inode *ni = NTFS_I(mapping->host); 452 + struct ntfs_volume *vol = ni->vol; 487 453 struct super_block *sb = vol->sb; 488 - runlist_element *rl; 489 - unsigned long flags, block_size = sb->s_blocksize; 490 - unsigned char block_size_bits = sb->s_blocksize_bits; 454 + struct runlist_element *rl; 455 + unsigned long flags; 491 456 u8 *cb, *cb_pos, *cb_end; 492 - struct buffer_head **bhs; 493 - unsigned long offset, index = page->index; 457 + unsigned long offset, index = page->__folio_index; 494 458 u32 cb_size = ni->itype.compressed.block_size; 495 459 u64 cb_size_mask = cb_size - 1UL; 496 - VCN vcn; 497 - LCN lcn; 460 + s64 vcn; 461 + s64 lcn; 498 462 /* The first wanted vcn (minimum alignment is PAGE_SIZE). */ 499 - VCN start_vcn = (((s64)index << PAGE_SHIFT) & ~cb_size_mask) >> 463 + s64 start_vcn = (((s64)index << PAGE_SHIFT) & ~cb_size_mask) >> 500 464 vol->cluster_size_bits; 501 465 /* 502 466 * The first vcn after the last wanted vcn (minimum alignment is again 503 467 * PAGE_SIZE. 504 468 */ 505 - VCN end_vcn = ((((s64)(index + 1UL) << PAGE_SHIFT) + cb_size - 1) 469 + s64 end_vcn = ((((s64)(index + 1UL) << PAGE_SHIFT) + cb_size - 1) 506 470 & ~cb_size_mask) >> vol->cluster_size_bits; 507 471 /* Number of compression blocks (cbs) in the wanted vcn range. */ 508 - unsigned int nr_cbs = (end_vcn - start_vcn) << vol->cluster_size_bits 509 - >> ni->itype.compressed.block_size_bits; 472 + unsigned int nr_cbs = ntfs_cluster_to_bytes(vol, end_vcn - start_vcn) >> 473 + ni->itype.compressed.block_size_bits; 510 474 /* 511 475 * Number of pages required to store the uncompressed data from all 512 476 * compression blocks (cbs) overlapping @page. Due to alignment 513 477 * guarantees of start_vcn and end_vcn, no need to round up here. 514 478 */ 515 - unsigned int nr_pages = (end_vcn - start_vcn) << 516 - vol->cluster_size_bits >> PAGE_SHIFT; 517 - unsigned int xpage, max_page, cur_page, cur_ofs, i; 479 + unsigned int nr_pages = ntfs_cluster_to_pidx(vol, end_vcn - start_vcn); 480 + unsigned int xpage, max_page, cur_page, cur_ofs, i, page_ofs, page_index; 518 481 unsigned int cb_clusters, cb_max_ofs; 519 - int block, max_block, cb_max_page, bhs_size, nr_bhs, err = 0; 482 + int cb_max_page, err = 0; 520 483 struct page **pages; 521 484 int *completed_pages; 522 485 unsigned char xpage_done = 0; 486 + struct page *lpage; 523 487 524 - ntfs_debug("Entering, page->index = 0x%lx, cb_size = 0x%x, nr_pages = " 525 - "%i.", index, cb_size, nr_pages); 488 + ntfs_debug("Entering, page->index = 0x%lx, cb_size = 0x%x, nr_pages = %i.", 489 + index, cb_size, nr_pages); 526 490 /* 527 491 * Bad things happen if we get here for anything that is not an 528 492 * unnamed $DATA attribute. 529 493 */ 530 - BUG_ON(ni->type != AT_DATA); 531 - BUG_ON(ni->name_len); 494 + if (ni->type != AT_DATA || ni->name_len) { 495 + unlock_page(page); 496 + return -EIO; 497 + } 532 498 533 499 pages = kmalloc_array(nr_pages, sizeof(struct page *), GFP_NOFS); 534 500 completed_pages = kmalloc_array(nr_pages + 1, sizeof(int), GFP_NOFS); 535 501 536 - /* Allocate memory to store the buffer heads we need. */ 537 - bhs_size = cb_size / block_size * sizeof(struct buffer_head *); 538 - bhs = kmalloc(bhs_size, GFP_NOFS); 539 - 540 - if (unlikely(!pages || !bhs || !completed_pages)) { 541 - kfree(bhs); 502 + if (unlikely(!pages || !completed_pages)) { 542 503 kfree(pages); 543 504 kfree(completed_pages); 544 505 unlock_page(page); ··· 529 532 * We have already been given one page, this is the one we must do. 530 533 * Once again, the alignment guarantees keep it simple. 531 534 */ 532 - offset = start_vcn << vol->cluster_size_bits >> PAGE_SHIFT; 535 + offset = ntfs_cluster_to_pidx(vol, start_vcn); 533 536 xpage = index - offset; 534 537 pages[xpage] = page; 535 538 /* ··· 544 547 offset; 545 548 /* Is the page fully outside i_size? (truncate in progress) */ 546 549 if (xpage >= max_page) { 547 - kfree(bhs); 548 550 kfree(pages); 549 551 kfree(completed_pages); 550 - zero_user(page, 0, PAGE_SIZE); 552 + zero_user_segments(page, 0, PAGE_SIZE, 0, 0); 551 553 ntfs_debug("Compressed read outside i_size - truncated?"); 552 554 SetPageUptodate(page); 553 555 unlock_page(page); ··· 554 558 } 555 559 if (nr_pages < max_page) 556 560 max_page = nr_pages; 561 + 557 562 for (i = 0; i < max_page; i++, offset++) { 558 563 if (i != xpage) 559 564 pages[i] = grab_cache_page_nowait(mapping, offset); ··· 565 568 * in and/or dirty or we would be losing data or at 566 569 * least wasting our time. 567 570 */ 568 - if (!PageDirty(page) && (!PageUptodate(page) || 569 - PageError(page))) { 570 - ClearPageError(page); 571 - kmap(page); 571 + if (!PageDirty(page) && (!PageUptodate(page))) { 572 + kmap_local_page(page); 572 573 continue; 573 574 } 574 575 unlock_page(page); ··· 584 589 cb_clusters = ni->itype.compressed.block_clusters; 585 590 do_next_cb: 586 591 nr_cbs--; 587 - nr_bhs = 0; 588 592 589 - /* Read all cb buffer heads one cluster at a time. */ 593 + mutex_lock(&ntfs_cb_lock); 594 + if (!ntfs_compression_buffer) 595 + if (allocate_compression_buffers()) { 596 + mutex_unlock(&ntfs_cb_lock); 597 + goto err_out; 598 + } 599 + 600 + 601 + cb = ntfs_compression_buffer; 602 + cb_pos = cb; 603 + cb_end = cb + cb_size; 604 + 590 605 rl = NULL; 591 606 for (vcn = start_vcn, start_vcn += cb_clusters; vcn < start_vcn; 592 607 vcn++) { ··· 624 619 */ 625 620 if (lcn == LCN_HOLE) 626 621 break; 627 - if (is_retry || lcn != LCN_RL_NOT_MAPPED) 622 + if (is_retry || lcn != LCN_RL_NOT_MAPPED) { 623 + mutex_unlock(&ntfs_cb_lock); 628 624 goto rl_err; 625 + } 629 626 is_retry = true; 630 627 /* 631 628 * Attempt to map runlist, dropping lock for the ··· 636 629 up_read(&ni->runlist.lock); 637 630 if (!ntfs_map_runlist(ni, vcn)) 638 631 goto lock_retry_remap; 632 + mutex_unlock(&ntfs_cb_lock); 639 633 goto map_rl_err; 640 634 } 641 - block = lcn << vol->cluster_size_bits >> block_size_bits; 642 - /* Read the lcn from device in chunks of block_size bytes. */ 643 - max_block = block + (vol->cluster_size >> block_size_bits); 644 - do { 645 - ntfs_debug("block = 0x%x.", block); 646 - if (unlikely(!(bhs[nr_bhs] = sb_getblk(sb, block)))) 647 - goto getblk_err; 648 - nr_bhs++; 649 - } while (++block < max_block); 635 + 636 + page_ofs = ntfs_cluster_to_poff(vol, lcn); 637 + page_index = ntfs_cluster_to_pidx(vol, lcn); 638 + 639 + lpage = read_mapping_page(sb->s_bdev->bd_mapping, 640 + page_index, NULL); 641 + if (IS_ERR(lpage)) { 642 + err = PTR_ERR(lpage); 643 + mutex_unlock(&ntfs_cb_lock); 644 + goto read_err; 645 + } 646 + 647 + lock_page(lpage); 648 + memcpy(cb_pos, page_address(lpage) + page_ofs, 649 + vol->cluster_size); 650 + unlock_page(lpage); 651 + put_page(lpage); 652 + cb_pos += vol->cluster_size; 650 653 } 651 654 652 655 /* Release the lock if we took it. */ 653 656 if (rl) 654 657 up_read(&ni->runlist.lock); 655 658 656 - /* Setup and initiate io on all buffer heads. */ 657 - for (i = 0; i < nr_bhs; i++) { 658 - struct buffer_head *tbh = bhs[i]; 659 - 660 - if (!trylock_buffer(tbh)) 661 - continue; 662 - if (unlikely(buffer_uptodate(tbh))) { 663 - unlock_buffer(tbh); 664 - continue; 665 - } 666 - get_bh(tbh); 667 - tbh->b_end_io = end_buffer_read_sync; 668 - submit_bh(REQ_OP_READ, tbh); 669 - } 670 - 671 - /* Wait for io completion on all buffer heads. */ 672 - for (i = 0; i < nr_bhs; i++) { 673 - struct buffer_head *tbh = bhs[i]; 674 - 675 - if (buffer_uptodate(tbh)) 676 - continue; 677 - wait_on_buffer(tbh); 678 - /* 679 - * We need an optimization barrier here, otherwise we start 680 - * hitting the below fixup code when accessing a loopback 681 - * mounted ntfs partition. This indicates either there is a 682 - * race condition in the loop driver or, more likely, gcc 683 - * overoptimises the code without the barrier and it doesn't 684 - * do the Right Thing(TM). 685 - */ 686 - barrier(); 687 - if (unlikely(!buffer_uptodate(tbh))) { 688 - ntfs_warning(vol->sb, "Buffer is unlocked but not " 689 - "uptodate! Unplugging the disk queue " 690 - "and rescheduling."); 691 - get_bh(tbh); 692 - io_schedule(); 693 - put_bh(tbh); 694 - if (unlikely(!buffer_uptodate(tbh))) 695 - goto read_err; 696 - ntfs_warning(vol->sb, "Buffer is now uptodate. Good."); 697 - } 698 - } 699 - 700 - /* 701 - * Get the compression buffer. We must not sleep any more 702 - * until we are finished with it. 703 - */ 704 - spin_lock(&ntfs_cb_lock); 705 - cb = ntfs_compression_buffer; 706 - 707 - BUG_ON(!cb); 708 - 709 - cb_pos = cb; 710 - cb_end = cb + cb_size; 711 - 712 - /* Copy the buffer heads into the contiguous buffer. */ 713 - for (i = 0; i < nr_bhs; i++) { 714 - memcpy(cb_pos, bhs[i]->b_data, block_size); 715 - cb_pos += block_size; 716 - } 717 - 718 659 /* Just a precaution. */ 719 660 if (cb_pos + 2 <= cb + cb_size) 720 - *(u16*)cb_pos = 0; 661 + *(u16 *)cb_pos = 0; 721 662 722 663 /* Reset cb_pos back to the beginning. */ 723 664 cb_pos = cb; ··· 686 731 /* Sparse cb, zero out page range overlapping the cb. */ 687 732 ntfs_debug("Found sparse compression block."); 688 733 /* We can sleep from now on, so we drop lock. */ 689 - spin_unlock(&ntfs_cb_lock); 734 + mutex_unlock(&ntfs_cb_lock); 690 735 if (cb_max_ofs) 691 736 cb_max_page--; 692 737 for (; cur_page < cb_max_page; cur_page++) { ··· 699 744 PAGE_SIZE - 700 745 cur_ofs); 701 746 flush_dcache_page(page); 702 - kunmap(page); 747 + kunmap_local(page_address(page)); 703 748 SetPageUptodate(page); 704 749 unlock_page(page); 705 750 if (cur_page == xpage) ··· 733 778 734 779 ntfs_debug("Found uncompressed compression block."); 735 780 /* Uncompressed cb, copy it to the destination pages. */ 736 - /* 737 - * TODO: As a big optimization, we could detect this case 738 - * before we read all the pages and use block_read_full_folio() 739 - * on all full pages instead (we still have to treat partial 740 - * pages especially but at least we are getting rid of the 741 - * synchronous io for the majority of pages. 742 - * Or if we choose not to do the read-ahead/-behind stuff, we 743 - * could just return block_read_full_folio(pages[xpage]) as long 744 - * as PAGE_SIZE <= cb_size. 745 - */ 746 781 if (cb_max_ofs) 747 782 cb_max_page--; 748 783 /* First stage: copy data into destination pages. */ ··· 756 811 cur_ofs = cb_max_ofs; 757 812 } 758 813 /* We can sleep from now on, so drop lock. */ 759 - spin_unlock(&ntfs_cb_lock); 814 + mutex_unlock(&ntfs_cb_lock); 760 815 /* Second stage: finalize pages. */ 761 816 for (; cur2_page < cb_max_page; cur2_page++) { 762 817 page = pages[cur2_page]; ··· 768 823 handle_bounds_compressed_page(page, i_size, 769 824 initialized_size); 770 825 flush_dcache_page(page); 771 - kunmap(page); 826 + kunmap_local(page_address(page)); 772 827 SetPageUptodate(page); 773 828 unlock_page(page); 774 829 if (cur2_page == xpage) ··· 796 851 * ntfs_decompress(). 797 852 */ 798 853 if (err) { 799 - ntfs_error(vol->sb, "ntfs_decompress() failed in inode " 800 - "0x%lx with error code %i. Skipping " 801 - "this compression block.", 802 - ni->mft_no, -err); 854 + ntfs_error(vol->sb, 855 + "ntfs_decompress() failed in inode 0x%lx with error code %i. Skipping this compression block.", 856 + ni->mft_no, -err); 803 857 /* Release the unfinished pages. */ 804 858 for (; prev_cur_page < cur_page; prev_cur_page++) { 805 859 page = pages[prev_cur_page]; 806 860 if (page) { 807 861 flush_dcache_page(page); 808 - kunmap(page); 862 + kunmap_local(page_address(page)); 809 863 unlock_page(page); 810 864 if (prev_cur_page != xpage) 811 865 put_page(page); ··· 814 870 } 815 871 } 816 872 817 - /* Release the buffer heads. */ 818 - for (i = 0; i < nr_bhs; i++) 819 - brelse(bhs[i]); 820 - 821 873 /* Do we have more work to do? */ 822 874 if (nr_cbs) 823 875 goto do_next_cb; 824 - 825 - /* We no longer need the list of buffer heads. */ 826 - kfree(bhs); 827 876 828 877 /* Clean up if we have any pages left. Should never happen. */ 829 878 for (cur_page = 0; cur_page < max_page; cur_page++) { 830 879 page = pages[cur_page]; 831 880 if (page) { 832 - ntfs_error(vol->sb, "Still have pages left! " 833 - "Terminating them with extreme " 834 - "prejudice. Inode 0x%lx, page index " 835 - "0x%lx.", ni->mft_no, page->index); 881 + ntfs_error(vol->sb, 882 + "Still have pages left! Terminating them with extreme prejudice. Inode 0x%lx, page index 0x%lx.", 883 + ni->mft_no, page->__folio_index); 836 884 flush_dcache_page(page); 837 - kunmap(page); 885 + kunmap_local(page_address(page)); 838 886 unlock_page(page); 839 887 if (cur_page != xpage) 840 888 put_page(page); ··· 846 910 "EOVERFLOW" : (!err ? "EIO" : "unknown error")); 847 911 return err < 0 ? err : -EIO; 848 912 849 - read_err: 850 - ntfs_error(vol->sb, "IO error while reading compressed data."); 851 - /* Release the buffer heads. */ 852 - for (i = 0; i < nr_bhs; i++) 853 - brelse(bhs[i]); 854 - goto err_out; 855 - 856 913 map_rl_err: 857 - ntfs_error(vol->sb, "ntfs_map_runlist() failed. Cannot read " 858 - "compression block."); 914 + ntfs_error(vol->sb, "ntfs_map_runlist() failed. Cannot read compression block."); 859 915 goto err_out; 860 916 861 917 rl_err: 862 918 up_read(&ni->runlist.lock); 863 - ntfs_error(vol->sb, "ntfs_rl_vcn_to_lcn() failed. Cannot read " 864 - "compression block."); 919 + ntfs_error(vol->sb, "ntfs_rl_vcn_to_lcn() failed. Cannot read compression block."); 865 920 goto err_out; 866 921 867 - getblk_err: 922 + read_err: 868 923 up_read(&ni->runlist.lock); 869 - ntfs_error(vol->sb, "getblk() failed. Cannot read compression block."); 924 + ntfs_error(vol->sb, "IO error while reading compressed data."); 870 925 871 926 err_out: 872 - kfree(bhs); 873 927 for (i = cur_page; i < max_page; i++) { 874 928 page = pages[i]; 875 929 if (page) { 876 930 flush_dcache_page(page); 877 - kunmap(page); 931 + kunmap_local(page_address(page)); 878 932 unlock_page(page); 879 933 if (i != xpage) 880 934 put_page(page); ··· 873 947 kfree(pages); 874 948 kfree(completed_pages); 875 949 return -EIO; 950 + } 951 + 952 + /* 953 + * Match length at or above which ntfs_best_match() will stop searching for 954 + * longer matches. 955 + */ 956 + #define NICE_MATCH_LEN 18 957 + 958 + /* 959 + * Maximum number of potential matches that ntfs_best_match() will consider at 960 + * each position. 961 + */ 962 + #define MAX_SEARCH_DEPTH 24 963 + 964 + /* log base 2 of the number of entries in the hash table for match-finding. */ 965 + #define HASH_SHIFT 14 966 + 967 + /* 968 + * Constant for the multiplicative hash function. These hashing constants 969 + * are used solely for the match-finding algorithm during compression. 970 + * They are NOT part of the on-disk format. The decompressor does not 971 + * utilize this hash. 972 + */ 973 + #define HASH_MULTIPLIER 0x1E35A7BD 974 + 975 + struct compress_context { 976 + const unsigned char *inbuf; 977 + int bufsize; 978 + int size; 979 + int rel; 980 + int mxsz; 981 + s16 head[1 << HASH_SHIFT]; 982 + s16 prev[NTFS_SB_SIZE]; 983 + }; 984 + 985 + /* 986 + * Hash the next 3-byte sequence in the input buffer 987 + */ 988 + static inline unsigned int ntfs_hash(const u8 *p) 989 + { 990 + u32 str; 991 + u32 hash; 992 + 993 + /* 994 + * Unaligned access allowed, and little endian CPU. 995 + * Callers ensure that at least 4 (not 3) bytes are remaining. 996 + */ 997 + str = *(const u32 *)p & 0xFFFFFF; 998 + hash = str * HASH_MULTIPLIER; 999 + 1000 + /* High bits are more random than the low bits. */ 1001 + return hash >> (32 - HASH_SHIFT); 1002 + } 1003 + 1004 + /* 1005 + * Search for the longest sequence matching current position 1006 + * 1007 + * A hash table, each entry of which points to a chain of sequence 1008 + * positions sharing the corresponding hash code, is maintained to speed up 1009 + * searching for matches. To maintain the hash table, either 1010 + * ntfs_best_match() or ntfs_skip_position() has to be called for each 1011 + * consecutive position. 1012 + * 1013 + * This function is heavily used; it has to be optimized carefully. 1014 + * 1015 + * This function sets pctx->size and pctx->rel to the length and offset, 1016 + * respectively, of the longest match found. 1017 + * 1018 + * The minimum match length is assumed to be 3, and the maximum match 1019 + * length is assumed to be pctx->mxsz. If this function produces 1020 + * pctx->size < 3, then no match was found. 1021 + * 1022 + * Note: for the following reasons, this function is not guaranteed to find 1023 + * *the* longest match up to pctx->mxsz: 1024 + * 1025 + * (1) If this function finds a match of NICE_MATCH_LEN bytes or greater, 1026 + * it ends early because a match this long is good enough and it's not 1027 + * worth spending more time searching. 1028 + * 1029 + * (2) If this function considers MAX_SEARCH_DEPTH matches with a single 1030 + * position, it ends early and returns the longest match found so far. 1031 + * This saves a lot of time on degenerate inputs. 1032 + */ 1033 + static void ntfs_best_match(struct compress_context *pctx, const int i, 1034 + int best_len) 1035 + { 1036 + const u8 * const inbuf = pctx->inbuf; 1037 + const u8 * const strptr = &inbuf[i]; /* String we're matching against */ 1038 + s16 * const prev = pctx->prev; 1039 + const int max_len = min(pctx->bufsize - i, pctx->mxsz); 1040 + const int nice_len = min(NICE_MATCH_LEN, max_len); 1041 + int depth_remaining = MAX_SEARCH_DEPTH; 1042 + const u8 *best_matchptr = strptr; 1043 + unsigned int hash; 1044 + s16 cur_match; 1045 + const u8 *matchptr; 1046 + int len; 1047 + 1048 + if (max_len < 4) 1049 + goto out; 1050 + 1051 + /* Insert the current sequence into the appropriate hash chain. */ 1052 + hash = ntfs_hash(strptr); 1053 + cur_match = pctx->head[hash]; 1054 + prev[i] = cur_match; 1055 + pctx->head[hash] = i; 1056 + 1057 + if (best_len >= max_len) { 1058 + /* 1059 + * Lazy match is being attempted, but there aren't enough length 1060 + * bits remaining to code a longer match. 1061 + */ 1062 + goto out; 1063 + } 1064 + 1065 + /* Search the appropriate hash chain for matches. */ 1066 + 1067 + for (; cur_match >= 0 && depth_remaining--; cur_match = prev[cur_match]) { 1068 + matchptr = &inbuf[cur_match]; 1069 + 1070 + /* 1071 + * Considering the potential match at 'matchptr': is it longer 1072 + * than 'best_len'? 1073 + * 1074 + * The bytes at index 'best_len' are the most likely to differ, 1075 + * so check them first. 1076 + * 1077 + * The bytes at indices 'best_len - 1' and '0' are less 1078 + * important to check separately. But doing so still gives a 1079 + * slight performance improvement, at least on x86_64, probably 1080 + * because they create separate branches for the CPU to predict 1081 + * independently of the branches in the main comparison loops. 1082 + */ 1083 + if (matchptr[best_len] != strptr[best_len] || 1084 + matchptr[best_len - 1] != strptr[best_len - 1] || 1085 + matchptr[0] != strptr[0]) 1086 + goto next_match; 1087 + 1088 + for (len = 1; len < best_len - 1; len++) 1089 + if (matchptr[len] != strptr[len]) 1090 + goto next_match; 1091 + 1092 + /* 1093 + * The match is the longest found so far --- 1094 + * at least 'best_len' + 1 bytes. Continue extending it. 1095 + */ 1096 + 1097 + best_matchptr = matchptr; 1098 + 1099 + do { 1100 + if (++best_len >= nice_len) { 1101 + /* 1102 + * 'nice_len' reached; don't waste time 1103 + * searching for longer matches. Extend the 1104 + * match as far as possible and terminate the 1105 + * search. 1106 + */ 1107 + while (best_len < max_len && 1108 + (best_matchptr[best_len] == 1109 + strptr[best_len])) 1110 + best_len++; 1111 + goto out; 1112 + } 1113 + } while (best_matchptr[best_len] == strptr[best_len]); 1114 + 1115 + /* Found a longer match, but 'nice_len' not yet reached. */ 1116 + 1117 + next_match: 1118 + /* Continue to next match in the chain. */ 1119 + ; 1120 + } 1121 + 1122 + /* 1123 + * Reached end of chain, or ended early due to reaching the maximum 1124 + * search depth. 1125 + */ 1126 + 1127 + out: 1128 + /* Return the longest match we were able to find. */ 1129 + pctx->size = best_len; 1130 + pctx->rel = best_matchptr - strptr; /* given as a negative number! */ 1131 + } 1132 + 1133 + /* 1134 + * Advance the match-finder, but don't search for matches. 1135 + */ 1136 + static void ntfs_skip_position(struct compress_context *pctx, const int i) 1137 + { 1138 + unsigned int hash; 1139 + 1140 + if (pctx->bufsize - i < 4) 1141 + return; 1142 + 1143 + /* Insert the current sequence into the appropriate hash chain. */ 1144 + hash = ntfs_hash(pctx->inbuf + i); 1145 + pctx->prev[i] = pctx->head[hash]; 1146 + pctx->head[hash] = i; 1147 + } 1148 + 1149 + /* 1150 + * Compress a 4096-byte block 1151 + * 1152 + * Returns a header of two bytes followed by the compressed data. 1153 + * If compression is not effective, the header and an uncompressed 1154 + * block is returned. 1155 + * 1156 + * Note : two bytes may be output before output buffer overflow 1157 + * is detected, so a 4100-bytes output buffer must be reserved. 1158 + * 1159 + * Returns the size of the compressed block, including the 1160 + * header (minimal size is 2, maximum size is 4098) 1161 + * 0 if an error has been met. 1162 + */ 1163 + static unsigned int ntfs_compress_block(const char *inbuf, const int bufsize, 1164 + char *outbuf) 1165 + { 1166 + struct compress_context *pctx; 1167 + int i; /* current position */ 1168 + int j; /* end of best match from current position */ 1169 + int k; /* end of best match from next position */ 1170 + int offs; /* offset to best match */ 1171 + int bp; /* bits to store offset */ 1172 + int bp_cur; /* saved bits to store offset at current position */ 1173 + int mxoff; /* max match offset : 1 << bp */ 1174 + unsigned int xout; 1175 + unsigned int q; /* aggregated offset and size */ 1176 + int have_match; /* do we have a match at the current position? */ 1177 + char *ptag; /* location reserved for a tag */ 1178 + int tag; /* current value of tag */ 1179 + int ntag; /* count of bits still undefined in tag */ 1180 + 1181 + pctx = kvzalloc(sizeof(struct compress_context), GFP_NOFS); 1182 + if (!pctx) 1183 + return -ENOMEM; 1184 + 1185 + /* 1186 + * All hash chains start as empty. The special value '-1' indicates the 1187 + * end of each hash chain. 1188 + */ 1189 + memset(pctx->head, 0xFF, sizeof(pctx->head)); 1190 + 1191 + pctx->inbuf = (const unsigned char *)inbuf; 1192 + pctx->bufsize = bufsize; 1193 + xout = 2; 1194 + i = 0; 1195 + bp = 4; 1196 + mxoff = 1 << bp; 1197 + pctx->mxsz = (1 << (16 - bp)) + 2; 1198 + have_match = 0; 1199 + tag = 0; 1200 + ntag = 8; 1201 + ptag = &outbuf[xout++]; 1202 + 1203 + while ((i < bufsize) && (xout < (NTFS_SB_SIZE + 2))) { 1204 + 1205 + /* 1206 + * This implementation uses "lazy" parsing: it always chooses 1207 + * the longest match, unless the match at the next position is 1208 + * longer. This is the same strategy used by the high 1209 + * compression modes of zlib. 1210 + */ 1211 + if (!have_match) { 1212 + /* 1213 + * Find the longest match at the current position. But 1214 + * first adjust the maximum match length if needed. 1215 + * (This loop might need to run more than one time in 1216 + * the case that we just output a long match.) 1217 + */ 1218 + while (mxoff < i) { 1219 + bp++; 1220 + mxoff <<= 1; 1221 + pctx->mxsz = (pctx->mxsz + 2) >> 1; 1222 + } 1223 + ntfs_best_match(pctx, i, 2); 1224 + } 1225 + 1226 + if (pctx->size >= 3) { 1227 + /* Found a match at the current position. */ 1228 + j = i + pctx->size; 1229 + bp_cur = bp; 1230 + offs = pctx->rel; 1231 + 1232 + if (pctx->size >= NICE_MATCH_LEN) { 1233 + /* Choose long matches immediately. */ 1234 + q = (~offs << (16 - bp_cur)) + (j - i - 3); 1235 + outbuf[xout++] = q & 255; 1236 + outbuf[xout++] = (q >> 8) & 255; 1237 + tag |= (1 << (8 - ntag)); 1238 + 1239 + if (j == bufsize) { 1240 + /* 1241 + * Shortcut if the match extends to the 1242 + * end of the buffer. 1243 + */ 1244 + i = j; 1245 + --ntag; 1246 + break; 1247 + } 1248 + i += 1; 1249 + do { 1250 + ntfs_skip_position(pctx, i); 1251 + } while (++i != j); 1252 + have_match = 0; 1253 + } else { 1254 + /* 1255 + * Check for a longer match at the next 1256 + * position. 1257 + */ 1258 + 1259 + /* 1260 + * Doesn't need to be while() since we just 1261 + * adjusted the maximum match length at the 1262 + * previous position. 1263 + */ 1264 + if (mxoff < i + 1) { 1265 + bp++; 1266 + mxoff <<= 1; 1267 + pctx->mxsz = (pctx->mxsz + 2) >> 1; 1268 + } 1269 + ntfs_best_match(pctx, i + 1, pctx->size); 1270 + k = i + 1 + pctx->size; 1271 + 1272 + if (k > (j + 1)) { 1273 + /* 1274 + * Next match is longer. 1275 + * Output a literal. 1276 + */ 1277 + outbuf[xout++] = inbuf[i++]; 1278 + have_match = 1; 1279 + } else { 1280 + /* 1281 + * Next match isn't longer. 1282 + * Output the current match. 1283 + */ 1284 + q = (~offs << (16 - bp_cur)) + 1285 + (j - i - 3); 1286 + outbuf[xout++] = q & 255; 1287 + outbuf[xout++] = (q >> 8) & 255; 1288 + tag |= (1 << (8 - ntag)); 1289 + 1290 + /* 1291 + * The minimum match length is 3, and 1292 + * we've run two bytes through the 1293 + * matchfinder already. So the minimum 1294 + * number of positions we need to skip 1295 + * is 1. 1296 + */ 1297 + i += 2; 1298 + do { 1299 + ntfs_skip_position(pctx, i); 1300 + } while (++i != j); 1301 + have_match = 0; 1302 + } 1303 + } 1304 + } else { 1305 + /* No match at current position. Output a literal. */ 1306 + outbuf[xout++] = inbuf[i++]; 1307 + have_match = 0; 1308 + } 1309 + 1310 + /* Store the tag if fully used. */ 1311 + if (!--ntag) { 1312 + *ptag = tag; 1313 + ntag = 8; 1314 + ptag = &outbuf[xout++]; 1315 + tag = 0; 1316 + } 1317 + } 1318 + 1319 + /* Store the last tag if partially used. */ 1320 + if (ntag == 8) 1321 + xout--; 1322 + else 1323 + *ptag = tag; 1324 + 1325 + /* Determine whether to store the data compressed or uncompressed. */ 1326 + if ((i >= bufsize) && (xout < (NTFS_SB_SIZE + 2))) { 1327 + /* Compressed. */ 1328 + outbuf[0] = (xout - 3) & 255; 1329 + outbuf[1] = 0xb0 + (((xout - 3) >> 8) & 15); 1330 + } else { 1331 + /* Uncompressed. */ 1332 + memcpy(&outbuf[2], inbuf, bufsize); 1333 + if (bufsize < NTFS_SB_SIZE) 1334 + memset(&outbuf[bufsize + 2], 0, NTFS_SB_SIZE - bufsize); 1335 + outbuf[0] = 0xff; 1336 + outbuf[1] = 0x3f; 1337 + xout = NTFS_SB_SIZE + 2; 1338 + } 1339 + 1340 + /* 1341 + * Free the compression context and return the total number of bytes 1342 + * written to 'outbuf'. 1343 + */ 1344 + kvfree(pctx); 1345 + return xout; 1346 + } 1347 + 1348 + static int ntfs_write_cb(struct ntfs_inode *ni, loff_t pos, struct page **pages, 1349 + int pages_per_cb) 1350 + { 1351 + struct ntfs_volume *vol = ni->vol; 1352 + char *outbuf = NULL, *pbuf, *inbuf; 1353 + u32 compsz, p, insz = pages_per_cb << PAGE_SHIFT; 1354 + s32 rounded, bio_size; 1355 + unsigned int sz, bsz; 1356 + bool fail = false, allzeroes; 1357 + /* a single compressed zero */ 1358 + static char onezero[] = {0x01, 0xb0, 0x00, 0x00}; 1359 + /* a couple of compressed zeroes */ 1360 + static char twozeroes[] = {0x02, 0xb0, 0x00, 0x00, 0x00}; 1361 + /* more compressed zeroes, to be followed by some count */ 1362 + static char morezeroes[] = {0x03, 0xb0, 0x02, 0x00}; 1363 + struct page **pages_disk = NULL, *pg; 1364 + s64 bio_lcn; 1365 + struct runlist_element *rlc, *rl; 1366 + int i, err; 1367 + int pages_count = (round_up(ni->itype.compressed.block_size + 2 * 1368 + (ni->itype.compressed.block_size / NTFS_SB_SIZE) + 2, PAGE_SIZE)) / PAGE_SIZE; 1369 + size_t new_rl_count; 1370 + struct bio *bio = NULL; 1371 + loff_t new_length; 1372 + s64 new_vcn; 1373 + 1374 + inbuf = vmap(pages, pages_per_cb, VM_MAP, PAGE_KERNEL_RO); 1375 + if (!inbuf) 1376 + return -ENOMEM; 1377 + 1378 + /* may need 2 extra bytes per block and 2 more bytes */ 1379 + pages_disk = kcalloc(pages_count, sizeof(struct page *), GFP_NOFS); 1380 + if (!pages_disk) { 1381 + vunmap(inbuf); 1382 + return -ENOMEM; 1383 + } 1384 + 1385 + for (i = 0; i < pages_count; i++) { 1386 + pg = alloc_page(GFP_KERNEL); 1387 + if (!pg) { 1388 + err = -ENOMEM; 1389 + goto out; 1390 + } 1391 + pages_disk[i] = pg; 1392 + lock_page(pg); 1393 + kmap_local_page(pg); 1394 + } 1395 + 1396 + outbuf = vmap(pages_disk, pages_count, VM_MAP, PAGE_KERNEL); 1397 + if (!outbuf) { 1398 + err = -ENOMEM; 1399 + goto out; 1400 + } 1401 + 1402 + compsz = 0; 1403 + allzeroes = true; 1404 + for (p = 0; (p < insz) && !fail; p += NTFS_SB_SIZE) { 1405 + if ((p + NTFS_SB_SIZE) < insz) 1406 + bsz = NTFS_SB_SIZE; 1407 + else 1408 + bsz = insz - p; 1409 + pbuf = &outbuf[compsz]; 1410 + sz = ntfs_compress_block(&inbuf[p], bsz, pbuf); 1411 + /* fail if all the clusters (or more) are needed */ 1412 + if (!sz || ((compsz + sz + vol->cluster_size + 2) > 1413 + ni->itype.compressed.block_size)) 1414 + fail = true; 1415 + else { 1416 + if (allzeroes) { 1417 + /* check whether this is all zeroes */ 1418 + switch (sz) { 1419 + case 4: 1420 + allzeroes = !memcmp(pbuf, onezero, 4); 1421 + break; 1422 + case 5: 1423 + allzeroes = !memcmp(pbuf, twozeroes, 5); 1424 + break; 1425 + case 6: 1426 + allzeroes = !memcmp(pbuf, morezeroes, 4); 1427 + break; 1428 + default: 1429 + allzeroes = false; 1430 + break; 1431 + } 1432 + } 1433 + compsz += sz; 1434 + } 1435 + } 1436 + 1437 + if (!fail && !allzeroes) { 1438 + outbuf[compsz++] = 0; 1439 + outbuf[compsz++] = 0; 1440 + rounded = ((compsz - 1) | (vol->cluster_size - 1)) + 1; 1441 + memset(&outbuf[compsz], 0, rounded - compsz); 1442 + bio_size = rounded; 1443 + pages = pages_disk; 1444 + } else if (allzeroes) { 1445 + err = 0; 1446 + goto out; 1447 + } else { 1448 + bio_size = insz; 1449 + } 1450 + 1451 + new_vcn = ntfs_bytes_to_cluster(vol, pos & ~(ni->itype.compressed.block_size - 1)); 1452 + new_length = ntfs_bytes_to_cluster(vol, round_up(bio_size, vol->cluster_size)); 1453 + 1454 + err = ntfs_non_resident_attr_punch_hole(ni, new_vcn, ni->itype.compressed.block_clusters); 1455 + if (err < 0) 1456 + goto out; 1457 + 1458 + rlc = ntfs_cluster_alloc(vol, new_vcn, new_length, -1, DATA_ZONE, 1459 + false, true, true); 1460 + if (IS_ERR(rlc)) { 1461 + err = PTR_ERR(rlc); 1462 + goto out; 1463 + } 1464 + 1465 + bio_lcn = rlc->lcn; 1466 + down_write(&ni->runlist.lock); 1467 + rl = ntfs_runlists_merge(&ni->runlist, rlc, 0, &new_rl_count); 1468 + if (IS_ERR(rl)) { 1469 + up_write(&ni->runlist.lock); 1470 + ntfs_error(vol->sb, "Failed to merge runlists"); 1471 + err = PTR_ERR(rl); 1472 + if (ntfs_cluster_free_from_rl(vol, rlc)) 1473 + ntfs_error(vol->sb, "Failed to free hot clusters."); 1474 + kvfree(rlc); 1475 + goto out; 1476 + } 1477 + 1478 + ni->runlist.count = new_rl_count; 1479 + ni->runlist.rl = rl; 1480 + 1481 + err = ntfs_attr_update_mapping_pairs(ni, 0); 1482 + up_write(&ni->runlist.lock); 1483 + if (err) { 1484 + err = -EIO; 1485 + goto out; 1486 + } 1487 + 1488 + i = 0; 1489 + while (bio_size > 0) { 1490 + int page_size; 1491 + 1492 + if (bio_size >= PAGE_SIZE) { 1493 + page_size = PAGE_SIZE; 1494 + bio_size -= PAGE_SIZE; 1495 + } else { 1496 + page_size = bio_size; 1497 + bio_size = 0; 1498 + } 1499 + 1500 + setup_bio: 1501 + if (!bio) { 1502 + bio = bio_alloc(vol->sb->s_bdev, 1, REQ_OP_WRITE, 1503 + GFP_NOIO); 1504 + bio->bi_iter.bi_sector = 1505 + ntfs_bytes_to_sector(vol, 1506 + ntfs_cluster_to_bytes(vol, bio_lcn + i)); 1507 + } 1508 + 1509 + if (!bio_add_page(bio, pages[i], page_size, 0)) { 1510 + err = submit_bio_wait(bio); 1511 + bio_put(bio); 1512 + if (err) 1513 + goto out; 1514 + bio = NULL; 1515 + goto setup_bio; 1516 + } 1517 + i++; 1518 + } 1519 + 1520 + err = submit_bio_wait(bio); 1521 + bio_put(bio); 1522 + out: 1523 + vunmap(outbuf); 1524 + for (i = 0; i < pages_count; i++) { 1525 + pg = pages_disk[i]; 1526 + if (pg) { 1527 + kunmap_local(page_address(pg)); 1528 + unlock_page(pg); 1529 + put_page(pg); 1530 + } 1531 + } 1532 + kfree(pages_disk); 1533 + vunmap(inbuf); 1534 + NInoSetFileNameDirty(ni); 1535 + mark_mft_record_dirty(ni); 1536 + 1537 + return err; 1538 + } 1539 + 1540 + int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count, 1541 + struct iov_iter *from) 1542 + { 1543 + struct folio *folio; 1544 + struct page **pages = NULL, *page; 1545 + int pages_per_cb = ni->itype.compressed.block_size >> PAGE_SHIFT; 1546 + int cb_size = ni->itype.compressed.block_size, cb_off, err = 0; 1547 + int i, ip; 1548 + size_t written = 0; 1549 + struct address_space *mapping = VFS_I(ni)->i_mapping; 1550 + 1551 + if (NInoCompressed(ni) && pos + count > ni->allocated_size) { 1552 + int err; 1553 + loff_t end = pos + count; 1554 + 1555 + err = ntfs_attr_expand(ni, end, 1556 + round_up(end, ni->itype.compressed.block_size)); 1557 + if (err) 1558 + return err; 1559 + } 1560 + 1561 + pages = kmalloc_array(pages_per_cb, sizeof(struct page *), GFP_NOFS); 1562 + if (!pages) 1563 + return -ENOMEM; 1564 + 1565 + while (count) { 1566 + pgoff_t index; 1567 + size_t copied, bytes; 1568 + int off; 1569 + 1570 + off = pos & (cb_size - 1); 1571 + bytes = cb_size - off; 1572 + if (bytes > count) 1573 + bytes = count; 1574 + 1575 + cb_off = pos & ~(cb_size - 1); 1576 + index = cb_off >> PAGE_SHIFT; 1577 + 1578 + if (unlikely(fault_in_iov_iter_readable(from, bytes))) { 1579 + err = -EFAULT; 1580 + goto out; 1581 + } 1582 + 1583 + for (i = 0; i < pages_per_cb; i++) { 1584 + folio = read_mapping_folio(mapping, index + i, NULL); 1585 + if (IS_ERR(folio)) { 1586 + for (ip = 0; ip < i; ip++) { 1587 + folio_unlock(page_folio(pages[ip])); 1588 + folio_put(page_folio(pages[ip])); 1589 + } 1590 + err = PTR_ERR(folio); 1591 + goto out; 1592 + } 1593 + 1594 + folio_lock(folio); 1595 + pages[i] = folio_page(folio, 0); 1596 + } 1597 + 1598 + WARN_ON(!bytes); 1599 + copied = 0; 1600 + ip = off >> PAGE_SHIFT; 1601 + off = offset_in_page(pos); 1602 + 1603 + for (;;) { 1604 + size_t cp, tail = PAGE_SIZE - off; 1605 + 1606 + page = pages[ip]; 1607 + cp = copy_folio_from_iter_atomic(page_folio(page), off, 1608 + min(tail, bytes), from); 1609 + flush_dcache_page(page); 1610 + 1611 + copied += cp; 1612 + bytes -= cp; 1613 + if (!bytes || !cp) 1614 + break; 1615 + 1616 + if (cp < tail) { 1617 + off += cp; 1618 + } else { 1619 + ip++; 1620 + off = 0; 1621 + } 1622 + } 1623 + 1624 + err = ntfs_write_cb(ni, pos, pages, pages_per_cb); 1625 + 1626 + for (i = 0; i < pages_per_cb; i++) { 1627 + folio = page_folio(pages[i]); 1628 + if (i < ip) { 1629 + folio_clear_dirty(folio); 1630 + folio_mark_uptodate(folio); 1631 + } 1632 + folio_unlock(folio); 1633 + folio_put(folio); 1634 + } 1635 + 1636 + if (err) 1637 + goto out; 1638 + 1639 + cond_resched(); 1640 + pos += copied; 1641 + written += copied; 1642 + count = iov_iter_count(from); 1643 + } 1644 + 1645 + out: 1646 + kfree(pages); 1647 + if (err < 0) 1648 + written = err; 1649 + 1650 + return written; 876 1651 }