Lines Matching +full:record +full:- +full:size
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * mft.c - NTFS kernel mft record operations. Part of the Linux-NTFS project.
5 * Copyright (c) 2001-2012 Anton Altaparmakov and Tuxera Inc.
27 * map_mft_record_page - map the page in which a specific mft record resides
28 * @ni: ntfs inode whose mft record page to map
30 * This maps the page in which the mft record of the ntfs inode @ni is situated
31 * and returns a pointer to the mft record within the mapped page.
39 ntfs_volume *vol = ni->vol; in map_mft_record_page()
40 struct inode *mft_vi = vol->mft_ino; in map_mft_record_page()
45 BUG_ON(ni->page); in map_mft_record_page()
48 * page of the wanted mft record. FIXME: We need to check for in map_mft_record_page()
52 index = (u64)ni->mft_no << vol->mft_record_size_bits >> in map_mft_record_page()
54 ofs = (ni->mft_no << vol->mft_record_size_bits) & ~PAGE_MASK; in map_mft_record_page()
60 /* If the wanted index is out of bounds the mft record doesn't exist. */ in map_mft_record_page()
63 vol->mft_record_size) { in map_mft_record_page()
64 page = ERR_PTR(-ENOENT); in map_mft_record_page()
65 ntfs_error(vol->sb, "Attempt to read mft record 0x%lx, " in map_mft_record_page()
68 "driver.", ni->mft_no); in map_mft_record_page()
73 page = ntfs_map_page(mft_vi->i_mapping, index); in map_mft_record_page()
78 ni->page = page; in map_mft_record_page()
79 ni->page_ofs = ofs; in map_mft_record_page()
82 ntfs_error(vol->sb, "Mft record 0x%lx is corrupt. " in map_mft_record_page()
83 "Run chkdsk.", ni->mft_no); in map_mft_record_page()
85 page = ERR_PTR(-EIO); in map_mft_record_page()
89 ni->page = NULL; in map_mft_record_page()
90 ni->page_ofs = 0; in map_mft_record_page()
95 * map_mft_record - map, pin and lock an mft record
96 * @ni: ntfs inode whose MFT record to map
101 * The page of the record is mapped using map_mft_record_page() before being
105 * record (it in turn calls read_cache_page() which reads it in from disk if
111 * and the post-read mst fixups on each mft record in the page have been
133 * The mft record is now ours and we return a pointer to it. You need to check
137 * NOTE: Caller is responsible for setting the mft record dirty before calling
139 * modified the mft record...
141 * A: No, the inode ones mean we want to change the mft record, not we want to
148 ntfs_debug("Entering for mft_no 0x%lx.", ni->mft_no); in map_mft_record()
151 atomic_inc(&ni->count); in map_mft_record()
153 /* Serialize access to this mft record. */ in map_mft_record()
154 mutex_lock(&ni->mrec_lock); in map_mft_record()
160 mutex_unlock(&ni->mrec_lock); in map_mft_record()
161 atomic_dec(&ni->count); in map_mft_record()
162 ntfs_error(ni->vol->sb, "Failed with error code %lu.", -PTR_ERR(m)); in map_mft_record()
167 * unmap_mft_record_page - unmap the page in which a specific mft record resides
168 * @ni: ntfs inode whose mft record page to unmap
170 * This unmaps the page in which the mft record of the ntfs inode @ni is
182 BUG_ON(!ni->page); in unmap_mft_record_page()
185 ntfs_unmap_page(ni->page); in unmap_mft_record_page()
186 ni->page = NULL; in unmap_mft_record_page()
187 ni->page_ofs = 0; in unmap_mft_record_page()
192 * unmap_mft_record - release a mapped mft record
193 * @ni: ntfs inode whose MFT record to unmap
196 * record and releases it for others to get hold of. We also release the ntfs
199 * NOTE: If caller has modified the mft record, it is imperative to set the mft
200 * record dirty BEFORE calling unmap_mft_record().
204 struct page *page = ni->page; in unmap_mft_record()
208 ntfs_debug("Entering for mft_no 0x%lx.", ni->mft_no); in unmap_mft_record()
211 mutex_unlock(&ni->mrec_lock); in unmap_mft_record()
212 atomic_dec(&ni->count); in unmap_mft_record()
216 * caller in the non-extent, yet pure ntfs inode case, to do the actual in unmap_mft_record()
223 * map_extent_mft_record - load an extent inode and attach it to its base
228 * Load the extent mft record @mref and attach it to its base inode @base_ni.
229 * Return the mapped extent mft record if IS_ERR(result) is false. Otherwise
246 ntfs_debug("Mapping extent mft record 0x%lx (base mft record 0x%lx).", in map_extent_mft_record()
247 mft_no, base_ni->mft_no); in map_extent_mft_record()
249 atomic_inc(&base_ni->count); in map_extent_mft_record()
255 mutex_lock(&base_ni->extent_lock); in map_extent_mft_record()
256 if (base_ni->nr_extents > 0) { in map_extent_mft_record()
257 extent_nis = base_ni->ext.extent_ntfs_inos; in map_extent_mft_record()
258 for (i = 0; i < base_ni->nr_extents; i++) { in map_extent_mft_record()
259 if (mft_no != extent_nis[i]->mft_no) in map_extent_mft_record()
263 atomic_inc(&ni->count); in map_extent_mft_record()
268 mutex_unlock(&base_ni->extent_lock); in map_extent_mft_record()
269 atomic_dec(&base_ni->count); in map_extent_mft_record()
270 /* We found the record; just have to map and return it. */ in map_extent_mft_record()
273 atomic_dec(&ni->count); in map_extent_mft_record()
276 if (likely(le16_to_cpu(m->sequence_number) == seq_no)) { in map_extent_mft_record()
282 ntfs_error(base_ni->vol->sb, "Found stale extent mft " in map_extent_mft_record()
285 return ERR_PTR(-EIO); in map_extent_mft_record()
288 ntfs_error(base_ni->vol->sb, "Failed to map extent " in map_extent_mft_record()
289 "mft record, error code %ld.", -PTR_ERR(m)); in map_extent_mft_record()
292 /* Record wasn't there. Get a new ntfs inode and initialize it. */ in map_extent_mft_record()
293 ni = ntfs_new_extent_inode(base_ni->vol->sb, mft_no); in map_extent_mft_record()
295 mutex_unlock(&base_ni->extent_lock); in map_extent_mft_record()
296 atomic_dec(&base_ni->count); in map_extent_mft_record()
297 return ERR_PTR(-ENOMEM); in map_extent_mft_record()
299 ni->vol = base_ni->vol; in map_extent_mft_record()
300 ni->seq_no = seq_no; in map_extent_mft_record()
301 ni->nr_extents = -1; in map_extent_mft_record()
302 ni->ext.base_ntfs_ino = base_ni; in map_extent_mft_record()
303 /* Now map the record. */ in map_extent_mft_record()
306 mutex_unlock(&base_ni->extent_lock); in map_extent_mft_record()
307 atomic_dec(&base_ni->count); in map_extent_mft_record()
312 if (seq_no && (le16_to_cpu(m->sequence_number) != seq_no)) { in map_extent_mft_record()
313 ntfs_error(base_ni->vol->sb, "Found stale extent mft " in map_extent_mft_record()
316 m = ERR_PTR(-EIO); in map_extent_mft_record()
320 if (!(base_ni->nr_extents & 3)) { in map_extent_mft_record()
322 int new_size = (base_ni->nr_extents + 4) * sizeof(ntfs_inode *); in map_extent_mft_record()
326 ntfs_error(base_ni->vol->sb, "Failed to allocate " in map_extent_mft_record()
329 m = ERR_PTR(-ENOMEM); in map_extent_mft_record()
332 if (base_ni->nr_extents) { in map_extent_mft_record()
333 BUG_ON(!base_ni->ext.extent_ntfs_inos); in map_extent_mft_record()
334 memcpy(tmp, base_ni->ext.extent_ntfs_inos, new_size - in map_extent_mft_record()
336 kfree(base_ni->ext.extent_ntfs_inos); in map_extent_mft_record()
338 base_ni->ext.extent_ntfs_inos = tmp; in map_extent_mft_record()
340 base_ni->ext.extent_ntfs_inos[base_ni->nr_extents++] = ni; in map_extent_mft_record()
341 mutex_unlock(&base_ni->extent_lock); in map_extent_mft_record()
342 atomic_dec(&base_ni->count); in map_extent_mft_record()
348 mutex_unlock(&base_ni->extent_lock); in map_extent_mft_record()
349 atomic_dec(&base_ni->count); in map_extent_mft_record()
362 * __mark_mft_record_dirty - set the mft record and the page containing it dirty
363 * @ni: ntfs inode describing the mapped mft record
367 * Set the mapped (extent) mft record of the (base or extent) ntfs inode @ni,
368 * as well as the page containing the mft record, dirty. Also, mark the base
369 * vfs inode dirty. This ensures that any changes to the mft record are
377 * other hand, is not sufficient, because ->write_inode needs to be called even
388 ntfs_debug("Entering for inode 0x%lx.", ni->mft_no); in __mark_mft_record_dirty()
390 mark_ntfs_record_dirty(ni->page, ni->page_ofs); in __mark_mft_record_dirty()
392 mutex_lock(&ni->extent_lock); in __mark_mft_record_dirty()
393 if (likely(ni->nr_extents >= 0)) in __mark_mft_record_dirty()
396 base_ni = ni->ext.base_ntfs_ino; in __mark_mft_record_dirty()
397 mutex_unlock(&ni->extent_lock); in __mark_mft_record_dirty()
402 "linux-ntfs-dev@lists.sourceforge.net and say that you saw "
406 * ntfs_sync_mft_mirror_umount - synchronise an mft record to the mft mirror
407 * @vol: ntfs volume on which the mft record to synchronize resides
408 * @mft_no: mft record number of mft record to synchronize
409 * @m: mapped, mst protected (extent) mft record to synchronize
411 * Write the mapped, mst protected (extent) mft record @m with mft record
419 * On success return 0. On error return -errno.
430 BUG_ON(vol->mftmirr_ino); in ntfs_sync_mft_mirror_umount()
431 ntfs_error(vol->sb, "Umount time mft mirror syncing is not " in ntfs_sync_mft_mirror_umount()
433 return -EOPNOTSUPP; in ntfs_sync_mft_mirror_umount()
437 * ntfs_sync_mft_mirror - synchronize an mft record to the mft mirror
438 * @vol: ntfs volume on which the mft record to synchronize resides
439 * @mft_no: mft record number of mft record to synchronize
440 * @m: mapped, mst protected (extent) mft record to synchronize
443 * Write the mapped, mst protected (extent) mft record @m with mft record
446 * On success return 0. On error return -errno and set the volume errors flag
452 * schedule i/o via ->writepage or do it via kntfsd or whatever.
458 unsigned int blocksize = vol->sb->s_blocksize; in ntfs_sync_mft_mirror()
459 int max_bhs = vol->mft_record_size / blocksize; in ntfs_sync_mft_mirror()
466 unsigned char blocksize_bits = vol->sb->s_blocksize_bits; in ntfs_sync_mft_mirror()
471 return -EINVAL; in ntfs_sync_mft_mirror()
472 if (unlikely(!vol->mftmirr_ino)) { in ntfs_sync_mft_mirror()
479 /* Get the page containing the mirror copy of the mft record @m. */ in ntfs_sync_mft_mirror()
480 page = ntfs_map_page(vol->mftmirr_ino->i_mapping, mft_no >> in ntfs_sync_mft_mirror()
481 (PAGE_SHIFT - vol->mft_record_size_bits)); in ntfs_sync_mft_mirror()
483 ntfs_error(vol->sb, "Failed to map mft mirror page."); in ntfs_sync_mft_mirror()
490 /* Offset of the mft mirror record inside the page. */ in ntfs_sync_mft_mirror()
491 page_ofs = (mft_no << vol->mft_record_size_bits) & ~PAGE_MASK; in ntfs_sync_mft_mirror()
492 /* The address in the page of the mirror copy of the mft record @m. */ in ntfs_sync_mft_mirror()
494 /* Copy the mst protected mft record to the mirror. */ in ntfs_sync_mft_mirror()
495 memcpy(kmirr, m, vol->mft_record_size); in ntfs_sync_mft_mirror()
504 bh = bh->b_this_page; in ntfs_sync_mft_mirror()
506 tail->b_this_page = head; in ntfs_sync_mft_mirror()
514 m_start = kmirr - (u8*)page_address(page); in ntfs_sync_mft_mirror()
515 m_end = m_start + vol->mft_record_size; in ntfs_sync_mft_mirror()
518 /* If the buffer is outside the mft record, skip it. */ in ntfs_sync_mft_mirror()
529 bh->b_bdev = vol->sb->s_bdev; in ntfs_sync_mft_mirror()
531 vcn = ((VCN)mft_no << vol->mft_record_size_bits) + in ntfs_sync_mft_mirror()
532 (block_start - m_start); in ntfs_sync_mft_mirror()
533 vcn_ofs = vcn & vol->cluster_size_mask; in ntfs_sync_mft_mirror()
534 vcn >>= vol->cluster_size_bits; in ntfs_sync_mft_mirror()
536 down_read(&NTFS_I(vol->mftmirr_ino)-> in ntfs_sync_mft_mirror()
538 rl = NTFS_I(vol->mftmirr_ino)->runlist.rl; in ntfs_sync_mft_mirror()
546 while (rl->length && rl[1].vcn <= vcn) in ntfs_sync_mft_mirror()
552 bh->b_blocknr = ((lcn << in ntfs_sync_mft_mirror()
553 vol->cluster_size_bits) + in ntfs_sync_mft_mirror()
557 bh->b_blocknr = -1; in ntfs_sync_mft_mirror()
558 ntfs_error(vol->sb, "Cannot write mft mirror " in ntfs_sync_mft_mirror()
559 "record 0x%lx because its " in ntfs_sync_mft_mirror()
564 err = -EIO; in ntfs_sync_mft_mirror()
572 } while (block_start = block_end, (bh = bh->b_this_page) != head); in ntfs_sync_mft_mirror()
574 up_read(&NTFS_I(vol->mftmirr_ino)->runlist.lock); in ntfs_sync_mft_mirror()
585 tbh->b_end_io = end_buffer_write_sync; in ntfs_sync_mft_mirror()
594 err = -EIO; in ntfs_sync_mft_mirror()
617 ntfs_error(vol->sb, "I/O error while writing mft mirror " in ntfs_sync_mft_mirror()
618 "record 0x%lx!", mft_no); in ntfs_sync_mft_mirror()
620 ntfs_error(vol->sb, "Failed to synchronize $MFTMirr (error " in ntfs_sync_mft_mirror()
623 "after umounting to correct this.", -err); in ntfs_sync_mft_mirror()
630 * write_mft_record_nolock - write out a mapped (extent) mft record
631 * @ni: ntfs inode describing the mapped (extent) mft record
632 * @m: mapped (extent) mft record to write
635 * Write the mapped (extent) mft record @m described by the (regular or extent)
636 * ntfs inode @ni to backing store. If the mft record @m has a counterpart in
639 * We only write the mft record if the ntfs inode @ni is dirty and the first
640 * buffer belonging to its mft record is dirty, too. We ignore the dirty state
644 * On success, clean the mft record and return 0. On error, leave the mft
645 * record dirty and return -errno.
648 * However, if the mft record has a counterpart in the mft mirror and @sync is
649 * true, we write the mft record, wait for i/o completion, and only then write
651 * or the mft mirror will contain a self-consistent mft record @m. If @sync is
654 * up with a self-consistent mft record in the case of a crash but if you asked
658 * schedule i/o via ->writepage or do it via kntfsd or whatever.
662 ntfs_volume *vol = ni->vol; in write_mft_record_nolock()
663 struct page *page = ni->page; in write_mft_record_nolock()
664 unsigned int blocksize = vol->sb->s_blocksize; in write_mft_record_nolock()
665 unsigned char blocksize_bits = vol->sb->s_blocksize_bits; in write_mft_record_nolock()
666 int max_bhs = vol->mft_record_size / blocksize; in write_mft_record_nolock()
673 ntfs_debug("Entering for inode 0x%lx.", ni->mft_no); in write_mft_record_nolock()
678 err = -EINVAL; in write_mft_record_nolock()
685 * for the mft record @m and the page it is in. in write_mft_record_nolock()
694 m_start = ni->page_ofs; in write_mft_record_nolock()
695 m_end = m_start + vol->mft_record_size; in write_mft_record_nolock()
698 /* If the buffer is outside the mft record, skip it. */ in write_mft_record_nolock()
704 * If this block is not the first one in the record, we ignore in write_mft_record_nolock()
709 /* This block is the first one in the record. */ in write_mft_record_nolock()
722 bh->b_bdev = vol->sb->s_bdev; in write_mft_record_nolock()
724 vcn = ((VCN)ni->mft_no << vol->mft_record_size_bits) + in write_mft_record_nolock()
725 (block_start - m_start); in write_mft_record_nolock()
726 vcn_ofs = vcn & vol->cluster_size_mask; in write_mft_record_nolock()
727 vcn >>= vol->cluster_size_bits; in write_mft_record_nolock()
729 down_read(&NTFS_I(vol->mft_ino)->runlist.lock); in write_mft_record_nolock()
730 rl = NTFS_I(vol->mft_ino)->runlist.rl; in write_mft_record_nolock()
734 while (rl->length && rl[1].vcn <= vcn) in write_mft_record_nolock()
740 bh->b_blocknr = ((lcn << in write_mft_record_nolock()
741 vol->cluster_size_bits) + in write_mft_record_nolock()
745 bh->b_blocknr = -1; in write_mft_record_nolock()
746 ntfs_error(vol->sb, "Cannot write mft record " in write_mft_record_nolock()
750 ni->mft_no, (long long)lcn); in write_mft_record_nolock()
751 err = -EIO; in write_mft_record_nolock()
759 } while (block_start = block_end, (bh = bh->b_this_page) != head); in write_mft_record_nolock()
761 up_read(&NTFS_I(vol->mft_ino)->runlist.lock); in write_mft_record_nolock()
767 err = pre_write_mst_fixup((NTFS_RECORD*)m, vol->mft_record_size); in write_mft_record_nolock()
769 ntfs_error(vol->sb, "Failed to apply mst fixups!"); in write_mft_record_nolock()
782 tbh->b_end_io = end_buffer_write_sync; in write_mft_record_nolock()
786 if (!sync && ni->mft_no < vol->mftmirr_size) in write_mft_record_nolock()
787 ntfs_sync_mft_mirror(vol, ni->mft_no, m, sync); in write_mft_record_nolock()
794 err = -EIO; in write_mft_record_nolock()
804 if (sync && ni->mft_no < vol->mftmirr_size) in write_mft_record_nolock()
805 ntfs_sync_mft_mirror(vol, ni->mft_no, m, sync); in write_mft_record_nolock()
811 ntfs_error(vol->sb, "I/O error while writing mft record " in write_mft_record_nolock()
814 ni->mft_no); in write_mft_record_nolock()
828 * happens. ->clear_inode() will still be invoked so all extent inodes in write_mft_record_nolock()
831 if (err == -ENOMEM) { in write_mft_record_nolock()
832 ntfs_error(vol->sb, "Not enough memory to write mft record. " in write_mft_record_nolock()
842 * ntfs_may_write_mft_record - check if an mft record may be written out
843 * @vol: [IN] ntfs volume on which the mft record to check resides
844 * @mft_no: [IN] mft record number of the mft record to check
845 * @m: [IN] mapped mft record to check
848 * Check if the mapped (base or extent) mft record @m with mft record number
850 * and possible the ntfs inode of the mft record is locked and the base vfs
855 * Return 'true' if the mft record may be written out and 'false' if not.
861 * record in read_cache_page() until we are finished with the write out.
865 * If the inode is found in icache we know the mft record must be a base mft
866 * record. If it is dirty, we do not write it and return 'false' as the vfs
868 * cause the base mft record to be redirtied and written out again. (We know
869 * the access time update will modify the base mft record because Windows
871 * mft record.)
873 * If the inode is in icache and not dirty, we attempt to lock the mft record
875 * record and we return 'false'.
877 * If we manage to obtain the lock we have exclusive access to the mft record,
878 * which also allows us safe writeout of the mft record. We then set
881 * Note we cannot just lock the mft record and sleep while waiting for the lock
882 * because this would deadlock due to lock reversal (normally the mft record is
884 * when we try to lock the mft record).
888 * If the mft record is not a FILE record or it is a base mft record, we can
891 * We now know the mft record is an extent mft record. We check if the inode
892 * corresponding to its base mft record is in icache and obtain a reference to
895 * We now have the base inode for the extent mft record. We check if it has an
896 * ntfs inode for the extent mft record attached and if not it is safe to write
897 * the extent mft record and we return 'true'.
899 * The ntfs inode for the extent mft record is attached to the base inode so we
900 * attempt to lock the extent mft record and if we find the lock was already
901 * taken, it is not safe to write the extent mft record and we return 'false'.
904 * record, which also allows us safe writeout of the extent mft record. We
905 * set the ntfs inode of the extent mft record clean and then set @locked_ni to
917 * appear if the mft record is reused for a new inode before it got written
923 struct super_block *sb = vol->sb; in ntfs_may_write_mft_record()
924 struct inode *mft_vi = vol->mft_ino; in ntfs_may_write_mft_record()
937 * Check if the inode corresponding to this mft record is in the VFS in ntfs_may_write_mft_record()
968 atomic_inc(&ni->count); in ntfs_may_write_mft_record()
969 /* If the inode is dirty, do not write this record. */ in ntfs_may_write_mft_record()
973 atomic_dec(&ni->count); in ntfs_may_write_mft_record()
978 /* The inode is not dirty, try to take the mft record lock. */ in ntfs_may_write_mft_record()
979 if (unlikely(!mutex_trylock(&ni->mrec_lock))) { in ntfs_may_write_mft_record()
980 ntfs_debug("Mft record 0x%lx is already locked, do " in ntfs_may_write_mft_record()
982 atomic_dec(&ni->count); in ntfs_may_write_mft_record()
986 ntfs_debug("Managed to lock mft record 0x%lx, write it.", in ntfs_may_write_mft_record()
989 * The write has to occur while we hold the mft record lock so in ntfs_may_write_mft_record()
997 /* Write the record if it is not a mft record (type "FILE"). */ in ntfs_may_write_mft_record()
998 if (!ntfs_is_mft_record(m->magic)) { in ntfs_may_write_mft_record()
999 ntfs_debug("Mft record 0x%lx is not a FILE record, write it.", in ntfs_may_write_mft_record()
1003 /* Write the mft record if it is a base inode. */ in ntfs_may_write_mft_record()
1004 if (!m->base_mft_record) { in ntfs_may_write_mft_record()
1005 ntfs_debug("Mft record 0x%lx is a base record, write it.", in ntfs_may_write_mft_record()
1010 * This is an extent mft record. Check if the inode corresponding to in ntfs_may_write_mft_record()
1011 * its base mft record is in icache and obtain a reference to it if it in ntfs_may_write_mft_record()
1014 na.mft_no = MREF_LE(m->base_mft_record); in ntfs_may_write_mft_record()
1015 ntfs_debug("Mft record 0x%lx is an extent record. Looking for base " in ntfs_may_write_mft_record()
1027 * record. in ntfs_may_write_mft_record()
1030 "extent record.", na.mft_no); in ntfs_may_write_mft_record()
1036 * corresponding to this extent mft record attached. in ntfs_may_write_mft_record()
1039 mutex_lock(&ni->extent_lock); in ntfs_may_write_mft_record()
1040 if (ni->nr_extents <= 0) { in ntfs_may_write_mft_record()
1043 * extent mft record. in ntfs_may_write_mft_record()
1045 mutex_unlock(&ni->extent_lock); in ntfs_may_write_mft_record()
1048 "write the extent record.", na.mft_no); in ntfs_may_write_mft_record()
1052 extent_nis = ni->ext.extent_ntfs_inos; in ntfs_may_write_mft_record()
1053 for (eni = NULL, i = 0; i < ni->nr_extents; ++i) { in ntfs_may_write_mft_record()
1054 if (mft_no == extent_nis[i]->mft_no) { in ntfs_may_write_mft_record()
1057 * mft record. in ntfs_may_write_mft_record()
1065 * extent mft record. in ntfs_may_write_mft_record()
1068 mutex_unlock(&ni->extent_lock); in ntfs_may_write_mft_record()
1071 "inode 0x%lx, write the extent record.", in ntfs_may_write_mft_record()
1078 atomic_inc(&eni->count); in ntfs_may_write_mft_record()
1079 mutex_unlock(&ni->extent_lock); in ntfs_may_write_mft_record()
1081 * Found the extent inode coresponding to this extent mft record. in ntfs_may_write_mft_record()
1082 * Try to take the mft record lock. in ntfs_may_write_mft_record()
1084 if (unlikely(!mutex_trylock(&eni->mrec_lock))) { in ntfs_may_write_mft_record()
1085 atomic_dec(&eni->count); in ntfs_may_write_mft_record()
1087 ntfs_debug("Extent mft record 0x%lx is already locked, do " in ntfs_may_write_mft_record()
1091 ntfs_debug("Managed to lock extent mft record 0x%lx, write it.", in ntfs_may_write_mft_record()
1097 * The write has to occur while we hold the mft record lock so return in ntfs_may_write_mft_record()
1108 * ntfs_mft_bitmap_find_and_alloc_free_rec_nolock - see name
1109 * @vol: volume on which to search for a free mft record
1110 * @base_ni: open base inode if allocating an extent mft record or NULL
1112 * Search for a free mft record in the mft bitmap attribute on the ntfs volume
1117 * If @base_ni is not NULL start the search at the mft record after the base
1118 * mft record @base_ni.
1120 * Return the free mft record on success and -errno on error. An error code of
1121 * -ENOSPC means that there are no free mft records in the currently
1124 * Locking: Caller must hold vol->mftbmp_lock for writing.
1134 unsigned int page_ofs, size; in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock() local
1137 ntfs_debug("Searching for free mft record in the currently " in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1139 mftbmp_mapping = vol->mftbmp_ino->i_mapping; in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1144 read_lock_irqsave(&NTFS_I(vol->mft_ino)->size_lock, flags); in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1145 pass_end = NTFS_I(vol->mft_ino)->allocated_size >> in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1146 vol->mft_record_size_bits; in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1147 read_unlock_irqrestore(&NTFS_I(vol->mft_ino)->size_lock, flags); in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1148 read_lock_irqsave(&NTFS_I(vol->mftbmp_ino)->size_lock, flags); in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1149 ll = NTFS_I(vol->mftbmp_ino)->initialized_size << 3; in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1150 read_unlock_irqrestore(&NTFS_I(vol->mftbmp_ino)->size_lock, flags); in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1155 data_pos = vol->mft_data_pos; in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1157 data_pos = base_ni->mft_no + 1; in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1165 return -ENOSPC; in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1172 /* Loop until a free mft record is found. */ in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1174 /* Cap size to pass_end. */ in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1177 size = PAGE_SIZE - page_ofs; in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1178 ll = ((pass_end + 7) >> 3) - ofs; in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1179 if (size > ll) in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1180 size = ll; in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1181 size <<= 3; in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1186 if (size) { in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1190 ntfs_error(vol->sb, "Failed to read mft " in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1197 ntfs_debug("Before inner for loop: size 0x%x, " in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1198 "data_pos 0x%llx, bit 0x%llx", size, in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1200 for (; bit < size && data_pos + bit < pass_end; in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1210 return -ENOSPC; in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1217 "allocated mft record " in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1223 ntfs_debug("After inner for loop: size 0x%x, " in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1224 "data_pos 0x%llx, bit 0x%llx", size, in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1226 data_pos += size; in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1253 return -ENOSPC; in ntfs_mft_bitmap_find_and_alloc_free_rec_nolock()
1257 * ntfs_mft_bitmap_extend_allocation_nolock - extend mft bitmap by a cluster
1265 * Return 0 on success and -errno on error.
1267 * Locking: - Caller must hold vol->mftbmp_lock for writing.
1268 * - This function takes NTFS_I(vol->mftbmp_ino)->runlist.lock for
1270 * - This function takes vol->lcnbmp_lock for writing and releases it
1294 mft_ni = NTFS_I(vol->mft_ino); in ntfs_mft_bitmap_extend_allocation_nolock()
1295 mftbmp_ni = NTFS_I(vol->mftbmp_ino); in ntfs_mft_bitmap_extend_allocation_nolock()
1297 * Determine the last lcn of the mft bitmap. The allocated size of the in ntfs_mft_bitmap_extend_allocation_nolock()
1300 down_write(&mftbmp_ni->runlist.lock); in ntfs_mft_bitmap_extend_allocation_nolock()
1301 read_lock_irqsave(&mftbmp_ni->size_lock, flags); in ntfs_mft_bitmap_extend_allocation_nolock()
1302 ll = mftbmp_ni->allocated_size; in ntfs_mft_bitmap_extend_allocation_nolock()
1303 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags); in ntfs_mft_bitmap_extend_allocation_nolock()
1305 (ll - 1) >> vol->cluster_size_bits, NULL); in ntfs_mft_bitmap_extend_allocation_nolock()
1306 if (IS_ERR(rl) || unlikely(!rl->length || rl->lcn < 0)) { in ntfs_mft_bitmap_extend_allocation_nolock()
1307 up_write(&mftbmp_ni->runlist.lock); in ntfs_mft_bitmap_extend_allocation_nolock()
1308 ntfs_error(vol->sb, "Failed to determine last allocated " in ntfs_mft_bitmap_extend_allocation_nolock()
1311 ret = -EIO; in ntfs_mft_bitmap_extend_allocation_nolock()
1316 lcn = rl->lcn + rl->length; in ntfs_mft_bitmap_extend_allocation_nolock()
1325 page = ntfs_map_page(vol->lcnbmp_ino->i_mapping, in ntfs_mft_bitmap_extend_allocation_nolock()
1328 up_write(&mftbmp_ni->runlist.lock); in ntfs_mft_bitmap_extend_allocation_nolock()
1329 ntfs_error(vol->sb, "Failed to read from lcn bitmap."); in ntfs_mft_bitmap_extend_allocation_nolock()
1334 down_write(&vol->lcnbmp_lock); in ntfs_mft_bitmap_extend_allocation_nolock()
1340 up_write(&vol->lcnbmp_lock); in ntfs_mft_bitmap_extend_allocation_nolock()
1343 rl->length++; in ntfs_mft_bitmap_extend_allocation_nolock()
1348 up_write(&vol->lcnbmp_lock); in ntfs_mft_bitmap_extend_allocation_nolock()
1354 up_write(&mftbmp_ni->runlist.lock); in ntfs_mft_bitmap_extend_allocation_nolock()
1355 ntfs_error(vol->sb, "Failed to allocate a cluster for " in ntfs_mft_bitmap_extend_allocation_nolock()
1359 rl = ntfs_runlists_merge(mftbmp_ni->runlist.rl, rl2); in ntfs_mft_bitmap_extend_allocation_nolock()
1361 up_write(&mftbmp_ni->runlist.lock); in ntfs_mft_bitmap_extend_allocation_nolock()
1362 ntfs_error(vol->sb, "Failed to merge runlists for mft " in ntfs_mft_bitmap_extend_allocation_nolock()
1365 ntfs_error(vol->sb, "Failed to deallocate " in ntfs_mft_bitmap_extend_allocation_nolock()
1372 mftbmp_ni->runlist.rl = rl; in ntfs_mft_bitmap_extend_allocation_nolock()
1380 * Update the attribute record as well. Note: @rl is the last in ntfs_mft_bitmap_extend_allocation_nolock()
1381 * (non-terminator) runlist element of mft bitmap. in ntfs_mft_bitmap_extend_allocation_nolock()
1385 ntfs_error(vol->sb, "Failed to map mft record."); in ntfs_mft_bitmap_extend_allocation_nolock()
1391 ntfs_error(vol->sb, "Failed to get search context."); in ntfs_mft_bitmap_extend_allocation_nolock()
1392 ret = -ENOMEM; in ntfs_mft_bitmap_extend_allocation_nolock()
1395 ret = ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name, in ntfs_mft_bitmap_extend_allocation_nolock()
1396 mftbmp_ni->name_len, CASE_SENSITIVE, rl[1].vcn, NULL, in ntfs_mft_bitmap_extend_allocation_nolock()
1399 ntfs_error(vol->sb, "Failed to find last attribute extent of " in ntfs_mft_bitmap_extend_allocation_nolock()
1401 if (ret == -ENOENT) in ntfs_mft_bitmap_extend_allocation_nolock()
1402 ret = -EIO; in ntfs_mft_bitmap_extend_allocation_nolock()
1405 a = ctx->attr; in ntfs_mft_bitmap_extend_allocation_nolock()
1406 ll = sle64_to_cpu(a->data.non_resident.lowest_vcn); in ntfs_mft_bitmap_extend_allocation_nolock()
1408 for (rl2 = rl; rl2 > mftbmp_ni->runlist.rl; rl2--) { in ntfs_mft_bitmap_extend_allocation_nolock()
1409 if (ll >= rl2->vcn) in ntfs_mft_bitmap_extend_allocation_nolock()
1412 BUG_ON(ll < rl2->vcn); in ntfs_mft_bitmap_extend_allocation_nolock()
1413 BUG_ON(ll >= rl2->vcn + rl2->length); in ntfs_mft_bitmap_extend_allocation_nolock()
1414 /* Get the size for the new mapping pairs array for this extent. */ in ntfs_mft_bitmap_extend_allocation_nolock()
1415 mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, -1); in ntfs_mft_bitmap_extend_allocation_nolock()
1417 ntfs_error(vol->sb, "Get size for mapping pairs failed for " in ntfs_mft_bitmap_extend_allocation_nolock()
1421 ret = -EIO; in ntfs_mft_bitmap_extend_allocation_nolock()
1424 /* Expand the attribute record if necessary. */ in ntfs_mft_bitmap_extend_allocation_nolock()
1425 old_alen = le32_to_cpu(a->length); in ntfs_mft_bitmap_extend_allocation_nolock()
1426 ret = ntfs_attr_record_resize(ctx->mrec, a, mp_size + in ntfs_mft_bitmap_extend_allocation_nolock()
1427 le16_to_cpu(a->data.non_resident.mapping_pairs_offset)); in ntfs_mft_bitmap_extend_allocation_nolock()
1429 if (ret != -ENOSPC) { in ntfs_mft_bitmap_extend_allocation_nolock()
1430 ntfs_error(vol->sb, "Failed to resize attribute " in ntfs_mft_bitmap_extend_allocation_nolock()
1431 "record for mft bitmap attribute."); in ntfs_mft_bitmap_extend_allocation_nolock()
1435 // record or by starting a new extent in a new mft record or by in ntfs_mft_bitmap_extend_allocation_nolock()
1436 // moving other attributes out of this mft record. in ntfs_mft_bitmap_extend_allocation_nolock()
1437 // Note: It will need to be a special mft record and if none of in ntfs_mft_bitmap_extend_allocation_nolock()
1439 ntfs_error(vol->sb, "Not enough space in this mft record to " in ntfs_mft_bitmap_extend_allocation_nolock()
1442 ret = -EOPNOTSUPP; in ntfs_mft_bitmap_extend_allocation_nolock()
1446 /* Generate the mapping pairs array directly into the attr record. */ in ntfs_mft_bitmap_extend_allocation_nolock()
1448 le16_to_cpu(a->data.non_resident.mapping_pairs_offset), in ntfs_mft_bitmap_extend_allocation_nolock()
1449 mp_size, rl2, ll, -1, NULL); in ntfs_mft_bitmap_extend_allocation_nolock()
1451 ntfs_error(vol->sb, "Failed to build mapping pairs array for " in ntfs_mft_bitmap_extend_allocation_nolock()
1456 a->data.non_resident.highest_vcn = cpu_to_sle64(rl[1].vcn - 1); in ntfs_mft_bitmap_extend_allocation_nolock()
1459 * Reflect this in the ntfs_inode structure and the attribute record. in ntfs_mft_bitmap_extend_allocation_nolock()
1461 if (a->data.non_resident.lowest_vcn) { in ntfs_mft_bitmap_extend_allocation_nolock()
1466 flush_dcache_mft_record_page(ctx->ntfs_ino); in ntfs_mft_bitmap_extend_allocation_nolock()
1467 mark_mft_record_dirty(ctx->ntfs_ino); in ntfs_mft_bitmap_extend_allocation_nolock()
1469 ret = ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name, in ntfs_mft_bitmap_extend_allocation_nolock()
1470 mftbmp_ni->name_len, CASE_SENSITIVE, 0, NULL, in ntfs_mft_bitmap_extend_allocation_nolock()
1473 ntfs_error(vol->sb, "Failed to find first attribute " in ntfs_mft_bitmap_extend_allocation_nolock()
1477 a = ctx->attr; in ntfs_mft_bitmap_extend_allocation_nolock()
1479 write_lock_irqsave(&mftbmp_ni->size_lock, flags); in ntfs_mft_bitmap_extend_allocation_nolock()
1480 mftbmp_ni->allocated_size += vol->cluster_size; in ntfs_mft_bitmap_extend_allocation_nolock()
1481 a->data.non_resident.allocated_size = in ntfs_mft_bitmap_extend_allocation_nolock()
1482 cpu_to_sle64(mftbmp_ni->allocated_size); in ntfs_mft_bitmap_extend_allocation_nolock()
1483 write_unlock_irqrestore(&mftbmp_ni->size_lock, flags); in ntfs_mft_bitmap_extend_allocation_nolock()
1485 flush_dcache_mft_record_page(ctx->ntfs_ino); in ntfs_mft_bitmap_extend_allocation_nolock()
1486 mark_mft_record_dirty(ctx->ntfs_ino); in ntfs_mft_bitmap_extend_allocation_nolock()
1489 up_write(&mftbmp_ni->runlist.lock); in ntfs_mft_bitmap_extend_allocation_nolock()
1494 if (ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name, in ntfs_mft_bitmap_extend_allocation_nolock()
1495 mftbmp_ni->name_len, CASE_SENSITIVE, rl[1].vcn, NULL, in ntfs_mft_bitmap_extend_allocation_nolock()
1497 ntfs_error(vol->sb, "Failed to find last attribute extent of " in ntfs_mft_bitmap_extend_allocation_nolock()
1499 write_lock_irqsave(&mftbmp_ni->size_lock, flags); in ntfs_mft_bitmap_extend_allocation_nolock()
1500 mftbmp_ni->allocated_size += vol->cluster_size; in ntfs_mft_bitmap_extend_allocation_nolock()
1501 write_unlock_irqrestore(&mftbmp_ni->size_lock, flags); in ntfs_mft_bitmap_extend_allocation_nolock()
1504 up_write(&mftbmp_ni->runlist.lock); in ntfs_mft_bitmap_extend_allocation_nolock()
1506 * The only thing that is now wrong is ->allocated_size of the in ntfs_mft_bitmap_extend_allocation_nolock()
1512 a = ctx->attr; in ntfs_mft_bitmap_extend_allocation_nolock()
1513 a->data.non_resident.highest_vcn = cpu_to_sle64(rl[1].vcn - 2); in ntfs_mft_bitmap_extend_allocation_nolock()
1517 rl->length--; in ntfs_mft_bitmap_extend_allocation_nolock()
1518 rl[1].vcn--; in ntfs_mft_bitmap_extend_allocation_nolock()
1520 lcn = rl->lcn; in ntfs_mft_bitmap_extend_allocation_nolock()
1522 rl->lcn = rl[1].lcn; in ntfs_mft_bitmap_extend_allocation_nolock()
1523 rl->length = 0; in ntfs_mft_bitmap_extend_allocation_nolock()
1526 down_write(&vol->lcnbmp_lock); in ntfs_mft_bitmap_extend_allocation_nolock()
1527 if (ntfs_bitmap_clear_bit(vol->lcnbmp_ino, lcn)) { in ntfs_mft_bitmap_extend_allocation_nolock()
1528 ntfs_error(vol->sb, "Failed to free allocated cluster.%s", es); in ntfs_mft_bitmap_extend_allocation_nolock()
1531 up_write(&vol->lcnbmp_lock); in ntfs_mft_bitmap_extend_allocation_nolock()
1534 a->data.non_resident.mapping_pairs_offset), in ntfs_mft_bitmap_extend_allocation_nolock()
1535 old_alen - le16_to_cpu( in ntfs_mft_bitmap_extend_allocation_nolock()
1536 a->data.non_resident.mapping_pairs_offset), in ntfs_mft_bitmap_extend_allocation_nolock()
1537 rl2, ll, -1, NULL)) { in ntfs_mft_bitmap_extend_allocation_nolock()
1538 ntfs_error(vol->sb, "Failed to restore mapping pairs " in ntfs_mft_bitmap_extend_allocation_nolock()
1542 if (ntfs_attr_record_resize(ctx->mrec, a, old_alen)) { in ntfs_mft_bitmap_extend_allocation_nolock()
1543 ntfs_error(vol->sb, "Failed to restore attribute " in ntfs_mft_bitmap_extend_allocation_nolock()
1544 "record.%s", es); in ntfs_mft_bitmap_extend_allocation_nolock()
1547 flush_dcache_mft_record_page(ctx->ntfs_ino); in ntfs_mft_bitmap_extend_allocation_nolock()
1548 mark_mft_record_dirty(ctx->ntfs_ino); in ntfs_mft_bitmap_extend_allocation_nolock()
1554 up_write(&mftbmp_ni->runlist.lock); in ntfs_mft_bitmap_extend_allocation_nolock()
1559 * ntfs_mft_bitmap_extend_initialized_nolock - extend mftbmp initialized data
1568 * Return 0 on success and -error on error.
1570 * Locking: Caller must hold vol->mftbmp_lock for writing.
1583 ntfs_debug("Extending mft bitmap initiailized (and data) size."); in ntfs_mft_bitmap_extend_initialized_nolock()
1584 mft_ni = NTFS_I(vol->mft_ino); in ntfs_mft_bitmap_extend_initialized_nolock()
1585 mftbmp_vi = vol->mftbmp_ino; in ntfs_mft_bitmap_extend_initialized_nolock()
1587 /* Get the attribute record. */ in ntfs_mft_bitmap_extend_initialized_nolock()
1590 ntfs_error(vol->sb, "Failed to map mft record."); in ntfs_mft_bitmap_extend_initialized_nolock()
1595 ntfs_error(vol->sb, "Failed to get search context."); in ntfs_mft_bitmap_extend_initialized_nolock()
1596 ret = -ENOMEM; in ntfs_mft_bitmap_extend_initialized_nolock()
1599 ret = ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name, in ntfs_mft_bitmap_extend_initialized_nolock()
1600 mftbmp_ni->name_len, CASE_SENSITIVE, 0, NULL, 0, ctx); in ntfs_mft_bitmap_extend_initialized_nolock()
1602 ntfs_error(vol->sb, "Failed to find first attribute extent of " in ntfs_mft_bitmap_extend_initialized_nolock()
1604 if (ret == -ENOENT) in ntfs_mft_bitmap_extend_initialized_nolock()
1605 ret = -EIO; in ntfs_mft_bitmap_extend_initialized_nolock()
1608 a = ctx->attr; in ntfs_mft_bitmap_extend_initialized_nolock()
1609 write_lock_irqsave(&mftbmp_ni->size_lock, flags); in ntfs_mft_bitmap_extend_initialized_nolock()
1611 old_initialized_size = mftbmp_ni->initialized_size; in ntfs_mft_bitmap_extend_initialized_nolock()
1617 mftbmp_ni->initialized_size += 8; in ntfs_mft_bitmap_extend_initialized_nolock()
1618 a->data.non_resident.initialized_size = in ntfs_mft_bitmap_extend_initialized_nolock()
1619 cpu_to_sle64(mftbmp_ni->initialized_size); in ntfs_mft_bitmap_extend_initialized_nolock()
1620 if (mftbmp_ni->initialized_size > old_data_size) { in ntfs_mft_bitmap_extend_initialized_nolock()
1621 i_size_write(mftbmp_vi, mftbmp_ni->initialized_size); in ntfs_mft_bitmap_extend_initialized_nolock()
1622 a->data.non_resident.data_size = in ntfs_mft_bitmap_extend_initialized_nolock()
1623 cpu_to_sle64(mftbmp_ni->initialized_size); in ntfs_mft_bitmap_extend_initialized_nolock()
1625 write_unlock_irqrestore(&mftbmp_ni->size_lock, flags); in ntfs_mft_bitmap_extend_initialized_nolock()
1627 flush_dcache_mft_record_page(ctx->ntfs_ino); in ntfs_mft_bitmap_extend_initialized_nolock()
1628 mark_mft_record_dirty(ctx->ntfs_ino); in ntfs_mft_bitmap_extend_initialized_nolock()
1638 ntfs_error(vol->sb, "Failed to write to mft bitmap."); in ntfs_mft_bitmap_extend_initialized_nolock()
1642 ntfs_error(vol->sb, "Failed to map mft record.%s", es); in ntfs_mft_bitmap_extend_initialized_nolock()
1648 ntfs_error(vol->sb, "Failed to get search context.%s", es); in ntfs_mft_bitmap_extend_initialized_nolock()
1652 if (ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name, in ntfs_mft_bitmap_extend_initialized_nolock()
1653 mftbmp_ni->name_len, CASE_SENSITIVE, 0, NULL, 0, ctx)) { in ntfs_mft_bitmap_extend_initialized_nolock()
1654 ntfs_error(vol->sb, "Failed to find first attribute extent of " in ntfs_mft_bitmap_extend_initialized_nolock()
1663 a = ctx->attr; in ntfs_mft_bitmap_extend_initialized_nolock()
1664 write_lock_irqsave(&mftbmp_ni->size_lock, flags); in ntfs_mft_bitmap_extend_initialized_nolock()
1665 mftbmp_ni->initialized_size = old_initialized_size; in ntfs_mft_bitmap_extend_initialized_nolock()
1666 a->data.non_resident.initialized_size = in ntfs_mft_bitmap_extend_initialized_nolock()
1670 a->data.non_resident.data_size = cpu_to_sle64(old_data_size); in ntfs_mft_bitmap_extend_initialized_nolock()
1672 write_unlock_irqrestore(&mftbmp_ni->size_lock, flags); in ntfs_mft_bitmap_extend_initialized_nolock()
1673 flush_dcache_mft_record_page(ctx->ntfs_ino); in ntfs_mft_bitmap_extend_initialized_nolock()
1674 mark_mft_record_dirty(ctx->ntfs_ino); in ntfs_mft_bitmap_extend_initialized_nolock()
1678 read_lock_irqsave(&mftbmp_ni->size_lock, flags); in ntfs_mft_bitmap_extend_initialized_nolock()
1681 (long long)mftbmp_ni->allocated_size, in ntfs_mft_bitmap_extend_initialized_nolock()
1683 (long long)mftbmp_ni->initialized_size); in ntfs_mft_bitmap_extend_initialized_nolock()
1684 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags); in ntfs_mft_bitmap_extend_initialized_nolock()
1691 * ntfs_mft_data_extend_allocation_nolock - extend mft data attribute
1695 * worth of clusters or if not enough space for this by one mft record worth
1701 * Return 0 on success and -errno on error.
1703 * Locking: - Caller must hold vol->mftbmp_lock for writing.
1704 * - This function takes NTFS_I(vol->mft_ino)->runlist.lock for
1706 * - This function calls functions which take vol->lcnbmp_lock for
1725 mft_ni = NTFS_I(vol->mft_ino); in ntfs_mft_data_extend_allocation_nolock()
1728 * the mft data attribute. The allocated size of the mft data in ntfs_mft_data_extend_allocation_nolock()
1731 down_write(&mft_ni->runlist.lock); in ntfs_mft_data_extend_allocation_nolock()
1732 read_lock_irqsave(&mft_ni->size_lock, flags); in ntfs_mft_data_extend_allocation_nolock()
1733 ll = mft_ni->allocated_size; in ntfs_mft_data_extend_allocation_nolock()
1734 read_unlock_irqrestore(&mft_ni->size_lock, flags); in ntfs_mft_data_extend_allocation_nolock()
1736 (ll - 1) >> vol->cluster_size_bits, NULL); in ntfs_mft_data_extend_allocation_nolock()
1737 if (IS_ERR(rl) || unlikely(!rl->length || rl->lcn < 0)) { in ntfs_mft_data_extend_allocation_nolock()
1738 up_write(&mft_ni->runlist.lock); in ntfs_mft_data_extend_allocation_nolock()
1739 ntfs_error(vol->sb, "Failed to determine last allocated " in ntfs_mft_data_extend_allocation_nolock()
1742 ret = -EIO; in ntfs_mft_data_extend_allocation_nolock()
1747 lcn = rl->lcn + rl->length; in ntfs_mft_data_extend_allocation_nolock()
1749 /* Minimum allocation is one mft record worth of clusters. */ in ntfs_mft_data_extend_allocation_nolock()
1750 min_nr = vol->mft_record_size >> vol->cluster_size_bits; in ntfs_mft_data_extend_allocation_nolock()
1754 nr = vol->mft_record_size << 4 >> vol->cluster_size_bits; in ntfs_mft_data_extend_allocation_nolock()
1757 /* Ensure we do not go above 2^32-1 mft records. */ in ntfs_mft_data_extend_allocation_nolock()
1758 read_lock_irqsave(&mft_ni->size_lock, flags); in ntfs_mft_data_extend_allocation_nolock()
1759 ll = mft_ni->allocated_size; in ntfs_mft_data_extend_allocation_nolock()
1760 read_unlock_irqrestore(&mft_ni->size_lock, flags); in ntfs_mft_data_extend_allocation_nolock()
1761 if (unlikely((ll + (nr << vol->cluster_size_bits)) >> in ntfs_mft_data_extend_allocation_nolock()
1762 vol->mft_record_size_bits >= (1ll << 32))) { in ntfs_mft_data_extend_allocation_nolock()
1764 if (unlikely((ll + (nr << vol->cluster_size_bits)) >> in ntfs_mft_data_extend_allocation_nolock()
1765 vol->mft_record_size_bits >= (1ll << 32))) { in ntfs_mft_data_extend_allocation_nolock()
1766 ntfs_warning(vol->sb, "Cannot allocate mft record " in ntfs_mft_data_extend_allocation_nolock()
1769 up_write(&mft_ni->runlist.lock); in ntfs_mft_data_extend_allocation_nolock()
1770 return -ENOSPC; in ntfs_mft_data_extend_allocation_nolock()
1781 if (PTR_ERR(rl2) != -ENOSPC || nr == min_nr) { in ntfs_mft_data_extend_allocation_nolock()
1782 ntfs_error(vol->sb, "Failed to allocate the minimal " in ntfs_mft_data_extend_allocation_nolock()
1785 up_write(&mft_ni->runlist.lock); in ntfs_mft_data_extend_allocation_nolock()
1797 rl = ntfs_runlists_merge(mft_ni->runlist.rl, rl2); in ntfs_mft_data_extend_allocation_nolock()
1799 up_write(&mft_ni->runlist.lock); in ntfs_mft_data_extend_allocation_nolock()
1800 ntfs_error(vol->sb, "Failed to merge runlists for mft data " in ntfs_mft_data_extend_allocation_nolock()
1803 ntfs_error(vol->sb, "Failed to deallocate clusters " in ntfs_mft_data_extend_allocation_nolock()
1810 mft_ni->runlist.rl = rl; in ntfs_mft_data_extend_allocation_nolock()
1815 /* Update the attribute record as well. */ in ntfs_mft_data_extend_allocation_nolock()
1818 ntfs_error(vol->sb, "Failed to map mft record."); in ntfs_mft_data_extend_allocation_nolock()
1824 ntfs_error(vol->sb, "Failed to get search context."); in ntfs_mft_data_extend_allocation_nolock()
1825 ret = -ENOMEM; in ntfs_mft_data_extend_allocation_nolock()
1828 ret = ntfs_attr_lookup(mft_ni->type, mft_ni->name, mft_ni->name_len, in ntfs_mft_data_extend_allocation_nolock()
1831 ntfs_error(vol->sb, "Failed to find last attribute extent of " in ntfs_mft_data_extend_allocation_nolock()
1833 if (ret == -ENOENT) in ntfs_mft_data_extend_allocation_nolock()
1834 ret = -EIO; in ntfs_mft_data_extend_allocation_nolock()
1837 a = ctx->attr; in ntfs_mft_data_extend_allocation_nolock()
1838 ll = sle64_to_cpu(a->data.non_resident.lowest_vcn); in ntfs_mft_data_extend_allocation_nolock()
1840 for (rl2 = rl; rl2 > mft_ni->runlist.rl; rl2--) { in ntfs_mft_data_extend_allocation_nolock()
1841 if (ll >= rl2->vcn) in ntfs_mft_data_extend_allocation_nolock()
1844 BUG_ON(ll < rl2->vcn); in ntfs_mft_data_extend_allocation_nolock()
1845 BUG_ON(ll >= rl2->vcn + rl2->length); in ntfs_mft_data_extend_allocation_nolock()
1846 /* Get the size for the new mapping pairs array for this extent. */ in ntfs_mft_data_extend_allocation_nolock()
1847 mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, -1); in ntfs_mft_data_extend_allocation_nolock()
1849 ntfs_error(vol->sb, "Get size for mapping pairs failed for " in ntfs_mft_data_extend_allocation_nolock()
1853 ret = -EIO; in ntfs_mft_data_extend_allocation_nolock()
1856 /* Expand the attribute record if necessary. */ in ntfs_mft_data_extend_allocation_nolock()
1857 old_alen = le32_to_cpu(a->length); in ntfs_mft_data_extend_allocation_nolock()
1858 ret = ntfs_attr_record_resize(ctx->mrec, a, mp_size + in ntfs_mft_data_extend_allocation_nolock()
1859 le16_to_cpu(a->data.non_resident.mapping_pairs_offset)); in ntfs_mft_data_extend_allocation_nolock()
1861 if (ret != -ENOSPC) { in ntfs_mft_data_extend_allocation_nolock()
1862 ntfs_error(vol->sb, "Failed to resize attribute " in ntfs_mft_data_extend_allocation_nolock()
1863 "record for mft data attribute."); in ntfs_mft_data_extend_allocation_nolock()
1867 // record or by starting a new extent in a new mft record or by in ntfs_mft_data_extend_allocation_nolock()
1868 // moving other attributes out of this mft record. in ntfs_mft_data_extend_allocation_nolock()
1870 // this extent is not required to find the mft record in in ntfs_mft_data_extend_allocation_nolock()
1872 // move an existing record away, insert ours in its place, and in ntfs_mft_data_extend_allocation_nolock()
1873 // then place the moved record into the newly allocated space in ntfs_mft_data_extend_allocation_nolock()
1875 // record appropriately. This is rather complicated... in ntfs_mft_data_extend_allocation_nolock()
1876 ntfs_error(vol->sb, "Not enough space in this mft record to " in ntfs_mft_data_extend_allocation_nolock()
1879 ret = -EOPNOTSUPP; in ntfs_mft_data_extend_allocation_nolock()
1883 /* Generate the mapping pairs array directly into the attr record. */ in ntfs_mft_data_extend_allocation_nolock()
1885 le16_to_cpu(a->data.non_resident.mapping_pairs_offset), in ntfs_mft_data_extend_allocation_nolock()
1886 mp_size, rl2, ll, -1, NULL); in ntfs_mft_data_extend_allocation_nolock()
1888 ntfs_error(vol->sb, "Failed to build mapping pairs array of " in ntfs_mft_data_extend_allocation_nolock()
1893 a->data.non_resident.highest_vcn = cpu_to_sle64(rl[1].vcn - 1); in ntfs_mft_data_extend_allocation_nolock()
1896 * Reflect this in the ntfs_inode structure and the attribute record. in ntfs_mft_data_extend_allocation_nolock()
1897 * @rl is the last (non-terminator) runlist element of mft data in ntfs_mft_data_extend_allocation_nolock()
1900 if (a->data.non_resident.lowest_vcn) { in ntfs_mft_data_extend_allocation_nolock()
1905 flush_dcache_mft_record_page(ctx->ntfs_ino); in ntfs_mft_data_extend_allocation_nolock()
1906 mark_mft_record_dirty(ctx->ntfs_ino); in ntfs_mft_data_extend_allocation_nolock()
1908 ret = ntfs_attr_lookup(mft_ni->type, mft_ni->name, in ntfs_mft_data_extend_allocation_nolock()
1909 mft_ni->name_len, CASE_SENSITIVE, 0, NULL, 0, in ntfs_mft_data_extend_allocation_nolock()
1912 ntfs_error(vol->sb, "Failed to find first attribute " in ntfs_mft_data_extend_allocation_nolock()
1916 a = ctx->attr; in ntfs_mft_data_extend_allocation_nolock()
1918 write_lock_irqsave(&mft_ni->size_lock, flags); in ntfs_mft_data_extend_allocation_nolock()
1919 mft_ni->allocated_size += nr << vol->cluster_size_bits; in ntfs_mft_data_extend_allocation_nolock()
1920 a->data.non_resident.allocated_size = in ntfs_mft_data_extend_allocation_nolock()
1921 cpu_to_sle64(mft_ni->allocated_size); in ntfs_mft_data_extend_allocation_nolock()
1922 write_unlock_irqrestore(&mft_ni->size_lock, flags); in ntfs_mft_data_extend_allocation_nolock()
1924 flush_dcache_mft_record_page(ctx->ntfs_ino); in ntfs_mft_data_extend_allocation_nolock()
1925 mark_mft_record_dirty(ctx->ntfs_ino); in ntfs_mft_data_extend_allocation_nolock()
1928 up_write(&mft_ni->runlist.lock); in ntfs_mft_data_extend_allocation_nolock()
1933 if (ntfs_attr_lookup(mft_ni->type, mft_ni->name, mft_ni->name_len, in ntfs_mft_data_extend_allocation_nolock()
1935 ntfs_error(vol->sb, "Failed to find last attribute extent of " in ntfs_mft_data_extend_allocation_nolock()
1937 write_lock_irqsave(&mft_ni->size_lock, flags); in ntfs_mft_data_extend_allocation_nolock()
1938 mft_ni->allocated_size += nr << vol->cluster_size_bits; in ntfs_mft_data_extend_allocation_nolock()
1939 write_unlock_irqrestore(&mft_ni->size_lock, flags); in ntfs_mft_data_extend_allocation_nolock()
1942 up_write(&mft_ni->runlist.lock); in ntfs_mft_data_extend_allocation_nolock()
1944 * The only thing that is now wrong is ->allocated_size of the in ntfs_mft_data_extend_allocation_nolock()
1950 ctx->attr->data.non_resident.highest_vcn = in ntfs_mft_data_extend_allocation_nolock()
1951 cpu_to_sle64(old_last_vcn - 1); in ntfs_mft_data_extend_allocation_nolock()
1953 if (ntfs_cluster_free(mft_ni, old_last_vcn, -1, ctx) < 0) { in ntfs_mft_data_extend_allocation_nolock()
1954 ntfs_error(vol->sb, "Failed to free clusters from mft data " in ntfs_mft_data_extend_allocation_nolock()
1959 if (ntfs_rl_truncate_nolock(vol, &mft_ni->runlist, old_last_vcn)) { in ntfs_mft_data_extend_allocation_nolock()
1960 ntfs_error(vol->sb, "Failed to truncate mft data attribute " in ntfs_mft_data_extend_allocation_nolock()
1965 a = ctx->attr; in ntfs_mft_data_extend_allocation_nolock()
1966 if (mp_rebuilt && !IS_ERR(ctx->mrec)) { in ntfs_mft_data_extend_allocation_nolock()
1968 a->data.non_resident.mapping_pairs_offset), in ntfs_mft_data_extend_allocation_nolock()
1969 old_alen - le16_to_cpu( in ntfs_mft_data_extend_allocation_nolock()
1970 a->data.non_resident.mapping_pairs_offset), in ntfs_mft_data_extend_allocation_nolock()
1971 rl2, ll, -1, NULL)) { in ntfs_mft_data_extend_allocation_nolock()
1972 ntfs_error(vol->sb, "Failed to restore mapping pairs " in ntfs_mft_data_extend_allocation_nolock()
1976 if (ntfs_attr_record_resize(ctx->mrec, a, old_alen)) { in ntfs_mft_data_extend_allocation_nolock()
1977 ntfs_error(vol->sb, "Failed to restore attribute " in ntfs_mft_data_extend_allocation_nolock()
1978 "record.%s", es); in ntfs_mft_data_extend_allocation_nolock()
1981 flush_dcache_mft_record_page(ctx->ntfs_ino); in ntfs_mft_data_extend_allocation_nolock()
1982 mark_mft_record_dirty(ctx->ntfs_ino); in ntfs_mft_data_extend_allocation_nolock()
1983 } else if (IS_ERR(ctx->mrec)) { in ntfs_mft_data_extend_allocation_nolock()
1984 ntfs_error(vol->sb, "Failed to restore attribute search " in ntfs_mft_data_extend_allocation_nolock()
1992 up_write(&mft_ni->runlist.lock); in ntfs_mft_data_extend_allocation_nolock()
1997 * ntfs_mft_record_layout - layout an mft record into a memory buffer
1998 * @vol: volume to which the mft record will belong
1999 * @mft_no: mft reference specifying the mft record number
2000 * @m: destination buffer of size >= @vol->mft_record_size bytes
2002 * Layout an empty, unused mft record with the mft record number @mft_no into
2003 * the buffer @m. The volume @vol is needed because the mft record structure
2005 * record will be used on.
2007 * Return 0 on success and -errno on error.
2014 ntfs_debug("Entering for mft record 0x%llx.", (long long)mft_no); in ntfs_mft_record_layout()
2016 ntfs_error(vol->sb, "Mft record number 0x%llx exceeds " in ntfs_mft_record_layout()
2018 return -ERANGE; in ntfs_mft_record_layout()
2020 /* Start by clearing the whole mft record to gives us a clean slate. */ in ntfs_mft_record_layout()
2021 memset(m, 0, vol->mft_record_size); in ntfs_mft_record_layout()
2022 /* Aligned to 2-byte boundary. */ in ntfs_mft_record_layout()
2023 if (vol->major_ver < 3 || (vol->major_ver == 3 && !vol->minor_ver)) in ntfs_mft_record_layout()
2024 m->usa_ofs = cpu_to_le16((sizeof(MFT_RECORD_OLD) + 1) & ~1); in ntfs_mft_record_layout()
2026 m->usa_ofs = cpu_to_le16((sizeof(MFT_RECORD) + 1) & ~1); in ntfs_mft_record_layout()
2031 m->reserved = 0; in ntfs_mft_record_layout()
2032 m->mft_record_number = cpu_to_le32((u32)mft_no); in ntfs_mft_record_layout()
2034 m->magic = magic_FILE; in ntfs_mft_record_layout()
2035 if (vol->mft_record_size >= NTFS_BLOCK_SIZE) in ntfs_mft_record_layout()
2036 m->usa_count = cpu_to_le16(vol->mft_record_size / in ntfs_mft_record_layout()
2039 m->usa_count = cpu_to_le16(1); in ntfs_mft_record_layout()
2040 ntfs_warning(vol->sb, "Sector size is bigger than mft record " in ntfs_mft_record_layout()
2041 "size. Setting usa_count to 1. If chkdsk " in ntfs_mft_record_layout()
2043 "linux-ntfs-dev@lists.sourceforge.net stating " in ntfs_mft_record_layout()
2049 *(le16*)((u8*)m + le16_to_cpu(m->usa_ofs)) = cpu_to_le16(1); in ntfs_mft_record_layout()
2050 m->lsn = 0; in ntfs_mft_record_layout()
2051 m->sequence_number = cpu_to_le16(1); in ntfs_mft_record_layout()
2052 m->link_count = 0; in ntfs_mft_record_layout()
2055 * aligned to 8-byte boundary. in ntfs_mft_record_layout()
2057 m->attrs_offset = cpu_to_le16((le16_to_cpu(m->usa_ofs) + in ntfs_mft_record_layout()
2058 (le16_to_cpu(m->usa_count) << 1) + 7) & ~7); in ntfs_mft_record_layout()
2059 m->flags = 0; in ntfs_mft_record_layout()
2062 * attrs_offset is already aligned to 8-byte boundary, so no need to in ntfs_mft_record_layout()
2065 m->bytes_in_use = cpu_to_le32(le16_to_cpu(m->attrs_offset) + 8); in ntfs_mft_record_layout()
2066 m->bytes_allocated = cpu_to_le32(vol->mft_record_size); in ntfs_mft_record_layout()
2067 m->base_mft_record = 0; in ntfs_mft_record_layout()
2068 m->next_attr_instance = 0; in ntfs_mft_record_layout()
2070 a = (ATTR_RECORD*)((u8*)m + le16_to_cpu(m->attrs_offset)); in ntfs_mft_record_layout()
2071 a->type = AT_END; in ntfs_mft_record_layout()
2072 a->length = 0; in ntfs_mft_record_layout()
2078 * ntfs_mft_record_format - format an mft record on an ntfs volume
2079 * @vol: volume on which to format the mft record
2080 * @mft_no: mft record number to format
2082 * Format the mft record @mft_no in $MFT/$DATA, i.e. lay out an empty, unused
2083 * mft record into the appropriate place of the mft data attribute. This is
2086 * Return 0 on success and -errno on error.
2091 struct inode *mft_vi = vol->mft_ino; in ntfs_mft_record_format()
2098 ntfs_debug("Entering for mft record 0x%llx.", (long long)mft_no); in ntfs_mft_record_format()
2101 * page of the wanted mft record. in ntfs_mft_record_format()
2103 index = mft_no << vol->mft_record_size_bits >> PAGE_SHIFT; in ntfs_mft_record_format()
2104 ofs = (mft_no << vol->mft_record_size_bits) & ~PAGE_MASK; in ntfs_mft_record_format()
2109 if (unlikely(index > end_index || ofs + vol->mft_record_size >= in ntfs_mft_record_format()
2111 ntfs_error(vol->sb, "Tried to format non-existing mft " in ntfs_mft_record_format()
2112 "record 0x%llx.", (long long)mft_no); in ntfs_mft_record_format()
2113 return -ENOENT; in ntfs_mft_record_format()
2116 /* Read, map, and pin the page containing the mft record. */ in ntfs_mft_record_format()
2117 page = ntfs_map_page(mft_vi->i_mapping, index); in ntfs_mft_record_format()
2119 ntfs_error(vol->sb, "Failed to map page containing mft record " in ntfs_mft_record_format()
2129 ntfs_error(vol->sb, "Failed to layout mft record 0x%llx.", in ntfs_mft_record_format()
2140 * Make sure the mft record is written out to disk. We could use in ntfs_mft_record_format()
2142 * unnecessary as ntfs_writepage() will write the dirty record anyway. in ntfs_mft_record_format()
2151 * ntfs_mft_record_alloc - allocate an mft record on an ntfs volume
2152 * @vol: [IN] volume on which to allocate the mft record
2154 * @base_ni: [IN] open base inode if allocating an extent mft record or NULL
2155 * @mrec: [OUT] on successful return this is the mapped mft record
2157 * Allocate an mft record in $MFT/$DATA of an open ntfs volume @vol.
2159 * If @base_ni is NULL make the mft record a base mft record, i.e. a file or
2165 * If @base_ni is not NULL make the allocated mft record an extent record,
2166 * allocate it starting at the mft record after the base mft record and attach
2172 * allocated mft record. *@mrec is then set to the allocated, mapped, pinned,
2173 * and locked mft record. If IS_ERR() is true, the function failed and the
2179 * To find a free mft record, we scan the mft bitmap for a zero bit. To
2194 * record. If there are no free records left in the range 24 to number of
2196 * create free mft records. We extend the allocated size of $MFT/$DATA by 16
2197 * records at a time or one cluster, if cluster size is above 16kiB. If there
2198 * is not sufficient space to do this, we try to extend by a single mft record
2199 * or one cluster, if cluster size is above the mft record size.
2202 * allocated mft record, incrementing mft data size and initialized size
2205 * mft records until we reach record 24 which we consider as the first free mft
2206 * record for use by normal files.
2209 * extend the initialized size (and data size) by 8 bytes, allocating another
2210 * cluster if required. The bitmap data size has to be at least equal to the
2215 * - initialized / extended the mft bitmap if necessary,
2216 * - initialized / extended the mft data if necessary,
2217 * - set the bit corresponding to the mft record being allocated in the
2219 * - opened an ntfs_inode for the allocated mft record, and we will have
2220 * - returned the ntfs_inode as well as the allocated mapped, pinned, and
2221 * locked mft record.
2223 * On error, the volume will be left in a consistent state and no record will
2230 * the base mft record and an attribute list is used, it is very important that
2258 ntfs_debug("Entering (allocating an extent mft record for " in ntfs_mft_record_alloc()
2259 "base mft record 0x%llx).", in ntfs_mft_record_alloc()
2260 (long long)base_ni->mft_no); in ntfs_mft_record_alloc()
2264 ntfs_debug("Entering (allocating a base mft record)."); in ntfs_mft_record_alloc()
2270 return ERR_PTR(-EOPNOTSUPP); in ntfs_mft_record_alloc()
2273 mft_ni = NTFS_I(vol->mft_ino); in ntfs_mft_record_alloc()
2274 mftbmp_ni = NTFS_I(vol->mftbmp_ino); in ntfs_mft_record_alloc()
2275 down_write(&vol->mftbmp_lock); in ntfs_mft_record_alloc()
2278 ntfs_debug("Found and allocated free record (#1), bit 0x%llx.", in ntfs_mft_record_alloc()
2282 if (bit != -ENOSPC) { in ntfs_mft_record_alloc()
2283 up_write(&vol->mftbmp_lock); in ntfs_mft_record_alloc()
2289 * so we can simply allocate the first unused mft record. in ntfs_mft_record_alloc()
2294 read_lock_irqsave(&mft_ni->size_lock, flags); in ntfs_mft_record_alloc()
2295 ll = mft_ni->initialized_size >> vol->mft_record_size_bits; in ntfs_mft_record_alloc()
2296 read_unlock_irqrestore(&mft_ni->size_lock, flags); in ntfs_mft_record_alloc()
2297 read_lock_irqsave(&mftbmp_ni->size_lock, flags); in ntfs_mft_record_alloc()
2298 old_data_initialized = mftbmp_ni->initialized_size; in ntfs_mft_record_alloc()
2299 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags); in ntfs_mft_record_alloc()
2306 ntfs_debug("Found free record (#2), bit 0x%llx.", in ntfs_mft_record_alloc()
2312 * mft record that we can allocate. in ntfs_mft_record_alloc()
2313 * Note: The smallest mft record we allocate is mft record 24. in ntfs_mft_record_alloc()
2318 read_lock_irqsave(&mftbmp_ni->size_lock, flags); in ntfs_mft_record_alloc()
2319 old_data_size = mftbmp_ni->allocated_size; in ntfs_mft_record_alloc()
2323 (long long)i_size_read(vol->mftbmp_ino), in ntfs_mft_record_alloc()
2325 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags); in ntfs_mft_record_alloc()
2331 up_write(&vol->mftbmp_lock); in ntfs_mft_record_alloc()
2335 read_lock_irqsave(&mftbmp_ni->size_lock, flags); in ntfs_mft_record_alloc()
2339 (long long)mftbmp_ni->allocated_size, in ntfs_mft_record_alloc()
2340 (long long)i_size_read(vol->mftbmp_ino), in ntfs_mft_record_alloc()
2341 (long long)mftbmp_ni->initialized_size); in ntfs_mft_record_alloc()
2342 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags); in ntfs_mft_record_alloc()
2352 up_write(&vol->mftbmp_lock); in ntfs_mft_record_alloc()
2356 read_lock_irqsave(&mftbmp_ni->size_lock, flags); in ntfs_mft_record_alloc()
2360 (long long)mftbmp_ni->allocated_size, in ntfs_mft_record_alloc()
2361 (long long)i_size_read(vol->mftbmp_ino), in ntfs_mft_record_alloc()
2362 (long long)mftbmp_ni->initialized_size); in ntfs_mft_record_alloc()
2363 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags); in ntfs_mft_record_alloc()
2365 ntfs_debug("Found free record (#3), bit 0x%llx.", (long long)bit); in ntfs_mft_record_alloc()
2367 /* @bit is the found free mft record, allocate it in the mft bitmap. */ in ntfs_mft_record_alloc()
2369 err = ntfs_bitmap_set_bit(vol->mftbmp_ino, bit); in ntfs_mft_record_alloc()
2371 ntfs_error(vol->sb, "Failed to allocate bit in mft bitmap."); in ntfs_mft_record_alloc()
2372 up_write(&vol->mftbmp_lock); in ntfs_mft_record_alloc()
2381 * will impact decisions for mft bitmap and mft record allocation done in ntfs_mft_record_alloc()
2383 * parallel allocation could allocate the same mft record as this one. in ntfs_mft_record_alloc()
2385 ll = (bit + 1) << vol->mft_record_size_bits; in ntfs_mft_record_alloc()
2386 read_lock_irqsave(&mft_ni->size_lock, flags); in ntfs_mft_record_alloc()
2387 old_data_initialized = mft_ni->initialized_size; in ntfs_mft_record_alloc()
2388 read_unlock_irqrestore(&mft_ni->size_lock, flags); in ntfs_mft_record_alloc()
2390 ntfs_debug("Allocated mft record already initialized."); in ntfs_mft_record_alloc()
2393 ntfs_debug("Initializing allocated mft record."); in ntfs_mft_record_alloc()
2395 * The mft record is outside the initialized data. Extend the mft data in ntfs_mft_record_alloc()
2396 * attribute until it covers the allocated record. The loop is only in ntfs_mft_record_alloc()
2400 read_lock_irqsave(&mft_ni->size_lock, flags); in ntfs_mft_record_alloc()
2404 (long long)mft_ni->allocated_size, in ntfs_mft_record_alloc()
2405 (long long)i_size_read(vol->mft_ino), in ntfs_mft_record_alloc()
2406 (long long)mft_ni->initialized_size); in ntfs_mft_record_alloc()
2407 while (ll > mft_ni->allocated_size) { in ntfs_mft_record_alloc()
2408 read_unlock_irqrestore(&mft_ni->size_lock, flags); in ntfs_mft_record_alloc()
2411 ntfs_error(vol->sb, "Failed to extend mft data " in ntfs_mft_record_alloc()
2415 read_lock_irqsave(&mft_ni->size_lock, flags); in ntfs_mft_record_alloc()
2419 (long long)mft_ni->allocated_size, in ntfs_mft_record_alloc()
2420 (long long)i_size_read(vol->mft_ino), in ntfs_mft_record_alloc()
2421 (long long)mft_ni->initialized_size); in ntfs_mft_record_alloc()
2423 read_unlock_irqrestore(&mft_ni->size_lock, flags); in ntfs_mft_record_alloc()
2425 * Extend mft data initialized size (and data size of course) to reach in ntfs_mft_record_alloc()
2426 * the allocated mft record, formatting the mft records allong the way. in ntfs_mft_record_alloc()
2429 * record itself in one fell swoop later on. in ntfs_mft_record_alloc()
2431 write_lock_irqsave(&mft_ni->size_lock, flags); in ntfs_mft_record_alloc()
2432 old_data_initialized = mft_ni->initialized_size; in ntfs_mft_record_alloc()
2433 old_data_size = vol->mft_ino->i_size; in ntfs_mft_record_alloc()
2434 while (ll > mft_ni->initialized_size) { in ntfs_mft_record_alloc()
2437 new_initialized_size = mft_ni->initialized_size + in ntfs_mft_record_alloc()
2438 vol->mft_record_size; in ntfs_mft_record_alloc()
2439 mft_no = mft_ni->initialized_size >> vol->mft_record_size_bits; in ntfs_mft_record_alloc()
2440 if (new_initialized_size > i_size_read(vol->mft_ino)) in ntfs_mft_record_alloc()
2441 i_size_write(vol->mft_ino, new_initialized_size); in ntfs_mft_record_alloc()
2442 write_unlock_irqrestore(&mft_ni->size_lock, flags); in ntfs_mft_record_alloc()
2443 ntfs_debug("Initializing mft record 0x%llx.", in ntfs_mft_record_alloc()
2447 ntfs_error(vol->sb, "Failed to format mft record."); in ntfs_mft_record_alloc()
2450 write_lock_irqsave(&mft_ni->size_lock, flags); in ntfs_mft_record_alloc()
2451 mft_ni->initialized_size = new_initialized_size; in ntfs_mft_record_alloc()
2453 write_unlock_irqrestore(&mft_ni->size_lock, flags); in ntfs_mft_record_alloc()
2455 /* Update the mft data attribute record to reflect the new sizes. */ in ntfs_mft_record_alloc()
2458 ntfs_error(vol->sb, "Failed to map mft record."); in ntfs_mft_record_alloc()
2464 ntfs_error(vol->sb, "Failed to get search context."); in ntfs_mft_record_alloc()
2465 err = -ENOMEM; in ntfs_mft_record_alloc()
2469 err = ntfs_attr_lookup(mft_ni->type, mft_ni->name, mft_ni->name_len, in ntfs_mft_record_alloc()
2472 ntfs_error(vol->sb, "Failed to find first attribute extent of " in ntfs_mft_record_alloc()
2478 a = ctx->attr; in ntfs_mft_record_alloc()
2479 read_lock_irqsave(&mft_ni->size_lock, flags); in ntfs_mft_record_alloc()
2480 a->data.non_resident.initialized_size = in ntfs_mft_record_alloc()
2481 cpu_to_sle64(mft_ni->initialized_size); in ntfs_mft_record_alloc()
2482 a->data.non_resident.data_size = in ntfs_mft_record_alloc()
2483 cpu_to_sle64(i_size_read(vol->mft_ino)); in ntfs_mft_record_alloc()
2484 read_unlock_irqrestore(&mft_ni->size_lock, flags); in ntfs_mft_record_alloc()
2486 flush_dcache_mft_record_page(ctx->ntfs_ino); in ntfs_mft_record_alloc()
2487 mark_mft_record_dirty(ctx->ntfs_ino); in ntfs_mft_record_alloc()
2490 read_lock_irqsave(&mft_ni->size_lock, flags); in ntfs_mft_record_alloc()
2491 ntfs_debug("Status of mft data after mft record initialization: " in ntfs_mft_record_alloc()
2494 (long long)mft_ni->allocated_size, in ntfs_mft_record_alloc()
2495 (long long)i_size_read(vol->mft_ino), in ntfs_mft_record_alloc()
2496 (long long)mft_ni->initialized_size); in ntfs_mft_record_alloc()
2497 BUG_ON(i_size_read(vol->mft_ino) > mft_ni->allocated_size); in ntfs_mft_record_alloc()
2498 BUG_ON(mft_ni->initialized_size > i_size_read(vol->mft_ino)); in ntfs_mft_record_alloc()
2499 read_unlock_irqrestore(&mft_ni->size_lock, flags); in ntfs_mft_record_alloc()
2504 * allocated mft record still needs to be marked as in use to match the in ntfs_mft_record_alloc()
2506 * this mft record is not referenced from anywhere yet and the fact in ntfs_mft_record_alloc()
2507 * that it is allocated in the mft bitmap means that no-one will try to in ntfs_mft_record_alloc()
2510 up_write(&vol->mftbmp_lock); in ntfs_mft_record_alloc()
2512 * We now have allocated and initialized the mft record. Calculate the in ntfs_mft_record_alloc()
2513 * index of and the offset within the page cache page the record is in. in ntfs_mft_record_alloc()
2515 index = bit << vol->mft_record_size_bits >> PAGE_SHIFT; in ntfs_mft_record_alloc()
2516 ofs = (bit << vol->mft_record_size_bits) & ~PAGE_MASK; in ntfs_mft_record_alloc()
2517 /* Read, map, and pin the page containing the mft record. */ in ntfs_mft_record_alloc()
2518 page = ntfs_map_page(vol->mft_ino->i_mapping, index); in ntfs_mft_record_alloc()
2520 ntfs_error(vol->sb, "Failed to map page containing allocated " in ntfs_mft_record_alloc()
2521 "mft record 0x%llx.", (long long)bit); in ntfs_mft_record_alloc()
2529 /* If we just formatted the mft record no need to do it again. */ in ntfs_mft_record_alloc()
2531 /* Sanity check that the mft record is really not in use. */ in ntfs_mft_record_alloc()
2532 if (ntfs_is_file_record(m->magic) && in ntfs_mft_record_alloc()
2533 (m->flags & MFT_RECORD_IN_USE)) { in ntfs_mft_record_alloc()
2534 ntfs_error(vol->sb, "Mft record 0x%llx was marked " in ntfs_mft_record_alloc()
2539 err = -EIO; in ntfs_mft_record_alloc()
2547 * We need to (re-)format the mft record, preserving the in ntfs_mft_record_alloc()
2549 * sequence number if it is not zero or -1 (0xffff). This in ntfs_mft_record_alloc()
2551 * wrong with the previous mft record. in ntfs_mft_record_alloc()
2553 seq_no = m->sequence_number; in ntfs_mft_record_alloc()
2554 usn = *(le16*)((u8*)m + le16_to_cpu(m->usa_ofs)); in ntfs_mft_record_alloc()
2557 ntfs_error(vol->sb, "Failed to layout allocated mft " in ntfs_mft_record_alloc()
2558 "record 0x%llx.", (long long)bit); in ntfs_mft_record_alloc()
2565 m->sequence_number = seq_no; in ntfs_mft_record_alloc()
2567 *(le16*)((u8*)m + le16_to_cpu(m->usa_ofs)) = usn; in ntfs_mft_record_alloc()
2569 /* Set the mft record itself in use. */ in ntfs_mft_record_alloc()
2570 m->flags |= MFT_RECORD_IN_USE; in ntfs_mft_record_alloc()
2572 m->flags |= MFT_RECORD_IS_DIRECTORY; in ntfs_mft_record_alloc()
2579 * Setup the base mft record in the extent mft record. This in ntfs_mft_record_alloc()
2580 * completes initialization of the allocated extent mft record in ntfs_mft_record_alloc()
2583 m->base_mft_record = MK_LE_MREF(base_ni->mft_no, in ntfs_mft_record_alloc()
2584 base_ni->seq_no); in ntfs_mft_record_alloc()
2586 * Allocate an extent inode structure for the new mft record, in ntfs_mft_record_alloc()
2588 * its, i.e. the allocated, mft record. in ntfs_mft_record_alloc()
2592 ntfs_error(vol->sb, "Failed to map allocated extent " in ntfs_mft_record_alloc()
2593 "mft record 0x%llx.", (long long)bit); in ntfs_mft_record_alloc()
2595 /* Set the mft record itself not in use. */ in ntfs_mft_record_alloc()
2596 m->flags &= cpu_to_le16( in ntfs_mft_record_alloc()
2599 /* Make sure the mft record is written out to disk. */ in ntfs_mft_record_alloc()
2607 * Make sure the allocated mft record is written out to disk. in ntfs_mft_record_alloc()
2610 * record (e.g. at a minimum a new attribute will be added to in ntfs_mft_record_alloc()
2611 * the mft record. in ntfs_mft_record_alloc()
2622 * Allocate a new VFS inode and set it up. NOTE: @vi->i_nlink in ntfs_mft_record_alloc()
2623 * is set to 1 but the mft record->link_count is 0. The caller in ntfs_mft_record_alloc()
2626 vi = new_inode(vol->sb); in ntfs_mft_record_alloc()
2628 err = -ENOMEM; in ntfs_mft_record_alloc()
2629 /* Set the mft record itself not in use. */ in ntfs_mft_record_alloc()
2630 m->flags &= cpu_to_le16( in ntfs_mft_record_alloc()
2633 /* Make sure the mft record is written out to disk. */ in ntfs_mft_record_alloc()
2639 vi->i_ino = bit; in ntfs_mft_record_alloc()
2642 vi->i_uid = vol->uid; in ntfs_mft_record_alloc()
2643 vi->i_gid = vol->gid; in ntfs_mft_record_alloc()
2653 vi->i_mode = S_IFDIR | S_IRWXUGO; in ntfs_mft_record_alloc()
2654 vi->i_mode &= ~vol->dmask; in ntfs_mft_record_alloc()
2657 ni->type = AT_INDEX_ALLOCATION; in ntfs_mft_record_alloc()
2658 ni->name = I30; in ntfs_mft_record_alloc()
2659 ni->name_len = 4; in ntfs_mft_record_alloc()
2661 ni->itype.index.block_size = 4096; in ntfs_mft_record_alloc()
2662 ni->itype.index.block_size_bits = ntfs_ffs(4096) - 1; in ntfs_mft_record_alloc()
2663 ni->itype.index.collation_rule = COLLATION_FILE_NAME; in ntfs_mft_record_alloc()
2664 if (vol->cluster_size <= ni->itype.index.block_size) { in ntfs_mft_record_alloc()
2665 ni->itype.index.vcn_size = vol->cluster_size; in ntfs_mft_record_alloc()
2666 ni->itype.index.vcn_size_bits = in ntfs_mft_record_alloc()
2667 vol->cluster_size_bits; in ntfs_mft_record_alloc()
2669 ni->itype.index.vcn_size = vol->sector_size; in ntfs_mft_record_alloc()
2670 ni->itype.index.vcn_size_bits = in ntfs_mft_record_alloc()
2671 vol->sector_size_bits; in ntfs_mft_record_alloc()
2674 vi->i_mode = S_IFREG | S_IRWXUGO; in ntfs_mft_record_alloc()
2675 vi->i_mode &= ~vol->fmask; in ntfs_mft_record_alloc()
2677 ni->type = AT_DATA; in ntfs_mft_record_alloc()
2678 ni->name = NULL; in ntfs_mft_record_alloc()
2679 ni->name_len = 0; in ntfs_mft_record_alloc()
2682 vi->i_mode &= ~S_IWUGO; in ntfs_mft_record_alloc()
2687 * Set the file size to 0, the ntfs inode sizes are set to 0 by in ntfs_mft_record_alloc()
2690 vi->i_size = 0; in ntfs_mft_record_alloc()
2691 vi->i_blocks = 0; in ntfs_mft_record_alloc()
2694 vi->i_generation = ni->seq_no = le16_to_cpu(m->sequence_number); in ntfs_mft_record_alloc()
2696 * Manually map, pin, and lock the mft record as we already in ntfs_mft_record_alloc()
2699 atomic_inc(&ni->count); in ntfs_mft_record_alloc()
2700 mutex_lock(&ni->mrec_lock); in ntfs_mft_record_alloc()
2701 ni->page = page; in ntfs_mft_record_alloc()
2702 ni->page_ofs = ofs; in ntfs_mft_record_alloc()
2704 * Make sure the allocated mft record is written out to disk. in ntfs_mft_record_alloc()
2709 * that anyway after finishing with the new mft record (e.g. at in ntfs_mft_record_alloc()
2711 * record. in ntfs_mft_record_alloc()
2720 vol->mft_data_pos = bit + 1; in ntfs_mft_record_alloc()
2723 * Return the opened, allocated inode of the allocated mft record as in ntfs_mft_record_alloc()
2724 * well as the mapped, pinned, and locked mft record. in ntfs_mft_record_alloc()
2731 write_lock_irqsave(&mft_ni->size_lock, flags); in ntfs_mft_record_alloc()
2732 mft_ni->initialized_size = old_data_initialized; in ntfs_mft_record_alloc()
2733 i_size_write(vol->mft_ino, old_data_size); in ntfs_mft_record_alloc()
2734 write_unlock_irqrestore(&mft_ni->size_lock, flags); in ntfs_mft_record_alloc()
2737 down_write(&vol->mftbmp_lock); in ntfs_mft_record_alloc()
2739 if (ntfs_bitmap_clear_bit(vol->mftbmp_ino, bit)) { in ntfs_mft_record_alloc()
2740 ntfs_error(vol->sb, "Failed to clear bit in mft bitmap.%s", es); in ntfs_mft_record_alloc()
2743 up_write(&vol->mftbmp_lock); in ntfs_mft_record_alloc()
2747 ntfs_warning(vol->sb, "Cannot allocate mft record because the maximum " in ntfs_mft_record_alloc()
2749 up_write(&vol->mftbmp_lock); in ntfs_mft_record_alloc()
2750 return ERR_PTR(-ENOSPC); in ntfs_mft_record_alloc()
2754 * ntfs_extent_mft_record_free - free an extent mft record on an ntfs volume
2755 * @ni: ntfs inode of the mapped extent mft record to free
2756 * @m: mapped extent mft record of the ntfs inode @ni
2758 * Free the mapped extent mft record @m of the extent ntfs inode @ni.
2760 * Note that this function unmaps the mft record and closes and destroys @ni
2764 * On success return 0 and on error return -errno. @ni and @m are still valid
2770 * caller no longer wants to reference this mft record so it does not matter to
2776 unsigned long mft_no = ni->mft_no; in ntfs_extent_mft_record_free()
2777 ntfs_volume *vol = ni->vol; in ntfs_extent_mft_record_free()
2785 BUG_ON(ni->nr_extents != -1); in ntfs_extent_mft_record_free()
2787 mutex_lock(&ni->extent_lock); in ntfs_extent_mft_record_free()
2788 base_ni = ni->ext.base_ntfs_ino; in ntfs_extent_mft_record_free()
2789 mutex_unlock(&ni->extent_lock); in ntfs_extent_mft_record_free()
2791 BUG_ON(base_ni->nr_extents <= 0); in ntfs_extent_mft_record_free()
2794 mft_no, base_ni->mft_no); in ntfs_extent_mft_record_free()
2796 mutex_lock(&base_ni->extent_lock); in ntfs_extent_mft_record_free()
2799 if (atomic_read(&ni->count) > 2) { in ntfs_extent_mft_record_free()
2800 ntfs_error(vol->sb, "Tried to free busy extent inode 0x%lx, " in ntfs_extent_mft_record_free()
2801 "not freeing.", base_ni->mft_no); in ntfs_extent_mft_record_free()
2802 mutex_unlock(&base_ni->extent_lock); in ntfs_extent_mft_record_free()
2803 return -EBUSY; in ntfs_extent_mft_record_free()
2807 extent_nis = base_ni->ext.extent_ntfs_inos; in ntfs_extent_mft_record_free()
2808 err = -ENOENT; in ntfs_extent_mft_record_free()
2809 for (i = 0; i < base_ni->nr_extents; i++) { in ntfs_extent_mft_record_free()
2813 base_ni->nr_extents--; in ntfs_extent_mft_record_free()
2814 memmove(extent_nis, extent_nis + 1, (base_ni->nr_extents - i) * in ntfs_extent_mft_record_free()
2820 mutex_unlock(&base_ni->extent_lock); in ntfs_extent_mft_record_free()
2823 ntfs_error(vol->sb, "Extent inode 0x%lx is not attached to " in ntfs_extent_mft_record_free()
2825 base_ni->mft_no); in ntfs_extent_mft_record_free()
2834 /* Mark the mft record as not in use. */ in ntfs_extent_mft_record_free()
2835 m->flags &= ~MFT_RECORD_IN_USE; in ntfs_extent_mft_record_free()
2838 old_seq_no = m->sequence_number; in ntfs_extent_mft_record_free()
2844 m->sequence_number = cpu_to_le16(seq_no); in ntfs_extent_mft_record_free()
2849 * record to be freed is guaranteed to do it already. in ntfs_extent_mft_record_free()
2854 ntfs_error(vol->sb, "Failed to write mft record 0x%lx, not " in ntfs_extent_mft_record_free()
2863 /* Clear the bit in the $MFT/$BITMAP corresponding to this record. */ in ntfs_extent_mft_record_free()
2864 down_write(&vol->mftbmp_lock); in ntfs_extent_mft_record_free()
2865 err = ntfs_bitmap_clear_bit(vol->mftbmp_ino, mft_no); in ntfs_extent_mft_record_free()
2866 up_write(&vol->mftbmp_lock); in ntfs_extent_mft_record_free()
2873 ntfs_error(vol->sb, "Failed to clear bit in mft bitmap.%s", es); in ntfs_extent_mft_record_free()
2879 mutex_lock(&base_ni->extent_lock); in ntfs_extent_mft_record_free()
2880 extent_nis = base_ni->ext.extent_ntfs_inos; in ntfs_extent_mft_record_free()
2881 if (!(base_ni->nr_extents & 3)) { in ntfs_extent_mft_record_free()
2882 int new_size = (base_ni->nr_extents + 4) * sizeof(ntfs_inode*); in ntfs_extent_mft_record_free()
2886 ntfs_error(vol->sb, "Failed to allocate internal " in ntfs_extent_mft_record_free()
2888 mutex_unlock(&base_ni->extent_lock); in ntfs_extent_mft_record_free()
2892 if (base_ni->nr_extents) { in ntfs_extent_mft_record_free()
2893 BUG_ON(!base_ni->ext.extent_ntfs_inos); in ntfs_extent_mft_record_free()
2894 memcpy(extent_nis, base_ni->ext.extent_ntfs_inos, in ntfs_extent_mft_record_free()
2895 new_size - 4 * sizeof(ntfs_inode*)); in ntfs_extent_mft_record_free()
2896 kfree(base_ni->ext.extent_ntfs_inos); in ntfs_extent_mft_record_free()
2898 base_ni->ext.extent_ntfs_inos = extent_nis; in ntfs_extent_mft_record_free()
2900 m->flags |= MFT_RECORD_IN_USE; in ntfs_extent_mft_record_free()
2901 m->sequence_number = old_seq_no; in ntfs_extent_mft_record_free()
2902 extent_nis[base_ni->nr_extents++] = ni; in ntfs_extent_mft_record_free()
2903 mutex_unlock(&base_ni->extent_lock); in ntfs_extent_mft_record_free()