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