xref: /haiku/src/add-ons/kernel/file_systems/ntfs/libntfs/mft.c (revision a906d0a031e721e2f2ec9d95274103e74a3a774f)
1 /**
2  * mft.c - Mft record handling code. Originated from the Linux-NTFS project.
3  *
4  * Copyright (c) 2000-2004 Anton Altaparmakov
5  * Copyright (c) 2004-2005 Richard Russon
6  * Copyright (c) 2004-2008 Szabolcs Szakacsits
7  * Copyright (c)      2005 Yura Pakhuchiy
8  *
9  * This program/include file is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as published
11  * by the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program/include file is distributed in the hope that it will be
15  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
16  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program (in the main directory of the NTFS-3G
21  * distribution in the file COPYING); if not, write to the Free Software
22  * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24 
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 
29 #ifdef HAVE_STDLIB_H
30 #include <stdlib.h>
31 #endif
32 #ifdef HAVE_STDIO_H
33 #include <stdio.h>
34 #endif
35 #ifdef HAVE_ERRNO_H
36 #include <errno.h>
37 #endif
38 #ifdef HAVE_STRING_H
39 #include <string.h>
40 #endif
41 #ifdef HAVE_LIMITS_H
42 #include <limits.h>
43 #endif
44 #include <time.h>
45 
46 #include "compat.h"
47 #include "types.h"
48 #include "device.h"
49 #include "debug.h"
50 #include "bitmap.h"
51 #include "attrib.h"
52 #include "inode.h"
53 #include "volume.h"
54 #include "layout.h"
55 #include "lcnalloc.h"
56 #include "mft.h"
57 #include "logging.h"
58 #include "misc.h"
59 
60 /**
61  * ntfs_mft_records_read - read records from the mft from disk
62  * @vol:	volume to read from
63  * @mref:	starting mft record number to read
64  * @count:	number of mft records to read
65  * @b:		output data buffer
66  *
67  * Read @count mft records starting at @mref from volume @vol into buffer
68  * @b. Return 0 on success or -1 on error, with errno set to the error
69  * code.
70  *
71  * If any of the records exceed the initialized size of the $MFT/$DATA
72  * attribute, i.e. they cannot possibly be allocated mft records, assume this
73  * is a bug and return error code ESPIPE.
74  *
75  * The read mft records are mst deprotected and are hence ready to use. The
76  * caller should check each record with is_baad_record() in case mst
77  * deprotection failed.
78  *
79  * NOTE: @b has to be at least of size @count * vol->mft_record_size.
80  */
81 int ntfs_mft_records_read(const ntfs_volume *vol, const MFT_REF mref,
82 		const s64 count, MFT_RECORD *b)
83 {
84 	s64 br;
85 	VCN m;
86 
87 	ntfs_log_trace("inode %llu\n", (unsigned long long)MREF(mref));
88 
89 	if (!vol || !vol->mft_na || !b || count < 0) {
90 		errno = EINVAL;
91 		ntfs_log_perror("%s: b=%p  count=%lld  mft=%llu", __FUNCTION__,
92 			b, (long long)count, (unsigned long long)MREF(mref));
93 		return -1;
94 	}
95 	m = MREF(mref);
96 	/* Refuse to read non-allocated mft records. */
97 	if (m + count > vol->mft_na->initialized_size >>
98 			vol->mft_record_size_bits) {
99 		errno = ESPIPE;
100 		ntfs_log_perror("Trying to read non-allocated mft records "
101 				"(%lld > %lld)", (long long)m + count,
102 				(long long)vol->mft_na->initialized_size >>
103 				vol->mft_record_size_bits);
104 		return -1;
105 	}
106 	br = ntfs_attr_mst_pread(vol->mft_na, m << vol->mft_record_size_bits,
107 			count, vol->mft_record_size, b);
108 	if (br != count) {
109 		if (br != -1)
110 			errno = EIO;
111 		ntfs_log_perror("Failed to read of MFT, mft=%llu count=%lld "
112 				"br=%lld", (long long)m, (long long)count,
113 				(long long)br);
114 		return -1;
115 	}
116 	return 0;
117 }
118 
119 /**
120  * ntfs_mft_records_write - write mft records to disk
121  * @vol:	volume to write to
122  * @mref:	starting mft record number to write
123  * @count:	number of mft records to write
124  * @b:		data buffer containing the mft records to write
125  *
126  * Write @count mft records starting at @mref from data buffer @b to volume
127  * @vol. Return 0 on success or -1 on error, with errno set to the error code.
128  *
129  * If any of the records exceed the initialized size of the $MFT/$DATA
130  * attribute, i.e. they cannot possibly be allocated mft records, assume this
131  * is a bug and return error code ESPIPE.
132  *
133  * Before the mft records are written, they are mst protected. After the write,
134  * they are deprotected again, thus resulting in an increase in the update
135  * sequence number inside the data buffer @b.
136  *
137  * If any mft records are written which are also represented in the mft mirror
138  * $MFTMirr, we make a copy of the relevant parts of the data buffer @b into a
139  * temporary buffer before we do the actual write. Then if at least one mft
140  * record was successfully written, we write the appropriate mft records from
141  * the copied buffer to the mft mirror, too.
142  */
143 int ntfs_mft_records_write(const ntfs_volume *vol, const MFT_REF mref,
144 		const s64 count, MFT_RECORD *b)
145 {
146 	s64 bw;
147 	VCN m;
148 	void *bmirr = NULL;
149 	int cnt = 0, res = 0;
150 
151 	if (!vol || !vol->mft_na || vol->mftmirr_size <= 0 || !b || count < 0) {
152 		errno = EINVAL;
153 		return -1;
154 	}
155 	m = MREF(mref);
156 	/* Refuse to write non-allocated mft records. */
157 	if (m + count > vol->mft_na->initialized_size >>
158 			vol->mft_record_size_bits) {
159 		errno = ESPIPE;
160 		ntfs_log_perror("Trying to write non-allocated mft records "
161 				"(%lld > %lld)", (long long)m + count,
162 				(long long)vol->mft_na->initialized_size >>
163 				vol->mft_record_size_bits);
164 		return -1;
165 	}
166 	if (m < vol->mftmirr_size) {
167 		if (!vol->mftmirr_na) {
168 			errno = EINVAL;
169 			return -1;
170 		}
171 		cnt = vol->mftmirr_size - m;
172 		if (cnt > count)
173 			cnt = count;
174 		bmirr = ntfs_malloc(cnt * vol->mft_record_size);
175 		if (!bmirr)
176 			return -1;
177 		memcpy(bmirr, b, cnt * vol->mft_record_size);
178 	}
179 	bw = ntfs_attr_mst_pwrite(vol->mft_na, m << vol->mft_record_size_bits,
180 			count, vol->mft_record_size, b);
181 	if (bw != count) {
182 		if (bw != -1)
183 			errno = EIO;
184 		if (bw >= 0)
185 			ntfs_log_debug("Error: partial write while writing $Mft "
186 					"record(s)!\n");
187 		else
188 			ntfs_log_perror("Error writing $Mft record(s)");
189 		res = errno;
190 	}
191 	if (bmirr && bw > 0) {
192 		if (bw < cnt)
193 			cnt = bw;
194 		bw = ntfs_attr_mst_pwrite(vol->mftmirr_na,
195 				m << vol->mft_record_size_bits, cnt,
196 				vol->mft_record_size, bmirr);
197 		if (bw != cnt) {
198 			if (bw != -1)
199 				errno = EIO;
200 			ntfs_log_debug("Error: failed to sync $MFTMirr! Run "
201 					"chkdsk.\n");
202 			res = errno;
203 		}
204 	}
205 	free(bmirr);
206 	if (!res)
207 		return res;
208 	errno = res;
209 	return -1;
210 }
211 
212 int ntfs_mft_record_check(const ntfs_volume *vol, const MFT_REF mref,
213 			  MFT_RECORD *m)
214 {
215 	ATTR_RECORD *a;
216 	int ret = -1;
217 
218 	if (!ntfs_is_file_record(m->magic)) {
219 		ntfs_log_error("Record %llu has no FILE magic (0x%x)\n",
220 			       (unsigned long long)MREF(mref), *(le32 *)m);
221 		goto err_out;
222 	}
223 
224 	if (le32_to_cpu(m->bytes_allocated) != vol->mft_record_size) {
225 		ntfs_log_error("Record %llu has corrupt allocation size "
226 			       "(%u <> %u)\n", (unsigned long long)MREF(mref),
227 			       vol->mft_record_size,
228 			       le32_to_cpu(m->bytes_allocated));
229 		goto err_out;
230 	}
231 
232 	a = (ATTR_RECORD *)((char *)m + le16_to_cpu(m->attrs_offset));
233 	if (p2n(a) < p2n(m) || (char *)a > (char *)m + vol->mft_record_size) {
234 		ntfs_log_error("Record %llu is corrupt\n",
235 			       (unsigned long long)MREF(mref));
236 		goto err_out;
237 	}
238 
239 	ret = 0;
240 err_out:
241 	if (ret)
242 		errno = EIO;
243 	return ret;
244 }
245 
246 /**
247  * ntfs_file_record_read - read a FILE record from the mft from disk
248  * @vol:	volume to read from
249  * @mref:	mft reference specifying mft record to read
250  * @mrec:	address of pointer in which to return the mft record
251  * @attr:	address of pointer in which to return the first attribute
252  *
253  * Read a FILE record from the mft of @vol from the storage medium. @mref
254  * specifies the mft record to read, including the sequence number, which can
255  * be 0 if no sequence number checking is to be performed.
256  *
257  * The function allocates a buffer large enough to hold the mft record and
258  * reads the record into the buffer (mst deprotecting it in the process).
259  * *@mrec is then set to point to the buffer.
260  *
261  * If @attr is not NULL, *@attr is set to point to the first attribute in the
262  * mft record, i.e. *@attr is a pointer into *@mrec.
263  *
264  * Return 0 on success, or -1 on error, with errno set to the error code.
265  *
266  * The read mft record is checked for having the magic FILE,
267  * and for having a matching sequence number (if MSEQNO(*@mref) != 0).
268  * If either of these fails, -1 is returned and errno is set to EIO. If you get
269  * this, but you still want to read the mft record (e.g. in order to correct
270  * it), use ntfs_mft_record_read() directly.
271  *
272  * Note: Caller has to free *@mrec when finished.
273  *
274  * Note: We do not check if the mft record is flagged in use. The caller can
275  *	 check if desired.
276  */
277 int ntfs_file_record_read(const ntfs_volume *vol, const MFT_REF mref,
278 		MFT_RECORD **mrec, ATTR_RECORD **attr)
279 {
280 	MFT_RECORD *m;
281 
282 	if (!vol || !mrec) {
283 		errno = EINVAL;
284 		ntfs_log_perror("%s: mrec=%p", __FUNCTION__, mrec);
285 		return -1;
286 	}
287 
288 	m = *mrec;
289 	if (!m) {
290 		m = ntfs_malloc(vol->mft_record_size);
291 		if (!m)
292 			return -1;
293 	}
294 	if (ntfs_mft_record_read(vol, mref, m))
295 		goto err_out;
296 
297 	if (ntfs_mft_record_check(vol, mref, m))
298 		goto err_out;
299 
300 	if (MSEQNO(mref) && MSEQNO(mref) != le16_to_cpu(m->sequence_number)) {
301 		ntfs_log_error("Record %llu has wrong SeqNo (%d <> %d)\n",
302 			       (unsigned long long)MREF(mref), MSEQNO(mref),
303 			       le16_to_cpu(m->sequence_number));
304 		errno = EIO;
305 		goto err_out;
306 	}
307 	*mrec = m;
308 	if (attr)
309 		*attr = (ATTR_RECORD*)((char*)m + le16_to_cpu(m->attrs_offset));
310 	return 0;
311 err_out:
312 	if (m != *mrec)
313 		free(m);
314 	return -1;
315 }
316 
317 /**
318  * ntfs_mft_record_layout - layout an mft record into a memory buffer
319  * @vol:	volume to which the mft record will belong
320  * @mref:	mft reference specifying the mft record number
321  * @mrec:	destination buffer of size >= @vol->mft_record_size bytes
322  *
323  * Layout an empty, unused mft record with the mft reference @mref into the
324  * buffer @m.  The volume @vol is needed because the mft record structure was
325  * modified in NTFS 3.1 so we need to know which volume version this mft record
326  * will be used on.
327  *
328  * On success return 0 and on error return -1 with errno set to the error code.
329  */
330 int ntfs_mft_record_layout(const ntfs_volume *vol, const MFT_REF mref,
331 		MFT_RECORD *mrec)
332 {
333 	ATTR_RECORD *a;
334 
335 	if (!vol || !mrec) {
336 		errno = EINVAL;
337 		ntfs_log_perror("%s: mrec=%p", __FUNCTION__, mrec);
338 		return -1;
339 	}
340 	/* Aligned to 2-byte boundary. */
341 	if (vol->major_ver < 3 || (vol->major_ver == 3 && !vol->minor_ver))
342 		mrec->usa_ofs = cpu_to_le16((sizeof(MFT_RECORD_OLD) + 1) & ~1);
343 	else {
344 		/* Abort if mref is > 32 bits. */
345 		if (MREF(mref) & 0x0000ffff00000000ull) {
346 			errno = ERANGE;
347 			ntfs_log_perror("Mft reference exceeds 32 bits");
348 			return -1;
349 		}
350 		mrec->usa_ofs = cpu_to_le16((sizeof(MFT_RECORD) + 1) & ~1);
351 		/*
352 		 * Set the NTFS 3.1+ specific fields while we know that the
353 		 * volume version is 3.1+.
354 		 */
355 		mrec->reserved = cpu_to_le16(0);
356 		mrec->mft_record_number = cpu_to_le32(MREF(mref));
357 	}
358 	mrec->magic = magic_FILE;
359 	if (vol->mft_record_size >= NTFS_BLOCK_SIZE)
360 		mrec->usa_count = cpu_to_le16(vol->mft_record_size /
361 				NTFS_BLOCK_SIZE + 1);
362 	else {
363 		mrec->usa_count = cpu_to_le16(1);
364 		ntfs_log_error("Sector size is bigger than MFT record size.  "
365 				"Setting usa_count to 1.  If Windows chkdsk "
366 				"reports this as corruption, please email %s "
367 				"stating that you saw this message and that "
368 				"the file system created was corrupt.  "
369 				"Thank you.\n", NTFS_DEV_LIST);
370 	}
371 	/* Set the update sequence number to 1. */
372 	*(u16*)((u8*)mrec + le16_to_cpu(mrec->usa_ofs)) = cpu_to_le16(1);
373 	mrec->lsn = cpu_to_le64(0ull);
374 	mrec->sequence_number = cpu_to_le16(1);
375 	mrec->link_count = cpu_to_le16(0);
376 	/* Aligned to 8-byte boundary. */
377 	mrec->attrs_offset = cpu_to_le16((le16_to_cpu(mrec->usa_ofs) +
378 			(le16_to_cpu(mrec->usa_count) << 1) + 7) & ~7);
379 	mrec->flags = cpu_to_le16(0);
380 	/*
381 	 * Using attrs_offset plus eight bytes (for the termination attribute),
382 	 * aligned to 8-byte boundary.
383 	 */
384 	mrec->bytes_in_use = cpu_to_le32((le16_to_cpu(mrec->attrs_offset) + 8 +
385 			7) & ~7);
386 	mrec->bytes_allocated = cpu_to_le32(vol->mft_record_size);
387 	mrec->base_mft_record = cpu_to_le64((MFT_REF)0);
388 	mrec->next_attr_instance = cpu_to_le16(0);
389 	a = (ATTR_RECORD*)((u8*)mrec + le16_to_cpu(mrec->attrs_offset));
390 	a->type = AT_END;
391 	a->length = cpu_to_le32(0);
392 	/* Finally, clear the unused part of the mft record. */
393 	memset((u8*)a + 8, 0, vol->mft_record_size - ((u8*)a + 8 - (u8*)mrec));
394 	return 0;
395 }
396 
397 /**
398  * ntfs_mft_record_format - format an mft record on an ntfs volume
399  * @vol:	volume on which to format the mft record
400  * @mref:	mft reference specifying mft record to format
401  *
402  * Format the mft record with the mft reference @mref in $MFT/$DATA, i.e. lay
403  * out an empty, unused mft record in memory and write it to the volume @vol.
404  *
405  * On success return 0 and on error return -1 with errno set to the error code.
406  */
407 int ntfs_mft_record_format(const ntfs_volume *vol, const MFT_REF mref)
408 {
409 	MFT_RECORD *m;
410 	int ret = -1;
411 
412 	ntfs_log_enter("Entering\n");
413 
414 	m = ntfs_calloc(vol->mft_record_size);
415 	if (!m)
416 		goto out;
417 
418 	if (ntfs_mft_record_layout(vol, mref, m))
419 		goto free_m;
420 
421 	if (ntfs_mft_record_write(vol, mref, m))
422 		goto free_m;
423 
424 	ret = 0;
425 free_m:
426 	free(m);
427 out:
428 	ntfs_log_leave("\n");
429 	return ret;
430 }
431 
432 static const char *es = "  Leaving inconsistent metadata.  Run chkdsk.";
433 
434 /**
435  * ntfs_ffz - Find the first unset (zero) bit in a word
436  * @word:
437  *
438  * Description...
439  *
440  * Returns:
441  */
442 static inline unsigned int ntfs_ffz(unsigned int word)
443 {
444 	return ffs(~word) - 1;
445 }
446 
447 static int ntfs_is_mft(ntfs_inode *ni)
448 {
449 	if (ni && ni->mft_no == FILE_MFT)
450 		return 1;
451 	return 0;
452 }
453 
454 #ifndef PAGE_SIZE
455 #define PAGE_SIZE 4096
456 #endif
457 
458 #define RESERVED_MFT_RECORDS   64
459 
460 /**
461  * ntfs_mft_bitmap_find_free_rec - find a free mft record in the mft bitmap
462  * @vol:	volume on which to search for a free mft record
463  * @base_ni:	open base inode if allocating an extent mft record or NULL
464  *
465  * Search for a free mft record in the mft bitmap attribute on the ntfs volume
466  * @vol.
467  *
468  * If @base_ni is NULL start the search at the default allocator position.
469  *
470  * If @base_ni is not NULL start the search at the mft record after the base
471  * mft record @base_ni.
472  *
473  * Return the free mft record on success and -1 on error with errno set to the
474  * error code.  An error code of ENOSPC means that there are no free mft
475  * records in the currently initialized mft bitmap.
476  */
477 static int ntfs_mft_bitmap_find_free_rec(ntfs_volume *vol, ntfs_inode *base_ni)
478 {
479 	s64 pass_end, ll, data_pos, pass_start, ofs, bit;
480 	ntfs_attr *mftbmp_na;
481 	u8 *buf, *byte;
482 	unsigned int size;
483 	u8 pass, b;
484 	int ret = -1;
485 
486 	ntfs_log_enter("Entering\n");
487 
488 	mftbmp_na = vol->mftbmp_na;
489 	/*
490 	 * Set the end of the pass making sure we do not overflow the mft
491 	 * bitmap.
492 	 */
493 	size = PAGE_SIZE;
494 	pass_end = vol->mft_na->allocated_size >> vol->mft_record_size_bits;
495 	ll = mftbmp_na->initialized_size << 3;
496 	if (pass_end > ll)
497 		pass_end = ll;
498 	pass = 1;
499 	if (!base_ni)
500 		data_pos = vol->mft_data_pos;
501 	else
502 		data_pos = base_ni->mft_no + 1;
503 	if (data_pos < RESERVED_MFT_RECORDS)
504 		data_pos = RESERVED_MFT_RECORDS;
505 	if (data_pos >= pass_end) {
506 		data_pos = RESERVED_MFT_RECORDS;
507 		pass = 2;
508 		/* This happens on a freshly formatted volume. */
509 		if (data_pos >= pass_end) {
510 			errno = ENOSPC;
511 			goto leave;
512 		}
513 	}
514 	if (ntfs_is_mft(base_ni)) {
515 		data_pos = 0;
516 		pass = 2;
517 	}
518 	pass_start = data_pos;
519 	buf = ntfs_malloc(PAGE_SIZE);
520 	if (!buf)
521 		goto leave;
522 
523 	ntfs_log_debug("Starting bitmap search: pass %u, pass_start 0x%llx, "
524 			"pass_end 0x%llx, data_pos 0x%llx.\n", pass,
525 			(long long)pass_start, (long long)pass_end,
526 			(long long)data_pos);
527 #ifdef DEBUG
528 	byte = NULL;
529 	b = 0;
530 #endif
531 	/* Loop until a free mft record is found. */
532 	for (; pass <= 2; size = PAGE_SIZE) {
533 		/* Cap size to pass_end. */
534 		ofs = data_pos >> 3;
535 		ll = ((pass_end + 7) >> 3) - ofs;
536 		if (size > ll)
537 			size = ll;
538 		ll = ntfs_attr_pread(mftbmp_na, ofs, size, buf);
539 		if (ll < 0) {
540 			ntfs_log_perror("Failed to read $MFT bitmap");
541 			free(buf);
542 			goto leave;
543 		}
544 		ntfs_log_debug("Read 0x%llx bytes.\n", (long long)ll);
545 		/* If we read at least one byte, search @buf for a zero bit. */
546 		if (ll) {
547 			size = ll << 3;
548 			bit = data_pos & 7;
549 			data_pos &= ~7ull;
550 			ntfs_log_debug("Before inner for loop: size 0x%x, "
551 					"data_pos 0x%llx, bit 0x%llx, "
552 					"*byte 0x%hhx, b %u.\n", size,
553 					(long long)data_pos, (long long)bit,
554 					byte ? *byte : -1, b);
555 			for (; bit < size && data_pos + bit < pass_end;
556 					bit &= ~7ull, bit += 8) {
557 				/*
558 				 * If we're extending $MFT and running out of the first
559 				 * mft record (base record) then give up searching since
560 				 * no guarantee that the found record will be accessible.
561 				 */
562 				if (ntfs_is_mft(base_ni) && bit > 400)
563 					goto out;
564 
565 				byte = buf + (bit >> 3);
566 				if (*byte == 0xff)
567 					continue;
568 
569 				/* Note: ffz() result must be zero based. */
570 				b = ntfs_ffz((unsigned long)*byte);
571 				if (b < 8 && b >= (bit & 7)) {
572 					free(buf);
573 					ret = data_pos + (bit & ~7ull) + b;
574 					goto leave;
575 				}
576 			}
577 			ntfs_log_debug("After inner for loop: size 0x%x, "
578 					"data_pos 0x%llx, bit 0x%llx, "
579 					"*byte 0x%hhx, b %u.\n", size,
580 					(long long)data_pos, (long long)bit,
581 					byte ? *byte : -1, b);
582 			data_pos += size;
583 			/*
584 			 * If the end of the pass has not been reached yet,
585 			 * continue searching the mft bitmap for a zero bit.
586 			 */
587 			if (data_pos < pass_end)
588 				continue;
589 		}
590 		/* Do the next pass. */
591 		pass++;
592 		if (pass == 2) {
593 			/*
594 			 * Starting the second pass, in which we scan the first
595 			 * part of the zone which we omitted earlier.
596 			 */
597 			pass_end = pass_start;
598 			data_pos = pass_start = RESERVED_MFT_RECORDS;
599 			ntfs_log_debug("pass %i, pass_start 0x%llx, pass_end "
600 					"0x%llx.\n", pass, (long long)pass_start,
601 					(long long)pass_end);
602 			if (data_pos >= pass_end)
603 				break;
604 		}
605 	}
606 	/* No free mft records in currently initialized mft bitmap. */
607 out:
608 	free(buf);
609 	errno = ENOSPC;
610 leave:
611 	ntfs_log_leave("\n");
612 	return ret;
613 }
614 
615 static int ntfs_mft_attr_extend(ntfs_attr *na)
616 {
617 	int ret = STATUS_ERROR;
618 	ntfs_log_enter("Entering\n");
619 
620 	if (!NInoAttrList(na->ni)) {
621 		if (ntfs_inode_add_attrlist(na->ni)) {
622 			ntfs_log_perror("%s: Can not add attrlist #3", __FUNCTION__);
623 			goto out;
624 		}
625 		/* We can't sync the $MFT inode since its runlist is bogus. */
626 		ret = STATUS_KEEP_SEARCHING;
627 		goto out;
628 	}
629 
630 	if (ntfs_attr_update_mapping_pairs(na, 0)) {
631 		ntfs_log_perror("%s: MP update failed", __FUNCTION__);
632 		goto out;
633 	}
634 
635 	ret = STATUS_OK;
636 out:
637 	ntfs_log_leave("\n");
638 	return ret;
639 }
640 
641 /**
642  * ntfs_mft_bitmap_extend_allocation_i - see ntfs_mft_bitmap_extend_allocation
643  */
644 static int ntfs_mft_bitmap_extend_allocation_i(ntfs_volume *vol)
645 {
646 	LCN lcn;
647 	s64 ll = 0; /* silence compiler warning */
648 	ntfs_attr *mftbmp_na;
649 	runlist_element *rl, *rl2 = NULL; /* silence compiler warning */
650 	ntfs_attr_search_ctx *ctx;
651 	MFT_RECORD *m = NULL; /* silence compiler warning */
652 	ATTR_RECORD *a = NULL; /* silence compiler warning */
653 	int err, mp_size;
654 	int ret = STATUS_ERROR;
655 	u32 old_alen = 0; /* silence compiler warning */
656 	BOOL mp_rebuilt = FALSE;
657 	BOOL update_mp = FALSE;
658 
659 	mftbmp_na = vol->mftbmp_na;
660 	/*
661 	 * Determine the last lcn of the mft bitmap.  The allocated size of the
662 	 * mft bitmap cannot be zero so we are ok to do this.
663 	 */
664 	rl = ntfs_attr_find_vcn(mftbmp_na, (mftbmp_na->allocated_size - 1) >>
665 			vol->cluster_size_bits);
666 	if (!rl || !rl->length || rl->lcn < 0) {
667 		ntfs_log_error("Failed to determine last allocated "
668 				"cluster of mft bitmap attribute.\n");
669 		if (rl)
670 			errno = EIO;
671 		return STATUS_ERROR;
672 	}
673 	lcn = rl->lcn + rl->length;
674 
675 	rl2 = ntfs_cluster_alloc(vol, rl[1].vcn, 1, lcn, DATA_ZONE);
676 	if (!rl2) {
677 		ntfs_log_error("Failed to allocate a cluster for "
678 				"the mft bitmap.\n");
679 		return STATUS_ERROR;
680 	}
681 	rl = ntfs_runlists_merge(mftbmp_na->rl, rl2);
682 	if (!rl) {
683 		err = errno;
684 		ntfs_log_error("Failed to merge runlists for mft "
685 				"bitmap.\n");
686 		if (ntfs_cluster_free_from_rl(vol, rl2))
687 			ntfs_log_error("Failed to deallocate "
688 					"cluster.%s\n", es);
689 		free(rl2);
690 		errno = err;
691 		return STATUS_ERROR;
692 	}
693 	mftbmp_na->rl = rl;
694 	ntfs_log_debug("Adding one run to mft bitmap.\n");
695 	/* Find the last run in the new runlist. */
696 	for (; rl[1].length; rl++)
697 		;
698 	/*
699 	 * Update the attribute record as well.  Note: @rl is the last
700 	 * (non-terminator) runlist element of mft bitmap.
701 	 */
702 	ctx = ntfs_attr_get_search_ctx(mftbmp_na->ni, NULL);
703 	if (!ctx)
704 		goto undo_alloc;
705 
706 	if (ntfs_attr_lookup(mftbmp_na->type, mftbmp_na->name,
707 			mftbmp_na->name_len, 0, rl[1].vcn, NULL, 0, ctx)) {
708 		ntfs_log_error("Failed to find last attribute extent of "
709 				"mft bitmap attribute.\n");
710 		goto undo_alloc;
711 	}
712 	m = ctx->mrec;
713 	a = ctx->attr;
714 	ll = sle64_to_cpu(a->lowest_vcn);
715 	rl2 = ntfs_attr_find_vcn(mftbmp_na, ll);
716 	if (!rl2 || !rl2->length) {
717 		ntfs_log_error("Failed to determine previous last "
718 				"allocated cluster of mft bitmap attribute.\n");
719 		if (rl2)
720 			errno = EIO;
721 		goto undo_alloc;
722 	}
723 	/* Get the size for the new mapping pairs array for this extent. */
724 	mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, INT_MAX);
725 	if (mp_size <= 0) {
726 		ntfs_log_error("Get size for mapping pairs failed for "
727 				"mft bitmap attribute extent.\n");
728 		goto undo_alloc;
729 	}
730 	/* Expand the attribute record if necessary. */
731 	old_alen = le32_to_cpu(a->length);
732 	if (ntfs_attr_record_resize(m, a, mp_size +
733 			le16_to_cpu(a->mapping_pairs_offset))) {
734 		ntfs_log_info("extending $MFT bitmap\n");
735 		ret = ntfs_mft_attr_extend(vol->mftbmp_na);
736 		if (ret == STATUS_OK)
737 			goto ok;
738 		if (ret == STATUS_ERROR) {
739 			ntfs_log_perror("%s: ntfs_mft_attr_extend failed", __FUNCTION__);
740 			update_mp = TRUE;
741 		}
742 		goto undo_alloc;
743 	}
744 	mp_rebuilt = TRUE;
745 	/* Generate the mapping pairs array directly into the attr record. */
746 	if (ntfs_mapping_pairs_build(vol, (u8*)a +
747 			le16_to_cpu(a->mapping_pairs_offset), mp_size, rl2, ll,
748 			NULL)) {
749 		ntfs_log_error("Failed to build mapping pairs array for "
750 				"mft bitmap attribute.\n");
751 		errno = EIO;
752 		goto undo_alloc;
753 	}
754 	/* Update the highest_vcn. */
755 	a->highest_vcn = cpu_to_sle64(rl[1].vcn - 1);
756 	/*
757 	 * We now have extended the mft bitmap allocated_size by one cluster.
758 	 * Reflect this in the ntfs_attr structure and the attribute record.
759 	 */
760 	if (a->lowest_vcn) {
761 		/*
762 		 * We are not in the first attribute extent, switch to it, but
763 		 * first ensure the changes will make it to disk later.
764 		 */
765 		ntfs_inode_mark_dirty(ctx->ntfs_ino);
766 		ntfs_attr_reinit_search_ctx(ctx);
767 		if (ntfs_attr_lookup(mftbmp_na->type, mftbmp_na->name,
768 				mftbmp_na->name_len, 0, 0, NULL, 0, ctx)) {
769 			ntfs_log_error("Failed to find first attribute "
770 					"extent of mft bitmap attribute.\n");
771 			goto restore_undo_alloc;
772 		}
773 		a = ctx->attr;
774 	}
775 ok:
776 	mftbmp_na->allocated_size += vol->cluster_size;
777 	a->allocated_size = cpu_to_sle64(mftbmp_na->allocated_size);
778 	/* Ensure the changes make it to disk. */
779 	ntfs_inode_mark_dirty(ctx->ntfs_ino);
780 	ntfs_attr_put_search_ctx(ctx);
781 	return STATUS_OK;
782 
783 restore_undo_alloc:
784 	err = errno;
785 	ntfs_attr_reinit_search_ctx(ctx);
786 	if (ntfs_attr_lookup(mftbmp_na->type, mftbmp_na->name,
787 			mftbmp_na->name_len, 0, rl[1].vcn, NULL, 0, ctx)) {
788 		ntfs_log_error("Failed to find last attribute extent of "
789 				"mft bitmap attribute.%s\n", es);
790 		ntfs_attr_put_search_ctx(ctx);
791 		mftbmp_na->allocated_size += vol->cluster_size;
792 		/*
793 		 * The only thing that is now wrong is ->allocated_size of the
794 		 * base attribute extent which chkdsk should be able to fix.
795 		 */
796 		errno = err;
797 		return STATUS_ERROR;
798 	}
799 	m = ctx->mrec;
800 	a = ctx->attr;
801 	a->highest_vcn = cpu_to_sle64(rl[1].vcn - 2);
802 	errno = err;
803 undo_alloc:
804 	err = errno;
805 
806 	/* Remove the last run from the runlist. */
807 	lcn = rl->lcn;
808 	rl->lcn = rl[1].lcn;
809 	rl->length = 0;
810 
811 	/* FIXME: use an ntfs_cluster_free_* function */
812 	if (ntfs_bitmap_clear_bit(vol->lcnbmp_na, lcn))
813 		ntfs_log_error("Failed to free cluster.%s\n", es);
814 	else
815 		vol->free_clusters++;
816 	if (mp_rebuilt) {
817 		if (ntfs_mapping_pairs_build(vol, (u8*)a +
818 				le16_to_cpu(a->mapping_pairs_offset),
819 				old_alen - le16_to_cpu(a->mapping_pairs_offset),
820 				rl2, ll, NULL))
821 			ntfs_log_error("Failed to restore mapping "
822 					"pairs array.%s\n", es);
823 		if (ntfs_attr_record_resize(m, a, old_alen))
824 			ntfs_log_error("Failed to restore attribute "
825 					"record.%s\n", es);
826 		ntfs_inode_mark_dirty(ctx->ntfs_ino);
827 	}
828 	if (update_mp) {
829 		if (ntfs_attr_update_mapping_pairs(vol->mftbmp_na, 0))
830 			ntfs_log_perror("%s: MP update failed", __FUNCTION__);
831 	}
832 	if (ctx)
833 		ntfs_attr_put_search_ctx(ctx);
834 	errno = err;
835 	return ret;
836 }
837 
838 /**
839  * ntfs_mft_bitmap_extend_allocation - extend mft bitmap attribute by a cluster
840  * @vol:	volume on which to extend the mft bitmap attribute
841  *
842  * Extend the mft bitmap attribute on the ntfs volume @vol by one cluster.
843  *
844  * Note:  Only changes allocated_size, i.e. does not touch initialized_size or
845  * data_size.
846  *
847  * Return 0 on success and -1 on error with errno set to the error code.
848  */
849 static int ntfs_mft_bitmap_extend_allocation(ntfs_volume *vol)
850 {
851 	int ret;
852 
853 	ntfs_log_enter("Entering\n");
854 	ret = ntfs_mft_bitmap_extend_allocation_i(vol);
855 	ntfs_log_leave("\n");
856 	return ret;
857 }
858 /**
859  * ntfs_mft_bitmap_extend_initialized - extend mft bitmap initialized data
860  * @vol:	volume on which to extend the mft bitmap attribute
861  *
862  * Extend the initialized portion of the mft bitmap attribute on the ntfs
863  * volume @vol by 8 bytes.
864  *
865  * Note:  Only changes initialized_size and data_size, i.e. requires that
866  * allocated_size is big enough to fit the new initialized_size.
867  *
868  * Return 0 on success and -1 on error with errno set to the error code.
869  */
870 static int ntfs_mft_bitmap_extend_initialized(ntfs_volume *vol)
871 {
872 	s64 old_data_size, old_initialized_size, ll;
873 	ntfs_attr *mftbmp_na;
874 	ntfs_attr_search_ctx *ctx;
875 	ATTR_RECORD *a;
876 	int err;
877 	int ret = -1;
878 
879 	ntfs_log_enter("Entering\n");
880 
881 	mftbmp_na = vol->mftbmp_na;
882 	ctx = ntfs_attr_get_search_ctx(mftbmp_na->ni, NULL);
883 	if (!ctx)
884 		goto out;
885 
886 	if (ntfs_attr_lookup(mftbmp_na->type, mftbmp_na->name,
887 			mftbmp_na->name_len, 0, 0, NULL, 0, ctx)) {
888 		ntfs_log_error("Failed to find first attribute extent of "
889 				"mft bitmap attribute.\n");
890 		err = errno;
891 		goto put_err_out;
892 	}
893 	a = ctx->attr;
894 	old_data_size = mftbmp_na->data_size;
895 	old_initialized_size = mftbmp_na->initialized_size;
896 	mftbmp_na->initialized_size += 8;
897 	a->initialized_size = cpu_to_sle64(mftbmp_na->initialized_size);
898 	if (mftbmp_na->initialized_size > mftbmp_na->data_size) {
899 		mftbmp_na->data_size = mftbmp_na->initialized_size;
900 		a->data_size = cpu_to_sle64(mftbmp_na->data_size);
901 	}
902 	/* Ensure the changes make it to disk. */
903 	ntfs_inode_mark_dirty(ctx->ntfs_ino);
904 	ntfs_attr_put_search_ctx(ctx);
905 	/* Initialize the mft bitmap attribute value with zeroes. */
906 	ll = 0;
907 	ll = ntfs_attr_pwrite(mftbmp_na, old_initialized_size, 8, &ll);
908 	if (ll == 8) {
909 		ntfs_log_debug("Wrote eight initialized bytes to mft bitmap.\n");
910 		vol->free_mft_records += (8 * 8);
911 		ret = 0;
912 		goto out;
913 	}
914 	ntfs_log_error("Failed to write to mft bitmap.\n");
915 	err = errno;
916 	if (ll >= 0)
917 		err = EIO;
918 	/* Try to recover from the error. */
919 	ctx = ntfs_attr_get_search_ctx(mftbmp_na->ni, NULL);
920 	if (!ctx)
921 		goto err_out;
922 
923 	if (ntfs_attr_lookup(mftbmp_na->type, mftbmp_na->name,
924 			mftbmp_na->name_len, 0, 0, NULL, 0, ctx)) {
925 		ntfs_log_error("Failed to find first attribute extent of "
926 				"mft bitmap attribute.%s\n", es);
927 put_err_out:
928 		ntfs_attr_put_search_ctx(ctx);
929 		goto err_out;
930 	}
931 	a = ctx->attr;
932 	mftbmp_na->initialized_size = old_initialized_size;
933 	a->initialized_size = cpu_to_sle64(old_initialized_size);
934 	if (mftbmp_na->data_size != old_data_size) {
935 		mftbmp_na->data_size = old_data_size;
936 		a->data_size = cpu_to_sle64(old_data_size);
937 	}
938 	ntfs_inode_mark_dirty(ctx->ntfs_ino);
939 	ntfs_attr_put_search_ctx(ctx);
940 	ntfs_log_debug("Restored status of mftbmp: allocated_size 0x%llx, "
941 			"data_size 0x%llx, initialized_size 0x%llx.\n",
942 			(long long)mftbmp_na->allocated_size,
943 			(long long)mftbmp_na->data_size,
944 			(long long)mftbmp_na->initialized_size);
945 err_out:
946 	errno = err;
947 out:
948 	ntfs_log_leave("\n");
949 	return ret;
950 }
951 
952 /**
953  * ntfs_mft_data_extend_allocation - extend mft data attribute
954  * @vol:	volume on which to extend the mft data attribute
955  *
956  * Extend the mft data attribute on the ntfs volume @vol by 16 mft records
957  * worth of clusters or if not enough space for this by one mft record worth
958  * of clusters.
959  *
960  * Note:  Only changes allocated_size, i.e. does not touch initialized_size or
961  * data_size.
962  *
963  * Return 0 on success and -1 on error with errno set to the error code.
964  */
965 static int ntfs_mft_data_extend_allocation(ntfs_volume *vol)
966 {
967 	LCN lcn;
968 	VCN old_last_vcn;
969 	s64 min_nr, nr, ll = 0; /* silence compiler warning */
970 	ntfs_attr *mft_na;
971 	runlist_element *rl, *rl2;
972 	ntfs_attr_search_ctx *ctx;
973 	MFT_RECORD *m = NULL; /* silence compiler warning */
974 	ATTR_RECORD *a = NULL; /* silence compiler warning */
975 	int err, mp_size;
976 	int ret = STATUS_ERROR;
977 	u32 old_alen = 0; /* silence compiler warning */
978 	BOOL mp_rebuilt = FALSE;
979 	BOOL update_mp = FALSE;
980 
981 	ntfs_log_enter("Extending mft data allocation.\n");
982 
983 	mft_na = vol->mft_na;
984 	/*
985 	 * Determine the preferred allocation location, i.e. the last lcn of
986 	 * the mft data attribute.  The allocated size of the mft data
987 	 * attribute cannot be zero so we are ok to do this.
988 	 */
989 	rl = ntfs_attr_find_vcn(mft_na,
990 			(mft_na->allocated_size - 1) >> vol->cluster_size_bits);
991 
992 	if (!rl || !rl->length || rl->lcn < 0) {
993 		ntfs_log_error("Failed to determine last allocated "
994 				"cluster of mft data attribute.\n");
995 		if (rl)
996 			errno = EIO;
997 		goto out;
998 	}
999 
1000 	lcn = rl->lcn + rl->length;
1001 	ntfs_log_debug("Last lcn of mft data attribute is 0x%llx.\n", (long long)lcn);
1002 	/* Minimum allocation is one mft record worth of clusters. */
1003 	min_nr = vol->mft_record_size >> vol->cluster_size_bits;
1004 	if (!min_nr)
1005 		min_nr = 1;
1006 	/* Want to allocate 16 mft records worth of clusters. */
1007 	nr = vol->mft_record_size << 4 >> vol->cluster_size_bits;
1008 	if (!nr)
1009 		nr = min_nr;
1010 
1011 	old_last_vcn = rl[1].vcn;
1012 	do {
1013 		rl2 = ntfs_cluster_alloc(vol, old_last_vcn, nr, lcn, MFT_ZONE);
1014 		if (rl2)
1015 			break;
1016 		if (errno != ENOSPC || nr == min_nr) {
1017 			ntfs_log_perror("Failed to allocate (%lld) clusters "
1018 					"for $MFT", (long long)nr);
1019 			goto out;
1020 		}
1021 		/*
1022 		 * There is not enough space to do the allocation, but there
1023 		 * might be enough space to do a minimal allocation so try that
1024 		 * before failing.
1025 		 */
1026 		nr = min_nr;
1027 		ntfs_log_debug("Retrying mft data allocation with minimal cluster "
1028 				"count %lli.\n", (long long)nr);
1029 	} while (1);
1030 
1031 	ntfs_log_debug("Allocated %lld clusters.\n", (long long)nr);
1032 
1033 	rl = ntfs_runlists_merge(mft_na->rl, rl2);
1034 	if (!rl) {
1035 		err = errno;
1036 		ntfs_log_error("Failed to merge runlists for mft data "
1037 				"attribute.\n");
1038 		if (ntfs_cluster_free_from_rl(vol, rl2))
1039 			ntfs_log_error("Failed to deallocate clusters "
1040 					"from the mft data attribute.%s\n", es);
1041 		free(rl2);
1042 		errno = err;
1043 		goto out;
1044 	}
1045 	mft_na->rl = rl;
1046 
1047 	/* Find the last run in the new runlist. */
1048 	for (; rl[1].length; rl++)
1049 		;
1050 	/* Update the attribute record as well. */
1051 	ctx = ntfs_attr_get_search_ctx(mft_na->ni, NULL);
1052 	if (!ctx)
1053 		goto undo_alloc;
1054 
1055 	if (ntfs_attr_lookup(mft_na->type, mft_na->name, mft_na->name_len, 0,
1056 			rl[1].vcn, NULL, 0, ctx)) {
1057 		ntfs_log_error("Failed to find last attribute extent of "
1058 				"mft data attribute.\n");
1059 		goto undo_alloc;
1060 	}
1061 	m = ctx->mrec;
1062 	a = ctx->attr;
1063 	ll = sle64_to_cpu(a->lowest_vcn);
1064 	rl2 = ntfs_attr_find_vcn(mft_na, ll);
1065 	if (!rl2 || !rl2->length) {
1066 		ntfs_log_error("Failed to determine previous last "
1067 				"allocated cluster of mft data attribute.\n");
1068 		if (rl2)
1069 			errno = EIO;
1070 		goto undo_alloc;
1071 	}
1072 	/* Get the size for the new mapping pairs array for this extent. */
1073 	mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, INT_MAX);
1074 	if (mp_size <= 0) {
1075 		ntfs_log_error("Get size for mapping pairs failed for "
1076 				"mft data attribute extent.\n");
1077 		goto undo_alloc;
1078 	}
1079 	/* Expand the attribute record if necessary. */
1080 	old_alen = le32_to_cpu(a->length);
1081 	if (ntfs_attr_record_resize(m, a,
1082 			mp_size + le16_to_cpu(a->mapping_pairs_offset))) {
1083 		ret = ntfs_mft_attr_extend(vol->mft_na);
1084 		if (ret == STATUS_OK)
1085 			goto ok;
1086 		if (ret == STATUS_ERROR) {
1087 			ntfs_log_perror("%s: ntfs_mft_attr_extend failed", __FUNCTION__);
1088 			update_mp = TRUE;
1089 		}
1090 		goto undo_alloc;
1091 	}
1092 	mp_rebuilt = TRUE;
1093 	/*
1094 	 * Generate the mapping pairs array directly into the attribute record.
1095 	 */
1096 	if (ntfs_mapping_pairs_build(vol,
1097 			(u8*)a + le16_to_cpu(a->mapping_pairs_offset), mp_size,
1098 			rl2, ll, NULL)) {
1099 		ntfs_log_error("Failed to build mapping pairs array of "
1100 				"mft data attribute.\n");
1101 		errno = EIO;
1102 		goto undo_alloc;
1103 	}
1104 	/* Update the highest_vcn. */
1105 	a->highest_vcn = cpu_to_sle64(rl[1].vcn - 1);
1106 	/*
1107 	 * We now have extended the mft data allocated_size by nr clusters.
1108 	 * Reflect this in the ntfs_attr structure and the attribute record.
1109 	 * @rl is the last (non-terminator) runlist element of mft data
1110 	 * attribute.
1111 	 */
1112 	if (a->lowest_vcn) {
1113 		/*
1114 		 * We are not in the first attribute extent, switch to it, but
1115 		 * first ensure the changes will make it to disk later.
1116 		 */
1117 		ntfs_inode_mark_dirty(ctx->ntfs_ino);
1118 		ntfs_attr_reinit_search_ctx(ctx);
1119 		if (ntfs_attr_lookup(mft_na->type, mft_na->name,
1120 				mft_na->name_len, 0, 0, NULL, 0, ctx)) {
1121 			ntfs_log_error("Failed to find first attribute "
1122 					"extent of mft data attribute.\n");
1123 			goto restore_undo_alloc;
1124 		}
1125 		a = ctx->attr;
1126 	}
1127 ok:
1128 	mft_na->allocated_size += nr << vol->cluster_size_bits;
1129 	a->allocated_size = cpu_to_sle64(mft_na->allocated_size);
1130 	/* Ensure the changes make it to disk. */
1131 	ntfs_inode_mark_dirty(ctx->ntfs_ino);
1132 	ntfs_attr_put_search_ctx(ctx);
1133 	ret = STATUS_OK;
1134 out:
1135 	ntfs_log_leave("\n");
1136 	return ret;
1137 
1138 restore_undo_alloc:
1139 	err = errno;
1140 	ntfs_attr_reinit_search_ctx(ctx);
1141 	if (ntfs_attr_lookup(mft_na->type, mft_na->name, mft_na->name_len, 0,
1142 			rl[1].vcn, NULL, 0, ctx)) {
1143 		ntfs_log_error("Failed to find last attribute extent of "
1144 				"mft data attribute.%s\n", es);
1145 		ntfs_attr_put_search_ctx(ctx);
1146 		mft_na->allocated_size += nr << vol->cluster_size_bits;
1147 		/*
1148 		 * The only thing that is now wrong is ->allocated_size of the
1149 		 * base attribute extent which chkdsk should be able to fix.
1150 		 */
1151 		errno = err;
1152 		ret = STATUS_ERROR;
1153 		goto out;
1154 	}
1155 	m = ctx->mrec;
1156 	a = ctx->attr;
1157 	a->highest_vcn = cpu_to_sle64(old_last_vcn - 1);
1158 	errno = err;
1159 undo_alloc:
1160 	err = errno;
1161 	if (ntfs_cluster_free(vol, mft_na, old_last_vcn, -1) < 0)
1162 		ntfs_log_error("Failed to free clusters from mft data "
1163 				"attribute.%s\n", es);
1164 	if (ntfs_rl_truncate(&mft_na->rl, old_last_vcn))
1165 		ntfs_log_error("Failed to truncate mft data attribute "
1166 				"runlist.%s\n", es);
1167 	if (mp_rebuilt) {
1168 		if (ntfs_mapping_pairs_build(vol, (u8*)a +
1169 				le16_to_cpu(a->mapping_pairs_offset),
1170 				old_alen - le16_to_cpu(a->mapping_pairs_offset),
1171 				rl2, ll, NULL))
1172 			ntfs_log_error("Failed to restore mapping pairs "
1173 					"array.%s\n", es);
1174 		if (ntfs_attr_record_resize(m, a, old_alen))
1175 			ntfs_log_error("Failed to restore attribute "
1176 					"record.%s\n", es);
1177 		ntfs_inode_mark_dirty(ctx->ntfs_ino);
1178 	}
1179 	if (update_mp) {
1180 		if (ntfs_attr_update_mapping_pairs(vol->mft_na, 0))
1181 			ntfs_log_perror("%s: MP update failed", __FUNCTION__);
1182 	}
1183 	if (ctx)
1184 		ntfs_attr_put_search_ctx(ctx);
1185 	errno = err;
1186 	goto out;
1187 }
1188 
1189 
1190 static int ntfs_mft_record_init(ntfs_volume *vol, s64 size)
1191 {
1192 	int ret = -1;
1193 	ntfs_attr *mft_na, *mftbmp_na;
1194 	s64 old_data_initialized, old_data_size;
1195 	ntfs_attr_search_ctx *ctx;
1196 
1197 	ntfs_log_enter("Entering\n");
1198 
1199 	/* NOTE: Caller must sanity check vol, vol->mft_na and vol->mftbmp_na */
1200 
1201 	mft_na = vol->mft_na;
1202 	mftbmp_na = vol->mftbmp_na;
1203 
1204 	/*
1205 	 * The mft record is outside the initialized data. Extend the mft data
1206 	 * attribute until it covers the allocated record. The loop is only
1207 	 * actually traversed more than once when a freshly formatted volume
1208 	 * is first written to so it optimizes away nicely in the common case.
1209 	 */
1210 	ntfs_log_debug("Status of mft data before extension: "
1211 			"allocated_size 0x%llx, data_size 0x%llx, "
1212 			"initialized_size 0x%llx.\n",
1213 			(long long)mft_na->allocated_size,
1214 			(long long)mft_na->data_size,
1215 			(long long)mft_na->initialized_size);
1216 	while (size > mft_na->allocated_size) {
1217 		if (ntfs_mft_data_extend_allocation(vol) == STATUS_ERROR)
1218 			goto out;
1219 		ntfs_log_debug("Status of mft data after allocation extension: "
1220 				"allocated_size 0x%llx, data_size 0x%llx, "
1221 				"initialized_size 0x%llx.\n",
1222 				(long long)mft_na->allocated_size,
1223 				(long long)mft_na->data_size,
1224 				(long long)mft_na->initialized_size);
1225 	}
1226 
1227 	old_data_initialized = mft_na->initialized_size;
1228 	old_data_size = mft_na->data_size;
1229 
1230 	/*
1231 	 * Extend mft data initialized size (and data size of course) to reach
1232 	 * the allocated mft record, formatting the mft records along the way.
1233 	 * Note: We only modify the ntfs_attr structure as that is all that is
1234 	 * needed by ntfs_mft_record_format().  We will update the attribute
1235 	 * record itself in one fell swoop later on.
1236 	 */
1237 	while (size > mft_na->initialized_size) {
1238 		s64 ll2 = mft_na->initialized_size >> vol->mft_record_size_bits;
1239 		mft_na->initialized_size += vol->mft_record_size;
1240 		if (mft_na->initialized_size > mft_na->data_size)
1241 			mft_na->data_size = mft_na->initialized_size;
1242 		ntfs_log_debug("Initializing mft record 0x%llx.\n", (long long)ll2);
1243 		if (ntfs_mft_record_format(vol, ll2) < 0) {
1244 			ntfs_log_perror("Failed to format mft record");
1245 			goto undo_data_init;
1246 		}
1247 	}
1248 
1249 	/* Update the mft data attribute record to reflect the new sizes. */
1250 	ctx = ntfs_attr_get_search_ctx(mft_na->ni, NULL);
1251 	if (!ctx)
1252 		goto undo_data_init;
1253 
1254 	if (ntfs_attr_lookup(mft_na->type, mft_na->name, mft_na->name_len, 0,
1255 			0, NULL, 0, ctx)) {
1256 		ntfs_log_error("Failed to find first attribute extent of "
1257 				"mft data attribute.\n");
1258 		ntfs_attr_put_search_ctx(ctx);
1259 		goto undo_data_init;
1260 	}
1261 	ctx->attr->initialized_size = cpu_to_sle64(mft_na->initialized_size);
1262 	ctx->attr->data_size = cpu_to_sle64(mft_na->data_size);
1263 	ctx->attr->allocated_size = cpu_to_sle64(mft_na->allocated_size);
1264 
1265 	/* Ensure the changes make it to disk. */
1266 	ntfs_inode_mark_dirty(ctx->ntfs_ino);
1267 	ntfs_attr_put_search_ctx(ctx);
1268 	ntfs_log_debug("Status of mft data after mft record initialization: "
1269 			"allocated_size 0x%llx, data_size 0x%llx, "
1270 			"initialized_size 0x%llx.\n",
1271 			(long long)mft_na->allocated_size,
1272 			(long long)mft_na->data_size,
1273 			(long long)mft_na->initialized_size);
1274 
1275 	/* Sanity checks. */
1276 	if (mft_na->data_size > mft_na->allocated_size ||
1277 	    mft_na->initialized_size > mft_na->data_size)
1278 		NTFS_BUG("mft_na sanity checks failed");
1279 
1280 	/* Sync MFT to minimize data loss if there won't be clean unmount. */
1281 	if (ntfs_inode_sync(mft_na->ni))
1282 		goto undo_data_init;
1283 
1284 	ret = 0;
1285 out:
1286 	ntfs_log_leave("\n");
1287 	return ret;
1288 
1289 undo_data_init:
1290 	mft_na->initialized_size = old_data_initialized;
1291 	mft_na->data_size = old_data_size;
1292 	goto out;
1293 }
1294 
1295 static int ntfs_mft_rec_init(ntfs_volume *vol, s64 size)
1296 {
1297 	int ret = -1;
1298 	ntfs_attr *mft_na, *mftbmp_na;
1299 	s64 old_data_initialized, old_data_size;
1300 	ntfs_attr_search_ctx *ctx;
1301 
1302 	ntfs_log_enter("Entering\n");
1303 
1304 	mft_na = vol->mft_na;
1305 	mftbmp_na = vol->mftbmp_na;
1306 
1307 	if (size > mft_na->allocated_size || size > mft_na->initialized_size) {
1308 		errno = EIO;
1309 		ntfs_log_perror("%s: unexpected $MFT sizes, see below", __FUNCTION__);
1310 		ntfs_log_error("$MFT: size=%lld  allocated_size=%lld  "
1311 			       "data_size=%lld  initialized_size=%lld\n",
1312 			       (long long)size,
1313 			       (long long)mft_na->allocated_size,
1314 			       (long long)mft_na->data_size,
1315 			       (long long)mft_na->initialized_size);
1316 		goto out;
1317 	}
1318 
1319 	old_data_initialized = mft_na->initialized_size;
1320 	old_data_size = mft_na->data_size;
1321 
1322 	/* Update the mft data attribute record to reflect the new sizes. */
1323 	ctx = ntfs_attr_get_search_ctx(mft_na->ni, NULL);
1324 	if (!ctx)
1325 		goto undo_data_init;
1326 
1327 	if (ntfs_attr_lookup(mft_na->type, mft_na->name, mft_na->name_len, 0,
1328 			0, NULL, 0, ctx)) {
1329 		ntfs_log_error("Failed to find first attribute extent of "
1330 				"mft data attribute.\n");
1331 		ntfs_attr_put_search_ctx(ctx);
1332 		goto undo_data_init;
1333 	}
1334 	ctx->attr->initialized_size = cpu_to_sle64(mft_na->initialized_size);
1335 	ctx->attr->data_size = cpu_to_sle64(mft_na->data_size);
1336 
1337 	/* CHECKME: ctx->attr->allocation_size is already ok? */
1338 
1339 	/* Ensure the changes make it to disk. */
1340 	ntfs_inode_mark_dirty(ctx->ntfs_ino);
1341 	ntfs_attr_put_search_ctx(ctx);
1342 
1343 	/* Sanity checks. */
1344 	if (mft_na->data_size > mft_na->allocated_size ||
1345 	    mft_na->initialized_size > mft_na->data_size)
1346 		NTFS_BUG("mft_na sanity checks failed");
1347 out:
1348 	ntfs_log_leave("\n");
1349 	return ret;
1350 
1351 undo_data_init:
1352 	mft_na->initialized_size = old_data_initialized;
1353 	mft_na->data_size = old_data_size;
1354 	goto out;
1355 }
1356 
1357 static ntfs_inode *ntfs_mft_rec_alloc(ntfs_volume *vol)
1358 {
1359 	s64 ll, bit;
1360 	ntfs_attr *mft_na, *mftbmp_na;
1361 	MFT_RECORD *m;
1362 	ntfs_inode *ni = NULL;
1363 	ntfs_inode *base_ni;
1364 	int err;
1365 	le16 seq_no, usn;
1366 
1367 	ntfs_log_enter("Entering\n");
1368 
1369 	mft_na = vol->mft_na;
1370 	mftbmp_na = vol->mftbmp_na;
1371 
1372 	base_ni = mft_na->ni;
1373 
1374 	bit = ntfs_mft_bitmap_find_free_rec(vol, base_ni);
1375 	if (bit >= 0)
1376 		goto found_free_rec;
1377 
1378 	if (errno != ENOSPC)
1379 		goto out;
1380 
1381 	errno = ENOSPC;
1382 	/* strerror() is intentionally used below, we want to log this error. */
1383 	ntfs_log_error("No free mft record for $MFT: %s\n", strerror(errno));
1384 	goto err_out;
1385 
1386 found_free_rec:
1387 	if (ntfs_bitmap_set_bit(mftbmp_na, bit)) {
1388 		ntfs_log_error("Failed to allocate bit in mft bitmap #2\n");
1389 		goto err_out;
1390 	}
1391 
1392 	ll = (bit + 1) << vol->mft_record_size_bits;
1393 	if (ll > mft_na->initialized_size)
1394 		if (ntfs_mft_rec_init(vol, ll) < 0)
1395 			goto undo_mftbmp_alloc;
1396 	/*
1397 	 * We now have allocated and initialized the mft record.  Need to read
1398 	 * it from disk and re-format it, preserving the sequence number if it
1399 	 * is not zero as well as the update sequence number if it is not zero
1400 	 * or -1 (0xffff).
1401 	 */
1402 	m = ntfs_malloc(vol->mft_record_size);
1403 	if (!m)
1404 		goto undo_mftbmp_alloc;
1405 
1406 	if (ntfs_mft_record_read(vol, bit, m)) {
1407 		free(m);
1408 		goto undo_mftbmp_alloc;
1409 	}
1410 	/* Sanity check that the mft record is really not in use. */
1411 	if (ntfs_is_file_record(m->magic) && (m->flags & MFT_RECORD_IN_USE)) {
1412 		ntfs_log_error("Inode %lld is used but it wasn't marked in "
1413 			       "$MFT bitmap. Fixed.\n", (long long)bit);
1414 		free(m);
1415 		goto undo_mftbmp_alloc;
1416 	}
1417 
1418 	seq_no = m->sequence_number;
1419 	usn = *(le16*)((u8*)m + le16_to_cpu(m->usa_ofs));
1420 	if (ntfs_mft_record_layout(vol, bit, m)) {
1421 		ntfs_log_error("Failed to re-format mft record.\n");
1422 		free(m);
1423 		goto undo_mftbmp_alloc;
1424 	}
1425 	if (seq_no)
1426 		m->sequence_number = seq_no;
1427 	seq_no = usn;
1428 	if (seq_no && seq_no != const_cpu_to_le16(0xffff))
1429 		*(le16*)((u8*)m + le16_to_cpu(m->usa_ofs)) = usn;
1430 	/* Set the mft record itself in use. */
1431 	m->flags |= MFT_RECORD_IN_USE;
1432 	/* Now need to open an ntfs inode for the mft record. */
1433 	ni = ntfs_inode_allocate(vol);
1434 	if (!ni) {
1435 		ntfs_log_error("Failed to allocate buffer for inode.\n");
1436 		free(m);
1437 		goto undo_mftbmp_alloc;
1438 	}
1439 	ni->mft_no = bit;
1440 	ni->mrec = m;
1441 	/*
1442 	 * If we are allocating an extent mft record, make the opened inode an
1443 	 * extent inode and attach it to the base inode.  Also, set the base
1444 	 * mft record reference in the extent inode.
1445 	 */
1446 	ni->nr_extents = -1;
1447 	ni->base_ni = base_ni;
1448 	m->base_mft_record = MK_LE_MREF(base_ni->mft_no,
1449 					le16_to_cpu(base_ni->mrec->sequence_number));
1450 	/*
1451 	 * Attach the extent inode to the base inode, reallocating
1452 	 * memory if needed.
1453 	 */
1454 	if (!(base_ni->nr_extents & 3)) {
1455 		ntfs_inode **extent_nis;
1456 		int i;
1457 
1458 		i = (base_ni->nr_extents + 4) * sizeof(ntfs_inode *);
1459 		extent_nis = ntfs_malloc(i);
1460 		if (!extent_nis) {
1461 			free(m);
1462 			free(ni);
1463 			goto undo_mftbmp_alloc;
1464 		}
1465 		if (base_ni->nr_extents) {
1466 			memcpy(extent_nis, base_ni->extent_nis,
1467 					i - 4 * sizeof(ntfs_inode *));
1468 			free(base_ni->extent_nis);
1469 		}
1470 		base_ni->extent_nis = extent_nis;
1471 	}
1472 	base_ni->extent_nis[base_ni->nr_extents++] = ni;
1473 
1474 	/* Make sure the allocated inode is written out to disk later. */
1475 	ntfs_inode_mark_dirty(ni);
1476 	/* Initialize time, allocated and data size in ntfs_inode struct. */
1477 	ni->data_size = ni->allocated_size = 0;
1478 	ni->flags = 0;
1479 	ni->creation_time = ni->last_data_change_time =
1480 			ni->last_mft_change_time =
1481 			ni->last_access_time = ntfs_current_time();
1482 	/* Update the default mft allocation position if it was used. */
1483 	if (!base_ni)
1484 		vol->mft_data_pos = bit + 1;
1485 	/* Return the opened, allocated inode of the allocated mft record. */
1486 	ntfs_log_error("allocated %sinode %lld\n",
1487 			base_ni ? "extent " : "", (long long)bit);
1488 out:
1489 	ntfs_log_leave("\n");
1490 	return ni;
1491 
1492 undo_mftbmp_alloc:
1493 	err = errno;
1494 	if (ntfs_bitmap_clear_bit(mftbmp_na, bit))
1495 		ntfs_log_error("Failed to clear bit in mft bitmap.%s\n", es);
1496 	errno = err;
1497 err_out:
1498 	if (!errno)
1499 		errno = EIO;
1500 	ni = NULL;
1501 	goto out;
1502 }
1503 
1504 /**
1505  * ntfs_mft_record_alloc - allocate an mft record on an ntfs volume
1506  * @vol:	volume on which to allocate the mft record
1507  * @base_ni:	open base inode if allocating an extent mft record or NULL
1508  *
1509  * Allocate an mft record in $MFT/$DATA of an open ntfs volume @vol.
1510  *
1511  * If @base_ni is NULL make the mft record a base mft record and allocate it at
1512  * the default allocator position.
1513  *
1514  * If @base_ni is not NULL make the allocated mft record an extent record,
1515  * allocate it starting at the mft record after the base mft record and attach
1516  * the allocated and opened ntfs inode to the base inode @base_ni.
1517  *
1518  * On success return the now opened ntfs (extent) inode of the mft record.
1519  *
1520  * On error return NULL with errno set to the error code.
1521  *
1522  * To find a free mft record, we scan the mft bitmap for a zero bit.  To
1523  * optimize this we start scanning at the place specified by @base_ni or if
1524  * @base_ni is NULL we start where we last stopped and we perform wrap around
1525  * when we reach the end.  Note, we do not try to allocate mft records below
1526  * number 24 because numbers 0 to 15 are the defined system files anyway and 16
1527  * to 24 are special in that they are used for storing extension mft records
1528  * for the $DATA attribute of $MFT.  This is required to avoid the possibility
1529  * of creating a run list with a circular dependence which once written to disk
1530  * can never be read in again.  Windows will only use records 16 to 24 for
1531  * normal files if the volume is completely out of space.  We never use them
1532  * which means that when the volume is really out of space we cannot create any
1533  * more files while Windows can still create up to 8 small files.  We can start
1534  * doing this at some later time, it does not matter much for now.
1535  *
1536  * When scanning the mft bitmap, we only search up to the last allocated mft
1537  * record.  If there are no free records left in the range 24 to number of
1538  * allocated mft records, then we extend the $MFT/$DATA attribute in order to
1539  * create free mft records.  We extend the allocated size of $MFT/$DATA by 16
1540  * records at a time or one cluster, if cluster size is above 16kiB.  If there
1541  * is not sufficient space to do this, we try to extend by a single mft record
1542  * or one cluster, if cluster size is above the mft record size, but we only do
1543  * this if there is enough free space, which we know from the values returned
1544  * by the failed cluster allocation function when we tried to do the first
1545  * allocation.
1546  *
1547  * No matter how many mft records we allocate, we initialize only the first
1548  * allocated mft record, incrementing mft data size and initialized size
1549  * accordingly, open an ntfs_inode for it and return it to the caller, unless
1550  * there are less than 24 mft records, in which case we allocate and initialize
1551  * mft records until we reach record 24 which we consider as the first free mft
1552  * record for use by normal files.
1553  *
1554  * If during any stage we overflow the initialized data in the mft bitmap, we
1555  * extend the initialized size (and data size) by 8 bytes, allocating another
1556  * cluster if required.  The bitmap data size has to be at least equal to the
1557  * number of mft records in the mft, but it can be bigger, in which case the
1558  * superfluous bits are padded with zeroes.
1559  *
1560  * Thus, when we return successfully (return value non-zero), we will have:
1561  *	- initialized / extended the mft bitmap if necessary,
1562  *	- initialized / extended the mft data if necessary,
1563  *	- set the bit corresponding to the mft record being allocated in the
1564  *	  mft bitmap,
1565  *	- open an ntfs_inode for the allocated mft record, and we will
1566  *	- return the ntfs_inode.
1567  *
1568  * On error (return value zero), nothing will have changed.  If we had changed
1569  * anything before the error occurred, we will have reverted back to the
1570  * starting state before returning to the caller.  Thus, except for bugs, we
1571  * should always leave the volume in a consistent state when returning from
1572  * this function.
1573  *
1574  * Note, this function cannot make use of most of the normal functions, like
1575  * for example for attribute resizing, etc, because when the run list overflows
1576  * the base mft record and an attribute list is used, it is very important that
1577  * the extension mft records used to store the $DATA attribute of $MFT can be
1578  * reached without having to read the information contained inside them, as
1579  * this would make it impossible to find them in the first place after the
1580  * volume is dismounted.  $MFT/$BITMAP probably does not need to follow this
1581  * rule because the bitmap is not essential for finding the mft records, but on
1582  * the other hand, handling the bitmap in this special way would make life
1583  * easier because otherwise there might be circular invocations of functions
1584  * when reading the bitmap but if we are careful, we should be able to avoid
1585  * all problems.
1586  */
1587 ntfs_inode *ntfs_mft_record_alloc(ntfs_volume *vol, ntfs_inode *base_ni)
1588 {
1589 	s64 ll, bit;
1590 	ntfs_attr *mft_na, *mftbmp_na;
1591 	MFT_RECORD *m;
1592 	ntfs_inode *ni = NULL;
1593 	int err;
1594 	le16 seq_no, usn;
1595 
1596 	if (base_ni)
1597 		ntfs_log_enter("Entering (allocating an extent mft record for "
1598 			       "base mft record %lld).\n",
1599 			       (long long)base_ni->mft_no);
1600 	else
1601 		ntfs_log_enter("Entering (allocating a base mft record)\n");
1602 	if (!vol || !vol->mft_na || !vol->mftbmp_na) {
1603 		errno = EINVAL;
1604 		goto out;
1605 	}
1606 
1607 	if (ntfs_is_mft(base_ni)) {
1608 		ni = ntfs_mft_rec_alloc(vol);
1609 		goto out;
1610 	}
1611 
1612 	mft_na = vol->mft_na;
1613 	mftbmp_na = vol->mftbmp_na;
1614 retry:
1615 	bit = ntfs_mft_bitmap_find_free_rec(vol, base_ni);
1616 	if (bit >= 0) {
1617 		ntfs_log_debug("found free record (#1) at %lld\n",
1618 				(long long)bit);
1619 		goto found_free_rec;
1620 	}
1621 	if (errno != ENOSPC)
1622 		goto out;
1623 	/*
1624 	 * No free mft records left.  If the mft bitmap already covers more
1625 	 * than the currently used mft records, the next records are all free,
1626 	 * so we can simply allocate the first unused mft record.
1627 	 * Note: We also have to make sure that the mft bitmap at least covers
1628 	 * the first 24 mft records as they are special and whilst they may not
1629 	 * be in use, we do not allocate from them.
1630 	 */
1631 	ll = mft_na->initialized_size >> vol->mft_record_size_bits;
1632 	if (mftbmp_na->initialized_size << 3 > ll &&
1633 			mftbmp_na->initialized_size > RESERVED_MFT_RECORDS / 8) {
1634 		bit = ll;
1635 		if (bit < RESERVED_MFT_RECORDS)
1636 			bit = RESERVED_MFT_RECORDS;
1637 		ntfs_log_debug("found free record (#2) at %lld\n",
1638 				(long long)bit);
1639 		goto found_free_rec;
1640 	}
1641 	/*
1642 	 * The mft bitmap needs to be expanded until it covers the first unused
1643 	 * mft record that we can allocate.
1644 	 * Note: The smallest mft record we allocate is mft record 24.
1645 	 */
1646 	ntfs_log_debug("Status of mftbmp before extension: allocated_size 0x%llx, "
1647 			"data_size 0x%llx, initialized_size 0x%llx.\n",
1648 			(long long)mftbmp_na->allocated_size,
1649 			(long long)mftbmp_na->data_size,
1650 			(long long)mftbmp_na->initialized_size);
1651 	if (mftbmp_na->initialized_size + 8 > mftbmp_na->allocated_size) {
1652 
1653 		int ret = ntfs_mft_bitmap_extend_allocation(vol);
1654 
1655 		if (ret == STATUS_ERROR)
1656 			goto err_out;
1657 		if (ret == STATUS_KEEP_SEARCHING) {
1658 			ret = ntfs_mft_bitmap_extend_allocation(vol);
1659 			if (ret != STATUS_OK)
1660 				goto err_out;
1661 		}
1662 
1663 		ntfs_log_debug("Status of mftbmp after allocation extension: "
1664 				"allocated_size 0x%llx, data_size 0x%llx, "
1665 				"initialized_size 0x%llx.\n",
1666 				(long long)mftbmp_na->allocated_size,
1667 				(long long)mftbmp_na->data_size,
1668 				(long long)mftbmp_na->initialized_size);
1669 	}
1670 	/*
1671 	 * We now have sufficient allocated space, extend the initialized_size
1672 	 * as well as the data_size if necessary and fill the new space with
1673 	 * zeroes.
1674 	 */
1675 	bit = mftbmp_na->initialized_size << 3;
1676 	if (ntfs_mft_bitmap_extend_initialized(vol))
1677 		goto err_out;
1678 	ntfs_log_debug("Status of mftbmp after initialized extension: "
1679 			"allocated_size 0x%llx, data_size 0x%llx, "
1680 			"initialized_size 0x%llx.\n",
1681 			(long long)mftbmp_na->allocated_size,
1682 			(long long)mftbmp_na->data_size,
1683 			(long long)mftbmp_na->initialized_size);
1684 	ntfs_log_debug("found free record (#3) at %lld\n", (long long)bit);
1685 found_free_rec:
1686 	/* @bit is the found free mft record, allocate it in the mft bitmap. */
1687 	if (ntfs_bitmap_set_bit(mftbmp_na, bit)) {
1688 		ntfs_log_error("Failed to allocate bit in mft bitmap.\n");
1689 		goto err_out;
1690 	}
1691 
1692 	/* The mft bitmap is now uptodate.  Deal with mft data attribute now. */
1693 	ll = (bit + 1) << vol->mft_record_size_bits;
1694 	if (ll > mft_na->initialized_size)
1695 		if (ntfs_mft_record_init(vol, ll) < 0)
1696 			goto undo_mftbmp_alloc;
1697 
1698 	/*
1699 	 * We now have allocated and initialized the mft record.  Need to read
1700 	 * it from disk and re-format it, preserving the sequence number if it
1701 	 * is not zero as well as the update sequence number if it is not zero
1702 	 * or -1 (0xffff).
1703 	 */
1704 	m = ntfs_malloc(vol->mft_record_size);
1705 	if (!m)
1706 		goto undo_mftbmp_alloc;
1707 
1708 	if (ntfs_mft_record_read(vol, bit, m)) {
1709 		free(m);
1710 		goto undo_mftbmp_alloc;
1711 	}
1712 	/* Sanity check that the mft record is really not in use. */
1713 	if (ntfs_is_file_record(m->magic) && (m->flags & MFT_RECORD_IN_USE)) {
1714 		ntfs_log_error("Inode %lld is used but it wasn't marked in "
1715 			       "$MFT bitmap. Fixed.\n", (long long)bit);
1716 		free(m);
1717 		goto retry;
1718 	}
1719 	seq_no = m->sequence_number;
1720 	usn = *(le16*)((u8*)m + le16_to_cpu(m->usa_ofs));
1721 	if (ntfs_mft_record_layout(vol, bit, m)) {
1722 		ntfs_log_error("Failed to re-format mft record.\n");
1723 		free(m);
1724 		goto undo_mftbmp_alloc;
1725 	}
1726 	if (seq_no)
1727 		m->sequence_number = seq_no;
1728 	seq_no = usn;
1729 	if (seq_no && seq_no != const_cpu_to_le16(0xffff))
1730 		*(le16*)((u8*)m + le16_to_cpu(m->usa_ofs)) = usn;
1731 	/* Set the mft record itself in use. */
1732 	m->flags |= MFT_RECORD_IN_USE;
1733 	/* Now need to open an ntfs inode for the mft record. */
1734 	ni = ntfs_inode_allocate(vol);
1735 	if (!ni) {
1736 		ntfs_log_error("Failed to allocate buffer for inode.\n");
1737 		free(m);
1738 		goto undo_mftbmp_alloc;
1739 	}
1740 	ni->mft_no = bit;
1741 	ni->mrec = m;
1742 	/*
1743 	 * If we are allocating an extent mft record, make the opened inode an
1744 	 * extent inode and attach it to the base inode.  Also, set the base
1745 	 * mft record reference in the extent inode.
1746 	 */
1747 	if (base_ni) {
1748 		ni->nr_extents = -1;
1749 		ni->base_ni = base_ni;
1750 		m->base_mft_record = MK_LE_MREF(base_ni->mft_no,
1751 				le16_to_cpu(base_ni->mrec->sequence_number));
1752 		/*
1753 		 * Attach the extent inode to the base inode, reallocating
1754 		 * memory if needed.
1755 		 */
1756 		if (!(base_ni->nr_extents & 3)) {
1757 			ntfs_inode **extent_nis;
1758 			int i;
1759 
1760 			i = (base_ni->nr_extents + 4) * sizeof(ntfs_inode *);
1761 			extent_nis = ntfs_malloc(i);
1762 			if (!extent_nis) {
1763 				free(m);
1764 				free(ni);
1765 				goto undo_mftbmp_alloc;
1766 			}
1767 			if (base_ni->nr_extents) {
1768 				memcpy(extent_nis, base_ni->extent_nis,
1769 						i - 4 * sizeof(ntfs_inode *));
1770 				free(base_ni->extent_nis);
1771 			}
1772 			base_ni->extent_nis = extent_nis;
1773 		}
1774 		base_ni->extent_nis[base_ni->nr_extents++] = ni;
1775 	}
1776 	/* Make sure the allocated inode is written out to disk later. */
1777 	ntfs_inode_mark_dirty(ni);
1778 	/* Initialize time, allocated and data size in ntfs_inode struct. */
1779 	ni->data_size = ni->allocated_size = 0;
1780 	ni->flags = 0;
1781 	ni->creation_time = ni->last_data_change_time =
1782 			ni->last_mft_change_time =
1783 			ni->last_access_time = ntfs_current_time();
1784 	/* Update the default mft allocation position if it was used. */
1785 	if (!base_ni)
1786 		vol->mft_data_pos = bit + 1;
1787 	/* Return the opened, allocated inode of the allocated mft record. */
1788 	ntfs_log_debug("allocated %sinode 0x%llx.\n",
1789 			base_ni ? "extent " : "", (long long)bit);
1790 	vol->free_mft_records--;
1791 out:
1792 	ntfs_log_leave("\n");
1793 	return ni;
1794 
1795 undo_mftbmp_alloc:
1796 	err = errno;
1797 	if (ntfs_bitmap_clear_bit(mftbmp_na, bit))
1798 		ntfs_log_error("Failed to clear bit in mft bitmap.%s\n", es);
1799 	errno = err;
1800 err_out:
1801 	if (!errno)
1802 		errno = EIO;
1803 	ni = NULL;
1804 	goto out;
1805 }
1806 
1807 /**
1808  * ntfs_mft_record_free - free an mft record on an ntfs volume
1809  * @vol:	volume on which to free the mft record
1810  * @ni:		open ntfs inode of the mft record to free
1811  *
1812  * Free the mft record of the open inode @ni on the mounted ntfs volume @vol.
1813  * Note that this function calls ntfs_inode_close() internally and hence you
1814  * cannot use the pointer @ni any more after this function returns success.
1815  *
1816  * On success return 0 and on error return -1 with errno set to the error code.
1817  */
1818 int ntfs_mft_record_free(ntfs_volume *vol, ntfs_inode *ni)
1819 {
1820 	u64 mft_no;
1821 	int err;
1822 	u16 seq_no;
1823 	le16 old_seq_no;
1824 
1825 	ntfs_log_trace("Entering for inode 0x%llx.\n", (long long) ni->mft_no);
1826 
1827 	if (!vol || !vol->mftbmp_na || !ni) {
1828 		errno = EINVAL;
1829 		return -1;
1830 	}
1831 
1832 	/* Cache the mft reference for later. */
1833 	mft_no = ni->mft_no;
1834 
1835 	/* Mark the mft record as not in use. */
1836 	ni->mrec->flags &= ~MFT_RECORD_IN_USE;
1837 
1838 	/* Increment the sequence number, skipping zero, if it is not zero. */
1839 	old_seq_no = ni->mrec->sequence_number;
1840 	seq_no = le16_to_cpu(old_seq_no);
1841 	if (seq_no == 0xffff)
1842 		seq_no = 1;
1843 	else if (seq_no)
1844 		seq_no++;
1845 	ni->mrec->sequence_number = cpu_to_le16(seq_no);
1846 
1847 	/* Set the inode dirty and write it out. */
1848 	ntfs_inode_mark_dirty(ni);
1849 	if (ntfs_inode_sync(ni)) {
1850 		err = errno;
1851 		goto sync_rollback;
1852 	}
1853 
1854 	/* Clear the bit in the $MFT/$BITMAP corresponding to this record. */
1855 	if (ntfs_bitmap_clear_bit(vol->mftbmp_na, mft_no)) {
1856 		err = errno;
1857 		// FIXME: If ntfs_bitmap_clear_run() guarantees rollback on
1858 		//	  error, this could be changed to goto sync_rollback;
1859 		goto bitmap_rollback;
1860 	}
1861 
1862 	/* Throw away the now freed inode. */
1863 #if CACHE_NIDATA_SIZE
1864 	if (!ntfs_inode_real_close(ni)) {
1865 #else
1866 	if (!ntfs_inode_close(ni)) {
1867 #endif
1868 		vol->free_mft_records++;
1869 		return 0;
1870 	}
1871 	err = errno;
1872 
1873 	/* Rollback what we did... */
1874 bitmap_rollback:
1875 	if (ntfs_bitmap_set_bit(vol->mftbmp_na, mft_no))
1876 		ntfs_log_debug("Eeek! Rollback failed in ntfs_mft_record_free().  "
1877 				"Leaving inconsistent metadata!\n");
1878 sync_rollback:
1879 	ni->mrec->flags |= MFT_RECORD_IN_USE;
1880 	ni->mrec->sequence_number = old_seq_no;
1881 	ntfs_inode_mark_dirty(ni);
1882 	errno = err;
1883 	return -1;
1884 }
1885 
1886 /**
1887  * ntfs_mft_usn_dec - Decrement USN by one
1888  * @mrec:	pointer to an mft record
1889  *
1890  * On success return 0 and on error return -1 with errno set.
1891  */
1892 int ntfs_mft_usn_dec(MFT_RECORD *mrec)
1893 {
1894 	u16 usn;
1895 	le16 *usnp;
1896 
1897 	if (!mrec) {
1898 		errno = EINVAL;
1899 		return -1;
1900 	}
1901 	usnp = (le16*)((char*)mrec + le16_to_cpu(mrec->usa_ofs));
1902 	usn = le16_to_cpup(usnp);
1903 	if (usn-- <= 1)
1904 		usn = 0xfffe;
1905 	*usnp = cpu_to_le16(usn);
1906 
1907 	return 0;
1908 }
1909 
1910