File Index
Files and directories
Sign in
FILE

frecord.c

File preview with syntax highlighting.

linux-6.19.14/fs/ntfs3/frecord.c C
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 *
   4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
   5 *
   6 */
   7
   8#include <linux/fiemap.h>
   9#include <linux/fs.h>
  10#include <linux/minmax.h>
  11#include <linux/vmalloc.h>
  12
  13#include "debug.h"
  14#include "ntfs.h"
  15#include "ntfs_fs.h"
  16#ifdef CONFIG_NTFS3_LZX_XPRESS
  17#include "lib/lib.h"
  18#endif
  19
  20static struct mft_inode *ni_ins_mi(struct ntfs_inode *ni, struct rb_root *tree,
  21				   CLST ino, struct rb_node *ins)
  22{
  23	struct rb_node **p = &tree->rb_node;
  24	struct rb_node *pr = NULL;
  25
  26	while (*p) {
  27		struct mft_inode *mi;
  28
  29		pr = *p;
  30		mi = rb_entry(pr, struct mft_inode, node);
  31		if (mi->rno > ino)
  32			p = &pr->rb_left;
  33		else if (mi->rno < ino)
  34			p = &pr->rb_right;
  35		else
  36			return mi;
  37	}
  38
  39	if (!ins)
  40		return NULL;
  41
  42	rb_link_node(ins, pr, p);
  43	rb_insert_color(ins, tree);
  44	return rb_entry(ins, struct mft_inode, node);
  45}
  46
  47/*
  48 * ni_find_mi - Find mft_inode by record number.
  49 */
  50static struct mft_inode *ni_find_mi(struct ntfs_inode *ni, CLST rno)
  51{
  52	return ni_ins_mi(ni, &ni->mi_tree, rno, NULL);
  53}
  54
  55/*
  56 * ni_add_mi - Add new mft_inode into ntfs_inode.
  57 */
  58static void ni_add_mi(struct ntfs_inode *ni, struct mft_inode *mi)
  59{
  60	ni_ins_mi(ni, &ni->mi_tree, mi->rno, &mi->node);
  61}
  62
  63/*
  64 * ni_remove_mi - Remove mft_inode from ntfs_inode.
  65 */
  66void ni_remove_mi(struct ntfs_inode *ni, struct mft_inode *mi)
  67{
  68	rb_erase(&mi->node, &ni->mi_tree);
  69}
  70
  71/*
  72 * ni_std - Return: Pointer into std_info from primary record.
  73 */
  74struct ATTR_STD_INFO *ni_std(struct ntfs_inode *ni)
  75{
  76	const struct ATTRIB *attr;
  77
  78	attr = mi_find_attr(ni, &ni->mi, NULL, ATTR_STD, NULL, 0, NULL);
  79	return attr ? resident_data_ex(attr, sizeof(struct ATTR_STD_INFO)) :
  80		      NULL;
  81}
  82
  83/*
  84 * ni_std5
  85 *
  86 * Return: Pointer into std_info from primary record.
  87 */
  88struct ATTR_STD_INFO5 *ni_std5(struct ntfs_inode *ni)
  89{
  90	const struct ATTRIB *attr;
  91
  92	attr = mi_find_attr(ni, &ni->mi, NULL, ATTR_STD, NULL, 0, NULL);
  93
  94	return attr ? resident_data_ex(attr, sizeof(struct ATTR_STD_INFO5)) :
  95		      NULL;
  96}
  97
  98/*
  99 * ni_clear - Clear resources allocated by ntfs_inode.
 100 */
 101void ni_clear(struct ntfs_inode *ni)
 102{
 103	struct rb_node *node;
 104
 105	if (!ni->vfs_inode.i_nlink && ni->mi.mrec &&
 106	    is_rec_inuse(ni->mi.mrec) &&
 107	    !(ni->mi.sbi->flags & NTFS_FLAGS_LOG_REPLAYING))
 108		ni_delete_all(ni);
 109
 110	al_destroy(ni);
 111
 112	for (node = rb_first(&ni->mi_tree); node;) {
 113		struct rb_node *next = rb_next(node);
 114		struct mft_inode *mi = rb_entry(node, struct mft_inode, node);
 115
 116		rb_erase(node, &ni->mi_tree);
 117		mi_put(mi);
 118		node = next;
 119	}
 120
 121	/* Bad inode always has mode == S_IFREG. */
 122	if (ni->ni_flags & NI_FLAG_DIR)
 123		indx_clear(&ni->dir);
 124	else {
 125		run_close(&ni->file.run);
 126#ifdef CONFIG_NTFS3_LZX_XPRESS
 127		if (ni->file.offs_folio) {
 128			/* On-demand allocated page for offsets. */
 129			folio_put(ni->file.offs_folio);
 130			ni->file.offs_folio = NULL;
 131		}
 132#endif
 133	}
 134
 135	mi_clear(&ni->mi);
 136}
 137
 138/*
 139 * ni_load_mi_ex - Find mft_inode by record number.
 140 */
 141int ni_load_mi_ex(struct ntfs_inode *ni, CLST rno, struct mft_inode **mi)
 142{
 143	int err;
 144	struct mft_inode *r;
 145
 146	r = ni_find_mi(ni, rno);
 147	if (r)
 148		goto out;
 149
 150	err = mi_get(ni->mi.sbi, rno, &r);
 151	if (err) {
 152		_ntfs_bad_inode(&ni->vfs_inode);
 153		return err;
 154	}
 155
 156	ni_add_mi(ni, r);
 157
 158out:
 159	if (mi)
 160		*mi = r;
 161	return 0;
 162}
 163
 164/*
 165 * ni_load_mi - Load mft_inode corresponded list_entry.
 166 */
 167int ni_load_mi(struct ntfs_inode *ni, const struct ATTR_LIST_ENTRY *le,
 168	       struct mft_inode **mi)
 169{
 170	CLST rno;
 171
 172	if (!le) {
 173		*mi = &ni->mi;
 174		return 0;
 175	}
 176
 177	rno = ino_get(&le->ref);
 178	if (rno == ni->mi.rno) {
 179		*mi = &ni->mi;
 180		return 0;
 181	}
 182	return ni_load_mi_ex(ni, rno, mi);
 183}
 184
 185/*
 186 * ni_find_attr
 187 *
 188 * Return: Attribute and record this attribute belongs to.
 189 */
 190struct ATTRIB *ni_find_attr(struct ntfs_inode *ni, struct ATTRIB *attr,
 191			    struct ATTR_LIST_ENTRY **le_o, enum ATTR_TYPE type,
 192			    const __le16 *name, u8 name_len, const CLST *vcn,
 193			    struct mft_inode **mi)
 194{
 195	struct ATTR_LIST_ENTRY *le;
 196	struct mft_inode *m;
 197
 198	if (!ni->attr_list.size ||
 199	    (!name_len && (type == ATTR_LIST || type == ATTR_STD))) {
 200		if (le_o)
 201			*le_o = NULL;
 202		if (mi)
 203			*mi = &ni->mi;
 204
 205		/* Look for required attribute in primary record. */
 206		return mi_find_attr(ni, &ni->mi, attr, type, name, name_len,
 207				    NULL);
 208	}
 209
 210	/* First look for list entry of required type. */
 211	le = al_find_ex(ni, le_o ? *le_o : NULL, type, name, name_len, vcn);
 212	if (!le)
 213		return NULL;
 214
 215	if (le_o)
 216		*le_o = le;
 217
 218	/* Load record that contains this attribute. */
 219	if (ni_load_mi(ni, le, &m))
 220		return NULL;
 221
 222	/* Look for required attribute. */
 223	attr = mi_find_attr(ni, m, NULL, type, name, name_len, &le->id);
 224
 225	if (!attr)
 226		goto out;
 227
 228	if (!attr->non_res) {
 229		if (vcn && *vcn)
 230			goto out;
 231	} else if (!vcn) {
 232		if (attr->nres.svcn)
 233			goto out;
 234	} else if (le64_to_cpu(attr->nres.svcn) > *vcn ||
 235		   *vcn > le64_to_cpu(attr->nres.evcn)) {
 236		goto out;
 237	}
 238
 239	if (mi)
 240		*mi = m;
 241	return attr;
 242
 243out:
 244	_ntfs_bad_inode(&ni->vfs_inode);
 245	return NULL;
 246}
 247
 248/*
 249 * ni_enum_attr_ex - Enumerates attributes in ntfs_inode.
 250 */
 251struct ATTRIB *ni_enum_attr_ex(struct ntfs_inode *ni, struct ATTRIB *attr,
 252			       struct ATTR_LIST_ENTRY **le,
 253			       struct mft_inode **mi)
 254{
 255	struct mft_inode *mi2;
 256	struct ATTR_LIST_ENTRY *le2;
 257
 258	/* Do we have an attribute list? */
 259	if (!ni->attr_list.size) {
 260		*le = NULL;
 261		if (mi)
 262			*mi = &ni->mi;
 263		/* Enum attributes in primary record. */
 264		return mi_enum_attr(ni, &ni->mi, attr);
 265	}
 266
 267	/* Get next list entry. */
 268	le2 = *le = al_enumerate(ni, attr ? *le : NULL);
 269	if (!le2)
 270		return NULL;
 271
 272	/* Load record that contains the required attribute. */
 273	if (ni_load_mi(ni, le2, &mi2))
 274		return NULL;
 275
 276	if (mi)
 277		*mi = mi2;
 278
 279	/* Find attribute in loaded record. */
 280	return rec_find_attr_le(ni, mi2, le2);
 281}
 282
 283/*
 284 * ni_load_all_mi - Load all subrecords.
 285 */
 286int ni_load_all_mi(struct ntfs_inode *ni)
 287{
 288	int err;
 289	struct ATTR_LIST_ENTRY *le;
 290
 291	if (!ni->attr_list.size)
 292		return 0;
 293
 294	le = NULL;
 295
 296	while ((le = al_enumerate(ni, le))) {
 297		CLST rno = ino_get(&le->ref);
 298
 299		if (rno == ni->mi.rno)
 300			continue;
 301
 302		err = ni_load_mi_ex(ni, rno, NULL);
 303		if (err)
 304			return err;
 305	}
 306
 307	return 0;
 308}
 309
 310/*
 311 * ni_add_subrecord - Allocate + format + attach a new subrecord.
 312 */
 313bool ni_add_subrecord(struct ntfs_inode *ni, CLST rno, struct mft_inode **mi)
 314{
 315	struct mft_inode *m;
 316
 317	m = kzalloc(sizeof(struct mft_inode), GFP_NOFS);
 318	if (!m)
 319		return false;
 320
 321	if (mi_format_new(m, ni->mi.sbi, rno, 0, ni->mi.rno == MFT_REC_MFT)) {
 322		mi_put(m);
 323		return false;
 324	}
 325
 326	mi_get_ref(&ni->mi, &m->mrec->parent_ref);
 327
 328	*mi = ni_ins_mi(ni, &ni->mi_tree, m->rno, &m->node);
 329	if (*mi != m)
 330		mi_put(m);
 331
 332	return true;
 333}
 334
 335/*
 336 * ni_remove_attr - Remove all attributes for the given type/name/id.
 337 */
 338int ni_remove_attr(struct ntfs_inode *ni, enum ATTR_TYPE type,
 339		   const __le16 *name, u8 name_len, bool base_only,
 340		   const __le16 *id)
 341{
 342	int err;
 343	struct ATTRIB *attr;
 344	struct ATTR_LIST_ENTRY *le;
 345	struct mft_inode *mi;
 346	u32 type_in;
 347	int diff;
 348
 349	if (base_only || type == ATTR_LIST || !ni->attr_list.size) {
 350		attr = mi_find_attr(ni, &ni->mi, NULL, type, name, name_len,
 351				    id);
 352		if (!attr)
 353			return -ENOENT;
 354
 355		mi_remove_attr(ni, &ni->mi, attr);
 356		return 0;
 357	}
 358
 359	type_in = le32_to_cpu(type);
 360	le = NULL;
 361
 362	for (;;) {
 363		le = al_enumerate(ni, le);
 364		if (!le)
 365			return 0;
 366
 367next_le2:
 368		diff = le32_to_cpu(le->type) - type_in;
 369		if (diff < 0)
 370			continue;
 371
 372		if (diff > 0)
 373			return 0;
 374
 375		if (le->name_len != name_len)
 376			continue;
 377
 378		if (name_len &&
 379		    memcmp(le_name(le), name, name_len * sizeof(short)))
 380			continue;
 381
 382		if (id && le->id != *id)
 383			continue;
 384		err = ni_load_mi(ni, le, &mi);
 385		if (err)
 386			return err;
 387
 388		al_remove_le(ni, le);
 389
 390		attr = mi_find_attr(ni, mi, NULL, type, name, name_len, id);
 391		if (!attr)
 392			return -ENOENT;
 393
 394		mi_remove_attr(ni, mi, attr);
 395
 396		if (PtrOffset(ni->attr_list.le, le) >= ni->attr_list.size)
 397			return 0;
 398		goto next_le2;
 399	}
 400}
 401
 402/*
 403 * ni_ins_new_attr - Insert the attribute into record.
 404 *
 405 * Return: Not full constructed attribute or NULL if not possible to create.
 406 */
 407static struct ATTRIB *
 408ni_ins_new_attr(struct ntfs_inode *ni, struct mft_inode *mi,
 409		struct ATTR_LIST_ENTRY *le, enum ATTR_TYPE type,
 410		const __le16 *name, u8 name_len, u32 asize, u16 name_off,
 411		CLST svcn, struct ATTR_LIST_ENTRY **ins_le)
 412{
 413	int err;
 414	struct ATTRIB *attr;
 415	bool le_added = false;
 416	struct MFT_REF ref;
 417
 418	mi_get_ref(mi, &ref);
 419
 420	if (type != ATTR_LIST && !le && ni->attr_list.size) {
 421		err = al_add_le(ni, type, name, name_len, svcn, cpu_to_le16(-1),
 422				&ref, &le);
 423		if (err) {
 424			/* No memory or no space. */
 425			return ERR_PTR(err);
 426		}
 427		le_added = true;
 428
 429		/*
 430		 * al_add_le -> attr_set_size (list) -> ni_expand_list
 431		 * which moves some attributes out of primary record
 432		 * this means that name may point into moved memory
 433		 * reinit 'name' from le.
 434		 */
 435		name = le->name;
 436	}
 437
 438	attr = mi_insert_attr(ni, mi, type, name, name_len, asize, name_off);
 439	if (!attr) {
 440		if (le_added)
 441			al_remove_le(ni, le);
 442		return NULL;
 443	}
 444
 445	if (type == ATTR_LIST) {
 446		/* Attr list is not in list entry array. */
 447		goto out;
 448	}
 449
 450	if (!le)
 451		goto out;
 452
 453	/* Update ATTRIB Id and record reference. */
 454	le->id = attr->id;
 455	ni->attr_list.dirty = true;
 456	le->ref = ref;
 457
 458out:
 459	if (ins_le)
 460		*ins_le = le;
 461	return attr;
 462}
 463
 464/*
 465 * ni_repack
 466 *
 467 * Random write access to sparsed or compressed file may result to
 468 * not optimized packed runs.
 469 * Here is the place to optimize it.
 470 */
 471static int ni_repack(struct ntfs_inode *ni)
 472{
 473#if 1
 474	return 0;
 475#else
 476	int err = 0;
 477	struct ntfs_sb_info *sbi = ni->mi.sbi;
 478	struct mft_inode *mi, *mi_p = NULL;
 479	struct ATTRIB *attr = NULL, *attr_p;
 480	struct ATTR_LIST_ENTRY *le = NULL, *le_p;
 481	CLST alloc = 0;
 482	u8 cluster_bits = sbi->cluster_bits;
 483	CLST svcn, evcn = 0, svcn_p, evcn_p, next_svcn;
 484	u32 roff, rs = sbi->record_size;
 485	struct runs_tree run;
 486
 487	run_init(&run);
 488
 489	while ((attr = ni_enum_attr_ex(ni, attr, &le, &mi))) {
 490		if (!attr->non_res)
 491			continue;
 492
 493		svcn = le64_to_cpu(attr->nres.svcn);
 494		if (svcn != le64_to_cpu(le->vcn)) {
 495			err = -EINVAL;
 496			break;
 497		}
 498
 499		if (!svcn) {
 500			alloc = le64_to_cpu(attr->nres.alloc_size) >>
 501				cluster_bits;
 502			mi_p = NULL;
 503		} else if (svcn != evcn + 1) {
 504			err = -EINVAL;
 505			break;
 506		}
 507
 508		evcn = le64_to_cpu(attr->nres.evcn);
 509
 510		if (svcn > evcn + 1) {
 511			err = -EINVAL;
 512			break;
 513		}
 514
 515		if (!mi_p) {
 516			/* Do not try if not enough free space. */
 517			if (le32_to_cpu(mi->mrec->used) + 8 >= rs)
 518				continue;
 519
 520			/* Do not try if last attribute segment. */
 521			if (evcn + 1 == alloc)
 522				continue;
 523			run_close(&run);
 524		}
 525
 526		roff = le16_to_cpu(attr->nres.run_off);
 527
 528		if (roff > le32_to_cpu(attr->size)) {
 529			err = -EINVAL;
 530			break;
 531		}
 532
 533		err = run_unpack(&run, sbi, ni->mi.rno, svcn, evcn, svcn,
 534				 Add2Ptr(attr, roff),
 535				 le32_to_cpu(attr->size) - roff);
 536		if (err < 0)
 537			break;
 538
 539		if (!mi_p) {
 540			mi_p = mi;
 541			attr_p = attr;
 542			svcn_p = svcn;
 543			evcn_p = evcn;
 544			le_p = le;
 545			err = 0;
 546			continue;
 547		}
 548
 549		/*
 550		 * Run contains data from two records: mi_p and mi
 551		 * Try to pack in one.
 552		 */
 553		err = mi_pack_runs(mi_p, attr_p, &run, evcn + 1 - svcn_p);
 554		if (err)
 555			break;
 556
 557		next_svcn = le64_to_cpu(attr_p->nres.evcn) + 1;
 558
 559		if (next_svcn >= evcn + 1) {
 560			/* We can remove this attribute segment. */
 561			al_remove_le(ni, le);
 562			mi_remove_attr(NULL, mi, attr);
 563			le = le_p;
 564			continue;
 565		}
 566
 567		attr->nres.svcn = le->vcn = cpu_to_le64(next_svcn);
 568		mi->dirty = true;
 569		ni->attr_list.dirty = true;
 570
 571		if (evcn + 1 == alloc) {
 572			err = mi_pack_runs(mi, attr, &run,
 573					   evcn + 1 - next_svcn);
 574			if (err)
 575				break;
 576			mi_p = NULL;
 577		} else {
 578			mi_p = mi;
 579			attr_p = attr;
 580			svcn_p = next_svcn;
 581			evcn_p = evcn;
 582			le_p = le;
 583			run_truncate_head(&run, next_svcn);
 584		}
 585	}
 586
 587	if (err) {
 588		ntfs_inode_warn(&ni->vfs_inode, "repack problem");
 589		ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
 590
 591		/* Pack loaded but not packed runs. */
 592		if (mi_p)
 593			mi_pack_runs(mi_p, attr_p, &run, evcn_p + 1 - svcn_p);
 594	}
 595
 596	run_close(&run);
 597	return err;
 598#endif
 599}
 600
 601/*
 602 * ni_try_remove_attr_list
 603 *
 604 * Can we remove attribute list?
 605 * Check the case when primary record contains enough space for all attributes.
 606 */
 607static int ni_try_remove_attr_list(struct ntfs_inode *ni)
 608{
 609	int err = 0;
 610	struct ntfs_sb_info *sbi = ni->mi.sbi;
 611	struct ATTRIB *attr, *attr_list, *attr_ins;
 612	struct ATTR_LIST_ENTRY *le;
 613	struct mft_inode *mi;
 614	u32 asize, free;
 615	struct MFT_REF ref;
 616	struct MFT_REC *mrec;
 617	__le16 id;
 618
 619	if (!ni->attr_list.dirty)
 620		return 0;
 621
 622	err = ni_repack(ni);
 623	if (err)
 624		return err;
 625
 626	attr_list = mi_find_attr(ni, &ni->mi, NULL, ATTR_LIST, NULL, 0, NULL);
 627	if (!attr_list)
 628		return 0;
 629
 630	asize = le32_to_cpu(attr_list->size);
 631
 632	/* Free space in primary record without attribute list. */
 633	free = sbi->record_size - le32_to_cpu(ni->mi.mrec->used) + asize;
 634	mi_get_ref(&ni->mi, &ref);
 635
 636	le = NULL;
 637	while ((le = al_enumerate(ni, le))) {
 638		if (!memcmp(&le->ref, &ref, sizeof(ref)))
 639			continue;
 640
 641		if (le->vcn)
 642			return 0;
 643
 644		mi = ni_find_mi(ni, ino_get(&le->ref));
 645		if (!mi)
 646			return 0;
 647
 648		attr = mi_find_attr(ni, mi, NULL, le->type, le_name(le),
 649				    le->name_len, &le->id);
 650		if (!attr)
 651			return 0;
 652
 653		asize = le32_to_cpu(attr->size);
 654		if (asize > free)
 655			return 0;
 656
 657		free -= asize;
 658	}
 659
 660	/* Make a copy of primary record to restore if error. */
 661	mrec = kmemdup(ni->mi.mrec, sbi->record_size, GFP_NOFS);
 662	if (!mrec)
 663		return 0; /* Not critical. */
 664
 665	/* It seems that attribute list can be removed from primary record. */
 666	mi_remove_attr(NULL, &ni->mi, attr_list);
 667
 668	/*
 669	 * Repeat the cycle above and copy all attributes to primary record.
 670	 * Do not remove original attributes from subrecords!
 671	 * It should be success!
 672	 */
 673	le = NULL;
 674	while ((le = al_enumerate(ni, le))) {
 675		if (!memcmp(&le->ref, &ref, sizeof(ref)))
 676			continue;
 677
 678		mi = ni_find_mi(ni, ino_get(&le->ref));
 679		if (!mi) {
 680			/* Should never happened, 'cause already checked. */
 681			goto out;
 682		}
 683
 684		attr = mi_find_attr(ni, mi, NULL, le->type, le_name(le),
 685				    le->name_len, &le->id);
 686		if (!attr) {
 687			/* Should never happened, 'cause already checked. */
 688			goto out;
 689		}
 690		asize = le32_to_cpu(attr->size);
 691
 692		/* Insert into primary record. */
 693		attr_ins = mi_insert_attr(ni, &ni->mi, le->type, le_name(le),
 694					  le->name_len, asize,
 695					  le16_to_cpu(attr->name_off));
 696		if (!attr_ins) {
 697			/*
 698			 * No space in primary record (already checked).
 699			 */
 700			goto out;
 701		}
 702
 703		/* Copy all except id. */
 704		id = attr_ins->id;
 705		memcpy(attr_ins, attr, asize);
 706		attr_ins->id = id;
 707	}
 708
 709	/*
 710	 * Repeat the cycle above and remove all attributes from subrecords.
 711	 */
 712	le = NULL;
 713	while ((le = al_enumerate(ni, le))) {
 714		if (!memcmp(&le->ref, &ref, sizeof(ref)))
 715			continue;
 716
 717		mi = ni_find_mi(ni, ino_get(&le->ref));
 718		if (!mi)
 719			continue;
 720
 721		attr = mi_find_attr(ni, mi, NULL, le->type, le_name(le),
 722				    le->name_len, &le->id);
 723		if (!attr)
 724			continue;
 725
 726		/* Remove from original record. */
 727		mi_remove_attr(NULL, mi, attr);
 728	}
 729
 730	run_deallocate(sbi, &ni->attr_list.run, true);
 731	run_close(&ni->attr_list.run);
 732	ni->attr_list.size = 0;
 733	kvfree(ni->attr_list.le);
 734	ni->attr_list.le = NULL;
 735	ni->attr_list.dirty = false;
 736
 737	kfree(mrec);
 738	return 0;
 739out:
 740	/* Restore primary record. */
 741	swap(mrec, ni->mi.mrec);
 742	kfree(mrec);
 743	return 0;
 744}
 745
 746/*
 747 * ni_create_attr_list - Generates an attribute list for this primary record.
 748 */
 749int ni_create_attr_list(struct ntfs_inode *ni)
 750{
 751	struct ntfs_sb_info *sbi = ni->mi.sbi;
 752	int err;
 753	u32 lsize;
 754	struct ATTRIB *attr;
 755	struct ATTRIB *arr_move[7];
 756	struct ATTR_LIST_ENTRY *le, *le_b[7];
 757	struct MFT_REC *rec;
 758	bool is_mft;
 759	CLST rno = 0;
 760	struct mft_inode *mi;
 761	u32 free_b, nb, to_free, rs;
 762	u16 sz;
 763
 764	is_mft = ni->mi.rno == MFT_REC_MFT;
 765	rec = ni->mi.mrec;
 766	rs = sbi->record_size;
 767
 768	/*
 769	 * Skip estimating exact memory requirement.
 770	 * Looks like one record_size is always enough.
 771	 */
 772	le = kzalloc(al_aligned(rs), GFP_NOFS);
 773	if (!le)
 774		return -ENOMEM;
 775
 776	mi_get_ref(&ni->mi, &le->ref);
 777	ni->attr_list.le = le;
 778
 779	attr = NULL;
 780	nb = 0;
 781	free_b = 0;
 782	attr = NULL;
 783
 784	for (; (attr = mi_enum_attr(ni, &ni->mi, attr)); le = Add2Ptr(le, sz)) {
 785		sz = le_size(attr->name_len);
 786		le->type = attr->type;
 787		le->size = cpu_to_le16(sz);
 788		le->name_len = attr->name_len;
 789		le->name_off = offsetof(struct ATTR_LIST_ENTRY, name);
 790		le->vcn = 0;
 791		if (le != ni->attr_list.le)
 792			le->ref = ni->attr_list.le->ref;
 793		le->id = attr->id;
 794
 795		if (attr->name_len)
 796			memcpy(le->name, attr_name(attr),
 797			       sizeof(short) * attr->name_len);
 798		else if (attr->type == ATTR_STD)
 799			continue;
 800		else if (attr->type == ATTR_LIST)
 801			continue;
 802		else if (is_mft && attr->type == ATTR_DATA)
 803			continue;
 804
 805		if (!nb || nb < ARRAY_SIZE(arr_move)) {
 806			le_b[nb] = le;
 807			arr_move[nb++] = attr;
 808			free_b += le32_to_cpu(attr->size);
 809		}
 810	}
 811
 812	lsize = PtrOffset(ni->attr_list.le, le);
 813	ni->attr_list.size = lsize;
 814
 815	to_free = le32_to_cpu(rec->used) + lsize + SIZEOF_RESIDENT;
 816	if (to_free <= rs) {
 817		to_free = 0;
 818	} else {
 819		to_free -= rs;
 820
 821		if (to_free > free_b) {
 822			err = -EINVAL;
 823			goto out;
 824		}
 825	}
 826
 827	/* Allocate child MFT. */
 828	err = ntfs_look_free_mft(sbi, &rno, is_mft, ni, &mi);
 829	if (err)
 830		goto out;
 831
 832	err = -EINVAL;
 833	/* Call mi_remove_attr() in reverse order to keep pointers 'arr_move' valid. */
 834	while (to_free > 0) {
 835		struct ATTRIB *b = arr_move[--nb];
 836		u32 asize = le32_to_cpu(b->size);
 837		u16 name_off = le16_to_cpu(b->name_off);
 838
 839		attr = mi_insert_attr(ni, mi, b->type, Add2Ptr(b, name_off),
 840				      b->name_len, asize, name_off);
 841		if (!attr)
 842			goto out;
 843
 844		mi_get_ref(mi, &le_b[nb]->ref);
 845		le_b[nb]->id = attr->id;
 846
 847		/* Copy all except id. */
 848		memcpy(attr, b, asize);
 849		attr->id = le_b[nb]->id;
 850
 851		/* Remove from primary record. */
 852		if (!mi_remove_attr(NULL, &ni->mi, b))
 853			goto out;
 854
 855		if (to_free <= asize)
 856			break;
 857		to_free -= asize;
 858		if (!nb)
 859			goto out;
 860	}
 861
 862	attr = mi_insert_attr(ni, &ni->mi, ATTR_LIST, NULL, 0,
 863			      lsize + SIZEOF_RESIDENT, SIZEOF_RESIDENT);
 864	if (!attr)
 865		goto out;
 866
 867	attr->non_res = 0;
 868	attr->flags = 0;
 869	attr->res.data_size = cpu_to_le32(lsize);
 870	attr->res.data_off = SIZEOF_RESIDENT_LE;
 871	attr->res.flags = 0;
 872	attr->res.res = 0;
 873
 874	memcpy(resident_data_ex(attr, lsize), ni->attr_list.le, lsize);
 875
 876	ni->attr_list.dirty = false;
 877
 878	mark_inode_dirty(&ni->vfs_inode);
 879	return 0;
 880
 881out:
 882	kvfree(ni->attr_list.le);
 883	ni->attr_list.le = NULL;
 884	ni->attr_list.size = 0;
 885	return err;
 886}
 887
 888/*
 889 * ni_ins_attr_ext - Add an external attribute to the ntfs_inode.
 890 */
 891static int ni_ins_attr_ext(struct ntfs_inode *ni, struct ATTR_LIST_ENTRY *le,
 892			   enum ATTR_TYPE type, const __le16 *name, u8 name_len,
 893			   u32 asize, CLST svcn, u16 name_off, bool force_ext,
 894			   struct ATTRIB **ins_attr, struct mft_inode **ins_mi,
 895			   struct ATTR_LIST_ENTRY **ins_le)
 896{
 897	struct ATTRIB *attr;
 898	struct mft_inode *mi;
 899	CLST rno;
 900	u64 vbo;
 901	struct rb_node *node;
 902	int err;
 903	bool is_mft, is_mft_data;
 904	struct ntfs_sb_info *sbi = ni->mi.sbi;
 905
 906	is_mft = ni->mi.rno == MFT_REC_MFT;
 907	is_mft_data = is_mft && type == ATTR_DATA && !name_len;
 908
 909	if (asize > sbi->max_bytes_per_attr) {
 910		err = -EINVAL;
 911		goto out;
 912	}
 913
 914	/*
 915	 * Standard information and attr_list cannot be made external.
 916	 * The Log File cannot have any external attributes.
 917	 */
 918	if (type == ATTR_STD || type == ATTR_LIST ||
 919	    ni->mi.rno == MFT_REC_LOG) {
 920		err = -EINVAL;
 921		goto out;
 922	}
 923
 924	/* Create attribute list if it is not already existed. */
 925	if (!ni->attr_list.size) {
 926		err = ni_create_attr_list(ni);
 927		if (err)
 928			goto out;
 929	}
 930
 931	vbo = is_mft_data ? ((u64)svcn << sbi->cluster_bits) : 0;
 932
 933	if (force_ext)
 934		goto insert_ext;
 935
 936	/* Load all subrecords into memory. */
 937	err = ni_load_all_mi(ni);
 938	if (err)
 939		goto out;
 940
 941	/* Check each of loaded subrecord. */
 942	for (node = rb_first(&ni->mi_tree); node; node = rb_next(node)) {
 943		mi = rb_entry(node, struct mft_inode, node);
 944
 945		if (is_mft_data &&
 946		    (mi_enum_attr(ni, mi, NULL) ||
 947		     vbo <= ((u64)mi->rno << sbi->record_bits))) {
 948			/* We can't accept this record 'cause MFT's bootstrapping. */
 949			continue;
 950		}
 951		if (is_mft &&
 952		    mi_find_attr(ni, mi, NULL, ATTR_DATA, NULL, 0, NULL)) {
 953			/*
 954			 * This child record already has a ATTR_DATA.
 955			 * So it can't accept any other records.
 956			 */
 957			continue;
 958		}
 959
 960		if ((type != ATTR_NAME || name_len) &&
 961		    mi_find_attr(ni, mi, NULL, type, name, name_len, NULL)) {
 962			/* Only indexed attributes can share same record. */
 963			continue;
 964		}
 965
 966		/*
 967		 * Do not try to insert this attribute
 968		 * if there is no room in record.
 969		 */
 970		if (le32_to_cpu(mi->mrec->used) + asize > sbi->record_size)
 971			continue;
 972
 973		/* Try to insert attribute into this subrecord. */
 974		attr = ni_ins_new_attr(ni, mi, le, type, name, name_len, asize,
 975				       name_off, svcn, ins_le);
 976		if (!attr)
 977			continue;
 978		if (IS_ERR(attr))
 979			return PTR_ERR(attr);
 980
 981		if (ins_attr)
 982			*ins_attr = attr;
 983		if (ins_mi)
 984			*ins_mi = mi;
 985		return 0;
 986	}
 987
 988insert_ext:
 989	/* We have to allocate a new child subrecord. */
 990	err = ntfs_look_free_mft(sbi, &rno, is_mft_data, ni, &mi);
 991	if (err)
 992		goto out;
 993
 994	if (is_mft_data && vbo <= ((u64)rno << sbi->record_bits)) {
 995		err = -EINVAL;
 996		goto out1;
 997	}
 998
 999	attr = ni_ins_new_attr(ni, mi, le, type, name, name_len, asize,
1000			       name_off, svcn, ins_le);
1001	if (!attr) {
1002		err = -EINVAL;
1003		goto out2;
1004	}
1005
1006	if (IS_ERR(attr)) {
1007		err = PTR_ERR(attr);
1008		goto out2;
1009	}
1010
1011	if (ins_attr)
1012		*ins_attr = attr;
1013	if (ins_mi)
1014		*ins_mi = mi;
1015
1016	return 0;
1017
1018out2:
1019	ni_remove_mi(ni, mi);
1020
1021out1:
1022	mi_put(mi);
1023	ntfs_mark_rec_free(sbi, rno, is_mft);
1024
1025out:
1026	return err;
1027}
1028
1029/*
1030 * ni_insert_attr - Insert an attribute into the file.
1031 *
1032 * If the primary record has room, it will just insert the attribute.
1033 * If not, it may make the attribute external.
1034 * For $MFT::Data it may make room for the attribute by
1035 * making other attributes external.
1036 *
1037 * NOTE:
1038 * The ATTR_LIST and ATTR_STD cannot be made external.
1039 * This function does not fill new attribute full.
1040 * It only fills 'size'/'type'/'id'/'name_len' fields.
1041 */
1042static int ni_insert_attr(struct ntfs_inode *ni, enum ATTR_TYPE type,
1043			  const __le16 *name, u8 name_len, u32 asize,
1044			  u16 name_off, CLST svcn, struct ATTRIB **ins_attr,
1045			  struct mft_inode **ins_mi,
1046			  struct ATTR_LIST_ENTRY **ins_le)
1047{
1048	struct ntfs_sb_info *sbi = ni->mi.sbi;
1049	int err;
1050	struct ATTRIB *attr, *eattr;
1051	struct MFT_REC *rec;
1052	bool is_mft;
1053	struct ATTR_LIST_ENTRY *le;
1054	u32 list_reserve, max_free, free, used, t32;
1055	__le16 id;
1056	u16 t16;
1057
1058	is_mft = ni->mi.rno == MFT_REC_MFT;
1059	rec = ni->mi.mrec;
1060
1061	list_reserve = SIZEOF_NONRESIDENT + 3 * (1 + 2 * sizeof(u32));
1062	used = le32_to_cpu(rec->used);
1063	free = sbi->record_size - used;
1064
1065	if (is_mft && type != ATTR_LIST) {
1066		/* Reserve space for the ATTRIB list. */
1067		if (free < list_reserve)
1068			free = 0;
1069		else
1070			free -= list_reserve;
1071	}
1072
1073	if (asize <= free) {
1074		attr = ni_ins_new_attr(ni, &ni->mi, NULL, type, name, name_len,
1075				       asize, name_off, svcn, ins_le);
1076		if (IS_ERR(attr)) {
1077			err = PTR_ERR(attr);
1078			goto out;
1079		}
1080
1081		if (attr) {
1082			if (ins_attr)
1083				*ins_attr = attr;
1084			if (ins_mi)
1085				*ins_mi = &ni->mi;
1086			err = 0;
1087			goto out;
1088		}
1089	}
1090
1091	if (!is_mft || type != ATTR_DATA || svcn) {
1092		/* This ATTRIB will be external. */
1093		err = ni_ins_attr_ext(ni, NULL, type, name, name_len, asize,
1094				      svcn, name_off, false, ins_attr, ins_mi,
1095				      ins_le);
1096		goto out;
1097	}
1098
1099	/*
1100	 * Here we have: "is_mft && type == ATTR_DATA && !svcn"
1101	 *
1102	 * The first chunk of the $MFT::Data ATTRIB must be the base record.
1103	 * Evict as many other attributes as possible.
1104	 */
1105	max_free = free;
1106
1107	/* Estimate the result of moving all possible attributes away. */
1108	attr = NULL;
1109
1110	while ((attr = mi_enum_attr(ni, &ni->mi, attr))) {
1111		if (attr->type == ATTR_STD)
1112			continue;
1113		if (attr->type == ATTR_LIST)
1114			continue;
1115		max_free += le32_to_cpu(attr->size);
1116	}
1117
1118	if (max_free < asize + list_reserve) {
1119		/* Impossible to insert this attribute into primary record. */
1120		err = -EINVAL;
1121		goto out;
1122	}
1123
1124	/* Start real attribute moving. */
1125	attr = NULL;
1126
1127	for (;;) {
1128		attr = mi_enum_attr(ni, &ni->mi, attr);
1129		if (!attr) {
1130			/* We should never be here 'cause we have already check this case. */
1131			err = -EINVAL;
1132			goto out;
1133		}
1134
1135		/* Skip attributes that MUST be primary record. */
1136		if (attr->type == ATTR_STD || attr->type == ATTR_LIST)
1137			continue;
1138
1139		le = NULL;
1140		if (ni->attr_list.size) {
1141			le = al_find_le(ni, NULL, attr);
1142			if (!le) {
1143				/* Really this is a serious bug. */
1144				err = -EINVAL;
1145				goto out;
1146			}
1147		}
1148
1149		t32 = le32_to_cpu(attr->size);
1150		t16 = le16_to_cpu(attr->name_off);
1151		err = ni_ins_attr_ext(ni, le, attr->type, Add2Ptr(attr, t16),
1152				      attr->name_len, t32, attr_svcn(attr), t16,
1153				      false, &eattr, NULL, NULL);
1154		if (err)
1155			return err;
1156
1157		id = eattr->id;
1158		memcpy(eattr, attr, t32);
1159		eattr->id = id;
1160
1161		/* Remove from primary record. */
1162		mi_remove_attr(NULL, &ni->mi, attr);
1163
1164		/* attr now points to next attribute. */
1165		if (attr->type == ATTR_END)
1166			goto out;
1167	}
1168	while (asize + list_reserve > sbi->record_size - le32_to_cpu(rec->used))
1169		;
1170
1171	attr = ni_ins_new_attr(ni, &ni->mi, NULL, type, name, name_len, asize,
1172			       name_off, svcn, ins_le);
1173	if (!attr) {
1174		err = -EINVAL;
1175		goto out;
1176	}
1177
1178	if (IS_ERR(attr)) {
1179		err = PTR_ERR(attr);
1180		goto out;
1181	}
1182
1183	if (ins_attr)
1184		*ins_attr = attr;
1185	if (ins_mi)
1186		*ins_mi = &ni->mi;
1187
1188out:
1189	return err;
1190}
1191
1192/* ni_expand_mft_list - Split ATTR_DATA of $MFT. */
1193static int ni_expand_mft_list(struct ntfs_inode *ni)
1194{
1195	int err = 0;
1196	struct runs_tree *run = &ni->file.run;
1197	u32 asize, run_size, done = 0;
1198	struct ATTRIB *attr;
1199	struct rb_node *node;
1200	CLST mft_min, mft_new, svcn, evcn, plen;
1201	struct mft_inode *mi, *mi_min, *mi_new;
1202	struct ntfs_sb_info *sbi = ni->mi.sbi;
1203
1204	/* Find the nearest MFT. */
1205	mft_min = 0;
1206	mft_new = 0;
1207	mi_min = NULL;
1208
1209	for (node = rb_first(&ni->mi_tree); node; node = rb_next(node)) {
1210		mi = rb_entry(node, struct mft_inode, node);
1211
1212		attr = mi_enum_attr(ni, mi, NULL);
1213
1214		if (!attr) {
1215			mft_min = mi->rno;
1216			mi_min = mi;
1217			break;
1218		}
1219	}
1220
1221	if (ntfs_look_free_mft(sbi, &mft_new, true, ni, &mi_new)) {
1222		mft_new = 0;
1223		/* Really this is not critical. */
1224	} else if (mft_min > mft_new) {
1225		mft_min = mft_new;
1226		mi_min = mi_new;
1227	} else {
1228		ntfs_mark_rec_free(sbi, mft_new, true);
1229		mft_new = 0;
1230		ni_remove_mi(ni, mi_new);
1231	}
1232
1233	attr = mi_find_attr(ni, &ni->mi, NULL, ATTR_DATA, NULL, 0, NULL);
1234	if (!attr) {
1235		err = -EINVAL;
1236		goto out;
1237	}
1238
1239	asize = le32_to_cpu(attr->size);
1240
1241	evcn = le64_to_cpu(attr->nres.evcn);
1242	svcn = bytes_to_cluster(sbi, (u64)(mft_min + 1) << sbi->record_bits);
1243	if (evcn + 1 >= svcn) {
1244		err = -EINVAL;
1245		goto out;
1246	}
1247
1248	/*
1249	 * Split primary attribute [0 evcn] in two parts [0 svcn) + [svcn evcn].
1250	 *
1251	 * Update first part of ATTR_DATA in 'primary MFT.
1252	 */
1253	err = run_pack(run, 0, svcn, Add2Ptr(attr, SIZEOF_NONRESIDENT),
1254		       asize - SIZEOF_NONRESIDENT, &plen);
1255	if (err < 0)
1256		goto out;
1257
1258	run_size = ALIGN(err, 8);
1259	err = 0;
1260
1261	if (plen < svcn) {
1262		err = -EINVAL;
1263		goto out;
1264	}
1265
1266	attr->nres.evcn = cpu_to_le64(svcn - 1);
1267	attr->size = cpu_to_le32(run_size + SIZEOF_NONRESIDENT);
1268	/* 'done' - How many bytes of primary MFT becomes free. */
1269	done = asize - run_size - SIZEOF_NONRESIDENT;
1270	le32_sub_cpu(&ni->mi.mrec->used, done);
1271
1272	/* Estimate packed size (run_buf=NULL). */
1273	err = run_pack(run, svcn, evcn + 1 - svcn, NULL, sbi->record_size,
1274		       &plen);
1275	if (err < 0)
1276		goto out;
1277
1278	run_size = ALIGN(err, 8);
1279	err = 0;
1280
1281	if (plen < evcn + 1 - svcn) {
1282		err = -EINVAL;
1283		goto out;
1284	}
1285
1286	/*
1287	 * This function may implicitly call expand attr_list.
1288	 * Insert second part of ATTR_DATA in 'mi_min'.
1289	 */
1290	attr = ni_ins_new_attr(ni, mi_min, NULL, ATTR_DATA, NULL, 0,
1291			       SIZEOF_NONRESIDENT + run_size,
1292			       SIZEOF_NONRESIDENT, svcn, NULL);
1293	if (!attr) {
1294		err = -EINVAL;
1295		goto out;
1296	}
1297
1298	if (IS_ERR(attr)) {
1299		err = PTR_ERR(attr);
1300		goto out;
1301	}
1302
1303	attr->non_res = 1;
1304	attr->name_off = SIZEOF_NONRESIDENT_LE;
1305	attr->flags = 0;
1306
1307	/* This function can't fail - cause already checked above. */
1308	run_pack(run, svcn, evcn + 1 - svcn, Add2Ptr(attr, SIZEOF_NONRESIDENT),
1309		 run_size, &plen);
1310
1311	attr->nres.svcn = cpu_to_le64(svcn);
1312	attr->nres.evcn = cpu_to_le64(evcn);
1313	attr->nres.run_off = cpu_to_le16(SIZEOF_NONRESIDENT);
1314
1315out:
1316	if (mft_new) {
1317		ntfs_mark_rec_free(sbi, mft_new, true);
1318		ni_remove_mi(ni, mi_new);
1319	}
1320
1321	return !err && !done ? -EOPNOTSUPP : err;
1322}
1323
1324/*
1325 * ni_expand_list - Move all possible attributes out of primary record.
1326 */
1327int ni_expand_list(struct ntfs_inode *ni)
1328{
1329	int err = 0;
1330	u32 asize, done = 0;
1331	struct ATTRIB *attr, *ins_attr;
1332	struct ATTR_LIST_ENTRY *le;
1333	bool is_mft = ni->mi.rno == MFT_REC_MFT;
1334	struct MFT_REF ref;
1335
1336	mi_get_ref(&ni->mi, &ref);
1337	le = NULL;
1338
1339	while ((le = al_enumerate(ni, le))) {
1340		if (le->type == ATTR_STD)
1341			continue;
1342
1343		if (memcmp(&ref, &le->ref, sizeof(struct MFT_REF)))
1344			continue;
1345
1346		if (is_mft && le->type == ATTR_DATA)
1347			continue;
1348
1349		/* Find attribute in primary record. */
1350		attr = rec_find_attr_le(ni, &ni->mi, le);
1351		if (!attr) {
1352			err = -EINVAL;
1353			goto out;
1354		}
1355
1356		asize = le32_to_cpu(attr->size);
1357
1358		/* Always insert into new record to avoid collisions (deep recursive). */
1359		err = ni_ins_attr_ext(ni, le, attr->type, attr_name(attr),
1360				      attr->name_len, asize, attr_svcn(attr),
1361				      le16_to_cpu(attr->name_off), true,
1362				      &ins_attr, NULL, NULL);
1363
1364		if (err)
1365			goto out;
1366
1367		memcpy(ins_attr, attr, asize);
1368		ins_attr->id = le->id;
1369		/* Remove from primary record. */
1370		mi_remove_attr(NULL, &ni->mi, attr);
1371
1372		done += asize;
1373		goto out;
1374	}
1375
1376	if (!is_mft) {
1377		err = -EFBIG; /* Attr list is too big(?) */
1378		goto out;
1379	}
1380
1381	/* Split MFT data as much as possible. */
1382	err = ni_expand_mft_list(ni);
1383
1384out:
1385	return !err && !done ? -EOPNOTSUPP : err;
1386}
1387
1388/*
1389 * ni_insert_nonresident - Insert new nonresident attribute.
1390 */
1391int ni_insert_nonresident(struct ntfs_inode *ni, enum ATTR_TYPE type,
1392			  const __le16 *name, u8 name_len,
1393			  const struct runs_tree *run, CLST svcn, CLST len,
1394			  __le16 flags, struct ATTRIB **new_attr,
1395			  struct mft_inode **mi, struct ATTR_LIST_ENTRY **le)
1396{
1397	int err;
1398	CLST plen;
1399	struct ATTRIB *attr;
1400	bool is_ext = (flags & (ATTR_FLAG_SPARSED | ATTR_FLAG_COMPRESSED)) &&
1401		      !svcn;
1402	u32 name_size = ALIGN(name_len * sizeof(short), 8);
1403	u32 name_off = is_ext ? SIZEOF_NONRESIDENT_EX : SIZEOF_NONRESIDENT;
1404	u32 run_off = name_off + name_size;
1405	u32 run_size, asize;
1406	struct ntfs_sb_info *sbi = ni->mi.sbi;
1407
1408	/* Estimate packed size (run_buf=NULL). */
1409	err = run_pack(run, svcn, len, NULL, sbi->max_bytes_per_attr - run_off,
1410		       &plen);
1411	if (err < 0)
1412		goto out;
1413
1414	run_size = ALIGN(err, 8);
1415
1416	if (plen < len) {
1417		err = -EINVAL;
1418		goto out;
1419	}
1420
1421	asize = run_off + run_size;
1422
1423	if (asize > sbi->max_bytes_per_attr) {
1424		err = -EINVAL;
1425		goto out;
1426	}
1427
1428	err = ni_insert_attr(ni, type, name, name_len, asize, name_off, svcn,
1429			     &attr, mi, le);
1430
1431	if (err)
1432		goto out;
1433
1434	attr->non_res = 1;
1435	attr->name_off = cpu_to_le16(name_off);
1436	attr->flags = flags;
1437
1438	/* This function can't fail - cause already checked above. */
1439	run_pack(run, svcn, len, Add2Ptr(attr, run_off), run_size, &plen);
1440
1441	attr->nres.svcn = cpu_to_le64(svcn);
1442	attr->nres.evcn = cpu_to_le64((u64)svcn + len - 1);
1443
1444	if (new_attr)
1445		*new_attr = attr;
1446
1447	*(__le64 *)&attr->nres.run_off = cpu_to_le64(run_off);
1448
1449	attr->nres.alloc_size =
1450		svcn ? 0 : cpu_to_le64((u64)len << ni->mi.sbi->cluster_bits);
1451	attr->nres.data_size = attr->nres.alloc_size;
1452	attr->nres.valid_size = attr->nres.alloc_size;
1453
1454	if (is_ext) {
1455		if (flags & ATTR_FLAG_COMPRESSED)
1456			attr->nres.c_unit = NTFS_LZNT_CUNIT;
1457		attr->nres.total_size = attr->nres.alloc_size;
1458	}
1459
1460out:
1461	return err;
1462}
1463
1464/*
1465 * ni_insert_resident - Inserts new resident attribute.
1466 */
1467int ni_insert_resident(struct ntfs_inode *ni, u32 data_size,
1468		       enum ATTR_TYPE type, const __le16 *name, u8 name_len,
1469		       struct ATTRIB **new_attr, struct mft_inode **mi,
1470		       struct ATTR_LIST_ENTRY **le)
1471{
1472	int err;
1473	u32 name_size = ALIGN(name_len * sizeof(short), 8);
1474	u32 asize = SIZEOF_RESIDENT + name_size + ALIGN(data_size, 8);
1475	struct ATTRIB *attr;
1476
1477	err = ni_insert_attr(ni, type, name, name_len, asize, SIZEOF_RESIDENT,
1478			     0, &attr, mi, le);
1479	if (err)
1480		return err;
1481
1482	attr->non_res = 0;
1483	attr->flags = 0;
1484
1485	attr->res.data_size = cpu_to_le32(data_size);
1486	attr->res.data_off = cpu_to_le16(SIZEOF_RESIDENT + name_size);
1487	if (type == ATTR_NAME) {
1488		attr->res.flags = RESIDENT_FLAG_INDEXED;
1489
1490		/* is_attr_indexed(attr)) == true */
1491		le16_add_cpu(&ni->mi.mrec->hard_links, 1);
1492		ni->mi.dirty = true;
1493	}
1494	attr->res.res = 0;
1495
1496	if (new_attr)
1497		*new_attr = attr;
1498
1499	return 0;
1500}
1501
1502/*
1503 * ni_remove_attr_le - Remove attribute from record.
1504 */
1505void ni_remove_attr_le(struct ntfs_inode *ni, struct ATTRIB *attr,
1506		       struct mft_inode *mi, struct ATTR_LIST_ENTRY *le)
1507{
1508	mi_remove_attr(ni, mi, attr);
1509
1510	if (le)
1511		al_remove_le(ni, le);
1512}
1513
1514/*
1515 * ni_delete_all - Remove all attributes and frees allocates space.
1516 *
1517 * ntfs_evict_inode->ntfs_clear_inode->ni_delete_all (if no links).
1518 */
1519int ni_delete_all(struct ntfs_inode *ni)
1520{
1521	int err;
1522	struct ATTR_LIST_ENTRY *le = NULL;
1523	struct ATTRIB *attr = NULL;
1524	struct rb_node *node;
1525	u16 roff;
1526	u32 asize;
1527	CLST svcn, evcn;
1528	struct ntfs_sb_info *sbi = ni->mi.sbi;
1529	bool nt3 = is_ntfs3(sbi);
1530	struct MFT_REF ref;
1531
1532	while ((attr = ni_enum_attr_ex(ni, attr, &le, NULL))) {
1533		if (!nt3 || attr->name_len) {
1534			;
1535		} else if (attr->type == ATTR_REPARSE) {
1536			mi_get_ref(&ni->mi, &ref);
1537			ntfs_remove_reparse(sbi, 0, &ref);
1538		} else if (attr->type == ATTR_ID && !attr->non_res &&
1539			   le32_to_cpu(attr->res.data_size) >=
1540				   sizeof(struct GUID)) {
1541			ntfs_objid_remove(sbi, resident_data(attr));
1542		}
1543
1544		if (!attr->non_res)
1545			continue;
1546
1547		svcn = le64_to_cpu(attr->nres.svcn);
1548		evcn = le64_to_cpu(attr->nres.evcn);
1549
1550		if (evcn + 1 <= svcn)
1551			continue;
1552
1553		asize = le32_to_cpu(attr->size);
1554		roff = le16_to_cpu(attr->nres.run_off);
1555
1556		if (roff > asize) {
1557			/* ni_enum_attr_ex checks this case. */
1558			continue;
1559		}
1560
1561		/* run==1 means unpack and deallocate. */
1562		run_unpack_ex(RUN_DEALLOCATE, sbi, ni->mi.rno, svcn, evcn, svcn,
1563			      Add2Ptr(attr, roff), asize - roff);
1564	}
1565
1566	if (ni->attr_list.size) {
1567		run_deallocate(ni->mi.sbi, &ni->attr_list.run, true);
1568		al_destroy(ni);
1569	}
1570
1571	/* Free all subrecords. */
1572	for (node = rb_first(&ni->mi_tree); node;) {
1573		struct rb_node *next = rb_next(node);
1574		struct mft_inode *mi = rb_entry(node, struct mft_inode, node);
1575
1576		clear_rec_inuse(mi->mrec);
1577		mi->dirty = true;
1578		mi_write(mi, 0);
1579
1580		ntfs_mark_rec_free(sbi, mi->rno, false);
1581		ni_remove_mi(ni, mi);
1582		mi_put(mi);
1583		node = next;
1584	}
1585
1586	/* Free base record. */
1587	clear_rec_inuse(ni->mi.mrec);
1588	ni->mi.dirty = true;
1589	err = mi_write(&ni->mi, 0);
1590
1591	ntfs_mark_rec_free(sbi, ni->mi.rno, false);
1592
1593	return err;
1594}
1595
1596/* ni_fname_name
1597 *
1598 * Return: File name attribute by its value.
1599 */
1600struct ATTR_FILE_NAME *ni_fname_name(struct ntfs_inode *ni,
1601				     const struct le_str *uni,
1602				     const struct MFT_REF *home_dir,
1603				     struct mft_inode **mi,
1604				     struct ATTR_LIST_ENTRY **le)
1605{
1606	struct ATTRIB *attr = NULL;
1607	struct ATTR_FILE_NAME *fname;
1608
1609	if (le)
1610		*le = NULL;
1611
1612	/* Enumerate all names. */
1613next:
1614	attr = ni_find_attr(ni, attr, le, ATTR_NAME, NULL, 0, NULL, mi);
1615	if (!attr)
1616		return NULL;
1617
1618	fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
1619	if (!fname)
1620		goto next;
1621
1622	if (home_dir && memcmp(home_dir, &fname->home, sizeof(*home_dir)))
1623		goto next;
1624
1625	if (!uni)
1626		return fname;
1627
1628	if (uni->len != fname->name_len)
1629		goto next;
1630
1631	if (ntfs_cmp_names(uni->name, uni->len, fname->name, uni->len, NULL,
1632			   false))
1633		goto next;
1634	return fname;
1635}
1636
1637/*
1638 * ni_fname_type
1639 *
1640 * Return: File name attribute with given type.
1641 */
1642struct ATTR_FILE_NAME *ni_fname_type(struct ntfs_inode *ni, u8 name_type,
1643				     struct mft_inode **mi,
1644				     struct ATTR_LIST_ENTRY **le)
1645{
1646	struct ATTRIB *attr = NULL;
1647	struct ATTR_FILE_NAME *fname;
1648
1649	*le = NULL;
1650
1651	if (name_type == FILE_NAME_POSIX)
1652		return NULL;
1653
1654	/* Enumerate all names. */
1655	for (;;) {
1656		attr = ni_find_attr(ni, attr, le, ATTR_NAME, NULL, 0, NULL, mi);
1657		if (!attr)
1658			return NULL;
1659
1660		fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
1661		if (fname && name_type == fname->type)
1662			return fname;
1663	}
1664}
1665
1666/*
1667 * ni_new_attr_flags
1668 *
1669 * Process compressed/sparsed in special way.
1670 * NOTE: You need to set ni->std_fa = new_fa
1671 * after this function to keep internal structures in consistency.
1672 */
1673int ni_new_attr_flags(struct ntfs_inode *ni, enum FILE_ATTRIBUTE new_fa)
1674{
1675	struct ATTRIB *attr;
1676	struct mft_inode *mi;
1677	__le16 new_aflags;
1678	u32 new_asize;
1679
1680	attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi);
1681	if (!attr)
1682		return -EINVAL;
1683
1684	new_aflags = attr->flags;
1685
1686	if (new_fa & FILE_ATTRIBUTE_SPARSE_FILE)
1687		new_aflags |= ATTR_FLAG_SPARSED;
1688	else
1689		new_aflags &= ~ATTR_FLAG_SPARSED;
1690
1691	if (new_fa & FILE_ATTRIBUTE_COMPRESSED)
1692		new_aflags |= ATTR_FLAG_COMPRESSED;
1693	else
1694		new_aflags &= ~ATTR_FLAG_COMPRESSED;
1695
1696	if (new_aflags == attr->flags)
1697		return 0;
1698
1699	if ((new_aflags & (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED)) ==
1700	    (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED)) {
1701		ntfs_inode_warn(&ni->vfs_inode,
1702				"file can't be sparsed and compressed");
1703		return -EOPNOTSUPP;
1704	}
1705
1706	if (!attr->non_res)
1707		goto out;
1708
1709	if (attr->nres.data_size) {
1710		ntfs_inode_warn(
1711			&ni->vfs_inode,
1712			"one can change sparsed/compressed only for empty files");
1713		return -EOPNOTSUPP;
1714	}
1715
1716	/* Resize nonresident empty attribute in-place only. */
1717	new_asize = (new_aflags & (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED)) ?
1718			    (SIZEOF_NONRESIDENT_EX + 8) :
1719			    (SIZEOF_NONRESIDENT + 8);
1720
1721	if (!mi_resize_attr(mi, attr, new_asize - le32_to_cpu(attr->size)))
1722		return -EOPNOTSUPP;
1723
1724	if (new_aflags & ATTR_FLAG_SPARSED) {
1725		attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1726		/* Windows uses 16 clusters per frame but supports one cluster per frame too. */
1727		attr->nres.c_unit = 0;
1728		ni->vfs_inode.i_mapping->a_ops = &ntfs_aops;
1729	} else if (new_aflags & ATTR_FLAG_COMPRESSED) {
1730		attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1731		/* The only allowed: 16 clusters per frame. */
1732		attr->nres.c_unit = NTFS_LZNT_CUNIT;
1733		ni->vfs_inode.i_mapping->a_ops = &ntfs_aops_cmpr;
1734	} else {
1735		attr->name_off = SIZEOF_NONRESIDENT_LE;
1736		/* Normal files. */
1737		attr->nres.c_unit = 0;
1738		ni->vfs_inode.i_mapping->a_ops = &ntfs_aops;
1739	}
1740	attr->nres.run_off = attr->name_off;
1741out:
1742	attr->flags = new_aflags;
1743	mi->dirty = true;
1744
1745	return 0;
1746}
1747
1748/*
1749 * ni_parse_reparse
1750 *
1751 * buffer - memory for reparse buffer header
1752 */
1753enum REPARSE_SIGN ni_parse_reparse(struct ntfs_inode *ni, struct ATTRIB *attr,
1754				   struct REPARSE_DATA_BUFFER *buffer)
1755{
1756	const struct REPARSE_DATA_BUFFER *rp = NULL;
1757	u8 bits;
1758	u16 len;
1759	typeof(rp->CompressReparseBuffer) *cmpr;
1760
1761	/* Try to estimate reparse point. */
1762	if (!attr->non_res) {
1763		rp = resident_data_ex(attr, sizeof(struct REPARSE_DATA_BUFFER));
1764	} else if (le64_to_cpu(attr->nres.data_size) >=
1765		   sizeof(struct REPARSE_DATA_BUFFER)) {
1766		struct runs_tree run;
1767
1768		run_init(&run);
1769
1770		if (!attr_load_runs_vcn(ni, ATTR_REPARSE, NULL, 0, &run, 0) &&
1771		    !ntfs_read_run_nb(ni->mi.sbi, &run, 0, buffer,
1772				      sizeof(struct REPARSE_DATA_BUFFER),
1773				      NULL)) {
1774			rp = buffer;
1775		}
1776
1777		run_close(&run);
1778	}
1779
1780	if (!rp)
1781		return REPARSE_NONE;
1782
1783	len = le16_to_cpu(rp->ReparseDataLength);
1784	switch (rp->ReparseTag) {
1785	case (IO_REPARSE_TAG_MICROSOFT | IO_REPARSE_TAG_SYMBOLIC_LINK):
1786		break; /* Symbolic link. */
1787	case IO_REPARSE_TAG_MOUNT_POINT:
1788		break; /* Mount points and junctions. */
1789	case IO_REPARSE_TAG_SYMLINK:
1790		break;
1791	case IO_REPARSE_TAG_COMPRESS:
1792		/*
1793		 * WOF - Windows Overlay Filter - Used to compress files with
1794		 * LZX/Xpress.
1795		 *
1796		 * Unlike native NTFS file compression, the Windows
1797		 * Overlay Filter supports only read operations. This means
1798		 * that it doesn't need to sector-align each compressed chunk,
1799		 * so the compressed data can be packed more tightly together.
1800		 * If you open the file for writing, the WOF just decompresses
1801		 * the entire file, turning it back into a plain file.
1802		 *
1803		 * Ntfs3 driver decompresses the entire file only on write or
1804		 * change size requests.
1805		 */
1806
1807		cmpr = &rp->CompressReparseBuffer;
1808		if (len < sizeof(*cmpr) ||
1809		    cmpr->WofVersion != WOF_CURRENT_VERSION ||
1810		    cmpr->WofProvider != WOF_PROVIDER_SYSTEM ||
1811		    cmpr->ProviderVer != WOF_PROVIDER_CURRENT_VERSION) {
1812			return REPARSE_NONE;
1813		}
1814
1815		switch (cmpr->CompressionFormat) {
1816		case WOF_COMPRESSION_XPRESS4K:
1817			bits = 0xc; // 4k
1818			break;
1819		case WOF_COMPRESSION_XPRESS8K:
1820			bits = 0xd; // 8k
1821			break;
1822		case WOF_COMPRESSION_XPRESS16K:
1823			bits = 0xe; // 16k
1824			break;
1825		case WOF_COMPRESSION_LZX32K:
1826			bits = 0xf; // 32k
1827			break;
1828		default:
1829			bits = 0x10; // 64k
1830			break;
1831		}
1832		ni_set_ext_compress_bits(ni, bits);
1833		return REPARSE_COMPRESSED;
1834
1835	case IO_REPARSE_TAG_DEDUP:
1836		ni->ni_flags |= NI_FLAG_DEDUPLICATED;
1837		return REPARSE_DEDUPLICATED;
1838
1839	default:
1840		if (rp->ReparseTag & IO_REPARSE_TAG_NAME_SURROGATE)
1841			break;
1842
1843		return REPARSE_NONE;
1844	}
1845
1846	if (buffer != rp)
1847		memcpy(buffer, rp, sizeof(struct REPARSE_DATA_BUFFER));
1848
1849	/* Looks like normal symlink. */
1850	return REPARSE_LINK;
1851}
1852
1853/*
1854 * ni_fiemap - Helper for file_fiemap().
1855 *
1856 * Assumed ni_lock.
1857 * TODO: Less aggressive locks.
1858 */
1859int ni_fiemap(struct ntfs_inode *ni, struct fiemap_extent_info *fieinfo,
1860	      __u64 vbo, __u64 len)
1861{
1862	int err = 0;
1863	struct ntfs_sb_info *sbi = ni->mi.sbi;
1864	u8 cluster_bits = sbi->cluster_bits;
1865	struct runs_tree run;
1866	struct ATTRIB *attr;
1867	CLST vcn = vbo >> cluster_bits;
1868	CLST lcn, clen;
1869	u64 valid = ni->i_valid;
1870	u64 lbo, bytes;
1871	u64 end, alloc_size;
1872	size_t idx = -1;
1873	u32 flags;
1874	bool ok;
1875
1876	run_init(&run);
1877	if (S_ISDIR(ni->vfs_inode.i_mode)) {
1878		attr = ni_find_attr(ni, NULL, NULL, ATTR_ALLOC, I30_NAME,
1879				    ARRAY_SIZE(I30_NAME), NULL, NULL);
1880	} else {
1881		attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL,
1882				    NULL);
1883		if (!attr) {
1884			err = -EINVAL;
1885			goto out;
1886		}
1887		if (is_attr_compressed(attr)) {
1888			/* Unfortunately cp -r incorrectly treats compressed clusters. */
1889			err = -EOPNOTSUPP;
1890			ntfs_inode_warn(
1891				&ni->vfs_inode,
1892				"fiemap is not supported for compressed file (cp -r)");
1893			goto out;
1894		}
1895	}
1896
1897	if (!attr || !attr->non_res) {
1898		err = fiemap_fill_next_extent(
1899			fieinfo, 0, 0,
1900			attr ? le32_to_cpu(attr->res.data_size) : 0,
1901			FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_LAST |
1902				FIEMAP_EXTENT_MERGED);
1903		goto out;
1904	}
1905
1906	end = vbo + len;
1907	alloc_size = le64_to_cpu(attr->nres.alloc_size);
1908	if (end > alloc_size)
1909		end = alloc_size;
1910
1911	while (vbo < end) {
1912		if (idx == -1) {
1913			ok = run_lookup_entry(&run, vcn, &lcn, &clen, &idx);
1914		} else {
1915			CLST vcn_next = vcn;
1916
1917			ok = run_get_entry(&run, ++idx, &vcn, &lcn, &clen) &&
1918			     vcn == vcn_next;
1919			if (!ok)
1920				vcn = vcn_next;
1921		}
1922
1923		if (!ok) {
1924			err = attr_load_runs_vcn(ni, attr->type,
1925						 attr_name(attr),
1926						 attr->name_len, &run, vcn);
1927
1928			if (err)
1929				break;
1930
1931			ok = run_lookup_entry(&run, vcn, &lcn, &clen, &idx);
1932
1933			if (!ok) {
1934				err = -EINVAL;
1935				break;
1936			}
1937		}
1938
1939		if (!clen) {
1940			err = -EINVAL; // ?
1941			break;
1942		}
1943
1944		if (lcn == SPARSE_LCN) {
1945			vcn += clen;
1946			vbo = (u64)vcn << cluster_bits;
1947			continue;
1948		}
1949
1950		flags = FIEMAP_EXTENT_MERGED;
1951		if (S_ISDIR(ni->vfs_inode.i_mode)) {
1952			;
1953		} else if (is_attr_compressed(attr)) {
1954			CLST clst_data;
1955
1956			err = attr_is_frame_compressed(ni, attr,
1957						       vcn >> attr->nres.c_unit,
1958						       &clst_data, &run);
1959			if (err)
1960				break;
1961			if (clst_data < NTFS_LZNT_CLUSTERS)
1962				flags |= FIEMAP_EXTENT_ENCODED;
1963		} else if (is_attr_encrypted(attr)) {
1964			flags |= FIEMAP_EXTENT_DATA_ENCRYPTED;
1965		}
1966
1967		vbo = (u64)vcn << cluster_bits;
1968		bytes = (u64)clen << cluster_bits;
1969		lbo = (u64)lcn << cluster_bits;
1970
1971		vcn += clen;
1972
1973		if (vbo + bytes >= end)
1974			bytes = end - vbo;
1975
1976		if (vbo + bytes <= valid) {
1977			;
1978		} else if (vbo >= valid) {
1979			flags |= FIEMAP_EXTENT_UNWRITTEN;
1980		} else {
1981			/* vbo < valid && valid < vbo + bytes */
1982			u64 dlen = valid - vbo;
1983
1984			if (vbo + dlen >= end)
1985				flags |= FIEMAP_EXTENT_LAST;
1986
1987			err = fiemap_fill_next_extent(fieinfo, vbo, lbo, dlen,
1988						      flags);
1989
1990			if (err < 0)
1991				break;
1992			if (err == 1) {
1993				err = 0;
1994				break;
1995			}
1996
1997			vbo = valid;
1998			bytes -= dlen;
1999			if (!bytes)
2000				continue;
2001
2002			lbo += dlen;
2003			flags |= FIEMAP_EXTENT_UNWRITTEN;
2004		}
2005
2006		if (vbo + bytes >= end)
2007			flags |= FIEMAP_EXTENT_LAST;
2008
2009		err = fiemap_fill_next_extent(fieinfo, vbo, lbo, bytes, flags);
2010		if (err < 0)
2011			break;
2012		if (err == 1) {
2013			err = 0;
2014			break;
2015		}
2016
2017		vbo += bytes;
2018	}
2019
2020out:
2021	run_close(&run);
2022	return err;
2023}
2024
2025static struct page *ntfs_lock_new_page(struct address_space *mapping,
2026		pgoff_t index, gfp_t gfp)
2027{
2028	struct folio *folio = __filemap_get_folio(mapping, index,
2029			FGP_LOCK | FGP_ACCESSED | FGP_CREAT, gfp);
2030	struct page *page;
2031
2032	if (IS_ERR(folio))
2033		return ERR_CAST(folio);
2034
2035	if (!folio_test_uptodate(folio))
2036		return folio_file_page(folio, index);
2037
2038	/* Use a temporary page to avoid data corruption */
2039	folio_unlock(folio);
2040	folio_put(folio);
2041	page = alloc_page(gfp);
2042	if (!page)
2043		return ERR_PTR(-ENOMEM);
2044	__SetPageLocked(page);
2045	return page;
2046}
2047
2048/*
2049 * ni_read_folio_cmpr
2050 *
2051 * When decompressing, we typically obtain more than one page per reference.
2052 * We inject the additional pages into the page cache.
2053 */
2054int ni_read_folio_cmpr(struct ntfs_inode *ni, struct folio *folio)
2055{
2056	int err;
2057	struct ntfs_sb_info *sbi = ni->mi.sbi;
2058	struct address_space *mapping = folio->mapping;
2059	pgoff_t index;
2060	u64 frame_vbo, vbo = folio_pos(folio);
2061	struct page **pages = NULL; /* Array of at most 16 pages. stack? */
2062	u8 frame_bits;
2063	CLST frame;
2064	u32 i, idx, frame_size, pages_per_frame;
2065	gfp_t gfp_mask;
2066	struct page *pg;
2067
2068	if (vbo >= i_size_read(&ni->vfs_inode)) {
2069		folio_zero_range(folio, 0, folio_size(folio));
2070		folio_mark_uptodate(folio);
2071		err = 0;
2072		goto out;
2073	}
2074
2075	if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
2076		/* Xpress or LZX. */
2077		frame_bits = ni_ext_compress_bits(ni);
2078	} else {
2079		/* LZNT compression. */
2080		frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits;
2081	}
2082	frame_size = 1u << frame_bits;
2083	frame = vbo >> frame_bits;
2084	frame_vbo = (u64)frame << frame_bits;
2085	idx = (vbo - frame_vbo) >> PAGE_SHIFT;
2086
2087	pages_per_frame = frame_size >> PAGE_SHIFT;
2088	pages = kcalloc(pages_per_frame, sizeof(struct page *), GFP_NOFS);
2089	if (!pages) {
2090		err = -ENOMEM;
2091		goto out;
2092	}
2093
2094	pages[idx] = &folio->page;
2095	index = frame_vbo >> PAGE_SHIFT;
2096	gfp_mask = mapping_gfp_mask(mapping);
2097
2098	for (i = 0; i < pages_per_frame; i++, index++) {
2099		if (i == idx)
2100			continue;
2101
2102		pg = ntfs_lock_new_page(mapping, index, gfp_mask);
2103		if (IS_ERR(pg)) {
2104			err = PTR_ERR(pg);
2105			goto out1;
2106		}
2107		pages[i] = pg;
2108	}
2109
2110	ni_lock(ni);
2111	err = ni_read_frame(ni, frame_vbo, pages, pages_per_frame, 0);
2112	ni_unlock(ni);
2113
2114out1:
2115	for (i = 0; i < pages_per_frame; i++) {
2116		pg = pages[i];
2117		if (i == idx || !pg)
2118			continue;
2119		unlock_page(pg);
2120		put_page(pg);
2121	}
2122
2123out:
2124	/* At this point, err contains 0 or -EIO depending on the "critical" page. */
2125	kfree(pages);
2126	folio_unlock(folio);
2127
2128	return err;
2129}
2130
2131#ifdef CONFIG_NTFS3_LZX_XPRESS
2132/*
2133 * ni_decompress_file - Decompress LZX/Xpress compressed file.
2134 *
2135 * Remove ATTR_DATA::WofCompressedData.
2136 * Remove ATTR_REPARSE.
2137 */
2138int ni_decompress_file(struct ntfs_inode *ni)
2139{
2140	struct ntfs_sb_info *sbi = ni->mi.sbi;
2141	struct inode *inode = &ni->vfs_inode;
2142	loff_t i_size = i_size_read(inode);
2143	struct address_space *mapping = inode->i_mapping;
2144	gfp_t gfp_mask = mapping_gfp_mask(mapping);
2145	struct page **pages = NULL;
2146	struct ATTR_LIST_ENTRY *le;
2147	struct ATTRIB *attr;
2148	CLST vcn, cend, lcn, clen, end;
2149	pgoff_t index;
2150	u64 vbo;
2151	u8 frame_bits;
2152	u32 i, frame_size, pages_per_frame, bytes;
2153	struct mft_inode *mi;
2154	int err;
2155
2156	/* Clusters for decompressed data. */
2157	cend = bytes_to_cluster(sbi, i_size);
2158
2159	if (!i_size)
2160		goto remove_wof;
2161
2162	/* Check in advance. */
2163	if (cend > wnd_zeroes(&sbi->used.bitmap)) {
2164		err = -ENOSPC;
2165		goto out;
2166	}
2167
2168	frame_bits = ni_ext_compress_bits(ni);
2169	frame_size = 1u << frame_bits;
2170	pages_per_frame = frame_size >> PAGE_SHIFT;
2171	pages = kcalloc(pages_per_frame, sizeof(struct page *), GFP_NOFS);
2172	if (!pages) {
2173		err = -ENOMEM;
2174		goto out;
2175	}
2176
2177	/*
2178	 * Step 1: Decompress data and copy to new allocated clusters.
2179	 */
2180	index = 0;
2181	for (vbo = 0; vbo < i_size; vbo += bytes) {
2182		bool new;
2183
2184		bytes = vbo + frame_size > i_size ? (i_size - vbo) : frame_size;
2185		end = bytes_to_cluster(sbi, vbo + bytes);
2186
2187		for (vcn = vbo >> sbi->cluster_bits; vcn < end; vcn += clen) {
2188			err = attr_data_get_block(ni, vcn, cend - vcn, &lcn,
2189						  &clen, &new, false);
2190			if (err)
2191				goto out;
2192		}
2193
2194		for (i = 0; i < pages_per_frame; i++, index++) {
2195			struct page *pg;
2196
2197			pg = ntfs_lock_new_page(mapping, index, gfp_mask);
2198			if (IS_ERR(pg)) {
2199				while (i--) {
2200					unlock_page(pages[i]);
2201					put_page(pages[i]);
2202				}
2203				err = PTR_ERR(pg);
2204				goto out;
2205			}
2206			pages[i] = pg;
2207		}
2208
2209		err = ni_read_frame(ni, vbo, pages, pages_per_frame, 1);
2210
2211		for (i = 0; i < pages_per_frame; i++) {
2212			unlock_page(pages[i]);
2213			put_page(pages[i]);
2214		}
2215
2216		if (err)
2217			goto out;
2218
2219		cond_resched();
2220	}
2221
2222remove_wof:
2223	/*
2224	 * Step 2: Deallocate attributes ATTR_DATA::WofCompressedData
2225	 * and ATTR_REPARSE.
2226	 */
2227	attr = NULL;
2228	le = NULL;
2229	while ((attr = ni_enum_attr_ex(ni, attr, &le, NULL))) {
2230		CLST svcn, evcn;
2231		u32 asize, roff;
2232
2233		if (attr->type == ATTR_REPARSE) {
2234			struct MFT_REF ref;
2235
2236			mi_get_ref(&ni->mi, &ref);
2237			ntfs_remove_reparse(sbi, 0, &ref);
2238		}
2239
2240		if (!attr->non_res)
2241			continue;
2242
2243		if (attr->type != ATTR_REPARSE &&
2244		    (attr->type != ATTR_DATA ||
2245		     attr->name_len != ARRAY_SIZE(WOF_NAME) ||
2246		     memcmp(attr_name(attr), WOF_NAME, sizeof(WOF_NAME))))
2247			continue;
2248
2249		svcn = le64_to_cpu(attr->nres.svcn);
2250		evcn = le64_to_cpu(attr->nres.evcn);
2251
2252		if (evcn + 1 <= svcn)
2253			continue;
2254
2255		asize = le32_to_cpu(attr->size);
2256		roff = le16_to_cpu(attr->nres.run_off);
2257
2258		if (roff > asize) {
2259			err = -EINVAL;
2260			goto out;
2261		}
2262
2263		/*run==1  Means unpack and deallocate. */
2264		run_unpack_ex(RUN_DEALLOCATE, sbi, ni->mi.rno, svcn, evcn, svcn,
2265			      Add2Ptr(attr, roff), asize - roff);
2266	}
2267
2268	/*
2269	 * Step 3: Remove attribute ATTR_DATA::WofCompressedData.
2270	 */
2271	err = ni_remove_attr(ni, ATTR_DATA, WOF_NAME, ARRAY_SIZE(WOF_NAME),
2272			     false, NULL);
2273	if (err)
2274		goto out;
2275
2276	/*
2277	 * Step 4: Remove ATTR_REPARSE.
2278	 */
2279	err = ni_remove_attr(ni, ATTR_REPARSE, NULL, 0, false, NULL);
2280	if (err)
2281		goto out;
2282
2283	/*
2284	 * Step 5: Remove sparse flag from data attribute.
2285	 */
2286	attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, &mi);
2287	if (!attr) {
2288		err = -EINVAL;
2289		goto out;
2290	}
2291
2292	if (attr->non_res && is_attr_sparsed(attr)) {
2293		/* Sparsed attribute header is 8 bytes bigger than normal. */
2294		struct MFT_REC *rec = mi->mrec;
2295		u32 used = le32_to_cpu(rec->used);
2296		u32 asize = le32_to_cpu(attr->size);
2297		u16 roff = le16_to_cpu(attr->nres.run_off);
2298		char *rbuf = Add2Ptr(attr, roff);
2299
2300		memmove(rbuf - 8, rbuf, used - PtrOffset(rec, rbuf));
2301		attr->size = cpu_to_le32(asize - 8);
2302		attr->flags &= ~ATTR_FLAG_SPARSED;
2303		attr->nres.run_off = cpu_to_le16(roff - 8);
2304		attr->nres.c_unit = 0;
2305		rec->used = cpu_to_le32(used - 8);
2306		mi->dirty = true;
2307		ni->std_fa &= ~(FILE_ATTRIBUTE_SPARSE_FILE |
2308				FILE_ATTRIBUTE_REPARSE_POINT);
2309
2310		mark_inode_dirty(inode);
2311	}
2312
2313	/* Clear cached flag. */
2314	ni->ni_flags &= ~NI_FLAG_COMPRESSED_MASK;
2315	if (ni->file.offs_folio) {
2316		folio_put(ni->file.offs_folio);
2317		ni->file.offs_folio = NULL;
2318	}
2319	mapping->a_ops = &ntfs_aops;
2320
2321out:
2322	kfree(pages);
2323	if (err)
2324		_ntfs_bad_inode(inode);
2325
2326	return err;
2327}
2328
2329/*
2330 * decompress_lzx_xpress - External compression LZX/Xpress.
2331 */
2332static int decompress_lzx_xpress(struct ntfs_sb_info *sbi, const char *cmpr,
2333				 size_t cmpr_size, void *unc, size_t unc_size,
2334				 u32 frame_size)
2335{
2336	int err;
2337	void *ctx;
2338
2339	if (cmpr_size == unc_size) {
2340		/* Frame not compressed. */
2341		memcpy(unc, cmpr, unc_size);
2342		return 0;
2343	}
2344
2345	err = 0;
2346	if (frame_size == 0x8000) {
2347		mutex_lock(&sbi->compress.mtx_lzx);
2348		/* LZX: Frame compressed. */
2349		ctx = sbi->compress.lzx;
2350		if (!ctx) {
2351			/* Lazy initialize LZX decompress context. */
2352			ctx = lzx_allocate_decompressor();
2353			if (!ctx) {
2354				err = -ENOMEM;
2355				goto out1;
2356			}
2357
2358			sbi->compress.lzx = ctx;
2359		}
2360
2361		if (lzx_decompress(ctx, cmpr, cmpr_size, unc, unc_size)) {
2362			/* Treat all errors as "invalid argument". */
2363			err = -EINVAL;
2364		}
2365out1:
2366		mutex_unlock(&sbi->compress.mtx_lzx);
2367	} else {
2368		/* XPRESS: Frame compressed. */
2369		mutex_lock(&sbi->compress.mtx_xpress);
2370		ctx = sbi->compress.xpress;
2371		if (!ctx) {
2372			/* Lazy initialize Xpress decompress context. */
2373			ctx = xpress_allocate_decompressor();
2374			if (!ctx) {
2375				err = -ENOMEM;
2376				goto out2;
2377			}
2378
2379			sbi->compress.xpress = ctx;
2380		}
2381
2382		if (xpress_decompress(ctx, cmpr, cmpr_size, unc, unc_size)) {
2383			/* Treat all errors as "invalid argument". */
2384			err = -EINVAL;
2385		}
2386out2:
2387		mutex_unlock(&sbi->compress.mtx_xpress);
2388	}
2389	return err;
2390}
2391#endif
2392
2393/*
2394 * ni_read_frame
2395 *
2396 * Pages - Array of locked pages.
2397 */
2398int ni_read_frame(struct ntfs_inode *ni, u64 frame_vbo, struct page **pages,
2399		  u32 pages_per_frame, int copy)
2400{
2401	int err;
2402	struct ntfs_sb_info *sbi = ni->mi.sbi;
2403	u8 cluster_bits = sbi->cluster_bits;
2404	char *frame_ondisk = NULL;
2405	char *frame_mem = NULL;
2406	struct ATTR_LIST_ENTRY *le = NULL;
2407	struct runs_tree *run = &ni->file.run;
2408	u64 valid_size = ni->i_valid;
2409	u64 vbo_disk;
2410	size_t unc_size;
2411	u32 frame_size, i, ondisk_size;
2412	struct page *pg;
2413	struct ATTRIB *attr;
2414	CLST frame, clst_data;
2415
2416	/*
2417	 * To simplify decompress algorithm do vmap for source
2418	 * and target pages.
2419	 */
2420	frame_size = pages_per_frame << PAGE_SHIFT;
2421	frame_mem = vmap(pages, pages_per_frame, VM_MAP, PAGE_KERNEL);
2422	if (!frame_mem) {
2423		err = -ENOMEM;
2424		goto out;
2425	}
2426
2427	attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL, NULL);
2428	if (!attr) {
2429		err = -ENOENT;
2430		goto out1;
2431	}
2432
2433	if (!attr->non_res) {
2434		u32 data_size = le32_to_cpu(attr->res.data_size);
2435
2436		memset(frame_mem, 0, frame_size);
2437		if (frame_vbo < data_size) {
2438			ondisk_size = data_size - frame_vbo;
2439			memcpy(frame_mem, resident_data(attr) + frame_vbo,
2440			       min(ondisk_size, frame_size));
2441		}
2442		err = 0;
2443		goto out1;
2444	}
2445
2446	if (frame_vbo >= valid_size) {
2447		memset(frame_mem, 0, frame_size);
2448		err = 0;
2449		goto out1;
2450	}
2451
2452	if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
2453#ifndef CONFIG_NTFS3_LZX_XPRESS
2454		err = -EOPNOTSUPP;
2455		goto out1;
2456#else
2457		loff_t i_size = i_size_read(&ni->vfs_inode);
2458		u32 frame_bits = ni_ext_compress_bits(ni);
2459		u64 frame64 = frame_vbo >> frame_bits;
2460		u64 frames, vbo_data;
2461
2462		if (frame_size != (1u << frame_bits)) {
2463			err = -EINVAL;
2464			goto out1;
2465		}
2466		switch (frame_size) {
2467		case 0x1000:
2468		case 0x2000:
2469		case 0x4000:
2470		case 0x8000:
2471			break;
2472		default:
2473			/* Unknown compression. */
2474			err = -EOPNOTSUPP;
2475			goto out1;
2476		}
2477
2478		attr = ni_find_attr(ni, attr, &le, ATTR_DATA, WOF_NAME,
2479				    ARRAY_SIZE(WOF_NAME), NULL, NULL);
2480		if (!attr) {
2481			ntfs_inode_err(
2482				&ni->vfs_inode,
2483				"external compressed file should contains data attribute \"WofCompressedData\"");
2484			err = -EINVAL;
2485			goto out1;
2486		}
2487
2488		if (!attr->non_res) {
2489			run = NULL;
2490		} else {
2491			run = run_alloc();
2492			if (!run) {
2493				err = -ENOMEM;
2494				goto out1;
2495			}
2496		}
2497
2498		frames = (i_size - 1) >> frame_bits;
2499
2500		err = attr_wof_frame_info(ni, attr, run, frame64, frames,
2501					  frame_bits, &ondisk_size, &vbo_data);
2502		if (err)
2503			goto out1;
2504
2505		if (frame64 == frames) {
2506			unc_size = 1 + ((i_size - 1) & (frame_size - 1));
2507			ondisk_size = attr_size(attr) - vbo_data;
2508		} else {
2509			unc_size = frame_size;
2510		}
2511
2512		if (ondisk_size > frame_size) {
2513			err = -EINVAL;
2514			goto out1;
2515		}
2516
2517		if (!attr->non_res) {
2518			if (vbo_data + ondisk_size >
2519			    le32_to_cpu(attr->res.data_size)) {
2520				err = -EINVAL;
2521				goto out1;
2522			}
2523
2524			err = decompress_lzx_xpress(
2525				sbi, Add2Ptr(resident_data(attr), vbo_data),
2526				ondisk_size, frame_mem, unc_size, frame_size);
2527			goto out1;
2528		}
2529		vbo_disk = vbo_data;
2530		/* Load all runs to read [vbo_disk-vbo_to). */
2531		err = attr_load_runs_range(ni, ATTR_DATA, WOF_NAME,
2532					   ARRAY_SIZE(WOF_NAME), run, vbo_disk,
2533					   vbo_data + ondisk_size);
2534		if (err)
2535			goto out1;
2536#endif
2537	} else if (is_attr_compressed(attr)) {
2538		/* LZNT compression. */
2539		if (sbi->cluster_size > NTFS_LZNT_MAX_CLUSTER) {
2540			err = -EOPNOTSUPP;
2541			goto out1;
2542		}
2543
2544		if (attr->nres.c_unit != NTFS_LZNT_CUNIT) {
2545			err = -EOPNOTSUPP;
2546			goto out1;
2547		}
2548
2549		down_write(&ni->file.run_lock);
2550		run_truncate_around(run, le64_to_cpu(attr->nres.svcn));
2551		frame = frame_vbo >> (cluster_bits + NTFS_LZNT_CUNIT);
2552		err = attr_is_frame_compressed(ni, attr, frame, &clst_data,
2553					       run);
2554		up_write(&ni->file.run_lock);
2555		if (err)
2556			goto out1;
2557
2558		if (!clst_data) {
2559			memset(frame_mem, 0, frame_size);
2560			goto out1;
2561		}
2562
2563		frame_size = sbi->cluster_size << NTFS_LZNT_CUNIT;
2564		ondisk_size = clst_data << cluster_bits;
2565
2566		if (clst_data >= NTFS_LZNT_CLUSTERS) {
2567			/* Frame is not compressed. */
2568			down_read(&ni->file.run_lock);
2569			err = ntfs_read_run(sbi, run, frame_mem, frame_vbo,
2570					    ondisk_size);
2571			up_read(&ni->file.run_lock);
2572			goto out1;
2573		}
2574		vbo_disk = frame_vbo;
2575	} else {
2576		__builtin_unreachable();
2577		err = -EINVAL;
2578		goto out1;
2579	}
2580
2581	/* Allocate memory to read compressed data to. */
2582	frame_ondisk = kvmalloc(ondisk_size, GFP_KERNEL);
2583	if (!frame_ondisk) {
2584		err = -ENOMEM;
2585		goto out1;
2586	}
2587
2588	/* Read 'ondisk_size' bytes from disk. */
2589	down_read(&ni->file.run_lock);
2590	err = ntfs_read_run(sbi, run, frame_ondisk, vbo_disk, ondisk_size);
2591	up_read(&ni->file.run_lock);
2592	if (err)
2593		goto out2;
2594
2595#ifdef CONFIG_NTFS3_LZX_XPRESS
2596	if (run != &ni->file.run) {
2597		/* LZX or XPRESS */
2598		err = decompress_lzx_xpress(sbi, frame_ondisk, ondisk_size,
2599					    frame_mem, unc_size, frame_size);
2600	} else
2601#endif
2602	{
2603		/* LZNT - Native NTFS compression. */
2604		unc_size = decompress_lznt(frame_ondisk, ondisk_size, frame_mem,
2605					   frame_size);
2606		if ((ssize_t)unc_size < 0)
2607			err = unc_size;
2608		else if (!unc_size || unc_size > frame_size)
2609			err = -EINVAL;
2610	}
2611	if (!err && valid_size < frame_vbo + frame_size) {
2612		size_t ok = valid_size - frame_vbo;
2613
2614		memset(frame_mem + ok, 0, frame_size - ok);
2615	}
2616
2617out2:
2618	kvfree(frame_ondisk);
2619out1:
2620#ifdef CONFIG_NTFS3_LZX_XPRESS
2621	if (run != &ni->file.run)
2622		run_free(run);
2623	if (!err && copy) {
2624		/* We are called from 'ni_decompress_file' */
2625		/* Copy decompressed LZX or XPRESS data into new place. */
2626		down_read(&ni->file.run_lock);
2627		err = ntfs_write_run(sbi, &ni->file.run, frame_mem, frame_vbo,
2628				     frame_size);
2629		up_read(&ni->file.run_lock);
2630	}
2631#endif
2632	vunmap(frame_mem);
2633out:
2634	for (i = 0; i < pages_per_frame; i++) {
2635		pg = pages[i];
2636		SetPageUptodate(pg);
2637	}
2638
2639	return err;
2640}
2641
2642/*
2643 * ni_write_frame
2644 *
2645 * Pages - Array of locked pages.
2646 */
2647int ni_write_frame(struct ntfs_inode *ni, struct page **pages,
2648		   u32 pages_per_frame)
2649{
2650	int err;
2651	struct ntfs_sb_info *sbi = ni->mi.sbi;
2652	struct folio *folio = page_folio(pages[0]);
2653	u8 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits;
2654	u32 frame_size = sbi->cluster_size << NTFS_LZNT_CUNIT;
2655	u64 frame_vbo = folio_pos(folio);
2656	CLST frame = frame_vbo >> frame_bits;
2657	char *frame_ondisk = NULL;
2658	struct ATTR_LIST_ENTRY *le = NULL;
2659	char *frame_mem;
2660	struct ATTRIB *attr;
2661	struct mft_inode *mi;
2662	size_t compr_size, ondisk_size;
2663	struct lznt *lznt;
2664
2665	attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL, &mi);
2666	if (!attr) {
2667		err = -ENOENT;
2668		goto out;
2669	}
2670
2671	if (WARN_ON(!is_attr_compressed(attr))) {
2672		err = -EINVAL;
2673		goto out;
2674	}
2675
2676	if (sbi->cluster_size > NTFS_LZNT_MAX_CLUSTER) {
2677		err = -EOPNOTSUPP;
2678		goto out;
2679	}
2680
2681	if (!attr->non_res) {
2682		down_write(&ni->file.run_lock);
2683		err = attr_make_nonresident(ni, attr, le, mi,
2684					    le32_to_cpu(attr->res.data_size),
2685					    &ni->file.run, &attr, pages[0]);
2686		up_write(&ni->file.run_lock);
2687		if (err)
2688			goto out;
2689	}
2690
2691	if (attr->nres.c_unit != NTFS_LZNT_CUNIT) {
2692		err = -EOPNOTSUPP;
2693		goto out;
2694	}
2695
2696	/* Allocate memory to write compressed data to. */
2697	frame_ondisk = kvmalloc(frame_size, GFP_KERNEL);
2698	if (!frame_ondisk) {
2699		err = -ENOMEM;
2700		goto out;
2701	}
2702
2703	/* Map in-memory frame for read-only. */
2704	frame_mem = vmap(pages, pages_per_frame, VM_MAP, PAGE_KERNEL_RO);
2705	if (!frame_mem) {
2706		err = -ENOMEM;
2707		goto out1;
2708	}
2709
2710	mutex_lock(&sbi->compress.mtx_lznt);
2711	lznt = NULL;
2712	if (!sbi->compress.lznt) {
2713		/*
2714		 * LZNT implements two levels of compression:
2715		 * 0 - Standard compression
2716		 * 1 - Best compression, requires a lot of cpu
2717		 * use mount option?
2718		 */
2719		lznt = get_lznt_ctx(0);
2720		if (!lznt) {
2721			mutex_unlock(&sbi->compress.mtx_lznt);
2722			err = -ENOMEM;
2723			goto out2;
2724		}
2725
2726		sbi->compress.lznt = lznt;
2727		lznt = NULL;
2728	}
2729
2730	/* Compress: frame_mem -> frame_ondisk */
2731	compr_size = compress_lznt(frame_mem, frame_size, frame_ondisk,
2732				   frame_size, sbi->compress.lznt);
2733	mutex_unlock(&sbi->compress.mtx_lznt);
2734	kfree(lznt);
2735
2736	if (compr_size + sbi->cluster_size > frame_size) {
2737		/* Frame is not compressed. */
2738		compr_size = frame_size;
2739		ondisk_size = frame_size;
2740	} else if (compr_size) {
2741		/* Frame is compressed. */
2742		ondisk_size = ntfs_up_cluster(sbi, compr_size);
2743		memset(frame_ondisk + compr_size, 0, ondisk_size - compr_size);
2744	} else {
2745		/* Frame is sparsed. */
2746		ondisk_size = 0;
2747	}
2748
2749	down_write(&ni->file.run_lock);
2750	run_truncate_around(&ni->file.run, le64_to_cpu(attr->nres.svcn));
2751	err = attr_allocate_frame(ni, frame, compr_size, ni->i_valid);
2752	up_write(&ni->file.run_lock);
2753	if (err)
2754		goto out2;
2755
2756	if (!ondisk_size)
2757		goto out2;
2758
2759	down_read(&ni->file.run_lock);
2760	err = ntfs_write_run(sbi, &ni->file.run,
2761			     ondisk_size < frame_size ? frame_ondisk :
2762							frame_mem,
2763			     frame_vbo, ondisk_size);
2764	up_read(&ni->file.run_lock);
2765
2766out2:
2767	vunmap(frame_mem);
2768out1:
2769	kvfree(frame_ondisk);
2770out:
2771	return err;
2772}
2773
2774/*
2775 * ni_remove_name - Removes name 'de' from MFT and from directory.
2776 * 'de2' and 'undo_step' are used to restore MFT/dir, if error occurs.
2777 */
2778int ni_remove_name(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
2779		   struct NTFS_DE *de, struct NTFS_DE **de2, int *undo_step)
2780{
2781	int err;
2782	struct ntfs_sb_info *sbi = ni->mi.sbi;
2783	struct ATTR_FILE_NAME *de_name = (struct ATTR_FILE_NAME *)(de + 1);
2784	struct ATTR_FILE_NAME *fname;
2785	struct ATTR_LIST_ENTRY *le;
2786	struct mft_inode *mi;
2787	u16 de_key_size = le16_to_cpu(de->key_size);
2788	u8 name_type;
2789
2790	*undo_step = 0;
2791
2792	/* Find name in record. */
2793	mi_get_ref(&dir_ni->mi, &de_name->home);
2794
2795	fname = ni_fname_name(ni, (struct le_str *)&de_name->name_len,
2796			      &de_name->home, &mi, &le);
2797	if (!fname)
2798		return -ENOENT;
2799
2800	memcpy(&de_name->dup, &fname->dup, sizeof(struct NTFS_DUP_INFO));
2801	name_type = paired_name(fname->type);
2802
2803	/* Mark ntfs as dirty. It will be cleared at umount. */
2804	ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
2805
2806	/* Step 1: Remove name from directory. */
2807	err = indx_delete_entry(&dir_ni->dir, dir_ni, fname, de_key_size, sbi);
2808	if (err)
2809		return err;
2810
2811	/* Step 2: Remove name from MFT. */
2812	ni_remove_attr_le(ni, attr_from_name(fname), mi, le);
2813
2814	*undo_step = 2;
2815
2816	/* Get paired name. */
2817	fname = ni_fname_type(ni, name_type, &mi, &le);
2818	if (fname) {
2819		u16 de2_key_size = fname_full_size(fname);
2820
2821		*de2 = Add2Ptr(de, 1024);
2822		(*de2)->key_size = cpu_to_le16(de2_key_size);
2823
2824		memcpy(*de2 + 1, fname, de2_key_size);
2825
2826		/* Step 3: Remove paired name from directory. */
2827		err = indx_delete_entry(&dir_ni->dir, dir_ni, fname,
2828					de2_key_size, sbi);
2829		if (err)
2830			return err;
2831
2832		/* Step 4: Remove paired name from MFT. */
2833		ni_remove_attr_le(ni, attr_from_name(fname), mi, le);
2834
2835		*undo_step = 4;
2836	}
2837	return 0;
2838}
2839
2840/*
2841 * ni_remove_name_undo - Paired function for ni_remove_name.
2842 *
2843 * Return: True if ok
2844 */
2845bool ni_remove_name_undo(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
2846			 struct NTFS_DE *de, struct NTFS_DE *de2, int undo_step)
2847{
2848	struct ntfs_sb_info *sbi = ni->mi.sbi;
2849	struct ATTRIB *attr;
2850	u16 de_key_size;
2851
2852	switch (undo_step) {
2853	case 4:
2854		de_key_size = le16_to_cpu(de2->key_size);
2855		if (ni_insert_resident(ni, de_key_size, ATTR_NAME, NULL, 0,
2856				       &attr, NULL, NULL))
2857			return false;
2858		memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), de2 + 1, de_key_size);
2859
2860		mi_get_ref(&ni->mi, &de2->ref);
2861		de2->size = cpu_to_le16(ALIGN(de_key_size, 8) +
2862					sizeof(struct NTFS_DE));
2863		de2->flags = 0;
2864		de2->res = 0;
2865
2866		if (indx_insert_entry(&dir_ni->dir, dir_ni, de2, sbi, NULL, 1))
2867			return false;
2868		fallthrough;
2869
2870	case 2:
2871		de_key_size = le16_to_cpu(de->key_size);
2872
2873		if (ni_insert_resident(ni, de_key_size, ATTR_NAME, NULL, 0,
2874				       &attr, NULL, NULL))
2875			return false;
2876
2877		memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), de + 1, de_key_size);
2878		mi_get_ref(&ni->mi, &de->ref);
2879
2880		if (indx_insert_entry(&dir_ni->dir, dir_ni, de, sbi, NULL, 1))
2881			return false;
2882	}
2883
2884	return true;
2885}
2886
2887/*
2888 * ni_add_name - Add new name into MFT and into directory.
2889 */
2890int ni_add_name(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
2891		struct NTFS_DE *de)
2892{
2893	int err;
2894	struct ntfs_sb_info *sbi = ni->mi.sbi;
2895	struct ATTRIB *attr;
2896	struct ATTR_LIST_ENTRY *le;
2897	struct mft_inode *mi;
2898	struct ATTR_FILE_NAME *fname;
2899	struct ATTR_FILE_NAME *de_name = (struct ATTR_FILE_NAME *)(de + 1);
2900	u16 de_key_size = le16_to_cpu(de->key_size);
2901
2902	if (sbi->options->windows_names &&
2903	    !valid_windows_name(sbi, (struct le_str *)&de_name->name_len))
2904		return -EINVAL;
2905
2906	/* If option "hide_dot_files" then set hidden attribute for dot files. */
2907	if (ni->mi.sbi->options->hide_dot_files) {
2908		if (de_name->name_len > 0 &&
2909		    le16_to_cpu(de_name->name[0]) == '.')
2910			ni->std_fa |= FILE_ATTRIBUTE_HIDDEN;
2911		else
2912			ni->std_fa &= ~FILE_ATTRIBUTE_HIDDEN;
2913	}
2914
2915	mi_get_ref(&ni->mi, &de->ref);
2916	mi_get_ref(&dir_ni->mi, &de_name->home);
2917
2918	/* Fill duplicate from any ATTR_NAME. */
2919	fname = ni_fname_name(ni, NULL, NULL, NULL, NULL);
2920	if (fname)
2921		memcpy(&de_name->dup, &fname->dup, sizeof(fname->dup));
2922	de_name->dup.fa = ni->std_fa;
2923
2924	/* Insert new name into MFT. */
2925	err = ni_insert_resident(ni, de_key_size, ATTR_NAME, NULL, 0, &attr,
2926				 &mi, &le);
2927	if (err)
2928		return err;
2929
2930	memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), de_name, de_key_size);
2931
2932	/* Insert new name into directory. */
2933	err = indx_insert_entry(&dir_ni->dir, dir_ni, de, sbi, NULL, 0);
2934	if (err)
2935		ni_remove_attr_le(ni, attr, mi, le);
2936
2937	return err;
2938}
2939
2940/*
2941 * ni_rename - Remove one name and insert new name.
2942 */
2943int ni_rename(struct ntfs_inode *dir_ni, struct ntfs_inode *new_dir_ni,
2944	      struct ntfs_inode *ni, struct NTFS_DE *de, struct NTFS_DE *new_de)
2945{
2946	int err;
2947	struct NTFS_DE *de2 = NULL;
2948	int undo = 0;
2949
2950	/*
2951	 * There are two possible ways to rename:
2952	 * 1) Add new name and remove old name.
2953	 * 2) Remove old name and add new name.
2954	 *
2955	 * In most cases (not all!) adding new name into MFT and into directory can
2956	 * allocate additional cluster(s).
2957	 * Second way may result to bad inode if we can't add new name
2958	 * and then can't restore (add) old name.
2959	 */
2960
2961	/*
2962	 * Way 1 - Add new + remove old.
2963	 */
2964	err = ni_add_name(new_dir_ni, ni, new_de);
2965	if (!err) {
2966		err = ni_remove_name(dir_ni, ni, de, &de2, &undo);
2967		WARN_ON(err &&
2968			ni_remove_name(new_dir_ni, ni, new_de, &de2, &undo));
2969	}
2970
2971	/*
2972	 * Way 2 - Remove old + add new.
2973	 */
2974	/*
2975	 *	err = ni_remove_name(dir_ni, ni, de, &de2, &undo);
2976	 *	if (!err) {
2977	 *		err = ni_add_name(new_dir_ni, ni, new_de);
2978	 *		if (err && !ni_remove_name_undo(dir_ni, ni, de, de2, undo))
2979	 *			*is_bad = true;
2980	 *	}
2981	 */
2982
2983	return err;
2984}
2985
2986/*
2987 * ni_is_dirty - Return: True if 'ni' requires ni_write_inode.
2988 */
2989bool ni_is_dirty(struct inode *inode)
2990{
2991	struct ntfs_inode *ni = ntfs_i(inode);
2992	struct rb_node *node;
2993
2994	if (ni->mi.dirty || ni->attr_list.dirty ||
2995	    (ni->ni_flags & NI_FLAG_UPDATE_PARENT))
2996		return true;
2997
2998	for (node = rb_first(&ni->mi_tree); node; node = rb_next(node)) {
2999		if (rb_entry(node, struct mft_inode, node)->dirty)
3000			return true;
3001	}
3002
3003	return false;
3004}
3005
3006/*
3007 * ni_update_parent
3008 *
3009 * Update duplicate info of ATTR_FILE_NAME in MFT and in parent directories.
3010 */
3011static bool ni_update_parent(struct ntfs_inode *ni, struct NTFS_DUP_INFO *dup,
3012			     int sync)
3013{
3014	struct ATTRIB *attr;
3015	struct mft_inode *mi;
3016	struct ATTR_LIST_ENTRY *le = NULL;
3017	struct ntfs_sb_info *sbi = ni->mi.sbi;
3018	struct super_block *sb = sbi->sb;
3019	bool re_dirty = false;
3020
3021	if (ni->mi.mrec->flags & RECORD_FLAG_DIR) {
3022		dup->fa |= FILE_ATTRIBUTE_DIRECTORY;
3023		attr = NULL;
3024		dup->alloc_size = 0;
3025		dup->data_size = 0;
3026	} else {
3027		dup->fa &= ~FILE_ATTRIBUTE_DIRECTORY;
3028
3029		attr = ni_find_attr(ni, NULL, &le, ATTR_DATA, NULL, 0, NULL,
3030				    &mi);
3031		if (!attr) {
3032			dup->alloc_size = dup->data_size = 0;
3033		} else if (!attr->non_res) {
3034			u32 data_size = le32_to_cpu(attr->res.data_size);
3035
3036			dup->alloc_size = cpu_to_le64(ALIGN(data_size, 8));
3037			dup->data_size = cpu_to_le64(data_size);
3038		} else {
3039			u64 new_valid = ni->i_valid;
3040			u64 data_size = le64_to_cpu(attr->nres.data_size);
3041			__le64 valid_le;
3042
3043			dup->alloc_size = is_attr_ext(attr) ?
3044						  attr->nres.total_size :
3045						  attr->nres.alloc_size;
3046			dup->data_size = attr->nres.data_size;
3047
3048			if (new_valid > data_size)
3049				new_valid = data_size;
3050
3051			valid_le = cpu_to_le64(new_valid);
3052			if (valid_le != attr->nres.valid_size) {
3053				attr->nres.valid_size = valid_le;
3054				mi->dirty = true;
3055			}
3056		}
3057	}
3058
3059	dup->extend_data = 0;
3060
3061	if (dup->fa & FILE_ATTRIBUTE_REPARSE_POINT) {
3062		attr = ni_find_attr(ni, NULL, NULL, ATTR_REPARSE, NULL, 0, NULL,
3063				    NULL);
3064
3065		if (attr) {
3066			const struct REPARSE_POINT *rp;
3067
3068			rp = resident_data_ex(attr,
3069					      sizeof(struct REPARSE_POINT));
3070			/* If ATTR_REPARSE exists 'rp' can't be NULL. */
3071			if (rp)
3072				dup->extend_data = rp->ReparseTag;
3073		}
3074	} else if (ni->ni_flags & NI_FLAG_EA) {
3075		attr = ni_find_attr(ni, attr, &le, ATTR_EA_INFO, NULL, 0, NULL,
3076				    NULL);
3077		if (attr) {
3078			const struct EA_INFO *info;
3079
3080			info = resident_data_ex(attr, sizeof(struct EA_INFO));
3081			/* If ATTR_EA_INFO exists 'info' can't be NULL. */
3082			if (info)
3083				dup->extend_data = info->size;
3084		}
3085	}
3086
3087	attr = NULL;
3088	le = NULL;
3089
3090	while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL,
3091				    &mi))) {
3092		struct inode *dir;
3093		struct ATTR_FILE_NAME *fname;
3094
3095		fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
3096		if (!fname || !memcmp(&fname->dup, dup, sizeof(fname->dup)))
3097			continue;
3098
3099		/* Check simple case when parent inode equals current inode. */
3100		if (ino_get(&fname->home) == ni->vfs_inode.i_ino) {
3101			ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
3102			continue;
3103		}
3104
3105		/* ntfs_iget5 may sleep. */
3106		dir = ntfs_iget5(sb, &fname->home, NULL);
3107		if (IS_ERR(dir)) {
3108			ntfs_inode_warn(
3109				&ni->vfs_inode,
3110				"failed to open parent directory r=%lx to update",
3111				(long)ino_get(&fname->home));
3112			continue;
3113		}
3114
3115		if (!is_bad_inode(dir)) {
3116			struct ntfs_inode *dir_ni = ntfs_i(dir);
3117
3118			if (!ni_trylock(dir_ni)) {
3119				re_dirty = true;
3120			} else {
3121				indx_update_dup(dir_ni, sbi, fname, dup, sync);
3122				ni_unlock(dir_ni);
3123				memcpy(&fname->dup, dup, sizeof(fname->dup));
3124				mi->dirty = true;
3125			}
3126		}
3127		iput(dir);
3128	}
3129
3130	return re_dirty;
3131}
3132
3133/*
3134 * ni_write_inode - Write MFT base record and all subrecords to disk.
3135 */
3136int ni_write_inode(struct inode *inode, int sync, const char *hint)
3137{
3138	int err = 0, err2;
3139	struct ntfs_inode *ni = ntfs_i(inode);
3140	struct super_block *sb = inode->i_sb;
3141	struct ntfs_sb_info *sbi = sb->s_fs_info;
3142	bool re_dirty = false;
3143	struct ATTR_STD_INFO *std;
3144	struct rb_node *node, *next;
3145	struct NTFS_DUP_INFO dup;
3146
3147	if (is_bad_inode(inode) || sb_rdonly(sb))
3148		return 0;
3149
3150	/* Avoid any operation if inode is bad. */
3151	if (unlikely(is_bad_ni(ni)))
3152		return -EINVAL;
3153
3154	if (unlikely(ntfs3_forced_shutdown(sb)))
3155		return -EIO;
3156
3157	if (!ni_trylock(ni)) {
3158		/* 'ni' is under modification, skip for now. */
3159		mark_inode_dirty_sync(inode);
3160		return 0;
3161	}
3162
3163	if (!ni->mi.mrec)
3164		goto out;
3165
3166	if (is_rec_inuse(ni->mi.mrec) &&
3167	    !(sbi->flags & NTFS_FLAGS_LOG_REPLAYING) && inode->i_nlink) {
3168		bool modified = false;
3169		struct timespec64 ts;
3170
3171		/* Update times in standard attribute. */
3172		std = ni_std(ni);
3173		if (!std) {
3174			err = -EINVAL;
3175			goto out;
3176		}
3177
3178		/* Update the access times if they have changed. */
3179		ts = inode_get_mtime(inode);
3180		dup.m_time = kernel2nt(&ts);
3181		if (std->m_time != dup.m_time) {
3182			std->m_time = dup.m_time;
3183			modified = true;
3184		}
3185
3186		ts = inode_get_ctime(inode);
3187		dup.c_time = kernel2nt(&ts);
3188		if (std->c_time != dup.c_time) {
3189			std->c_time = dup.c_time;
3190			modified = true;
3191		}
3192
3193		ts = inode_get_atime(inode);
3194		dup.a_time = kernel2nt(&ts);
3195		if (std->a_time != dup.a_time) {
3196			std->a_time = dup.a_time;
3197			modified = true;
3198		}
3199
3200		dup.fa = ni->std_fa;
3201		if (std->fa != dup.fa) {
3202			std->fa = dup.fa;
3203			modified = true;
3204		}
3205
3206		/* std attribute is always in primary MFT record. */
3207		if (modified)
3208			ni->mi.dirty = true;
3209
3210		if (!ntfs_is_meta_file(sbi, inode->i_ino) &&
3211		    (modified || (ni->ni_flags & NI_FLAG_UPDATE_PARENT))
3212		    /* Avoid __wait_on_freeing_inode(inode). */
3213		    && (sb->s_flags & SB_ACTIVE)) {
3214			dup.cr_time = std->cr_time;
3215			/* Not critical if this function fail. */
3216			re_dirty = ni_update_parent(ni, &dup, sync);
3217
3218			if (re_dirty)
3219				ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
3220			else
3221				ni->ni_flags &= ~NI_FLAG_UPDATE_PARENT;
3222		}
3223
3224		/* Update attribute list. */
3225		if (ni->attr_list.size && ni->attr_list.dirty) {
3226			if (inode->i_ino != MFT_REC_MFT || sync) {
3227				err = ni_try_remove_attr_list(ni);
3228				if (err)
3229					goto out;
3230			}
3231
3232			err = al_update(ni, sync);
3233			if (err)
3234				goto out;
3235		}
3236	}
3237
3238	for (node = rb_first(&ni->mi_tree); node; node = next) {
3239		struct mft_inode *mi = rb_entry(node, struct mft_inode, node);
3240		bool is_empty;
3241
3242		next = rb_next(node);
3243
3244		if (!mi->dirty)
3245			continue;
3246
3247		is_empty = !mi_enum_attr(ni, mi, NULL);
3248
3249		if (is_empty)
3250			clear_rec_inuse(mi->mrec);
3251
3252		err2 = mi_write(mi, sync);
3253		if (!err && err2)
3254			err = err2;
3255
3256		if (is_empty) {
3257			ntfs_mark_rec_free(sbi, mi->rno, false);
3258			rb_erase(node, &ni->mi_tree);
3259			mi_put(mi);
3260		}
3261	}
3262
3263	if (ni->mi.dirty) {
3264		err2 = mi_write(&ni->mi, sync);
3265		if (!err && err2)
3266			err = err2;
3267	}
3268out:
3269	ni_unlock(ni);
3270
3271	if (err) {
3272		ntfs_inode_err(inode, "%s failed, %d.", hint, err);
3273		ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
3274		return err;
3275	}
3276
3277	if (re_dirty)
3278		mark_inode_dirty_sync(inode);
3279
3280	return 0;
3281}
~/ Read-only file preview.