2 * mft.c - Mft record handling code. Originated from the Linux-NTFS project.
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
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.
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.
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
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
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
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.
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.
79 * NOTE: @b has to be at least of size @count * vol->mft_record_size.
81 int ntfs_mft_records_read(const ntfs_volume
*vol
, const MFT_REF mref
,
82 const s64 count
, MFT_RECORD
*b
)
87 ntfs_log_trace("inode %llu\n", (unsigned long long)MREF(mref
));
89 if (!vol
|| !vol
->mft_na
|| !b
|| count
< 0) {
91 ntfs_log_perror("%s: b=%p count=%lld mft=%llu", __FUNCTION__
,
92 b
, (long long)count
, (unsigned long long)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
) {
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
);
106 br
= ntfs_attr_mst_pread(vol
->mft_na
, m
<< vol
->mft_record_size_bits
,
107 count
, vol
->mft_record_size
, b
);
111 ntfs_log_perror("Failed to read of MFT, mft=%llu count=%lld "
112 "br=%lld", (long long)m
, (long long)count
,
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
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.
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.
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.
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.
143 int ntfs_mft_records_write(const ntfs_volume
*vol
, const MFT_REF mref
,
144 const s64 count
, MFT_RECORD
*b
)
149 int cnt
= 0, res
= 0;
151 if (!vol
|| !vol
->mft_na
|| vol
->mftmirr_size
<= 0 || !b
|| count
< 0) {
156 /* Refuse to write non-allocated mft records. */
157 if (m
+ count
> vol
->mft_na
->initialized_size
>>
158 vol
->mft_record_size_bits
) {
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
);
166 if (m
< vol
->mftmirr_size
) {
167 if (!vol
->mftmirr_na
) {
171 cnt
= vol
->mftmirr_size
- m
;
174 bmirr
= ntfs_malloc(cnt
* vol
->mft_record_size
);
177 memcpy(bmirr
, b
, cnt
* vol
->mft_record_size
);
179 bw
= ntfs_attr_mst_pwrite(vol
->mft_na
, m
<< vol
->mft_record_size_bits
,
180 count
, vol
->mft_record_size
, b
);
185 ntfs_log_debug("Error: partial write while writing $Mft "
188 ntfs_log_perror("Error writing $Mft record(s)");
191 if (bmirr
&& bw
> 0) {
194 bw
= ntfs_attr_mst_pwrite(vol
->mftmirr_na
,
195 m
<< vol
->mft_record_size_bits
, cnt
,
196 vol
->mft_record_size
, bmirr
);
200 ntfs_log_debug("Error: failed to sync $MFTMirr! Run "
212 int ntfs_mft_record_check(const ntfs_volume
*vol
, const MFT_REF mref
,
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
);
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
));
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
));
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
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.
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.
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.
264 * Return 0 on success, or -1 on error, with errno set to the error code.
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.
272 * Note: Caller has to free *@mrec when finished.
274 * Note: We do not check if the mft record is flagged in use. The caller can
277 int ntfs_file_record_read(const ntfs_volume
*vol
, const MFT_REF mref
,
278 MFT_RECORD
**mrec
, ATTR_RECORD
**attr
)
284 ntfs_log_perror("%s: mrec=%p", __FUNCTION__
, mrec
);
290 m
= ntfs_malloc(vol
->mft_record_size
);
294 if (ntfs_mft_record_read(vol
, mref
, m
))
297 if (ntfs_mft_record_check(vol
, mref
, m
))
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
));
309 *attr
= (ATTR_RECORD
*)((char*)m
+ le16_to_cpu(m
->attrs_offset
));
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
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
328 * On success return 0 and on error return -1 with errno set to the error code.
330 int ntfs_mft_record_layout(const ntfs_volume
*vol
, const MFT_REF mref
,
337 ntfs_log_perror("%s: mrec=%p", __FUNCTION__
, mrec
);
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);
344 /* Abort if mref is > 32 bits. */
345 if (MREF(mref
) & 0x0000ffff00000000ull
) {
347 ntfs_log_perror("Mft reference exceeds 32 bits");
350 mrec
->usa_ofs
= cpu_to_le16((sizeof(MFT_RECORD
) + 1) & ~1);
352 * Set the NTFS 3.1+ specific fields while we know that the
353 * volume version is 3.1+.
355 mrec
->reserved
= cpu_to_le16(0);
356 mrec
->mft_record_number
= cpu_to_le32(MREF(mref
));
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);
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
);
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);
381 * Using attrs_offset plus eight bytes (for the termination attribute),
382 * aligned to 8-byte boundary.
384 mrec
->bytes_in_use
= cpu_to_le32((le16_to_cpu(mrec
->attrs_offset
) + 8 +
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
));
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
));
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
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.
405 * On success return 0 and on error return -1 with errno set to the error code.
407 int ntfs_mft_record_format(const ntfs_volume
*vol
, const MFT_REF mref
)
412 ntfs_log_enter("Entering\n");
414 m
= ntfs_calloc(vol
->mft_record_size
);
418 if (ntfs_mft_record_layout(vol
, mref
, m
))
421 if (ntfs_mft_record_write(vol
, mref
, m
))
428 ntfs_log_leave("\n");
432 static const char *es
= " Leaving inconsistent metadata. Run chkdsk.";
435 * ntfs_ffz - Find the first unset (zero) bit in a word
442 static inline unsigned int ntfs_ffz(unsigned int word
)
444 return ffs(~word
) - 1;
447 static int ntfs_is_mft(ntfs_inode
*ni
)
449 if (ni
&& ni
->mft_no
== FILE_MFT
)
455 #define PAGE_SIZE 4096
458 #define RESERVED_MFT_RECORDS 64
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
465 * Search for a free mft record in the mft bitmap attribute on the ntfs volume
468 * If @base_ni is NULL start the search at the default allocator position.
470 * If @base_ni is not NULL start the search at the mft record after the base
471 * mft record @base_ni.
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.
477 static int ntfs_mft_bitmap_find_free_rec(ntfs_volume
*vol
, ntfs_inode
*base_ni
)
479 s64 pass_end
, ll
, data_pos
, pass_start
, ofs
, bit
;
480 ntfs_attr
*mftbmp_na
;
486 ntfs_log_enter("Entering\n");
488 mftbmp_na
= vol
->mftbmp_na
;
490 * Set the end of the pass making sure we do not overflow the mft
494 pass_end
= vol
->mft_na
->allocated_size
>> vol
->mft_record_size_bits
;
495 ll
= mftbmp_na
->initialized_size
<< 3;
500 data_pos
= vol
->mft_data_pos
;
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
;
508 /* This happens on a freshly formatted volume. */
509 if (data_pos
>= pass_end
) {
514 if (ntfs_is_mft(base_ni
)) {
518 pass_start
= data_pos
;
519 buf
= ntfs_malloc(PAGE_SIZE
);
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
);
531 /* Loop until a free mft record is found. */
532 for (; pass
<= 2; size
= PAGE_SIZE
) {
533 /* Cap size to pass_end. */
535 ll
= ((pass_end
+ 7) >> 3) - ofs
;
538 ll
= ntfs_attr_pread(mftbmp_na
, ofs
, size
, buf
);
540 ntfs_log_perror("Failed to read $MFT bitmap");
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. */
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) {
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.
562 if (ntfs_is_mft(base_ni
) && bit
> 400)
565 byte
= buf
+ (bit
>> 3);
569 /* Note: ffz() result must be zero based. */
570 b
= ntfs_ffz((unsigned long)*byte
);
571 if (b
< 8 && b
>= (bit
& 7)) {
573 ret
= data_pos
+ (bit
& ~7ull) + b
;
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
);
584 * If the end of the pass has not been reached yet,
585 * continue searching the mft bitmap for a zero bit.
587 if (data_pos
< pass_end
)
590 /* Do the next pass. */
594 * Starting the second pass, in which we scan the first
595 * part of the zone which we omitted earlier.
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
)
606 /* No free mft records in currently initialized mft bitmap. */
611 ntfs_log_leave("\n");
615 static int ntfs_mft_attr_extend(ntfs_attr
*na
)
617 int ret
= STATUS_ERROR
;
618 ntfs_log_enter("Entering\n");
620 if (!NInoAttrList(na
->ni
)) {
621 if (ntfs_inode_add_attrlist(na
->ni
)) {
622 ntfs_log_perror("%s: Can not add attrlist #3", __FUNCTION__
);
625 /* We can't sync the $MFT inode since its runlist is bogus. */
626 ret
= STATUS_KEEP_SEARCHING
;
630 if (ntfs_attr_update_mapping_pairs(na
, 0)) {
631 ntfs_log_perror("%s: MP update failed", __FUNCTION__
);
637 ntfs_log_leave("\n");
642 * ntfs_mft_bitmap_extend_allocation_i - see ntfs_mft_bitmap_extend_allocation
644 static int ntfs_mft_bitmap_extend_allocation_i(ntfs_volume
*vol
)
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 */
654 int ret
= STATUS_ERROR
;
655 u32 old_alen
= 0; /* silence compiler warning */
656 BOOL mp_rebuilt
= FALSE
;
657 BOOL update_mp
= FALSE
;
659 mftbmp_na
= vol
->mftbmp_na
;
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.
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");
673 lcn
= rl
->lcn
+ rl
->length
;
675 rl2
= ntfs_cluster_alloc(vol
, rl
[1].vcn
, 1, lcn
, DATA_ZONE
);
677 ntfs_log_error("Failed to allocate a cluster for "
678 "the mft bitmap.\n");
681 rl
= ntfs_runlists_merge(mftbmp_na
->rl
, rl2
);
684 ntfs_log_error("Failed to merge runlists for mft "
686 if (ntfs_cluster_free_from_rl(vol
, rl2
))
687 ntfs_log_error("Failed to deallocate "
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
++)
699 * Update the attribute record as well. Note: @rl is the last
700 * (non-terminator) runlist element of mft bitmap.
702 ctx
= ntfs_attr_get_search_ctx(mftbmp_na
->ni
, NULL
);
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");
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");
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
);
726 ntfs_log_error("Get size for mapping pairs failed for "
727 "mft bitmap attribute extent.\n");
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
)
738 if (ret
== STATUS_ERROR
) {
739 ntfs_log_perror("%s: ntfs_mft_attr_extend failed", __FUNCTION__
);
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
,
749 ntfs_log_error("Failed to build mapping pairs array for "
750 "mft bitmap attribute.\n");
754 /* Update the highest_vcn. */
755 a
->highest_vcn
= cpu_to_sle64(rl
[1].vcn
- 1);
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.
762 * We are not in the first attribute extent, switch to it, but
763 * first ensure the changes will make it to disk later.
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
;
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
);
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
;
793 * The only thing that is now wrong is ->allocated_size of the
794 * base attribute extent which chkdsk should be able to fix.
801 a
->highest_vcn
= cpu_to_sle64(rl
[1].vcn
- 2);
806 /* Remove the last run from the runlist. */
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
);
815 vol
->free_clusters
++;
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
),
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 "
826 ntfs_inode_mark_dirty(ctx
->ntfs_ino
);
829 if (ntfs_attr_update_mapping_pairs(vol
->mftbmp_na
, 0))
830 ntfs_log_perror("%s: MP update failed", __FUNCTION__
);
833 ntfs_attr_put_search_ctx(ctx
);
839 * ntfs_mft_bitmap_extend_allocation - extend mft bitmap attribute by a cluster
840 * @vol: volume on which to extend the mft bitmap attribute
842 * Extend the mft bitmap attribute on the ntfs volume @vol by one cluster.
844 * Note: Only changes allocated_size, i.e. does not touch initialized_size or
847 * Return 0 on success and -1 on error with errno set to the error code.
849 static int ntfs_mft_bitmap_extend_allocation(ntfs_volume
*vol
)
853 ntfs_log_enter("Entering\n");
854 ret
= ntfs_mft_bitmap_extend_allocation_i(vol
);
855 ntfs_log_leave("\n");
859 * ntfs_mft_bitmap_extend_initialized - extend mft bitmap initialized data
860 * @vol: volume on which to extend the mft bitmap attribute
862 * Extend the initialized portion of the mft bitmap attribute on the ntfs
863 * volume @vol by 8 bytes.
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.
868 * Return 0 on success and -1 on error with errno set to the error code.
870 static int ntfs_mft_bitmap_extend_initialized(ntfs_volume
*vol
)
872 s64 old_data_size
, old_initialized_size
, ll
;
873 ntfs_attr
*mftbmp_na
;
874 ntfs_attr_search_ctx
*ctx
;
879 ntfs_log_enter("Entering\n");
881 mftbmp_na
= vol
->mftbmp_na
;
882 ctx
= ntfs_attr_get_search_ctx(mftbmp_na
->ni
, NULL
);
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");
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
);
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. */
907 ll
= ntfs_attr_pwrite(mftbmp_na
, old_initialized_size
, 8, &ll
);
909 ntfs_log_debug("Wrote eight initialized bytes to mft bitmap.\n");
910 vol
->free_mft_records
+= (8 * 8);
914 ntfs_log_error("Failed to write to mft bitmap.\n");
918 /* Try to recover from the error. */
919 ctx
= ntfs_attr_get_search_ctx(mftbmp_na
->ni
, NULL
);
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
);
928 ntfs_attr_put_search_ctx(ctx
);
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
);
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
);
948 ntfs_log_leave("\n");
953 * ntfs_mft_data_extend_allocation - extend mft data attribute
954 * @vol: volume on which to extend the mft data attribute
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
960 * Note: Only changes allocated_size, i.e. does not touch initialized_size or
963 * Return 0 on success and -1 on error with errno set to the error code.
965 static int ntfs_mft_data_extend_allocation(ntfs_volume
*vol
)
969 s64 min_nr
, nr
, ll
= 0; /* silence compiler warning */
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 */
976 int ret
= STATUS_ERROR
;
977 u32 old_alen
= 0; /* silence compiler warning */
978 BOOL mp_rebuilt
= FALSE
;
979 BOOL update_mp
= FALSE
;
981 ntfs_log_enter("Extending mft data allocation.\n");
983 mft_na
= vol
->mft_na
;
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.
989 rl
= ntfs_attr_find_vcn(mft_na
,
990 (mft_na
->allocated_size
- 1) >> vol
->cluster_size_bits
);
992 if (!rl
|| !rl
->length
|| rl
->lcn
< 0) {
993 ntfs_log_error("Failed to determine last allocated "
994 "cluster of mft data attribute.\n");
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
;
1006 /* Want to allocate 16 mft records worth of clusters. */
1007 nr
= vol
->mft_record_size
<< 4 >> vol
->cluster_size_bits
;
1011 old_last_vcn
= rl
[1].vcn
;
1013 rl2
= ntfs_cluster_alloc(vol
, old_last_vcn
, nr
, lcn
, MFT_ZONE
);
1016 if (errno
!= ENOSPC
|| nr
== min_nr
) {
1017 ntfs_log_perror("Failed to allocate (%lld) clusters "
1018 "for $MFT", (long long)nr
);
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
1027 ntfs_log_debug("Retrying mft data allocation with minimal cluster "
1028 "count %lli.\n", (long long)nr
);
1031 ntfs_log_debug("Allocated %lld clusters.\n", (long long)nr
);
1033 rl
= ntfs_runlists_merge(mft_na
->rl
, rl2
);
1036 ntfs_log_error("Failed to merge runlists for mft data "
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
);
1047 /* Find the last run in the new runlist. */
1048 for (; rl
[1].length
; rl
++)
1050 /* Update the attribute record as well. */
1051 ctx
= ntfs_attr_get_search_ctx(mft_na
->ni
, NULL
);
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");
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");
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
);
1075 ntfs_log_error("Get size for mapping pairs failed for "
1076 "mft data attribute extent.\n");
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
)
1086 if (ret
== STATUS_ERROR
) {
1087 ntfs_log_perror("%s: ntfs_mft_attr_extend failed", __FUNCTION__
);
1094 * Generate the mapping pairs array directly into the attribute record.
1096 if (ntfs_mapping_pairs_build(vol
,
1097 (u8
*)a
+ le16_to_cpu(a
->mapping_pairs_offset
), mp_size
,
1099 ntfs_log_error("Failed to build mapping pairs array of "
1100 "mft data attribute.\n");
1104 /* Update the highest_vcn. */
1105 a
->highest_vcn
= cpu_to_sle64(rl
[1].vcn
- 1);
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
1112 if (a
->lowest_vcn
) {
1114 * We are not in the first attribute extent, switch to it, but
1115 * first ensure the changes will make it to disk later.
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
;
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
);
1135 ntfs_log_leave("\n");
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
;
1148 * The only thing that is now wrong is ->allocated_size of the
1149 * base attribute extent which chkdsk should be able to fix.
1157 a
->highest_vcn
= cpu_to_sle64(old_last_vcn
- 1);
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
);
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
),
1172 ntfs_log_error("Failed to restore mapping pairs "
1174 if (ntfs_attr_record_resize(m
, a
, old_alen
))
1175 ntfs_log_error("Failed to restore attribute "
1177 ntfs_inode_mark_dirty(ctx
->ntfs_ino
);
1180 if (ntfs_attr_update_mapping_pairs(vol
->mft_na
, 0))
1181 ntfs_log_perror("%s: MP update failed", __FUNCTION__
);
1184 ntfs_attr_put_search_ctx(ctx
);
1190 static int ntfs_mft_record_init(ntfs_volume
*vol
, s64 size
)
1193 ntfs_attr
*mft_na
, *mftbmp_na
;
1194 s64 old_data_initialized
, old_data_size
;
1195 ntfs_attr_search_ctx
*ctx
;
1197 ntfs_log_enter("Entering\n");
1199 /* NOTE: Caller must sanity check vol, vol->mft_na and vol->mftbmp_na */
1201 mft_na
= vol
->mft_na
;
1202 mftbmp_na
= vol
->mftbmp_na
;
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.
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
)
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
);
1227 old_data_initialized
= mft_na
->initialized_size
;
1228 old_data_size
= mft_na
->data_size
;
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.
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
;
1249 /* Update the mft data attribute record to reflect the new sizes. */
1250 ctx
= ntfs_attr_get_search_ctx(mft_na
->ni
, NULL
);
1252 goto undo_data_init
;
1254 if (ntfs_attr_lookup(mft_na
->type
, mft_na
->name
, mft_na
->name_len
, 0,
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
;
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
);
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
);
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");
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
;
1286 ntfs_log_leave("\n");
1290 mft_na
->initialized_size
= old_data_initialized
;
1291 mft_na
->data_size
= old_data_size
;
1295 static int ntfs_mft_rec_init(ntfs_volume
*vol
, s64 size
)
1298 ntfs_attr
*mft_na
, *mftbmp_na
;
1299 s64 old_data_initialized
, old_data_size
;
1300 ntfs_attr_search_ctx
*ctx
;
1302 ntfs_log_enter("Entering\n");
1304 mft_na
= vol
->mft_na
;
1305 mftbmp_na
= vol
->mftbmp_na
;
1307 if (size
> mft_na
->allocated_size
|| size
> mft_na
->initialized_size
) {
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",
1313 (long long)mft_na
->allocated_size
,
1314 (long long)mft_na
->data_size
,
1315 (long long)mft_na
->initialized_size
);
1319 old_data_initialized
= mft_na
->initialized_size
;
1320 old_data_size
= mft_na
->data_size
;
1322 /* Update the mft data attribute record to reflect the new sizes. */
1323 ctx
= ntfs_attr_get_search_ctx(mft_na
->ni
, NULL
);
1325 goto undo_data_init
;
1327 if (ntfs_attr_lookup(mft_na
->type
, mft_na
->name
, mft_na
->name_len
, 0,
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
;
1334 ctx
->attr
->initialized_size
= cpu_to_sle64(mft_na
->initialized_size
);
1335 ctx
->attr
->data_size
= cpu_to_sle64(mft_na
->data_size
);
1337 /* CHECKME: ctx->attr->allocation_size is already ok? */
1339 /* Ensure the changes make it to disk. */
1340 ntfs_inode_mark_dirty(ctx
->ntfs_ino
);
1341 ntfs_attr_put_search_ctx(ctx
);
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");
1348 ntfs_log_leave("\n");
1352 mft_na
->initialized_size
= old_data_initialized
;
1353 mft_na
->data_size
= old_data_size
;
1357 static ntfs_inode
*ntfs_mft_rec_alloc(ntfs_volume
*vol
)
1360 ntfs_attr
*mft_na
, *mftbmp_na
;
1362 ntfs_inode
*ni
= NULL
;
1363 ntfs_inode
*base_ni
;
1367 ntfs_log_enter("Entering\n");
1369 mft_na
= vol
->mft_na
;
1370 mftbmp_na
= vol
->mftbmp_na
;
1372 base_ni
= mft_na
->ni
;
1374 bit
= ntfs_mft_bitmap_find_free_rec(vol
, base_ni
);
1376 goto found_free_rec
;
1378 if (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
));
1387 if (ntfs_bitmap_set_bit(mftbmp_na
, bit
)) {
1388 ntfs_log_error("Failed to allocate bit in mft bitmap #2\n");
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
;
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
1402 m
= ntfs_malloc(vol
->mft_record_size
);
1404 goto undo_mftbmp_alloc
;
1406 if (ntfs_mft_record_read(vol
, bit
, m
)) {
1408 goto undo_mftbmp_alloc
;
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
);
1415 goto undo_mftbmp_alloc
;
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");
1423 goto undo_mftbmp_alloc
;
1426 m
->sequence_number
= seq_no
;
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
);
1435 ntfs_log_error("Failed to allocate buffer for inode.\n");
1437 goto undo_mftbmp_alloc
;
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.
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
));
1451 * Attach the extent inode to the base inode, reallocating
1454 if (!(base_ni
->nr_extents
& 3)) {
1455 ntfs_inode
**extent_nis
;
1458 i
= (base_ni
->nr_extents
+ 4) * sizeof(ntfs_inode
*);
1459 extent_nis
= ntfs_malloc(i
);
1463 goto undo_mftbmp_alloc
;
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
);
1470 base_ni
->extent_nis
= extent_nis
;
1472 base_ni
->extent_nis
[base_ni
->nr_extents
++] = ni
;
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;
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. */
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
);
1489 ntfs_log_leave("\n");
1494 if (ntfs_bitmap_clear_bit(mftbmp_na
, bit
))
1495 ntfs_log_error("Failed to clear bit in mft bitmap.%s\n", es
);
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
1509 * Allocate an mft record in $MFT/$DATA of an open ntfs volume @vol.
1511 * If @base_ni is NULL make the mft record a base mft record and allocate it at
1512 * the default allocator position.
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.
1518 * On success return the now opened ntfs (extent) inode of the mft record.
1520 * On error return NULL with errno set to the error code.
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.
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
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.
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.
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
1565 * - open an ntfs_inode for the allocated mft record, and we will
1566 * - return the ntfs_inode.
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
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
1587 ntfs_inode
*ntfs_mft_record_alloc(ntfs_volume
*vol
, ntfs_inode
*base_ni
)
1590 ntfs_attr
*mft_na
, *mftbmp_na
;
1592 ntfs_inode
*ni
= NULL
;
1597 ntfs_log_enter("Entering (allocating an extent mft record for "
1598 "base mft record %lld).\n",
1599 (long long)base_ni
->mft_no
);
1601 ntfs_log_enter("Entering (allocating a base mft record)\n");
1602 if (!vol
|| !vol
->mft_na
|| !vol
->mftbmp_na
) {
1607 if (ntfs_is_mft(base_ni
)) {
1608 ni
= ntfs_mft_rec_alloc(vol
);
1612 mft_na
= vol
->mft_na
;
1613 mftbmp_na
= vol
->mftbmp_na
;
1615 bit
= ntfs_mft_bitmap_find_free_rec(vol
, base_ni
);
1617 ntfs_log_debug("found free record (#1) at %lld\n",
1619 goto found_free_rec
;
1621 if (errno
!= ENOSPC
)
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.
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) {
1635 if (bit
< RESERVED_MFT_RECORDS
)
1636 bit
= RESERVED_MFT_RECORDS
;
1637 ntfs_log_debug("found free record (#2) at %lld\n",
1639 goto found_free_rec
;
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.
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
) {
1653 int ret
= ntfs_mft_bitmap_extend_allocation(vol
);
1655 if (ret
== STATUS_ERROR
)
1657 if (ret
== STATUS_KEEP_SEARCHING
) {
1658 ret
= ntfs_mft_bitmap_extend_allocation(vol
);
1659 if (ret
!= STATUS_OK
)
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
);
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
1675 bit
= mftbmp_na
->initialized_size
<< 3;
1676 if (ntfs_mft_bitmap_extend_initialized(vol
))
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
);
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");
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
;
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
1704 m
= ntfs_malloc(vol
->mft_record_size
);
1706 goto undo_mftbmp_alloc
;
1708 if (ntfs_mft_record_read(vol
, bit
, m
)) {
1710 goto undo_mftbmp_alloc
;
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
);
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");
1724 goto undo_mftbmp_alloc
;
1727 m
->sequence_number
= seq_no
;
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
);
1736 ntfs_log_error("Failed to allocate buffer for inode.\n");
1738 goto undo_mftbmp_alloc
;
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.
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
));
1753 * Attach the extent inode to the base inode, reallocating
1756 if (!(base_ni
->nr_extents
& 3)) {
1757 ntfs_inode
**extent_nis
;
1760 i
= (base_ni
->nr_extents
+ 4) * sizeof(ntfs_inode
*);
1761 extent_nis
= ntfs_malloc(i
);
1765 goto undo_mftbmp_alloc
;
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
);
1772 base_ni
->extent_nis
= extent_nis
;
1774 base_ni
->extent_nis
[base_ni
->nr_extents
++] = ni
;
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;
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. */
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
--;
1792 ntfs_log_leave("\n");
1797 if (ntfs_bitmap_clear_bit(mftbmp_na
, bit
))
1798 ntfs_log_error("Failed to clear bit in mft bitmap.%s\n", es
);
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
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.
1816 * On success return 0 and on error return -1 with errno set to the error code.
1818 int ntfs_mft_record_free(ntfs_volume
*vol
, ntfs_inode
*ni
)
1825 ntfs_log_trace("Entering for inode 0x%llx.\n", (long long) ni
->mft_no
);
1827 if (!vol
|| !vol
->mftbmp_na
|| !ni
) {
1832 /* Cache the mft reference for later. */
1833 mft_no
= ni
->mft_no
;
1835 /* Mark the mft record as not in use. */
1836 ni
->mrec
->flags
&= ~MFT_RECORD_IN_USE
;
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)
1845 ni
->mrec
->sequence_number
= cpu_to_le16(seq_no
);
1847 /* Set the inode dirty and write it out. */
1848 ntfs_inode_mark_dirty(ni
);
1849 if (ntfs_inode_sync(ni
)) {
1854 /* Clear the bit in the $MFT/$BITMAP corresponding to this record. */
1855 if (ntfs_bitmap_clear_bit(vol
->mftbmp_na
, mft_no
)) {
1857 // FIXME: If ntfs_bitmap_clear_run() guarantees rollback on
1858 // error, this could be changed to goto sync_rollback;
1859 goto bitmap_rollback
;
1862 /* Throw away the now freed inode. */
1863 #if CACHE_NIDATA_SIZE
1864 if (!ntfs_inode_real_close(ni
)) {
1866 if (!ntfs_inode_close(ni
)) {
1868 vol
->free_mft_records
++;
1873 /* Rollback what we did... */
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");
1879 ni
->mrec
->flags
|= MFT_RECORD_IN_USE
;
1880 ni
->mrec
->sequence_number
= old_seq_no
;
1881 ntfs_inode_mark_dirty(ni
);
1887 * ntfs_mft_usn_dec - Decrement USN by one
1888 * @mrec: pointer to an mft record
1890 * On success return 0 and on error return -1 with errno set.
1892 int ntfs_mft_usn_dec(MFT_RECORD
*mrec
)
1901 usnp
= (le16
*)((char*)mrec
+ le16_to_cpu(mrec
->usa_ofs
));
1902 usn
= le16_to_cpup(usnp
);
1905 *usnp
= cpu_to_le16(usn
);