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 if (!NVolNoFixupWarn(vol
))
220 ntfs_log_error("Record %llu has no FILE magic (0x%x)\n",
221 (unsigned long long)MREF(mref
),
222 (int)le32_to_cpu(*(le32
*)m
));
226 if (le32_to_cpu(m
->bytes_allocated
) != vol
->mft_record_size
) {
227 ntfs_log_error("Record %llu has corrupt allocation size "
228 "(%u <> %u)\n", (unsigned long long)MREF(mref
),
229 vol
->mft_record_size
,
230 le32_to_cpu(m
->bytes_allocated
));
234 a
= (ATTR_RECORD
*)((char *)m
+ le16_to_cpu(m
->attrs_offset
));
235 if (p2n(a
) < p2n(m
) || (char *)a
> (char *)m
+ vol
->mft_record_size
) {
236 ntfs_log_error("Record %llu is corrupt\n",
237 (unsigned long long)MREF(mref
));
249 * ntfs_file_record_read - read a FILE record from the mft from disk
250 * @vol: volume to read from
251 * @mref: mft reference specifying mft record to read
252 * @mrec: address of pointer in which to return the mft record
253 * @attr: address of pointer in which to return the first attribute
255 * Read a FILE record from the mft of @vol from the storage medium. @mref
256 * specifies the mft record to read, including the sequence number, which can
257 * be 0 if no sequence number checking is to be performed.
259 * The function allocates a buffer large enough to hold the mft record and
260 * reads the record into the buffer (mst deprotecting it in the process).
261 * *@mrec is then set to point to the buffer.
263 * If @attr is not NULL, *@attr is set to point to the first attribute in the
264 * mft record, i.e. *@attr is a pointer into *@mrec.
266 * Return 0 on success, or -1 on error, with errno set to the error code.
268 * The read mft record is checked for having the magic FILE,
269 * and for having a matching sequence number (if MSEQNO(*@mref) != 0).
270 * If either of these fails, -1 is returned and errno is set to EIO. If you get
271 * this, but you still want to read the mft record (e.g. in order to correct
272 * it), use ntfs_mft_record_read() directly.
274 * Note: Caller has to free *@mrec when finished.
276 * Note: We do not check if the mft record is flagged in use. The caller can
279 int ntfs_file_record_read(const ntfs_volume
*vol
, const MFT_REF mref
,
280 MFT_RECORD
**mrec
, ATTR_RECORD
**attr
)
286 ntfs_log_perror("%s: mrec=%p", __FUNCTION__
, mrec
);
292 m
= ntfs_malloc(vol
->mft_record_size
);
296 if (ntfs_mft_record_read(vol
, mref
, m
))
299 if (ntfs_mft_record_check(vol
, mref
, m
))
302 if (MSEQNO(mref
) && MSEQNO(mref
) != le16_to_cpu(m
->sequence_number
)) {
303 ntfs_log_error("Record %llu has wrong SeqNo (%d <> %d)\n",
304 (unsigned long long)MREF(mref
), MSEQNO(mref
),
305 le16_to_cpu(m
->sequence_number
));
311 *attr
= (ATTR_RECORD
*)((char*)m
+ le16_to_cpu(m
->attrs_offset
));
320 * ntfs_mft_record_layout - layout an mft record into a memory buffer
321 * @vol: volume to which the mft record will belong
322 * @mref: mft reference specifying the mft record number
323 * @mrec: destination buffer of size >= @vol->mft_record_size bytes
325 * Layout an empty, unused mft record with the mft reference @mref into the
326 * buffer @m. The volume @vol is needed because the mft record structure was
327 * modified in NTFS 3.1 so we need to know which volume version this mft record
330 * On success return 0 and on error return -1 with errno set to the error code.
332 int ntfs_mft_record_layout(const ntfs_volume
*vol
, const MFT_REF mref
,
339 ntfs_log_perror("%s: mrec=%p", __FUNCTION__
, mrec
);
342 /* Aligned to 2-byte boundary. */
343 if (vol
->major_ver
< 3 || (vol
->major_ver
== 3 && !vol
->minor_ver
))
344 mrec
->usa_ofs
= cpu_to_le16((sizeof(MFT_RECORD_OLD
) + 1) & ~1);
346 /* Abort if mref is > 32 bits. */
347 if (MREF(mref
) & 0x0000ffff00000000ull
) {
349 ntfs_log_perror("Mft reference exceeds 32 bits");
352 mrec
->usa_ofs
= cpu_to_le16((sizeof(MFT_RECORD
) + 1) & ~1);
354 * Set the NTFS 3.1+ specific fields while we know that the
355 * volume version is 3.1+.
357 mrec
->reserved
= cpu_to_le16(0);
358 mrec
->mft_record_number
= cpu_to_le32(MREF(mref
));
360 mrec
->magic
= magic_FILE
;
361 if (vol
->mft_record_size
>= NTFS_BLOCK_SIZE
)
362 mrec
->usa_count
= cpu_to_le16(vol
->mft_record_size
/
363 NTFS_BLOCK_SIZE
+ 1);
365 mrec
->usa_count
= cpu_to_le16(1);
366 ntfs_log_error("Sector size is bigger than MFT record size. "
367 "Setting usa_count to 1. If Windows chkdsk "
368 "reports this as corruption, please email %s "
369 "stating that you saw this message and that "
370 "the file system created was corrupt. "
371 "Thank you.\n", NTFS_DEV_LIST
);
373 /* Set the update sequence number to 1. */
374 *(u16
*)((u8
*)mrec
+ le16_to_cpu(mrec
->usa_ofs
)) = cpu_to_le16(1);
375 mrec
->lsn
= cpu_to_le64(0ull);
376 mrec
->sequence_number
= cpu_to_le16(1);
377 mrec
->link_count
= cpu_to_le16(0);
378 /* Aligned to 8-byte boundary. */
379 mrec
->attrs_offset
= cpu_to_le16((le16_to_cpu(mrec
->usa_ofs
) +
380 (le16_to_cpu(mrec
->usa_count
) << 1) + 7) & ~7);
381 mrec
->flags
= cpu_to_le16(0);
383 * Using attrs_offset plus eight bytes (for the termination attribute),
384 * aligned to 8-byte boundary.
386 mrec
->bytes_in_use
= cpu_to_le32((le16_to_cpu(mrec
->attrs_offset
) + 8 +
388 mrec
->bytes_allocated
= cpu_to_le32(vol
->mft_record_size
);
389 mrec
->base_mft_record
= cpu_to_le64((MFT_REF
)0);
390 mrec
->next_attr_instance
= cpu_to_le16(0);
391 a
= (ATTR_RECORD
*)((u8
*)mrec
+ le16_to_cpu(mrec
->attrs_offset
));
393 a
->length
= cpu_to_le32(0);
394 /* Finally, clear the unused part of the mft record. */
395 memset((u8
*)a
+ 8, 0, vol
->mft_record_size
- ((u8
*)a
+ 8 - (u8
*)mrec
));
400 * ntfs_mft_record_format - format an mft record on an ntfs volume
401 * @vol: volume on which to format the mft record
402 * @mref: mft reference specifying mft record to format
404 * Format the mft record with the mft reference @mref in $MFT/$DATA, i.e. lay
405 * out an empty, unused mft record in memory and write it to the volume @vol.
407 * On success return 0 and on error return -1 with errno set to the error code.
409 int ntfs_mft_record_format(const ntfs_volume
*vol
, const MFT_REF mref
)
414 ntfs_log_enter("Entering\n");
416 m
= ntfs_calloc(vol
->mft_record_size
);
420 if (ntfs_mft_record_layout(vol
, mref
, m
))
423 if (ntfs_mft_record_write(vol
, mref
, m
))
430 ntfs_log_leave("\n");
434 static const char *es
= " Leaving inconsistent metadata. Run chkdsk.";
437 * ntfs_ffz - Find the first unset (zero) bit in a word
444 static inline unsigned int ntfs_ffz(unsigned int word
)
446 return ffs(~word
) - 1;
449 static int ntfs_is_mft(ntfs_inode
*ni
)
451 if (ni
&& ni
->mft_no
== FILE_MFT
)
457 #define PAGE_SIZE 4096
460 #define RESERVED_MFT_RECORDS 64
463 * ntfs_mft_bitmap_find_free_rec - find a free mft record in the mft bitmap
464 * @vol: volume on which to search for a free mft record
465 * @base_ni: open base inode if allocating an extent mft record or NULL
467 * Search for a free mft record in the mft bitmap attribute on the ntfs volume
470 * If @base_ni is NULL start the search at the default allocator position.
472 * If @base_ni is not NULL start the search at the mft record after the base
473 * mft record @base_ni.
475 * Return the free mft record on success and -1 on error with errno set to the
476 * error code. An error code of ENOSPC means that there are no free mft
477 * records in the currently initialized mft bitmap.
479 static int ntfs_mft_bitmap_find_free_rec(ntfs_volume
*vol
, ntfs_inode
*base_ni
)
481 s64 pass_end
, ll
, data_pos
, pass_start
, ofs
, bit
;
482 ntfs_attr
*mftbmp_na
;
488 ntfs_log_enter("Entering\n");
490 mftbmp_na
= vol
->mftbmp_na
;
492 * Set the end of the pass making sure we do not overflow the mft
496 pass_end
= vol
->mft_na
->allocated_size
>> vol
->mft_record_size_bits
;
497 ll
= mftbmp_na
->initialized_size
<< 3;
502 data_pos
= vol
->mft_data_pos
;
504 data_pos
= base_ni
->mft_no
+ 1;
505 if (data_pos
< RESERVED_MFT_RECORDS
)
506 data_pos
= RESERVED_MFT_RECORDS
;
507 if (data_pos
>= pass_end
) {
508 data_pos
= RESERVED_MFT_RECORDS
;
510 /* This happens on a freshly formatted volume. */
511 if (data_pos
>= pass_end
) {
516 if (ntfs_is_mft(base_ni
)) {
520 pass_start
= data_pos
;
521 buf
= ntfs_malloc(PAGE_SIZE
);
525 ntfs_log_debug("Starting bitmap search: pass %u, pass_start 0x%llx, "
526 "pass_end 0x%llx, data_pos 0x%llx.\n", pass
,
527 (long long)pass_start
, (long long)pass_end
,
528 (long long)data_pos
);
533 /* Loop until a free mft record is found. */
534 for (; pass
<= 2; size
= PAGE_SIZE
) {
535 /* Cap size to pass_end. */
537 ll
= ((pass_end
+ 7) >> 3) - ofs
;
540 ll
= ntfs_attr_pread(mftbmp_na
, ofs
, size
, buf
);
542 ntfs_log_perror("Failed to read $MFT bitmap");
546 ntfs_log_debug("Read 0x%llx bytes.\n", (long long)ll
);
547 /* If we read at least one byte, search @buf for a zero bit. */
552 ntfs_log_debug("Before inner for loop: size 0x%x, "
553 "data_pos 0x%llx, bit 0x%llx, "
554 "*byte 0x%hhx, b %u.\n", size
,
555 (long long)data_pos
, (long long)bit
,
556 byte
? *byte
: -1, b
);
557 for (; bit
< size
&& data_pos
+ bit
< pass_end
;
558 bit
&= ~7ull, bit
+= 8) {
560 * If we're extending $MFT and running out of the first
561 * mft record (base record) then give up searching since
562 * no guarantee that the found record will be accessible.
564 if (ntfs_is_mft(base_ni
) && bit
> 400)
567 byte
= buf
+ (bit
>> 3);
571 /* Note: ffz() result must be zero based. */
572 b
= ntfs_ffz((unsigned long)*byte
);
573 if (b
< 8 && b
>= (bit
& 7)) {
575 ret
= data_pos
+ (bit
& ~7ull) + b
;
579 ntfs_log_debug("After inner for loop: size 0x%x, "
580 "data_pos 0x%llx, bit 0x%llx, "
581 "*byte 0x%hhx, b %u.\n", size
,
582 (long long)data_pos
, (long long)bit
,
583 byte
? *byte
: -1, b
);
586 * If the end of the pass has not been reached yet,
587 * continue searching the mft bitmap for a zero bit.
589 if (data_pos
< pass_end
)
592 /* Do the next pass. */
596 * Starting the second pass, in which we scan the first
597 * part of the zone which we omitted earlier.
599 pass_end
= pass_start
;
600 data_pos
= pass_start
= RESERVED_MFT_RECORDS
;
601 ntfs_log_debug("pass %i, pass_start 0x%llx, pass_end "
602 "0x%llx.\n", pass
, (long long)pass_start
,
603 (long long)pass_end
);
604 if (data_pos
>= pass_end
)
608 /* No free mft records in currently initialized mft bitmap. */
613 ntfs_log_leave("\n");
617 static int ntfs_mft_attr_extend(ntfs_attr
*na
)
619 int ret
= STATUS_ERROR
;
620 ntfs_log_enter("Entering\n");
622 if (!NInoAttrList(na
->ni
)) {
623 if (ntfs_inode_add_attrlist(na
->ni
)) {
624 ntfs_log_perror("%s: Can not add attrlist #3", __FUNCTION__
);
627 /* We can't sync the $MFT inode since its runlist is bogus. */
628 ret
= STATUS_KEEP_SEARCHING
;
632 if (ntfs_attr_update_mapping_pairs(na
, 0)) {
633 ntfs_log_perror("%s: MP update failed", __FUNCTION__
);
639 ntfs_log_leave("\n");
644 * ntfs_mft_bitmap_extend_allocation_i - see ntfs_mft_bitmap_extend_allocation
646 static int ntfs_mft_bitmap_extend_allocation_i(ntfs_volume
*vol
)
649 s64 ll
= 0; /* silence compiler warning */
650 ntfs_attr
*mftbmp_na
;
651 runlist_element
*rl
, *rl2
= NULL
; /* silence compiler warning */
652 ntfs_attr_search_ctx
*ctx
;
653 MFT_RECORD
*m
= NULL
; /* silence compiler warning */
654 ATTR_RECORD
*a
= NULL
; /* silence compiler warning */
656 int ret
= STATUS_ERROR
;
657 u32 old_alen
= 0; /* silence compiler warning */
658 BOOL mp_rebuilt
= FALSE
;
659 BOOL update_mp
= FALSE
;
661 mftbmp_na
= vol
->mftbmp_na
;
663 * Determine the last lcn of the mft bitmap. The allocated size of the
664 * mft bitmap cannot be zero so we are ok to do this.
666 rl
= ntfs_attr_find_vcn(mftbmp_na
, (mftbmp_na
->allocated_size
- 1) >>
667 vol
->cluster_size_bits
);
668 if (!rl
|| !rl
->length
|| rl
->lcn
< 0) {
669 ntfs_log_error("Failed to determine last allocated "
670 "cluster of mft bitmap attribute.\n");
675 lcn
= rl
->lcn
+ rl
->length
;
677 rl2
= ntfs_cluster_alloc(vol
, rl
[1].vcn
, 1, lcn
, DATA_ZONE
);
679 ntfs_log_error("Failed to allocate a cluster for "
680 "the mft bitmap.\n");
683 rl
= ntfs_runlists_merge(mftbmp_na
->rl
, rl2
);
686 ntfs_log_error("Failed to merge runlists for mft "
688 if (ntfs_cluster_free_from_rl(vol
, rl2
))
689 ntfs_log_error("Failed to deallocate "
696 ntfs_log_debug("Adding one run to mft bitmap.\n");
697 /* Find the last run in the new runlist. */
698 for (; rl
[1].length
; rl
++)
701 * Update the attribute record as well. Note: @rl is the last
702 * (non-terminator) runlist element of mft bitmap.
704 ctx
= ntfs_attr_get_search_ctx(mftbmp_na
->ni
, NULL
);
708 if (ntfs_attr_lookup(mftbmp_na
->type
, mftbmp_na
->name
,
709 mftbmp_na
->name_len
, 0, rl
[1].vcn
, NULL
, 0, ctx
)) {
710 ntfs_log_error("Failed to find last attribute extent of "
711 "mft bitmap attribute.\n");
716 ll
= sle64_to_cpu(a
->lowest_vcn
);
717 rl2
= ntfs_attr_find_vcn(mftbmp_na
, ll
);
718 if (!rl2
|| !rl2
->length
) {
719 ntfs_log_error("Failed to determine previous last "
720 "allocated cluster of mft bitmap attribute.\n");
725 /* Get the size for the new mapping pairs array for this extent. */
726 mp_size
= ntfs_get_size_for_mapping_pairs(vol
, rl2
, ll
, INT_MAX
);
728 ntfs_log_error("Get size for mapping pairs failed for "
729 "mft bitmap attribute extent.\n");
732 /* Expand the attribute record if necessary. */
733 old_alen
= le32_to_cpu(a
->length
);
734 if (ntfs_attr_record_resize(m
, a
, mp_size
+
735 le16_to_cpu(a
->mapping_pairs_offset
))) {
736 ntfs_log_info("extending $MFT bitmap\n");
737 ret
= ntfs_mft_attr_extend(vol
->mftbmp_na
);
738 if (ret
== STATUS_OK
)
740 if (ret
== STATUS_ERROR
) {
741 ntfs_log_perror("%s: ntfs_mft_attr_extend failed", __FUNCTION__
);
747 /* Generate the mapping pairs array directly into the attr record. */
748 if (ntfs_mapping_pairs_build(vol
, (u8
*)a
+
749 le16_to_cpu(a
->mapping_pairs_offset
), mp_size
, rl2
, ll
,
751 ntfs_log_error("Failed to build mapping pairs array for "
752 "mft bitmap attribute.\n");
756 /* Update the highest_vcn. */
757 a
->highest_vcn
= cpu_to_sle64(rl
[1].vcn
- 1);
759 * We now have extended the mft bitmap allocated_size by one cluster.
760 * Reflect this in the ntfs_attr structure and the attribute record.
764 * We are not in the first attribute extent, switch to it, but
765 * first ensure the changes will make it to disk later.
767 ntfs_inode_mark_dirty(ctx
->ntfs_ino
);
768 ntfs_attr_reinit_search_ctx(ctx
);
769 if (ntfs_attr_lookup(mftbmp_na
->type
, mftbmp_na
->name
,
770 mftbmp_na
->name_len
, 0, 0, NULL
, 0, ctx
)) {
771 ntfs_log_error("Failed to find first attribute "
772 "extent of mft bitmap attribute.\n");
773 goto restore_undo_alloc
;
778 mftbmp_na
->allocated_size
+= vol
->cluster_size
;
779 a
->allocated_size
= cpu_to_sle64(mftbmp_na
->allocated_size
);
780 /* Ensure the changes make it to disk. */
781 ntfs_inode_mark_dirty(ctx
->ntfs_ino
);
782 ntfs_attr_put_search_ctx(ctx
);
787 ntfs_attr_reinit_search_ctx(ctx
);
788 if (ntfs_attr_lookup(mftbmp_na
->type
, mftbmp_na
->name
,
789 mftbmp_na
->name_len
, 0, rl
[1].vcn
, NULL
, 0, ctx
)) {
790 ntfs_log_error("Failed to find last attribute extent of "
791 "mft bitmap attribute.%s\n", es
);
792 ntfs_attr_put_search_ctx(ctx
);
793 mftbmp_na
->allocated_size
+= vol
->cluster_size
;
795 * The only thing that is now wrong is ->allocated_size of the
796 * base attribute extent which chkdsk should be able to fix.
803 a
->highest_vcn
= cpu_to_sle64(rl
[1].vcn
- 2);
808 /* Remove the last run from the runlist. */
813 /* FIXME: use an ntfs_cluster_free_* function */
814 if (ntfs_bitmap_clear_bit(vol
->lcnbmp_na
, lcn
))
815 ntfs_log_error("Failed to free cluster.%s\n", es
);
817 vol
->free_clusters
++;
819 if (ntfs_mapping_pairs_build(vol
, (u8
*)a
+
820 le16_to_cpu(a
->mapping_pairs_offset
),
821 old_alen
- le16_to_cpu(a
->mapping_pairs_offset
),
823 ntfs_log_error("Failed to restore mapping "
824 "pairs array.%s\n", es
);
825 if (ntfs_attr_record_resize(m
, a
, old_alen
))
826 ntfs_log_error("Failed to restore attribute "
828 ntfs_inode_mark_dirty(ctx
->ntfs_ino
);
831 if (ntfs_attr_update_mapping_pairs(vol
->mftbmp_na
, 0))
832 ntfs_log_perror("%s: MP update failed", __FUNCTION__
);
835 ntfs_attr_put_search_ctx(ctx
);
841 * ntfs_mft_bitmap_extend_allocation - extend mft bitmap attribute by a cluster
842 * @vol: volume on which to extend the mft bitmap attribute
844 * Extend the mft bitmap attribute on the ntfs volume @vol by one cluster.
846 * Note: Only changes allocated_size, i.e. does not touch initialized_size or
849 * Return 0 on success and -1 on error with errno set to the error code.
851 static int ntfs_mft_bitmap_extend_allocation(ntfs_volume
*vol
)
855 ntfs_log_enter("Entering\n");
856 ret
= ntfs_mft_bitmap_extend_allocation_i(vol
);
857 ntfs_log_leave("\n");
861 * ntfs_mft_bitmap_extend_initialized - extend mft bitmap initialized data
862 * @vol: volume on which to extend the mft bitmap attribute
864 * Extend the initialized portion of the mft bitmap attribute on the ntfs
865 * volume @vol by 8 bytes.
867 * Note: Only changes initialized_size and data_size, i.e. requires that
868 * allocated_size is big enough to fit the new initialized_size.
870 * Return 0 on success and -1 on error with errno set to the error code.
872 static int ntfs_mft_bitmap_extend_initialized(ntfs_volume
*vol
)
874 s64 old_data_size
, old_initialized_size
, ll
;
875 ntfs_attr
*mftbmp_na
;
876 ntfs_attr_search_ctx
*ctx
;
881 ntfs_log_enter("Entering\n");
883 mftbmp_na
= vol
->mftbmp_na
;
884 ctx
= ntfs_attr_get_search_ctx(mftbmp_na
->ni
, NULL
);
888 if (ntfs_attr_lookup(mftbmp_na
->type
, mftbmp_na
->name
,
889 mftbmp_na
->name_len
, 0, 0, NULL
, 0, ctx
)) {
890 ntfs_log_error("Failed to find first attribute extent of "
891 "mft bitmap attribute.\n");
896 old_data_size
= mftbmp_na
->data_size
;
897 old_initialized_size
= mftbmp_na
->initialized_size
;
898 mftbmp_na
->initialized_size
+= 8;
899 a
->initialized_size
= cpu_to_sle64(mftbmp_na
->initialized_size
);
900 if (mftbmp_na
->initialized_size
> mftbmp_na
->data_size
) {
901 mftbmp_na
->data_size
= mftbmp_na
->initialized_size
;
902 a
->data_size
= cpu_to_sle64(mftbmp_na
->data_size
);
904 /* Ensure the changes make it to disk. */
905 ntfs_inode_mark_dirty(ctx
->ntfs_ino
);
906 ntfs_attr_put_search_ctx(ctx
);
907 /* Initialize the mft bitmap attribute value with zeroes. */
909 ll
= ntfs_attr_pwrite(mftbmp_na
, old_initialized_size
, 8, &ll
);
911 ntfs_log_debug("Wrote eight initialized bytes to mft bitmap.\n");
912 vol
->free_mft_records
+= (8 * 8);
916 ntfs_log_error("Failed to write to mft bitmap.\n");
920 /* Try to recover from the error. */
921 ctx
= ntfs_attr_get_search_ctx(mftbmp_na
->ni
, NULL
);
925 if (ntfs_attr_lookup(mftbmp_na
->type
, mftbmp_na
->name
,
926 mftbmp_na
->name_len
, 0, 0, NULL
, 0, ctx
)) {
927 ntfs_log_error("Failed to find first attribute extent of "
928 "mft bitmap attribute.%s\n", es
);
930 ntfs_attr_put_search_ctx(ctx
);
934 mftbmp_na
->initialized_size
= old_initialized_size
;
935 a
->initialized_size
= cpu_to_sle64(old_initialized_size
);
936 if (mftbmp_na
->data_size
!= old_data_size
) {
937 mftbmp_na
->data_size
= old_data_size
;
938 a
->data_size
= cpu_to_sle64(old_data_size
);
940 ntfs_inode_mark_dirty(ctx
->ntfs_ino
);
941 ntfs_attr_put_search_ctx(ctx
);
942 ntfs_log_debug("Restored status of mftbmp: allocated_size 0x%llx, "
943 "data_size 0x%llx, initialized_size 0x%llx.\n",
944 (long long)mftbmp_na
->allocated_size
,
945 (long long)mftbmp_na
->data_size
,
946 (long long)mftbmp_na
->initialized_size
);
950 ntfs_log_leave("\n");
955 * ntfs_mft_data_extend_allocation - extend mft data attribute
956 * @vol: volume on which to extend the mft data attribute
958 * Extend the mft data attribute on the ntfs volume @vol by 16 mft records
959 * worth of clusters or if not enough space for this by one mft record worth
962 * Note: Only changes allocated_size, i.e. does not touch initialized_size or
965 * Return 0 on success and -1 on error with errno set to the error code.
967 static int ntfs_mft_data_extend_allocation(ntfs_volume
*vol
)
971 s64 min_nr
, nr
, ll
= 0; /* silence compiler warning */
973 runlist_element
*rl
, *rl2
;
974 ntfs_attr_search_ctx
*ctx
;
975 MFT_RECORD
*m
= NULL
; /* silence compiler warning */
976 ATTR_RECORD
*a
= NULL
; /* silence compiler warning */
978 int ret
= STATUS_ERROR
;
979 u32 old_alen
= 0; /* silence compiler warning */
980 BOOL mp_rebuilt
= FALSE
;
981 BOOL update_mp
= FALSE
;
983 ntfs_log_enter("Extending mft data allocation.\n");
985 mft_na
= vol
->mft_na
;
987 * Determine the preferred allocation location, i.e. the last lcn of
988 * the mft data attribute. The allocated size of the mft data
989 * attribute cannot be zero so we are ok to do this.
991 rl
= ntfs_attr_find_vcn(mft_na
,
992 (mft_na
->allocated_size
- 1) >> vol
->cluster_size_bits
);
994 if (!rl
|| !rl
->length
|| rl
->lcn
< 0) {
995 ntfs_log_error("Failed to determine last allocated "
996 "cluster of mft data attribute.\n");
1002 lcn
= rl
->lcn
+ rl
->length
;
1003 ntfs_log_debug("Last lcn of mft data attribute is 0x%llx.\n", (long long)lcn
);
1004 /* Minimum allocation is one mft record worth of clusters. */
1005 min_nr
= vol
->mft_record_size
>> vol
->cluster_size_bits
;
1008 /* Want to allocate 16 mft records worth of clusters. */
1009 nr
= vol
->mft_record_size
<< 4 >> vol
->cluster_size_bits
;
1013 old_last_vcn
= rl
[1].vcn
;
1015 rl2
= ntfs_cluster_alloc(vol
, old_last_vcn
, nr
, lcn
, MFT_ZONE
);
1018 if (errno
!= ENOSPC
|| nr
== min_nr
) {
1019 ntfs_log_perror("Failed to allocate (%lld) clusters "
1020 "for $MFT", (long long)nr
);
1024 * There is not enough space to do the allocation, but there
1025 * might be enough space to do a minimal allocation so try that
1029 ntfs_log_debug("Retrying mft data allocation with minimal cluster "
1030 "count %lli.\n", (long long)nr
);
1033 ntfs_log_debug("Allocated %lld clusters.\n", (long long)nr
);
1035 rl
= ntfs_runlists_merge(mft_na
->rl
, rl2
);
1038 ntfs_log_error("Failed to merge runlists for mft data "
1040 if (ntfs_cluster_free_from_rl(vol
, rl2
))
1041 ntfs_log_error("Failed to deallocate clusters "
1042 "from the mft data attribute.%s\n", es
);
1049 /* Find the last run in the new runlist. */
1050 for (; rl
[1].length
; rl
++)
1052 /* Update the attribute record as well. */
1053 ctx
= ntfs_attr_get_search_ctx(mft_na
->ni
, NULL
);
1057 if (ntfs_attr_lookup(mft_na
->type
, mft_na
->name
, mft_na
->name_len
, 0,
1058 rl
[1].vcn
, NULL
, 0, ctx
)) {
1059 ntfs_log_error("Failed to find last attribute extent of "
1060 "mft data attribute.\n");
1065 ll
= sle64_to_cpu(a
->lowest_vcn
);
1066 rl2
= ntfs_attr_find_vcn(mft_na
, ll
);
1067 if (!rl2
|| !rl2
->length
) {
1068 ntfs_log_error("Failed to determine previous last "
1069 "allocated cluster of mft data attribute.\n");
1074 /* Get the size for the new mapping pairs array for this extent. */
1075 mp_size
= ntfs_get_size_for_mapping_pairs(vol
, rl2
, ll
, INT_MAX
);
1077 ntfs_log_error("Get size for mapping pairs failed for "
1078 "mft data attribute extent.\n");
1081 /* Expand the attribute record if necessary. */
1082 old_alen
= le32_to_cpu(a
->length
);
1083 if (ntfs_attr_record_resize(m
, a
,
1084 mp_size
+ le16_to_cpu(a
->mapping_pairs_offset
))) {
1085 ret
= ntfs_mft_attr_extend(vol
->mft_na
);
1086 if (ret
== STATUS_OK
)
1088 if (ret
== STATUS_ERROR
) {
1089 ntfs_log_perror("%s: ntfs_mft_attr_extend failed", __FUNCTION__
);
1096 * Generate the mapping pairs array directly into the attribute record.
1098 if (ntfs_mapping_pairs_build(vol
,
1099 (u8
*)a
+ le16_to_cpu(a
->mapping_pairs_offset
), mp_size
,
1101 ntfs_log_error("Failed to build mapping pairs array of "
1102 "mft data attribute.\n");
1106 /* Update the highest_vcn. */
1107 a
->highest_vcn
= cpu_to_sle64(rl
[1].vcn
- 1);
1109 * We now have extended the mft data allocated_size by nr clusters.
1110 * Reflect this in the ntfs_attr structure and the attribute record.
1111 * @rl is the last (non-terminator) runlist element of mft data
1114 if (a
->lowest_vcn
) {
1116 * We are not in the first attribute extent, switch to it, but
1117 * first ensure the changes will make it to disk later.
1119 ntfs_inode_mark_dirty(ctx
->ntfs_ino
);
1120 ntfs_attr_reinit_search_ctx(ctx
);
1121 if (ntfs_attr_lookup(mft_na
->type
, mft_na
->name
,
1122 mft_na
->name_len
, 0, 0, NULL
, 0, ctx
)) {
1123 ntfs_log_error("Failed to find first attribute "
1124 "extent of mft data attribute.\n");
1125 goto restore_undo_alloc
;
1130 mft_na
->allocated_size
+= nr
<< vol
->cluster_size_bits
;
1131 a
->allocated_size
= cpu_to_sle64(mft_na
->allocated_size
);
1132 /* Ensure the changes make it to disk. */
1133 ntfs_inode_mark_dirty(ctx
->ntfs_ino
);
1134 ntfs_attr_put_search_ctx(ctx
);
1137 ntfs_log_leave("\n");
1142 ntfs_attr_reinit_search_ctx(ctx
);
1143 if (ntfs_attr_lookup(mft_na
->type
, mft_na
->name
, mft_na
->name_len
, 0,
1144 rl
[1].vcn
, NULL
, 0, ctx
)) {
1145 ntfs_log_error("Failed to find last attribute extent of "
1146 "mft data attribute.%s\n", es
);
1147 ntfs_attr_put_search_ctx(ctx
);
1148 mft_na
->allocated_size
+= nr
<< vol
->cluster_size_bits
;
1150 * The only thing that is now wrong is ->allocated_size of the
1151 * base attribute extent which chkdsk should be able to fix.
1159 a
->highest_vcn
= cpu_to_sle64(old_last_vcn
- 1);
1163 if (ntfs_cluster_free(vol
, mft_na
, old_last_vcn
, -1) < 0)
1164 ntfs_log_error("Failed to free clusters from mft data "
1165 "attribute.%s\n", es
);
1166 if (ntfs_rl_truncate(&mft_na
->rl
, old_last_vcn
))
1167 ntfs_log_error("Failed to truncate mft data attribute "
1168 "runlist.%s\n", es
);
1170 if (ntfs_mapping_pairs_build(vol
, (u8
*)a
+
1171 le16_to_cpu(a
->mapping_pairs_offset
),
1172 old_alen
- le16_to_cpu(a
->mapping_pairs_offset
),
1174 ntfs_log_error("Failed to restore mapping pairs "
1176 if (ntfs_attr_record_resize(m
, a
, old_alen
))
1177 ntfs_log_error("Failed to restore attribute "
1179 ntfs_inode_mark_dirty(ctx
->ntfs_ino
);
1182 if (ntfs_attr_update_mapping_pairs(vol
->mft_na
, 0))
1183 ntfs_log_perror("%s: MP update failed", __FUNCTION__
);
1186 ntfs_attr_put_search_ctx(ctx
);
1192 static int ntfs_mft_record_init(ntfs_volume
*vol
, s64 size
)
1196 s64 old_data_initialized
, old_data_size
;
1197 ntfs_attr_search_ctx
*ctx
;
1199 ntfs_log_enter("Entering\n");
1201 /* NOTE: Caller must sanity check vol, vol->mft_na and vol->mftbmp_na */
1203 mft_na
= vol
->mft_na
;
1206 * The mft record is outside the initialized data. Extend the mft data
1207 * attribute until it covers the allocated record. The loop is only
1208 * actually traversed more than once when a freshly formatted volume
1209 * is first written to so it optimizes away nicely in the common case.
1211 ntfs_log_debug("Status of mft data before extension: "
1212 "allocated_size 0x%llx, data_size 0x%llx, "
1213 "initialized_size 0x%llx.\n",
1214 (long long)mft_na
->allocated_size
,
1215 (long long)mft_na
->data_size
,
1216 (long long)mft_na
->initialized_size
);
1217 while (size
> mft_na
->allocated_size
) {
1218 if (ntfs_mft_data_extend_allocation(vol
) == STATUS_ERROR
)
1220 ntfs_log_debug("Status of mft data after allocation extension: "
1221 "allocated_size 0x%llx, data_size 0x%llx, "
1222 "initialized_size 0x%llx.\n",
1223 (long long)mft_na
->allocated_size
,
1224 (long long)mft_na
->data_size
,
1225 (long long)mft_na
->initialized_size
);
1228 old_data_initialized
= mft_na
->initialized_size
;
1229 old_data_size
= mft_na
->data_size
;
1232 * Extend mft data initialized size (and data size of course) to reach
1233 * the allocated mft record, formatting the mft records along the way.
1234 * Note: We only modify the ntfs_attr structure as that is all that is
1235 * needed by ntfs_mft_record_format(). We will update the attribute
1236 * record itself in one fell swoop later on.
1238 while (size
> mft_na
->initialized_size
) {
1239 s64 ll2
= mft_na
->initialized_size
>> vol
->mft_record_size_bits
;
1240 mft_na
->initialized_size
+= vol
->mft_record_size
;
1241 if (mft_na
->initialized_size
> mft_na
->data_size
)
1242 mft_na
->data_size
= mft_na
->initialized_size
;
1243 ntfs_log_debug("Initializing mft record 0x%llx.\n", (long long)ll2
);
1244 if (ntfs_mft_record_format(vol
, ll2
) < 0) {
1245 ntfs_log_perror("Failed to format mft record");
1246 goto undo_data_init
;
1250 /* Update the mft data attribute record to reflect the new sizes. */
1251 ctx
= ntfs_attr_get_search_ctx(mft_na
->ni
, NULL
);
1253 goto undo_data_init
;
1255 if (ntfs_attr_lookup(mft_na
->type
, mft_na
->name
, mft_na
->name_len
, 0,
1257 ntfs_log_error("Failed to find first attribute extent of "
1258 "mft data attribute.\n");
1259 ntfs_attr_put_search_ctx(ctx
);
1260 goto undo_data_init
;
1262 ctx
->attr
->initialized_size
= cpu_to_sle64(mft_na
->initialized_size
);
1263 ctx
->attr
->data_size
= cpu_to_sle64(mft_na
->data_size
);
1264 ctx
->attr
->allocated_size
= cpu_to_sle64(mft_na
->allocated_size
);
1266 /* Ensure the changes make it to disk. */
1267 ntfs_inode_mark_dirty(ctx
->ntfs_ino
);
1268 ntfs_attr_put_search_ctx(ctx
);
1269 ntfs_log_debug("Status of mft data after mft record initialization: "
1270 "allocated_size 0x%llx, data_size 0x%llx, "
1271 "initialized_size 0x%llx.\n",
1272 (long long)mft_na
->allocated_size
,
1273 (long long)mft_na
->data_size
,
1274 (long long)mft_na
->initialized_size
);
1276 /* Sanity checks. */
1277 if (mft_na
->data_size
> mft_na
->allocated_size
||
1278 mft_na
->initialized_size
> mft_na
->data_size
)
1279 NTFS_BUG("mft_na sanity checks failed");
1281 /* Sync MFT to minimize data loss if there won't be clean unmount. */
1282 if (ntfs_inode_sync(mft_na
->ni
))
1283 goto undo_data_init
;
1287 ntfs_log_leave("\n");
1291 mft_na
->initialized_size
= old_data_initialized
;
1292 mft_na
->data_size
= old_data_size
;
1296 static int ntfs_mft_rec_init(ntfs_volume
*vol
, s64 size
)
1300 s64 old_data_initialized
, old_data_size
;
1301 ntfs_attr_search_ctx
*ctx
;
1303 ntfs_log_enter("Entering\n");
1305 mft_na
= vol
->mft_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
);