2 * mft.c - NTFS kernel mft record operations. Part of the Linux-NTFS project.
4 * Copyright (c) 2001-2006 Anton Altaparmakov
5 * Copyright (c) 2002 Richard Russon
7 * This program/include file is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program/include file is distributed in the hope that it will be
13 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program (in the main directory of the Linux-NTFS
19 * distribution in the file COPYING); if not, write to the Free Software
20 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/buffer_head.h>
24 #include <linux/swap.h>
37 * map_mft_record_page - map the page in which a specific mft record resides
38 * @ni: ntfs inode whose mft record page to map
40 * This maps the page in which the mft record of the ntfs inode @ni is situated
41 * and returns a pointer to the mft record within the mapped page.
43 * Return value needs to be checked with IS_ERR() and if that is true PTR_ERR()
44 * contains the negative error code returned.
46 static inline MFT_RECORD
*map_mft_record_page(ntfs_inode
*ni
)
49 ntfs_volume
*vol
= ni
->vol
;
50 struct inode
*mft_vi
= vol
->mft_ino
;
52 unsigned long index
, end_index
;
57 * The index into the page cache and the offset within the page cache
58 * page of the wanted mft record. FIXME: We need to check for
59 * overflowing the unsigned long, but I don't think we would ever get
60 * here if the volume was that big...
62 index
= (u64
)ni
->mft_no
<< vol
->mft_record_size_bits
>>
64 ofs
= (ni
->mft_no
<< vol
->mft_record_size_bits
) & ~PAGE_CACHE_MASK
;
66 i_size
= i_size_read(mft_vi
);
67 /* The maximum valid index into the page cache for $MFT's data. */
68 end_index
= i_size
>> PAGE_CACHE_SHIFT
;
70 /* If the wanted index is out of bounds the mft record doesn't exist. */
71 if (unlikely(index
>= end_index
)) {
72 if (index
> end_index
|| (i_size
& ~PAGE_CACHE_MASK
) < ofs
+
73 vol
->mft_record_size
) {
74 page
= ERR_PTR(-ENOENT
);
75 ntfs_error(vol
->sb
, "Attemt to read mft record 0x%lx, "
76 "which is beyond the end of the mft. "
77 "This is probably a bug in the ntfs "
78 "driver.", ni
->mft_no
);
82 /* Read, map, and pin the page. */
83 page
= ntfs_map_page(mft_vi
->i_mapping
, index
);
84 if (likely(!IS_ERR(page
))) {
85 /* Catch multi sector transfer fixup errors. */
86 if (likely(ntfs_is_mft_recordp((le32
*)(page_address(page
) +
90 return page_address(page
) + ofs
;
92 ntfs_error(vol
->sb
, "Mft record 0x%lx is corrupt. "
93 "Run chkdsk.", ni
->mft_no
);
94 ntfs_unmap_page(page
);
105 * map_mft_record - map, pin and lock an mft record
106 * @ni: ntfs inode whose MFT record to map
108 * First, take the mrec_lock mutex. We might now be sleeping, while waiting
109 * for the mutex if it was already locked by someone else.
111 * The page of the record is mapped using map_mft_record_page() before being
112 * returned to the caller.
114 * This in turn uses ntfs_map_page() to get the page containing the wanted mft
115 * record (it in turn calls read_cache_page() which reads it in from disk if
116 * necessary, increments the use count on the page so that it cannot disappear
117 * under us and returns a reference to the page cache page).
119 * If read_cache_page() invokes ntfs_readpage() to load the page from disk, it
120 * sets PG_locked and clears PG_uptodate on the page. Once I/O has completed
121 * and the post-read mst fixups on each mft record in the page have been
122 * performed, the page gets PG_uptodate set and PG_locked cleared (this is done
123 * in our asynchronous I/O completion handler end_buffer_read_mft_async()).
124 * ntfs_map_page() waits for PG_locked to become clear and checks if
125 * PG_uptodate is set and returns an error code if not. This provides
126 * sufficient protection against races when reading/using the page.
128 * However there is the write mapping to think about. Doing the above described
129 * checking here will be fine, because when initiating the write we will set
130 * PG_locked and clear PG_uptodate making sure nobody is touching the page
131 * contents. Doing the locking this way means that the commit to disk code in
132 * the page cache code paths is automatically sufficiently locked with us as
133 * we will not touch a page that has been locked or is not uptodate. The only
134 * locking problem then is them locking the page while we are accessing it.
136 * So that code will end up having to own the mrec_lock of all mft
137 * records/inodes present in the page before I/O can proceed. In that case we
138 * wouldn't need to bother with PG_locked and PG_uptodate as nobody will be
139 * accessing anything without owning the mrec_lock mutex. But we do need to
140 * use them because of the read_cache_page() invocation and the code becomes so
141 * much simpler this way that it is well worth it.
143 * The mft record is now ours and we return a pointer to it. You need to check
144 * the returned pointer with IS_ERR() and if that is true, PTR_ERR() will return
147 * NOTE: Caller is responsible for setting the mft record dirty before calling
148 * unmap_mft_record(). This is obviously only necessary if the caller really
149 * modified the mft record...
150 * Q: Do we want to recycle one of the VFS inode state bits instead?
151 * A: No, the inode ones mean we want to change the mft record, not we want to
154 MFT_RECORD
*map_mft_record(ntfs_inode
*ni
)
158 ntfs_debug("Entering for mft_no 0x%lx.", ni
->mft_no
);
160 /* Make sure the ntfs inode doesn't go away. */
161 atomic_inc(&ni
->count
);
163 /* Serialize access to this mft record. */
164 mutex_lock(&ni
->mrec_lock
);
166 m
= map_mft_record_page(ni
);
167 if (likely(!IS_ERR(m
)))
170 mutex_unlock(&ni
->mrec_lock
);
171 atomic_dec(&ni
->count
);
172 ntfs_error(ni
->vol
->sb
, "Failed with error code %lu.", -PTR_ERR(m
));
177 * unmap_mft_record_page - unmap the page in which a specific mft record resides
178 * @ni: ntfs inode whose mft record page to unmap
180 * This unmaps the page in which the mft record of the ntfs inode @ni is
181 * situated and returns. This is a NOOP if highmem is not configured.
183 * The unmap happens via ntfs_unmap_page() which in turn decrements the use
184 * count on the page thus releasing it from the pinned state.
186 * We do not actually unmap the page from memory of course, as that will be
187 * done by the page cache code itself when memory pressure increases or
190 static inline void unmap_mft_record_page(ntfs_inode
*ni
)
194 // TODO: If dirty, blah...
195 ntfs_unmap_page(ni
->page
);
202 * unmap_mft_record - release a mapped mft record
203 * @ni: ntfs inode whose MFT record to unmap
205 * We release the page mapping and the mrec_lock mutex which unmaps the mft
206 * record and releases it for others to get hold of. We also release the ntfs
207 * inode by decrementing the ntfs inode reference count.
209 * NOTE: If caller has modified the mft record, it is imperative to set the mft
210 * record dirty BEFORE calling unmap_mft_record().
212 void unmap_mft_record(ntfs_inode
*ni
)
214 struct page
*page
= ni
->page
;
218 ntfs_debug("Entering for mft_no 0x%lx.", ni
->mft_no
);
220 unmap_mft_record_page(ni
);
221 mutex_unlock(&ni
->mrec_lock
);
222 atomic_dec(&ni
->count
);
224 * If pure ntfs_inode, i.e. no vfs inode attached, we leave it to
225 * ntfs_clear_extent_inode() in the extent inode case, and to the
226 * caller in the non-extent, yet pure ntfs inode case, to do the actual
227 * tear down of all structures and freeing of all allocated memory.
233 * map_extent_mft_record - load an extent inode and attach it to its base
234 * @base_ni: base ntfs inode
235 * @mref: mft reference of the extent inode to load
236 * @ntfs_ino: on successful return, pointer to the ntfs_inode structure
238 * Load the extent mft record @mref and attach it to its base inode @base_ni.
239 * Return the mapped extent mft record if IS_ERR(result) is false. Otherwise
240 * PTR_ERR(result) gives the negative error code.
242 * On successful return, @ntfs_ino contains a pointer to the ntfs_inode
243 * structure of the mapped extent inode.
245 MFT_RECORD
*map_extent_mft_record(ntfs_inode
*base_ni
, MFT_REF mref
,
246 ntfs_inode
**ntfs_ino
)
249 ntfs_inode
*ni
= NULL
;
250 ntfs_inode
**extent_nis
= NULL
;
252 unsigned long mft_no
= MREF(mref
);
253 u16 seq_no
= MSEQNO(mref
);
254 bool destroy_ni
= false;
256 ntfs_debug("Mapping extent mft record 0x%lx (base mft record 0x%lx).",
257 mft_no
, base_ni
->mft_no
);
258 /* Make sure the base ntfs inode doesn't go away. */
259 atomic_inc(&base_ni
->count
);
261 * Check if this extent inode has already been added to the base inode,
262 * in which case just return it. If not found, add it to the base
263 * inode before returning it.
265 mutex_lock(&base_ni
->extent_lock
);
266 if (base_ni
->nr_extents
> 0) {
267 extent_nis
= base_ni
->ext
.extent_ntfs_inos
;
268 for (i
= 0; i
< base_ni
->nr_extents
; i
++) {
269 if (mft_no
!= extent_nis
[i
]->mft_no
)
272 /* Make sure the ntfs inode doesn't go away. */
273 atomic_inc(&ni
->count
);
277 if (likely(ni
!= NULL
)) {
278 mutex_unlock(&base_ni
->extent_lock
);
279 atomic_dec(&base_ni
->count
);
280 /* We found the record; just have to map and return it. */
281 m
= map_mft_record(ni
);
282 /* map_mft_record() has incremented this on success. */
283 atomic_dec(&ni
->count
);
284 if (likely(!IS_ERR(m
))) {
285 /* Verify the sequence number. */
286 if (likely(le16_to_cpu(m
->sequence_number
) == seq_no
)) {
287 ntfs_debug("Done 1.");
291 unmap_mft_record(ni
);
292 ntfs_error(base_ni
->vol
->sb
, "Found stale extent mft "
293 "reference! Corrupt filesystem. "
295 return ERR_PTR(-EIO
);
298 ntfs_error(base_ni
->vol
->sb
, "Failed to map extent "
299 "mft record, error code %ld.", -PTR_ERR(m
));
302 /* Record wasn't there. Get a new ntfs inode and initialize it. */
303 ni
= ntfs_new_extent_inode(base_ni
->vol
->sb
, mft_no
);
305 mutex_unlock(&base_ni
->extent_lock
);
306 atomic_dec(&base_ni
->count
);
307 return ERR_PTR(-ENOMEM
);
309 ni
->vol
= base_ni
->vol
;
312 ni
->ext
.base_ntfs_ino
= base_ni
;
313 /* Now map the record. */
314 m
= map_mft_record(ni
);
316 mutex_unlock(&base_ni
->extent_lock
);
317 atomic_dec(&base_ni
->count
);
318 ntfs_clear_extent_inode(ni
);
321 /* Verify the sequence number if it is present. */
322 if (seq_no
&& (le16_to_cpu(m
->sequence_number
) != seq_no
)) {
323 ntfs_error(base_ni
->vol
->sb
, "Found stale extent mft "
324 "reference! Corrupt filesystem. Run chkdsk.");
329 /* Attach extent inode to base inode, reallocating memory if needed. */
330 if (!(base_ni
->nr_extents
& 3)) {
332 int new_size
= (base_ni
->nr_extents
+ 4) * sizeof(ntfs_inode
*);
334 tmp
= kmalloc(new_size
, GFP_NOFS
);
335 if (unlikely(!tmp
)) {
336 ntfs_error(base_ni
->vol
->sb
, "Failed to allocate "
339 m
= ERR_PTR(-ENOMEM
);
342 if (base_ni
->nr_extents
) {
343 BUG_ON(!base_ni
->ext
.extent_ntfs_inos
);
344 memcpy(tmp
, base_ni
->ext
.extent_ntfs_inos
, new_size
-
345 4 * sizeof(ntfs_inode
*));
346 kfree(base_ni
->ext
.extent_ntfs_inos
);
348 base_ni
->ext
.extent_ntfs_inos
= tmp
;
350 base_ni
->ext
.extent_ntfs_inos
[base_ni
->nr_extents
++] = ni
;
351 mutex_unlock(&base_ni
->extent_lock
);
352 atomic_dec(&base_ni
->count
);
353 ntfs_debug("Done 2.");
357 unmap_mft_record(ni
);
358 mutex_unlock(&base_ni
->extent_lock
);
359 atomic_dec(&base_ni
->count
);
361 * If the extent inode was not attached to the base inode we need to
362 * release it or we will leak memory.
365 ntfs_clear_extent_inode(ni
);
372 * __mark_mft_record_dirty - set the mft record and the page containing it dirty
373 * @ni: ntfs inode describing the mapped mft record
375 * Internal function. Users should call mark_mft_record_dirty() instead.
377 * Set the mapped (extent) mft record of the (base or extent) ntfs inode @ni,
378 * as well as the page containing the mft record, dirty. Also, mark the base
379 * vfs inode dirty. This ensures that any changes to the mft record are
380 * written out to disk.
382 * NOTE: We only set I_DIRTY_SYNC and I_DIRTY_DATASYNC (and not I_DIRTY_PAGES)
383 * on the base vfs inode, because even though file data may have been modified,
384 * it is dirty in the inode meta data rather than the data page cache of the
385 * inode, and thus there are no data pages that need writing out. Therefore, a
386 * full mark_inode_dirty() is overkill. A mark_inode_dirty_sync(), on the
387 * other hand, is not sufficient, because I_DIRTY_DATASYNC needs to be set to
388 * ensure ->write_inode is called from generic_osync_inode() and this needs to
389 * happen or the file data would not necessarily hit the device synchronously,
390 * even though the vfs inode has the O_SYNC flag set. Also, I_DIRTY_DATASYNC
391 * simply "feels" better than just I_DIRTY_SYNC, since the file data has not
392 * actually hit the block device yet, which is not what I_DIRTY_SYNC on its own
395 void __mark_mft_record_dirty(ntfs_inode
*ni
)
399 ntfs_debug("Entering for inode 0x%lx.", ni
->mft_no
);
400 BUG_ON(NInoAttr(ni
));
401 mark_ntfs_record_dirty(ni
->page
, ni
->page_ofs
);
402 /* Determine the base vfs inode and mark it dirty, too. */
403 mutex_lock(&ni
->extent_lock
);
404 if (likely(ni
->nr_extents
>= 0))
407 base_ni
= ni
->ext
.base_ntfs_ino
;
408 mutex_unlock(&ni
->extent_lock
);
409 __mark_inode_dirty(VFS_I(base_ni
), I_DIRTY_SYNC
| I_DIRTY_DATASYNC
);
412 static const char *ntfs_please_email
= "Please email "
413 "linux-ntfs-dev@lists.sourceforge.net and say that you saw "
414 "this message. Thank you.";
417 * ntfs_sync_mft_mirror_umount - synchronise an mft record to the mft mirror
418 * @vol: ntfs volume on which the mft record to synchronize resides
419 * @mft_no: mft record number of mft record to synchronize
420 * @m: mapped, mst protected (extent) mft record to synchronize
422 * Write the mapped, mst protected (extent) mft record @m with mft record
423 * number @mft_no to the mft mirror ($MFTMirr) of the ntfs volume @vol,
424 * bypassing the page cache and the $MFTMirr inode itself.
426 * This function is only for use at umount time when the mft mirror inode has
427 * already been disposed off. We BUG() if we are called while the mft mirror
428 * inode is still attached to the volume.
430 * On success return 0. On error return -errno.
432 * NOTE: This function is not implemented yet as I am not convinced it can
433 * actually be triggered considering the sequence of commits we do in super.c::
434 * ntfs_put_super(). But just in case we provide this place holder as the
435 * alternative would be either to BUG() or to get a NULL pointer dereference
438 static int ntfs_sync_mft_mirror_umount(ntfs_volume
*vol
,
439 const unsigned long mft_no
, MFT_RECORD
*m
)
441 BUG_ON(vol
->mftmirr_ino
);
442 ntfs_error(vol
->sb
, "Umount time mft mirror syncing is not "
443 "implemented yet. %s", ntfs_please_email
);
448 * ntfs_sync_mft_mirror - synchronize an mft record to the mft mirror
449 * @vol: ntfs volume on which the mft record to synchronize resides
450 * @mft_no: mft record number of mft record to synchronize
451 * @m: mapped, mst protected (extent) mft record to synchronize
452 * @sync: if true, wait for i/o completion
454 * Write the mapped, mst protected (extent) mft record @m with mft record
455 * number @mft_no to the mft mirror ($MFTMirr) of the ntfs volume @vol.
457 * On success return 0. On error return -errno and set the volume errors flag
458 * in the ntfs volume @vol.
460 * NOTE: We always perform synchronous i/o and ignore the @sync parameter.
462 * TODO: If @sync is false, want to do truly asynchronous i/o, i.e. just
463 * schedule i/o via ->writepage or do it via kntfsd or whatever.
465 int ntfs_sync_mft_mirror(ntfs_volume
*vol
, const unsigned long mft_no
,
466 MFT_RECORD
*m
, int sync
)
469 unsigned int blocksize
= vol
->sb
->s_blocksize
;
470 int max_bhs
= vol
->mft_record_size
/ blocksize
;
471 struct buffer_head
*bhs
[max_bhs
];
472 struct buffer_head
*bh
, *head
;
475 unsigned int block_start
, block_end
, m_start
, m_end
, page_ofs
;
476 int i_bhs
, nr_bhs
, err
= 0;
477 unsigned char blocksize_bits
= vol
->sb
->s_blocksize_bits
;
479 ntfs_debug("Entering for inode 0x%lx.", mft_no
);
481 if (unlikely(!vol
->mftmirr_ino
)) {
482 /* This could happen during umount... */
483 err
= ntfs_sync_mft_mirror_umount(vol
, mft_no
, m
);
488 /* Get the page containing the mirror copy of the mft record @m. */
489 page
= ntfs_map_page(vol
->mftmirr_ino
->i_mapping
, mft_no
>>
490 (PAGE_CACHE_SHIFT
- vol
->mft_record_size_bits
));
492 ntfs_error(vol
->sb
, "Failed to map mft mirror page.");
497 BUG_ON(!PageUptodate(page
));
498 ClearPageUptodate(page
);
499 /* Offset of the mft mirror record inside the page. */
500 page_ofs
= (mft_no
<< vol
->mft_record_size_bits
) & ~PAGE_CACHE_MASK
;
501 /* The address in the page of the mirror copy of the mft record @m. */
502 kmirr
= page_address(page
) + page_ofs
;
503 /* Copy the mst protected mft record to the mirror. */
504 memcpy(kmirr
, m
, vol
->mft_record_size
);
505 /* Create uptodate buffers if not present. */
506 if (unlikely(!page_has_buffers(page
))) {
507 struct buffer_head
*tail
;
509 bh
= head
= alloc_page_buffers(page
, blocksize
, 1);
511 set_buffer_uptodate(bh
);
513 bh
= bh
->b_this_page
;
515 tail
->b_this_page
= head
;
516 attach_page_buffers(page
, head
);
518 bh
= head
= page_buffers(page
);
523 m_start
= kmirr
- (u8
*)page_address(page
);
524 m_end
= m_start
+ vol
->mft_record_size
;
526 block_end
= block_start
+ blocksize
;
527 /* If the buffer is outside the mft record, skip it. */
528 if (block_end
<= m_start
)
530 if (unlikely(block_start
>= m_end
))
532 /* Need to map the buffer if it is not mapped already. */
533 if (unlikely(!buffer_mapped(bh
))) {
536 unsigned int vcn_ofs
;
538 bh
->b_bdev
= vol
->sb
->s_bdev
;
539 /* Obtain the vcn and offset of the current block. */
540 vcn
= ((VCN
)mft_no
<< vol
->mft_record_size_bits
) +
541 (block_start
- m_start
);
542 vcn_ofs
= vcn
& vol
->cluster_size_mask
;
543 vcn
>>= vol
->cluster_size_bits
;
545 down_read(&NTFS_I(vol
->mftmirr_ino
)->
547 rl
= NTFS_I(vol
->mftmirr_ino
)->runlist
.rl
;
549 * $MFTMirr always has the whole of its runlist
554 /* Seek to element containing target vcn. */
555 while (rl
->length
&& rl
[1].vcn
<= vcn
)
557 lcn
= ntfs_rl_vcn_to_lcn(rl
, vcn
);
558 /* For $MFTMirr, only lcn >= 0 is a successful remap. */
559 if (likely(lcn
>= 0)) {
560 /* Setup buffer head to correct block. */
561 bh
->b_blocknr
= ((lcn
<<
562 vol
->cluster_size_bits
) +
563 vcn_ofs
) >> blocksize_bits
;
564 set_buffer_mapped(bh
);
567 ntfs_error(vol
->sb
, "Cannot write mft mirror "
568 "record 0x%lx because its "
569 "location on disk could not "
570 "be determined (error code "
576 BUG_ON(!buffer_uptodate(bh
));
577 BUG_ON(!nr_bhs
&& (m_start
!= block_start
));
578 BUG_ON(nr_bhs
>= max_bhs
);
580 BUG_ON((nr_bhs
>= max_bhs
) && (m_end
!= block_end
));
581 } while (block_start
= block_end
, (bh
= bh
->b_this_page
) != head
);
583 up_read(&NTFS_I(vol
->mftmirr_ino
)->runlist
.lock
);
585 /* Lock buffers and start synchronous write i/o on them. */
586 for (i_bhs
= 0; i_bhs
< nr_bhs
; i_bhs
++) {
587 struct buffer_head
*tbh
= bhs
[i_bhs
];
589 if (unlikely(test_set_buffer_locked(tbh
)))
591 BUG_ON(!buffer_uptodate(tbh
));
592 clear_buffer_dirty(tbh
);
594 tbh
->b_end_io
= end_buffer_write_sync
;
595 submit_bh(WRITE
, tbh
);
597 /* Wait on i/o completion of buffers. */
598 for (i_bhs
= 0; i_bhs
< nr_bhs
; i_bhs
++) {
599 struct buffer_head
*tbh
= bhs
[i_bhs
];
602 if (unlikely(!buffer_uptodate(tbh
))) {
605 * Set the buffer uptodate so the page and
606 * buffer states do not become out of sync.
608 set_buffer_uptodate(tbh
);
611 } else /* if (unlikely(err)) */ {
612 /* Clean the buffers. */
613 for (i_bhs
= 0; i_bhs
< nr_bhs
; i_bhs
++)
614 clear_buffer_dirty(bhs
[i_bhs
]);
616 /* Current state: all buffers are clean, unlocked, and uptodate. */
617 /* Remove the mst protection fixups again. */
618 post_write_mst_fixup((NTFS_RECORD
*)kmirr
);
619 flush_dcache_page(page
);
620 SetPageUptodate(page
);
622 ntfs_unmap_page(page
);
626 ntfs_error(vol
->sb
, "I/O error while writing mft mirror "
627 "record 0x%lx!", mft_no
);
629 ntfs_error(vol
->sb
, "Failed to synchronize $MFTMirr (error "
630 "code %i). Volume will be left marked dirty "
631 "on umount. Run ntfsfix on the partition "
632 "after umounting to correct this.", -err
);
639 * write_mft_record_nolock - write out a mapped (extent) mft record
640 * @ni: ntfs inode describing the mapped (extent) mft record
641 * @m: mapped (extent) mft record to write
642 * @sync: if true, wait for i/o completion
644 * Write the mapped (extent) mft record @m described by the (regular or extent)
645 * ntfs inode @ni to backing store. If the mft record @m has a counterpart in
646 * the mft mirror, that is also updated.
648 * We only write the mft record if the ntfs inode @ni is dirty and the first
649 * buffer belonging to its mft record is dirty, too. We ignore the dirty state
650 * of subsequent buffers because we could have raced with
651 * fs/ntfs/aops.c::mark_ntfs_record_dirty().
653 * On success, clean the mft record and return 0. On error, leave the mft
654 * record dirty and return -errno.
656 * NOTE: We always perform synchronous i/o and ignore the @sync parameter.
657 * However, if the mft record has a counterpart in the mft mirror and @sync is
658 * true, we write the mft record, wait for i/o completion, and only then write
659 * the mft mirror copy. This ensures that if the system crashes either the mft
660 * or the mft mirror will contain a self-consistent mft record @m. If @sync is
661 * false on the other hand, we start i/o on both and then wait for completion
662 * on them. This provides a speedup but no longer guarantees that you will end
663 * up with a self-consistent mft record in the case of a crash but if you asked
664 * for asynchronous writing you probably do not care about that anyway.
666 * TODO: If @sync is false, want to do truly asynchronous i/o, i.e. just
667 * schedule i/o via ->writepage or do it via kntfsd or whatever.
669 int write_mft_record_nolock(ntfs_inode
*ni
, MFT_RECORD
*m
, int sync
)
671 ntfs_volume
*vol
= ni
->vol
;
672 struct page
*page
= ni
->page
;
673 unsigned int blocksize
= vol
->sb
->s_blocksize
;
674 unsigned char blocksize_bits
= vol
->sb
->s_blocksize_bits
;
675 int max_bhs
= vol
->mft_record_size
/ blocksize
;
676 struct buffer_head
*bhs
[max_bhs
];
677 struct buffer_head
*bh
, *head
;
679 unsigned int block_start
, block_end
, m_start
, m_end
;
680 int i_bhs
, nr_bhs
, err
= 0;
682 ntfs_debug("Entering for inode 0x%lx.", ni
->mft_no
);
683 BUG_ON(NInoAttr(ni
));
685 BUG_ON(!PageLocked(page
));
687 * If the ntfs_inode is clean no need to do anything. If it is dirty,
688 * mark it as clean now so that it can be redirtied later on if needed.
689 * There is no danger of races since the caller is holding the locks
690 * for the mft record @m and the page it is in.
692 if (!NInoTestClearDirty(ni
))
694 bh
= head
= page_buffers(page
);
699 m_start
= ni
->page_ofs
;
700 m_end
= m_start
+ vol
->mft_record_size
;
702 block_end
= block_start
+ blocksize
;
703 /* If the buffer is outside the mft record, skip it. */
704 if (block_end
<= m_start
)
706 if (unlikely(block_start
>= m_end
))
709 * If this block is not the first one in the record, we ignore
710 * the buffer's dirty state because we could have raced with a
711 * parallel mark_ntfs_record_dirty().
713 if (block_start
== m_start
) {
714 /* This block is the first one in the record. */
715 if (!buffer_dirty(bh
)) {
717 /* Clean records are not written out. */
721 /* Need to map the buffer if it is not mapped already. */
722 if (unlikely(!buffer_mapped(bh
))) {
725 unsigned int vcn_ofs
;
727 bh
->b_bdev
= vol
->sb
->s_bdev
;
728 /* Obtain the vcn and offset of the current block. */
729 vcn
= ((VCN
)ni
->mft_no
<< vol
->mft_record_size_bits
) +
730 (block_start
- m_start
);
731 vcn_ofs
= vcn
& vol
->cluster_size_mask
;
732 vcn
>>= vol
->cluster_size_bits
;
734 down_read(&NTFS_I(vol
->mft_ino
)->runlist
.lock
);
735 rl
= NTFS_I(vol
->mft_ino
)->runlist
.rl
;
738 /* Seek to element containing target vcn. */
739 while (rl
->length
&& rl
[1].vcn
<= vcn
)
741 lcn
= ntfs_rl_vcn_to_lcn(rl
, vcn
);
742 /* For $MFT, only lcn >= 0 is a successful remap. */
743 if (likely(lcn
>= 0)) {
744 /* Setup buffer head to correct block. */
745 bh
->b_blocknr
= ((lcn
<<
746 vol
->cluster_size_bits
) +
747 vcn_ofs
) >> blocksize_bits
;
748 set_buffer_mapped(bh
);
751 ntfs_error(vol
->sb
, "Cannot write mft record "
752 "0x%lx because its location "
753 "on disk could not be "
754 "determined (error code %lli).",
755 ni
->mft_no
, (long long)lcn
);
759 BUG_ON(!buffer_uptodate(bh
));
760 BUG_ON(!nr_bhs
&& (m_start
!= block_start
));
761 BUG_ON(nr_bhs
>= max_bhs
);
763 BUG_ON((nr_bhs
>= max_bhs
) && (m_end
!= block_end
));
764 } while (block_start
= block_end
, (bh
= bh
->b_this_page
) != head
);
766 up_read(&NTFS_I(vol
->mft_ino
)->runlist
.lock
);
771 /* Apply the mst protection fixups. */
772 err
= pre_write_mst_fixup((NTFS_RECORD
*)m
, vol
->mft_record_size
);
774 ntfs_error(vol
->sb
, "Failed to apply mst fixups!");
777 flush_dcache_mft_record_page(ni
);
778 /* Lock buffers and start synchronous write i/o on them. */
779 for (i_bhs
= 0; i_bhs
< nr_bhs
; i_bhs
++) {
780 struct buffer_head
*tbh
= bhs
[i_bhs
];
782 if (unlikely(test_set_buffer_locked(tbh
)))
784 BUG_ON(!buffer_uptodate(tbh
));
785 clear_buffer_dirty(tbh
);
787 tbh
->b_end_io
= end_buffer_write_sync
;
788 submit_bh(WRITE
, tbh
);
790 /* Synchronize the mft mirror now if not @sync. */
791 if (!sync
&& ni
->mft_no
< vol
->mftmirr_size
)
792 ntfs_sync_mft_mirror(vol
, ni
->mft_no
, m
, sync
);
793 /* Wait on i/o completion of buffers. */
794 for (i_bhs
= 0; i_bhs
< nr_bhs
; i_bhs
++) {
795 struct buffer_head
*tbh
= bhs
[i_bhs
];
798 if (unlikely(!buffer_uptodate(tbh
))) {
801 * Set the buffer uptodate so the page and buffer
802 * states do not become out of sync.
804 if (PageUptodate(page
))
805 set_buffer_uptodate(tbh
);
808 /* If @sync, now synchronize the mft mirror. */
809 if (sync
&& ni
->mft_no
< vol
->mftmirr_size
)
810 ntfs_sync_mft_mirror(vol
, ni
->mft_no
, m
, sync
);
811 /* Remove the mst protection fixups again. */
812 post_write_mst_fixup((NTFS_RECORD
*)m
);
813 flush_dcache_mft_record_page(ni
);
815 /* I/O error during writing. This is really bad! */
816 ntfs_error(vol
->sb
, "I/O error while writing mft record "
817 "0x%lx! Marking base inode as bad. You "
818 "should unmount the volume and run chkdsk.",
826 /* Clean the buffers. */
827 for (i_bhs
= 0; i_bhs
< nr_bhs
; i_bhs
++)
828 clear_buffer_dirty(bhs
[i_bhs
]);
831 * Current state: all buffers are clean, unlocked, and uptodate.
832 * The caller should mark the base inode as bad so that no more i/o
833 * happens. ->clear_inode() will still be invoked so all extent inodes
834 * and other allocated memory will be freed.
836 if (err
== -ENOMEM
) {
837 ntfs_error(vol
->sb
, "Not enough memory to write mft record. "
838 "Redirtying so the write is retried later.");
839 mark_mft_record_dirty(ni
);
847 * ntfs_may_write_mft_record - check if an mft record may be written out
848 * @vol: [IN] ntfs volume on which the mft record to check resides
849 * @mft_no: [IN] mft record number of the mft record to check
850 * @m: [IN] mapped mft record to check
851 * @locked_ni: [OUT] caller has to unlock this ntfs inode if one is returned
853 * Check if the mapped (base or extent) mft record @m with mft record number
854 * @mft_no belonging to the ntfs volume @vol may be written out. If necessary
855 * and possible the ntfs inode of the mft record is locked and the base vfs
856 * inode is pinned. The locked ntfs inode is then returned in @locked_ni. The
857 * caller is responsible for unlocking the ntfs inode and unpinning the base
860 * Return 'true' if the mft record may be written out and 'false' if not.
862 * The caller has locked the page and cleared the uptodate flag on it which
863 * means that we can safely write out any dirty mft records that do not have
864 * their inodes in icache as determined by ilookup5() as anyone
865 * opening/creating such an inode would block when attempting to map the mft
866 * record in read_cache_page() until we are finished with the write out.
868 * Here is a description of the tests we perform:
870 * If the inode is found in icache we know the mft record must be a base mft
871 * record. If it is dirty, we do not write it and return 'false' as the vfs
872 * inode write paths will result in the access times being updated which would
873 * cause the base mft record to be redirtied and written out again. (We know
874 * the access time update will modify the base mft record because Windows
875 * chkdsk complains if the standard information attribute is not in the base
878 * If the inode is in icache and not dirty, we attempt to lock the mft record
879 * and if we find the lock was already taken, it is not safe to write the mft
880 * record and we return 'false'.
882 * If we manage to obtain the lock we have exclusive access to the mft record,
883 * which also allows us safe writeout of the mft record. We then set
884 * @locked_ni to the locked ntfs inode and return 'true'.
886 * Note we cannot just lock the mft record and sleep while waiting for the lock
887 * because this would deadlock due to lock reversal (normally the mft record is
888 * locked before the page is locked but we already have the page locked here
889 * when we try to lock the mft record).
891 * If the inode is not in icache we need to perform further checks.
893 * If the mft record is not a FILE record or it is a base mft record, we can
894 * safely write it and return 'true'.
896 * We now know the mft record is an extent mft record. We check if the inode
897 * corresponding to its base mft record is in icache and obtain a reference to
898 * it if it is. If it is not, we can safely write it and return 'true'.
900 * We now have the base inode for the extent mft record. We check if it has an
901 * ntfs inode for the extent mft record attached and if not it is safe to write
902 * the extent mft record and we return 'true'.
904 * The ntfs inode for the extent mft record is attached to the base inode so we
905 * attempt to lock the extent mft record and if we find the lock was already
906 * taken, it is not safe to write the extent mft record and we return 'false'.
908 * If we manage to obtain the lock we have exclusive access to the extent mft
909 * record, which also allows us safe writeout of the extent mft record. We
910 * set the ntfs inode of the extent mft record clean and then set @locked_ni to
911 * the now locked ntfs inode and return 'true'.
913 * Note, the reason for actually writing dirty mft records here and not just
914 * relying on the vfs inode dirty code paths is that we can have mft records
915 * modified without them ever having actual inodes in memory. Also we can have
916 * dirty mft records with clean ntfs inodes in memory. None of the described
917 * cases would result in the dirty mft records being written out if we only
918 * relied on the vfs inode dirty code paths. And these cases can really occur
919 * during allocation of new mft records and in particular when the
920 * initialized_size of the $MFT/$DATA attribute is extended and the new space
921 * is initialized using ntfs_mft_record_format(). The clean inode can then
922 * appear if the mft record is reused for a new inode before it got written
925 bool ntfs_may_write_mft_record(ntfs_volume
*vol
, const unsigned long mft_no
,
926 const MFT_RECORD
*m
, ntfs_inode
**locked_ni
)
928 struct super_block
*sb
= vol
->sb
;
929 struct inode
*mft_vi
= vol
->mft_ino
;
931 ntfs_inode
*ni
, *eni
, **extent_nis
;
935 ntfs_debug("Entering for inode 0x%lx.", mft_no
);
937 * Normally we do not return a locked inode so set @locked_ni to NULL.
942 * Check if the inode corresponding to this mft record is in the VFS
943 * inode cache and obtain a reference to it if it is.
945 ntfs_debug("Looking for inode 0x%lx in icache.", mft_no
);
951 * Optimize inode 0, i.e. $MFT itself, since we have it in memory and
952 * we get here for it rather often.
955 /* Balance the below iput(). */
957 BUG_ON(vi
!= mft_vi
);
960 * Have to use ilookup5_nowait() since ilookup5() waits for the
961 * inode lock which causes ntfs to deadlock when a concurrent
962 * inode write via the inode dirty code paths and the page
963 * dirty code path of the inode dirty code path when writing
966 vi
= ilookup5_nowait(sb
, mft_no
, (test_t
)ntfs_test_inode
, &na
);
969 ntfs_debug("Base inode 0x%lx is in icache.", mft_no
);
970 /* The inode is in icache. */
972 /* Take a reference to the ntfs inode. */
973 atomic_inc(&ni
->count
);
974 /* If the inode is dirty, do not write this record. */
976 ntfs_debug("Inode 0x%lx is dirty, do not write it.",
978 atomic_dec(&ni
->count
);
982 ntfs_debug("Inode 0x%lx is not dirty.", mft_no
);
983 /* The inode is not dirty, try to take the mft record lock. */
984 if (unlikely(!mutex_trylock(&ni
->mrec_lock
))) {
985 ntfs_debug("Mft record 0x%lx is already locked, do "
986 "not write it.", mft_no
);
987 atomic_dec(&ni
->count
);
991 ntfs_debug("Managed to lock mft record 0x%lx, write it.",
994 * The write has to occur while we hold the mft record lock so
995 * return the locked ntfs inode.
1000 ntfs_debug("Inode 0x%lx is not in icache.", mft_no
);
1001 /* The inode is not in icache. */
1002 /* Write the record if it is not a mft record (type "FILE"). */
1003 if (!ntfs_is_mft_record(m
->magic
)) {
1004 ntfs_debug("Mft record 0x%lx is not a FILE record, write it.",
1008 /* Write the mft record if it is a base inode. */
1009 if (!m
->base_mft_record
) {
1010 ntfs_debug("Mft record 0x%lx is a base record, write it.",
1015 * This is an extent mft record. Check if the inode corresponding to
1016 * its base mft record is in icache and obtain a reference to it if it
1019 na
.mft_no
= MREF_LE(m
->base_mft_record
);
1020 ntfs_debug("Mft record 0x%lx is an extent record. Looking for base "
1021 "inode 0x%lx in icache.", mft_no
, na
.mft_no
);
1023 /* Balance the below iput(). */
1025 BUG_ON(vi
!= mft_vi
);
1027 vi
= ilookup5_nowait(sb
, na
.mft_no
, (test_t
)ntfs_test_inode
,
1031 * The base inode is not in icache, write this extent mft
1034 ntfs_debug("Base inode 0x%lx is not in icache, write the "
1035 "extent record.", na
.mft_no
);
1038 ntfs_debug("Base inode 0x%lx is in icache.", na
.mft_no
);
1040 * The base inode is in icache. Check if it has the extent inode
1041 * corresponding to this extent mft record attached.
1044 mutex_lock(&ni
->extent_lock
);
1045 if (ni
->nr_extents
<= 0) {
1047 * The base inode has no attached extent inodes, write this
1048 * extent mft record.
1050 mutex_unlock(&ni
->extent_lock
);
1052 ntfs_debug("Base inode 0x%lx has no attached extent inodes, "
1053 "write the extent record.", na
.mft_no
);
1056 /* Iterate over the attached extent inodes. */
1057 extent_nis
= ni
->ext
.extent_ntfs_inos
;
1058 for (eni
= NULL
, i
= 0; i
< ni
->nr_extents
; ++i
) {
1059 if (mft_no
== extent_nis
[i
]->mft_no
) {
1061 * Found the extent inode corresponding to this extent
1064 eni
= extent_nis
[i
];
1069 * If the extent inode was not attached to the base inode, write this
1070 * extent mft record.
1073 mutex_unlock(&ni
->extent_lock
);
1075 ntfs_debug("Extent inode 0x%lx is not attached to its base "
1076 "inode 0x%lx, write the extent record.",
1080 ntfs_debug("Extent inode 0x%lx is attached to its base inode 0x%lx.",
1082 /* Take a reference to the extent ntfs inode. */
1083 atomic_inc(&eni
->count
);
1084 mutex_unlock(&ni
->extent_lock
);
1086 * Found the extent inode coresponding to this extent mft record.
1087 * Try to take the mft record lock.
1089 if (unlikely(!mutex_trylock(&eni
->mrec_lock
))) {
1090 atomic_dec(&eni
->count
);
1092 ntfs_debug("Extent mft record 0x%lx is already locked, do "
1093 "not write it.", mft_no
);
1096 ntfs_debug("Managed to lock extent mft record 0x%lx, write it.",
1098 if (NInoTestClearDirty(eni
))
1099 ntfs_debug("Extent inode 0x%lx is dirty, marking it clean.",
1102 * The write has to occur while we hold the mft record lock so return
1103 * the locked extent ntfs inode.
1109 static const char *es
= " Leaving inconsistent metadata. Unmount and run "
1113 * ntfs_mft_bitmap_find_and_alloc_free_rec_nolock - see name
1114 * @vol: volume on which to search for a free mft record
1115 * @base_ni: open base inode if allocating an extent mft record or NULL
1117 * Search for a free mft record in the mft bitmap attribute on the ntfs volume
1120 * If @base_ni is NULL start the search at the default allocator position.
1122 * If @base_ni is not NULL start the search at the mft record after the base
1123 * mft record @base_ni.
1125 * Return the free mft record on success and -errno on error. An error code of
1126 * -ENOSPC means that there are no free mft records in the currently
1127 * initialized mft bitmap.
1129 * Locking: Caller must hold vol->mftbmp_lock for writing.
1131 static int ntfs_mft_bitmap_find_and_alloc_free_rec_nolock(ntfs_volume
*vol
,
1132 ntfs_inode
*base_ni
)
1134 s64 pass_end
, ll
, data_pos
, pass_start
, ofs
, bit
;
1135 unsigned long flags
;
1136 struct address_space
*mftbmp_mapping
;
1139 unsigned int page_ofs
, size
;
1142 ntfs_debug("Searching for free mft record in the currently "
1143 "initialized mft bitmap.");
1144 mftbmp_mapping
= vol
->mftbmp_ino
->i_mapping
;
1146 * Set the end of the pass making sure we do not overflow the mft
1149 read_lock_irqsave(&NTFS_I(vol
->mft_ino
)->size_lock
, flags
);
1150 pass_end
= NTFS_I(vol
->mft_ino
)->allocated_size
>>
1151 vol
->mft_record_size_bits
;
1152 read_unlock_irqrestore(&NTFS_I(vol
->mft_ino
)->size_lock
, flags
);
1153 read_lock_irqsave(&NTFS_I(vol
->mftbmp_ino
)->size_lock
, flags
);
1154 ll
= NTFS_I(vol
->mftbmp_ino
)->initialized_size
<< 3;
1155 read_unlock_irqrestore(&NTFS_I(vol
->mftbmp_ino
)->size_lock
, flags
);
1160 data_pos
= vol
->mft_data_pos
;
1162 data_pos
= base_ni
->mft_no
+ 1;
1165 if (data_pos
>= pass_end
) {
1168 /* This happens on a freshly formatted volume. */
1169 if (data_pos
>= pass_end
)
1172 pass_start
= data_pos
;
1173 ntfs_debug("Starting bitmap search: pass %u, pass_start 0x%llx, "
1174 "pass_end 0x%llx, data_pos 0x%llx.", pass
,
1175 (long long)pass_start
, (long long)pass_end
,
1176 (long long)data_pos
);
1177 /* Loop until a free mft record is found. */
1178 for (; pass
<= 2;) {
1179 /* Cap size to pass_end. */
1180 ofs
= data_pos
>> 3;
1181 page_ofs
= ofs
& ~PAGE_CACHE_MASK
;
1182 size
= PAGE_CACHE_SIZE
- page_ofs
;
1183 ll
= ((pass_end
+ 7) >> 3) - ofs
;
1188 * If we are still within the active pass, search the next page
1192 page
= ntfs_map_page(mftbmp_mapping
,
1193 ofs
>> PAGE_CACHE_SHIFT
);
1194 if (unlikely(IS_ERR(page
))) {
1195 ntfs_error(vol
->sb
, "Failed to read mft "
1196 "bitmap, aborting.");
1197 return PTR_ERR(page
);
1199 buf
= (u8
*)page_address(page
) + page_ofs
;
1202 ntfs_debug("Before inner for loop: size 0x%x, "
1203 "data_pos 0x%llx, bit 0x%llx", size
,
1204 (long long)data_pos
, (long long)bit
);
1205 for (; bit
< size
&& data_pos
+ bit
< pass_end
;
1206 bit
&= ~7ull, bit
+= 8) {
1207 byte
= buf
+ (bit
>> 3);
1210 b
= ffz((unsigned long)*byte
);
1211 if (b
< 8 && b
>= (bit
& 7)) {
1212 ll
= data_pos
+ (bit
& ~7ull) + b
;
1213 if (unlikely(ll
> (1ll << 32))) {
1214 ntfs_unmap_page(page
);
1218 flush_dcache_page(page
);
1219 set_page_dirty(page
);
1220 ntfs_unmap_page(page
);
1221 ntfs_debug("Done. (Found and "
1222 "allocated mft record "
1228 ntfs_debug("After inner for loop: size 0x%x, "
1229 "data_pos 0x%llx, bit 0x%llx", size
,
1230 (long long)data_pos
, (long long)bit
);
1232 ntfs_unmap_page(page
);
1234 * If the end of the pass has not been reached yet,
1235 * continue searching the mft bitmap for a zero bit.
1237 if (data_pos
< pass_end
)
1240 /* Do the next pass. */
1243 * Starting the second pass, in which we scan the first
1244 * part of the zone which we omitted earlier.
1246 pass_end
= pass_start
;
1247 data_pos
= pass_start
= 24;
1248 ntfs_debug("pass %i, pass_start 0x%llx, pass_end "
1249 "0x%llx.", pass
, (long long)pass_start
,
1250 (long long)pass_end
);
1251 if (data_pos
>= pass_end
)
1255 /* No free mft records in currently initialized mft bitmap. */
1256 ntfs_debug("Done. (No free mft records left in currently initialized "
1262 * ntfs_mft_bitmap_extend_allocation_nolock - extend mft bitmap by a cluster
1263 * @vol: volume on which to extend the mft bitmap attribute
1265 * Extend the mft bitmap attribute on the ntfs volume @vol by one cluster.
1267 * Note: Only changes allocated_size, i.e. does not touch initialized_size or
1270 * Return 0 on success and -errno on error.
1272 * Locking: - Caller must hold vol->mftbmp_lock for writing.
1273 * - This function takes NTFS_I(vol->mftbmp_ino)->runlist.lock for
1274 * writing and releases it before returning.
1275 * - This function takes vol->lcnbmp_lock for writing and releases it
1278 static int ntfs_mft_bitmap_extend_allocation_nolock(ntfs_volume
*vol
)
1282 unsigned long flags
;
1284 ntfs_inode
*mft_ni
, *mftbmp_ni
;
1285 runlist_element
*rl
, *rl2
= NULL
;
1286 ntfs_attr_search_ctx
*ctx
= NULL
;
1288 ATTR_RECORD
*a
= NULL
;
1296 } status
= { 0, 0, 0 };
1298 ntfs_debug("Extending mft bitmap allocation.");
1299 mft_ni
= NTFS_I(vol
->mft_ino
);
1300 mftbmp_ni
= NTFS_I(vol
->mftbmp_ino
);
1302 * Determine the last lcn of the mft bitmap. The allocated size of the
1303 * mft bitmap cannot be zero so we are ok to do this.
1305 down_write(&mftbmp_ni
->runlist
.lock
);
1306 read_lock_irqsave(&mftbmp_ni
->size_lock
, flags
);
1307 ll
= mftbmp_ni
->allocated_size
;
1308 read_unlock_irqrestore(&mftbmp_ni
->size_lock
, flags
);
1309 rl
= ntfs_attr_find_vcn_nolock(mftbmp_ni
,
1310 (ll
- 1) >> vol
->cluster_size_bits
, NULL
);
1311 if (unlikely(IS_ERR(rl
) || !rl
->length
|| rl
->lcn
< 0)) {
1312 up_write(&mftbmp_ni
->runlist
.lock
);
1313 ntfs_error(vol
->sb
, "Failed to determine last allocated "
1314 "cluster of mft bitmap attribute.");
1321 lcn
= rl
->lcn
+ rl
->length
;
1322 ntfs_debug("Last lcn of mft bitmap attribute is 0x%llx.",
1325 * Attempt to get the cluster following the last allocated cluster by
1326 * hand as it may be in the MFT zone so the allocator would not give it
1330 page
= ntfs_map_page(vol
->lcnbmp_ino
->i_mapping
,
1331 ll
>> PAGE_CACHE_SHIFT
);
1333 up_write(&mftbmp_ni
->runlist
.lock
);
1334 ntfs_error(vol
->sb
, "Failed to read from lcn bitmap.");
1335 return PTR_ERR(page
);
1337 b
= (u8
*)page_address(page
) + (ll
& ~PAGE_CACHE_MASK
);
1338 tb
= 1 << (lcn
& 7ull);
1339 down_write(&vol
->lcnbmp_lock
);
1340 if (*b
!= 0xff && !(*b
& tb
)) {
1341 /* Next cluster is free, allocate it. */
1343 flush_dcache_page(page
);
1344 set_page_dirty(page
);
1345 up_write(&vol
->lcnbmp_lock
);
1346 ntfs_unmap_page(page
);
1347 /* Update the mft bitmap runlist. */
1350 status
.added_cluster
= 1;
1351 ntfs_debug("Appending one cluster to mft bitmap.");
1353 up_write(&vol
->lcnbmp_lock
);
1354 ntfs_unmap_page(page
);
1355 /* Allocate a cluster from the DATA_ZONE. */
1356 rl2
= ntfs_cluster_alloc(vol
, rl
[1].vcn
, 1, lcn
, DATA_ZONE
,
1359 up_write(&mftbmp_ni
->runlist
.lock
);
1360 ntfs_error(vol
->sb
, "Failed to allocate a cluster for "
1362 return PTR_ERR(rl2
);
1364 rl
= ntfs_runlists_merge(mftbmp_ni
->runlist
.rl
, rl2
);
1366 up_write(&mftbmp_ni
->runlist
.lock
);
1367 ntfs_error(vol
->sb
, "Failed to merge runlists for mft "
1369 if (ntfs_cluster_free_from_rl(vol
, rl2
)) {
1370 ntfs_error(vol
->sb
, "Failed to dealocate "
1371 "allocated cluster.%s", es
);
1377 mftbmp_ni
->runlist
.rl
= rl
;
1378 status
.added_run
= 1;
1379 ntfs_debug("Adding one run to mft bitmap.");
1380 /* Find the last run in the new runlist. */
1381 for (; rl
[1].length
; rl
++)
1385 * Update the attribute record as well. Note: @rl is the last
1386 * (non-terminator) runlist element of mft bitmap.
1388 mrec
= map_mft_record(mft_ni
);
1390 ntfs_error(vol
->sb
, "Failed to map mft record.");
1391 ret
= PTR_ERR(mrec
);
1394 ctx
= ntfs_attr_get_search_ctx(mft_ni
, mrec
);
1395 if (unlikely(!ctx
)) {
1396 ntfs_error(vol
->sb
, "Failed to get search context.");
1400 ret
= ntfs_attr_lookup(mftbmp_ni
->type
, mftbmp_ni
->name
,
1401 mftbmp_ni
->name_len
, CASE_SENSITIVE
, rl
[1].vcn
, NULL
,
1403 if (unlikely(ret
)) {
1404 ntfs_error(vol
->sb
, "Failed to find last attribute extent of "
1405 "mft bitmap attribute.");
1411 ll
= sle64_to_cpu(a
->data
.non_resident
.lowest_vcn
);
1412 /* Search back for the previous last allocated cluster of mft bitmap. */
1413 for (rl2
= rl
; rl2
> mftbmp_ni
->runlist
.rl
; rl2
--) {
1417 BUG_ON(ll
< rl2
->vcn
);
1418 BUG_ON(ll
>= rl2
->vcn
+ rl2
->length
);
1419 /* Get the size for the new mapping pairs array for this extent. */
1420 mp_size
= ntfs_get_size_for_mapping_pairs(vol
, rl2
, ll
, -1);
1421 if (unlikely(mp_size
<= 0)) {
1422 ntfs_error(vol
->sb
, "Get size for mapping pairs failed for "
1423 "mft bitmap attribute extent.");
1429 /* Expand the attribute record if necessary. */
1430 old_alen
= le32_to_cpu(a
->length
);
1431 ret
= ntfs_attr_record_resize(ctx
->mrec
, a
, mp_size
+
1432 le16_to_cpu(a
->data
.non_resident
.mapping_pairs_offset
));
1433 if (unlikely(ret
)) {
1434 if (ret
!= -ENOSPC
) {
1435 ntfs_error(vol
->sb
, "Failed to resize attribute "
1436 "record for mft bitmap attribute.");
1439 // TODO: Deal with this by moving this extent to a new mft
1440 // record or by starting a new extent in a new mft record or by
1441 // moving other attributes out of this mft record.
1442 // Note: It will need to be a special mft record and if none of
1443 // those are available it gets rather complicated...
1444 ntfs_error(vol
->sb
, "Not enough space in this mft record to "
1445 "accomodate extended mft bitmap attribute "
1446 "extent. Cannot handle this yet.");
1450 status
.mp_rebuilt
= 1;
1451 /* Generate the mapping pairs array directly into the attr record. */
1452 ret
= ntfs_mapping_pairs_build(vol
, (u8
*)a
+
1453 le16_to_cpu(a
->data
.non_resident
.mapping_pairs_offset
),
1454 mp_size
, rl2
, ll
, -1, NULL
);
1455 if (unlikely(ret
)) {
1456 ntfs_error(vol
->sb
, "Failed to build mapping pairs array for "
1457 "mft bitmap attribute.");
1460 /* Update the highest_vcn. */
1461 a
->data
.non_resident
.highest_vcn
= cpu_to_sle64(rl
[1].vcn
- 1);
1463 * We now have extended the mft bitmap allocated_size by one cluster.
1464 * Reflect this in the ntfs_inode structure and the attribute record.
1466 if (a
->data
.non_resident
.lowest_vcn
) {
1468 * We are not in the first attribute extent, switch to it, but
1469 * first ensure the changes will make it to disk later.
1471 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
1472 mark_mft_record_dirty(ctx
->ntfs_ino
);
1473 ntfs_attr_reinit_search_ctx(ctx
);
1474 ret
= ntfs_attr_lookup(mftbmp_ni
->type
, mftbmp_ni
->name
,
1475 mftbmp_ni
->name_len
, CASE_SENSITIVE
, 0, NULL
,
1477 if (unlikely(ret
)) {
1478 ntfs_error(vol
->sb
, "Failed to find first attribute "
1479 "extent of mft bitmap attribute.");
1480 goto restore_undo_alloc
;
1484 write_lock_irqsave(&mftbmp_ni
->size_lock
, flags
);
1485 mftbmp_ni
->allocated_size
+= vol
->cluster_size
;
1486 a
->data
.non_resident
.allocated_size
=
1487 cpu_to_sle64(mftbmp_ni
->allocated_size
);
1488 write_unlock_irqrestore(&mftbmp_ni
->size_lock
, flags
);
1489 /* Ensure the changes make it to disk. */
1490 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
1491 mark_mft_record_dirty(ctx
->ntfs_ino
);
1492 ntfs_attr_put_search_ctx(ctx
);
1493 unmap_mft_record(mft_ni
);
1494 up_write(&mftbmp_ni
->runlist
.lock
);
1495 ntfs_debug("Done.");
1498 ntfs_attr_reinit_search_ctx(ctx
);
1499 if (ntfs_attr_lookup(mftbmp_ni
->type
, mftbmp_ni
->name
,
1500 mftbmp_ni
->name_len
, CASE_SENSITIVE
, rl
[1].vcn
, NULL
,
1502 ntfs_error(vol
->sb
, "Failed to find last attribute extent of "
1503 "mft bitmap attribute.%s", es
);
1504 write_lock_irqsave(&mftbmp_ni
->size_lock
, flags
);
1505 mftbmp_ni
->allocated_size
+= vol
->cluster_size
;
1506 write_unlock_irqrestore(&mftbmp_ni
->size_lock
, flags
);
1507 ntfs_attr_put_search_ctx(ctx
);
1508 unmap_mft_record(mft_ni
);
1509 up_write(&mftbmp_ni
->runlist
.lock
);
1511 * The only thing that is now wrong is ->allocated_size of the
1512 * base attribute extent which chkdsk should be able to fix.
1518 a
->data
.non_resident
.highest_vcn
= cpu_to_sle64(rl
[1].vcn
- 2);
1520 if (status
.added_cluster
) {
1521 /* Truncate the last run in the runlist by one cluster. */
1524 } else if (status
.added_run
) {
1526 /* Remove the last run from the runlist. */
1527 rl
->lcn
= rl
[1].lcn
;
1530 /* Deallocate the cluster. */
1531 down_write(&vol
->lcnbmp_lock
);
1532 if (ntfs_bitmap_clear_bit(vol
->lcnbmp_ino
, lcn
)) {
1533 ntfs_error(vol
->sb
, "Failed to free allocated cluster.%s", es
);
1536 up_write(&vol
->lcnbmp_lock
);
1537 if (status
.mp_rebuilt
) {
1538 if (ntfs_mapping_pairs_build(vol
, (u8
*)a
+ le16_to_cpu(
1539 a
->data
.non_resident
.mapping_pairs_offset
),
1540 old_alen
- le16_to_cpu(
1541 a
->data
.non_resident
.mapping_pairs_offset
),
1542 rl2
, ll
, -1, NULL
)) {
1543 ntfs_error(vol
->sb
, "Failed to restore mapping pairs "
1547 if (ntfs_attr_record_resize(ctx
->mrec
, a
, old_alen
)) {
1548 ntfs_error(vol
->sb
, "Failed to restore attribute "
1552 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
1553 mark_mft_record_dirty(ctx
->ntfs_ino
);
1556 ntfs_attr_put_search_ctx(ctx
);
1558 unmap_mft_record(mft_ni
);
1559 up_write(&mftbmp_ni
->runlist
.lock
);
1564 * ntfs_mft_bitmap_extend_initialized_nolock - extend mftbmp initialized data
1565 * @vol: volume on which to extend the mft bitmap attribute
1567 * Extend the initialized portion of the mft bitmap attribute on the ntfs
1568 * volume @vol by 8 bytes.
1570 * Note: Only changes initialized_size and data_size, i.e. requires that
1571 * allocated_size is big enough to fit the new initialized_size.
1573 * Return 0 on success and -error on error.
1575 * Locking: Caller must hold vol->mftbmp_lock for writing.
1577 static int ntfs_mft_bitmap_extend_initialized_nolock(ntfs_volume
*vol
)
1579 s64 old_data_size
, old_initialized_size
;
1580 unsigned long flags
;
1581 struct inode
*mftbmp_vi
;
1582 ntfs_inode
*mft_ni
, *mftbmp_ni
;
1583 ntfs_attr_search_ctx
*ctx
;
1588 ntfs_debug("Extending mft bitmap initiailized (and data) size.");
1589 mft_ni
= NTFS_I(vol
->mft_ino
);
1590 mftbmp_vi
= vol
->mftbmp_ino
;
1591 mftbmp_ni
= NTFS_I(mftbmp_vi
);
1592 /* Get the attribute record. */
1593 mrec
= map_mft_record(mft_ni
);
1595 ntfs_error(vol
->sb
, "Failed to map mft record.");
1596 return PTR_ERR(mrec
);
1598 ctx
= ntfs_attr_get_search_ctx(mft_ni
, mrec
);
1599 if (unlikely(!ctx
)) {
1600 ntfs_error(vol
->sb
, "Failed to get search context.");
1604 ret
= ntfs_attr_lookup(mftbmp_ni
->type
, mftbmp_ni
->name
,
1605 mftbmp_ni
->name_len
, CASE_SENSITIVE
, 0, NULL
, 0, ctx
);
1606 if (unlikely(ret
)) {
1607 ntfs_error(vol
->sb
, "Failed to find first attribute extent of "
1608 "mft bitmap attribute.");
1614 write_lock_irqsave(&mftbmp_ni
->size_lock
, flags
);
1615 old_data_size
= i_size_read(mftbmp_vi
);
1616 old_initialized_size
= mftbmp_ni
->initialized_size
;
1618 * We can simply update the initialized_size before filling the space
1619 * with zeroes because the caller is holding the mft bitmap lock for
1620 * writing which ensures that no one else is trying to access the data.
1622 mftbmp_ni
->initialized_size
+= 8;
1623 a
->data
.non_resident
.initialized_size
=
1624 cpu_to_sle64(mftbmp_ni
->initialized_size
);
1625 if (mftbmp_ni
->initialized_size
> old_data_size
) {
1626 i_size_write(mftbmp_vi
, mftbmp_ni
->initialized_size
);
1627 a
->data
.non_resident
.data_size
=
1628 cpu_to_sle64(mftbmp_ni
->initialized_size
);
1630 write_unlock_irqrestore(&mftbmp_ni
->size_lock
, flags
);
1631 /* Ensure the changes make it to disk. */
1632 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
1633 mark_mft_record_dirty(ctx
->ntfs_ino
);
1634 ntfs_attr_put_search_ctx(ctx
);
1635 unmap_mft_record(mft_ni
);
1636 /* Initialize the mft bitmap attribute value with zeroes. */
1637 ret
= ntfs_attr_set(mftbmp_ni
, old_initialized_size
, 8, 0);
1639 ntfs_debug("Done. (Wrote eight initialized bytes to mft "
1643 ntfs_error(vol
->sb
, "Failed to write to mft bitmap.");
1644 /* Try to recover from the error. */
1645 mrec
= map_mft_record(mft_ni
);
1647 ntfs_error(vol
->sb
, "Failed to map mft record.%s", es
);
1651 ctx
= ntfs_attr_get_search_ctx(mft_ni
, mrec
);
1652 if (unlikely(!ctx
)) {
1653 ntfs_error(vol
->sb
, "Failed to get search context.%s", es
);
1657 if (ntfs_attr_lookup(mftbmp_ni
->type
, mftbmp_ni
->name
,
1658 mftbmp_ni
->name_len
, CASE_SENSITIVE
, 0, NULL
, 0, ctx
)) {
1659 ntfs_error(vol
->sb
, "Failed to find first attribute extent of "
1660 "mft bitmap attribute.%s", es
);
1663 ntfs_attr_put_search_ctx(ctx
);
1665 unmap_mft_record(mft_ni
);
1669 write_lock_irqsave(&mftbmp_ni
->size_lock
, flags
);
1670 mftbmp_ni
->initialized_size
= old_initialized_size
;
1671 a
->data
.non_resident
.initialized_size
=
1672 cpu_to_sle64(old_initialized_size
);
1673 if (i_size_read(mftbmp_vi
) != old_data_size
) {
1674 i_size_write(mftbmp_vi
, old_data_size
);
1675 a
->data
.non_resident
.data_size
= cpu_to_sle64(old_data_size
);
1677 write_unlock_irqrestore(&mftbmp_ni
->size_lock
, flags
);
1678 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
1679 mark_mft_record_dirty(ctx
->ntfs_ino
);
1680 ntfs_attr_put_search_ctx(ctx
);
1681 unmap_mft_record(mft_ni
);
1683 read_lock_irqsave(&mftbmp_ni
->size_lock
, flags
);
1684 ntfs_debug("Restored status of mftbmp: allocated_size 0x%llx, "
1685 "data_size 0x%llx, initialized_size 0x%llx.",
1686 (long long)mftbmp_ni
->allocated_size
,
1687 (long long)i_size_read(mftbmp_vi
),
1688 (long long)mftbmp_ni
->initialized_size
);
1689 read_unlock_irqrestore(&mftbmp_ni
->size_lock
, flags
);
1696 * ntfs_mft_data_extend_allocation_nolock - extend mft data attribute
1697 * @vol: volume on which to extend the mft data attribute
1699 * Extend the mft data attribute on the ntfs volume @vol by 16 mft records
1700 * worth of clusters or if not enough space for this by one mft record worth
1703 * Note: Only changes allocated_size, i.e. does not touch initialized_size or
1706 * Return 0 on success and -errno on error.
1708 * Locking: - Caller must hold vol->mftbmp_lock for writing.
1709 * - This function takes NTFS_I(vol->mft_ino)->runlist.lock for
1710 * writing and releases it before returning.
1711 * - This function calls functions which take vol->lcnbmp_lock for
1712 * writing and release it before returning.
1714 static int ntfs_mft_data_extend_allocation_nolock(ntfs_volume
*vol
)
1719 unsigned long flags
;
1721 runlist_element
*rl
, *rl2
;
1722 ntfs_attr_search_ctx
*ctx
= NULL
;
1724 ATTR_RECORD
*a
= NULL
;
1727 bool mp_rebuilt
= false;
1729 ntfs_debug("Extending mft data allocation.");
1730 mft_ni
= NTFS_I(vol
->mft_ino
);
1732 * Determine the preferred allocation location, i.e. the last lcn of
1733 * the mft data attribute. The allocated size of the mft data
1734 * attribute cannot be zero so we are ok to do this.
1736 down_write(&mft_ni
->runlist
.lock
);
1737 read_lock_irqsave(&mft_ni
->size_lock
, flags
);
1738 ll
= mft_ni
->allocated_size
;
1739 read_unlock_irqrestore(&mft_ni
->size_lock
, flags
);
1740 rl
= ntfs_attr_find_vcn_nolock(mft_ni
,
1741 (ll
- 1) >> vol
->cluster_size_bits
, NULL
);
1742 if (unlikely(IS_ERR(rl
) || !rl
->length
|| rl
->lcn
< 0)) {
1743 up_write(&mft_ni
->runlist
.lock
);
1744 ntfs_error(vol
->sb
, "Failed to determine last allocated "
1745 "cluster of mft data attribute.");
1752 lcn
= rl
->lcn
+ rl
->length
;
1753 ntfs_debug("Last lcn of mft data attribute is 0x%llx.", (long long)lcn
);
1754 /* Minimum allocation is one mft record worth of clusters. */
1755 min_nr
= vol
->mft_record_size
>> vol
->cluster_size_bits
;
1758 /* Want to allocate 16 mft records worth of clusters. */
1759 nr
= vol
->mft_record_size
<< 4 >> vol
->cluster_size_bits
;
1762 /* Ensure we do not go above 2^32-1 mft records. */
1763 read_lock_irqsave(&mft_ni
->size_lock
, flags
);
1764 ll
= mft_ni
->allocated_size
;
1765 read_unlock_irqrestore(&mft_ni
->size_lock
, flags
);
1766 if (unlikely((ll
+ (nr
<< vol
->cluster_size_bits
)) >>
1767 vol
->mft_record_size_bits
>= (1ll << 32))) {
1769 if (unlikely((ll
+ (nr
<< vol
->cluster_size_bits
)) >>
1770 vol
->mft_record_size_bits
>= (1ll << 32))) {
1771 ntfs_warning(vol
->sb
, "Cannot allocate mft record "
1772 "because the maximum number of inodes "
1773 "(2^32) has already been reached.");
1774 up_write(&mft_ni
->runlist
.lock
);
1778 ntfs_debug("Trying mft data allocation with %s cluster count %lli.",
1779 nr
> min_nr
? "default" : "minimal", (long long)nr
);
1780 old_last_vcn
= rl
[1].vcn
;
1782 rl2
= ntfs_cluster_alloc(vol
, old_last_vcn
, nr
, lcn
, MFT_ZONE
,
1784 if (likely(!IS_ERR(rl2
)))
1786 if (PTR_ERR(rl2
) != -ENOSPC
|| nr
== min_nr
) {
1787 ntfs_error(vol
->sb
, "Failed to allocate the minimal "
1788 "number of clusters (%lli) for the "
1789 "mft data attribute.", (long long)nr
);
1790 up_write(&mft_ni
->runlist
.lock
);
1791 return PTR_ERR(rl2
);
1794 * There is not enough space to do the allocation, but there
1795 * might be enough space to do a minimal allocation so try that
1799 ntfs_debug("Retrying mft data allocation with minimal cluster "
1800 "count %lli.", (long long)nr
);
1802 rl
= ntfs_runlists_merge(mft_ni
->runlist
.rl
, rl2
);
1804 up_write(&mft_ni
->runlist
.lock
);
1805 ntfs_error(vol
->sb
, "Failed to merge runlists for mft data "
1807 if (ntfs_cluster_free_from_rl(vol
, rl2
)) {
1808 ntfs_error(vol
->sb
, "Failed to dealocate clusters "
1809 "from the mft data attribute.%s", es
);
1815 mft_ni
->runlist
.rl
= rl
;
1816 ntfs_debug("Allocated %lli clusters.", (long long)nr
);
1817 /* Find the last run in the new runlist. */
1818 for (; rl
[1].length
; rl
++)
1820 /* Update the attribute record as well. */
1821 mrec
= map_mft_record(mft_ni
);
1823 ntfs_error(vol
->sb
, "Failed to map mft record.");
1824 ret
= PTR_ERR(mrec
);
1827 ctx
= ntfs_attr_get_search_ctx(mft_ni
, mrec
);
1828 if (unlikely(!ctx
)) {
1829 ntfs_error(vol
->sb
, "Failed to get search context.");
1833 ret
= ntfs_attr_lookup(mft_ni
->type
, mft_ni
->name
, mft_ni
->name_len
,
1834 CASE_SENSITIVE
, rl
[1].vcn
, NULL
, 0, ctx
);
1835 if (unlikely(ret
)) {
1836 ntfs_error(vol
->sb
, "Failed to find last attribute extent of "
1837 "mft data attribute.");
1843 ll
= sle64_to_cpu(a
->data
.non_resident
.lowest_vcn
);
1844 /* Search back for the previous last allocated cluster of mft bitmap. */
1845 for (rl2
= rl
; rl2
> mft_ni
->runlist
.rl
; rl2
--) {
1849 BUG_ON(ll
< rl2
->vcn
);
1850 BUG_ON(ll
>= rl2
->vcn
+ rl2
->length
);
1851 /* Get the size for the new mapping pairs array for this extent. */
1852 mp_size
= ntfs_get_size_for_mapping_pairs(vol
, rl2
, ll
, -1);
1853 if (unlikely(mp_size
<= 0)) {
1854 ntfs_error(vol
->sb
, "Get size for mapping pairs failed for "
1855 "mft data attribute extent.");
1861 /* Expand the attribute record if necessary. */
1862 old_alen
= le32_to_cpu(a
->length
);
1863 ret
= ntfs_attr_record_resize(ctx
->mrec
, a
, mp_size
+
1864 le16_to_cpu(a
->data
.non_resident
.mapping_pairs_offset
));
1865 if (unlikely(ret
)) {
1866 if (ret
!= -ENOSPC
) {
1867 ntfs_error(vol
->sb
, "Failed to resize attribute "
1868 "record for mft data attribute.");
1871 // TODO: Deal with this by moving this extent to a new mft
1872 // record or by starting a new extent in a new mft record or by
1873 // moving other attributes out of this mft record.
1874 // Note: Use the special reserved mft records and ensure that
1875 // this extent is not required to find the mft record in
1876 // question. If no free special records left we would need to
1877 // move an existing record away, insert ours in its place, and
1878 // then place the moved record into the newly allocated space
1879 // and we would then need to update all references to this mft
1880 // record appropriately. This is rather complicated...
1881 ntfs_error(vol
->sb
, "Not enough space in this mft record to "
1882 "accomodate extended mft data attribute "
1883 "extent. Cannot handle this yet.");
1888 /* Generate the mapping pairs array directly into the attr record. */
1889 ret
= ntfs_mapping_pairs_build(vol
, (u8
*)a
+
1890 le16_to_cpu(a
->data
.non_resident
.mapping_pairs_offset
),
1891 mp_size
, rl2
, ll
, -1, NULL
);
1892 if (unlikely(ret
)) {
1893 ntfs_error(vol
->sb
, "Failed to build mapping pairs array of "
1894 "mft data attribute.");
1897 /* Update the highest_vcn. */
1898 a
->data
.non_resident
.highest_vcn
= cpu_to_sle64(rl
[1].vcn
- 1);
1900 * We now have extended the mft data allocated_size by nr clusters.
1901 * Reflect this in the ntfs_inode structure and the attribute record.
1902 * @rl is the last (non-terminator) runlist element of mft data
1905 if (a
->data
.non_resident
.lowest_vcn
) {
1907 * We are not in the first attribute extent, switch to it, but
1908 * first ensure the changes will make it to disk later.
1910 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
1911 mark_mft_record_dirty(ctx
->ntfs_ino
);
1912 ntfs_attr_reinit_search_ctx(ctx
);
1913 ret
= ntfs_attr_lookup(mft_ni
->type
, mft_ni
->name
,
1914 mft_ni
->name_len
, CASE_SENSITIVE
, 0, NULL
, 0,
1916 if (unlikely(ret
)) {
1917 ntfs_error(vol
->sb
, "Failed to find first attribute "
1918 "extent of mft data attribute.");
1919 goto restore_undo_alloc
;
1923 write_lock_irqsave(&mft_ni
->size_lock
, flags
);
1924 mft_ni
->allocated_size
+= nr
<< vol
->cluster_size_bits
;
1925 a
->data
.non_resident
.allocated_size
=
1926 cpu_to_sle64(mft_ni
->allocated_size
);
1927 write_unlock_irqrestore(&mft_ni
->size_lock
, flags
);
1928 /* Ensure the changes make it to disk. */
1929 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
1930 mark_mft_record_dirty(ctx
->ntfs_ino
);
1931 ntfs_attr_put_search_ctx(ctx
);
1932 unmap_mft_record(mft_ni
);
1933 up_write(&mft_ni
->runlist
.lock
);
1934 ntfs_debug("Done.");
1937 ntfs_attr_reinit_search_ctx(ctx
);
1938 if (ntfs_attr_lookup(mft_ni
->type
, mft_ni
->name
, mft_ni
->name_len
,
1939 CASE_SENSITIVE
, rl
[1].vcn
, NULL
, 0, ctx
)) {
1940 ntfs_error(vol
->sb
, "Failed to find last attribute extent of "
1941 "mft data attribute.%s", es
);
1942 write_lock_irqsave(&mft_ni
->size_lock
, flags
);
1943 mft_ni
->allocated_size
+= nr
<< vol
->cluster_size_bits
;
1944 write_unlock_irqrestore(&mft_ni
->size_lock
, flags
);
1945 ntfs_attr_put_search_ctx(ctx
);
1946 unmap_mft_record(mft_ni
);
1947 up_write(&mft_ni
->runlist
.lock
);
1949 * The only thing that is now wrong is ->allocated_size of the
1950 * base attribute extent which chkdsk should be able to fix.
1955 ctx
->attr
->data
.non_resident
.highest_vcn
=
1956 cpu_to_sle64(old_last_vcn
- 1);
1958 if (ntfs_cluster_free(mft_ni
, old_last_vcn
, -1, ctx
) < 0) {
1959 ntfs_error(vol
->sb
, "Failed to free clusters from mft data "
1960 "attribute.%s", es
);
1964 if (ntfs_rl_truncate_nolock(vol
, &mft_ni
->runlist
, old_last_vcn
)) {
1965 ntfs_error(vol
->sb
, "Failed to truncate mft data attribute "
1969 if (mp_rebuilt
&& !IS_ERR(ctx
->mrec
)) {
1970 if (ntfs_mapping_pairs_build(vol
, (u8
*)a
+ le16_to_cpu(
1971 a
->data
.non_resident
.mapping_pairs_offset
),
1972 old_alen
- le16_to_cpu(
1973 a
->data
.non_resident
.mapping_pairs_offset
),
1974 rl2
, ll
, -1, NULL
)) {
1975 ntfs_error(vol
->sb
, "Failed to restore mapping pairs "
1979 if (ntfs_attr_record_resize(ctx
->mrec
, a
, old_alen
)) {
1980 ntfs_error(vol
->sb
, "Failed to restore attribute "
1984 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
1985 mark_mft_record_dirty(ctx
->ntfs_ino
);
1986 } else if (IS_ERR(ctx
->mrec
)) {
1987 ntfs_error(vol
->sb
, "Failed to restore attribute search "
1992 ntfs_attr_put_search_ctx(ctx
);
1994 unmap_mft_record(mft_ni
);
1995 up_write(&mft_ni
->runlist
.lock
);
2000 * ntfs_mft_record_layout - layout an mft record into a memory buffer
2001 * @vol: volume to which the mft record will belong
2002 * @mft_no: mft reference specifying the mft record number
2003 * @m: destination buffer of size >= @vol->mft_record_size bytes
2005 * Layout an empty, unused mft record with the mft record number @mft_no into
2006 * the buffer @m. The volume @vol is needed because the mft record structure
2007 * was modified in NTFS 3.1 so we need to know which volume version this mft
2008 * record will be used on.
2010 * Return 0 on success and -errno on error.
2012 static int ntfs_mft_record_layout(const ntfs_volume
*vol
, const s64 mft_no
,
2017 ntfs_debug("Entering for mft record 0x%llx.", (long long)mft_no
);
2018 if (mft_no
>= (1ll << 32)) {
2019 ntfs_error(vol
->sb
, "Mft record number 0x%llx exceeds "
2020 "maximum of 2^32.", (long long)mft_no
);
2023 /* Start by clearing the whole mft record to gives us a clean slate. */
2024 memset(m
, 0, vol
->mft_record_size
);
2025 /* Aligned to 2-byte boundary. */
2026 if (vol
->major_ver
< 3 || (vol
->major_ver
== 3 && !vol
->minor_ver
))
2027 m
->usa_ofs
= cpu_to_le16((sizeof(MFT_RECORD_OLD
) + 1) & ~1);
2029 m
->usa_ofs
= cpu_to_le16((sizeof(MFT_RECORD
) + 1) & ~1);
2031 * Set the NTFS 3.1+ specific fields while we know that the
2032 * volume version is 3.1+.
2035 m
->mft_record_number
= cpu_to_le32((u32
)mft_no
);
2037 m
->magic
= magic_FILE
;
2038 if (vol
->mft_record_size
>= NTFS_BLOCK_SIZE
)
2039 m
->usa_count
= cpu_to_le16(vol
->mft_record_size
/
2040 NTFS_BLOCK_SIZE
+ 1);
2042 m
->usa_count
= cpu_to_le16(1);
2043 ntfs_warning(vol
->sb
, "Sector size is bigger than mft record "
2044 "size. Setting usa_count to 1. If chkdsk "
2045 "reports this as corruption, please email "
2046 "linux-ntfs-dev@lists.sourceforge.net stating "
2047 "that you saw this message and that the "
2048 "modified filesystem created was corrupt. "
2051 /* Set the update sequence number to 1. */
2052 *(le16
*)((u8
*)m
+ le16_to_cpu(m
->usa_ofs
)) = cpu_to_le16(1);
2054 m
->sequence_number
= cpu_to_le16(1);
2057 * Place the attributes straight after the update sequence array,
2058 * aligned to 8-byte boundary.
2060 m
->attrs_offset
= cpu_to_le16((le16_to_cpu(m
->usa_ofs
) +
2061 (le16_to_cpu(m
->usa_count
) << 1) + 7) & ~7);
2064 * Using attrs_offset plus eight bytes (for the termination attribute).
2065 * attrs_offset is already aligned to 8-byte boundary, so no need to
2068 m
->bytes_in_use
= cpu_to_le32(le16_to_cpu(m
->attrs_offset
) + 8);
2069 m
->bytes_allocated
= cpu_to_le32(vol
->mft_record_size
);
2070 m
->base_mft_record
= 0;
2071 m
->next_attr_instance
= 0;
2072 /* Add the termination attribute. */
2073 a
= (ATTR_RECORD
*)((u8
*)m
+ le16_to_cpu(m
->attrs_offset
));
2076 ntfs_debug("Done.");
2081 * ntfs_mft_record_format - format an mft record on an ntfs volume
2082 * @vol: volume on which to format the mft record
2083 * @mft_no: mft record number to format
2085 * Format the mft record @mft_no in $MFT/$DATA, i.e. lay out an empty, unused
2086 * mft record into the appropriate place of the mft data attribute. This is
2087 * used when extending the mft data attribute.
2089 * Return 0 on success and -errno on error.
2091 static int ntfs_mft_record_format(const ntfs_volume
*vol
, const s64 mft_no
)
2094 struct inode
*mft_vi
= vol
->mft_ino
;
2097 pgoff_t index
, end_index
;
2101 ntfs_debug("Entering for mft record 0x%llx.", (long long)mft_no
);
2103 * The index into the page cache and the offset within the page cache
2104 * page of the wanted mft record.
2106 index
= mft_no
<< vol
->mft_record_size_bits
>> PAGE_CACHE_SHIFT
;
2107 ofs
= (mft_no
<< vol
->mft_record_size_bits
) & ~PAGE_CACHE_MASK
;
2108 /* The maximum valid index into the page cache for $MFT's data. */
2109 i_size
= i_size_read(mft_vi
);
2110 end_index
= i_size
>> PAGE_CACHE_SHIFT
;
2111 if (unlikely(index
>= end_index
)) {
2112 if (unlikely(index
> end_index
|| ofs
+ vol
->mft_record_size
>=
2113 (i_size
& ~PAGE_CACHE_MASK
))) {
2114 ntfs_error(vol
->sb
, "Tried to format non-existing mft "
2115 "record 0x%llx.", (long long)mft_no
);
2119 /* Read, map, and pin the page containing the mft record. */
2120 page
= ntfs_map_page(mft_vi
->i_mapping
, index
);
2121 if (unlikely(IS_ERR(page
))) {
2122 ntfs_error(vol
->sb
, "Failed to map page containing mft record "
2123 "to format 0x%llx.", (long long)mft_no
);
2124 return PTR_ERR(page
);
2127 BUG_ON(!PageUptodate(page
));
2128 ClearPageUptodate(page
);
2129 m
= (MFT_RECORD
*)((u8
*)page_address(page
) + ofs
);
2130 err
= ntfs_mft_record_layout(vol
, mft_no
, m
);
2131 if (unlikely(err
)) {
2132 ntfs_error(vol
->sb
, "Failed to layout mft record 0x%llx.",
2134 SetPageUptodate(page
);
2136 ntfs_unmap_page(page
);
2139 flush_dcache_page(page
);
2140 SetPageUptodate(page
);
2143 * Make sure the mft record is written out to disk. We could use
2144 * ilookup5() to check if an inode is in icache and so on but this is
2145 * unnecessary as ntfs_writepage() will write the dirty record anyway.
2147 mark_ntfs_record_dirty(page
, ofs
);
2148 ntfs_unmap_page(page
);
2149 ntfs_debug("Done.");
2154 * ntfs_mft_record_alloc - allocate an mft record on an ntfs volume
2155 * @vol: [IN] volume on which to allocate the mft record
2156 * @mode: [IN] mode if want a file or directory, i.e. base inode or 0
2157 * @base_ni: [IN] open base inode if allocating an extent mft record or NULL
2158 * @mrec: [OUT] on successful return this is the mapped mft record
2160 * Allocate an mft record in $MFT/$DATA of an open ntfs volume @vol.
2162 * If @base_ni is NULL make the mft record a base mft record, i.e. a file or
2163 * direvctory inode, and allocate it at the default allocator position. In
2164 * this case @mode is the file mode as given to us by the caller. We in
2165 * particular use @mode to distinguish whether a file or a directory is being
2166 * created (S_IFDIR(mode) and S_IFREG(mode), respectively).
2168 * If @base_ni is not NULL make the allocated mft record an extent record,
2169 * allocate it starting at the mft record after the base mft record and attach
2170 * the allocated and opened ntfs inode to the base inode @base_ni. In this
2171 * case @mode must be 0 as it is meaningless for extent inodes.
2173 * You need to check the return value with IS_ERR(). If false, the function
2174 * was successful and the return value is the now opened ntfs inode of the
2175 * allocated mft record. *@mrec is then set to the allocated, mapped, pinned,
2176 * and locked mft record. If IS_ERR() is true, the function failed and the
2177 * error code is obtained from PTR_ERR(return value). *@mrec is undefined in
2180 * Allocation strategy:
2182 * To find a free mft record, we scan the mft bitmap for a zero bit. To
2183 * optimize this we start scanning at the place specified by @base_ni or if
2184 * @base_ni is NULL we start where we last stopped and we perform wrap around
2185 * when we reach the end. Note, we do not try to allocate mft records below
2186 * number 24 because numbers 0 to 15 are the defined system files anyway and 16
2187 * to 24 are special in that they are used for storing extension mft records
2188 * for the $DATA attribute of $MFT. This is required to avoid the possibility
2189 * of creating a runlist with a circular dependency which once written to disk
2190 * can never be read in again. Windows will only use records 16 to 24 for
2191 * normal files if the volume is completely out of space. We never use them
2192 * which means that when the volume is really out of space we cannot create any
2193 * more files while Windows can still create up to 8 small files. We can start
2194 * doing this at some later time, it does not matter much for now.
2196 * When scanning the mft bitmap, we only search up to the last allocated mft
2197 * record. If there are no free records left in the range 24 to number of
2198 * allocated mft records, then we extend the $MFT/$DATA attribute in order to
2199 * create free mft records. We extend the allocated size of $MFT/$DATA by 16
2200 * records at a time or one cluster, if cluster size is above 16kiB. If there
2201 * is not sufficient space to do this, we try to extend by a single mft record
2202 * or one cluster, if cluster size is above the mft record size.
2204 * No matter how many mft records we allocate, we initialize only the first
2205 * allocated mft record, incrementing mft data size and initialized size
2206 * accordingly, open an ntfs_inode for it and return it to the caller, unless
2207 * there are less than 24 mft records, in which case we allocate and initialize
2208 * mft records until we reach record 24 which we consider as the first free mft
2209 * record for use by normal files.
2211 * If during any stage we overflow the initialized data in the mft bitmap, we
2212 * extend the initialized size (and data size) by 8 bytes, allocating another
2213 * cluster if required. The bitmap data size has to be at least equal to the
2214 * number of mft records in the mft, but it can be bigger, in which case the
2215 * superflous bits are padded with zeroes.
2217 * Thus, when we return successfully (IS_ERR() is false), we will have:
2218 * - initialized / extended the mft bitmap if necessary,
2219 * - initialized / extended the mft data if necessary,
2220 * - set the bit corresponding to the mft record being allocated in the
2222 * - opened an ntfs_inode for the allocated mft record, and we will have
2223 * - returned the ntfs_inode as well as the allocated mapped, pinned, and
2224 * locked mft record.
2226 * On error, the volume will be left in a consistent state and no record will
2227 * be allocated. If rolling back a partial operation fails, we may leave some
2228 * inconsistent metadata in which case we set NVolErrors() so the volume is
2229 * left dirty when unmounted.
2231 * Note, this function cannot make use of most of the normal functions, like
2232 * for example for attribute resizing, etc, because when the run list overflows
2233 * the base mft record and an attribute list is used, it is very important that
2234 * the extension mft records used to store the $DATA attribute of $MFT can be
2235 * reached without having to read the information contained inside them, as
2236 * this would make it impossible to find them in the first place after the
2237 * volume is unmounted. $MFT/$BITMAP probably does not need to follow this
2238 * rule because the bitmap is not essential for finding the mft records, but on
2239 * the other hand, handling the bitmap in this special way would make life
2240 * easier because otherwise there might be circular invocations of functions
2241 * when reading the bitmap.
2243 ntfs_inode
*ntfs_mft_record_alloc(ntfs_volume
*vol
, const int mode
,
2244 ntfs_inode
*base_ni
, MFT_RECORD
**mrec
)
2246 s64 ll
, bit
, old_data_initialized
, old_data_size
;
2247 unsigned long flags
;
2250 ntfs_inode
*mft_ni
, *mftbmp_ni
, *ni
;
2251 ntfs_attr_search_ctx
*ctx
;
2258 bool record_formatted
= false;
2261 ntfs_debug("Entering (allocating an extent mft record for "
2262 "base mft record 0x%llx).",
2263 (long long)base_ni
->mft_no
);
2264 /* @mode and @base_ni are mutually exclusive. */
2267 ntfs_debug("Entering (allocating a base mft record).");
2269 /* @mode and @base_ni are mutually exclusive. */
2271 /* We only support creation of normal files and directories. */
2272 if (!S_ISREG(mode
) && !S_ISDIR(mode
))
2273 return ERR_PTR(-EOPNOTSUPP
);
2276 mft_ni
= NTFS_I(vol
->mft_ino
);
2277 mftbmp_ni
= NTFS_I(vol
->mftbmp_ino
);
2278 down_write(&vol
->mftbmp_lock
);
2279 bit
= ntfs_mft_bitmap_find_and_alloc_free_rec_nolock(vol
, base_ni
);
2281 ntfs_debug("Found and allocated free record (#1), bit 0x%llx.",
2283 goto have_alloc_rec
;
2285 if (bit
!= -ENOSPC
) {
2286 up_write(&vol
->mftbmp_lock
);
2287 return ERR_PTR(bit
);
2290 * No free mft records left. If the mft bitmap already covers more
2291 * than the currently used mft records, the next records are all free,
2292 * so we can simply allocate the first unused mft record.
2293 * Note: We also have to make sure that the mft bitmap at least covers
2294 * the first 24 mft records as they are special and whilst they may not
2295 * be in use, we do not allocate from them.
2297 read_lock_irqsave(&mft_ni
->size_lock
, flags
);
2298 ll
= mft_ni
->initialized_size
>> vol
->mft_record_size_bits
;
2299 read_unlock_irqrestore(&mft_ni
->size_lock
, flags
);
2300 read_lock_irqsave(&mftbmp_ni
->size_lock
, flags
);
2301 old_data_initialized
= mftbmp_ni
->initialized_size
;
2302 read_unlock_irqrestore(&mftbmp_ni
->size_lock
, flags
);
2303 if (old_data_initialized
<< 3 > ll
&& old_data_initialized
> 3) {
2307 if (unlikely(bit
>= (1ll << 32)))
2309 ntfs_debug("Found free record (#2), bit 0x%llx.",
2311 goto found_free_rec
;
2314 * The mft bitmap needs to be expanded until it covers the first unused
2315 * mft record that we can allocate.
2316 * Note: The smallest mft record we allocate is mft record 24.
2318 bit
= old_data_initialized
<< 3;
2319 if (unlikely(bit
>= (1ll << 32)))
2321 read_lock_irqsave(&mftbmp_ni
->size_lock
, flags
);
2322 old_data_size
= mftbmp_ni
->allocated_size
;
2323 ntfs_debug("Status of mftbmp before extension: allocated_size 0x%llx, "
2324 "data_size 0x%llx, initialized_size 0x%llx.",
2325 (long long)old_data_size
,
2326 (long long)i_size_read(vol
->mftbmp_ino
),
2327 (long long)old_data_initialized
);
2328 read_unlock_irqrestore(&mftbmp_ni
->size_lock
, flags
);
2329 if (old_data_initialized
+ 8 > old_data_size
) {
2330 /* Need to extend bitmap by one more cluster. */
2331 ntfs_debug("mftbmp: initialized_size + 8 > allocated_size.");
2332 err
= ntfs_mft_bitmap_extend_allocation_nolock(vol
);
2333 if (unlikely(err
)) {
2334 up_write(&vol
->mftbmp_lock
);
2338 read_lock_irqsave(&mftbmp_ni
->size_lock
, flags
);
2339 ntfs_debug("Status of mftbmp after allocation extension: "
2340 "allocated_size 0x%llx, data_size 0x%llx, "
2341 "initialized_size 0x%llx.",
2342 (long long)mftbmp_ni
->allocated_size
,
2343 (long long)i_size_read(vol
->mftbmp_ino
),
2344 (long long)mftbmp_ni
->initialized_size
);
2345 read_unlock_irqrestore(&mftbmp_ni
->size_lock
, flags
);
2349 * We now have sufficient allocated space, extend the initialized_size
2350 * as well as the data_size if necessary and fill the new space with
2353 err
= ntfs_mft_bitmap_extend_initialized_nolock(vol
);
2354 if (unlikely(err
)) {
2355 up_write(&vol
->mftbmp_lock
);
2359 read_lock_irqsave(&mftbmp_ni
->size_lock
, flags
);
2360 ntfs_debug("Status of mftbmp after initialized extention: "
2361 "allocated_size 0x%llx, data_size 0x%llx, "
2362 "initialized_size 0x%llx.",
2363 (long long)mftbmp_ni
->allocated_size
,
2364 (long long)i_size_read(vol
->mftbmp_ino
),
2365 (long long)mftbmp_ni
->initialized_size
);
2366 read_unlock_irqrestore(&mftbmp_ni
->size_lock
, flags
);
2368 ntfs_debug("Found free record (#3), bit 0x%llx.", (long long)bit
);
2370 /* @bit is the found free mft record, allocate it in the mft bitmap. */
2371 ntfs_debug("At found_free_rec.");
2372 err
= ntfs_bitmap_set_bit(vol
->mftbmp_ino
, bit
);
2373 if (unlikely(err
)) {
2374 ntfs_error(vol
->sb
, "Failed to allocate bit in mft bitmap.");
2375 up_write(&vol
->mftbmp_lock
);
2378 ntfs_debug("Set bit 0x%llx in mft bitmap.", (long long)bit
);
2381 * The mft bitmap is now uptodate. Deal with mft data attribute now.
2382 * Note, we keep hold of the mft bitmap lock for writing until all
2383 * modifications to the mft data attribute are complete, too, as they
2384 * will impact decisions for mft bitmap and mft record allocation done
2385 * by a parallel allocation and if the lock is not maintained a
2386 * parallel allocation could allocate the same mft record as this one.
2388 ll
= (bit
+ 1) << vol
->mft_record_size_bits
;
2389 read_lock_irqsave(&mft_ni
->size_lock
, flags
);
2390 old_data_initialized
= mft_ni
->initialized_size
;
2391 read_unlock_irqrestore(&mft_ni
->size_lock
, flags
);
2392 if (ll
<= old_data_initialized
) {
2393 ntfs_debug("Allocated mft record already initialized.");
2394 goto mft_rec_already_initialized
;
2396 ntfs_debug("Initializing allocated mft record.");
2398 * The mft record is outside the initialized data. Extend the mft data
2399 * attribute until it covers the allocated record. The loop is only
2400 * actually traversed more than once when a freshly formatted volume is
2401 * first written to so it optimizes away nicely in the common case.
2403 read_lock_irqsave(&mft_ni
->size_lock
, flags
);
2404 ntfs_debug("Status of mft data before extension: "
2405 "allocated_size 0x%llx, data_size 0x%llx, "
2406 "initialized_size 0x%llx.",
2407 (long long)mft_ni
->allocated_size
,
2408 (long long)i_size_read(vol
->mft_ino
),
2409 (long long)mft_ni
->initialized_size
);
2410 while (ll
> mft_ni
->allocated_size
) {
2411 read_unlock_irqrestore(&mft_ni
->size_lock
, flags
);
2412 err
= ntfs_mft_data_extend_allocation_nolock(vol
);
2413 if (unlikely(err
)) {
2414 ntfs_error(vol
->sb
, "Failed to extend mft data "
2416 goto undo_mftbmp_alloc_nolock
;
2418 read_lock_irqsave(&mft_ni
->size_lock
, flags
);
2419 ntfs_debug("Status of mft data after allocation extension: "
2420 "allocated_size 0x%llx, data_size 0x%llx, "
2421 "initialized_size 0x%llx.",
2422 (long long)mft_ni
->allocated_size
,
2423 (long long)i_size_read(vol
->mft_ino
),
2424 (long long)mft_ni
->initialized_size
);
2426 read_unlock_irqrestore(&mft_ni
->size_lock
, flags
);
2428 * Extend mft data initialized size (and data size of course) to reach
2429 * the allocated mft record, formatting the mft records allong the way.
2430 * Note: We only modify the ntfs_inode structure as that is all that is
2431 * needed by ntfs_mft_record_format(). We will update the attribute
2432 * record itself in one fell swoop later on.
2434 write_lock_irqsave(&mft_ni
->size_lock
, flags
);
2435 old_data_initialized
= mft_ni
->initialized_size
;
2436 old_data_size
= vol
->mft_ino
->i_size
;
2437 while (ll
> mft_ni
->initialized_size
) {
2438 s64 new_initialized_size
, mft_no
;
2440 new_initialized_size
= mft_ni
->initialized_size
+
2441 vol
->mft_record_size
;
2442 mft_no
= mft_ni
->initialized_size
>> vol
->mft_record_size_bits
;
2443 if (new_initialized_size
> i_size_read(vol
->mft_ino
))
2444 i_size_write(vol
->mft_ino
, new_initialized_size
);
2445 write_unlock_irqrestore(&mft_ni
->size_lock
, flags
);
2446 ntfs_debug("Initializing mft record 0x%llx.",
2448 err
= ntfs_mft_record_format(vol
, mft_no
);
2449 if (unlikely(err
)) {
2450 ntfs_error(vol
->sb
, "Failed to format mft record.");
2451 goto undo_data_init
;
2453 write_lock_irqsave(&mft_ni
->size_lock
, flags
);
2454 mft_ni
->initialized_size
= new_initialized_size
;
2456 write_unlock_irqrestore(&mft_ni
->size_lock
, flags
);
2457 record_formatted
= true;
2458 /* Update the mft data attribute record to reflect the new sizes. */
2459 m
= map_mft_record(mft_ni
);
2461 ntfs_error(vol
->sb
, "Failed to map mft record.");
2463 goto undo_data_init
;
2465 ctx
= ntfs_attr_get_search_ctx(mft_ni
, m
);
2466 if (unlikely(!ctx
)) {
2467 ntfs_error(vol
->sb
, "Failed to get search context.");
2469 unmap_mft_record(mft_ni
);
2470 goto undo_data_init
;
2472 err
= ntfs_attr_lookup(mft_ni
->type
, mft_ni
->name
, mft_ni
->name_len
,
2473 CASE_SENSITIVE
, 0, NULL
, 0, ctx
);
2474 if (unlikely(err
)) {
2475 ntfs_error(vol
->sb
, "Failed to find first attribute extent of "
2476 "mft data attribute.");
2477 ntfs_attr_put_search_ctx(ctx
);
2478 unmap_mft_record(mft_ni
);
2479 goto undo_data_init
;
2482 read_lock_irqsave(&mft_ni
->size_lock
, flags
);
2483 a
->data
.non_resident
.initialized_size
=
2484 cpu_to_sle64(mft_ni
->initialized_size
);
2485 a
->data
.non_resident
.data_size
=
2486 cpu_to_sle64(i_size_read(vol
->mft_ino
));
2487 read_unlock_irqrestore(&mft_ni
->size_lock
, flags
);
2488 /* Ensure the changes make it to disk. */
2489 flush_dcache_mft_record_page(ctx
->ntfs_ino
);
2490 mark_mft_record_dirty(ctx
->ntfs_ino
);
2491 ntfs_attr_put_search_ctx(ctx
);
2492 unmap_mft_record(mft_ni
);
2493 read_lock_irqsave(&mft_ni
->size_lock
, flags
);
2494 ntfs_debug("Status of mft data after mft record initialization: "
2495 "allocated_size 0x%llx, data_size 0x%llx, "
2496 "initialized_size 0x%llx.",
2497 (long long)mft_ni
->allocated_size
,
2498 (long long)i_size_read(vol
->mft_ino
),
2499 (long long)mft_ni
->initialized_size
);
2500 BUG_ON(i_size_read(vol
->mft_ino
) > mft_ni
->allocated_size
);
2501 BUG_ON(mft_ni
->initialized_size
> i_size_read(vol
->mft_ino
));
2502 read_unlock_irqrestore(&mft_ni
->size_lock
, flags
);
2503 mft_rec_already_initialized
:
2505 * We can finally drop the mft bitmap lock as the mft data attribute
2506 * has been fully updated. The only disparity left is that the
2507 * allocated mft record still needs to be marked as in use to match the
2508 * set bit in the mft bitmap but this is actually not a problem since
2509 * this mft record is not referenced from anywhere yet and the fact
2510 * that it is allocated in the mft bitmap means that no-one will try to
2511 * allocate it either.
2513 up_write(&vol
->mftbmp_lock
);
2515 * We now have allocated and initialized the mft record. Calculate the
2516 * index of and the offset within the page cache page the record is in.
2518 index
= bit
<< vol
->mft_record_size_bits
>> PAGE_CACHE_SHIFT
;
2519 ofs
= (bit
<< vol
->mft_record_size_bits
) & ~PAGE_CACHE_MASK
;
2520 /* Read, map, and pin the page containing the mft record. */
2521 page
= ntfs_map_page(vol
->mft_ino
->i_mapping
, index
);
2522 if (unlikely(IS_ERR(page
))) {
2523 ntfs_error(vol
->sb
, "Failed to map page containing allocated "
2524 "mft record 0x%llx.", (long long)bit
);
2525 err
= PTR_ERR(page
);
2526 goto undo_mftbmp_alloc
;
2529 BUG_ON(!PageUptodate(page
));
2530 ClearPageUptodate(page
);
2531 m
= (MFT_RECORD
*)((u8
*)page_address(page
) + ofs
);
2532 /* If we just formatted the mft record no need to do it again. */
2533 if (!record_formatted
) {
2534 /* Sanity check that the mft record is really not in use. */
2535 if (ntfs_is_file_record(m
->magic
) &&
2536 (m
->flags
& MFT_RECORD_IN_USE
)) {
2537 ntfs_error(vol
->sb
, "Mft record 0x%llx was marked "
2538 "free in mft bitmap but is marked "
2539 "used itself. Corrupt filesystem. "
2540 "Unmount and run chkdsk.",
2543 SetPageUptodate(page
);
2545 ntfs_unmap_page(page
);
2547 goto undo_mftbmp_alloc
;
2550 * We need to (re-)format the mft record, preserving the
2551 * sequence number if it is not zero as well as the update
2552 * sequence number if it is not zero or -1 (0xffff). This
2553 * means we do not need to care whether or not something went
2554 * wrong with the previous mft record.
2556 seq_no
= m
->sequence_number
;
2557 usn
= *(le16
*)((u8
*)m
+ le16_to_cpu(m
->usa_ofs
));
2558 err
= ntfs_mft_record_layout(vol
, bit
, m
);
2559 if (unlikely(err
)) {
2560 ntfs_error(vol
->sb
, "Failed to layout allocated mft "
2561 "record 0x%llx.", (long long)bit
);
2562 SetPageUptodate(page
);
2564 ntfs_unmap_page(page
);
2565 goto undo_mftbmp_alloc
;
2568 m
->sequence_number
= seq_no
;
2569 if (usn
&& le16_to_cpu(usn
) != 0xffff)
2570 *(le16
*)((u8
*)m
+ le16_to_cpu(m
->usa_ofs
)) = usn
;
2572 /* Set the mft record itself in use. */
2573 m
->flags
|= MFT_RECORD_IN_USE
;
2575 m
->flags
|= MFT_RECORD_IS_DIRECTORY
;
2576 flush_dcache_page(page
);
2577 SetPageUptodate(page
);
2580 * Setup the base mft record in the extent mft record. This
2581 * completes initialization of the allocated extent mft record
2582 * and we can simply use it with map_extent_mft_record().
2584 m
->base_mft_record
= MK_LE_MREF(base_ni
->mft_no
,
2587 * Allocate an extent inode structure for the new mft record,
2588 * attach it to the base inode @base_ni and map, pin, and lock
2589 * its, i.e. the allocated, mft record.
2591 m
= map_extent_mft_record(base_ni
, bit
, &ni
);
2593 ntfs_error(vol
->sb
, "Failed to map allocated extent "
2594 "mft record 0x%llx.", (long long)bit
);
2596 /* Set the mft record itself not in use. */
2597 m
->flags
&= cpu_to_le16(
2598 ~le16_to_cpu(MFT_RECORD_IN_USE
));
2599 flush_dcache_page(page
);
2600 /* Make sure the mft record is written out to disk. */
2601 mark_ntfs_record_dirty(page
, ofs
);
2603 ntfs_unmap_page(page
);
2604 goto undo_mftbmp_alloc
;
2607 * Make sure the allocated mft record is written out to disk.
2608 * No need to set the inode dirty because the caller is going
2609 * to do that anyway after finishing with the new extent mft
2610 * record (e.g. at a minimum a new attribute will be added to
2613 mark_ntfs_record_dirty(page
, ofs
);
2616 * Need to unmap the page since map_extent_mft_record() mapped
2617 * it as well so we have it mapped twice at the moment.
2619 ntfs_unmap_page(page
);
2622 * Allocate a new VFS inode and set it up. NOTE: @vi->i_nlink
2623 * is set to 1 but the mft record->link_count is 0. The caller
2624 * needs to bear this in mind.
2626 vi
= new_inode(vol
->sb
);
2627 if (unlikely(!vi
)) {
2629 /* Set the mft record itself not in use. */
2630 m
->flags
&= cpu_to_le16(
2631 ~le16_to_cpu(MFT_RECORD_IN_USE
));
2632 flush_dcache_page(page
);
2633 /* Make sure the mft record is written out to disk. */
2634 mark_ntfs_record_dirty(page
, ofs
);
2636 ntfs_unmap_page(page
);
2637 goto undo_mftbmp_alloc
;
2641 * This is for checking whether an inode has changed w.r.t. a
2642 * file so that the file can be updated if necessary (compare
2647 /* The owner and group come from the ntfs volume. */
2648 vi
->i_uid
= vol
->uid
;
2649 vi
->i_gid
= vol
->gid
;
2651 /* Initialize the ntfs specific part of @vi. */
2652 ntfs_init_big_inode(vi
);
2655 * Set the appropriate mode, attribute type, and name. For
2656 * directories, also setup the index values to the defaults.
2658 if (S_ISDIR(mode
)) {
2659 vi
->i_mode
= S_IFDIR
| S_IRWXUGO
;
2660 vi
->i_mode
&= ~vol
->dmask
;
2662 NInoSetMstProtected(ni
);
2663 ni
->type
= AT_INDEX_ALLOCATION
;
2667 ni
->itype
.index
.block_size
= 4096;
2668 ni
->itype
.index
.block_size_bits
= ntfs_ffs(4096) - 1;
2669 ni
->itype
.index
.collation_rule
= COLLATION_FILE_NAME
;
2670 if (vol
->cluster_size
<= ni
->itype
.index
.block_size
) {
2671 ni
->itype
.index
.vcn_size
= vol
->cluster_size
;
2672 ni
->itype
.index
.vcn_size_bits
=
2673 vol
->cluster_size_bits
;
2675 ni
->itype
.index
.vcn_size
= vol
->sector_size
;
2676 ni
->itype
.index
.vcn_size_bits
=
2677 vol
->sector_size_bits
;
2680 vi
->i_mode
= S_IFREG
| S_IRWXUGO
;
2681 vi
->i_mode
&= ~vol
->fmask
;
2688 vi
->i_mode
&= ~S_IWUGO
;
2690 /* Set the inode times to the current time. */
2691 vi
->i_atime
= vi
->i_mtime
= vi
->i_ctime
=
2692 current_fs_time(vi
->i_sb
);
2694 * Set the file size to 0, the ntfs inode sizes are set to 0 by
2695 * the call to ntfs_init_big_inode() below.
2700 /* Set the sequence number. */
2701 vi
->i_generation
= ni
->seq_no
= le16_to_cpu(m
->sequence_number
);
2703 * Manually map, pin, and lock the mft record as we already
2704 * have its page mapped and it is very easy to do.
2706 atomic_inc(&ni
->count
);
2707 mutex_lock(&ni
->mrec_lock
);
2711 * Make sure the allocated mft record is written out to disk.
2712 * NOTE: We do not set the ntfs inode dirty because this would
2713 * fail in ntfs_write_inode() because the inode does not have a
2714 * standard information attribute yet. Also, there is no need
2715 * to set the inode dirty because the caller is going to do
2716 * that anyway after finishing with the new mft record (e.g. at
2717 * a minimum some new attributes will be added to the mft
2720 mark_ntfs_record_dirty(page
, ofs
);
2723 /* Add the inode to the inode hash for the superblock. */
2724 insert_inode_hash(vi
);
2726 /* Update the default mft allocation position. */
2727 vol
->mft_data_pos
= bit
+ 1;
2730 * Return the opened, allocated inode of the allocated mft record as
2731 * well as the mapped, pinned, and locked mft record.
2733 ntfs_debug("Returning opened, allocated %sinode 0x%llx.",
2734 base_ni
? "extent " : "", (long long)bit
);
2738 write_lock_irqsave(&mft_ni
->size_lock
, flags
);
2739 mft_ni
->initialized_size
= old_data_initialized
;
2740 i_size_write(vol
->mft_ino
, old_data_size
);
2741 write_unlock_irqrestore(&mft_ni
->size_lock
, flags
);
2742 goto undo_mftbmp_alloc_nolock
;
2744 down_write(&vol
->mftbmp_lock
);
2745 undo_mftbmp_alloc_nolock
:
2746 if (ntfs_bitmap_clear_bit(vol
->mftbmp_ino
, bit
)) {
2747 ntfs_error(vol
->sb
, "Failed to clear bit in mft bitmap.%s", es
);
2750 up_write(&vol
->mftbmp_lock
);
2752 return ERR_PTR(err
);
2754 ntfs_warning(vol
->sb
, "Cannot allocate mft record because the maximum "
2755 "number of inodes (2^32) has already been reached.");
2756 up_write(&vol
->mftbmp_lock
);
2757 return ERR_PTR(-ENOSPC
);
2761 * ntfs_extent_mft_record_free - free an extent mft record on an ntfs volume
2762 * @ni: ntfs inode of the mapped extent mft record to free
2763 * @m: mapped extent mft record of the ntfs inode @ni
2765 * Free the mapped extent mft record @m of the extent ntfs inode @ni.
2767 * Note that this function unmaps the mft record and closes and destroys @ni
2768 * internally and hence you cannot use either @ni nor @m any more after this
2769 * function returns success.
2771 * On success return 0 and on error return -errno. @ni and @m are still valid
2772 * in this case and have not been freed.
2774 * For some errors an error message is displayed and the success code 0 is
2775 * returned and the volume is then left dirty on umount. This makes sense in
2776 * case we could not rollback the changes that were already done since the
2777 * caller no longer wants to reference this mft record so it does not matter to
2778 * the caller if something is wrong with it as long as it is properly detached
2779 * from the base inode.
2781 int ntfs_extent_mft_record_free(ntfs_inode
*ni
, MFT_RECORD
*m
)
2783 unsigned long mft_no
= ni
->mft_no
;
2784 ntfs_volume
*vol
= ni
->vol
;
2785 ntfs_inode
*base_ni
;
2786 ntfs_inode
**extent_nis
;
2791 BUG_ON(NInoAttr(ni
));
2792 BUG_ON(ni
->nr_extents
!= -1);
2794 mutex_lock(&ni
->extent_lock
);
2795 base_ni
= ni
->ext
.base_ntfs_ino
;
2796 mutex_unlock(&ni
->extent_lock
);
2798 BUG_ON(base_ni
->nr_extents
<= 0);
2800 ntfs_debug("Entering for extent inode 0x%lx, base inode 0x%lx.\n",
2801 mft_no
, base_ni
->mft_no
);
2803 mutex_lock(&base_ni
->extent_lock
);
2805 /* Make sure we are holding the only reference to the extent inode. */
2806 if (atomic_read(&ni
->count
) > 2) {
2807 ntfs_error(vol
->sb
, "Tried to free busy extent inode 0x%lx, "
2808 "not freeing.", base_ni
->mft_no
);
2809 mutex_unlock(&base_ni
->extent_lock
);
2813 /* Dissociate the ntfs inode from the base inode. */
2814 extent_nis
= base_ni
->ext
.extent_ntfs_inos
;
2816 for (i
= 0; i
< base_ni
->nr_extents
; i
++) {
2817 if (ni
!= extent_nis
[i
])
2820 base_ni
->nr_extents
--;
2821 memmove(extent_nis
, extent_nis
+ 1, (base_ni
->nr_extents
- i
) *
2822 sizeof(ntfs_inode
*));
2827 mutex_unlock(&base_ni
->extent_lock
);
2829 if (unlikely(err
)) {
2830 ntfs_error(vol
->sb
, "Extent inode 0x%lx is not attached to "
2831 "its base inode 0x%lx.", mft_no
,
2837 * The extent inode is no longer attached to the base inode so no one
2838 * can get a reference to it any more.
2841 /* Mark the mft record as not in use. */
2842 m
->flags
&= const_cpu_to_le16(~const_le16_to_cpu(MFT_RECORD_IN_USE
));
2844 /* Increment the sequence number, skipping zero, if it is not zero. */
2845 old_seq_no
= m
->sequence_number
;
2846 seq_no
= le16_to_cpu(old_seq_no
);
2847 if (seq_no
== 0xffff)
2851 m
->sequence_number
= cpu_to_le16(seq_no
);
2854 * Set the ntfs inode dirty and write it out. We do not need to worry
2855 * about the base inode here since whatever caused the extent mft
2856 * record to be freed is guaranteed to do it already.
2859 err
= write_mft_record(ni
, m
, 0);
2860 if (unlikely(err
)) {
2861 ntfs_error(vol
->sb
, "Failed to write mft record 0x%lx, not "
2862 "freeing.", mft_no
);
2866 /* Unmap and throw away the now freed extent inode. */
2867 unmap_extent_mft_record(ni
);
2868 ntfs_clear_extent_inode(ni
);
2870 /* Clear the bit in the $MFT/$BITMAP corresponding to this record. */
2871 down_write(&vol
->mftbmp_lock
);
2872 err
= ntfs_bitmap_clear_bit(vol
->mftbmp_ino
, mft_no
);
2873 up_write(&vol
->mftbmp_lock
);
2874 if (unlikely(err
)) {
2876 * The extent inode is gone but we failed to deallocate it in
2877 * the mft bitmap. Just emit a warning and leave the volume
2880 ntfs_error(vol
->sb
, "Failed to clear bit in mft bitmap.%s", es
);
2885 /* Rollback what we did... */
2886 mutex_lock(&base_ni
->extent_lock
);
2887 extent_nis
= base_ni
->ext
.extent_ntfs_inos
;
2888 if (!(base_ni
->nr_extents
& 3)) {
2889 int new_size
= (base_ni
->nr_extents
+ 4) * sizeof(ntfs_inode
*);
2891 extent_nis
= kmalloc(new_size
, GFP_NOFS
);
2892 if (unlikely(!extent_nis
)) {
2893 ntfs_error(vol
->sb
, "Failed to allocate internal "
2894 "buffer during rollback.%s", es
);
2895 mutex_unlock(&base_ni
->extent_lock
);
2897 goto rollback_error
;
2899 if (base_ni
->nr_extents
) {
2900 BUG_ON(!base_ni
->ext
.extent_ntfs_inos
);
2901 memcpy(extent_nis
, base_ni
->ext
.extent_ntfs_inos
,
2902 new_size
- 4 * sizeof(ntfs_inode
*));
2903 kfree(base_ni
->ext
.extent_ntfs_inos
);
2905 base_ni
->ext
.extent_ntfs_inos
= extent_nis
;
2907 m
->flags
|= MFT_RECORD_IN_USE
;
2908 m
->sequence_number
= old_seq_no
;
2909 extent_nis
[base_ni
->nr_extents
++] = ni
;
2910 mutex_unlock(&base_ni
->extent_lock
);
2911 mark_mft_record_dirty(ni
);
2914 #endif /* NTFS_RW */