xref: /openbmc/linux/fs/ntfs/attrib.c (revision 384740dc)
1 /**
2  * attrib.c - NTFS attribute operations.  Part of the Linux-NTFS project.
3  *
4  * Copyright (c) 2001-2007 Anton Altaparmakov
5  * Copyright (c) 2002 Richard Russon
6  *
7  * This program/include file is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as published
9  * by the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program/include file is distributed in the hope that it will be
13  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program (in the main directory of the Linux-NTFS
19  * distribution in the file COPYING); if not, write to the Free Software
20  * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22 
23 #include <linux/buffer_head.h>
24 #include <linux/sched.h>
25 #include <linux/swap.h>
26 #include <linux/writeback.h>
27 
28 #include "attrib.h"
29 #include "debug.h"
30 #include "layout.h"
31 #include "lcnalloc.h"
32 #include "malloc.h"
33 #include "mft.h"
34 #include "ntfs.h"
35 #include "types.h"
36 
37 /**
38  * ntfs_map_runlist_nolock - map (a part of) a runlist of an ntfs inode
39  * @ni:		ntfs inode for which to map (part of) a runlist
40  * @vcn:	map runlist part containing this vcn
41  * @ctx:	active attribute search context if present or NULL if not
42  *
43  * Map the part of a runlist containing the @vcn of the ntfs inode @ni.
44  *
45  * If @ctx is specified, it is an active search context of @ni and its base mft
46  * record.  This is needed when ntfs_map_runlist_nolock() encounters unmapped
47  * runlist fragments and allows their mapping.  If you do not have the mft
48  * record mapped, you can specify @ctx as NULL and ntfs_map_runlist_nolock()
49  * will perform the necessary mapping and unmapping.
50  *
51  * Note, ntfs_map_runlist_nolock() saves the state of @ctx on entry and
52  * restores it before returning.  Thus, @ctx will be left pointing to the same
53  * attribute on return as on entry.  However, the actual pointers in @ctx may
54  * point to different memory locations on return, so you must remember to reset
55  * any cached pointers from the @ctx, i.e. after the call to
56  * ntfs_map_runlist_nolock(), you will probably want to do:
57  *	m = ctx->mrec;
58  *	a = ctx->attr;
59  * Assuming you cache ctx->attr in a variable @a of type ATTR_RECORD * and that
60  * you cache ctx->mrec in a variable @m of type MFT_RECORD *.
61  *
62  * Return 0 on success and -errno on error.  There is one special error code
63  * which is not an error as such.  This is -ENOENT.  It means that @vcn is out
64  * of bounds of the runlist.
65  *
66  * Note the runlist can be NULL after this function returns if @vcn is zero and
67  * the attribute has zero allocated size, i.e. there simply is no runlist.
68  *
69  * WARNING: If @ctx is supplied, regardless of whether success or failure is
70  *	    returned, you need to check IS_ERR(@ctx->mrec) and if 'true' the @ctx
71  *	    is no longer valid, i.e. you need to either call
72  *	    ntfs_attr_reinit_search_ctx() or ntfs_attr_put_search_ctx() on it.
73  *	    In that case PTR_ERR(@ctx->mrec) will give you the error code for
74  *	    why the mapping of the old inode failed.
75  *
76  * Locking: - The runlist described by @ni must be locked for writing on entry
77  *	      and is locked on return.  Note the runlist will be modified.
78  *	    - If @ctx is NULL, the base mft record of @ni must not be mapped on
79  *	      entry and it will be left unmapped on return.
80  *	    - If @ctx is not NULL, the base mft record must be mapped on entry
81  *	      and it will be left mapped on return.
82  */
83 int ntfs_map_runlist_nolock(ntfs_inode *ni, VCN vcn, ntfs_attr_search_ctx *ctx)
84 {
85 	VCN end_vcn;
86 	unsigned long flags;
87 	ntfs_inode *base_ni;
88 	MFT_RECORD *m;
89 	ATTR_RECORD *a;
90 	runlist_element *rl;
91 	struct page *put_this_page = NULL;
92 	int err = 0;
93 	bool ctx_is_temporary, ctx_needs_reset;
94 	ntfs_attr_search_ctx old_ctx = { NULL, };
95 
96 	ntfs_debug("Mapping runlist part containing vcn 0x%llx.",
97 			(unsigned long long)vcn);
98 	if (!NInoAttr(ni))
99 		base_ni = ni;
100 	else
101 		base_ni = ni->ext.base_ntfs_ino;
102 	if (!ctx) {
103 		ctx_is_temporary = ctx_needs_reset = true;
104 		m = map_mft_record(base_ni);
105 		if (IS_ERR(m))
106 			return PTR_ERR(m);
107 		ctx = ntfs_attr_get_search_ctx(base_ni, m);
108 		if (unlikely(!ctx)) {
109 			err = -ENOMEM;
110 			goto err_out;
111 		}
112 	} else {
113 		VCN allocated_size_vcn;
114 
115 		BUG_ON(IS_ERR(ctx->mrec));
116 		a = ctx->attr;
117 		BUG_ON(!a->non_resident);
118 		ctx_is_temporary = false;
119 		end_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn);
120 		read_lock_irqsave(&ni->size_lock, flags);
121 		allocated_size_vcn = ni->allocated_size >>
122 				ni->vol->cluster_size_bits;
123 		read_unlock_irqrestore(&ni->size_lock, flags);
124 		if (!a->data.non_resident.lowest_vcn && end_vcn <= 0)
125 			end_vcn = allocated_size_vcn - 1;
126 		/*
127 		 * If we already have the attribute extent containing @vcn in
128 		 * @ctx, no need to look it up again.  We slightly cheat in
129 		 * that if vcn exceeds the allocated size, we will refuse to
130 		 * map the runlist below, so there is definitely no need to get
131 		 * the right attribute extent.
132 		 */
133 		if (vcn >= allocated_size_vcn || (a->type == ni->type &&
134 				a->name_length == ni->name_len &&
135 				!memcmp((u8*)a + le16_to_cpu(a->name_offset),
136 				ni->name, ni->name_len) &&
137 				sle64_to_cpu(a->data.non_resident.lowest_vcn)
138 				<= vcn && end_vcn >= vcn))
139 			ctx_needs_reset = false;
140 		else {
141 			/* Save the old search context. */
142 			old_ctx = *ctx;
143 			/*
144 			 * If the currently mapped (extent) inode is not the
145 			 * base inode we will unmap it when we reinitialize the
146 			 * search context which means we need to get a
147 			 * reference to the page containing the mapped mft
148 			 * record so we do not accidentally drop changes to the
149 			 * mft record when it has not been marked dirty yet.
150 			 */
151 			if (old_ctx.base_ntfs_ino && old_ctx.ntfs_ino !=
152 					old_ctx.base_ntfs_ino) {
153 				put_this_page = old_ctx.ntfs_ino->page;
154 				page_cache_get(put_this_page);
155 			}
156 			/*
157 			 * Reinitialize the search context so we can lookup the
158 			 * needed attribute extent.
159 			 */
160 			ntfs_attr_reinit_search_ctx(ctx);
161 			ctx_needs_reset = true;
162 		}
163 	}
164 	if (ctx_needs_reset) {
165 		err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
166 				CASE_SENSITIVE, vcn, NULL, 0, ctx);
167 		if (unlikely(err)) {
168 			if (err == -ENOENT)
169 				err = -EIO;
170 			goto err_out;
171 		}
172 		BUG_ON(!ctx->attr->non_resident);
173 	}
174 	a = ctx->attr;
175 	/*
176 	 * Only decompress the mapping pairs if @vcn is inside it.  Otherwise
177 	 * we get into problems when we try to map an out of bounds vcn because
178 	 * we then try to map the already mapped runlist fragment and
179 	 * ntfs_mapping_pairs_decompress() fails.
180 	 */
181 	end_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn) + 1;
182 	if (unlikely(vcn && vcn >= end_vcn)) {
183 		err = -ENOENT;
184 		goto err_out;
185 	}
186 	rl = ntfs_mapping_pairs_decompress(ni->vol, a, ni->runlist.rl);
187 	if (IS_ERR(rl))
188 		err = PTR_ERR(rl);
189 	else
190 		ni->runlist.rl = rl;
191 err_out:
192 	if (ctx_is_temporary) {
193 		if (likely(ctx))
194 			ntfs_attr_put_search_ctx(ctx);
195 		unmap_mft_record(base_ni);
196 	} else if (ctx_needs_reset) {
197 		/*
198 		 * If there is no attribute list, restoring the search context
199 		 * is acomplished simply by copying the saved context back over
200 		 * the caller supplied context.  If there is an attribute list,
201 		 * things are more complicated as we need to deal with mapping
202 		 * of mft records and resulting potential changes in pointers.
203 		 */
204 		if (NInoAttrList(base_ni)) {
205 			/*
206 			 * If the currently mapped (extent) inode is not the
207 			 * one we had before, we need to unmap it and map the
208 			 * old one.
209 			 */
210 			if (ctx->ntfs_ino != old_ctx.ntfs_ino) {
211 				/*
212 				 * If the currently mapped inode is not the
213 				 * base inode, unmap it.
214 				 */
215 				if (ctx->base_ntfs_ino && ctx->ntfs_ino !=
216 						ctx->base_ntfs_ino) {
217 					unmap_extent_mft_record(ctx->ntfs_ino);
218 					ctx->mrec = ctx->base_mrec;
219 					BUG_ON(!ctx->mrec);
220 				}
221 				/*
222 				 * If the old mapped inode is not the base
223 				 * inode, map it.
224 				 */
225 				if (old_ctx.base_ntfs_ino &&
226 						old_ctx.ntfs_ino !=
227 						old_ctx.base_ntfs_ino) {
228 retry_map:
229 					ctx->mrec = map_mft_record(
230 							old_ctx.ntfs_ino);
231 					/*
232 					 * Something bad has happened.  If out
233 					 * of memory retry till it succeeds.
234 					 * Any other errors are fatal and we
235 					 * return the error code in ctx->mrec.
236 					 * Let the caller deal with it...  We
237 					 * just need to fudge things so the
238 					 * caller can reinit and/or put the
239 					 * search context safely.
240 					 */
241 					if (IS_ERR(ctx->mrec)) {
242 						if (PTR_ERR(ctx->mrec) ==
243 								-ENOMEM) {
244 							schedule();
245 							goto retry_map;
246 						} else
247 							old_ctx.ntfs_ino =
248 								old_ctx.
249 								base_ntfs_ino;
250 					}
251 				}
252 			}
253 			/* Update the changed pointers in the saved context. */
254 			if (ctx->mrec != old_ctx.mrec) {
255 				if (!IS_ERR(ctx->mrec))
256 					old_ctx.attr = (ATTR_RECORD*)(
257 							(u8*)ctx->mrec +
258 							((u8*)old_ctx.attr -
259 							(u8*)old_ctx.mrec));
260 				old_ctx.mrec = ctx->mrec;
261 			}
262 		}
263 		/* Restore the search context to the saved one. */
264 		*ctx = old_ctx;
265 		/*
266 		 * We drop the reference on the page we took earlier.  In the
267 		 * case that IS_ERR(ctx->mrec) is true this means we might lose
268 		 * some changes to the mft record that had been made between
269 		 * the last time it was marked dirty/written out and now.  This
270 		 * at this stage is not a problem as the mapping error is fatal
271 		 * enough that the mft record cannot be written out anyway and
272 		 * the caller is very likely to shutdown the whole inode
273 		 * immediately and mark the volume dirty for chkdsk to pick up
274 		 * the pieces anyway.
275 		 */
276 		if (put_this_page)
277 			page_cache_release(put_this_page);
278 	}
279 	return err;
280 }
281 
282 /**
283  * ntfs_map_runlist - map (a part of) a runlist of an ntfs inode
284  * @ni:		ntfs inode for which to map (part of) a runlist
285  * @vcn:	map runlist part containing this vcn
286  *
287  * Map the part of a runlist containing the @vcn of the ntfs inode @ni.
288  *
289  * Return 0 on success and -errno on error.  There is one special error code
290  * which is not an error as such.  This is -ENOENT.  It means that @vcn is out
291  * of bounds of the runlist.
292  *
293  * Locking: - The runlist must be unlocked on entry and is unlocked on return.
294  *	    - This function takes the runlist lock for writing and may modify
295  *	      the runlist.
296  */
297 int ntfs_map_runlist(ntfs_inode *ni, VCN vcn)
298 {
299 	int err = 0;
300 
301 	down_write(&ni->runlist.lock);
302 	/* Make sure someone else didn't do the work while we were sleeping. */
303 	if (likely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) <=
304 			LCN_RL_NOT_MAPPED))
305 		err = ntfs_map_runlist_nolock(ni, vcn, NULL);
306 	up_write(&ni->runlist.lock);
307 	return err;
308 }
309 
310 /**
311  * ntfs_attr_vcn_to_lcn_nolock - convert a vcn into a lcn given an ntfs inode
312  * @ni:			ntfs inode of the attribute whose runlist to search
313  * @vcn:		vcn to convert
314  * @write_locked:	true if the runlist is locked for writing
315  *
316  * Find the virtual cluster number @vcn in the runlist of the ntfs attribute
317  * described by the ntfs inode @ni and return the corresponding logical cluster
318  * number (lcn).
319  *
320  * If the @vcn is not mapped yet, the attempt is made to map the attribute
321  * extent containing the @vcn and the vcn to lcn conversion is retried.
322  *
323  * If @write_locked is true the caller has locked the runlist for writing and
324  * if false for reading.
325  *
326  * Since lcns must be >= 0, we use negative return codes with special meaning:
327  *
328  * Return code	Meaning / Description
329  * ==========================================
330  *  LCN_HOLE	Hole / not allocated on disk.
331  *  LCN_ENOENT	There is no such vcn in the runlist, i.e. @vcn is out of bounds.
332  *  LCN_ENOMEM	Not enough memory to map runlist.
333  *  LCN_EIO	Critical error (runlist/file is corrupt, i/o error, etc).
334  *
335  * Locking: - The runlist must be locked on entry and is left locked on return.
336  *	    - If @write_locked is 'false', i.e. the runlist is locked for reading,
337  *	      the lock may be dropped inside the function so you cannot rely on
338  *	      the runlist still being the same when this function returns.
339  */
340 LCN ntfs_attr_vcn_to_lcn_nolock(ntfs_inode *ni, const VCN vcn,
341 		const bool write_locked)
342 {
343 	LCN lcn;
344 	unsigned long flags;
345 	bool is_retry = false;
346 
347 	ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, %s_locked.",
348 			ni->mft_no, (unsigned long long)vcn,
349 			write_locked ? "write" : "read");
350 	BUG_ON(!ni);
351 	BUG_ON(!NInoNonResident(ni));
352 	BUG_ON(vcn < 0);
353 	if (!ni->runlist.rl) {
354 		read_lock_irqsave(&ni->size_lock, flags);
355 		if (!ni->allocated_size) {
356 			read_unlock_irqrestore(&ni->size_lock, flags);
357 			return LCN_ENOENT;
358 		}
359 		read_unlock_irqrestore(&ni->size_lock, flags);
360 	}
361 retry_remap:
362 	/* Convert vcn to lcn.  If that fails map the runlist and retry once. */
363 	lcn = ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn);
364 	if (likely(lcn >= LCN_HOLE)) {
365 		ntfs_debug("Done, lcn 0x%llx.", (long long)lcn);
366 		return lcn;
367 	}
368 	if (lcn != LCN_RL_NOT_MAPPED) {
369 		if (lcn != LCN_ENOENT)
370 			lcn = LCN_EIO;
371 	} else if (!is_retry) {
372 		int err;
373 
374 		if (!write_locked) {
375 			up_read(&ni->runlist.lock);
376 			down_write(&ni->runlist.lock);
377 			if (unlikely(ntfs_rl_vcn_to_lcn(ni->runlist.rl, vcn) !=
378 					LCN_RL_NOT_MAPPED)) {
379 				up_write(&ni->runlist.lock);
380 				down_read(&ni->runlist.lock);
381 				goto retry_remap;
382 			}
383 		}
384 		err = ntfs_map_runlist_nolock(ni, vcn, NULL);
385 		if (!write_locked) {
386 			up_write(&ni->runlist.lock);
387 			down_read(&ni->runlist.lock);
388 		}
389 		if (likely(!err)) {
390 			is_retry = true;
391 			goto retry_remap;
392 		}
393 		if (err == -ENOENT)
394 			lcn = LCN_ENOENT;
395 		else if (err == -ENOMEM)
396 			lcn = LCN_ENOMEM;
397 		else
398 			lcn = LCN_EIO;
399 	}
400 	if (lcn != LCN_ENOENT)
401 		ntfs_error(ni->vol->sb, "Failed with error code %lli.",
402 				(long long)lcn);
403 	return lcn;
404 }
405 
406 /**
407  * ntfs_attr_find_vcn_nolock - find a vcn in the runlist of an ntfs inode
408  * @ni:		ntfs inode describing the runlist to search
409  * @vcn:	vcn to find
410  * @ctx:	active attribute search context if present or NULL if not
411  *
412  * Find the virtual cluster number @vcn in the runlist described by the ntfs
413  * inode @ni and return the address of the runlist element containing the @vcn.
414  *
415  * If the @vcn is not mapped yet, the attempt is made to map the attribute
416  * extent containing the @vcn and the vcn to lcn conversion is retried.
417  *
418  * If @ctx is specified, it is an active search context of @ni and its base mft
419  * record.  This is needed when ntfs_attr_find_vcn_nolock() encounters unmapped
420  * runlist fragments and allows their mapping.  If you do not have the mft
421  * record mapped, you can specify @ctx as NULL and ntfs_attr_find_vcn_nolock()
422  * will perform the necessary mapping and unmapping.
423  *
424  * Note, ntfs_attr_find_vcn_nolock() saves the state of @ctx on entry and
425  * restores it before returning.  Thus, @ctx will be left pointing to the same
426  * attribute on return as on entry.  However, the actual pointers in @ctx may
427  * point to different memory locations on return, so you must remember to reset
428  * any cached pointers from the @ctx, i.e. after the call to
429  * ntfs_attr_find_vcn_nolock(), you will probably want to do:
430  *	m = ctx->mrec;
431  *	a = ctx->attr;
432  * Assuming you cache ctx->attr in a variable @a of type ATTR_RECORD * and that
433  * you cache ctx->mrec in a variable @m of type MFT_RECORD *.
434  * Note you need to distinguish between the lcn of the returned runlist element
435  * being >= 0 and LCN_HOLE.  In the later case you have to return zeroes on
436  * read and allocate clusters on write.
437  *
438  * Return the runlist element containing the @vcn on success and
439  * ERR_PTR(-errno) on error.  You need to test the return value with IS_ERR()
440  * to decide if the return is success or failure and PTR_ERR() to get to the
441  * error code if IS_ERR() is true.
442  *
443  * The possible error return codes are:
444  *	-ENOENT - No such vcn in the runlist, i.e. @vcn is out of bounds.
445  *	-ENOMEM - Not enough memory to map runlist.
446  *	-EIO	- Critical error (runlist/file is corrupt, i/o error, etc).
447  *
448  * WARNING: If @ctx is supplied, regardless of whether success or failure is
449  *	    returned, you need to check IS_ERR(@ctx->mrec) and if 'true' the @ctx
450  *	    is no longer valid, i.e. you need to either call
451  *	    ntfs_attr_reinit_search_ctx() or ntfs_attr_put_search_ctx() on it.
452  *	    In that case PTR_ERR(@ctx->mrec) will give you the error code for
453  *	    why the mapping of the old inode failed.
454  *
455  * Locking: - The runlist described by @ni must be locked for writing on entry
456  *	      and is locked on return.  Note the runlist may be modified when
457  *	      needed runlist fragments need to be mapped.
458  *	    - If @ctx is NULL, the base mft record of @ni must not be mapped on
459  *	      entry and it will be left unmapped on return.
460  *	    - If @ctx is not NULL, the base mft record must be mapped on entry
461  *	      and it will be left mapped on return.
462  */
463 runlist_element *ntfs_attr_find_vcn_nolock(ntfs_inode *ni, const VCN vcn,
464 		ntfs_attr_search_ctx *ctx)
465 {
466 	unsigned long flags;
467 	runlist_element *rl;
468 	int err = 0;
469 	bool is_retry = false;
470 
471 	ntfs_debug("Entering for i_ino 0x%lx, vcn 0x%llx, with%s ctx.",
472 			ni->mft_no, (unsigned long long)vcn, ctx ? "" : "out");
473 	BUG_ON(!ni);
474 	BUG_ON(!NInoNonResident(ni));
475 	BUG_ON(vcn < 0);
476 	if (!ni->runlist.rl) {
477 		read_lock_irqsave(&ni->size_lock, flags);
478 		if (!ni->allocated_size) {
479 			read_unlock_irqrestore(&ni->size_lock, flags);
480 			return ERR_PTR(-ENOENT);
481 		}
482 		read_unlock_irqrestore(&ni->size_lock, flags);
483 	}
484 retry_remap:
485 	rl = ni->runlist.rl;
486 	if (likely(rl && vcn >= rl[0].vcn)) {
487 		while (likely(rl->length)) {
488 			if (unlikely(vcn < rl[1].vcn)) {
489 				if (likely(rl->lcn >= LCN_HOLE)) {
490 					ntfs_debug("Done.");
491 					return rl;
492 				}
493 				break;
494 			}
495 			rl++;
496 		}
497 		if (likely(rl->lcn != LCN_RL_NOT_MAPPED)) {
498 			if (likely(rl->lcn == LCN_ENOENT))
499 				err = -ENOENT;
500 			else
501 				err = -EIO;
502 		}
503 	}
504 	if (!err && !is_retry) {
505 		/*
506 		 * If the search context is invalid we cannot map the unmapped
507 		 * region.
508 		 */
509 		if (IS_ERR(ctx->mrec))
510 			err = PTR_ERR(ctx->mrec);
511 		else {
512 			/*
513 			 * The @vcn is in an unmapped region, map the runlist
514 			 * and retry.
515 			 */
516 			err = ntfs_map_runlist_nolock(ni, vcn, ctx);
517 			if (likely(!err)) {
518 				is_retry = true;
519 				goto retry_remap;
520 			}
521 		}
522 		if (err == -EINVAL)
523 			err = -EIO;
524 	} else if (!err)
525 		err = -EIO;
526 	if (err != -ENOENT)
527 		ntfs_error(ni->vol->sb, "Failed with error code %i.", err);
528 	return ERR_PTR(err);
529 }
530 
531 /**
532  * ntfs_attr_find - find (next) attribute in mft record
533  * @type:	attribute type to find
534  * @name:	attribute name to find (optional, i.e. NULL means don't care)
535  * @name_len:	attribute name length (only needed if @name present)
536  * @ic:		IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
537  * @val:	attribute value to find (optional, resident attributes only)
538  * @val_len:	attribute value length
539  * @ctx:	search context with mft record and attribute to search from
540  *
541  * You should not need to call this function directly.  Use ntfs_attr_lookup()
542  * instead.
543  *
544  * ntfs_attr_find() takes a search context @ctx as parameter and searches the
545  * mft record specified by @ctx->mrec, beginning at @ctx->attr, for an
546  * attribute of @type, optionally @name and @val.
547  *
548  * If the attribute is found, ntfs_attr_find() returns 0 and @ctx->attr will
549  * point to the found attribute.
550  *
551  * If the attribute is not found, ntfs_attr_find() returns -ENOENT and
552  * @ctx->attr will point to the attribute before which the attribute being
553  * searched for would need to be inserted if such an action were to be desired.
554  *
555  * On actual error, ntfs_attr_find() returns -EIO.  In this case @ctx->attr is
556  * undefined and in particular do not rely on it not changing.
557  *
558  * If @ctx->is_first is 'true', the search begins with @ctx->attr itself.  If it
559  * is 'false', the search begins after @ctx->attr.
560  *
561  * If @ic is IGNORE_CASE, the @name comparisson is not case sensitive and
562  * @ctx->ntfs_ino must be set to the ntfs inode to which the mft record
563  * @ctx->mrec belongs.  This is so we can get at the ntfs volume and hence at
564  * the upcase table.  If @ic is CASE_SENSITIVE, the comparison is case
565  * sensitive.  When @name is present, @name_len is the @name length in Unicode
566  * characters.
567  *
568  * If @name is not present (NULL), we assume that the unnamed attribute is
569  * being searched for.
570  *
571  * Finally, the resident attribute value @val is looked for, if present.  If
572  * @val is not present (NULL), @val_len is ignored.
573  *
574  * ntfs_attr_find() only searches the specified mft record and it ignores the
575  * presence of an attribute list attribute (unless it is the one being searched
576  * for, obviously).  If you need to take attribute lists into consideration,
577  * use ntfs_attr_lookup() instead (see below).  This also means that you cannot
578  * use ntfs_attr_find() to search for extent records of non-resident
579  * attributes, as extents with lowest_vcn != 0 are usually described by the
580  * attribute list attribute only. - Note that it is possible that the first
581  * extent is only in the attribute list while the last extent is in the base
582  * mft record, so do not rely on being able to find the first extent in the
583  * base mft record.
584  *
585  * Warning: Never use @val when looking for attribute types which can be
586  *	    non-resident as this most likely will result in a crash!
587  */
588 static int ntfs_attr_find(const ATTR_TYPE type, const ntfschar *name,
589 		const u32 name_len, const IGNORE_CASE_BOOL ic,
590 		const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
591 {
592 	ATTR_RECORD *a;
593 	ntfs_volume *vol = ctx->ntfs_ino->vol;
594 	ntfschar *upcase = vol->upcase;
595 	u32 upcase_len = vol->upcase_len;
596 
597 	/*
598 	 * Iterate over attributes in mft record starting at @ctx->attr, or the
599 	 * attribute following that, if @ctx->is_first is 'true'.
600 	 */
601 	if (ctx->is_first) {
602 		a = ctx->attr;
603 		ctx->is_first = false;
604 	} else
605 		a = (ATTR_RECORD*)((u8*)ctx->attr +
606 				le32_to_cpu(ctx->attr->length));
607 	for (;;	a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length))) {
608 		if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
609 				le32_to_cpu(ctx->mrec->bytes_allocated))
610 			break;
611 		ctx->attr = a;
612 		if (unlikely(le32_to_cpu(a->type) > le32_to_cpu(type) ||
613 				a->type == AT_END))
614 			return -ENOENT;
615 		if (unlikely(!a->length))
616 			break;
617 		if (a->type != type)
618 			continue;
619 		/*
620 		 * If @name is present, compare the two names.  If @name is
621 		 * missing, assume we want an unnamed attribute.
622 		 */
623 		if (!name) {
624 			/* The search failed if the found attribute is named. */
625 			if (a->name_length)
626 				return -ENOENT;
627 		} else if (!ntfs_are_names_equal(name, name_len,
628 			    (ntfschar*)((u8*)a + le16_to_cpu(a->name_offset)),
629 			    a->name_length, ic, upcase, upcase_len)) {
630 			register int rc;
631 
632 			rc = ntfs_collate_names(name, name_len,
633 					(ntfschar*)((u8*)a +
634 					le16_to_cpu(a->name_offset)),
635 					a->name_length, 1, IGNORE_CASE,
636 					upcase, upcase_len);
637 			/*
638 			 * If @name collates before a->name, there is no
639 			 * matching attribute.
640 			 */
641 			if (rc == -1)
642 				return -ENOENT;
643 			/* If the strings are not equal, continue search. */
644 			if (rc)
645 				continue;
646 			rc = ntfs_collate_names(name, name_len,
647 					(ntfschar*)((u8*)a +
648 					le16_to_cpu(a->name_offset)),
649 					a->name_length, 1, CASE_SENSITIVE,
650 					upcase, upcase_len);
651 			if (rc == -1)
652 				return -ENOENT;
653 			if (rc)
654 				continue;
655 		}
656 		/*
657 		 * The names match or @name not present and attribute is
658 		 * unnamed.  If no @val specified, we have found the attribute
659 		 * and are done.
660 		 */
661 		if (!val)
662 			return 0;
663 		/* @val is present; compare values. */
664 		else {
665 			register int rc;
666 
667 			rc = memcmp(val, (u8*)a + le16_to_cpu(
668 					a->data.resident.value_offset),
669 					min_t(u32, val_len, le32_to_cpu(
670 					a->data.resident.value_length)));
671 			/*
672 			 * If @val collates before the current attribute's
673 			 * value, there is no matching attribute.
674 			 */
675 			if (!rc) {
676 				register u32 avl;
677 
678 				avl = le32_to_cpu(
679 						a->data.resident.value_length);
680 				if (val_len == avl)
681 					return 0;
682 				if (val_len < avl)
683 					return -ENOENT;
684 			} else if (rc < 0)
685 				return -ENOENT;
686 		}
687 	}
688 	ntfs_error(vol->sb, "Inode is corrupt.  Run chkdsk.");
689 	NVolSetErrors(vol);
690 	return -EIO;
691 }
692 
693 /**
694  * load_attribute_list - load an attribute list into memory
695  * @vol:		ntfs volume from which to read
696  * @runlist:		runlist of the attribute list
697  * @al_start:		destination buffer
698  * @size:		size of the destination buffer in bytes
699  * @initialized_size:	initialized size of the attribute list
700  *
701  * Walk the runlist @runlist and load all clusters from it copying them into
702  * the linear buffer @al. The maximum number of bytes copied to @al is @size
703  * bytes. Note, @size does not need to be a multiple of the cluster size. If
704  * @initialized_size is less than @size, the region in @al between
705  * @initialized_size and @size will be zeroed and not read from disk.
706  *
707  * Return 0 on success or -errno on error.
708  */
709 int load_attribute_list(ntfs_volume *vol, runlist *runlist, u8 *al_start,
710 		const s64 size, const s64 initialized_size)
711 {
712 	LCN lcn;
713 	u8 *al = al_start;
714 	u8 *al_end = al + initialized_size;
715 	runlist_element *rl;
716 	struct buffer_head *bh;
717 	struct super_block *sb;
718 	unsigned long block_size;
719 	unsigned long block, max_block;
720 	int err = 0;
721 	unsigned char block_size_bits;
722 
723 	ntfs_debug("Entering.");
724 	if (!vol || !runlist || !al || size <= 0 || initialized_size < 0 ||
725 			initialized_size > size)
726 		return -EINVAL;
727 	if (!initialized_size) {
728 		memset(al, 0, size);
729 		return 0;
730 	}
731 	sb = vol->sb;
732 	block_size = sb->s_blocksize;
733 	block_size_bits = sb->s_blocksize_bits;
734 	down_read(&runlist->lock);
735 	rl = runlist->rl;
736 	if (!rl) {
737 		ntfs_error(sb, "Cannot read attribute list since runlist is "
738 				"missing.");
739 		goto err_out;
740 	}
741 	/* Read all clusters specified by the runlist one run at a time. */
742 	while (rl->length) {
743 		lcn = ntfs_rl_vcn_to_lcn(rl, rl->vcn);
744 		ntfs_debug("Reading vcn = 0x%llx, lcn = 0x%llx.",
745 				(unsigned long long)rl->vcn,
746 				(unsigned long long)lcn);
747 		/* The attribute list cannot be sparse. */
748 		if (lcn < 0) {
749 			ntfs_error(sb, "ntfs_rl_vcn_to_lcn() failed.  Cannot "
750 					"read attribute list.");
751 			goto err_out;
752 		}
753 		block = lcn << vol->cluster_size_bits >> block_size_bits;
754 		/* Read the run from device in chunks of block_size bytes. */
755 		max_block = block + (rl->length << vol->cluster_size_bits >>
756 				block_size_bits);
757 		ntfs_debug("max_block = 0x%lx.", max_block);
758 		do {
759 			ntfs_debug("Reading block = 0x%lx.", block);
760 			bh = sb_bread(sb, block);
761 			if (!bh) {
762 				ntfs_error(sb, "sb_bread() failed. Cannot "
763 						"read attribute list.");
764 				goto err_out;
765 			}
766 			if (al + block_size >= al_end)
767 				goto do_final;
768 			memcpy(al, bh->b_data, block_size);
769 			brelse(bh);
770 			al += block_size;
771 		} while (++block < max_block);
772 		rl++;
773 	}
774 	if (initialized_size < size) {
775 initialize:
776 		memset(al_start + initialized_size, 0, size - initialized_size);
777 	}
778 done:
779 	up_read(&runlist->lock);
780 	return err;
781 do_final:
782 	if (al < al_end) {
783 		/*
784 		 * Partial block.
785 		 *
786 		 * Note: The attribute list can be smaller than its allocation
787 		 * by multiple clusters.  This has been encountered by at least
788 		 * two people running Windows XP, thus we cannot do any
789 		 * truncation sanity checking here. (AIA)
790 		 */
791 		memcpy(al, bh->b_data, al_end - al);
792 		brelse(bh);
793 		if (initialized_size < size)
794 			goto initialize;
795 		goto done;
796 	}
797 	brelse(bh);
798 	/* Real overflow! */
799 	ntfs_error(sb, "Attribute list buffer overflow. Read attribute list "
800 			"is truncated.");
801 err_out:
802 	err = -EIO;
803 	goto done;
804 }
805 
806 /**
807  * ntfs_external_attr_find - find an attribute in the attribute list of an inode
808  * @type:	attribute type to find
809  * @name:	attribute name to find (optional, i.e. NULL means don't care)
810  * @name_len:	attribute name length (only needed if @name present)
811  * @ic:		IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
812  * @lowest_vcn:	lowest vcn to find (optional, non-resident attributes only)
813  * @val:	attribute value to find (optional, resident attributes only)
814  * @val_len:	attribute value length
815  * @ctx:	search context with mft record and attribute to search from
816  *
817  * You should not need to call this function directly.  Use ntfs_attr_lookup()
818  * instead.
819  *
820  * Find an attribute by searching the attribute list for the corresponding
821  * attribute list entry.  Having found the entry, map the mft record if the
822  * attribute is in a different mft record/inode, ntfs_attr_find() the attribute
823  * in there and return it.
824  *
825  * On first search @ctx->ntfs_ino must be the base mft record and @ctx must
826  * have been obtained from a call to ntfs_attr_get_search_ctx().  On subsequent
827  * calls @ctx->ntfs_ino can be any extent inode, too (@ctx->base_ntfs_ino is
828  * then the base inode).
829  *
830  * After finishing with the attribute/mft record you need to call
831  * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any
832  * mapped inodes, etc).
833  *
834  * If the attribute is found, ntfs_external_attr_find() returns 0 and
835  * @ctx->attr will point to the found attribute.  @ctx->mrec will point to the
836  * mft record in which @ctx->attr is located and @ctx->al_entry will point to
837  * the attribute list entry for the attribute.
838  *
839  * If the attribute is not found, ntfs_external_attr_find() returns -ENOENT and
840  * @ctx->attr will point to the attribute in the base mft record before which
841  * the attribute being searched for would need to be inserted if such an action
842  * were to be desired.  @ctx->mrec will point to the mft record in which
843  * @ctx->attr is located and @ctx->al_entry will point to the attribute list
844  * entry of the attribute before which the attribute being searched for would
845  * need to be inserted if such an action were to be desired.
846  *
847  * Thus to insert the not found attribute, one wants to add the attribute to
848  * @ctx->mrec (the base mft record) and if there is not enough space, the
849  * attribute should be placed in a newly allocated extent mft record.  The
850  * attribute list entry for the inserted attribute should be inserted in the
851  * attribute list attribute at @ctx->al_entry.
852  *
853  * On actual error, ntfs_external_attr_find() returns -EIO.  In this case
854  * @ctx->attr is undefined and in particular do not rely on it not changing.
855  */
856 static int ntfs_external_attr_find(const ATTR_TYPE type,
857 		const ntfschar *name, const u32 name_len,
858 		const IGNORE_CASE_BOOL ic, const VCN lowest_vcn,
859 		const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx)
860 {
861 	ntfs_inode *base_ni, *ni;
862 	ntfs_volume *vol;
863 	ATTR_LIST_ENTRY *al_entry, *next_al_entry;
864 	u8 *al_start, *al_end;
865 	ATTR_RECORD *a;
866 	ntfschar *al_name;
867 	u32 al_name_len;
868 	int err = 0;
869 	static const char *es = " Unmount and run chkdsk.";
870 
871 	ni = ctx->ntfs_ino;
872 	base_ni = ctx->base_ntfs_ino;
873 	ntfs_debug("Entering for inode 0x%lx, type 0x%x.", ni->mft_no, type);
874 	if (!base_ni) {
875 		/* First call happens with the base mft record. */
876 		base_ni = ctx->base_ntfs_ino = ctx->ntfs_ino;
877 		ctx->base_mrec = ctx->mrec;
878 	}
879 	if (ni == base_ni)
880 		ctx->base_attr = ctx->attr;
881 	if (type == AT_END)
882 		goto not_found;
883 	vol = base_ni->vol;
884 	al_start = base_ni->attr_list;
885 	al_end = al_start + base_ni->attr_list_size;
886 	if (!ctx->al_entry)
887 		ctx->al_entry = (ATTR_LIST_ENTRY*)al_start;
888 	/*
889 	 * Iterate over entries in attribute list starting at @ctx->al_entry,
890 	 * or the entry following that, if @ctx->is_first is 'true'.
891 	 */
892 	if (ctx->is_first) {
893 		al_entry = ctx->al_entry;
894 		ctx->is_first = false;
895 	} else
896 		al_entry = (ATTR_LIST_ENTRY*)((u8*)ctx->al_entry +
897 				le16_to_cpu(ctx->al_entry->length));
898 	for (;; al_entry = next_al_entry) {
899 		/* Out of bounds check. */
900 		if ((u8*)al_entry < base_ni->attr_list ||
901 				(u8*)al_entry > al_end)
902 			break;	/* Inode is corrupt. */
903 		ctx->al_entry = al_entry;
904 		/* Catch the end of the attribute list. */
905 		if ((u8*)al_entry == al_end)
906 			goto not_found;
907 		if (!al_entry->length)
908 			break;
909 		if ((u8*)al_entry + 6 > al_end || (u8*)al_entry +
910 				le16_to_cpu(al_entry->length) > al_end)
911 			break;
912 		next_al_entry = (ATTR_LIST_ENTRY*)((u8*)al_entry +
913 				le16_to_cpu(al_entry->length));
914 		if (le32_to_cpu(al_entry->type) > le32_to_cpu(type))
915 			goto not_found;
916 		if (type != al_entry->type)
917 			continue;
918 		/*
919 		 * If @name is present, compare the two names.  If @name is
920 		 * missing, assume we want an unnamed attribute.
921 		 */
922 		al_name_len = al_entry->name_length;
923 		al_name = (ntfschar*)((u8*)al_entry + al_entry->name_offset);
924 		if (!name) {
925 			if (al_name_len)
926 				goto not_found;
927 		} else if (!ntfs_are_names_equal(al_name, al_name_len, name,
928 				name_len, ic, vol->upcase, vol->upcase_len)) {
929 			register int rc;
930 
931 			rc = ntfs_collate_names(name, name_len, al_name,
932 					al_name_len, 1, IGNORE_CASE,
933 					vol->upcase, vol->upcase_len);
934 			/*
935 			 * If @name collates before al_name, there is no
936 			 * matching attribute.
937 			 */
938 			if (rc == -1)
939 				goto not_found;
940 			/* If the strings are not equal, continue search. */
941 			if (rc)
942 				continue;
943 			/*
944 			 * FIXME: Reverse engineering showed 0, IGNORE_CASE but
945 			 * that is inconsistent with ntfs_attr_find().  The
946 			 * subsequent rc checks were also different.  Perhaps I
947 			 * made a mistake in one of the two.  Need to recheck
948 			 * which is correct or at least see what is going on...
949 			 * (AIA)
950 			 */
951 			rc = ntfs_collate_names(name, name_len, al_name,
952 					al_name_len, 1, CASE_SENSITIVE,
953 					vol->upcase, vol->upcase_len);
954 			if (rc == -1)
955 				goto not_found;
956 			if (rc)
957 				continue;
958 		}
959 		/*
960 		 * The names match or @name not present and attribute is
961 		 * unnamed.  Now check @lowest_vcn.  Continue search if the
962 		 * next attribute list entry still fits @lowest_vcn.  Otherwise
963 		 * we have reached the right one or the search has failed.
964 		 */
965 		if (lowest_vcn && (u8*)next_al_entry >= al_start	    &&
966 				(u8*)next_al_entry + 6 < al_end		    &&
967 				(u8*)next_al_entry + le16_to_cpu(
968 					next_al_entry->length) <= al_end    &&
969 				sle64_to_cpu(next_al_entry->lowest_vcn) <=
970 					lowest_vcn			    &&
971 				next_al_entry->type == al_entry->type	    &&
972 				next_al_entry->name_length == al_name_len   &&
973 				ntfs_are_names_equal((ntfschar*)((u8*)
974 					next_al_entry +
975 					next_al_entry->name_offset),
976 					next_al_entry->name_length,
977 					al_name, al_name_len, CASE_SENSITIVE,
978 					vol->upcase, vol->upcase_len))
979 			continue;
980 		if (MREF_LE(al_entry->mft_reference) == ni->mft_no) {
981 			if (MSEQNO_LE(al_entry->mft_reference) != ni->seq_no) {
982 				ntfs_error(vol->sb, "Found stale mft "
983 						"reference in attribute list "
984 						"of base inode 0x%lx.%s",
985 						base_ni->mft_no, es);
986 				err = -EIO;
987 				break;
988 			}
989 		} else { /* Mft references do not match. */
990 			/* If there is a mapped record unmap it first. */
991 			if (ni != base_ni)
992 				unmap_extent_mft_record(ni);
993 			/* Do we want the base record back? */
994 			if (MREF_LE(al_entry->mft_reference) ==
995 					base_ni->mft_no) {
996 				ni = ctx->ntfs_ino = base_ni;
997 				ctx->mrec = ctx->base_mrec;
998 			} else {
999 				/* We want an extent record. */
1000 				ctx->mrec = map_extent_mft_record(base_ni,
1001 						le64_to_cpu(
1002 						al_entry->mft_reference), &ni);
1003 				if (IS_ERR(ctx->mrec)) {
1004 					ntfs_error(vol->sb, "Failed to map "
1005 							"extent mft record "
1006 							"0x%lx of base inode "
1007 							"0x%lx.%s",
1008 							MREF_LE(al_entry->
1009 							mft_reference),
1010 							base_ni->mft_no, es);
1011 					err = PTR_ERR(ctx->mrec);
1012 					if (err == -ENOENT)
1013 						err = -EIO;
1014 					/* Cause @ctx to be sanitized below. */
1015 					ni = NULL;
1016 					break;
1017 				}
1018 				ctx->ntfs_ino = ni;
1019 			}
1020 			ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1021 					le16_to_cpu(ctx->mrec->attrs_offset));
1022 		}
1023 		/*
1024 		 * ctx->vfs_ino, ctx->mrec, and ctx->attr now point to the
1025 		 * mft record containing the attribute represented by the
1026 		 * current al_entry.
1027 		 */
1028 		/*
1029 		 * We could call into ntfs_attr_find() to find the right
1030 		 * attribute in this mft record but this would be less
1031 		 * efficient and not quite accurate as ntfs_attr_find() ignores
1032 		 * the attribute instance numbers for example which become
1033 		 * important when one plays with attribute lists.  Also,
1034 		 * because a proper match has been found in the attribute list
1035 		 * entry above, the comparison can now be optimized.  So it is
1036 		 * worth re-implementing a simplified ntfs_attr_find() here.
1037 		 */
1038 		a = ctx->attr;
1039 		/*
1040 		 * Use a manual loop so we can still use break and continue
1041 		 * with the same meanings as above.
1042 		 */
1043 do_next_attr_loop:
1044 		if ((u8*)a < (u8*)ctx->mrec || (u8*)a > (u8*)ctx->mrec +
1045 				le32_to_cpu(ctx->mrec->bytes_allocated))
1046 			break;
1047 		if (a->type == AT_END)
1048 			break;
1049 		if (!a->length)
1050 			break;
1051 		if (al_entry->instance != a->instance)
1052 			goto do_next_attr;
1053 		/*
1054 		 * If the type and/or the name are mismatched between the
1055 		 * attribute list entry and the attribute record, there is
1056 		 * corruption so we break and return error EIO.
1057 		 */
1058 		if (al_entry->type != a->type)
1059 			break;
1060 		if (!ntfs_are_names_equal((ntfschar*)((u8*)a +
1061 				le16_to_cpu(a->name_offset)), a->name_length,
1062 				al_name, al_name_len, CASE_SENSITIVE,
1063 				vol->upcase, vol->upcase_len))
1064 			break;
1065 		ctx->attr = a;
1066 		/*
1067 		 * If no @val specified or @val specified and it matches, we
1068 		 * have found it!
1069 		 */
1070 		if (!val || (!a->non_resident && le32_to_cpu(
1071 				a->data.resident.value_length) == val_len &&
1072 				!memcmp((u8*)a +
1073 				le16_to_cpu(a->data.resident.value_offset),
1074 				val, val_len))) {
1075 			ntfs_debug("Done, found.");
1076 			return 0;
1077 		}
1078 do_next_attr:
1079 		/* Proceed to the next attribute in the current mft record. */
1080 		a = (ATTR_RECORD*)((u8*)a + le32_to_cpu(a->length));
1081 		goto do_next_attr_loop;
1082 	}
1083 	if (!err) {
1084 		ntfs_error(vol->sb, "Base inode 0x%lx contains corrupt "
1085 				"attribute list attribute.%s", base_ni->mft_no,
1086 				es);
1087 		err = -EIO;
1088 	}
1089 	if (ni != base_ni) {
1090 		if (ni)
1091 			unmap_extent_mft_record(ni);
1092 		ctx->ntfs_ino = base_ni;
1093 		ctx->mrec = ctx->base_mrec;
1094 		ctx->attr = ctx->base_attr;
1095 	}
1096 	if (err != -ENOMEM)
1097 		NVolSetErrors(vol);
1098 	return err;
1099 not_found:
1100 	/*
1101 	 * If we were looking for AT_END, we reset the search context @ctx and
1102 	 * use ntfs_attr_find() to seek to the end of the base mft record.
1103 	 */
1104 	if (type == AT_END) {
1105 		ntfs_attr_reinit_search_ctx(ctx);
1106 		return ntfs_attr_find(AT_END, name, name_len, ic, val, val_len,
1107 				ctx);
1108 	}
1109 	/*
1110 	 * The attribute was not found.  Before we return, we want to ensure
1111 	 * @ctx->mrec and @ctx->attr indicate the position at which the
1112 	 * attribute should be inserted in the base mft record.  Since we also
1113 	 * want to preserve @ctx->al_entry we cannot reinitialize the search
1114 	 * context using ntfs_attr_reinit_search_ctx() as this would set
1115 	 * @ctx->al_entry to NULL.  Thus we do the necessary bits manually (see
1116 	 * ntfs_attr_init_search_ctx() below).  Note, we _only_ preserve
1117 	 * @ctx->al_entry as the remaining fields (base_*) are identical to
1118 	 * their non base_ counterparts and we cannot set @ctx->base_attr
1119 	 * correctly yet as we do not know what @ctx->attr will be set to by
1120 	 * the call to ntfs_attr_find() below.
1121 	 */
1122 	if (ni != base_ni)
1123 		unmap_extent_mft_record(ni);
1124 	ctx->mrec = ctx->base_mrec;
1125 	ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1126 			le16_to_cpu(ctx->mrec->attrs_offset));
1127 	ctx->is_first = true;
1128 	ctx->ntfs_ino = base_ni;
1129 	ctx->base_ntfs_ino = NULL;
1130 	ctx->base_mrec = NULL;
1131 	ctx->base_attr = NULL;
1132 	/*
1133 	 * In case there are multiple matches in the base mft record, need to
1134 	 * keep enumerating until we get an attribute not found response (or
1135 	 * another error), otherwise we would keep returning the same attribute
1136 	 * over and over again and all programs using us for enumeration would
1137 	 * lock up in a tight loop.
1138 	 */
1139 	do {
1140 		err = ntfs_attr_find(type, name, name_len, ic, val, val_len,
1141 				ctx);
1142 	} while (!err);
1143 	ntfs_debug("Done, not found.");
1144 	return err;
1145 }
1146 
1147 /**
1148  * ntfs_attr_lookup - find an attribute in an ntfs inode
1149  * @type:	attribute type to find
1150  * @name:	attribute name to find (optional, i.e. NULL means don't care)
1151  * @name_len:	attribute name length (only needed if @name present)
1152  * @ic:		IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present)
1153  * @lowest_vcn:	lowest vcn to find (optional, non-resident attributes only)
1154  * @val:	attribute value to find (optional, resident attributes only)
1155  * @val_len:	attribute value length
1156  * @ctx:	search context with mft record and attribute to search from
1157  *
1158  * Find an attribute in an ntfs inode.  On first search @ctx->ntfs_ino must
1159  * be the base mft record and @ctx must have been obtained from a call to
1160  * ntfs_attr_get_search_ctx().
1161  *
1162  * This function transparently handles attribute lists and @ctx is used to
1163  * continue searches where they were left off at.
1164  *
1165  * After finishing with the attribute/mft record you need to call
1166  * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any
1167  * mapped inodes, etc).
1168  *
1169  * Return 0 if the search was successful and -errno if not.
1170  *
1171  * When 0, @ctx->attr is the found attribute and it is in mft record
1172  * @ctx->mrec.  If an attribute list attribute is present, @ctx->al_entry is
1173  * the attribute list entry of the found attribute.
1174  *
1175  * When -ENOENT, @ctx->attr is the attribute which collates just after the
1176  * attribute being searched for, i.e. if one wants to add the attribute to the
1177  * mft record this is the correct place to insert it into.  If an attribute
1178  * list attribute is present, @ctx->al_entry is the attribute list entry which
1179  * collates just after the attribute list entry of the attribute being searched
1180  * for, i.e. if one wants to add the attribute to the mft record this is the
1181  * correct place to insert its attribute list entry into.
1182  *
1183  * When -errno != -ENOENT, an error occured during the lookup.  @ctx->attr is
1184  * then undefined and in particular you should not rely on it not changing.
1185  */
1186 int ntfs_attr_lookup(const ATTR_TYPE type, const ntfschar *name,
1187 		const u32 name_len, const IGNORE_CASE_BOOL ic,
1188 		const VCN lowest_vcn, const u8 *val, const u32 val_len,
1189 		ntfs_attr_search_ctx *ctx)
1190 {
1191 	ntfs_inode *base_ni;
1192 
1193 	ntfs_debug("Entering.");
1194 	BUG_ON(IS_ERR(ctx->mrec));
1195 	if (ctx->base_ntfs_ino)
1196 		base_ni = ctx->base_ntfs_ino;
1197 	else
1198 		base_ni = ctx->ntfs_ino;
1199 	/* Sanity check, just for debugging really. */
1200 	BUG_ON(!base_ni);
1201 	if (!NInoAttrList(base_ni) || type == AT_ATTRIBUTE_LIST)
1202 		return ntfs_attr_find(type, name, name_len, ic, val, val_len,
1203 				ctx);
1204 	return ntfs_external_attr_find(type, name, name_len, ic, lowest_vcn,
1205 			val, val_len, ctx);
1206 }
1207 
1208 /**
1209  * ntfs_attr_init_search_ctx - initialize an attribute search context
1210  * @ctx:	attribute search context to initialize
1211  * @ni:		ntfs inode with which to initialize the search context
1212  * @mrec:	mft record with which to initialize the search context
1213  *
1214  * Initialize the attribute search context @ctx with @ni and @mrec.
1215  */
1216 static inline void ntfs_attr_init_search_ctx(ntfs_attr_search_ctx *ctx,
1217 		ntfs_inode *ni, MFT_RECORD *mrec)
1218 {
1219 	*ctx = (ntfs_attr_search_ctx) {
1220 		.mrec = mrec,
1221 		/* Sanity checks are performed elsewhere. */
1222 		.attr = (ATTR_RECORD*)((u8*)mrec +
1223 				le16_to_cpu(mrec->attrs_offset)),
1224 		.is_first = true,
1225 		.ntfs_ino = ni,
1226 	};
1227 }
1228 
1229 /**
1230  * ntfs_attr_reinit_search_ctx - reinitialize an attribute search context
1231  * @ctx:	attribute search context to reinitialize
1232  *
1233  * Reinitialize the attribute search context @ctx, unmapping an associated
1234  * extent mft record if present, and initialize the search context again.
1235  *
1236  * This is used when a search for a new attribute is being started to reset
1237  * the search context to the beginning.
1238  */
1239 void ntfs_attr_reinit_search_ctx(ntfs_attr_search_ctx *ctx)
1240 {
1241 	if (likely(!ctx->base_ntfs_ino)) {
1242 		/* No attribute list. */
1243 		ctx->is_first = true;
1244 		/* Sanity checks are performed elsewhere. */
1245 		ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec +
1246 				le16_to_cpu(ctx->mrec->attrs_offset));
1247 		/*
1248 		 * This needs resetting due to ntfs_external_attr_find() which
1249 		 * can leave it set despite having zeroed ctx->base_ntfs_ino.
1250 		 */
1251 		ctx->al_entry = NULL;
1252 		return;
1253 	} /* Attribute list. */
1254 	if (ctx->ntfs_ino != ctx->base_ntfs_ino)
1255 		unmap_extent_mft_record(ctx->ntfs_ino);
1256 	ntfs_attr_init_search_ctx(ctx, ctx->base_ntfs_ino, ctx->base_mrec);
1257 	return;
1258 }
1259 
1260 /**
1261  * ntfs_attr_get_search_ctx - allocate/initialize a new attribute search context
1262  * @ni:		ntfs inode with which to initialize the search context
1263  * @mrec:	mft record with which to initialize the search context
1264  *
1265  * Allocate a new attribute search context, initialize it with @ni and @mrec,
1266  * and return it. Return NULL if allocation failed.
1267  */
1268 ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(ntfs_inode *ni, MFT_RECORD *mrec)
1269 {
1270 	ntfs_attr_search_ctx *ctx;
1271 
1272 	ctx = kmem_cache_alloc(ntfs_attr_ctx_cache, GFP_NOFS);
1273 	if (ctx)
1274 		ntfs_attr_init_search_ctx(ctx, ni, mrec);
1275 	return ctx;
1276 }
1277 
1278 /**
1279  * ntfs_attr_put_search_ctx - release an attribute search context
1280  * @ctx:	attribute search context to free
1281  *
1282  * Release the attribute search context @ctx, unmapping an associated extent
1283  * mft record if present.
1284  */
1285 void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx)
1286 {
1287 	if (ctx->base_ntfs_ino && ctx->ntfs_ino != ctx->base_ntfs_ino)
1288 		unmap_extent_mft_record(ctx->ntfs_ino);
1289 	kmem_cache_free(ntfs_attr_ctx_cache, ctx);
1290 	return;
1291 }
1292 
1293 #ifdef NTFS_RW
1294 
1295 /**
1296  * ntfs_attr_find_in_attrdef - find an attribute in the $AttrDef system file
1297  * @vol:	ntfs volume to which the attribute belongs
1298  * @type:	attribute type which to find
1299  *
1300  * Search for the attribute definition record corresponding to the attribute
1301  * @type in the $AttrDef system file.
1302  *
1303  * Return the attribute type definition record if found and NULL if not found.
1304  */
1305 static ATTR_DEF *ntfs_attr_find_in_attrdef(const ntfs_volume *vol,
1306 		const ATTR_TYPE type)
1307 {
1308 	ATTR_DEF *ad;
1309 
1310 	BUG_ON(!vol->attrdef);
1311 	BUG_ON(!type);
1312 	for (ad = vol->attrdef; (u8*)ad - (u8*)vol->attrdef <
1313 			vol->attrdef_size && ad->type; ++ad) {
1314 		/* We have not found it yet, carry on searching. */
1315 		if (likely(le32_to_cpu(ad->type) < le32_to_cpu(type)))
1316 			continue;
1317 		/* We found the attribute; return it. */
1318 		if (likely(ad->type == type))
1319 			return ad;
1320 		/* We have gone too far already.  No point in continuing. */
1321 		break;
1322 	}
1323 	/* Attribute not found. */
1324 	ntfs_debug("Attribute type 0x%x not found in $AttrDef.",
1325 			le32_to_cpu(type));
1326 	return NULL;
1327 }
1328 
1329 /**
1330  * ntfs_attr_size_bounds_check - check a size of an attribute type for validity
1331  * @vol:	ntfs volume to which the attribute belongs
1332  * @type:	attribute type which to check
1333  * @size:	size which to check
1334  *
1335  * Check whether the @size in bytes is valid for an attribute of @type on the
1336  * ntfs volume @vol.  This information is obtained from $AttrDef system file.
1337  *
1338  * Return 0 if valid, -ERANGE if not valid, or -ENOENT if the attribute is not
1339  * listed in $AttrDef.
1340  */
1341 int ntfs_attr_size_bounds_check(const ntfs_volume *vol, const ATTR_TYPE type,
1342 		const s64 size)
1343 {
1344 	ATTR_DEF *ad;
1345 
1346 	BUG_ON(size < 0);
1347 	/*
1348 	 * $ATTRIBUTE_LIST has a maximum size of 256kiB, but this is not
1349 	 * listed in $AttrDef.
1350 	 */
1351 	if (unlikely(type == AT_ATTRIBUTE_LIST && size > 256 * 1024))
1352 		return -ERANGE;
1353 	/* Get the $AttrDef entry for the attribute @type. */
1354 	ad = ntfs_attr_find_in_attrdef(vol, type);
1355 	if (unlikely(!ad))
1356 		return -ENOENT;
1357 	/* Do the bounds check. */
1358 	if (((sle64_to_cpu(ad->min_size) > 0) &&
1359 			size < sle64_to_cpu(ad->min_size)) ||
1360 			((sle64_to_cpu(ad->max_size) > 0) && size >
1361 			sle64_to_cpu(ad->max_size)))
1362 		return -ERANGE;
1363 	return 0;
1364 }
1365 
1366 /**
1367  * ntfs_attr_can_be_non_resident - check if an attribute can be non-resident
1368  * @vol:	ntfs volume to which the attribute belongs
1369  * @type:	attribute type which to check
1370  *
1371  * Check whether the attribute of @type on the ntfs volume @vol is allowed to
1372  * be non-resident.  This information is obtained from $AttrDef system file.
1373  *
1374  * Return 0 if the attribute is allowed to be non-resident, -EPERM if not, and
1375  * -ENOENT if the attribute is not listed in $AttrDef.
1376  */
1377 int ntfs_attr_can_be_non_resident(const ntfs_volume *vol, const ATTR_TYPE type)
1378 {
1379 	ATTR_DEF *ad;
1380 
1381 	/* Find the attribute definition record in $AttrDef. */
1382 	ad = ntfs_attr_find_in_attrdef(vol, type);
1383 	if (unlikely(!ad))
1384 		return -ENOENT;
1385 	/* Check the flags and return the result. */
1386 	if (ad->flags & ATTR_DEF_RESIDENT)
1387 		return -EPERM;
1388 	return 0;
1389 }
1390 
1391 /**
1392  * ntfs_attr_can_be_resident - check if an attribute can be resident
1393  * @vol:	ntfs volume to which the attribute belongs
1394  * @type:	attribute type which to check
1395  *
1396  * Check whether the attribute of @type on the ntfs volume @vol is allowed to
1397  * be resident.  This information is derived from our ntfs knowledge and may
1398  * not be completely accurate, especially when user defined attributes are
1399  * present.  Basically we allow everything to be resident except for index
1400  * allocation and $EA attributes.
1401  *
1402  * Return 0 if the attribute is allowed to be non-resident and -EPERM if not.
1403  *
1404  * Warning: In the system file $MFT the attribute $Bitmap must be non-resident
1405  *	    otherwise windows will not boot (blue screen of death)!  We cannot
1406  *	    check for this here as we do not know which inode's $Bitmap is
1407  *	    being asked about so the caller needs to special case this.
1408  */
1409 int ntfs_attr_can_be_resident(const ntfs_volume *vol, const ATTR_TYPE type)
1410 {
1411 	if (type == AT_INDEX_ALLOCATION)
1412 		return -EPERM;
1413 	return 0;
1414 }
1415 
1416 /**
1417  * ntfs_attr_record_resize - resize an attribute record
1418  * @m:		mft record containing attribute record
1419  * @a:		attribute record to resize
1420  * @new_size:	new size in bytes to which to resize the attribute record @a
1421  *
1422  * Resize the attribute record @a, i.e. the resident part of the attribute, in
1423  * the mft record @m to @new_size bytes.
1424  *
1425  * Return 0 on success and -errno on error.  The following error codes are
1426  * defined:
1427  *	-ENOSPC	- Not enough space in the mft record @m to perform the resize.
1428  *
1429  * Note: On error, no modifications have been performed whatsoever.
1430  *
1431  * Warning: If you make a record smaller without having copied all the data you
1432  *	    are interested in the data may be overwritten.
1433  */
1434 int ntfs_attr_record_resize(MFT_RECORD *m, ATTR_RECORD *a, u32 new_size)
1435 {
1436 	ntfs_debug("Entering for new_size %u.", new_size);
1437 	/* Align to 8 bytes if it is not already done. */
1438 	if (new_size & 7)
1439 		new_size = (new_size + 7) & ~7;
1440 	/* If the actual attribute length has changed, move things around. */
1441 	if (new_size != le32_to_cpu(a->length)) {
1442 		u32 new_muse = le32_to_cpu(m->bytes_in_use) -
1443 				le32_to_cpu(a->length) + new_size;
1444 		/* Not enough space in this mft record. */
1445 		if (new_muse > le32_to_cpu(m->bytes_allocated))
1446 			return -ENOSPC;
1447 		/* Move attributes following @a to their new location. */
1448 		memmove((u8*)a + new_size, (u8*)a + le32_to_cpu(a->length),
1449 				le32_to_cpu(m->bytes_in_use) - ((u8*)a -
1450 				(u8*)m) - le32_to_cpu(a->length));
1451 		/* Adjust @m to reflect the change in used space. */
1452 		m->bytes_in_use = cpu_to_le32(new_muse);
1453 		/* Adjust @a to reflect the new size. */
1454 		if (new_size >= offsetof(ATTR_REC, length) + sizeof(a->length))
1455 			a->length = cpu_to_le32(new_size);
1456 	}
1457 	return 0;
1458 }
1459 
1460 /**
1461  * ntfs_resident_attr_value_resize - resize the value of a resident attribute
1462  * @m:		mft record containing attribute record
1463  * @a:		attribute record whose value to resize
1464  * @new_size:	new size in bytes to which to resize the attribute value of @a
1465  *
1466  * Resize the value of the attribute @a in the mft record @m to @new_size bytes.
1467  * If the value is made bigger, the newly allocated space is cleared.
1468  *
1469  * Return 0 on success and -errno on error.  The following error codes are
1470  * defined:
1471  *	-ENOSPC	- Not enough space in the mft record @m to perform the resize.
1472  *
1473  * Note: On error, no modifications have been performed whatsoever.
1474  *
1475  * Warning: If you make a record smaller without having copied all the data you
1476  *	    are interested in the data may be overwritten.
1477  */
1478 int ntfs_resident_attr_value_resize(MFT_RECORD *m, ATTR_RECORD *a,
1479 		const u32 new_size)
1480 {
1481 	u32 old_size;
1482 
1483 	/* Resize the resident part of the attribute record. */
1484 	if (ntfs_attr_record_resize(m, a,
1485 			le16_to_cpu(a->data.resident.value_offset) + new_size))
1486 		return -ENOSPC;
1487 	/*
1488 	 * The resize succeeded!  If we made the attribute value bigger, clear
1489 	 * the area between the old size and @new_size.
1490 	 */
1491 	old_size = le32_to_cpu(a->data.resident.value_length);
1492 	if (new_size > old_size)
1493 		memset((u8*)a + le16_to_cpu(a->data.resident.value_offset) +
1494 				old_size, 0, new_size - old_size);
1495 	/* Finally update the length of the attribute value. */
1496 	a->data.resident.value_length = cpu_to_le32(new_size);
1497 	return 0;
1498 }
1499 
1500 /**
1501  * ntfs_attr_make_non_resident - convert a resident to a non-resident attribute
1502  * @ni:		ntfs inode describing the attribute to convert
1503  * @data_size:	size of the resident data to copy to the non-resident attribute
1504  *
1505  * Convert the resident ntfs attribute described by the ntfs inode @ni to a
1506  * non-resident one.
1507  *
1508  * @data_size must be equal to the attribute value size.  This is needed since
1509  * we need to know the size before we can map the mft record and our callers
1510  * always know it.  The reason we cannot simply read the size from the vfs
1511  * inode i_size is that this is not necessarily uptodate.  This happens when
1512  * ntfs_attr_make_non_resident() is called in the ->truncate call path(s).
1513  *
1514  * Return 0 on success and -errno on error.  The following error return codes
1515  * are defined:
1516  *	-EPERM	- The attribute is not allowed to be non-resident.
1517  *	-ENOMEM	- Not enough memory.
1518  *	-ENOSPC	- Not enough disk space.
1519  *	-EINVAL	- Attribute not defined on the volume.
1520  *	-EIO	- I/o error or other error.
1521  * Note that -ENOSPC is also returned in the case that there is not enough
1522  * space in the mft record to do the conversion.  This can happen when the mft
1523  * record is already very full.  The caller is responsible for trying to make
1524  * space in the mft record and trying again.  FIXME: Do we need a separate
1525  * error return code for this kind of -ENOSPC or is it always worth trying
1526  * again in case the attribute may then fit in a resident state so no need to
1527  * make it non-resident at all?  Ho-hum...  (AIA)
1528  *
1529  * NOTE to self: No changes in the attribute list are required to move from
1530  *		 a resident to a non-resident attribute.
1531  *
1532  * Locking: - The caller must hold i_mutex on the inode.
1533  */
1534 int ntfs_attr_make_non_resident(ntfs_inode *ni, const u32 data_size)
1535 {
1536 	s64 new_size;
1537 	struct inode *vi = VFS_I(ni);
1538 	ntfs_volume *vol = ni->vol;
1539 	ntfs_inode *base_ni;
1540 	MFT_RECORD *m;
1541 	ATTR_RECORD *a;
1542 	ntfs_attr_search_ctx *ctx;
1543 	struct page *page;
1544 	runlist_element *rl;
1545 	u8 *kaddr;
1546 	unsigned long flags;
1547 	int mp_size, mp_ofs, name_ofs, arec_size, err, err2;
1548 	u32 attr_size;
1549 	u8 old_res_attr_flags;
1550 
1551 	/* Check that the attribute is allowed to be non-resident. */
1552 	err = ntfs_attr_can_be_non_resident(vol, ni->type);
1553 	if (unlikely(err)) {
1554 		if (err == -EPERM)
1555 			ntfs_debug("Attribute is not allowed to be "
1556 					"non-resident.");
1557 		else
1558 			ntfs_debug("Attribute not defined on the NTFS "
1559 					"volume!");
1560 		return err;
1561 	}
1562 	/*
1563 	 * FIXME: Compressed and encrypted attributes are not supported when
1564 	 * writing and we should never have gotten here for them.
1565 	 */
1566 	BUG_ON(NInoCompressed(ni));
1567 	BUG_ON(NInoEncrypted(ni));
1568 	/*
1569 	 * The size needs to be aligned to a cluster boundary for allocation
1570 	 * purposes.
1571 	 */
1572 	new_size = (data_size + vol->cluster_size - 1) &
1573 			~(vol->cluster_size - 1);
1574 	if (new_size > 0) {
1575 		/*
1576 		 * Will need the page later and since the page lock nests
1577 		 * outside all ntfs locks, we need to get the page now.
1578 		 */
1579 		page = find_or_create_page(vi->i_mapping, 0,
1580 				mapping_gfp_mask(vi->i_mapping));
1581 		if (unlikely(!page))
1582 			return -ENOMEM;
1583 		/* Start by allocating clusters to hold the attribute value. */
1584 		rl = ntfs_cluster_alloc(vol, 0, new_size >>
1585 				vol->cluster_size_bits, -1, DATA_ZONE, true);
1586 		if (IS_ERR(rl)) {
1587 			err = PTR_ERR(rl);
1588 			ntfs_debug("Failed to allocate cluster%s, error code "
1589 					"%i.", (new_size >>
1590 					vol->cluster_size_bits) > 1 ? "s" : "",
1591 					err);
1592 			goto page_err_out;
1593 		}
1594 	} else {
1595 		rl = NULL;
1596 		page = NULL;
1597 	}
1598 	/* Determine the size of the mapping pairs array. */
1599 	mp_size = ntfs_get_size_for_mapping_pairs(vol, rl, 0, -1);
1600 	if (unlikely(mp_size < 0)) {
1601 		err = mp_size;
1602 		ntfs_debug("Failed to get size for mapping pairs array, error "
1603 				"code %i.", err);
1604 		goto rl_err_out;
1605 	}
1606 	down_write(&ni->runlist.lock);
1607 	if (!NInoAttr(ni))
1608 		base_ni = ni;
1609 	else
1610 		base_ni = ni->ext.base_ntfs_ino;
1611 	m = map_mft_record(base_ni);
1612 	if (IS_ERR(m)) {
1613 		err = PTR_ERR(m);
1614 		m = NULL;
1615 		ctx = NULL;
1616 		goto err_out;
1617 	}
1618 	ctx = ntfs_attr_get_search_ctx(base_ni, m);
1619 	if (unlikely(!ctx)) {
1620 		err = -ENOMEM;
1621 		goto err_out;
1622 	}
1623 	err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
1624 			CASE_SENSITIVE, 0, NULL, 0, ctx);
1625 	if (unlikely(err)) {
1626 		if (err == -ENOENT)
1627 			err = -EIO;
1628 		goto err_out;
1629 	}
1630 	m = ctx->mrec;
1631 	a = ctx->attr;
1632 	BUG_ON(NInoNonResident(ni));
1633 	BUG_ON(a->non_resident);
1634 	/*
1635 	 * Calculate new offsets for the name and the mapping pairs array.
1636 	 */
1637 	if (NInoSparse(ni) || NInoCompressed(ni))
1638 		name_ofs = (offsetof(ATTR_REC,
1639 				data.non_resident.compressed_size) +
1640 				sizeof(a->data.non_resident.compressed_size) +
1641 				7) & ~7;
1642 	else
1643 		name_ofs = (offsetof(ATTR_REC,
1644 				data.non_resident.compressed_size) + 7) & ~7;
1645 	mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
1646 	/*
1647 	 * Determine the size of the resident part of the now non-resident
1648 	 * attribute record.
1649 	 */
1650 	arec_size = (mp_ofs + mp_size + 7) & ~7;
1651 	/*
1652 	 * If the page is not uptodate bring it uptodate by copying from the
1653 	 * attribute value.
1654 	 */
1655 	attr_size = le32_to_cpu(a->data.resident.value_length);
1656 	BUG_ON(attr_size != data_size);
1657 	if (page && !PageUptodate(page)) {
1658 		kaddr = kmap_atomic(page, KM_USER0);
1659 		memcpy(kaddr, (u8*)a +
1660 				le16_to_cpu(a->data.resident.value_offset),
1661 				attr_size);
1662 		memset(kaddr + attr_size, 0, PAGE_CACHE_SIZE - attr_size);
1663 		kunmap_atomic(kaddr, KM_USER0);
1664 		flush_dcache_page(page);
1665 		SetPageUptodate(page);
1666 	}
1667 	/* Backup the attribute flag. */
1668 	old_res_attr_flags = a->data.resident.flags;
1669 	/* Resize the resident part of the attribute record. */
1670 	err = ntfs_attr_record_resize(m, a, arec_size);
1671 	if (unlikely(err))
1672 		goto err_out;
1673 	/*
1674 	 * Convert the resident part of the attribute record to describe a
1675 	 * non-resident attribute.
1676 	 */
1677 	a->non_resident = 1;
1678 	/* Move the attribute name if it exists and update the offset. */
1679 	if (a->name_length)
1680 		memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
1681 				a->name_length * sizeof(ntfschar));
1682 	a->name_offset = cpu_to_le16(name_ofs);
1683 	/* Setup the fields specific to non-resident attributes. */
1684 	a->data.non_resident.lowest_vcn = 0;
1685 	a->data.non_resident.highest_vcn = cpu_to_sle64((new_size - 1) >>
1686 			vol->cluster_size_bits);
1687 	a->data.non_resident.mapping_pairs_offset = cpu_to_le16(mp_ofs);
1688 	memset(&a->data.non_resident.reserved, 0,
1689 			sizeof(a->data.non_resident.reserved));
1690 	a->data.non_resident.allocated_size = cpu_to_sle64(new_size);
1691 	a->data.non_resident.data_size =
1692 			a->data.non_resident.initialized_size =
1693 			cpu_to_sle64(attr_size);
1694 	if (NInoSparse(ni) || NInoCompressed(ni)) {
1695 		a->data.non_resident.compression_unit = 0;
1696 		if (NInoCompressed(ni) || vol->major_ver < 3)
1697 			a->data.non_resident.compression_unit = 4;
1698 		a->data.non_resident.compressed_size =
1699 				a->data.non_resident.allocated_size;
1700 	} else
1701 		a->data.non_resident.compression_unit = 0;
1702 	/* Generate the mapping pairs array into the attribute record. */
1703 	err = ntfs_mapping_pairs_build(vol, (u8*)a + mp_ofs,
1704 			arec_size - mp_ofs, rl, 0, -1, NULL);
1705 	if (unlikely(err)) {
1706 		ntfs_debug("Failed to build mapping pairs, error code %i.",
1707 				err);
1708 		goto undo_err_out;
1709 	}
1710 	/* Setup the in-memory attribute structure to be non-resident. */
1711 	ni->runlist.rl = rl;
1712 	write_lock_irqsave(&ni->size_lock, flags);
1713 	ni->allocated_size = new_size;
1714 	if (NInoSparse(ni) || NInoCompressed(ni)) {
1715 		ni->itype.compressed.size = ni->allocated_size;
1716 		if (a->data.non_resident.compression_unit) {
1717 			ni->itype.compressed.block_size = 1U << (a->data.
1718 					non_resident.compression_unit +
1719 					vol->cluster_size_bits);
1720 			ni->itype.compressed.block_size_bits =
1721 					ffs(ni->itype.compressed.block_size) -
1722 					1;
1723 			ni->itype.compressed.block_clusters = 1U <<
1724 					a->data.non_resident.compression_unit;
1725 		} else {
1726 			ni->itype.compressed.block_size = 0;
1727 			ni->itype.compressed.block_size_bits = 0;
1728 			ni->itype.compressed.block_clusters = 0;
1729 		}
1730 		vi->i_blocks = ni->itype.compressed.size >> 9;
1731 	} else
1732 		vi->i_blocks = ni->allocated_size >> 9;
1733 	write_unlock_irqrestore(&ni->size_lock, flags);
1734 	/*
1735 	 * This needs to be last since the address space operations ->readpage
1736 	 * and ->writepage can run concurrently with us as they are not
1737 	 * serialized on i_mutex.  Note, we are not allowed to fail once we flip
1738 	 * this switch, which is another reason to do this last.
1739 	 */
1740 	NInoSetNonResident(ni);
1741 	/* Mark the mft record dirty, so it gets written back. */
1742 	flush_dcache_mft_record_page(ctx->ntfs_ino);
1743 	mark_mft_record_dirty(ctx->ntfs_ino);
1744 	ntfs_attr_put_search_ctx(ctx);
1745 	unmap_mft_record(base_ni);
1746 	up_write(&ni->runlist.lock);
1747 	if (page) {
1748 		set_page_dirty(page);
1749 		unlock_page(page);
1750 		mark_page_accessed(page);
1751 		page_cache_release(page);
1752 	}
1753 	ntfs_debug("Done.");
1754 	return 0;
1755 undo_err_out:
1756 	/* Convert the attribute back into a resident attribute. */
1757 	a->non_resident = 0;
1758 	/* Move the attribute name if it exists and update the offset. */
1759 	name_ofs = (offsetof(ATTR_RECORD, data.resident.reserved) +
1760 			sizeof(a->data.resident.reserved) + 7) & ~7;
1761 	if (a->name_length)
1762 		memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset),
1763 				a->name_length * sizeof(ntfschar));
1764 	mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7;
1765 	a->name_offset = cpu_to_le16(name_ofs);
1766 	arec_size = (mp_ofs + attr_size + 7) & ~7;
1767 	/* Resize the resident part of the attribute record. */
1768 	err2 = ntfs_attr_record_resize(m, a, arec_size);
1769 	if (unlikely(err2)) {
1770 		/*
1771 		 * This cannot happen (well if memory corruption is at work it
1772 		 * could happen in theory), but deal with it as well as we can.
1773 		 * If the old size is too small, truncate the attribute,
1774 		 * otherwise simply give it a larger allocated size.
1775 		 * FIXME: Should check whether chkdsk complains when the
1776 		 * allocated size is much bigger than the resident value size.
1777 		 */
1778 		arec_size = le32_to_cpu(a->length);
1779 		if ((mp_ofs + attr_size) > arec_size) {
1780 			err2 = attr_size;
1781 			attr_size = arec_size - mp_ofs;
1782 			ntfs_error(vol->sb, "Failed to undo partial resident "
1783 					"to non-resident attribute "
1784 					"conversion.  Truncating inode 0x%lx, "
1785 					"attribute type 0x%x from %i bytes to "
1786 					"%i bytes to maintain metadata "
1787 					"consistency.  THIS MEANS YOU ARE "
1788 					"LOSING %i BYTES DATA FROM THIS %s.",
1789 					vi->i_ino,
1790 					(unsigned)le32_to_cpu(ni->type),
1791 					err2, attr_size, err2 - attr_size,
1792 					((ni->type == AT_DATA) &&
1793 					!ni->name_len) ? "FILE": "ATTRIBUTE");
1794 			write_lock_irqsave(&ni->size_lock, flags);
1795 			ni->initialized_size = attr_size;
1796 			i_size_write(vi, attr_size);
1797 			write_unlock_irqrestore(&ni->size_lock, flags);
1798 		}
1799 	}
1800 	/* Setup the fields specific to resident attributes. */
1801 	a->data.resident.value_length = cpu_to_le32(attr_size);
1802 	a->data.resident.value_offset = cpu_to_le16(mp_ofs);
1803 	a->data.resident.flags = old_res_attr_flags;
1804 	memset(&a->data.resident.reserved, 0,
1805 			sizeof(a->data.resident.reserved));
1806 	/* Copy the data from the page back to the attribute value. */
1807 	if (page) {
1808 		kaddr = kmap_atomic(page, KM_USER0);
1809 		memcpy((u8*)a + mp_ofs, kaddr, attr_size);
1810 		kunmap_atomic(kaddr, KM_USER0);
1811 	}
1812 	/* Setup the allocated size in the ntfs inode in case it changed. */
1813 	write_lock_irqsave(&ni->size_lock, flags);
1814 	ni->allocated_size = arec_size - mp_ofs;
1815 	write_unlock_irqrestore(&ni->size_lock, flags);
1816 	/* Mark the mft record dirty, so it gets written back. */
1817 	flush_dcache_mft_record_page(ctx->ntfs_ino);
1818 	mark_mft_record_dirty(ctx->ntfs_ino);
1819 err_out:
1820 	if (ctx)
1821 		ntfs_attr_put_search_ctx(ctx);
1822 	if (m)
1823 		unmap_mft_record(base_ni);
1824 	ni->runlist.rl = NULL;
1825 	up_write(&ni->runlist.lock);
1826 rl_err_out:
1827 	if (rl) {
1828 		if (ntfs_cluster_free_from_rl(vol, rl) < 0) {
1829 			ntfs_error(vol->sb, "Failed to release allocated "
1830 					"cluster(s) in error code path.  Run "
1831 					"chkdsk to recover the lost "
1832 					"cluster(s).");
1833 			NVolSetErrors(vol);
1834 		}
1835 		ntfs_free(rl);
1836 page_err_out:
1837 		unlock_page(page);
1838 		page_cache_release(page);
1839 	}
1840 	if (err == -EINVAL)
1841 		err = -EIO;
1842 	return err;
1843 }
1844 
1845 /**
1846  * ntfs_attr_extend_allocation - extend the allocated space of an attribute
1847  * @ni:			ntfs inode of the attribute whose allocation to extend
1848  * @new_alloc_size:	new size in bytes to which to extend the allocation to
1849  * @new_data_size:	new size in bytes to which to extend the data to
1850  * @data_start:		beginning of region which is required to be non-sparse
1851  *
1852  * Extend the allocated space of an attribute described by the ntfs inode @ni
1853  * to @new_alloc_size bytes.  If @data_start is -1, the whole extension may be
1854  * implemented as a hole in the file (as long as both the volume and the ntfs
1855  * inode @ni have sparse support enabled).  If @data_start is >= 0, then the
1856  * region between the old allocated size and @data_start - 1 may be made sparse
1857  * but the regions between @data_start and @new_alloc_size must be backed by
1858  * actual clusters.
1859  *
1860  * If @new_data_size is -1, it is ignored.  If it is >= 0, then the data size
1861  * of the attribute is extended to @new_data_size.  Note that the i_size of the
1862  * vfs inode is not updated.  Only the data size in the base attribute record
1863  * is updated.  The caller has to update i_size separately if this is required.
1864  * WARNING: It is a BUG() for @new_data_size to be smaller than the old data
1865  * size as well as for @new_data_size to be greater than @new_alloc_size.
1866  *
1867  * For resident attributes this involves resizing the attribute record and if
1868  * necessary moving it and/or other attributes into extent mft records and/or
1869  * converting the attribute to a non-resident attribute which in turn involves
1870  * extending the allocation of a non-resident attribute as described below.
1871  *
1872  * For non-resident attributes this involves allocating clusters in the data
1873  * zone on the volume (except for regions that are being made sparse) and
1874  * extending the run list to describe the allocated clusters as well as
1875  * updating the mapping pairs array of the attribute.  This in turn involves
1876  * resizing the attribute record and if necessary moving it and/or other
1877  * attributes into extent mft records and/or splitting the attribute record
1878  * into multiple extent attribute records.
1879  *
1880  * Also, the attribute list attribute is updated if present and in some of the
1881  * above cases (the ones where extent mft records/attributes come into play),
1882  * an attribute list attribute is created if not already present.
1883  *
1884  * Return the new allocated size on success and -errno on error.  In the case
1885  * that an error is encountered but a partial extension at least up to
1886  * @data_start (if present) is possible, the allocation is partially extended
1887  * and this is returned.  This means the caller must check the returned size to
1888  * determine if the extension was partial.  If @data_start is -1 then partial
1889  * allocations are not performed.
1890  *
1891  * WARNING: Do not call ntfs_attr_extend_allocation() for $MFT/$DATA.
1892  *
1893  * Locking: This function takes the runlist lock of @ni for writing as well as
1894  * locking the mft record of the base ntfs inode.  These locks are maintained
1895  * throughout execution of the function.  These locks are required so that the
1896  * attribute can be resized safely and so that it can for example be converted
1897  * from resident to non-resident safely.
1898  *
1899  * TODO: At present attribute list attribute handling is not implemented.
1900  *
1901  * TODO: At present it is not safe to call this function for anything other
1902  * than the $DATA attribute(s) of an uncompressed and unencrypted file.
1903  */
1904 s64 ntfs_attr_extend_allocation(ntfs_inode *ni, s64 new_alloc_size,
1905 		const s64 new_data_size, const s64 data_start)
1906 {
1907 	VCN vcn;
1908 	s64 ll, allocated_size, start = data_start;
1909 	struct inode *vi = VFS_I(ni);
1910 	ntfs_volume *vol = ni->vol;
1911 	ntfs_inode *base_ni;
1912 	MFT_RECORD *m;
1913 	ATTR_RECORD *a;
1914 	ntfs_attr_search_ctx *ctx;
1915 	runlist_element *rl, *rl2;
1916 	unsigned long flags;
1917 	int err, mp_size;
1918 	u32 attr_len = 0; /* Silence stupid gcc warning. */
1919 	bool mp_rebuilt;
1920 
1921 #ifdef DEBUG
1922 	read_lock_irqsave(&ni->size_lock, flags);
1923 	allocated_size = ni->allocated_size;
1924 	read_unlock_irqrestore(&ni->size_lock, flags);
1925 	ntfs_debug("Entering for i_ino 0x%lx, attribute type 0x%x, "
1926 			"old_allocated_size 0x%llx, "
1927 			"new_allocated_size 0x%llx, new_data_size 0x%llx, "
1928 			"data_start 0x%llx.", vi->i_ino,
1929 			(unsigned)le32_to_cpu(ni->type),
1930 			(unsigned long long)allocated_size,
1931 			(unsigned long long)new_alloc_size,
1932 			(unsigned long long)new_data_size,
1933 			(unsigned long long)start);
1934 #endif
1935 retry_extend:
1936 	/*
1937 	 * For non-resident attributes, @start and @new_size need to be aligned
1938 	 * to cluster boundaries for allocation purposes.
1939 	 */
1940 	if (NInoNonResident(ni)) {
1941 		if (start > 0)
1942 			start &= ~(s64)vol->cluster_size_mask;
1943 		new_alloc_size = (new_alloc_size + vol->cluster_size - 1) &
1944 				~(s64)vol->cluster_size_mask;
1945 	}
1946 	BUG_ON(new_data_size >= 0 && new_data_size > new_alloc_size);
1947 	/* Check if new size is allowed in $AttrDef. */
1948 	err = ntfs_attr_size_bounds_check(vol, ni->type, new_alloc_size);
1949 	if (unlikely(err)) {
1950 		/* Only emit errors when the write will fail completely. */
1951 		read_lock_irqsave(&ni->size_lock, flags);
1952 		allocated_size = ni->allocated_size;
1953 		read_unlock_irqrestore(&ni->size_lock, flags);
1954 		if (start < 0 || start >= allocated_size) {
1955 			if (err == -ERANGE) {
1956 				ntfs_error(vol->sb, "Cannot extend allocation "
1957 						"of inode 0x%lx, attribute "
1958 						"type 0x%x, because the new "
1959 						"allocation would exceed the "
1960 						"maximum allowed size for "
1961 						"this attribute type.",
1962 						vi->i_ino, (unsigned)
1963 						le32_to_cpu(ni->type));
1964 			} else {
1965 				ntfs_error(vol->sb, "Cannot extend allocation "
1966 						"of inode 0x%lx, attribute "
1967 						"type 0x%x, because this "
1968 						"attribute type is not "
1969 						"defined on the NTFS volume.  "
1970 						"Possible corruption!  You "
1971 						"should run chkdsk!",
1972 						vi->i_ino, (unsigned)
1973 						le32_to_cpu(ni->type));
1974 			}
1975 		}
1976 		/* Translate error code to be POSIX conformant for write(2). */
1977 		if (err == -ERANGE)
1978 			err = -EFBIG;
1979 		else
1980 			err = -EIO;
1981 		return err;
1982 	}
1983 	if (!NInoAttr(ni))
1984 		base_ni = ni;
1985 	else
1986 		base_ni = ni->ext.base_ntfs_ino;
1987 	/*
1988 	 * We will be modifying both the runlist (if non-resident) and the mft
1989 	 * record so lock them both down.
1990 	 */
1991 	down_write(&ni->runlist.lock);
1992 	m = map_mft_record(base_ni);
1993 	if (IS_ERR(m)) {
1994 		err = PTR_ERR(m);
1995 		m = NULL;
1996 		ctx = NULL;
1997 		goto err_out;
1998 	}
1999 	ctx = ntfs_attr_get_search_ctx(base_ni, m);
2000 	if (unlikely(!ctx)) {
2001 		err = -ENOMEM;
2002 		goto err_out;
2003 	}
2004 	read_lock_irqsave(&ni->size_lock, flags);
2005 	allocated_size = ni->allocated_size;
2006 	read_unlock_irqrestore(&ni->size_lock, flags);
2007 	/*
2008 	 * If non-resident, seek to the last extent.  If resident, there is
2009 	 * only one extent, so seek to that.
2010 	 */
2011 	vcn = NInoNonResident(ni) ? allocated_size >> vol->cluster_size_bits :
2012 			0;
2013 	/*
2014 	 * Abort if someone did the work whilst we waited for the locks.  If we
2015 	 * just converted the attribute from resident to non-resident it is
2016 	 * likely that exactly this has happened already.  We cannot quite
2017 	 * abort if we need to update the data size.
2018 	 */
2019 	if (unlikely(new_alloc_size <= allocated_size)) {
2020 		ntfs_debug("Allocated size already exceeds requested size.");
2021 		new_alloc_size = allocated_size;
2022 		if (new_data_size < 0)
2023 			goto done;
2024 		/*
2025 		 * We want the first attribute extent so that we can update the
2026 		 * data size.
2027 		 */
2028 		vcn = 0;
2029 	}
2030 	err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
2031 			CASE_SENSITIVE, vcn, NULL, 0, ctx);
2032 	if (unlikely(err)) {
2033 		if (err == -ENOENT)
2034 			err = -EIO;
2035 		goto err_out;
2036 	}
2037 	m = ctx->mrec;
2038 	a = ctx->attr;
2039 	/* Use goto to reduce indentation. */
2040 	if (a->non_resident)
2041 		goto do_non_resident_extend;
2042 	BUG_ON(NInoNonResident(ni));
2043 	/* The total length of the attribute value. */
2044 	attr_len = le32_to_cpu(a->data.resident.value_length);
2045 	/*
2046 	 * Extend the attribute record to be able to store the new attribute
2047 	 * size.  ntfs_attr_record_resize() will not do anything if the size is
2048 	 * not changing.
2049 	 */
2050 	if (new_alloc_size < vol->mft_record_size &&
2051 			!ntfs_attr_record_resize(m, a,
2052 			le16_to_cpu(a->data.resident.value_offset) +
2053 			new_alloc_size)) {
2054 		/* The resize succeeded! */
2055 		write_lock_irqsave(&ni->size_lock, flags);
2056 		ni->allocated_size = le32_to_cpu(a->length) -
2057 				le16_to_cpu(a->data.resident.value_offset);
2058 		write_unlock_irqrestore(&ni->size_lock, flags);
2059 		if (new_data_size >= 0) {
2060 			BUG_ON(new_data_size < attr_len);
2061 			a->data.resident.value_length =
2062 					cpu_to_le32((u32)new_data_size);
2063 		}
2064 		goto flush_done;
2065 	}
2066 	/*
2067 	 * We have to drop all the locks so we can call
2068 	 * ntfs_attr_make_non_resident().  This could be optimised by try-
2069 	 * locking the first page cache page and only if that fails dropping
2070 	 * the locks, locking the page, and redoing all the locking and
2071 	 * lookups.  While this would be a huge optimisation, it is not worth
2072 	 * it as this is definitely a slow code path.
2073 	 */
2074 	ntfs_attr_put_search_ctx(ctx);
2075 	unmap_mft_record(base_ni);
2076 	up_write(&ni->runlist.lock);
2077 	/*
2078 	 * Not enough space in the mft record, try to make the attribute
2079 	 * non-resident and if successful restart the extension process.
2080 	 */
2081 	err = ntfs_attr_make_non_resident(ni, attr_len);
2082 	if (likely(!err))
2083 		goto retry_extend;
2084 	/*
2085 	 * Could not make non-resident.  If this is due to this not being
2086 	 * permitted for this attribute type or there not being enough space,
2087 	 * try to make other attributes non-resident.  Otherwise fail.
2088 	 */
2089 	if (unlikely(err != -EPERM && err != -ENOSPC)) {
2090 		/* Only emit errors when the write will fail completely. */
2091 		read_lock_irqsave(&ni->size_lock, flags);
2092 		allocated_size = ni->allocated_size;
2093 		read_unlock_irqrestore(&ni->size_lock, flags);
2094 		if (start < 0 || start >= allocated_size)
2095 			ntfs_error(vol->sb, "Cannot extend allocation of "
2096 					"inode 0x%lx, attribute type 0x%x, "
2097 					"because the conversion from resident "
2098 					"to non-resident attribute failed "
2099 					"with error code %i.", vi->i_ino,
2100 					(unsigned)le32_to_cpu(ni->type), err);
2101 		if (err != -ENOMEM)
2102 			err = -EIO;
2103 		goto conv_err_out;
2104 	}
2105 	/* TODO: Not implemented from here, abort. */
2106 	read_lock_irqsave(&ni->size_lock, flags);
2107 	allocated_size = ni->allocated_size;
2108 	read_unlock_irqrestore(&ni->size_lock, flags);
2109 	if (start < 0 || start >= allocated_size) {
2110 		if (err == -ENOSPC)
2111 			ntfs_error(vol->sb, "Not enough space in the mft "
2112 					"record/on disk for the non-resident "
2113 					"attribute value.  This case is not "
2114 					"implemented yet.");
2115 		else /* if (err == -EPERM) */
2116 			ntfs_error(vol->sb, "This attribute type may not be "
2117 					"non-resident.  This case is not "
2118 					"implemented yet.");
2119 	}
2120 	err = -EOPNOTSUPP;
2121 	goto conv_err_out;
2122 #if 0
2123 	// TODO: Attempt to make other attributes non-resident.
2124 	if (!err)
2125 		goto do_resident_extend;
2126 	/*
2127 	 * Both the attribute list attribute and the standard information
2128 	 * attribute must remain in the base inode.  Thus, if this is one of
2129 	 * these attributes, we have to try to move other attributes out into
2130 	 * extent mft records instead.
2131 	 */
2132 	if (ni->type == AT_ATTRIBUTE_LIST ||
2133 			ni->type == AT_STANDARD_INFORMATION) {
2134 		// TODO: Attempt to move other attributes into extent mft
2135 		// records.
2136 		err = -EOPNOTSUPP;
2137 		if (!err)
2138 			goto do_resident_extend;
2139 		goto err_out;
2140 	}
2141 	// TODO: Attempt to move this attribute to an extent mft record, but
2142 	// only if it is not already the only attribute in an mft record in
2143 	// which case there would be nothing to gain.
2144 	err = -EOPNOTSUPP;
2145 	if (!err)
2146 		goto do_resident_extend;
2147 	/* There is nothing we can do to make enough space. )-: */
2148 	goto err_out;
2149 #endif
2150 do_non_resident_extend:
2151 	BUG_ON(!NInoNonResident(ni));
2152 	if (new_alloc_size == allocated_size) {
2153 		BUG_ON(vcn);
2154 		goto alloc_done;
2155 	}
2156 	/*
2157 	 * If the data starts after the end of the old allocation, this is a
2158 	 * $DATA attribute and sparse attributes are enabled on the volume and
2159 	 * for this inode, then create a sparse region between the old
2160 	 * allocated size and the start of the data.  Otherwise simply proceed
2161 	 * with filling the whole space between the old allocated size and the
2162 	 * new allocated size with clusters.
2163 	 */
2164 	if ((start >= 0 && start <= allocated_size) || ni->type != AT_DATA ||
2165 			!NVolSparseEnabled(vol) || NInoSparseDisabled(ni))
2166 		goto skip_sparse;
2167 	// TODO: This is not implemented yet.  We just fill in with real
2168 	// clusters for now...
2169 	ntfs_debug("Inserting holes is not-implemented yet.  Falling back to "
2170 			"allocating real clusters instead.");
2171 skip_sparse:
2172 	rl = ni->runlist.rl;
2173 	if (likely(rl)) {
2174 		/* Seek to the end of the runlist. */
2175 		while (rl->length)
2176 			rl++;
2177 	}
2178 	/* If this attribute extent is not mapped, map it now. */
2179 	if (unlikely(!rl || rl->lcn == LCN_RL_NOT_MAPPED ||
2180 			(rl->lcn == LCN_ENOENT && rl > ni->runlist.rl &&
2181 			(rl-1)->lcn == LCN_RL_NOT_MAPPED))) {
2182 		if (!rl && !allocated_size)
2183 			goto first_alloc;
2184 		rl = ntfs_mapping_pairs_decompress(vol, a, ni->runlist.rl);
2185 		if (IS_ERR(rl)) {
2186 			err = PTR_ERR(rl);
2187 			if (start < 0 || start >= allocated_size)
2188 				ntfs_error(vol->sb, "Cannot extend allocation "
2189 						"of inode 0x%lx, attribute "
2190 						"type 0x%x, because the "
2191 						"mapping of a runlist "
2192 						"fragment failed with error "
2193 						"code %i.", vi->i_ino,
2194 						(unsigned)le32_to_cpu(ni->type),
2195 						err);
2196 			if (err != -ENOMEM)
2197 				err = -EIO;
2198 			goto err_out;
2199 		}
2200 		ni->runlist.rl = rl;
2201 		/* Seek to the end of the runlist. */
2202 		while (rl->length)
2203 			rl++;
2204 	}
2205 	/*
2206 	 * We now know the runlist of the last extent is mapped and @rl is at
2207 	 * the end of the runlist.  We want to begin allocating clusters
2208 	 * starting at the last allocated cluster to reduce fragmentation.  If
2209 	 * there are no valid LCNs in the attribute we let the cluster
2210 	 * allocator choose the starting cluster.
2211 	 */
2212 	/* If the last LCN is a hole or simillar seek back to last real LCN. */
2213 	while (rl->lcn < 0 && rl > ni->runlist.rl)
2214 		rl--;
2215 first_alloc:
2216 	// FIXME: Need to implement partial allocations so at least part of the
2217 	// write can be performed when start >= 0.  (Needed for POSIX write(2)
2218 	// conformance.)
2219 	rl2 = ntfs_cluster_alloc(vol, allocated_size >> vol->cluster_size_bits,
2220 			(new_alloc_size - allocated_size) >>
2221 			vol->cluster_size_bits, (rl && (rl->lcn >= 0)) ?
2222 			rl->lcn + rl->length : -1, DATA_ZONE, true);
2223 	if (IS_ERR(rl2)) {
2224 		err = PTR_ERR(rl2);
2225 		if (start < 0 || start >= allocated_size)
2226 			ntfs_error(vol->sb, "Cannot extend allocation of "
2227 					"inode 0x%lx, attribute type 0x%x, "
2228 					"because the allocation of clusters "
2229 					"failed with error code %i.", vi->i_ino,
2230 					(unsigned)le32_to_cpu(ni->type), err);
2231 		if (err != -ENOMEM && err != -ENOSPC)
2232 			err = -EIO;
2233 		goto err_out;
2234 	}
2235 	rl = ntfs_runlists_merge(ni->runlist.rl, rl2);
2236 	if (IS_ERR(rl)) {
2237 		err = PTR_ERR(rl);
2238 		if (start < 0 || start >= allocated_size)
2239 			ntfs_error(vol->sb, "Cannot extend allocation of "
2240 					"inode 0x%lx, attribute type 0x%x, "
2241 					"because the runlist merge failed "
2242 					"with error code %i.", vi->i_ino,
2243 					(unsigned)le32_to_cpu(ni->type), err);
2244 		if (err != -ENOMEM)
2245 			err = -EIO;
2246 		if (ntfs_cluster_free_from_rl(vol, rl2)) {
2247 			ntfs_error(vol->sb, "Failed to release allocated "
2248 					"cluster(s) in error code path.  Run "
2249 					"chkdsk to recover the lost "
2250 					"cluster(s).");
2251 			NVolSetErrors(vol);
2252 		}
2253 		ntfs_free(rl2);
2254 		goto err_out;
2255 	}
2256 	ni->runlist.rl = rl;
2257 	ntfs_debug("Allocated 0x%llx clusters.", (long long)(new_alloc_size -
2258 			allocated_size) >> vol->cluster_size_bits);
2259 	/* Find the runlist element with which the attribute extent starts. */
2260 	ll = sle64_to_cpu(a->data.non_resident.lowest_vcn);
2261 	rl2 = ntfs_rl_find_vcn_nolock(rl, ll);
2262 	BUG_ON(!rl2);
2263 	BUG_ON(!rl2->length);
2264 	BUG_ON(rl2->lcn < LCN_HOLE);
2265 	mp_rebuilt = false;
2266 	/* Get the size for the new mapping pairs array for this extent. */
2267 	mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, -1);
2268 	if (unlikely(mp_size <= 0)) {
2269 		err = mp_size;
2270 		if (start < 0 || start >= allocated_size)
2271 			ntfs_error(vol->sb, "Cannot extend allocation of "
2272 					"inode 0x%lx, attribute type 0x%x, "
2273 					"because determining the size for the "
2274 					"mapping pairs failed with error code "
2275 					"%i.", vi->i_ino,
2276 					(unsigned)le32_to_cpu(ni->type), err);
2277 		err = -EIO;
2278 		goto undo_alloc;
2279 	}
2280 	/* Extend the attribute record to fit the bigger mapping pairs array. */
2281 	attr_len = le32_to_cpu(a->length);
2282 	err = ntfs_attr_record_resize(m, a, mp_size +
2283 			le16_to_cpu(a->data.non_resident.mapping_pairs_offset));
2284 	if (unlikely(err)) {
2285 		BUG_ON(err != -ENOSPC);
2286 		// TODO: Deal with this by moving this extent to a new mft
2287 		// record or by starting a new extent in a new mft record,
2288 		// possibly by extending this extent partially and filling it
2289 		// and creating a new extent for the remainder, or by making
2290 		// other attributes non-resident and/or by moving other
2291 		// attributes out of this mft record.
2292 		if (start < 0 || start >= allocated_size)
2293 			ntfs_error(vol->sb, "Not enough space in the mft "
2294 					"record for the extended attribute "
2295 					"record.  This case is not "
2296 					"implemented yet.");
2297 		err = -EOPNOTSUPP;
2298 		goto undo_alloc;
2299 	}
2300 	mp_rebuilt = true;
2301 	/* Generate the mapping pairs array directly into the attr record. */
2302 	err = ntfs_mapping_pairs_build(vol, (u8*)a +
2303 			le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
2304 			mp_size, rl2, ll, -1, NULL);
2305 	if (unlikely(err)) {
2306 		if (start < 0 || start >= allocated_size)
2307 			ntfs_error(vol->sb, "Cannot extend allocation of "
2308 					"inode 0x%lx, attribute type 0x%x, "
2309 					"because building the mapping pairs "
2310 					"failed with error code %i.", vi->i_ino,
2311 					(unsigned)le32_to_cpu(ni->type), err);
2312 		err = -EIO;
2313 		goto undo_alloc;
2314 	}
2315 	/* Update the highest_vcn. */
2316 	a->data.non_resident.highest_vcn = cpu_to_sle64((new_alloc_size >>
2317 			vol->cluster_size_bits) - 1);
2318 	/*
2319 	 * We now have extended the allocated size of the attribute.  Reflect
2320 	 * this in the ntfs_inode structure and the attribute record.
2321 	 */
2322 	if (a->data.non_resident.lowest_vcn) {
2323 		/*
2324 		 * We are not in the first attribute extent, switch to it, but
2325 		 * first ensure the changes will make it to disk later.
2326 		 */
2327 		flush_dcache_mft_record_page(ctx->ntfs_ino);
2328 		mark_mft_record_dirty(ctx->ntfs_ino);
2329 		ntfs_attr_reinit_search_ctx(ctx);
2330 		err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
2331 				CASE_SENSITIVE, 0, NULL, 0, ctx);
2332 		if (unlikely(err))
2333 			goto restore_undo_alloc;
2334 		/* @m is not used any more so no need to set it. */
2335 		a = ctx->attr;
2336 	}
2337 	write_lock_irqsave(&ni->size_lock, flags);
2338 	ni->allocated_size = new_alloc_size;
2339 	a->data.non_resident.allocated_size = cpu_to_sle64(new_alloc_size);
2340 	/*
2341 	 * FIXME: This would fail if @ni is a directory, $MFT, or an index,
2342 	 * since those can have sparse/compressed set.  For example can be
2343 	 * set compressed even though it is not compressed itself and in that
2344 	 * case the bit means that files are to be created compressed in the
2345 	 * directory...  At present this is ok as this code is only called for
2346 	 * regular files, and only for their $DATA attribute(s).
2347 	 * FIXME: The calculation is wrong if we created a hole above.  For now
2348 	 * it does not matter as we never create holes.
2349 	 */
2350 	if (NInoSparse(ni) || NInoCompressed(ni)) {
2351 		ni->itype.compressed.size += new_alloc_size - allocated_size;
2352 		a->data.non_resident.compressed_size =
2353 				cpu_to_sle64(ni->itype.compressed.size);
2354 		vi->i_blocks = ni->itype.compressed.size >> 9;
2355 	} else
2356 		vi->i_blocks = new_alloc_size >> 9;
2357 	write_unlock_irqrestore(&ni->size_lock, flags);
2358 alloc_done:
2359 	if (new_data_size >= 0) {
2360 		BUG_ON(new_data_size <
2361 				sle64_to_cpu(a->data.non_resident.data_size));
2362 		a->data.non_resident.data_size = cpu_to_sle64(new_data_size);
2363 	}
2364 flush_done:
2365 	/* Ensure the changes make it to disk. */
2366 	flush_dcache_mft_record_page(ctx->ntfs_ino);
2367 	mark_mft_record_dirty(ctx->ntfs_ino);
2368 done:
2369 	ntfs_attr_put_search_ctx(ctx);
2370 	unmap_mft_record(base_ni);
2371 	up_write(&ni->runlist.lock);
2372 	ntfs_debug("Done, new_allocated_size 0x%llx.",
2373 			(unsigned long long)new_alloc_size);
2374 	return new_alloc_size;
2375 restore_undo_alloc:
2376 	if (start < 0 || start >= allocated_size)
2377 		ntfs_error(vol->sb, "Cannot complete extension of allocation "
2378 				"of inode 0x%lx, attribute type 0x%x, because "
2379 				"lookup of first attribute extent failed with "
2380 				"error code %i.", vi->i_ino,
2381 				(unsigned)le32_to_cpu(ni->type), err);
2382 	if (err == -ENOENT)
2383 		err = -EIO;
2384 	ntfs_attr_reinit_search_ctx(ctx);
2385 	if (ntfs_attr_lookup(ni->type, ni->name, ni->name_len, CASE_SENSITIVE,
2386 			allocated_size >> vol->cluster_size_bits, NULL, 0,
2387 			ctx)) {
2388 		ntfs_error(vol->sb, "Failed to find last attribute extent of "
2389 				"attribute in error code path.  Run chkdsk to "
2390 				"recover.");
2391 		write_lock_irqsave(&ni->size_lock, flags);
2392 		ni->allocated_size = new_alloc_size;
2393 		/*
2394 		 * FIXME: This would fail if @ni is a directory...  See above.
2395 		 * FIXME: The calculation is wrong if we created a hole above.
2396 		 * For now it does not matter as we never create holes.
2397 		 */
2398 		if (NInoSparse(ni) || NInoCompressed(ni)) {
2399 			ni->itype.compressed.size += new_alloc_size -
2400 					allocated_size;
2401 			vi->i_blocks = ni->itype.compressed.size >> 9;
2402 		} else
2403 			vi->i_blocks = new_alloc_size >> 9;
2404 		write_unlock_irqrestore(&ni->size_lock, flags);
2405 		ntfs_attr_put_search_ctx(ctx);
2406 		unmap_mft_record(base_ni);
2407 		up_write(&ni->runlist.lock);
2408 		/*
2409 		 * The only thing that is now wrong is the allocated size of the
2410 		 * base attribute extent which chkdsk should be able to fix.
2411 		 */
2412 		NVolSetErrors(vol);
2413 		return err;
2414 	}
2415 	ctx->attr->data.non_resident.highest_vcn = cpu_to_sle64(
2416 			(allocated_size >> vol->cluster_size_bits) - 1);
2417 undo_alloc:
2418 	ll = allocated_size >> vol->cluster_size_bits;
2419 	if (ntfs_cluster_free(ni, ll, -1, ctx) < 0) {
2420 		ntfs_error(vol->sb, "Failed to release allocated cluster(s) "
2421 				"in error code path.  Run chkdsk to recover "
2422 				"the lost cluster(s).");
2423 		NVolSetErrors(vol);
2424 	}
2425 	m = ctx->mrec;
2426 	a = ctx->attr;
2427 	/*
2428 	 * If the runlist truncation fails and/or the search context is no
2429 	 * longer valid, we cannot resize the attribute record or build the
2430 	 * mapping pairs array thus we mark the inode bad so that no access to
2431 	 * the freed clusters can happen.
2432 	 */
2433 	if (ntfs_rl_truncate_nolock(vol, &ni->runlist, ll) || IS_ERR(m)) {
2434 		ntfs_error(vol->sb, "Failed to %s in error code path.  Run "
2435 				"chkdsk to recover.", IS_ERR(m) ?
2436 				"restore attribute search context" :
2437 				"truncate attribute runlist");
2438 		NVolSetErrors(vol);
2439 	} else if (mp_rebuilt) {
2440 		if (ntfs_attr_record_resize(m, a, attr_len)) {
2441 			ntfs_error(vol->sb, "Failed to restore attribute "
2442 					"record in error code path.  Run "
2443 					"chkdsk to recover.");
2444 			NVolSetErrors(vol);
2445 		} else /* if (success) */ {
2446 			if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu(
2447 					a->data.non_resident.
2448 					mapping_pairs_offset), attr_len -
2449 					le16_to_cpu(a->data.non_resident.
2450 					mapping_pairs_offset), rl2, ll, -1,
2451 					NULL)) {
2452 				ntfs_error(vol->sb, "Failed to restore "
2453 						"mapping pairs array in error "
2454 						"code path.  Run chkdsk to "
2455 						"recover.");
2456 				NVolSetErrors(vol);
2457 			}
2458 			flush_dcache_mft_record_page(ctx->ntfs_ino);
2459 			mark_mft_record_dirty(ctx->ntfs_ino);
2460 		}
2461 	}
2462 err_out:
2463 	if (ctx)
2464 		ntfs_attr_put_search_ctx(ctx);
2465 	if (m)
2466 		unmap_mft_record(base_ni);
2467 	up_write(&ni->runlist.lock);
2468 conv_err_out:
2469 	ntfs_debug("Failed.  Returning error code %i.", err);
2470 	return err;
2471 }
2472 
2473 /**
2474  * ntfs_attr_set - fill (a part of) an attribute with a byte
2475  * @ni:		ntfs inode describing the attribute to fill
2476  * @ofs:	offset inside the attribute at which to start to fill
2477  * @cnt:	number of bytes to fill
2478  * @val:	the unsigned 8-bit value with which to fill the attribute
2479  *
2480  * Fill @cnt bytes of the attribute described by the ntfs inode @ni starting at
2481  * byte offset @ofs inside the attribute with the constant byte @val.
2482  *
2483  * This function is effectively like memset() applied to an ntfs attribute.
2484  * Note thie function actually only operates on the page cache pages belonging
2485  * to the ntfs attribute and it marks them dirty after doing the memset().
2486  * Thus it relies on the vm dirty page write code paths to cause the modified
2487  * pages to be written to the mft record/disk.
2488  *
2489  * Return 0 on success and -errno on error.  An error code of -ESPIPE means
2490  * that @ofs + @cnt were outside the end of the attribute and no write was
2491  * performed.
2492  */
2493 int ntfs_attr_set(ntfs_inode *ni, const s64 ofs, const s64 cnt, const u8 val)
2494 {
2495 	ntfs_volume *vol = ni->vol;
2496 	struct address_space *mapping;
2497 	struct page *page;
2498 	u8 *kaddr;
2499 	pgoff_t idx, end;
2500 	unsigned start_ofs, end_ofs, size;
2501 
2502 	ntfs_debug("Entering for ofs 0x%llx, cnt 0x%llx, val 0x%hx.",
2503 			(long long)ofs, (long long)cnt, val);
2504 	BUG_ON(ofs < 0);
2505 	BUG_ON(cnt < 0);
2506 	if (!cnt)
2507 		goto done;
2508 	/*
2509 	 * FIXME: Compressed and encrypted attributes are not supported when
2510 	 * writing and we should never have gotten here for them.
2511 	 */
2512 	BUG_ON(NInoCompressed(ni));
2513 	BUG_ON(NInoEncrypted(ni));
2514 	mapping = VFS_I(ni)->i_mapping;
2515 	/* Work out the starting index and page offset. */
2516 	idx = ofs >> PAGE_CACHE_SHIFT;
2517 	start_ofs = ofs & ~PAGE_CACHE_MASK;
2518 	/* Work out the ending index and page offset. */
2519 	end = ofs + cnt;
2520 	end_ofs = end & ~PAGE_CACHE_MASK;
2521 	/* If the end is outside the inode size return -ESPIPE. */
2522 	if (unlikely(end > i_size_read(VFS_I(ni)))) {
2523 		ntfs_error(vol->sb, "Request exceeds end of attribute.");
2524 		return -ESPIPE;
2525 	}
2526 	end >>= PAGE_CACHE_SHIFT;
2527 	/* If there is a first partial page, need to do it the slow way. */
2528 	if (start_ofs) {
2529 		page = read_mapping_page(mapping, idx, NULL);
2530 		if (IS_ERR(page)) {
2531 			ntfs_error(vol->sb, "Failed to read first partial "
2532 					"page (error, index 0x%lx).", idx);
2533 			return PTR_ERR(page);
2534 		}
2535 		/*
2536 		 * If the last page is the same as the first page, need to
2537 		 * limit the write to the end offset.
2538 		 */
2539 		size = PAGE_CACHE_SIZE;
2540 		if (idx == end)
2541 			size = end_ofs;
2542 		kaddr = kmap_atomic(page, KM_USER0);
2543 		memset(kaddr + start_ofs, val, size - start_ofs);
2544 		flush_dcache_page(page);
2545 		kunmap_atomic(kaddr, KM_USER0);
2546 		set_page_dirty(page);
2547 		page_cache_release(page);
2548 		balance_dirty_pages_ratelimited(mapping);
2549 		cond_resched();
2550 		if (idx == end)
2551 			goto done;
2552 		idx++;
2553 	}
2554 	/* Do the whole pages the fast way. */
2555 	for (; idx < end; idx++) {
2556 		/* Find or create the current page.  (The page is locked.) */
2557 		page = grab_cache_page(mapping, idx);
2558 		if (unlikely(!page)) {
2559 			ntfs_error(vol->sb, "Insufficient memory to grab "
2560 					"page (index 0x%lx).", idx);
2561 			return -ENOMEM;
2562 		}
2563 		kaddr = kmap_atomic(page, KM_USER0);
2564 		memset(kaddr, val, PAGE_CACHE_SIZE);
2565 		flush_dcache_page(page);
2566 		kunmap_atomic(kaddr, KM_USER0);
2567 		/*
2568 		 * If the page has buffers, mark them uptodate since buffer
2569 		 * state and not page state is definitive in 2.6 kernels.
2570 		 */
2571 		if (page_has_buffers(page)) {
2572 			struct buffer_head *bh, *head;
2573 
2574 			bh = head = page_buffers(page);
2575 			do {
2576 				set_buffer_uptodate(bh);
2577 			} while ((bh = bh->b_this_page) != head);
2578 		}
2579 		/* Now that buffers are uptodate, set the page uptodate, too. */
2580 		SetPageUptodate(page);
2581 		/*
2582 		 * Set the page and all its buffers dirty and mark the inode
2583 		 * dirty, too.  The VM will write the page later on.
2584 		 */
2585 		set_page_dirty(page);
2586 		/* Finally unlock and release the page. */
2587 		unlock_page(page);
2588 		page_cache_release(page);
2589 		balance_dirty_pages_ratelimited(mapping);
2590 		cond_resched();
2591 	}
2592 	/* If there is a last partial page, need to do it the slow way. */
2593 	if (end_ofs) {
2594 		page = read_mapping_page(mapping, idx, NULL);
2595 		if (IS_ERR(page)) {
2596 			ntfs_error(vol->sb, "Failed to read last partial page "
2597 					"(error, index 0x%lx).", idx);
2598 			return PTR_ERR(page);
2599 		}
2600 		kaddr = kmap_atomic(page, KM_USER0);
2601 		memset(kaddr, val, end_ofs);
2602 		flush_dcache_page(page);
2603 		kunmap_atomic(kaddr, KM_USER0);
2604 		set_page_dirty(page);
2605 		page_cache_release(page);
2606 		balance_dirty_pages_ratelimited(mapping);
2607 		cond_resched();
2608 	}
2609 done:
2610 	ntfs_debug("Done.");
2611 	return 0;
2612 }
2613 
2614 #endif /* NTFS_RW */
2615