1a1d312deSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2aa0b42b7SRandy Dunlap /*
31da177e4SLinus Torvalds * mft.c - NTFS kernel mft record operations. Part of the Linux-NTFS project.
41da177e4SLinus Torvalds *
59b556248SAnton Altaparmakov * Copyright (c) 2001-2012 Anton Altaparmakov and Tuxera Inc.
61da177e4SLinus Torvalds * Copyright (c) 2002 Richard Russon
71da177e4SLinus Torvalds */
81da177e4SLinus Torvalds
91da177e4SLinus Torvalds #include <linux/buffer_head.h>
105a0e3ad6STejun Heo #include <linux/slab.h>
111da177e4SLinus Torvalds #include <linux/swap.h>
12be297968SChristoph Hellwig #include <linux/bio.h>
131da177e4SLinus Torvalds
141da177e4SLinus Torvalds #include "attrib.h"
151da177e4SLinus Torvalds #include "aops.h"
161da177e4SLinus Torvalds #include "bitmap.h"
171da177e4SLinus Torvalds #include "debug.h"
181da177e4SLinus Torvalds #include "dir.h"
191da177e4SLinus Torvalds #include "lcnalloc.h"
201da177e4SLinus Torvalds #include "malloc.h"
211da177e4SLinus Torvalds #include "mft.h"
221da177e4SLinus Torvalds #include "ntfs.h"
231da177e4SLinus Torvalds
24ab62ef82SKees Cook #define MAX_BHS (PAGE_SIZE / NTFS_BLOCK_SIZE)
25ab62ef82SKees Cook
261da177e4SLinus Torvalds /**
271da177e4SLinus Torvalds * map_mft_record_page - map the page in which a specific mft record resides
281da177e4SLinus Torvalds * @ni: ntfs inode whose mft record page to map
291da177e4SLinus Torvalds *
301da177e4SLinus Torvalds * This maps the page in which the mft record of the ntfs inode @ni is situated
311da177e4SLinus Torvalds * and returns a pointer to the mft record within the mapped page.
321da177e4SLinus Torvalds *
331da177e4SLinus Torvalds * Return value needs to be checked with IS_ERR() and if that is true PTR_ERR()
341da177e4SLinus Torvalds * contains the negative error code returned.
351da177e4SLinus Torvalds */
map_mft_record_page(ntfs_inode * ni)361da177e4SLinus Torvalds static inline MFT_RECORD *map_mft_record_page(ntfs_inode *ni)
371da177e4SLinus Torvalds {
3807a4e2daSAnton Altaparmakov loff_t i_size;
391da177e4SLinus Torvalds ntfs_volume *vol = ni->vol;
401da177e4SLinus Torvalds struct inode *mft_vi = vol->mft_ino;
411da177e4SLinus Torvalds struct page *page;
4269b41e3cSAnton Altaparmakov unsigned long index, end_index;
4369b41e3cSAnton Altaparmakov unsigned ofs;
441da177e4SLinus Torvalds
451da177e4SLinus Torvalds BUG_ON(ni->page);
461da177e4SLinus Torvalds /*
471da177e4SLinus Torvalds * The index into the page cache and the offset within the page cache
481da177e4SLinus Torvalds * page of the wanted mft record. FIXME: We need to check for
491da177e4SLinus Torvalds * overflowing the unsigned long, but I don't think we would ever get
501da177e4SLinus Torvalds * here if the volume was that big...
511da177e4SLinus Torvalds */
52c394e458SAnton Altaparmakov index = (u64)ni->mft_no << vol->mft_record_size_bits >>
5309cbfeafSKirill A. Shutemov PAGE_SHIFT;
5409cbfeafSKirill A. Shutemov ofs = (ni->mft_no << vol->mft_record_size_bits) & ~PAGE_MASK;
551da177e4SLinus Torvalds
5607a4e2daSAnton Altaparmakov i_size = i_size_read(mft_vi);
571da177e4SLinus Torvalds /* The maximum valid index into the page cache for $MFT's data. */
5809cbfeafSKirill A. Shutemov end_index = i_size >> PAGE_SHIFT;
591da177e4SLinus Torvalds
601da177e4SLinus Torvalds /* If the wanted index is out of bounds the mft record doesn't exist. */
611da177e4SLinus Torvalds if (unlikely(index >= end_index)) {
6209cbfeafSKirill A. Shutemov if (index > end_index || (i_size & ~PAGE_MASK) < ofs +
6307a4e2daSAnton Altaparmakov vol->mft_record_size) {
641da177e4SLinus Torvalds page = ERR_PTR(-ENOENT);
6525985edcSLucas De Marchi ntfs_error(vol->sb, "Attempt to read mft record 0x%lx, "
661da177e4SLinus Torvalds "which is beyond the end of the mft. "
671da177e4SLinus Torvalds "This is probably a bug in the ntfs "
681da177e4SLinus Torvalds "driver.", ni->mft_no);
691da177e4SLinus Torvalds goto err_out;
701da177e4SLinus Torvalds }
711da177e4SLinus Torvalds }
721da177e4SLinus Torvalds /* Read, map, and pin the page. */
731da177e4SLinus Torvalds page = ntfs_map_page(mft_vi->i_mapping, index);
74cc22c800SDenis Efremov if (!IS_ERR(page)) {
751da177e4SLinus Torvalds /* Catch multi sector transfer fixup errors. */
761da177e4SLinus Torvalds if (likely(ntfs_is_mft_recordp((le32*)(page_address(page) +
771da177e4SLinus Torvalds ofs)))) {
781da177e4SLinus Torvalds ni->page = page;
791da177e4SLinus Torvalds ni->page_ofs = ofs;
801da177e4SLinus Torvalds return page_address(page) + ofs;
811da177e4SLinus Torvalds }
821da177e4SLinus Torvalds ntfs_error(vol->sb, "Mft record 0x%lx is corrupt. "
831da177e4SLinus Torvalds "Run chkdsk.", ni->mft_no);
841da177e4SLinus Torvalds ntfs_unmap_page(page);
851da177e4SLinus Torvalds page = ERR_PTR(-EIO);
86f95c4018SAnton Altaparmakov NVolSetErrors(vol);
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds err_out:
891da177e4SLinus Torvalds ni->page = NULL;
901da177e4SLinus Torvalds ni->page_ofs = 0;
911da177e4SLinus Torvalds return (void*)page;
921da177e4SLinus Torvalds }
931da177e4SLinus Torvalds
941da177e4SLinus Torvalds /**
951da177e4SLinus Torvalds * map_mft_record - map, pin and lock an mft record
961da177e4SLinus Torvalds * @ni: ntfs inode whose MFT record to map
971da177e4SLinus Torvalds *
984e5e529aSIngo Molnar * First, take the mrec_lock mutex. We might now be sleeping, while waiting
994e5e529aSIngo Molnar * for the mutex if it was already locked by someone else.
1001da177e4SLinus Torvalds *
1011da177e4SLinus Torvalds * The page of the record is mapped using map_mft_record_page() before being
1021da177e4SLinus Torvalds * returned to the caller.
1031da177e4SLinus Torvalds *
1041da177e4SLinus Torvalds * This in turn uses ntfs_map_page() to get the page containing the wanted mft
1051da177e4SLinus Torvalds * record (it in turn calls read_cache_page() which reads it in from disk if
1061da177e4SLinus Torvalds * necessary, increments the use count on the page so that it cannot disappear
1071da177e4SLinus Torvalds * under us and returns a reference to the page cache page).
1081da177e4SLinus Torvalds *
1091da177e4SLinus Torvalds * If read_cache_page() invokes ntfs_readpage() to load the page from disk, it
1101da177e4SLinus Torvalds * sets PG_locked and clears PG_uptodate on the page. Once I/O has completed
1111da177e4SLinus Torvalds * and the post-read mst fixups on each mft record in the page have been
1121da177e4SLinus Torvalds * performed, the page gets PG_uptodate set and PG_locked cleared (this is done
1131da177e4SLinus Torvalds * in our asynchronous I/O completion handler end_buffer_read_mft_async()).
1141da177e4SLinus Torvalds * ntfs_map_page() waits for PG_locked to become clear and checks if
1151da177e4SLinus Torvalds * PG_uptodate is set and returns an error code if not. This provides
1161da177e4SLinus Torvalds * sufficient protection against races when reading/using the page.
1171da177e4SLinus Torvalds *
1181da177e4SLinus Torvalds * However there is the write mapping to think about. Doing the above described
1191da177e4SLinus Torvalds * checking here will be fine, because when initiating the write we will set
1201da177e4SLinus Torvalds * PG_locked and clear PG_uptodate making sure nobody is touching the page
1211da177e4SLinus Torvalds * contents. Doing the locking this way means that the commit to disk code in
1221da177e4SLinus Torvalds * the page cache code paths is automatically sufficiently locked with us as
1231da177e4SLinus Torvalds * we will not touch a page that has been locked or is not uptodate. The only
1241da177e4SLinus Torvalds * locking problem then is them locking the page while we are accessing it.
1251da177e4SLinus Torvalds *
1261da177e4SLinus Torvalds * So that code will end up having to own the mrec_lock of all mft
1271da177e4SLinus Torvalds * records/inodes present in the page before I/O can proceed. In that case we
1281da177e4SLinus Torvalds * wouldn't need to bother with PG_locked and PG_uptodate as nobody will be
1294e5e529aSIngo Molnar * accessing anything without owning the mrec_lock mutex. But we do need to
1304e5e529aSIngo Molnar * use them because of the read_cache_page() invocation and the code becomes so
1314e5e529aSIngo Molnar * much simpler this way that it is well worth it.
1321da177e4SLinus Torvalds *
1331da177e4SLinus Torvalds * The mft record is now ours and we return a pointer to it. You need to check
1341da177e4SLinus Torvalds * the returned pointer with IS_ERR() and if that is true, PTR_ERR() will return
1351da177e4SLinus Torvalds * the error code.
1361da177e4SLinus Torvalds *
1371da177e4SLinus Torvalds * NOTE: Caller is responsible for setting the mft record dirty before calling
1381da177e4SLinus Torvalds * unmap_mft_record(). This is obviously only necessary if the caller really
1391da177e4SLinus Torvalds * modified the mft record...
1401da177e4SLinus Torvalds * Q: Do we want to recycle one of the VFS inode state bits instead?
1411da177e4SLinus Torvalds * A: No, the inode ones mean we want to change the mft record, not we want to
1421da177e4SLinus Torvalds * write it out.
1431da177e4SLinus Torvalds */
map_mft_record(ntfs_inode * ni)1441da177e4SLinus Torvalds MFT_RECORD *map_mft_record(ntfs_inode *ni)
1451da177e4SLinus Torvalds {
1461da177e4SLinus Torvalds MFT_RECORD *m;
1471da177e4SLinus Torvalds
1481da177e4SLinus Torvalds ntfs_debug("Entering for mft_no 0x%lx.", ni->mft_no);
1491da177e4SLinus Torvalds
1501da177e4SLinus Torvalds /* Make sure the ntfs inode doesn't go away. */
1511da177e4SLinus Torvalds atomic_inc(&ni->count);
1521da177e4SLinus Torvalds
1531da177e4SLinus Torvalds /* Serialize access to this mft record. */
1544e5e529aSIngo Molnar mutex_lock(&ni->mrec_lock);
1551da177e4SLinus Torvalds
1561da177e4SLinus Torvalds m = map_mft_record_page(ni);
157cc22c800SDenis Efremov if (!IS_ERR(m))
1581da177e4SLinus Torvalds return m;
1591da177e4SLinus Torvalds
1604e5e529aSIngo Molnar mutex_unlock(&ni->mrec_lock);
1611da177e4SLinus Torvalds atomic_dec(&ni->count);
1621da177e4SLinus Torvalds ntfs_error(ni->vol->sb, "Failed with error code %lu.", -PTR_ERR(m));
1631da177e4SLinus Torvalds return m;
1641da177e4SLinus Torvalds }
1651da177e4SLinus Torvalds
1661da177e4SLinus Torvalds /**
1671da177e4SLinus Torvalds * unmap_mft_record_page - unmap the page in which a specific mft record resides
1681da177e4SLinus Torvalds * @ni: ntfs inode whose mft record page to unmap
1691da177e4SLinus Torvalds *
1701da177e4SLinus Torvalds * This unmaps the page in which the mft record of the ntfs inode @ni is
1711da177e4SLinus Torvalds * situated and returns. This is a NOOP if highmem is not configured.
1721da177e4SLinus Torvalds *
1731da177e4SLinus Torvalds * The unmap happens via ntfs_unmap_page() which in turn decrements the use
1741da177e4SLinus Torvalds * count on the page thus releasing it from the pinned state.
1751da177e4SLinus Torvalds *
1761da177e4SLinus Torvalds * We do not actually unmap the page from memory of course, as that will be
1771da177e4SLinus Torvalds * done by the page cache code itself when memory pressure increases or
1781da177e4SLinus Torvalds * whatever.
1791da177e4SLinus Torvalds */
unmap_mft_record_page(ntfs_inode * ni)1801da177e4SLinus Torvalds static inline void unmap_mft_record_page(ntfs_inode *ni)
1811da177e4SLinus Torvalds {
1821da177e4SLinus Torvalds BUG_ON(!ni->page);
1831da177e4SLinus Torvalds
1841da177e4SLinus Torvalds // TODO: If dirty, blah...
1851da177e4SLinus Torvalds ntfs_unmap_page(ni->page);
1861da177e4SLinus Torvalds ni->page = NULL;
1871da177e4SLinus Torvalds ni->page_ofs = 0;
1881da177e4SLinus Torvalds return;
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds
1911da177e4SLinus Torvalds /**
1921da177e4SLinus Torvalds * unmap_mft_record - release a mapped mft record
1931da177e4SLinus Torvalds * @ni: ntfs inode whose MFT record to unmap
1941da177e4SLinus Torvalds *
1951da177e4SLinus Torvalds * We release the page mapping and the mrec_lock mutex which unmaps the mft
1961da177e4SLinus Torvalds * record and releases it for others to get hold of. We also release the ntfs
1971da177e4SLinus Torvalds * inode by decrementing the ntfs inode reference count.
1981da177e4SLinus Torvalds *
1991da177e4SLinus Torvalds * NOTE: If caller has modified the mft record, it is imperative to set the mft
2001da177e4SLinus Torvalds * record dirty BEFORE calling unmap_mft_record().
2011da177e4SLinus Torvalds */
unmap_mft_record(ntfs_inode * ni)2021da177e4SLinus Torvalds void unmap_mft_record(ntfs_inode *ni)
2031da177e4SLinus Torvalds {
2041da177e4SLinus Torvalds struct page *page = ni->page;
2051da177e4SLinus Torvalds
2061da177e4SLinus Torvalds BUG_ON(!page);
2071da177e4SLinus Torvalds
2081da177e4SLinus Torvalds ntfs_debug("Entering for mft_no 0x%lx.", ni->mft_no);
2091da177e4SLinus Torvalds
2101da177e4SLinus Torvalds unmap_mft_record_page(ni);
2114e5e529aSIngo Molnar mutex_unlock(&ni->mrec_lock);
2121da177e4SLinus Torvalds atomic_dec(&ni->count);
2131da177e4SLinus Torvalds /*
2141da177e4SLinus Torvalds * If pure ntfs_inode, i.e. no vfs inode attached, we leave it to
2151da177e4SLinus Torvalds * ntfs_clear_extent_inode() in the extent inode case, and to the
2161da177e4SLinus Torvalds * caller in the non-extent, yet pure ntfs inode case, to do the actual
2171da177e4SLinus Torvalds * tear down of all structures and freeing of all allocated memory.
2181da177e4SLinus Torvalds */
2191da177e4SLinus Torvalds return;
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds
2221da177e4SLinus Torvalds /**
2231da177e4SLinus Torvalds * map_extent_mft_record - load an extent inode and attach it to its base
2241da177e4SLinus Torvalds * @base_ni: base ntfs inode
2251da177e4SLinus Torvalds * @mref: mft reference of the extent inode to load
2261da177e4SLinus Torvalds * @ntfs_ino: on successful return, pointer to the ntfs_inode structure
2271da177e4SLinus Torvalds *
2281da177e4SLinus Torvalds * Load the extent mft record @mref and attach it to its base inode @base_ni.
2291da177e4SLinus Torvalds * Return the mapped extent mft record if IS_ERR(result) is false. Otherwise
2301da177e4SLinus Torvalds * PTR_ERR(result) gives the negative error code.
2311da177e4SLinus Torvalds *
2321da177e4SLinus Torvalds * On successful return, @ntfs_ino contains a pointer to the ntfs_inode
2331da177e4SLinus Torvalds * structure of the mapped extent inode.
2341da177e4SLinus Torvalds */
map_extent_mft_record(ntfs_inode * base_ni,MFT_REF mref,ntfs_inode ** ntfs_ino)2351da177e4SLinus Torvalds MFT_RECORD *map_extent_mft_record(ntfs_inode *base_ni, MFT_REF mref,
2361da177e4SLinus Torvalds ntfs_inode **ntfs_ino)
2371da177e4SLinus Torvalds {
2381da177e4SLinus Torvalds MFT_RECORD *m;
2391da177e4SLinus Torvalds ntfs_inode *ni = NULL;
2401da177e4SLinus Torvalds ntfs_inode **extent_nis = NULL;
2411da177e4SLinus Torvalds int i;
2421da177e4SLinus Torvalds unsigned long mft_no = MREF(mref);
2431da177e4SLinus Torvalds u16 seq_no = MSEQNO(mref);
244c49c3111SRichard Knutsson bool destroy_ni = false;
2451da177e4SLinus Torvalds
2461da177e4SLinus Torvalds ntfs_debug("Mapping extent mft record 0x%lx (base mft record 0x%lx).",
2471da177e4SLinus Torvalds mft_no, base_ni->mft_no);
2481da177e4SLinus Torvalds /* Make sure the base ntfs inode doesn't go away. */
2491da177e4SLinus Torvalds atomic_inc(&base_ni->count);
2501da177e4SLinus Torvalds /*
2511da177e4SLinus Torvalds * Check if this extent inode has already been added to the base inode,
2521da177e4SLinus Torvalds * in which case just return it. If not found, add it to the base
2531da177e4SLinus Torvalds * inode before returning it.
2541da177e4SLinus Torvalds */
2554e5e529aSIngo Molnar mutex_lock(&base_ni->extent_lock);
2561da177e4SLinus Torvalds if (base_ni->nr_extents > 0) {
2571da177e4SLinus Torvalds extent_nis = base_ni->ext.extent_ntfs_inos;
2581da177e4SLinus Torvalds for (i = 0; i < base_ni->nr_extents; i++) {
2591da177e4SLinus Torvalds if (mft_no != extent_nis[i]->mft_no)
2601da177e4SLinus Torvalds continue;
2611da177e4SLinus Torvalds ni = extent_nis[i];
2621da177e4SLinus Torvalds /* Make sure the ntfs inode doesn't go away. */
2631da177e4SLinus Torvalds atomic_inc(&ni->count);
2641da177e4SLinus Torvalds break;
2651da177e4SLinus Torvalds }
2661da177e4SLinus Torvalds }
2671da177e4SLinus Torvalds if (likely(ni != NULL)) {
2684e5e529aSIngo Molnar mutex_unlock(&base_ni->extent_lock);
2691da177e4SLinus Torvalds atomic_dec(&base_ni->count);
2701da177e4SLinus Torvalds /* We found the record; just have to map and return it. */
2711da177e4SLinus Torvalds m = map_mft_record(ni);
2721da177e4SLinus Torvalds /* map_mft_record() has incremented this on success. */
2731da177e4SLinus Torvalds atomic_dec(&ni->count);
274cc22c800SDenis Efremov if (!IS_ERR(m)) {
2751da177e4SLinus Torvalds /* Verify the sequence number. */
2761da177e4SLinus Torvalds if (likely(le16_to_cpu(m->sequence_number) == seq_no)) {
2771da177e4SLinus Torvalds ntfs_debug("Done 1.");
2781da177e4SLinus Torvalds *ntfs_ino = ni;
2791da177e4SLinus Torvalds return m;
2801da177e4SLinus Torvalds }
2811da177e4SLinus Torvalds unmap_mft_record(ni);
2821da177e4SLinus Torvalds ntfs_error(base_ni->vol->sb, "Found stale extent mft "
2831da177e4SLinus Torvalds "reference! Corrupt filesystem. "
2841da177e4SLinus Torvalds "Run chkdsk.");
2851da177e4SLinus Torvalds return ERR_PTR(-EIO);
2861da177e4SLinus Torvalds }
2871da177e4SLinus Torvalds map_err_out:
2881da177e4SLinus Torvalds ntfs_error(base_ni->vol->sb, "Failed to map extent "
2891da177e4SLinus Torvalds "mft record, error code %ld.", -PTR_ERR(m));
2901da177e4SLinus Torvalds return m;
2911da177e4SLinus Torvalds }
2921da177e4SLinus Torvalds /* Record wasn't there. Get a new ntfs inode and initialize it. */
2931da177e4SLinus Torvalds ni = ntfs_new_extent_inode(base_ni->vol->sb, mft_no);
2941da177e4SLinus Torvalds if (unlikely(!ni)) {
2954e5e529aSIngo Molnar mutex_unlock(&base_ni->extent_lock);
2961da177e4SLinus Torvalds atomic_dec(&base_ni->count);
2971da177e4SLinus Torvalds return ERR_PTR(-ENOMEM);
2981da177e4SLinus Torvalds }
2991da177e4SLinus Torvalds ni->vol = base_ni->vol;
3001da177e4SLinus Torvalds ni->seq_no = seq_no;
3011da177e4SLinus Torvalds ni->nr_extents = -1;
3021da177e4SLinus Torvalds ni->ext.base_ntfs_ino = base_ni;
3031da177e4SLinus Torvalds /* Now map the record. */
3041da177e4SLinus Torvalds m = map_mft_record(ni);
3051da177e4SLinus Torvalds if (IS_ERR(m)) {
3064e5e529aSIngo Molnar mutex_unlock(&base_ni->extent_lock);
3071da177e4SLinus Torvalds atomic_dec(&base_ni->count);
3081da177e4SLinus Torvalds ntfs_clear_extent_inode(ni);
3091da177e4SLinus Torvalds goto map_err_out;
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds /* Verify the sequence number if it is present. */
3121da177e4SLinus Torvalds if (seq_no && (le16_to_cpu(m->sequence_number) != seq_no)) {
3131da177e4SLinus Torvalds ntfs_error(base_ni->vol->sb, "Found stale extent mft "
3141da177e4SLinus Torvalds "reference! Corrupt filesystem. Run chkdsk.");
315c49c3111SRichard Knutsson destroy_ni = true;
3161da177e4SLinus Torvalds m = ERR_PTR(-EIO);
3171da177e4SLinus Torvalds goto unm_err_out;
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds /* Attach extent inode to base inode, reallocating memory if needed. */
3201da177e4SLinus Torvalds if (!(base_ni->nr_extents & 3)) {
3211da177e4SLinus Torvalds ntfs_inode **tmp;
3221da177e4SLinus Torvalds int new_size = (base_ni->nr_extents + 4) * sizeof(ntfs_inode *);
3231da177e4SLinus Torvalds
324f52720caSPanagiotis Issaris tmp = kmalloc(new_size, GFP_NOFS);
3251da177e4SLinus Torvalds if (unlikely(!tmp)) {
3261da177e4SLinus Torvalds ntfs_error(base_ni->vol->sb, "Failed to allocate "
3271da177e4SLinus Torvalds "internal buffer.");
328c49c3111SRichard Knutsson destroy_ni = true;
3291da177e4SLinus Torvalds m = ERR_PTR(-ENOMEM);
3301da177e4SLinus Torvalds goto unm_err_out;
3311da177e4SLinus Torvalds }
3321da177e4SLinus Torvalds if (base_ni->nr_extents) {
3331da177e4SLinus Torvalds BUG_ON(!base_ni->ext.extent_ntfs_inos);
3341da177e4SLinus Torvalds memcpy(tmp, base_ni->ext.extent_ntfs_inos, new_size -
3351da177e4SLinus Torvalds 4 * sizeof(ntfs_inode *));
3361da177e4SLinus Torvalds kfree(base_ni->ext.extent_ntfs_inos);
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds base_ni->ext.extent_ntfs_inos = tmp;
3391da177e4SLinus Torvalds }
3401da177e4SLinus Torvalds base_ni->ext.extent_ntfs_inos[base_ni->nr_extents++] = ni;
3414e5e529aSIngo Molnar mutex_unlock(&base_ni->extent_lock);
3421da177e4SLinus Torvalds atomic_dec(&base_ni->count);
3431da177e4SLinus Torvalds ntfs_debug("Done 2.");
3441da177e4SLinus Torvalds *ntfs_ino = ni;
3451da177e4SLinus Torvalds return m;
3461da177e4SLinus Torvalds unm_err_out:
3471da177e4SLinus Torvalds unmap_mft_record(ni);
3484e5e529aSIngo Molnar mutex_unlock(&base_ni->extent_lock);
3491da177e4SLinus Torvalds atomic_dec(&base_ni->count);
3501da177e4SLinus Torvalds /*
3511da177e4SLinus Torvalds * If the extent inode was not attached to the base inode we need to
3521da177e4SLinus Torvalds * release it or we will leak memory.
3531da177e4SLinus Torvalds */
3541da177e4SLinus Torvalds if (destroy_ni)
3551da177e4SLinus Torvalds ntfs_clear_extent_inode(ni);
3561da177e4SLinus Torvalds return m;
3571da177e4SLinus Torvalds }
3581da177e4SLinus Torvalds
3591da177e4SLinus Torvalds #ifdef NTFS_RW
3601da177e4SLinus Torvalds
3611da177e4SLinus Torvalds /**
3621da177e4SLinus Torvalds * __mark_mft_record_dirty - set the mft record and the page containing it dirty
3631da177e4SLinus Torvalds * @ni: ntfs inode describing the mapped mft record
3641da177e4SLinus Torvalds *
3651da177e4SLinus Torvalds * Internal function. Users should call mark_mft_record_dirty() instead.
3661da177e4SLinus Torvalds *
3671da177e4SLinus Torvalds * Set the mapped (extent) mft record of the (base or extent) ntfs inode @ni,
3681da177e4SLinus Torvalds * as well as the page containing the mft record, dirty. Also, mark the base
3691da177e4SLinus Torvalds * vfs inode dirty. This ensures that any changes to the mft record are
3701da177e4SLinus Torvalds * written out to disk.
3711da177e4SLinus Torvalds *
3722c2acd2dSChristoph Hellwig * NOTE: We only set I_DIRTY_DATASYNC (and not I_DIRTY_PAGES)
3731da177e4SLinus Torvalds * on the base vfs inode, because even though file data may have been modified,
3741da177e4SLinus Torvalds * it is dirty in the inode meta data rather than the data page cache of the
3751da177e4SLinus Torvalds * inode, and thus there are no data pages that need writing out. Therefore, a
3761da177e4SLinus Torvalds * full mark_inode_dirty() is overkill. A mark_inode_dirty_sync(), on the
377ebbbf757SJan Kara * other hand, is not sufficient, because ->write_inode needs to be called even
378ebbbf757SJan Kara * in case of fdatasync. This needs to happen or the file data would not
379ebbbf757SJan Kara * necessarily hit the device synchronously, even though the vfs inode has the
380ebbbf757SJan Kara * O_SYNC flag set. Also, I_DIRTY_DATASYNC simply "feels" better than just
381ebbbf757SJan Kara * I_DIRTY_SYNC, since the file data has not actually hit the block device yet,
382ebbbf757SJan Kara * which is not what I_DIRTY_SYNC on its own would suggest.
3831da177e4SLinus Torvalds */
__mark_mft_record_dirty(ntfs_inode * ni)3841da177e4SLinus Torvalds void __mark_mft_record_dirty(ntfs_inode *ni)
3851da177e4SLinus Torvalds {
3861da177e4SLinus Torvalds ntfs_inode *base_ni;
3871da177e4SLinus Torvalds
3881da177e4SLinus Torvalds ntfs_debug("Entering for inode 0x%lx.", ni->mft_no);
3891da177e4SLinus Torvalds BUG_ON(NInoAttr(ni));
3901da177e4SLinus Torvalds mark_ntfs_record_dirty(ni->page, ni->page_ofs);
3911da177e4SLinus Torvalds /* Determine the base vfs inode and mark it dirty, too. */
3924e5e529aSIngo Molnar mutex_lock(&ni->extent_lock);
3931da177e4SLinus Torvalds if (likely(ni->nr_extents >= 0))
3941da177e4SLinus Torvalds base_ni = ni;
3951da177e4SLinus Torvalds else
3961da177e4SLinus Torvalds base_ni = ni->ext.base_ntfs_ino;
3974e5e529aSIngo Molnar mutex_unlock(&ni->extent_lock);
3982c2acd2dSChristoph Hellwig __mark_inode_dirty(VFS_I(base_ni), I_DIRTY_DATASYNC);
3991da177e4SLinus Torvalds }
4001da177e4SLinus Torvalds
4011da177e4SLinus Torvalds static const char *ntfs_please_email = "Please email "
4021da177e4SLinus Torvalds "linux-ntfs-dev@lists.sourceforge.net and say that you saw "
4031da177e4SLinus Torvalds "this message. Thank you.";
4041da177e4SLinus Torvalds
4051da177e4SLinus Torvalds /**
4061da177e4SLinus Torvalds * ntfs_sync_mft_mirror_umount - synchronise an mft record to the mft mirror
4071da177e4SLinus Torvalds * @vol: ntfs volume on which the mft record to synchronize resides
4081da177e4SLinus Torvalds * @mft_no: mft record number of mft record to synchronize
4091da177e4SLinus Torvalds * @m: mapped, mst protected (extent) mft record to synchronize
4101da177e4SLinus Torvalds *
4111da177e4SLinus Torvalds * Write the mapped, mst protected (extent) mft record @m with mft record
4121da177e4SLinus Torvalds * number @mft_no to the mft mirror ($MFTMirr) of the ntfs volume @vol,
4131da177e4SLinus Torvalds * bypassing the page cache and the $MFTMirr inode itself.
4141da177e4SLinus Torvalds *
4151da177e4SLinus Torvalds * This function is only for use at umount time when the mft mirror inode has
4161da177e4SLinus Torvalds * already been disposed off. We BUG() if we are called while the mft mirror
4171da177e4SLinus Torvalds * inode is still attached to the volume.
4181da177e4SLinus Torvalds *
4191da177e4SLinus Torvalds * On success return 0. On error return -errno.
4201da177e4SLinus Torvalds *
4211da177e4SLinus Torvalds * NOTE: This function is not implemented yet as I am not convinced it can
4221da177e4SLinus Torvalds * actually be triggered considering the sequence of commits we do in super.c::
4231da177e4SLinus Torvalds * ntfs_put_super(). But just in case we provide this place holder as the
4241da177e4SLinus Torvalds * alternative would be either to BUG() or to get a NULL pointer dereference
4251da177e4SLinus Torvalds * and Oops.
4261da177e4SLinus Torvalds */
ntfs_sync_mft_mirror_umount(ntfs_volume * vol,const unsigned long mft_no,MFT_RECORD * m)4271da177e4SLinus Torvalds static int ntfs_sync_mft_mirror_umount(ntfs_volume *vol,
4281da177e4SLinus Torvalds const unsigned long mft_no, MFT_RECORD *m)
4291da177e4SLinus Torvalds {
4301da177e4SLinus Torvalds BUG_ON(vol->mftmirr_ino);
4311da177e4SLinus Torvalds ntfs_error(vol->sb, "Umount time mft mirror syncing is not "
4321da177e4SLinus Torvalds "implemented yet. %s", ntfs_please_email);
4331da177e4SLinus Torvalds return -EOPNOTSUPP;
4341da177e4SLinus Torvalds }
4351da177e4SLinus Torvalds
4361da177e4SLinus Torvalds /**
4371da177e4SLinus Torvalds * ntfs_sync_mft_mirror - synchronize an mft record to the mft mirror
4381da177e4SLinus Torvalds * @vol: ntfs volume on which the mft record to synchronize resides
4391da177e4SLinus Torvalds * @mft_no: mft record number of mft record to synchronize
4401da177e4SLinus Torvalds * @m: mapped, mst protected (extent) mft record to synchronize
4411da177e4SLinus Torvalds * @sync: if true, wait for i/o completion
4421da177e4SLinus Torvalds *
4431da177e4SLinus Torvalds * Write the mapped, mst protected (extent) mft record @m with mft record
4441da177e4SLinus Torvalds * number @mft_no to the mft mirror ($MFTMirr) of the ntfs volume @vol.
4451da177e4SLinus Torvalds *
4461da177e4SLinus Torvalds * On success return 0. On error return -errno and set the volume errors flag
4471da177e4SLinus Torvalds * in the ntfs volume @vol.
4481da177e4SLinus Torvalds *
4491da177e4SLinus Torvalds * NOTE: We always perform synchronous i/o and ignore the @sync parameter.
4501da177e4SLinus Torvalds *
4511da177e4SLinus Torvalds * TODO: If @sync is false, want to do truly asynchronous i/o, i.e. just
4521da177e4SLinus Torvalds * schedule i/o via ->writepage or do it via kntfsd or whatever.
4531da177e4SLinus Torvalds */
ntfs_sync_mft_mirror(ntfs_volume * vol,const unsigned long mft_no,MFT_RECORD * m,int sync)4541da177e4SLinus Torvalds int ntfs_sync_mft_mirror(ntfs_volume *vol, const unsigned long mft_no,
4551da177e4SLinus Torvalds MFT_RECORD *m, int sync)
4561da177e4SLinus Torvalds {
4571da177e4SLinus Torvalds struct page *page;
4581da177e4SLinus Torvalds unsigned int blocksize = vol->sb->s_blocksize;
4591da177e4SLinus Torvalds int max_bhs = vol->mft_record_size / blocksize;
460ab62ef82SKees Cook struct buffer_head *bhs[MAX_BHS];
4611da177e4SLinus Torvalds struct buffer_head *bh, *head;
4621da177e4SLinus Torvalds u8 *kmirr;
4631da177e4SLinus Torvalds runlist_element *rl;
4641da177e4SLinus Torvalds unsigned int block_start, block_end, m_start, m_end, page_ofs;
4651da177e4SLinus Torvalds int i_bhs, nr_bhs, err = 0;
46678af34f0SAnton Altaparmakov unsigned char blocksize_bits = vol->sb->s_blocksize_bits;
4671da177e4SLinus Torvalds
4681da177e4SLinus Torvalds ntfs_debug("Entering for inode 0x%lx.", mft_no);
4691da177e4SLinus Torvalds BUG_ON(!max_bhs);
470ab62ef82SKees Cook if (WARN_ON(max_bhs > MAX_BHS))
471ab62ef82SKees Cook return -EINVAL;
4721da177e4SLinus Torvalds if (unlikely(!vol->mftmirr_ino)) {
4731da177e4SLinus Torvalds /* This could happen during umount... */
4741da177e4SLinus Torvalds err = ntfs_sync_mft_mirror_umount(vol, mft_no, m);
4751da177e4SLinus Torvalds if (likely(!err))
4761da177e4SLinus Torvalds return err;
4771da177e4SLinus Torvalds goto err_out;
4781da177e4SLinus Torvalds }
4791da177e4SLinus Torvalds /* Get the page containing the mirror copy of the mft record @m. */
4801da177e4SLinus Torvalds page = ntfs_map_page(vol->mftmirr_ino->i_mapping, mft_no >>
48109cbfeafSKirill A. Shutemov (PAGE_SHIFT - vol->mft_record_size_bits));
4821da177e4SLinus Torvalds if (IS_ERR(page)) {
4831da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to map mft mirror page.");
4841da177e4SLinus Torvalds err = PTR_ERR(page);
4851da177e4SLinus Torvalds goto err_out;
4861da177e4SLinus Torvalds }
4871da177e4SLinus Torvalds lock_page(page);
4881da177e4SLinus Torvalds BUG_ON(!PageUptodate(page));
4891da177e4SLinus Torvalds ClearPageUptodate(page);
4901da177e4SLinus Torvalds /* Offset of the mft mirror record inside the page. */
49109cbfeafSKirill A. Shutemov page_ofs = (mft_no << vol->mft_record_size_bits) & ~PAGE_MASK;
4921da177e4SLinus Torvalds /* The address in the page of the mirror copy of the mft record @m. */
4931da177e4SLinus Torvalds kmirr = page_address(page) + page_ofs;
4941da177e4SLinus Torvalds /* Copy the mst protected mft record to the mirror. */
4951da177e4SLinus Torvalds memcpy(kmirr, m, vol->mft_record_size);
4961da177e4SLinus Torvalds /* Create uptodate buffers if not present. */
4971da177e4SLinus Torvalds if (unlikely(!page_has_buffers(page))) {
4981da177e4SLinus Torvalds struct buffer_head *tail;
4991da177e4SLinus Torvalds
500640ab98fSJens Axboe bh = head = alloc_page_buffers(page, blocksize, true);
5011da177e4SLinus Torvalds do {
5021da177e4SLinus Torvalds set_buffer_uptodate(bh);
5031da177e4SLinus Torvalds tail = bh;
5041da177e4SLinus Torvalds bh = bh->b_this_page;
5051da177e4SLinus Torvalds } while (bh);
5061da177e4SLinus Torvalds tail->b_this_page = head;
50714ed109eSGuoqing Jiang attach_page_private(page, head);
5081da177e4SLinus Torvalds }
5091da177e4SLinus Torvalds bh = head = page_buffers(page);
5101da177e4SLinus Torvalds BUG_ON(!bh);
5111da177e4SLinus Torvalds rl = NULL;
5121da177e4SLinus Torvalds nr_bhs = 0;
5131da177e4SLinus Torvalds block_start = 0;
5141da177e4SLinus Torvalds m_start = kmirr - (u8*)page_address(page);
5151da177e4SLinus Torvalds m_end = m_start + vol->mft_record_size;
5161da177e4SLinus Torvalds do {
5171da177e4SLinus Torvalds block_end = block_start + blocksize;
5181da177e4SLinus Torvalds /* If the buffer is outside the mft record, skip it. */
5191da177e4SLinus Torvalds if (block_end <= m_start)
5201da177e4SLinus Torvalds continue;
5211da177e4SLinus Torvalds if (unlikely(block_start >= m_end))
5221da177e4SLinus Torvalds break;
5231da177e4SLinus Torvalds /* Need to map the buffer if it is not mapped already. */
5241da177e4SLinus Torvalds if (unlikely(!buffer_mapped(bh))) {
5251da177e4SLinus Torvalds VCN vcn;
5261da177e4SLinus Torvalds LCN lcn;
5271da177e4SLinus Torvalds unsigned int vcn_ofs;
5281da177e4SLinus Torvalds
529e74589acSAnton Altaparmakov bh->b_bdev = vol->sb->s_bdev;
5301da177e4SLinus Torvalds /* Obtain the vcn and offset of the current block. */
5311da177e4SLinus Torvalds vcn = ((VCN)mft_no << vol->mft_record_size_bits) +
5321da177e4SLinus Torvalds (block_start - m_start);
5331da177e4SLinus Torvalds vcn_ofs = vcn & vol->cluster_size_mask;
5341da177e4SLinus Torvalds vcn >>= vol->cluster_size_bits;
5351da177e4SLinus Torvalds if (!rl) {
5361da177e4SLinus Torvalds down_read(&NTFS_I(vol->mftmirr_ino)->
5371da177e4SLinus Torvalds runlist.lock);
5381da177e4SLinus Torvalds rl = NTFS_I(vol->mftmirr_ino)->runlist.rl;
5391da177e4SLinus Torvalds /*
5401da177e4SLinus Torvalds * $MFTMirr always has the whole of its runlist
5411da177e4SLinus Torvalds * in memory.
5421da177e4SLinus Torvalds */
5431da177e4SLinus Torvalds BUG_ON(!rl);
5441da177e4SLinus Torvalds }
5451da177e4SLinus Torvalds /* Seek to element containing target vcn. */
5461da177e4SLinus Torvalds while (rl->length && rl[1].vcn <= vcn)
5471da177e4SLinus Torvalds rl++;
5481da177e4SLinus Torvalds lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
5491da177e4SLinus Torvalds /* For $MFTMirr, only lcn >= 0 is a successful remap. */
5501da177e4SLinus Torvalds if (likely(lcn >= 0)) {
5511da177e4SLinus Torvalds /* Setup buffer head to correct block. */
5521da177e4SLinus Torvalds bh->b_blocknr = ((lcn <<
5531da177e4SLinus Torvalds vol->cluster_size_bits) +
5541da177e4SLinus Torvalds vcn_ofs) >> blocksize_bits;
5551da177e4SLinus Torvalds set_buffer_mapped(bh);
5561da177e4SLinus Torvalds } else {
5571da177e4SLinus Torvalds bh->b_blocknr = -1;
5581da177e4SLinus Torvalds ntfs_error(vol->sb, "Cannot write mft mirror "
5591da177e4SLinus Torvalds "record 0x%lx because its "
5601da177e4SLinus Torvalds "location on disk could not "
5611da177e4SLinus Torvalds "be determined (error code "
5621da177e4SLinus Torvalds "%lli).", mft_no,
5631da177e4SLinus Torvalds (long long)lcn);
5641da177e4SLinus Torvalds err = -EIO;
5651da177e4SLinus Torvalds }
5661da177e4SLinus Torvalds }
5671da177e4SLinus Torvalds BUG_ON(!buffer_uptodate(bh));
5681da177e4SLinus Torvalds BUG_ON(!nr_bhs && (m_start != block_start));
5691da177e4SLinus Torvalds BUG_ON(nr_bhs >= max_bhs);
5701da177e4SLinus Torvalds bhs[nr_bhs++] = bh;
5711da177e4SLinus Torvalds BUG_ON((nr_bhs >= max_bhs) && (m_end != block_end));
5721da177e4SLinus Torvalds } while (block_start = block_end, (bh = bh->b_this_page) != head);
5731da177e4SLinus Torvalds if (unlikely(rl))
5741da177e4SLinus Torvalds up_read(&NTFS_I(vol->mftmirr_ino)->runlist.lock);
5751da177e4SLinus Torvalds if (likely(!err)) {
5761da177e4SLinus Torvalds /* Lock buffers and start synchronous write i/o on them. */
5771da177e4SLinus Torvalds for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++) {
5781da177e4SLinus Torvalds struct buffer_head *tbh = bhs[i_bhs];
5791da177e4SLinus Torvalds
580ca5de404SNick Piggin if (!trylock_buffer(tbh))
5811da177e4SLinus Torvalds BUG();
5821da177e4SLinus Torvalds BUG_ON(!buffer_uptodate(tbh));
5831da177e4SLinus Torvalds clear_buffer_dirty(tbh);
5841da177e4SLinus Torvalds get_bh(tbh);
5851da177e4SLinus Torvalds tbh->b_end_io = end_buffer_write_sync;
5861420c4a5SBart Van Assche submit_bh(REQ_OP_WRITE, tbh);
5871da177e4SLinus Torvalds }
5881da177e4SLinus Torvalds /* Wait on i/o completion of buffers. */
5891da177e4SLinus Torvalds for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++) {
5901da177e4SLinus Torvalds struct buffer_head *tbh = bhs[i_bhs];
5911da177e4SLinus Torvalds
5921da177e4SLinus Torvalds wait_on_buffer(tbh);
5931da177e4SLinus Torvalds if (unlikely(!buffer_uptodate(tbh))) {
5941da177e4SLinus Torvalds err = -EIO;
5951da177e4SLinus Torvalds /*
5961da177e4SLinus Torvalds * Set the buffer uptodate so the page and
5971da177e4SLinus Torvalds * buffer states do not become out of sync.
5981da177e4SLinus Torvalds */
5991da177e4SLinus Torvalds set_buffer_uptodate(tbh);
6001da177e4SLinus Torvalds }
6011da177e4SLinus Torvalds }
6021da177e4SLinus Torvalds } else /* if (unlikely(err)) */ {
6031da177e4SLinus Torvalds /* Clean the buffers. */
6041da177e4SLinus Torvalds for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++)
6051da177e4SLinus Torvalds clear_buffer_dirty(bhs[i_bhs]);
6061da177e4SLinus Torvalds }
6071da177e4SLinus Torvalds /* Current state: all buffers are clean, unlocked, and uptodate. */
6081da177e4SLinus Torvalds /* Remove the mst protection fixups again. */
6091da177e4SLinus Torvalds post_write_mst_fixup((NTFS_RECORD*)kmirr);
6101da177e4SLinus Torvalds flush_dcache_page(page);
6111da177e4SLinus Torvalds SetPageUptodate(page);
6121da177e4SLinus Torvalds unlock_page(page);
6131da177e4SLinus Torvalds ntfs_unmap_page(page);
6141da177e4SLinus Torvalds if (likely(!err)) {
6151da177e4SLinus Torvalds ntfs_debug("Done.");
6161da177e4SLinus Torvalds } else {
6171da177e4SLinus Torvalds ntfs_error(vol->sb, "I/O error while writing mft mirror "
6181da177e4SLinus Torvalds "record 0x%lx!", mft_no);
6191da177e4SLinus Torvalds err_out:
6201da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to synchronize $MFTMirr (error "
6211da177e4SLinus Torvalds "code %i). Volume will be left marked dirty "
6221da177e4SLinus Torvalds "on umount. Run ntfsfix on the partition "
6231da177e4SLinus Torvalds "after umounting to correct this.", -err);
6241da177e4SLinus Torvalds NVolSetErrors(vol);
6251da177e4SLinus Torvalds }
6261da177e4SLinus Torvalds return err;
6271da177e4SLinus Torvalds }
6281da177e4SLinus Torvalds
6291da177e4SLinus Torvalds /**
6301da177e4SLinus Torvalds * write_mft_record_nolock - write out a mapped (extent) mft record
6311da177e4SLinus Torvalds * @ni: ntfs inode describing the mapped (extent) mft record
6321da177e4SLinus Torvalds * @m: mapped (extent) mft record to write
6331da177e4SLinus Torvalds * @sync: if true, wait for i/o completion
6341da177e4SLinus Torvalds *
6351da177e4SLinus Torvalds * Write the mapped (extent) mft record @m described by the (regular or extent)
6361da177e4SLinus Torvalds * ntfs inode @ni to backing store. If the mft record @m has a counterpart in
6371da177e4SLinus Torvalds * the mft mirror, that is also updated.
6381da177e4SLinus Torvalds *
6391da177e4SLinus Torvalds * We only write the mft record if the ntfs inode @ni is dirty and the first
6401da177e4SLinus Torvalds * buffer belonging to its mft record is dirty, too. We ignore the dirty state
6411da177e4SLinus Torvalds * of subsequent buffers because we could have raced with
6421da177e4SLinus Torvalds * fs/ntfs/aops.c::mark_ntfs_record_dirty().
6431da177e4SLinus Torvalds *
6441da177e4SLinus Torvalds * On success, clean the mft record and return 0. On error, leave the mft
645a778f217SAnton Altaparmakov * record dirty and return -errno.
6461da177e4SLinus Torvalds *
6471da177e4SLinus Torvalds * NOTE: We always perform synchronous i/o and ignore the @sync parameter.
6481da177e4SLinus Torvalds * However, if the mft record has a counterpart in the mft mirror and @sync is
6491da177e4SLinus Torvalds * true, we write the mft record, wait for i/o completion, and only then write
6501da177e4SLinus Torvalds * the mft mirror copy. This ensures that if the system crashes either the mft
6511da177e4SLinus Torvalds * or the mft mirror will contain a self-consistent mft record @m. If @sync is
6521da177e4SLinus Torvalds * false on the other hand, we start i/o on both and then wait for completion
6531da177e4SLinus Torvalds * on them. This provides a speedup but no longer guarantees that you will end
6541da177e4SLinus Torvalds * up with a self-consistent mft record in the case of a crash but if you asked
6551da177e4SLinus Torvalds * for asynchronous writing you probably do not care about that anyway.
6561da177e4SLinus Torvalds *
6571da177e4SLinus Torvalds * TODO: If @sync is false, want to do truly asynchronous i/o, i.e. just
6581da177e4SLinus Torvalds * schedule i/o via ->writepage or do it via kntfsd or whatever.
6591da177e4SLinus Torvalds */
write_mft_record_nolock(ntfs_inode * ni,MFT_RECORD * m,int sync)6601da177e4SLinus Torvalds int write_mft_record_nolock(ntfs_inode *ni, MFT_RECORD *m, int sync)
6611da177e4SLinus Torvalds {
6621da177e4SLinus Torvalds ntfs_volume *vol = ni->vol;
6631da177e4SLinus Torvalds struct page *page = ni->page;
66478af34f0SAnton Altaparmakov unsigned int blocksize = vol->sb->s_blocksize;
66578af34f0SAnton Altaparmakov unsigned char blocksize_bits = vol->sb->s_blocksize_bits;
6661da177e4SLinus Torvalds int max_bhs = vol->mft_record_size / blocksize;
667ab62ef82SKees Cook struct buffer_head *bhs[MAX_BHS];
6681da177e4SLinus Torvalds struct buffer_head *bh, *head;
6691da177e4SLinus Torvalds runlist_element *rl;
6701da177e4SLinus Torvalds unsigned int block_start, block_end, m_start, m_end;
6711da177e4SLinus Torvalds int i_bhs, nr_bhs, err = 0;
6721da177e4SLinus Torvalds
6731da177e4SLinus Torvalds ntfs_debug("Entering for inode 0x%lx.", ni->mft_no);
6741da177e4SLinus Torvalds BUG_ON(NInoAttr(ni));
6751da177e4SLinus Torvalds BUG_ON(!max_bhs);
6761da177e4SLinus Torvalds BUG_ON(!PageLocked(page));
677ab62ef82SKees Cook if (WARN_ON(max_bhs > MAX_BHS)) {
678ab62ef82SKees Cook err = -EINVAL;
679ab62ef82SKees Cook goto err_out;
680ab62ef82SKees Cook }
6811da177e4SLinus Torvalds /*
6821da177e4SLinus Torvalds * If the ntfs_inode is clean no need to do anything. If it is dirty,
6831da177e4SLinus Torvalds * mark it as clean now so that it can be redirtied later on if needed.
6841da177e4SLinus Torvalds * There is no danger of races since the caller is holding the locks
6851da177e4SLinus Torvalds * for the mft record @m and the page it is in.
6861da177e4SLinus Torvalds */
6871da177e4SLinus Torvalds if (!NInoTestClearDirty(ni))
6881da177e4SLinus Torvalds goto done;
6891da177e4SLinus Torvalds bh = head = page_buffers(page);
6901da177e4SLinus Torvalds BUG_ON(!bh);
6911da177e4SLinus Torvalds rl = NULL;
6921da177e4SLinus Torvalds nr_bhs = 0;
6931da177e4SLinus Torvalds block_start = 0;
6941da177e4SLinus Torvalds m_start = ni->page_ofs;
6951da177e4SLinus Torvalds m_end = m_start + vol->mft_record_size;
6961da177e4SLinus Torvalds do {
6971da177e4SLinus Torvalds block_end = block_start + blocksize;
6981da177e4SLinus Torvalds /* If the buffer is outside the mft record, skip it. */
6991da177e4SLinus Torvalds if (block_end <= m_start)
7001da177e4SLinus Torvalds continue;
7011da177e4SLinus Torvalds if (unlikely(block_start >= m_end))
7021da177e4SLinus Torvalds break;
7031da177e4SLinus Torvalds /*
7041da177e4SLinus Torvalds * If this block is not the first one in the record, we ignore
7051da177e4SLinus Torvalds * the buffer's dirty state because we could have raced with a
7061da177e4SLinus Torvalds * parallel mark_ntfs_record_dirty().
7071da177e4SLinus Torvalds */
7081da177e4SLinus Torvalds if (block_start == m_start) {
7091da177e4SLinus Torvalds /* This block is the first one in the record. */
7101da177e4SLinus Torvalds if (!buffer_dirty(bh)) {
7111da177e4SLinus Torvalds BUG_ON(nr_bhs);
7121da177e4SLinus Torvalds /* Clean records are not written out. */
7131da177e4SLinus Torvalds break;
7141da177e4SLinus Torvalds }
7151da177e4SLinus Torvalds }
7161da177e4SLinus Torvalds /* Need to map the buffer if it is not mapped already. */
7171da177e4SLinus Torvalds if (unlikely(!buffer_mapped(bh))) {
7181da177e4SLinus Torvalds VCN vcn;
7191da177e4SLinus Torvalds LCN lcn;
7201da177e4SLinus Torvalds unsigned int vcn_ofs;
7211da177e4SLinus Torvalds
722e74589acSAnton Altaparmakov bh->b_bdev = vol->sb->s_bdev;
7231da177e4SLinus Torvalds /* Obtain the vcn and offset of the current block. */
7241da177e4SLinus Torvalds vcn = ((VCN)ni->mft_no << vol->mft_record_size_bits) +
7251da177e4SLinus Torvalds (block_start - m_start);
7261da177e4SLinus Torvalds vcn_ofs = vcn & vol->cluster_size_mask;
7271da177e4SLinus Torvalds vcn >>= vol->cluster_size_bits;
7281da177e4SLinus Torvalds if (!rl) {
7291da177e4SLinus Torvalds down_read(&NTFS_I(vol->mft_ino)->runlist.lock);
7301da177e4SLinus Torvalds rl = NTFS_I(vol->mft_ino)->runlist.rl;
7311da177e4SLinus Torvalds BUG_ON(!rl);
7321da177e4SLinus Torvalds }
7331da177e4SLinus Torvalds /* Seek to element containing target vcn. */
7341da177e4SLinus Torvalds while (rl->length && rl[1].vcn <= vcn)
7351da177e4SLinus Torvalds rl++;
7361da177e4SLinus Torvalds lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
7371da177e4SLinus Torvalds /* For $MFT, only lcn >= 0 is a successful remap. */
7381da177e4SLinus Torvalds if (likely(lcn >= 0)) {
7391da177e4SLinus Torvalds /* Setup buffer head to correct block. */
7401da177e4SLinus Torvalds bh->b_blocknr = ((lcn <<
7411da177e4SLinus Torvalds vol->cluster_size_bits) +
7421da177e4SLinus Torvalds vcn_ofs) >> blocksize_bits;
7431da177e4SLinus Torvalds set_buffer_mapped(bh);
7441da177e4SLinus Torvalds } else {
7451da177e4SLinus Torvalds bh->b_blocknr = -1;
7461da177e4SLinus Torvalds ntfs_error(vol->sb, "Cannot write mft record "
7471da177e4SLinus Torvalds "0x%lx because its location "
7481da177e4SLinus Torvalds "on disk could not be "
7491da177e4SLinus Torvalds "determined (error code %lli).",
7501da177e4SLinus Torvalds ni->mft_no, (long long)lcn);
7511da177e4SLinus Torvalds err = -EIO;
7521da177e4SLinus Torvalds }
7531da177e4SLinus Torvalds }
7541da177e4SLinus Torvalds BUG_ON(!buffer_uptodate(bh));
7551da177e4SLinus Torvalds BUG_ON(!nr_bhs && (m_start != block_start));
7561da177e4SLinus Torvalds BUG_ON(nr_bhs >= max_bhs);
7571da177e4SLinus Torvalds bhs[nr_bhs++] = bh;
7581da177e4SLinus Torvalds BUG_ON((nr_bhs >= max_bhs) && (m_end != block_end));
7591da177e4SLinus Torvalds } while (block_start = block_end, (bh = bh->b_this_page) != head);
7601da177e4SLinus Torvalds if (unlikely(rl))
7611da177e4SLinus Torvalds up_read(&NTFS_I(vol->mft_ino)->runlist.lock);
7621da177e4SLinus Torvalds if (!nr_bhs)
7631da177e4SLinus Torvalds goto done;
7641da177e4SLinus Torvalds if (unlikely(err))
7651da177e4SLinus Torvalds goto cleanup_out;
7661da177e4SLinus Torvalds /* Apply the mst protection fixups. */
7671da177e4SLinus Torvalds err = pre_write_mst_fixup((NTFS_RECORD*)m, vol->mft_record_size);
7681da177e4SLinus Torvalds if (err) {
7691da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to apply mst fixups!");
7701da177e4SLinus Torvalds goto cleanup_out;
7711da177e4SLinus Torvalds }
7721da177e4SLinus Torvalds flush_dcache_mft_record_page(ni);
7731da177e4SLinus Torvalds /* Lock buffers and start synchronous write i/o on them. */
7741da177e4SLinus Torvalds for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++) {
7751da177e4SLinus Torvalds struct buffer_head *tbh = bhs[i_bhs];
7761da177e4SLinus Torvalds
777ca5de404SNick Piggin if (!trylock_buffer(tbh))
7781da177e4SLinus Torvalds BUG();
7791da177e4SLinus Torvalds BUG_ON(!buffer_uptodate(tbh));
7801da177e4SLinus Torvalds clear_buffer_dirty(tbh);
7811da177e4SLinus Torvalds get_bh(tbh);
7821da177e4SLinus Torvalds tbh->b_end_io = end_buffer_write_sync;
7831420c4a5SBart Van Assche submit_bh(REQ_OP_WRITE, tbh);
7841da177e4SLinus Torvalds }
7851da177e4SLinus Torvalds /* Synchronize the mft mirror now if not @sync. */
7861da177e4SLinus Torvalds if (!sync && ni->mft_no < vol->mftmirr_size)
7871da177e4SLinus Torvalds ntfs_sync_mft_mirror(vol, ni->mft_no, m, sync);
7881da177e4SLinus Torvalds /* Wait on i/o completion of buffers. */
7891da177e4SLinus Torvalds for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++) {
7901da177e4SLinus Torvalds struct buffer_head *tbh = bhs[i_bhs];
7911da177e4SLinus Torvalds
7921da177e4SLinus Torvalds wait_on_buffer(tbh);
7931da177e4SLinus Torvalds if (unlikely(!buffer_uptodate(tbh))) {
7941da177e4SLinus Torvalds err = -EIO;
7951da177e4SLinus Torvalds /*
7961da177e4SLinus Torvalds * Set the buffer uptodate so the page and buffer
7971da177e4SLinus Torvalds * states do not become out of sync.
7981da177e4SLinus Torvalds */
7991da177e4SLinus Torvalds if (PageUptodate(page))
8001da177e4SLinus Torvalds set_buffer_uptodate(tbh);
8011da177e4SLinus Torvalds }
8021da177e4SLinus Torvalds }
8031da177e4SLinus Torvalds /* If @sync, now synchronize the mft mirror. */
8041da177e4SLinus Torvalds if (sync && ni->mft_no < vol->mftmirr_size)
8051da177e4SLinus Torvalds ntfs_sync_mft_mirror(vol, ni->mft_no, m, sync);
8061da177e4SLinus Torvalds /* Remove the mst protection fixups again. */
8071da177e4SLinus Torvalds post_write_mst_fixup((NTFS_RECORD*)m);
8081da177e4SLinus Torvalds flush_dcache_mft_record_page(ni);
8091da177e4SLinus Torvalds if (unlikely(err)) {
8101da177e4SLinus Torvalds /* I/O error during writing. This is really bad! */
8111da177e4SLinus Torvalds ntfs_error(vol->sb, "I/O error while writing mft record "
8121da177e4SLinus Torvalds "0x%lx! Marking base inode as bad. You "
8131da177e4SLinus Torvalds "should unmount the volume and run chkdsk.",
8141da177e4SLinus Torvalds ni->mft_no);
8151da177e4SLinus Torvalds goto err_out;
8161da177e4SLinus Torvalds }
8171da177e4SLinus Torvalds done:
8181da177e4SLinus Torvalds ntfs_debug("Done.");
8191da177e4SLinus Torvalds return 0;
8201da177e4SLinus Torvalds cleanup_out:
8211da177e4SLinus Torvalds /* Clean the buffers. */
8221da177e4SLinus Torvalds for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++)
8231da177e4SLinus Torvalds clear_buffer_dirty(bhs[i_bhs]);
8241da177e4SLinus Torvalds err_out:
8251da177e4SLinus Torvalds /*
8261da177e4SLinus Torvalds * Current state: all buffers are clean, unlocked, and uptodate.
8271da177e4SLinus Torvalds * The caller should mark the base inode as bad so that no more i/o
8281da177e4SLinus Torvalds * happens. ->clear_inode() will still be invoked so all extent inodes
8291da177e4SLinus Torvalds * and other allocated memory will be freed.
8301da177e4SLinus Torvalds */
8311da177e4SLinus Torvalds if (err == -ENOMEM) {
8321da177e4SLinus Torvalds ntfs_error(vol->sb, "Not enough memory to write mft record. "
8331da177e4SLinus Torvalds "Redirtying so the write is retried later.");
8341da177e4SLinus Torvalds mark_mft_record_dirty(ni);
8351da177e4SLinus Torvalds err = 0;
8361da177e4SLinus Torvalds } else
8371da177e4SLinus Torvalds NVolSetErrors(vol);
8381da177e4SLinus Torvalds return err;
8391da177e4SLinus Torvalds }
8401da177e4SLinus Torvalds
8411da177e4SLinus Torvalds /**
8421da177e4SLinus Torvalds * ntfs_may_write_mft_record - check if an mft record may be written out
8431da177e4SLinus Torvalds * @vol: [IN] ntfs volume on which the mft record to check resides
8441da177e4SLinus Torvalds * @mft_no: [IN] mft record number of the mft record to check
8451da177e4SLinus Torvalds * @m: [IN] mapped mft record to check
8461da177e4SLinus Torvalds * @locked_ni: [OUT] caller has to unlock this ntfs inode if one is returned
8471da177e4SLinus Torvalds *
8481da177e4SLinus Torvalds * Check if the mapped (base or extent) mft record @m with mft record number
8491da177e4SLinus Torvalds * @mft_no belonging to the ntfs volume @vol may be written out. If necessary
8501da177e4SLinus Torvalds * and possible the ntfs inode of the mft record is locked and the base vfs
8511da177e4SLinus Torvalds * inode is pinned. The locked ntfs inode is then returned in @locked_ni. The
8521da177e4SLinus Torvalds * caller is responsible for unlocking the ntfs inode and unpinning the base
8531da177e4SLinus Torvalds * vfs inode.
8541da177e4SLinus Torvalds *
855c49c3111SRichard Knutsson * Return 'true' if the mft record may be written out and 'false' if not.
8561da177e4SLinus Torvalds *
8571da177e4SLinus Torvalds * The caller has locked the page and cleared the uptodate flag on it which
8581da177e4SLinus Torvalds * means that we can safely write out any dirty mft records that do not have
8591da177e4SLinus Torvalds * their inodes in icache as determined by ilookup5() as anyone
8601da177e4SLinus Torvalds * opening/creating such an inode would block when attempting to map the mft
8611da177e4SLinus Torvalds * record in read_cache_page() until we are finished with the write out.
8621da177e4SLinus Torvalds *
8631da177e4SLinus Torvalds * Here is a description of the tests we perform:
8641da177e4SLinus Torvalds *
8651da177e4SLinus Torvalds * If the inode is found in icache we know the mft record must be a base mft
866c49c3111SRichard Knutsson * record. If it is dirty, we do not write it and return 'false' as the vfs
8671da177e4SLinus Torvalds * inode write paths will result in the access times being updated which would
8681da177e4SLinus Torvalds * cause the base mft record to be redirtied and written out again. (We know
8691da177e4SLinus Torvalds * the access time update will modify the base mft record because Windows
8701da177e4SLinus Torvalds * chkdsk complains if the standard information attribute is not in the base
8711da177e4SLinus Torvalds * mft record.)
8721da177e4SLinus Torvalds *
8731da177e4SLinus Torvalds * If the inode is in icache and not dirty, we attempt to lock the mft record
8741da177e4SLinus Torvalds * and if we find the lock was already taken, it is not safe to write the mft
875c49c3111SRichard Knutsson * record and we return 'false'.
8761da177e4SLinus Torvalds *
8771da177e4SLinus Torvalds * If we manage to obtain the lock we have exclusive access to the mft record,
8781da177e4SLinus Torvalds * which also allows us safe writeout of the mft record. We then set
879c49c3111SRichard Knutsson * @locked_ni to the locked ntfs inode and return 'true'.
8801da177e4SLinus Torvalds *
8811da177e4SLinus Torvalds * Note we cannot just lock the mft record and sleep while waiting for the lock
8821da177e4SLinus Torvalds * because this would deadlock due to lock reversal (normally the mft record is
8831da177e4SLinus Torvalds * locked before the page is locked but we already have the page locked here
8841da177e4SLinus Torvalds * when we try to lock the mft record).
8851da177e4SLinus Torvalds *
8861da177e4SLinus Torvalds * If the inode is not in icache we need to perform further checks.
8871da177e4SLinus Torvalds *
8881da177e4SLinus Torvalds * If the mft record is not a FILE record or it is a base mft record, we can
889c49c3111SRichard Knutsson * safely write it and return 'true'.
8901da177e4SLinus Torvalds *
8911da177e4SLinus Torvalds * We now know the mft record is an extent mft record. We check if the inode
8921da177e4SLinus Torvalds * corresponding to its base mft record is in icache and obtain a reference to
893c49c3111SRichard Knutsson * it if it is. If it is not, we can safely write it and return 'true'.
8941da177e4SLinus Torvalds *
8951da177e4SLinus Torvalds * We now have the base inode for the extent mft record. We check if it has an
8961da177e4SLinus Torvalds * ntfs inode for the extent mft record attached and if not it is safe to write
897c49c3111SRichard Knutsson * the extent mft record and we return 'true'.
8981da177e4SLinus Torvalds *
8991da177e4SLinus Torvalds * The ntfs inode for the extent mft record is attached to the base inode so we
9001da177e4SLinus Torvalds * attempt to lock the extent mft record and if we find the lock was already
901c49c3111SRichard Knutsson * taken, it is not safe to write the extent mft record and we return 'false'.
9021da177e4SLinus Torvalds *
9031da177e4SLinus Torvalds * If we manage to obtain the lock we have exclusive access to the extent mft
9041da177e4SLinus Torvalds * record, which also allows us safe writeout of the extent mft record. We
9051da177e4SLinus Torvalds * set the ntfs inode of the extent mft record clean and then set @locked_ni to
906c49c3111SRichard Knutsson * the now locked ntfs inode and return 'true'.
9071da177e4SLinus Torvalds *
9081da177e4SLinus Torvalds * Note, the reason for actually writing dirty mft records here and not just
9091da177e4SLinus Torvalds * relying on the vfs inode dirty code paths is that we can have mft records
9101da177e4SLinus Torvalds * modified without them ever having actual inodes in memory. Also we can have
9111da177e4SLinus Torvalds * dirty mft records with clean ntfs inodes in memory. None of the described
9121da177e4SLinus Torvalds * cases would result in the dirty mft records being written out if we only
9131da177e4SLinus Torvalds * relied on the vfs inode dirty code paths. And these cases can really occur
9141da177e4SLinus Torvalds * during allocation of new mft records and in particular when the
9151da177e4SLinus Torvalds * initialized_size of the $MFT/$DATA attribute is extended and the new space
9161da177e4SLinus Torvalds * is initialized using ntfs_mft_record_format(). The clean inode can then
9171da177e4SLinus Torvalds * appear if the mft record is reused for a new inode before it got written
9181da177e4SLinus Torvalds * out.
9191da177e4SLinus Torvalds */
ntfs_may_write_mft_record(ntfs_volume * vol,const unsigned long mft_no,const MFT_RECORD * m,ntfs_inode ** locked_ni)920c49c3111SRichard Knutsson bool ntfs_may_write_mft_record(ntfs_volume *vol, const unsigned long mft_no,
9211da177e4SLinus Torvalds const MFT_RECORD *m, ntfs_inode **locked_ni)
9221da177e4SLinus Torvalds {
9231da177e4SLinus Torvalds struct super_block *sb = vol->sb;
9241da177e4SLinus Torvalds struct inode *mft_vi = vol->mft_ino;
9251da177e4SLinus Torvalds struct inode *vi;
9261da177e4SLinus Torvalds ntfs_inode *ni, *eni, **extent_nis;
9271da177e4SLinus Torvalds int i;
9281da177e4SLinus Torvalds ntfs_attr na;
9291da177e4SLinus Torvalds
9301da177e4SLinus Torvalds ntfs_debug("Entering for inode 0x%lx.", mft_no);
9311da177e4SLinus Torvalds /*
9321da177e4SLinus Torvalds * Normally we do not return a locked inode so set @locked_ni to NULL.
9331da177e4SLinus Torvalds */
9341da177e4SLinus Torvalds BUG_ON(!locked_ni);
9351da177e4SLinus Torvalds *locked_ni = NULL;
9361da177e4SLinus Torvalds /*
9371da177e4SLinus Torvalds * Check if the inode corresponding to this mft record is in the VFS
9381da177e4SLinus Torvalds * inode cache and obtain a reference to it if it is.
9391da177e4SLinus Torvalds */
9401da177e4SLinus Torvalds ntfs_debug("Looking for inode 0x%lx in icache.", mft_no);
9411da177e4SLinus Torvalds na.mft_no = mft_no;
9421da177e4SLinus Torvalds na.name = NULL;
9431da177e4SLinus Torvalds na.name_len = 0;
9441da177e4SLinus Torvalds na.type = AT_UNUSED;
9451da177e4SLinus Torvalds /*
946ba6d2377SAnton Altaparmakov * Optimize inode 0, i.e. $MFT itself, since we have it in memory and
947ba6d2377SAnton Altaparmakov * we get here for it rather often.
9481da177e4SLinus Torvalds */
9491da177e4SLinus Torvalds if (!mft_no) {
9501da177e4SLinus Torvalds /* Balance the below iput(). */
9511da177e4SLinus Torvalds vi = igrab(mft_vi);
9521da177e4SLinus Torvalds BUG_ON(vi != mft_vi);
953ba6d2377SAnton Altaparmakov } else {
954ba6d2377SAnton Altaparmakov /*
955ba6d2377SAnton Altaparmakov * Have to use ilookup5_nowait() since ilookup5() waits for the
956ba6d2377SAnton Altaparmakov * inode lock which causes ntfs to deadlock when a concurrent
957ba6d2377SAnton Altaparmakov * inode write via the inode dirty code paths and the page
958ba6d2377SAnton Altaparmakov * dirty code path of the inode dirty code path when writing
959ba6d2377SAnton Altaparmakov * $MFT occurs.
960ba6d2377SAnton Altaparmakov */
9611146f7e2SLuca Stefani vi = ilookup5_nowait(sb, mft_no, ntfs_test_inode, &na);
962ba6d2377SAnton Altaparmakov }
9631da177e4SLinus Torvalds if (vi) {
9641da177e4SLinus Torvalds ntfs_debug("Base inode 0x%lx is in icache.", mft_no);
9651da177e4SLinus Torvalds /* The inode is in icache. */
9661da177e4SLinus Torvalds ni = NTFS_I(vi);
9671da177e4SLinus Torvalds /* Take a reference to the ntfs inode. */
9681da177e4SLinus Torvalds atomic_inc(&ni->count);
9691da177e4SLinus Torvalds /* If the inode is dirty, do not write this record. */
9701da177e4SLinus Torvalds if (NInoDirty(ni)) {
9711da177e4SLinus Torvalds ntfs_debug("Inode 0x%lx is dirty, do not write it.",
9721da177e4SLinus Torvalds mft_no);
9731da177e4SLinus Torvalds atomic_dec(&ni->count);
9741da177e4SLinus Torvalds iput(vi);
975c49c3111SRichard Knutsson return false;
9761da177e4SLinus Torvalds }
9771da177e4SLinus Torvalds ntfs_debug("Inode 0x%lx is not dirty.", mft_no);
9781da177e4SLinus Torvalds /* The inode is not dirty, try to take the mft record lock. */
9794e5e529aSIngo Molnar if (unlikely(!mutex_trylock(&ni->mrec_lock))) {
9801da177e4SLinus Torvalds ntfs_debug("Mft record 0x%lx is already locked, do "
9811da177e4SLinus Torvalds "not write it.", mft_no);
9821da177e4SLinus Torvalds atomic_dec(&ni->count);
9831da177e4SLinus Torvalds iput(vi);
984c49c3111SRichard Knutsson return false;
9851da177e4SLinus Torvalds }
9861da177e4SLinus Torvalds ntfs_debug("Managed to lock mft record 0x%lx, write it.",
9871da177e4SLinus Torvalds mft_no);
9881da177e4SLinus Torvalds /*
9891da177e4SLinus Torvalds * The write has to occur while we hold the mft record lock so
9901da177e4SLinus Torvalds * return the locked ntfs inode.
9911da177e4SLinus Torvalds */
9921da177e4SLinus Torvalds *locked_ni = ni;
993c49c3111SRichard Knutsson return true;
9941da177e4SLinus Torvalds }
9951da177e4SLinus Torvalds ntfs_debug("Inode 0x%lx is not in icache.", mft_no);
9961da177e4SLinus Torvalds /* The inode is not in icache. */
9971da177e4SLinus Torvalds /* Write the record if it is not a mft record (type "FILE"). */
9981da177e4SLinus Torvalds if (!ntfs_is_mft_record(m->magic)) {
9991da177e4SLinus Torvalds ntfs_debug("Mft record 0x%lx is not a FILE record, write it.",
10001da177e4SLinus Torvalds mft_no);
1001c49c3111SRichard Knutsson return true;
10021da177e4SLinus Torvalds }
10031da177e4SLinus Torvalds /* Write the mft record if it is a base inode. */
10041da177e4SLinus Torvalds if (!m->base_mft_record) {
10051da177e4SLinus Torvalds ntfs_debug("Mft record 0x%lx is a base record, write it.",
10061da177e4SLinus Torvalds mft_no);
1007c49c3111SRichard Knutsson return true;
10081da177e4SLinus Torvalds }
10091da177e4SLinus Torvalds /*
10101da177e4SLinus Torvalds * This is an extent mft record. Check if the inode corresponding to
10111da177e4SLinus Torvalds * its base mft record is in icache and obtain a reference to it if it
10121da177e4SLinus Torvalds * is.
10131da177e4SLinus Torvalds */
10141da177e4SLinus Torvalds na.mft_no = MREF_LE(m->base_mft_record);
10151da177e4SLinus Torvalds ntfs_debug("Mft record 0x%lx is an extent record. Looking for base "
10161da177e4SLinus Torvalds "inode 0x%lx in icache.", mft_no, na.mft_no);
1017ba6d2377SAnton Altaparmakov if (!na.mft_no) {
1018ba6d2377SAnton Altaparmakov /* Balance the below iput(). */
1019ba6d2377SAnton Altaparmakov vi = igrab(mft_vi);
1020ba6d2377SAnton Altaparmakov BUG_ON(vi != mft_vi);
1021ba6d2377SAnton Altaparmakov } else
10221146f7e2SLuca Stefani vi = ilookup5_nowait(sb, na.mft_no, ntfs_test_inode,
1023ba6d2377SAnton Altaparmakov &na);
10241da177e4SLinus Torvalds if (!vi) {
10251da177e4SLinus Torvalds /*
10261da177e4SLinus Torvalds * The base inode is not in icache, write this extent mft
10271da177e4SLinus Torvalds * record.
10281da177e4SLinus Torvalds */
10291da177e4SLinus Torvalds ntfs_debug("Base inode 0x%lx is not in icache, write the "
10301da177e4SLinus Torvalds "extent record.", na.mft_no);
1031c49c3111SRichard Knutsson return true;
10321da177e4SLinus Torvalds }
10331da177e4SLinus Torvalds ntfs_debug("Base inode 0x%lx is in icache.", na.mft_no);
10341da177e4SLinus Torvalds /*
10351da177e4SLinus Torvalds * The base inode is in icache. Check if it has the extent inode
10361da177e4SLinus Torvalds * corresponding to this extent mft record attached.
10371da177e4SLinus Torvalds */
10381da177e4SLinus Torvalds ni = NTFS_I(vi);
10394e5e529aSIngo Molnar mutex_lock(&ni->extent_lock);
10401da177e4SLinus Torvalds if (ni->nr_extents <= 0) {
10411da177e4SLinus Torvalds /*
10421da177e4SLinus Torvalds * The base inode has no attached extent inodes, write this
10431da177e4SLinus Torvalds * extent mft record.
10441da177e4SLinus Torvalds */
10454e5e529aSIngo Molnar mutex_unlock(&ni->extent_lock);
10461da177e4SLinus Torvalds iput(vi);
10471da177e4SLinus Torvalds ntfs_debug("Base inode 0x%lx has no attached extent inodes, "
10481da177e4SLinus Torvalds "write the extent record.", na.mft_no);
1049c49c3111SRichard Knutsson return true;
10501da177e4SLinus Torvalds }
10511da177e4SLinus Torvalds /* Iterate over the attached extent inodes. */
10521da177e4SLinus Torvalds extent_nis = ni->ext.extent_ntfs_inos;
10531da177e4SLinus Torvalds for (eni = NULL, i = 0; i < ni->nr_extents; ++i) {
10541da177e4SLinus Torvalds if (mft_no == extent_nis[i]->mft_no) {
10551da177e4SLinus Torvalds /*
10561da177e4SLinus Torvalds * Found the extent inode corresponding to this extent
10571da177e4SLinus Torvalds * mft record.
10581da177e4SLinus Torvalds */
10591da177e4SLinus Torvalds eni = extent_nis[i];
10601da177e4SLinus Torvalds break;
10611da177e4SLinus Torvalds }
10621da177e4SLinus Torvalds }
10631da177e4SLinus Torvalds /*
10641da177e4SLinus Torvalds * If the extent inode was not attached to the base inode, write this
10651da177e4SLinus Torvalds * extent mft record.
10661da177e4SLinus Torvalds */
10671da177e4SLinus Torvalds if (!eni) {
10684e5e529aSIngo Molnar mutex_unlock(&ni->extent_lock);
10691da177e4SLinus Torvalds iput(vi);
10701da177e4SLinus Torvalds ntfs_debug("Extent inode 0x%lx is not attached to its base "
10711da177e4SLinus Torvalds "inode 0x%lx, write the extent record.",
10721da177e4SLinus Torvalds mft_no, na.mft_no);
1073c49c3111SRichard Knutsson return true;
10741da177e4SLinus Torvalds }
10751da177e4SLinus Torvalds ntfs_debug("Extent inode 0x%lx is attached to its base inode 0x%lx.",
10761da177e4SLinus Torvalds mft_no, na.mft_no);
10771da177e4SLinus Torvalds /* Take a reference to the extent ntfs inode. */
10781da177e4SLinus Torvalds atomic_inc(&eni->count);
10794e5e529aSIngo Molnar mutex_unlock(&ni->extent_lock);
10801da177e4SLinus Torvalds /*
10811da177e4SLinus Torvalds * Found the extent inode coresponding to this extent mft record.
10821da177e4SLinus Torvalds * Try to take the mft record lock.
10831da177e4SLinus Torvalds */
10844e5e529aSIngo Molnar if (unlikely(!mutex_trylock(&eni->mrec_lock))) {
10851da177e4SLinus Torvalds atomic_dec(&eni->count);
10861da177e4SLinus Torvalds iput(vi);
10871da177e4SLinus Torvalds ntfs_debug("Extent mft record 0x%lx is already locked, do "
10881da177e4SLinus Torvalds "not write it.", mft_no);
1089c49c3111SRichard Knutsson return false;
10901da177e4SLinus Torvalds }
10911da177e4SLinus Torvalds ntfs_debug("Managed to lock extent mft record 0x%lx, write it.",
10921da177e4SLinus Torvalds mft_no);
10931da177e4SLinus Torvalds if (NInoTestClearDirty(eni))
10941da177e4SLinus Torvalds ntfs_debug("Extent inode 0x%lx is dirty, marking it clean.",
10951da177e4SLinus Torvalds mft_no);
10961da177e4SLinus Torvalds /*
10971da177e4SLinus Torvalds * The write has to occur while we hold the mft record lock so return
10981da177e4SLinus Torvalds * the locked extent ntfs inode.
10991da177e4SLinus Torvalds */
11001da177e4SLinus Torvalds *locked_ni = eni;
1101c49c3111SRichard Knutsson return true;
11021da177e4SLinus Torvalds }
11031da177e4SLinus Torvalds
11041da177e4SLinus Torvalds static const char *es = " Leaving inconsistent metadata. Unmount and run "
11051da177e4SLinus Torvalds "chkdsk.";
11061da177e4SLinus Torvalds
11071da177e4SLinus Torvalds /**
11081da177e4SLinus Torvalds * ntfs_mft_bitmap_find_and_alloc_free_rec_nolock - see name
11091da177e4SLinus Torvalds * @vol: volume on which to search for a free mft record
11101da177e4SLinus Torvalds * @base_ni: open base inode if allocating an extent mft record or NULL
11111da177e4SLinus Torvalds *
11121da177e4SLinus Torvalds * Search for a free mft record in the mft bitmap attribute on the ntfs volume
11131da177e4SLinus Torvalds * @vol.
11141da177e4SLinus Torvalds *
11151da177e4SLinus Torvalds * If @base_ni is NULL start the search at the default allocator position.
11161da177e4SLinus Torvalds *
11171da177e4SLinus Torvalds * If @base_ni is not NULL start the search at the mft record after the base
11181da177e4SLinus Torvalds * mft record @base_ni.
11191da177e4SLinus Torvalds *
11201da177e4SLinus Torvalds * Return the free mft record on success and -errno on error. An error code of
11211da177e4SLinus Torvalds * -ENOSPC means that there are no free mft records in the currently
11221da177e4SLinus Torvalds * initialized mft bitmap.
11231da177e4SLinus Torvalds *
11241da177e4SLinus Torvalds * Locking: Caller must hold vol->mftbmp_lock for writing.
11251da177e4SLinus Torvalds */
ntfs_mft_bitmap_find_and_alloc_free_rec_nolock(ntfs_volume * vol,ntfs_inode * base_ni)11261da177e4SLinus Torvalds static int ntfs_mft_bitmap_find_and_alloc_free_rec_nolock(ntfs_volume *vol,
11271da177e4SLinus Torvalds ntfs_inode *base_ni)
11281da177e4SLinus Torvalds {
11291da177e4SLinus Torvalds s64 pass_end, ll, data_pos, pass_start, ofs, bit;
113007a4e2daSAnton Altaparmakov unsigned long flags;
11311da177e4SLinus Torvalds struct address_space *mftbmp_mapping;
11321da177e4SLinus Torvalds u8 *buf, *byte;
11331da177e4SLinus Torvalds struct page *page;
11341da177e4SLinus Torvalds unsigned int page_ofs, size;
11351da177e4SLinus Torvalds u8 pass, b;
11361da177e4SLinus Torvalds
11371da177e4SLinus Torvalds ntfs_debug("Searching for free mft record in the currently "
11381da177e4SLinus Torvalds "initialized mft bitmap.");
11391da177e4SLinus Torvalds mftbmp_mapping = vol->mftbmp_ino->i_mapping;
11401da177e4SLinus Torvalds /*
11411da177e4SLinus Torvalds * Set the end of the pass making sure we do not overflow the mft
11421da177e4SLinus Torvalds * bitmap.
11431da177e4SLinus Torvalds */
114407a4e2daSAnton Altaparmakov read_lock_irqsave(&NTFS_I(vol->mft_ino)->size_lock, flags);
11451da177e4SLinus Torvalds pass_end = NTFS_I(vol->mft_ino)->allocated_size >>
11461da177e4SLinus Torvalds vol->mft_record_size_bits;
114707a4e2daSAnton Altaparmakov read_unlock_irqrestore(&NTFS_I(vol->mft_ino)->size_lock, flags);
114807a4e2daSAnton Altaparmakov read_lock_irqsave(&NTFS_I(vol->mftbmp_ino)->size_lock, flags);
11491da177e4SLinus Torvalds ll = NTFS_I(vol->mftbmp_ino)->initialized_size << 3;
115007a4e2daSAnton Altaparmakov read_unlock_irqrestore(&NTFS_I(vol->mftbmp_ino)->size_lock, flags);
11511da177e4SLinus Torvalds if (pass_end > ll)
11521da177e4SLinus Torvalds pass_end = ll;
11531da177e4SLinus Torvalds pass = 1;
11541da177e4SLinus Torvalds if (!base_ni)
11551da177e4SLinus Torvalds data_pos = vol->mft_data_pos;
11561da177e4SLinus Torvalds else
11571da177e4SLinus Torvalds data_pos = base_ni->mft_no + 1;
11581da177e4SLinus Torvalds if (data_pos < 24)
11591da177e4SLinus Torvalds data_pos = 24;
11601da177e4SLinus Torvalds if (data_pos >= pass_end) {
11611da177e4SLinus Torvalds data_pos = 24;
11621da177e4SLinus Torvalds pass = 2;
11631da177e4SLinus Torvalds /* This happens on a freshly formatted volume. */
11641da177e4SLinus Torvalds if (data_pos >= pass_end)
11651da177e4SLinus Torvalds return -ENOSPC;
11661da177e4SLinus Torvalds }
11671da177e4SLinus Torvalds pass_start = data_pos;
11681da177e4SLinus Torvalds ntfs_debug("Starting bitmap search: pass %u, pass_start 0x%llx, "
11691da177e4SLinus Torvalds "pass_end 0x%llx, data_pos 0x%llx.", pass,
11701da177e4SLinus Torvalds (long long)pass_start, (long long)pass_end,
11711da177e4SLinus Torvalds (long long)data_pos);
11721da177e4SLinus Torvalds /* Loop until a free mft record is found. */
11731da177e4SLinus Torvalds for (; pass <= 2;) {
11741da177e4SLinus Torvalds /* Cap size to pass_end. */
11751da177e4SLinus Torvalds ofs = data_pos >> 3;
117609cbfeafSKirill A. Shutemov page_ofs = ofs & ~PAGE_MASK;
117709cbfeafSKirill A. Shutemov size = PAGE_SIZE - page_ofs;
11781da177e4SLinus Torvalds ll = ((pass_end + 7) >> 3) - ofs;
11791da177e4SLinus Torvalds if (size > ll)
11801da177e4SLinus Torvalds size = ll;
11811da177e4SLinus Torvalds size <<= 3;
11821da177e4SLinus Torvalds /*
11831da177e4SLinus Torvalds * If we are still within the active pass, search the next page
11841da177e4SLinus Torvalds * for a zero bit.
11851da177e4SLinus Torvalds */
11861da177e4SLinus Torvalds if (size) {
11871da177e4SLinus Torvalds page = ntfs_map_page(mftbmp_mapping,
118809cbfeafSKirill A. Shutemov ofs >> PAGE_SHIFT);
1189801678c5SHirofumi Nakagawa if (IS_ERR(page)) {
11901da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to read mft "
11911da177e4SLinus Torvalds "bitmap, aborting.");
11921da177e4SLinus Torvalds return PTR_ERR(page);
11931da177e4SLinus Torvalds }
11941da177e4SLinus Torvalds buf = (u8*)page_address(page) + page_ofs;
11951da177e4SLinus Torvalds bit = data_pos & 7;
11961da177e4SLinus Torvalds data_pos &= ~7ull;
11971da177e4SLinus Torvalds ntfs_debug("Before inner for loop: size 0x%x, "
11981da177e4SLinus Torvalds "data_pos 0x%llx, bit 0x%llx", size,
11991da177e4SLinus Torvalds (long long)data_pos, (long long)bit);
12001da177e4SLinus Torvalds for (; bit < size && data_pos + bit < pass_end;
12011da177e4SLinus Torvalds bit &= ~7ull, bit += 8) {
12021da177e4SLinus Torvalds byte = buf + (bit >> 3);
12031da177e4SLinus Torvalds if (*byte == 0xff)
12041da177e4SLinus Torvalds continue;
12051da177e4SLinus Torvalds b = ffz((unsigned long)*byte);
12061da177e4SLinus Torvalds if (b < 8 && b >= (bit & 7)) {
12071da177e4SLinus Torvalds ll = data_pos + (bit & ~7ull) + b;
12081da177e4SLinus Torvalds if (unlikely(ll > (1ll << 32))) {
12091da177e4SLinus Torvalds ntfs_unmap_page(page);
12101da177e4SLinus Torvalds return -ENOSPC;
12111da177e4SLinus Torvalds }
12121da177e4SLinus Torvalds *byte |= 1 << b;
12131da177e4SLinus Torvalds flush_dcache_page(page);
12141da177e4SLinus Torvalds set_page_dirty(page);
12151da177e4SLinus Torvalds ntfs_unmap_page(page);
12161da177e4SLinus Torvalds ntfs_debug("Done. (Found and "
12171da177e4SLinus Torvalds "allocated mft record "
12181da177e4SLinus Torvalds "0x%llx.)",
12191da177e4SLinus Torvalds (long long)ll);
12201da177e4SLinus Torvalds return ll;
12211da177e4SLinus Torvalds }
12221da177e4SLinus Torvalds }
12231da177e4SLinus Torvalds ntfs_debug("After inner for loop: size 0x%x, "
12241da177e4SLinus Torvalds "data_pos 0x%llx, bit 0x%llx", size,
12251da177e4SLinus Torvalds (long long)data_pos, (long long)bit);
12261da177e4SLinus Torvalds data_pos += size;
12271da177e4SLinus Torvalds ntfs_unmap_page(page);
12281da177e4SLinus Torvalds /*
12291da177e4SLinus Torvalds * If the end of the pass has not been reached yet,
12301da177e4SLinus Torvalds * continue searching the mft bitmap for a zero bit.
12311da177e4SLinus Torvalds */
12321da177e4SLinus Torvalds if (data_pos < pass_end)
12331da177e4SLinus Torvalds continue;
12341da177e4SLinus Torvalds }
12351da177e4SLinus Torvalds /* Do the next pass. */
12361da177e4SLinus Torvalds if (++pass == 2) {
12371da177e4SLinus Torvalds /*
12381da177e4SLinus Torvalds * Starting the second pass, in which we scan the first
12391da177e4SLinus Torvalds * part of the zone which we omitted earlier.
12401da177e4SLinus Torvalds */
12411da177e4SLinus Torvalds pass_end = pass_start;
12421da177e4SLinus Torvalds data_pos = pass_start = 24;
12431da177e4SLinus Torvalds ntfs_debug("pass %i, pass_start 0x%llx, pass_end "
12441da177e4SLinus Torvalds "0x%llx.", pass, (long long)pass_start,
12451da177e4SLinus Torvalds (long long)pass_end);
12461da177e4SLinus Torvalds if (data_pos >= pass_end)
12471da177e4SLinus Torvalds break;
12481da177e4SLinus Torvalds }
12491da177e4SLinus Torvalds }
12501da177e4SLinus Torvalds /* No free mft records in currently initialized mft bitmap. */
12511da177e4SLinus Torvalds ntfs_debug("Done. (No free mft records left in currently initialized "
12521da177e4SLinus Torvalds "mft bitmap.)");
12531da177e4SLinus Torvalds return -ENOSPC;
12541da177e4SLinus Torvalds }
12551da177e4SLinus Torvalds
12561da177e4SLinus Torvalds /**
12571da177e4SLinus Torvalds * ntfs_mft_bitmap_extend_allocation_nolock - extend mft bitmap by a cluster
12581da177e4SLinus Torvalds * @vol: volume on which to extend the mft bitmap attribute
12591da177e4SLinus Torvalds *
12601da177e4SLinus Torvalds * Extend the mft bitmap attribute on the ntfs volume @vol by one cluster.
12611da177e4SLinus Torvalds *
12621da177e4SLinus Torvalds * Note: Only changes allocated_size, i.e. does not touch initialized_size or
12631da177e4SLinus Torvalds * data_size.
12641da177e4SLinus Torvalds *
12651da177e4SLinus Torvalds * Return 0 on success and -errno on error.
12661da177e4SLinus Torvalds *
12671da177e4SLinus Torvalds * Locking: - Caller must hold vol->mftbmp_lock for writing.
12681da177e4SLinus Torvalds * - This function takes NTFS_I(vol->mftbmp_ino)->runlist.lock for
12691da177e4SLinus Torvalds * writing and releases it before returning.
12701da177e4SLinus Torvalds * - This function takes vol->lcnbmp_lock for writing and releases it
12711da177e4SLinus Torvalds * before returning.
12721da177e4SLinus Torvalds */
ntfs_mft_bitmap_extend_allocation_nolock(ntfs_volume * vol)12731da177e4SLinus Torvalds static int ntfs_mft_bitmap_extend_allocation_nolock(ntfs_volume *vol)
12741da177e4SLinus Torvalds {
12751da177e4SLinus Torvalds LCN lcn;
12761da177e4SLinus Torvalds s64 ll;
127707a4e2daSAnton Altaparmakov unsigned long flags;
12781da177e4SLinus Torvalds struct page *page;
12791da177e4SLinus Torvalds ntfs_inode *mft_ni, *mftbmp_ni;
12801da177e4SLinus Torvalds runlist_element *rl, *rl2 = NULL;
12811da177e4SLinus Torvalds ntfs_attr_search_ctx *ctx = NULL;
12821da177e4SLinus Torvalds MFT_RECORD *mrec;
12831da177e4SLinus Torvalds ATTR_RECORD *a = NULL;
12841da177e4SLinus Torvalds int ret, mp_size;
12851da177e4SLinus Torvalds u32 old_alen = 0;
12861da177e4SLinus Torvalds u8 *b, tb;
12871da177e4SLinus Torvalds struct {
12881da177e4SLinus Torvalds u8 added_cluster:1;
12891da177e4SLinus Torvalds u8 added_run:1;
12901da177e4SLinus Torvalds u8 mp_rebuilt:1;
12911da177e4SLinus Torvalds } status = { 0, 0, 0 };
12921da177e4SLinus Torvalds
12931da177e4SLinus Torvalds ntfs_debug("Extending mft bitmap allocation.");
12941da177e4SLinus Torvalds mft_ni = NTFS_I(vol->mft_ino);
12951da177e4SLinus Torvalds mftbmp_ni = NTFS_I(vol->mftbmp_ino);
12961da177e4SLinus Torvalds /*
12971da177e4SLinus Torvalds * Determine the last lcn of the mft bitmap. The allocated size of the
12981da177e4SLinus Torvalds * mft bitmap cannot be zero so we are ok to do this.
12991da177e4SLinus Torvalds */
1300b6ad6c52SAnton Altaparmakov down_write(&mftbmp_ni->runlist.lock);
130107a4e2daSAnton Altaparmakov read_lock_irqsave(&mftbmp_ni->size_lock, flags);
130207a4e2daSAnton Altaparmakov ll = mftbmp_ni->allocated_size;
130307a4e2daSAnton Altaparmakov read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
1304c0c1cc0eSAnton Altaparmakov rl = ntfs_attr_find_vcn_nolock(mftbmp_ni,
130569b41e3cSAnton Altaparmakov (ll - 1) >> vol->cluster_size_bits, NULL);
1306cc22c800SDenis Efremov if (IS_ERR(rl) || unlikely(!rl->length || rl->lcn < 0)) {
1307b6ad6c52SAnton Altaparmakov up_write(&mftbmp_ni->runlist.lock);
13081da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to determine last allocated "
13091da177e4SLinus Torvalds "cluster of mft bitmap attribute.");
1310b6ad6c52SAnton Altaparmakov if (!IS_ERR(rl))
13111da177e4SLinus Torvalds ret = -EIO;
1312b6ad6c52SAnton Altaparmakov else
13131da177e4SLinus Torvalds ret = PTR_ERR(rl);
13141da177e4SLinus Torvalds return ret;
13151da177e4SLinus Torvalds }
13161da177e4SLinus Torvalds lcn = rl->lcn + rl->length;
13171da177e4SLinus Torvalds ntfs_debug("Last lcn of mft bitmap attribute is 0x%llx.",
13181da177e4SLinus Torvalds (long long)lcn);
13191da177e4SLinus Torvalds /*
13201da177e4SLinus Torvalds * Attempt to get the cluster following the last allocated cluster by
13211da177e4SLinus Torvalds * hand as it may be in the MFT zone so the allocator would not give it
13221da177e4SLinus Torvalds * to us.
13231da177e4SLinus Torvalds */
13241da177e4SLinus Torvalds ll = lcn >> 3;
13251da177e4SLinus Torvalds page = ntfs_map_page(vol->lcnbmp_ino->i_mapping,
132609cbfeafSKirill A. Shutemov ll >> PAGE_SHIFT);
13271da177e4SLinus Torvalds if (IS_ERR(page)) {
13281da177e4SLinus Torvalds up_write(&mftbmp_ni->runlist.lock);
13291da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to read from lcn bitmap.");
13301da177e4SLinus Torvalds return PTR_ERR(page);
13311da177e4SLinus Torvalds }
133209cbfeafSKirill A. Shutemov b = (u8*)page_address(page) + (ll & ~PAGE_MASK);
13331da177e4SLinus Torvalds tb = 1 << (lcn & 7ull);
13341da177e4SLinus Torvalds down_write(&vol->lcnbmp_lock);
13351da177e4SLinus Torvalds if (*b != 0xff && !(*b & tb)) {
13361da177e4SLinus Torvalds /* Next cluster is free, allocate it. */
13371da177e4SLinus Torvalds *b |= tb;
13381da177e4SLinus Torvalds flush_dcache_page(page);
13391da177e4SLinus Torvalds set_page_dirty(page);
13401da177e4SLinus Torvalds up_write(&vol->lcnbmp_lock);
13411da177e4SLinus Torvalds ntfs_unmap_page(page);
13421da177e4SLinus Torvalds /* Update the mft bitmap runlist. */
13431da177e4SLinus Torvalds rl->length++;
13441da177e4SLinus Torvalds rl[1].vcn++;
13451da177e4SLinus Torvalds status.added_cluster = 1;
13461da177e4SLinus Torvalds ntfs_debug("Appending one cluster to mft bitmap.");
13471da177e4SLinus Torvalds } else {
13481da177e4SLinus Torvalds up_write(&vol->lcnbmp_lock);
13491da177e4SLinus Torvalds ntfs_unmap_page(page);
13501da177e4SLinus Torvalds /* Allocate a cluster from the DATA_ZONE. */
1351fc0fa7dcSAnton Altaparmakov rl2 = ntfs_cluster_alloc(vol, rl[1].vcn, 1, lcn, DATA_ZONE,
1352c49c3111SRichard Knutsson true);
13531da177e4SLinus Torvalds if (IS_ERR(rl2)) {
13541da177e4SLinus Torvalds up_write(&mftbmp_ni->runlist.lock);
13551da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to allocate a cluster for "
13561da177e4SLinus Torvalds "the mft bitmap.");
13571da177e4SLinus Torvalds return PTR_ERR(rl2);
13581da177e4SLinus Torvalds }
13591da177e4SLinus Torvalds rl = ntfs_runlists_merge(mftbmp_ni->runlist.rl, rl2);
13601da177e4SLinus Torvalds if (IS_ERR(rl)) {
13611da177e4SLinus Torvalds up_write(&mftbmp_ni->runlist.lock);
13621da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to merge runlists for mft "
13631da177e4SLinus Torvalds "bitmap.");
13641da177e4SLinus Torvalds if (ntfs_cluster_free_from_rl(vol, rl2)) {
13659b556248SAnton Altaparmakov ntfs_error(vol->sb, "Failed to deallocate "
13661da177e4SLinus Torvalds "allocated cluster.%s", es);
13671da177e4SLinus Torvalds NVolSetErrors(vol);
13681da177e4SLinus Torvalds }
13691da177e4SLinus Torvalds ntfs_free(rl2);
13701da177e4SLinus Torvalds return PTR_ERR(rl);
13711da177e4SLinus Torvalds }
13721da177e4SLinus Torvalds mftbmp_ni->runlist.rl = rl;
13731da177e4SLinus Torvalds status.added_run = 1;
13741da177e4SLinus Torvalds ntfs_debug("Adding one run to mft bitmap.");
13751da177e4SLinus Torvalds /* Find the last run in the new runlist. */
13761da177e4SLinus Torvalds for (; rl[1].length; rl++)
13771da177e4SLinus Torvalds ;
13781da177e4SLinus Torvalds }
13791da177e4SLinus Torvalds /*
13801da177e4SLinus Torvalds * Update the attribute record as well. Note: @rl is the last
13811da177e4SLinus Torvalds * (non-terminator) runlist element of mft bitmap.
13821da177e4SLinus Torvalds */
13831da177e4SLinus Torvalds mrec = map_mft_record(mft_ni);
13841da177e4SLinus Torvalds if (IS_ERR(mrec)) {
13851da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to map mft record.");
13861da177e4SLinus Torvalds ret = PTR_ERR(mrec);
13871da177e4SLinus Torvalds goto undo_alloc;
13881da177e4SLinus Torvalds }
13891da177e4SLinus Torvalds ctx = ntfs_attr_get_search_ctx(mft_ni, mrec);
13901da177e4SLinus Torvalds if (unlikely(!ctx)) {
13911da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to get search context.");
13921da177e4SLinus Torvalds ret = -ENOMEM;
13931da177e4SLinus Torvalds goto undo_alloc;
13941da177e4SLinus Torvalds }
13951da177e4SLinus Torvalds ret = ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name,
13961da177e4SLinus Torvalds mftbmp_ni->name_len, CASE_SENSITIVE, rl[1].vcn, NULL,
13971da177e4SLinus Torvalds 0, ctx);
13981da177e4SLinus Torvalds if (unlikely(ret)) {
13991da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to find last attribute extent of "
14001da177e4SLinus Torvalds "mft bitmap attribute.");
14011da177e4SLinus Torvalds if (ret == -ENOENT)
14021da177e4SLinus Torvalds ret = -EIO;
14031da177e4SLinus Torvalds goto undo_alloc;
14041da177e4SLinus Torvalds }
14051da177e4SLinus Torvalds a = ctx->attr;
14061da177e4SLinus Torvalds ll = sle64_to_cpu(a->data.non_resident.lowest_vcn);
14071da177e4SLinus Torvalds /* Search back for the previous last allocated cluster of mft bitmap. */
14081da177e4SLinus Torvalds for (rl2 = rl; rl2 > mftbmp_ni->runlist.rl; rl2--) {
14091da177e4SLinus Torvalds if (ll >= rl2->vcn)
14101da177e4SLinus Torvalds break;
14111da177e4SLinus Torvalds }
14121da177e4SLinus Torvalds BUG_ON(ll < rl2->vcn);
14131da177e4SLinus Torvalds BUG_ON(ll >= rl2->vcn + rl2->length);
14141da177e4SLinus Torvalds /* Get the size for the new mapping pairs array for this extent. */
1415fa3be923SAnton Altaparmakov mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, -1);
14161da177e4SLinus Torvalds if (unlikely(mp_size <= 0)) {
14171da177e4SLinus Torvalds ntfs_error(vol->sb, "Get size for mapping pairs failed for "
14181da177e4SLinus Torvalds "mft bitmap attribute extent.");
14191da177e4SLinus Torvalds ret = mp_size;
14201da177e4SLinus Torvalds if (!ret)
14211da177e4SLinus Torvalds ret = -EIO;
14221da177e4SLinus Torvalds goto undo_alloc;
14231da177e4SLinus Torvalds }
14241da177e4SLinus Torvalds /* Expand the attribute record if necessary. */
14251da177e4SLinus Torvalds old_alen = le32_to_cpu(a->length);
14261da177e4SLinus Torvalds ret = ntfs_attr_record_resize(ctx->mrec, a, mp_size +
14271da177e4SLinus Torvalds le16_to_cpu(a->data.non_resident.mapping_pairs_offset));
14281da177e4SLinus Torvalds if (unlikely(ret)) {
14291da177e4SLinus Torvalds if (ret != -ENOSPC) {
14301da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to resize attribute "
14311da177e4SLinus Torvalds "record for mft bitmap attribute.");
14321da177e4SLinus Torvalds goto undo_alloc;
14331da177e4SLinus Torvalds }
14341da177e4SLinus Torvalds // TODO: Deal with this by moving this extent to a new mft
14351da177e4SLinus Torvalds // record or by starting a new extent in a new mft record or by
14361da177e4SLinus Torvalds // moving other attributes out of this mft record.
1437b6ad6c52SAnton Altaparmakov // Note: It will need to be a special mft record and if none of
1438b6ad6c52SAnton Altaparmakov // those are available it gets rather complicated...
14391da177e4SLinus Torvalds ntfs_error(vol->sb, "Not enough space in this mft record to "
144025985edcSLucas De Marchi "accommodate extended mft bitmap attribute "
14411da177e4SLinus Torvalds "extent. Cannot handle this yet.");
14421da177e4SLinus Torvalds ret = -EOPNOTSUPP;
14431da177e4SLinus Torvalds goto undo_alloc;
14441da177e4SLinus Torvalds }
14451da177e4SLinus Torvalds status.mp_rebuilt = 1;
14461da177e4SLinus Torvalds /* Generate the mapping pairs array directly into the attr record. */
14471da177e4SLinus Torvalds ret = ntfs_mapping_pairs_build(vol, (u8*)a +
14481da177e4SLinus Torvalds le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
1449fa3be923SAnton Altaparmakov mp_size, rl2, ll, -1, NULL);
14501da177e4SLinus Torvalds if (unlikely(ret)) {
14511da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to build mapping pairs array for "
14521da177e4SLinus Torvalds "mft bitmap attribute.");
14531da177e4SLinus Torvalds goto undo_alloc;
14541da177e4SLinus Torvalds }
14551da177e4SLinus Torvalds /* Update the highest_vcn. */
14561da177e4SLinus Torvalds a->data.non_resident.highest_vcn = cpu_to_sle64(rl[1].vcn - 1);
14571da177e4SLinus Torvalds /*
14581da177e4SLinus Torvalds * We now have extended the mft bitmap allocated_size by one cluster.
14591da177e4SLinus Torvalds * Reflect this in the ntfs_inode structure and the attribute record.
14601da177e4SLinus Torvalds */
14611da177e4SLinus Torvalds if (a->data.non_resident.lowest_vcn) {
14621da177e4SLinus Torvalds /*
14631da177e4SLinus Torvalds * We are not in the first attribute extent, switch to it, but
14641da177e4SLinus Torvalds * first ensure the changes will make it to disk later.
14651da177e4SLinus Torvalds */
14661da177e4SLinus Torvalds flush_dcache_mft_record_page(ctx->ntfs_ino);
14671da177e4SLinus Torvalds mark_mft_record_dirty(ctx->ntfs_ino);
14681da177e4SLinus Torvalds ntfs_attr_reinit_search_ctx(ctx);
14691da177e4SLinus Torvalds ret = ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name,
14701da177e4SLinus Torvalds mftbmp_ni->name_len, CASE_SENSITIVE, 0, NULL,
14711da177e4SLinus Torvalds 0, ctx);
14721da177e4SLinus Torvalds if (unlikely(ret)) {
14731da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to find first attribute "
14741da177e4SLinus Torvalds "extent of mft bitmap attribute.");
14751da177e4SLinus Torvalds goto restore_undo_alloc;
14761da177e4SLinus Torvalds }
14771da177e4SLinus Torvalds a = ctx->attr;
14781da177e4SLinus Torvalds }
147907a4e2daSAnton Altaparmakov write_lock_irqsave(&mftbmp_ni->size_lock, flags);
14801da177e4SLinus Torvalds mftbmp_ni->allocated_size += vol->cluster_size;
14811da177e4SLinus Torvalds a->data.non_resident.allocated_size =
14821da177e4SLinus Torvalds cpu_to_sle64(mftbmp_ni->allocated_size);
148307a4e2daSAnton Altaparmakov write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
14841da177e4SLinus Torvalds /* Ensure the changes make it to disk. */
14851da177e4SLinus Torvalds flush_dcache_mft_record_page(ctx->ntfs_ino);
14861da177e4SLinus Torvalds mark_mft_record_dirty(ctx->ntfs_ino);
14871da177e4SLinus Torvalds ntfs_attr_put_search_ctx(ctx);
14881da177e4SLinus Torvalds unmap_mft_record(mft_ni);
14891da177e4SLinus Torvalds up_write(&mftbmp_ni->runlist.lock);
14901da177e4SLinus Torvalds ntfs_debug("Done.");
14911da177e4SLinus Torvalds return 0;
14921da177e4SLinus Torvalds restore_undo_alloc:
14931da177e4SLinus Torvalds ntfs_attr_reinit_search_ctx(ctx);
14941da177e4SLinus Torvalds if (ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name,
14951da177e4SLinus Torvalds mftbmp_ni->name_len, CASE_SENSITIVE, rl[1].vcn, NULL,
14961da177e4SLinus Torvalds 0, ctx)) {
14971da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to find last attribute extent of "
14981da177e4SLinus Torvalds "mft bitmap attribute.%s", es);
149907a4e2daSAnton Altaparmakov write_lock_irqsave(&mftbmp_ni->size_lock, flags);
15001da177e4SLinus Torvalds mftbmp_ni->allocated_size += vol->cluster_size;
150107a4e2daSAnton Altaparmakov write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
15021da177e4SLinus Torvalds ntfs_attr_put_search_ctx(ctx);
15031da177e4SLinus Torvalds unmap_mft_record(mft_ni);
15041da177e4SLinus Torvalds up_write(&mftbmp_ni->runlist.lock);
15051da177e4SLinus Torvalds /*
15061da177e4SLinus Torvalds * The only thing that is now wrong is ->allocated_size of the
15071da177e4SLinus Torvalds * base attribute extent which chkdsk should be able to fix.
15081da177e4SLinus Torvalds */
15091da177e4SLinus Torvalds NVolSetErrors(vol);
15101da177e4SLinus Torvalds return ret;
15111da177e4SLinus Torvalds }
15121da177e4SLinus Torvalds a = ctx->attr;
15131da177e4SLinus Torvalds a->data.non_resident.highest_vcn = cpu_to_sle64(rl[1].vcn - 2);
15141da177e4SLinus Torvalds undo_alloc:
15151da177e4SLinus Torvalds if (status.added_cluster) {
15161da177e4SLinus Torvalds /* Truncate the last run in the runlist by one cluster. */
15171da177e4SLinus Torvalds rl->length--;
15181da177e4SLinus Torvalds rl[1].vcn--;
15191da177e4SLinus Torvalds } else if (status.added_run) {
15201da177e4SLinus Torvalds lcn = rl->lcn;
15211da177e4SLinus Torvalds /* Remove the last run from the runlist. */
15221da177e4SLinus Torvalds rl->lcn = rl[1].lcn;
15231da177e4SLinus Torvalds rl->length = 0;
15241da177e4SLinus Torvalds }
15251da177e4SLinus Torvalds /* Deallocate the cluster. */
15261da177e4SLinus Torvalds down_write(&vol->lcnbmp_lock);
15271da177e4SLinus Torvalds if (ntfs_bitmap_clear_bit(vol->lcnbmp_ino, lcn)) {
15281da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to free allocated cluster.%s", es);
15291da177e4SLinus Torvalds NVolSetErrors(vol);
15301da177e4SLinus Torvalds }
15311da177e4SLinus Torvalds up_write(&vol->lcnbmp_lock);
15321da177e4SLinus Torvalds if (status.mp_rebuilt) {
15331da177e4SLinus Torvalds if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu(
15341da177e4SLinus Torvalds a->data.non_resident.mapping_pairs_offset),
15351da177e4SLinus Torvalds old_alen - le16_to_cpu(
15361da177e4SLinus Torvalds a->data.non_resident.mapping_pairs_offset),
1537fa3be923SAnton Altaparmakov rl2, ll, -1, NULL)) {
15381da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to restore mapping pairs "
15391da177e4SLinus Torvalds "array.%s", es);
15401da177e4SLinus Torvalds NVolSetErrors(vol);
15411da177e4SLinus Torvalds }
15421da177e4SLinus Torvalds if (ntfs_attr_record_resize(ctx->mrec, a, old_alen)) {
15431da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to restore attribute "
15441da177e4SLinus Torvalds "record.%s", es);
15451da177e4SLinus Torvalds NVolSetErrors(vol);
15461da177e4SLinus Torvalds }
15471da177e4SLinus Torvalds flush_dcache_mft_record_page(ctx->ntfs_ino);
15481da177e4SLinus Torvalds mark_mft_record_dirty(ctx->ntfs_ino);
15491da177e4SLinus Torvalds }
15501da177e4SLinus Torvalds if (ctx)
15511da177e4SLinus Torvalds ntfs_attr_put_search_ctx(ctx);
15521da177e4SLinus Torvalds if (!IS_ERR(mrec))
15531da177e4SLinus Torvalds unmap_mft_record(mft_ni);
15541da177e4SLinus Torvalds up_write(&mftbmp_ni->runlist.lock);
15551da177e4SLinus Torvalds return ret;
15561da177e4SLinus Torvalds }
15571da177e4SLinus Torvalds
15581da177e4SLinus Torvalds /**
15591da177e4SLinus Torvalds * ntfs_mft_bitmap_extend_initialized_nolock - extend mftbmp initialized data
15601da177e4SLinus Torvalds * @vol: volume on which to extend the mft bitmap attribute
15611da177e4SLinus Torvalds *
15621da177e4SLinus Torvalds * Extend the initialized portion of the mft bitmap attribute on the ntfs
15631da177e4SLinus Torvalds * volume @vol by 8 bytes.
15641da177e4SLinus Torvalds *
15651da177e4SLinus Torvalds * Note: Only changes initialized_size and data_size, i.e. requires that
15661da177e4SLinus Torvalds * allocated_size is big enough to fit the new initialized_size.
15671da177e4SLinus Torvalds *
15681da177e4SLinus Torvalds * Return 0 on success and -error on error.
15691da177e4SLinus Torvalds *
15701da177e4SLinus Torvalds * Locking: Caller must hold vol->mftbmp_lock for writing.
15711da177e4SLinus Torvalds */
ntfs_mft_bitmap_extend_initialized_nolock(ntfs_volume * vol)15721da177e4SLinus Torvalds static int ntfs_mft_bitmap_extend_initialized_nolock(ntfs_volume *vol)
15731da177e4SLinus Torvalds {
15741da177e4SLinus Torvalds s64 old_data_size, old_initialized_size;
157507a4e2daSAnton Altaparmakov unsigned long flags;
15761da177e4SLinus Torvalds struct inode *mftbmp_vi;
15771da177e4SLinus Torvalds ntfs_inode *mft_ni, *mftbmp_ni;
15781da177e4SLinus Torvalds ntfs_attr_search_ctx *ctx;
15791da177e4SLinus Torvalds MFT_RECORD *mrec;
15801da177e4SLinus Torvalds ATTR_RECORD *a;
15811da177e4SLinus Torvalds int ret;
15821da177e4SLinus Torvalds
15831da177e4SLinus Torvalds ntfs_debug("Extending mft bitmap initiailized (and data) size.");
15841da177e4SLinus Torvalds mft_ni = NTFS_I(vol->mft_ino);
15851da177e4SLinus Torvalds mftbmp_vi = vol->mftbmp_ino;
15861da177e4SLinus Torvalds mftbmp_ni = NTFS_I(mftbmp_vi);
15871da177e4SLinus Torvalds /* Get the attribute record. */
15881da177e4SLinus Torvalds mrec = map_mft_record(mft_ni);
15891da177e4SLinus Torvalds if (IS_ERR(mrec)) {
15901da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to map mft record.");
15911da177e4SLinus Torvalds return PTR_ERR(mrec);
15921da177e4SLinus Torvalds }
15931da177e4SLinus Torvalds ctx = ntfs_attr_get_search_ctx(mft_ni, mrec);
15941da177e4SLinus Torvalds if (unlikely(!ctx)) {
15951da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to get search context.");
15961da177e4SLinus Torvalds ret = -ENOMEM;
15971da177e4SLinus Torvalds goto unm_err_out;
15981da177e4SLinus Torvalds }
15991da177e4SLinus Torvalds ret = ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name,
16001da177e4SLinus Torvalds mftbmp_ni->name_len, CASE_SENSITIVE, 0, NULL, 0, ctx);
16011da177e4SLinus Torvalds if (unlikely(ret)) {
16021da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to find first attribute extent of "
16031da177e4SLinus Torvalds "mft bitmap attribute.");
16041da177e4SLinus Torvalds if (ret == -ENOENT)
16051da177e4SLinus Torvalds ret = -EIO;
16061da177e4SLinus Torvalds goto put_err_out;
16071da177e4SLinus Torvalds }
16081da177e4SLinus Torvalds a = ctx->attr;
160907a4e2daSAnton Altaparmakov write_lock_irqsave(&mftbmp_ni->size_lock, flags);
161007a4e2daSAnton Altaparmakov old_data_size = i_size_read(mftbmp_vi);
16111da177e4SLinus Torvalds old_initialized_size = mftbmp_ni->initialized_size;
16121da177e4SLinus Torvalds /*
16131da177e4SLinus Torvalds * We can simply update the initialized_size before filling the space
16141da177e4SLinus Torvalds * with zeroes because the caller is holding the mft bitmap lock for
16151da177e4SLinus Torvalds * writing which ensures that no one else is trying to access the data.
16161da177e4SLinus Torvalds */
16171da177e4SLinus Torvalds mftbmp_ni->initialized_size += 8;
16181da177e4SLinus Torvalds a->data.non_resident.initialized_size =
16191da177e4SLinus Torvalds cpu_to_sle64(mftbmp_ni->initialized_size);
162007a4e2daSAnton Altaparmakov if (mftbmp_ni->initialized_size > old_data_size) {
162107a4e2daSAnton Altaparmakov i_size_write(mftbmp_vi, mftbmp_ni->initialized_size);
16221da177e4SLinus Torvalds a->data.non_resident.data_size =
162307a4e2daSAnton Altaparmakov cpu_to_sle64(mftbmp_ni->initialized_size);
16241da177e4SLinus Torvalds }
162507a4e2daSAnton Altaparmakov write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
16261da177e4SLinus Torvalds /* Ensure the changes make it to disk. */
16271da177e4SLinus Torvalds flush_dcache_mft_record_page(ctx->ntfs_ino);
16281da177e4SLinus Torvalds mark_mft_record_dirty(ctx->ntfs_ino);
16291da177e4SLinus Torvalds ntfs_attr_put_search_ctx(ctx);
16301da177e4SLinus Torvalds unmap_mft_record(mft_ni);
16311da177e4SLinus Torvalds /* Initialize the mft bitmap attribute value with zeroes. */
16321da177e4SLinus Torvalds ret = ntfs_attr_set(mftbmp_ni, old_initialized_size, 8, 0);
16331da177e4SLinus Torvalds if (likely(!ret)) {
16341da177e4SLinus Torvalds ntfs_debug("Done. (Wrote eight initialized bytes to mft "
16351da177e4SLinus Torvalds "bitmap.");
16361da177e4SLinus Torvalds return 0;
16371da177e4SLinus Torvalds }
16381da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to write to mft bitmap.");
16391da177e4SLinus Torvalds /* Try to recover from the error. */
16401da177e4SLinus Torvalds mrec = map_mft_record(mft_ni);
16411da177e4SLinus Torvalds if (IS_ERR(mrec)) {
16421da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to map mft record.%s", es);
16431da177e4SLinus Torvalds NVolSetErrors(vol);
16441da177e4SLinus Torvalds return ret;
16451da177e4SLinus Torvalds }
16461da177e4SLinus Torvalds ctx = ntfs_attr_get_search_ctx(mft_ni, mrec);
16471da177e4SLinus Torvalds if (unlikely(!ctx)) {
16481da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to get search context.%s", es);
16491da177e4SLinus Torvalds NVolSetErrors(vol);
16501da177e4SLinus Torvalds goto unm_err_out;
16511da177e4SLinus Torvalds }
16521da177e4SLinus Torvalds if (ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name,
16531da177e4SLinus Torvalds mftbmp_ni->name_len, CASE_SENSITIVE, 0, NULL, 0, ctx)) {
16541da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to find first attribute extent of "
16551da177e4SLinus Torvalds "mft bitmap attribute.%s", es);
16561da177e4SLinus Torvalds NVolSetErrors(vol);
16571da177e4SLinus Torvalds put_err_out:
16581da177e4SLinus Torvalds ntfs_attr_put_search_ctx(ctx);
16591da177e4SLinus Torvalds unm_err_out:
16601da177e4SLinus Torvalds unmap_mft_record(mft_ni);
16611da177e4SLinus Torvalds goto err_out;
16621da177e4SLinus Torvalds }
16631da177e4SLinus Torvalds a = ctx->attr;
166407a4e2daSAnton Altaparmakov write_lock_irqsave(&mftbmp_ni->size_lock, flags);
16651da177e4SLinus Torvalds mftbmp_ni->initialized_size = old_initialized_size;
16661da177e4SLinus Torvalds a->data.non_resident.initialized_size =
16671da177e4SLinus Torvalds cpu_to_sle64(old_initialized_size);
166807a4e2daSAnton Altaparmakov if (i_size_read(mftbmp_vi) != old_data_size) {
166907a4e2daSAnton Altaparmakov i_size_write(mftbmp_vi, old_data_size);
16701da177e4SLinus Torvalds a->data.non_resident.data_size = cpu_to_sle64(old_data_size);
16711da177e4SLinus Torvalds }
167207a4e2daSAnton Altaparmakov write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
16731da177e4SLinus Torvalds flush_dcache_mft_record_page(ctx->ntfs_ino);
16741da177e4SLinus Torvalds mark_mft_record_dirty(ctx->ntfs_ino);
16751da177e4SLinus Torvalds ntfs_attr_put_search_ctx(ctx);
16761da177e4SLinus Torvalds unmap_mft_record(mft_ni);
167707a4e2daSAnton Altaparmakov #ifdef DEBUG
167807a4e2daSAnton Altaparmakov read_lock_irqsave(&mftbmp_ni->size_lock, flags);
16791da177e4SLinus Torvalds ntfs_debug("Restored status of mftbmp: allocated_size 0x%llx, "
16801da177e4SLinus Torvalds "data_size 0x%llx, initialized_size 0x%llx.",
16811da177e4SLinus Torvalds (long long)mftbmp_ni->allocated_size,
168207a4e2daSAnton Altaparmakov (long long)i_size_read(mftbmp_vi),
16831da177e4SLinus Torvalds (long long)mftbmp_ni->initialized_size);
168407a4e2daSAnton Altaparmakov read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
168507a4e2daSAnton Altaparmakov #endif /* DEBUG */
16861da177e4SLinus Torvalds err_out:
16871da177e4SLinus Torvalds return ret;
16881da177e4SLinus Torvalds }
16891da177e4SLinus Torvalds
16901da177e4SLinus Torvalds /**
16911da177e4SLinus Torvalds * ntfs_mft_data_extend_allocation_nolock - extend mft data attribute
16921da177e4SLinus Torvalds * @vol: volume on which to extend the mft data attribute
16931da177e4SLinus Torvalds *
16941da177e4SLinus Torvalds * Extend the mft data attribute on the ntfs volume @vol by 16 mft records
16951da177e4SLinus Torvalds * worth of clusters or if not enough space for this by one mft record worth
16961da177e4SLinus Torvalds * of clusters.
16971da177e4SLinus Torvalds *
16981da177e4SLinus Torvalds * Note: Only changes allocated_size, i.e. does not touch initialized_size or
16991da177e4SLinus Torvalds * data_size.
17001da177e4SLinus Torvalds *
17011da177e4SLinus Torvalds * Return 0 on success and -errno on error.
17021da177e4SLinus Torvalds *
17031da177e4SLinus Torvalds * Locking: - Caller must hold vol->mftbmp_lock for writing.
17041da177e4SLinus Torvalds * - This function takes NTFS_I(vol->mft_ino)->runlist.lock for
17051da177e4SLinus Torvalds * writing and releases it before returning.
17061da177e4SLinus Torvalds * - This function calls functions which take vol->lcnbmp_lock for
17071da177e4SLinus Torvalds * writing and release it before returning.
17081da177e4SLinus Torvalds */
ntfs_mft_data_extend_allocation_nolock(ntfs_volume * vol)17091da177e4SLinus Torvalds static int ntfs_mft_data_extend_allocation_nolock(ntfs_volume *vol)
17101da177e4SLinus Torvalds {
17111da177e4SLinus Torvalds LCN lcn;
17121da177e4SLinus Torvalds VCN old_last_vcn;
171307a4e2daSAnton Altaparmakov s64 min_nr, nr, ll;
171407a4e2daSAnton Altaparmakov unsigned long flags;
17151da177e4SLinus Torvalds ntfs_inode *mft_ni;
17161da177e4SLinus Torvalds runlist_element *rl, *rl2;
17171da177e4SLinus Torvalds ntfs_attr_search_ctx *ctx = NULL;
17181da177e4SLinus Torvalds MFT_RECORD *mrec;
17191da177e4SLinus Torvalds ATTR_RECORD *a = NULL;
17201da177e4SLinus Torvalds int ret, mp_size;
17211da177e4SLinus Torvalds u32 old_alen = 0;
1722c49c3111SRichard Knutsson bool mp_rebuilt = false;
17231da177e4SLinus Torvalds
17241da177e4SLinus Torvalds ntfs_debug("Extending mft data allocation.");
17251da177e4SLinus Torvalds mft_ni = NTFS_I(vol->mft_ino);
17261da177e4SLinus Torvalds /*
17271da177e4SLinus Torvalds * Determine the preferred allocation location, i.e. the last lcn of
17281da177e4SLinus Torvalds * the mft data attribute. The allocated size of the mft data
17291da177e4SLinus Torvalds * attribute cannot be zero so we are ok to do this.
17301da177e4SLinus Torvalds */
1731b6ad6c52SAnton Altaparmakov down_write(&mft_ni->runlist.lock);
173207a4e2daSAnton Altaparmakov read_lock_irqsave(&mft_ni->size_lock, flags);
173307a4e2daSAnton Altaparmakov ll = mft_ni->allocated_size;
173407a4e2daSAnton Altaparmakov read_unlock_irqrestore(&mft_ni->size_lock, flags);
1735c0c1cc0eSAnton Altaparmakov rl = ntfs_attr_find_vcn_nolock(mft_ni,
173669b41e3cSAnton Altaparmakov (ll - 1) >> vol->cluster_size_bits, NULL);
1737cc22c800SDenis Efremov if (IS_ERR(rl) || unlikely(!rl->length || rl->lcn < 0)) {
1738b6ad6c52SAnton Altaparmakov up_write(&mft_ni->runlist.lock);
17391da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to determine last allocated "
17401da177e4SLinus Torvalds "cluster of mft data attribute.");
1741b6ad6c52SAnton Altaparmakov if (!IS_ERR(rl))
17421da177e4SLinus Torvalds ret = -EIO;
1743b6ad6c52SAnton Altaparmakov else
17441da177e4SLinus Torvalds ret = PTR_ERR(rl);
17451da177e4SLinus Torvalds return ret;
17461da177e4SLinus Torvalds }
17471da177e4SLinus Torvalds lcn = rl->lcn + rl->length;
174807a4e2daSAnton Altaparmakov ntfs_debug("Last lcn of mft data attribute is 0x%llx.", (long long)lcn);
17491da177e4SLinus Torvalds /* Minimum allocation is one mft record worth of clusters. */
17501da177e4SLinus Torvalds min_nr = vol->mft_record_size >> vol->cluster_size_bits;
17511da177e4SLinus Torvalds if (!min_nr)
17521da177e4SLinus Torvalds min_nr = 1;
17531da177e4SLinus Torvalds /* Want to allocate 16 mft records worth of clusters. */
17541da177e4SLinus Torvalds nr = vol->mft_record_size << 4 >> vol->cluster_size_bits;
17551da177e4SLinus Torvalds if (!nr)
17561da177e4SLinus Torvalds nr = min_nr;
17571da177e4SLinus Torvalds /* Ensure we do not go above 2^32-1 mft records. */
175807a4e2daSAnton Altaparmakov read_lock_irqsave(&mft_ni->size_lock, flags);
175907a4e2daSAnton Altaparmakov ll = mft_ni->allocated_size;
176007a4e2daSAnton Altaparmakov read_unlock_irqrestore(&mft_ni->size_lock, flags);
176107a4e2daSAnton Altaparmakov if (unlikely((ll + (nr << vol->cluster_size_bits)) >>
17621da177e4SLinus Torvalds vol->mft_record_size_bits >= (1ll << 32))) {
17631da177e4SLinus Torvalds nr = min_nr;
176407a4e2daSAnton Altaparmakov if (unlikely((ll + (nr << vol->cluster_size_bits)) >>
17651da177e4SLinus Torvalds vol->mft_record_size_bits >= (1ll << 32))) {
17661da177e4SLinus Torvalds ntfs_warning(vol->sb, "Cannot allocate mft record "
17671da177e4SLinus Torvalds "because the maximum number of inodes "
17681da177e4SLinus Torvalds "(2^32) has already been reached.");
17691da177e4SLinus Torvalds up_write(&mft_ni->runlist.lock);
17701da177e4SLinus Torvalds return -ENOSPC;
17711da177e4SLinus Torvalds }
17721da177e4SLinus Torvalds }
17731da177e4SLinus Torvalds ntfs_debug("Trying mft data allocation with %s cluster count %lli.",
17741da177e4SLinus Torvalds nr > min_nr ? "default" : "minimal", (long long)nr);
17751da177e4SLinus Torvalds old_last_vcn = rl[1].vcn;
17761da177e4SLinus Torvalds do {
1777fc0fa7dcSAnton Altaparmakov rl2 = ntfs_cluster_alloc(vol, old_last_vcn, nr, lcn, MFT_ZONE,
1778c49c3111SRichard Knutsson true);
1779cc22c800SDenis Efremov if (!IS_ERR(rl2))
17801da177e4SLinus Torvalds break;
17811da177e4SLinus Torvalds if (PTR_ERR(rl2) != -ENOSPC || nr == min_nr) {
17821da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to allocate the minimal "
17831da177e4SLinus Torvalds "number of clusters (%lli) for the "
17841da177e4SLinus Torvalds "mft data attribute.", (long long)nr);
17851da177e4SLinus Torvalds up_write(&mft_ni->runlist.lock);
17861da177e4SLinus Torvalds return PTR_ERR(rl2);
17871da177e4SLinus Torvalds }
17881da177e4SLinus Torvalds /*
17891da177e4SLinus Torvalds * There is not enough space to do the allocation, but there
17901da177e4SLinus Torvalds * might be enough space to do a minimal allocation so try that
17911da177e4SLinus Torvalds * before failing.
17921da177e4SLinus Torvalds */
17931da177e4SLinus Torvalds nr = min_nr;
17941da177e4SLinus Torvalds ntfs_debug("Retrying mft data allocation with minimal cluster "
17951da177e4SLinus Torvalds "count %lli.", (long long)nr);
17961da177e4SLinus Torvalds } while (1);
17971da177e4SLinus Torvalds rl = ntfs_runlists_merge(mft_ni->runlist.rl, rl2);
17981da177e4SLinus Torvalds if (IS_ERR(rl)) {
17991da177e4SLinus Torvalds up_write(&mft_ni->runlist.lock);
18001da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to merge runlists for mft data "
18011da177e4SLinus Torvalds "attribute.");
18021da177e4SLinus Torvalds if (ntfs_cluster_free_from_rl(vol, rl2)) {
18039b556248SAnton Altaparmakov ntfs_error(vol->sb, "Failed to deallocate clusters "
18041da177e4SLinus Torvalds "from the mft data attribute.%s", es);
18051da177e4SLinus Torvalds NVolSetErrors(vol);
18061da177e4SLinus Torvalds }
18071da177e4SLinus Torvalds ntfs_free(rl2);
18081da177e4SLinus Torvalds return PTR_ERR(rl);
18091da177e4SLinus Torvalds }
18101da177e4SLinus Torvalds mft_ni->runlist.rl = rl;
18118907547dSRandy Dunlap ntfs_debug("Allocated %lli clusters.", (long long)nr);
18121da177e4SLinus Torvalds /* Find the last run in the new runlist. */
18131da177e4SLinus Torvalds for (; rl[1].length; rl++)
18141da177e4SLinus Torvalds ;
18151da177e4SLinus Torvalds /* Update the attribute record as well. */
18161da177e4SLinus Torvalds mrec = map_mft_record(mft_ni);
18171da177e4SLinus Torvalds if (IS_ERR(mrec)) {
18181da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to map mft record.");
18191da177e4SLinus Torvalds ret = PTR_ERR(mrec);
18201da177e4SLinus Torvalds goto undo_alloc;
18211da177e4SLinus Torvalds }
18221da177e4SLinus Torvalds ctx = ntfs_attr_get_search_ctx(mft_ni, mrec);
18231da177e4SLinus Torvalds if (unlikely(!ctx)) {
18241da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to get search context.");
18251da177e4SLinus Torvalds ret = -ENOMEM;
18261da177e4SLinus Torvalds goto undo_alloc;
18271da177e4SLinus Torvalds }
18281da177e4SLinus Torvalds ret = ntfs_attr_lookup(mft_ni->type, mft_ni->name, mft_ni->name_len,
18291da177e4SLinus Torvalds CASE_SENSITIVE, rl[1].vcn, NULL, 0, ctx);
18301da177e4SLinus Torvalds if (unlikely(ret)) {
18311da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to find last attribute extent of "
18321da177e4SLinus Torvalds "mft data attribute.");
18331da177e4SLinus Torvalds if (ret == -ENOENT)
18341da177e4SLinus Torvalds ret = -EIO;
18351da177e4SLinus Torvalds goto undo_alloc;
18361da177e4SLinus Torvalds }
18371da177e4SLinus Torvalds a = ctx->attr;
18381da177e4SLinus Torvalds ll = sle64_to_cpu(a->data.non_resident.lowest_vcn);
18391da177e4SLinus Torvalds /* Search back for the previous last allocated cluster of mft bitmap. */
18401da177e4SLinus Torvalds for (rl2 = rl; rl2 > mft_ni->runlist.rl; rl2--) {
18411da177e4SLinus Torvalds if (ll >= rl2->vcn)
18421da177e4SLinus Torvalds break;
18431da177e4SLinus Torvalds }
18441da177e4SLinus Torvalds BUG_ON(ll < rl2->vcn);
18451da177e4SLinus Torvalds BUG_ON(ll >= rl2->vcn + rl2->length);
18461da177e4SLinus Torvalds /* Get the size for the new mapping pairs array for this extent. */
1847fa3be923SAnton Altaparmakov mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, -1);
18481da177e4SLinus Torvalds if (unlikely(mp_size <= 0)) {
18491da177e4SLinus Torvalds ntfs_error(vol->sb, "Get size for mapping pairs failed for "
18501da177e4SLinus Torvalds "mft data attribute extent.");
18511da177e4SLinus Torvalds ret = mp_size;
18521da177e4SLinus Torvalds if (!ret)
18531da177e4SLinus Torvalds ret = -EIO;
18541da177e4SLinus Torvalds goto undo_alloc;
18551da177e4SLinus Torvalds }
18561da177e4SLinus Torvalds /* Expand the attribute record if necessary. */
18571da177e4SLinus Torvalds old_alen = le32_to_cpu(a->length);
18581da177e4SLinus Torvalds ret = ntfs_attr_record_resize(ctx->mrec, a, mp_size +
18591da177e4SLinus Torvalds le16_to_cpu(a->data.non_resident.mapping_pairs_offset));
18601da177e4SLinus Torvalds if (unlikely(ret)) {
18611da177e4SLinus Torvalds if (ret != -ENOSPC) {
18621da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to resize attribute "
18631da177e4SLinus Torvalds "record for mft data attribute.");
18641da177e4SLinus Torvalds goto undo_alloc;
18651da177e4SLinus Torvalds }
18661da177e4SLinus Torvalds // TODO: Deal with this by moving this extent to a new mft
18671da177e4SLinus Torvalds // record or by starting a new extent in a new mft record or by
18681da177e4SLinus Torvalds // moving other attributes out of this mft record.
18691da177e4SLinus Torvalds // Note: Use the special reserved mft records and ensure that
18701da177e4SLinus Torvalds // this extent is not required to find the mft record in
1871b6ad6c52SAnton Altaparmakov // question. If no free special records left we would need to
1872b6ad6c52SAnton Altaparmakov // move an existing record away, insert ours in its place, and
1873b6ad6c52SAnton Altaparmakov // then place the moved record into the newly allocated space
1874b6ad6c52SAnton Altaparmakov // and we would then need to update all references to this mft
1875b6ad6c52SAnton Altaparmakov // record appropriately. This is rather complicated...
18761da177e4SLinus Torvalds ntfs_error(vol->sb, "Not enough space in this mft record to "
187725985edcSLucas De Marchi "accommodate extended mft data attribute "
18781da177e4SLinus Torvalds "extent. Cannot handle this yet.");
18791da177e4SLinus Torvalds ret = -EOPNOTSUPP;
18801da177e4SLinus Torvalds goto undo_alloc;
18811da177e4SLinus Torvalds }
1882c49c3111SRichard Knutsson mp_rebuilt = true;
18831da177e4SLinus Torvalds /* Generate the mapping pairs array directly into the attr record. */
18841da177e4SLinus Torvalds ret = ntfs_mapping_pairs_build(vol, (u8*)a +
18851da177e4SLinus Torvalds le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
1886fa3be923SAnton Altaparmakov mp_size, rl2, ll, -1, NULL);
18871da177e4SLinus Torvalds if (unlikely(ret)) {
18881da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to build mapping pairs array of "
18891da177e4SLinus Torvalds "mft data attribute.");
18901da177e4SLinus Torvalds goto undo_alloc;
18911da177e4SLinus Torvalds }
18921da177e4SLinus Torvalds /* Update the highest_vcn. */
18931da177e4SLinus Torvalds a->data.non_resident.highest_vcn = cpu_to_sle64(rl[1].vcn - 1);
18941da177e4SLinus Torvalds /*
18951da177e4SLinus Torvalds * We now have extended the mft data allocated_size by nr clusters.
18961da177e4SLinus Torvalds * Reflect this in the ntfs_inode structure and the attribute record.
18971da177e4SLinus Torvalds * @rl is the last (non-terminator) runlist element of mft data
18981da177e4SLinus Torvalds * attribute.
18991da177e4SLinus Torvalds */
19001da177e4SLinus Torvalds if (a->data.non_resident.lowest_vcn) {
19011da177e4SLinus Torvalds /*
19021da177e4SLinus Torvalds * We are not in the first attribute extent, switch to it, but
19031da177e4SLinus Torvalds * first ensure the changes will make it to disk later.
19041da177e4SLinus Torvalds */
19051da177e4SLinus Torvalds flush_dcache_mft_record_page(ctx->ntfs_ino);
19061da177e4SLinus Torvalds mark_mft_record_dirty(ctx->ntfs_ino);
19071da177e4SLinus Torvalds ntfs_attr_reinit_search_ctx(ctx);
19081da177e4SLinus Torvalds ret = ntfs_attr_lookup(mft_ni->type, mft_ni->name,
19091da177e4SLinus Torvalds mft_ni->name_len, CASE_SENSITIVE, 0, NULL, 0,
19101da177e4SLinus Torvalds ctx);
19111da177e4SLinus Torvalds if (unlikely(ret)) {
19121da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to find first attribute "
19131da177e4SLinus Torvalds "extent of mft data attribute.");
19141da177e4SLinus Torvalds goto restore_undo_alloc;
19151da177e4SLinus Torvalds }
19161da177e4SLinus Torvalds a = ctx->attr;
19171da177e4SLinus Torvalds }
191807a4e2daSAnton Altaparmakov write_lock_irqsave(&mft_ni->size_lock, flags);
19191da177e4SLinus Torvalds mft_ni->allocated_size += nr << vol->cluster_size_bits;
19201da177e4SLinus Torvalds a->data.non_resident.allocated_size =
19211da177e4SLinus Torvalds cpu_to_sle64(mft_ni->allocated_size);
192207a4e2daSAnton Altaparmakov write_unlock_irqrestore(&mft_ni->size_lock, flags);
19231da177e4SLinus Torvalds /* Ensure the changes make it to disk. */
19241da177e4SLinus Torvalds flush_dcache_mft_record_page(ctx->ntfs_ino);
19251da177e4SLinus Torvalds mark_mft_record_dirty(ctx->ntfs_ino);
19261da177e4SLinus Torvalds ntfs_attr_put_search_ctx(ctx);
19271da177e4SLinus Torvalds unmap_mft_record(mft_ni);
19281da177e4SLinus Torvalds up_write(&mft_ni->runlist.lock);
19291da177e4SLinus Torvalds ntfs_debug("Done.");
19301da177e4SLinus Torvalds return 0;
19311da177e4SLinus Torvalds restore_undo_alloc:
19321da177e4SLinus Torvalds ntfs_attr_reinit_search_ctx(ctx);
19331da177e4SLinus Torvalds if (ntfs_attr_lookup(mft_ni->type, mft_ni->name, mft_ni->name_len,
19341da177e4SLinus Torvalds CASE_SENSITIVE, rl[1].vcn, NULL, 0, ctx)) {
19351da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to find last attribute extent of "
19361da177e4SLinus Torvalds "mft data attribute.%s", es);
193707a4e2daSAnton Altaparmakov write_lock_irqsave(&mft_ni->size_lock, flags);
19381da177e4SLinus Torvalds mft_ni->allocated_size += nr << vol->cluster_size_bits;
193907a4e2daSAnton Altaparmakov write_unlock_irqrestore(&mft_ni->size_lock, flags);
19401da177e4SLinus Torvalds ntfs_attr_put_search_ctx(ctx);
19411da177e4SLinus Torvalds unmap_mft_record(mft_ni);
19421da177e4SLinus Torvalds up_write(&mft_ni->runlist.lock);
19431da177e4SLinus Torvalds /*
19441da177e4SLinus Torvalds * The only thing that is now wrong is ->allocated_size of the
19451da177e4SLinus Torvalds * base attribute extent which chkdsk should be able to fix.
19461da177e4SLinus Torvalds */
19471da177e4SLinus Torvalds NVolSetErrors(vol);
19481da177e4SLinus Torvalds return ret;
19491da177e4SLinus Torvalds }
1950511bea5eSAnton Altaparmakov ctx->attr->data.non_resident.highest_vcn =
1951511bea5eSAnton Altaparmakov cpu_to_sle64(old_last_vcn - 1);
19521da177e4SLinus Torvalds undo_alloc:
1953511bea5eSAnton Altaparmakov if (ntfs_cluster_free(mft_ni, old_last_vcn, -1, ctx) < 0) {
19541da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to free clusters from mft data "
19551da177e4SLinus Torvalds "attribute.%s", es);
19561da177e4SLinus Torvalds NVolSetErrors(vol);
19571da177e4SLinus Torvalds }
1958aa4b92c5SDanila Chernetsov
19591da177e4SLinus Torvalds if (ntfs_rl_truncate_nolock(vol, &mft_ni->runlist, old_last_vcn)) {
19601da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to truncate mft data attribute "
19611da177e4SLinus Torvalds "runlist.%s", es);
19621da177e4SLinus Torvalds NVolSetErrors(vol);
19631da177e4SLinus Torvalds }
1964aa4b92c5SDanila Chernetsov if (ctx) {
1965aa4b92c5SDanila Chernetsov a = ctx->attr;
1966511bea5eSAnton Altaparmakov if (mp_rebuilt && !IS_ERR(ctx->mrec)) {
19671da177e4SLinus Torvalds if (ntfs_mapping_pairs_build(vol, (u8 *)a + le16_to_cpu(
19681da177e4SLinus Torvalds a->data.non_resident.mapping_pairs_offset),
19691da177e4SLinus Torvalds old_alen - le16_to_cpu(
19701da177e4SLinus Torvalds a->data.non_resident.mapping_pairs_offset),
1971fa3be923SAnton Altaparmakov rl2, ll, -1, NULL)) {
19721da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to restore mapping pairs "
19731da177e4SLinus Torvalds "array.%s", es);
19741da177e4SLinus Torvalds NVolSetErrors(vol);
19751da177e4SLinus Torvalds }
19761da177e4SLinus Torvalds if (ntfs_attr_record_resize(ctx->mrec, a, old_alen)) {
19771da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to restore attribute "
19781da177e4SLinus Torvalds "record.%s", es);
19791da177e4SLinus Torvalds NVolSetErrors(vol);
19801da177e4SLinus Torvalds }
19811da177e4SLinus Torvalds flush_dcache_mft_record_page(ctx->ntfs_ino);
19821da177e4SLinus Torvalds mark_mft_record_dirty(ctx->ntfs_ino);
1983511bea5eSAnton Altaparmakov } else if (IS_ERR(ctx->mrec)) {
1984511bea5eSAnton Altaparmakov ntfs_error(vol->sb, "Failed to restore attribute search "
1985511bea5eSAnton Altaparmakov "context.%s", es);
1986511bea5eSAnton Altaparmakov NVolSetErrors(vol);
19871da177e4SLinus Torvalds }
19881da177e4SLinus Torvalds ntfs_attr_put_search_ctx(ctx);
1989aa4b92c5SDanila Chernetsov }
19901da177e4SLinus Torvalds if (!IS_ERR(mrec))
19911da177e4SLinus Torvalds unmap_mft_record(mft_ni);
19921da177e4SLinus Torvalds up_write(&mft_ni->runlist.lock);
19931da177e4SLinus Torvalds return ret;
19941da177e4SLinus Torvalds }
19951da177e4SLinus Torvalds
19961da177e4SLinus Torvalds /**
19971da177e4SLinus Torvalds * ntfs_mft_record_layout - layout an mft record into a memory buffer
19981da177e4SLinus Torvalds * @vol: volume to which the mft record will belong
19991da177e4SLinus Torvalds * @mft_no: mft reference specifying the mft record number
20001da177e4SLinus Torvalds * @m: destination buffer of size >= @vol->mft_record_size bytes
20011da177e4SLinus Torvalds *
20021da177e4SLinus Torvalds * Layout an empty, unused mft record with the mft record number @mft_no into
20031da177e4SLinus Torvalds * the buffer @m. The volume @vol is needed because the mft record structure
20041da177e4SLinus Torvalds * was modified in NTFS 3.1 so we need to know which volume version this mft
20051da177e4SLinus Torvalds * record will be used on.
20061da177e4SLinus Torvalds *
20071da177e4SLinus Torvalds * Return 0 on success and -errno on error.
20081da177e4SLinus Torvalds */
ntfs_mft_record_layout(const ntfs_volume * vol,const s64 mft_no,MFT_RECORD * m)20091da177e4SLinus Torvalds static int ntfs_mft_record_layout(const ntfs_volume *vol, const s64 mft_no,
20101da177e4SLinus Torvalds MFT_RECORD *m)
20111da177e4SLinus Torvalds {
20121da177e4SLinus Torvalds ATTR_RECORD *a;
20131da177e4SLinus Torvalds
20141da177e4SLinus Torvalds ntfs_debug("Entering for mft record 0x%llx.", (long long)mft_no);
20151da177e4SLinus Torvalds if (mft_no >= (1ll << 32)) {
20161da177e4SLinus Torvalds ntfs_error(vol->sb, "Mft record number 0x%llx exceeds "
20171da177e4SLinus Torvalds "maximum of 2^32.", (long long)mft_no);
20181da177e4SLinus Torvalds return -ERANGE;
20191da177e4SLinus Torvalds }
20201da177e4SLinus Torvalds /* Start by clearing the whole mft record to gives us a clean slate. */
20211da177e4SLinus Torvalds memset(m, 0, vol->mft_record_size);
20221da177e4SLinus Torvalds /* Aligned to 2-byte boundary. */
20231da177e4SLinus Torvalds if (vol->major_ver < 3 || (vol->major_ver == 3 && !vol->minor_ver))
20241da177e4SLinus Torvalds m->usa_ofs = cpu_to_le16((sizeof(MFT_RECORD_OLD) + 1) & ~1);
20251da177e4SLinus Torvalds else {
20261da177e4SLinus Torvalds m->usa_ofs = cpu_to_le16((sizeof(MFT_RECORD) + 1) & ~1);
20271da177e4SLinus Torvalds /*
20281da177e4SLinus Torvalds * Set the NTFS 3.1+ specific fields while we know that the
20291da177e4SLinus Torvalds * volume version is 3.1+.
20301da177e4SLinus Torvalds */
20311da177e4SLinus Torvalds m->reserved = 0;
20321da177e4SLinus Torvalds m->mft_record_number = cpu_to_le32((u32)mft_no);
20331da177e4SLinus Torvalds }
20341da177e4SLinus Torvalds m->magic = magic_FILE;
20351da177e4SLinus Torvalds if (vol->mft_record_size >= NTFS_BLOCK_SIZE)
20361da177e4SLinus Torvalds m->usa_count = cpu_to_le16(vol->mft_record_size /
20371da177e4SLinus Torvalds NTFS_BLOCK_SIZE + 1);
20381da177e4SLinus Torvalds else {
20391da177e4SLinus Torvalds m->usa_count = cpu_to_le16(1);
20401da177e4SLinus Torvalds ntfs_warning(vol->sb, "Sector size is bigger than mft record "
20411da177e4SLinus Torvalds "size. Setting usa_count to 1. If chkdsk "
20421da177e4SLinus Torvalds "reports this as corruption, please email "
20431da177e4SLinus Torvalds "linux-ntfs-dev@lists.sourceforge.net stating "
20441da177e4SLinus Torvalds "that you saw this message and that the "
20451da177e4SLinus Torvalds "modified filesystem created was corrupt. "
20461da177e4SLinus Torvalds "Thank you.");
20471da177e4SLinus Torvalds }
20481da177e4SLinus Torvalds /* Set the update sequence number to 1. */
20491da177e4SLinus Torvalds *(le16*)((u8*)m + le16_to_cpu(m->usa_ofs)) = cpu_to_le16(1);
20501da177e4SLinus Torvalds m->lsn = 0;
20511da177e4SLinus Torvalds m->sequence_number = cpu_to_le16(1);
20521da177e4SLinus Torvalds m->link_count = 0;
20531da177e4SLinus Torvalds /*
20541da177e4SLinus Torvalds * Place the attributes straight after the update sequence array,
20551da177e4SLinus Torvalds * aligned to 8-byte boundary.
20561da177e4SLinus Torvalds */
20571da177e4SLinus Torvalds m->attrs_offset = cpu_to_le16((le16_to_cpu(m->usa_ofs) +
20581da177e4SLinus Torvalds (le16_to_cpu(m->usa_count) << 1) + 7) & ~7);
20591da177e4SLinus Torvalds m->flags = 0;
20601da177e4SLinus Torvalds /*
20611da177e4SLinus Torvalds * Using attrs_offset plus eight bytes (for the termination attribute).
20621da177e4SLinus Torvalds * attrs_offset is already aligned to 8-byte boundary, so no need to
20631da177e4SLinus Torvalds * align again.
20641da177e4SLinus Torvalds */
20651da177e4SLinus Torvalds m->bytes_in_use = cpu_to_le32(le16_to_cpu(m->attrs_offset) + 8);
20661da177e4SLinus Torvalds m->bytes_allocated = cpu_to_le32(vol->mft_record_size);
20671da177e4SLinus Torvalds m->base_mft_record = 0;
20681da177e4SLinus Torvalds m->next_attr_instance = 0;
20691da177e4SLinus Torvalds /* Add the termination attribute. */
20701da177e4SLinus Torvalds a = (ATTR_RECORD*)((u8*)m + le16_to_cpu(m->attrs_offset));
20711da177e4SLinus Torvalds a->type = AT_END;
20721da177e4SLinus Torvalds a->length = 0;
20731da177e4SLinus Torvalds ntfs_debug("Done.");
20741da177e4SLinus Torvalds return 0;
20751da177e4SLinus Torvalds }
20761da177e4SLinus Torvalds
20771da177e4SLinus Torvalds /**
20781da177e4SLinus Torvalds * ntfs_mft_record_format - format an mft record on an ntfs volume
20791da177e4SLinus Torvalds * @vol: volume on which to format the mft record
20801da177e4SLinus Torvalds * @mft_no: mft record number to format
20811da177e4SLinus Torvalds *
20821da177e4SLinus Torvalds * Format the mft record @mft_no in $MFT/$DATA, i.e. lay out an empty, unused
20831da177e4SLinus Torvalds * mft record into the appropriate place of the mft data attribute. This is
20841da177e4SLinus Torvalds * used when extending the mft data attribute.
20851da177e4SLinus Torvalds *
20861da177e4SLinus Torvalds * Return 0 on success and -errno on error.
20871da177e4SLinus Torvalds */
ntfs_mft_record_format(const ntfs_volume * vol,const s64 mft_no)20881da177e4SLinus Torvalds static int ntfs_mft_record_format(const ntfs_volume *vol, const s64 mft_no)
20891da177e4SLinus Torvalds {
209007a4e2daSAnton Altaparmakov loff_t i_size;
20911da177e4SLinus Torvalds struct inode *mft_vi = vol->mft_ino;
20921da177e4SLinus Torvalds struct page *page;
20931da177e4SLinus Torvalds MFT_RECORD *m;
20941da177e4SLinus Torvalds pgoff_t index, end_index;
20951da177e4SLinus Torvalds unsigned int ofs;
20961da177e4SLinus Torvalds int err;
20971da177e4SLinus Torvalds
20981da177e4SLinus Torvalds ntfs_debug("Entering for mft record 0x%llx.", (long long)mft_no);
20991da177e4SLinus Torvalds /*
21001da177e4SLinus Torvalds * The index into the page cache and the offset within the page cache
21011da177e4SLinus Torvalds * page of the wanted mft record.
21021da177e4SLinus Torvalds */
210309cbfeafSKirill A. Shutemov index = mft_no << vol->mft_record_size_bits >> PAGE_SHIFT;
210409cbfeafSKirill A. Shutemov ofs = (mft_no << vol->mft_record_size_bits) & ~PAGE_MASK;
21051da177e4SLinus Torvalds /* The maximum valid index into the page cache for $MFT's data. */
210607a4e2daSAnton Altaparmakov i_size = i_size_read(mft_vi);
210709cbfeafSKirill A. Shutemov end_index = i_size >> PAGE_SHIFT;
21081da177e4SLinus Torvalds if (unlikely(index >= end_index)) {
21091da177e4SLinus Torvalds if (unlikely(index > end_index || ofs + vol->mft_record_size >=
211009cbfeafSKirill A. Shutemov (i_size & ~PAGE_MASK))) {
21111da177e4SLinus Torvalds ntfs_error(vol->sb, "Tried to format non-existing mft "
21121da177e4SLinus Torvalds "record 0x%llx.", (long long)mft_no);
21131da177e4SLinus Torvalds return -ENOENT;
21141da177e4SLinus Torvalds }
21151da177e4SLinus Torvalds }
21161da177e4SLinus Torvalds /* Read, map, and pin the page containing the mft record. */
21171da177e4SLinus Torvalds page = ntfs_map_page(mft_vi->i_mapping, index);
2118801678c5SHirofumi Nakagawa if (IS_ERR(page)) {
21191da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to map page containing mft record "
21201da177e4SLinus Torvalds "to format 0x%llx.", (long long)mft_no);
21211da177e4SLinus Torvalds return PTR_ERR(page);
21221da177e4SLinus Torvalds }
21231da177e4SLinus Torvalds lock_page(page);
21241da177e4SLinus Torvalds BUG_ON(!PageUptodate(page));
21251da177e4SLinus Torvalds ClearPageUptodate(page);
21261da177e4SLinus Torvalds m = (MFT_RECORD*)((u8*)page_address(page) + ofs);
21271da177e4SLinus Torvalds err = ntfs_mft_record_layout(vol, mft_no, m);
21281da177e4SLinus Torvalds if (unlikely(err)) {
21291da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to layout mft record 0x%llx.",
21301da177e4SLinus Torvalds (long long)mft_no);
21311da177e4SLinus Torvalds SetPageUptodate(page);
21321da177e4SLinus Torvalds unlock_page(page);
21331da177e4SLinus Torvalds ntfs_unmap_page(page);
21341da177e4SLinus Torvalds return err;
21351da177e4SLinus Torvalds }
21361da177e4SLinus Torvalds flush_dcache_page(page);
21371da177e4SLinus Torvalds SetPageUptodate(page);
21381da177e4SLinus Torvalds unlock_page(page);
21391da177e4SLinus Torvalds /*
21401da177e4SLinus Torvalds * Make sure the mft record is written out to disk. We could use
21411da177e4SLinus Torvalds * ilookup5() to check if an inode is in icache and so on but this is
21421da177e4SLinus Torvalds * unnecessary as ntfs_writepage() will write the dirty record anyway.
21431da177e4SLinus Torvalds */
21441da177e4SLinus Torvalds mark_ntfs_record_dirty(page, ofs);
21451da177e4SLinus Torvalds ntfs_unmap_page(page);
21461da177e4SLinus Torvalds ntfs_debug("Done.");
21471da177e4SLinus Torvalds return 0;
21481da177e4SLinus Torvalds }
21491da177e4SLinus Torvalds
21501da177e4SLinus Torvalds /**
21511da177e4SLinus Torvalds * ntfs_mft_record_alloc - allocate an mft record on an ntfs volume
21521da177e4SLinus Torvalds * @vol: [IN] volume on which to allocate the mft record
21531da177e4SLinus Torvalds * @mode: [IN] mode if want a file or directory, i.e. base inode or 0
21541da177e4SLinus Torvalds * @base_ni: [IN] open base inode if allocating an extent mft record or NULL
21551da177e4SLinus Torvalds * @mrec: [OUT] on successful return this is the mapped mft record
21561da177e4SLinus Torvalds *
21571da177e4SLinus Torvalds * Allocate an mft record in $MFT/$DATA of an open ntfs volume @vol.
21581da177e4SLinus Torvalds *
21591da177e4SLinus Torvalds * If @base_ni is NULL make the mft record a base mft record, i.e. a file or
21601da177e4SLinus Torvalds * direvctory inode, and allocate it at the default allocator position. In
21611da177e4SLinus Torvalds * this case @mode is the file mode as given to us by the caller. We in
21621da177e4SLinus Torvalds * particular use @mode to distinguish whether a file or a directory is being
21631da177e4SLinus Torvalds * created (S_IFDIR(mode) and S_IFREG(mode), respectively).
21641da177e4SLinus Torvalds *
21651da177e4SLinus Torvalds * If @base_ni is not NULL make the allocated mft record an extent record,
21661da177e4SLinus Torvalds * allocate it starting at the mft record after the base mft record and attach
21671da177e4SLinus Torvalds * the allocated and opened ntfs inode to the base inode @base_ni. In this
21681da177e4SLinus Torvalds * case @mode must be 0 as it is meaningless for extent inodes.
21691da177e4SLinus Torvalds *
21701da177e4SLinus Torvalds * You need to check the return value with IS_ERR(). If false, the function
21711da177e4SLinus Torvalds * was successful and the return value is the now opened ntfs inode of the
21721da177e4SLinus Torvalds * allocated mft record. *@mrec is then set to the allocated, mapped, pinned,
21731da177e4SLinus Torvalds * and locked mft record. If IS_ERR() is true, the function failed and the
21741da177e4SLinus Torvalds * error code is obtained from PTR_ERR(return value). *@mrec is undefined in
21751da177e4SLinus Torvalds * this case.
21761da177e4SLinus Torvalds *
21771da177e4SLinus Torvalds * Allocation strategy:
21781da177e4SLinus Torvalds *
21791da177e4SLinus Torvalds * To find a free mft record, we scan the mft bitmap for a zero bit. To
21801da177e4SLinus Torvalds * optimize this we start scanning at the place specified by @base_ni or if
21811da177e4SLinus Torvalds * @base_ni is NULL we start where we last stopped and we perform wrap around
21821da177e4SLinus Torvalds * when we reach the end. Note, we do not try to allocate mft records below
21831da177e4SLinus Torvalds * number 24 because numbers 0 to 15 are the defined system files anyway and 16
21841da177e4SLinus Torvalds * to 24 are special in that they are used for storing extension mft records
21851da177e4SLinus Torvalds * for the $DATA attribute of $MFT. This is required to avoid the possibility
21861da177e4SLinus Torvalds * of creating a runlist with a circular dependency which once written to disk
21871da177e4SLinus Torvalds * can never be read in again. Windows will only use records 16 to 24 for
21881da177e4SLinus Torvalds * normal files if the volume is completely out of space. We never use them
21891da177e4SLinus Torvalds * which means that when the volume is really out of space we cannot create any
21901da177e4SLinus Torvalds * more files while Windows can still create up to 8 small files. We can start
21911da177e4SLinus Torvalds * doing this at some later time, it does not matter much for now.
21921da177e4SLinus Torvalds *
21931da177e4SLinus Torvalds * When scanning the mft bitmap, we only search up to the last allocated mft
21941da177e4SLinus Torvalds * record. If there are no free records left in the range 24 to number of
21951da177e4SLinus Torvalds * allocated mft records, then we extend the $MFT/$DATA attribute in order to
21961da177e4SLinus Torvalds * create free mft records. We extend the allocated size of $MFT/$DATA by 16
21971da177e4SLinus Torvalds * records at a time or one cluster, if cluster size is above 16kiB. If there
21981da177e4SLinus Torvalds * is not sufficient space to do this, we try to extend by a single mft record
21991da177e4SLinus Torvalds * or one cluster, if cluster size is above the mft record size.
22001da177e4SLinus Torvalds *
22011da177e4SLinus Torvalds * No matter how many mft records we allocate, we initialize only the first
22021da177e4SLinus Torvalds * allocated mft record, incrementing mft data size and initialized size
22031da177e4SLinus Torvalds * accordingly, open an ntfs_inode for it and return it to the caller, unless
22041da177e4SLinus Torvalds * there are less than 24 mft records, in which case we allocate and initialize
22051da177e4SLinus Torvalds * mft records until we reach record 24 which we consider as the first free mft
22061da177e4SLinus Torvalds * record for use by normal files.
22071da177e4SLinus Torvalds *
22081da177e4SLinus Torvalds * If during any stage we overflow the initialized data in the mft bitmap, we
22091da177e4SLinus Torvalds * extend the initialized size (and data size) by 8 bytes, allocating another
22101da177e4SLinus Torvalds * cluster if required. The bitmap data size has to be at least equal to the
22111da177e4SLinus Torvalds * number of mft records in the mft, but it can be bigger, in which case the
22121da177e4SLinus Torvalds * superflous bits are padded with zeroes.
22131da177e4SLinus Torvalds *
22141da177e4SLinus Torvalds * Thus, when we return successfully (IS_ERR() is false), we will have:
22151da177e4SLinus Torvalds * - initialized / extended the mft bitmap if necessary,
22161da177e4SLinus Torvalds * - initialized / extended the mft data if necessary,
22171da177e4SLinus Torvalds * - set the bit corresponding to the mft record being allocated in the
22181da177e4SLinus Torvalds * mft bitmap,
22191da177e4SLinus Torvalds * - opened an ntfs_inode for the allocated mft record, and we will have
22201da177e4SLinus Torvalds * - returned the ntfs_inode as well as the allocated mapped, pinned, and
22211da177e4SLinus Torvalds * locked mft record.
22221da177e4SLinus Torvalds *
22231da177e4SLinus Torvalds * On error, the volume will be left in a consistent state and no record will
22241da177e4SLinus Torvalds * be allocated. If rolling back a partial operation fails, we may leave some
22251da177e4SLinus Torvalds * inconsistent metadata in which case we set NVolErrors() so the volume is
22261da177e4SLinus Torvalds * left dirty when unmounted.
22271da177e4SLinus Torvalds *
22281da177e4SLinus Torvalds * Note, this function cannot make use of most of the normal functions, like
22291da177e4SLinus Torvalds * for example for attribute resizing, etc, because when the run list overflows
22301da177e4SLinus Torvalds * the base mft record and an attribute list is used, it is very important that
22311da177e4SLinus Torvalds * the extension mft records used to store the $DATA attribute of $MFT can be
22321da177e4SLinus Torvalds * reached without having to read the information contained inside them, as
22331da177e4SLinus Torvalds * this would make it impossible to find them in the first place after the
22341da177e4SLinus Torvalds * volume is unmounted. $MFT/$BITMAP probably does not need to follow this
22351da177e4SLinus Torvalds * rule because the bitmap is not essential for finding the mft records, but on
22361da177e4SLinus Torvalds * the other hand, handling the bitmap in this special way would make life
22371da177e4SLinus Torvalds * easier because otherwise there might be circular invocations of functions
22381da177e4SLinus Torvalds * when reading the bitmap.
22391da177e4SLinus Torvalds */
ntfs_mft_record_alloc(ntfs_volume * vol,const int mode,ntfs_inode * base_ni,MFT_RECORD ** mrec)22401da177e4SLinus Torvalds ntfs_inode *ntfs_mft_record_alloc(ntfs_volume *vol, const int mode,
22411da177e4SLinus Torvalds ntfs_inode *base_ni, MFT_RECORD **mrec)
22421da177e4SLinus Torvalds {
22431da177e4SLinus Torvalds s64 ll, bit, old_data_initialized, old_data_size;
224407a4e2daSAnton Altaparmakov unsigned long flags;
22451da177e4SLinus Torvalds struct inode *vi;
22461da177e4SLinus Torvalds struct page *page;
22471da177e4SLinus Torvalds ntfs_inode *mft_ni, *mftbmp_ni, *ni;
22481da177e4SLinus Torvalds ntfs_attr_search_ctx *ctx;
22491da177e4SLinus Torvalds MFT_RECORD *m;
22501da177e4SLinus Torvalds ATTR_RECORD *a;
22511da177e4SLinus Torvalds pgoff_t index;
22521da177e4SLinus Torvalds unsigned int ofs;
22531da177e4SLinus Torvalds int err;
22541da177e4SLinus Torvalds le16 seq_no, usn;
2255c49c3111SRichard Knutsson bool record_formatted = false;
22561da177e4SLinus Torvalds
22571da177e4SLinus Torvalds if (base_ni) {
22581da177e4SLinus Torvalds ntfs_debug("Entering (allocating an extent mft record for "
22591da177e4SLinus Torvalds "base mft record 0x%llx).",
22601da177e4SLinus Torvalds (long long)base_ni->mft_no);
22611da177e4SLinus Torvalds /* @mode and @base_ni are mutually exclusive. */
22621da177e4SLinus Torvalds BUG_ON(mode);
22631da177e4SLinus Torvalds } else
22641da177e4SLinus Torvalds ntfs_debug("Entering (allocating a base mft record).");
22651da177e4SLinus Torvalds if (mode) {
22661da177e4SLinus Torvalds /* @mode and @base_ni are mutually exclusive. */
22671da177e4SLinus Torvalds BUG_ON(base_ni);
22681da177e4SLinus Torvalds /* We only support creation of normal files and directories. */
22691da177e4SLinus Torvalds if (!S_ISREG(mode) && !S_ISDIR(mode))
22701da177e4SLinus Torvalds return ERR_PTR(-EOPNOTSUPP);
22711da177e4SLinus Torvalds }
22721da177e4SLinus Torvalds BUG_ON(!mrec);
22731da177e4SLinus Torvalds mft_ni = NTFS_I(vol->mft_ino);
22741da177e4SLinus Torvalds mftbmp_ni = NTFS_I(vol->mftbmp_ino);
22751da177e4SLinus Torvalds down_write(&vol->mftbmp_lock);
22761da177e4SLinus Torvalds bit = ntfs_mft_bitmap_find_and_alloc_free_rec_nolock(vol, base_ni);
22771da177e4SLinus Torvalds if (bit >= 0) {
22781da177e4SLinus Torvalds ntfs_debug("Found and allocated free record (#1), bit 0x%llx.",
22791da177e4SLinus Torvalds (long long)bit);
22801da177e4SLinus Torvalds goto have_alloc_rec;
22811da177e4SLinus Torvalds }
22821da177e4SLinus Torvalds if (bit != -ENOSPC) {
22831da177e4SLinus Torvalds up_write(&vol->mftbmp_lock);
22841da177e4SLinus Torvalds return ERR_PTR(bit);
22851da177e4SLinus Torvalds }
22861da177e4SLinus Torvalds /*
22871da177e4SLinus Torvalds * No free mft records left. If the mft bitmap already covers more
22881da177e4SLinus Torvalds * than the currently used mft records, the next records are all free,
22891da177e4SLinus Torvalds * so we can simply allocate the first unused mft record.
22901da177e4SLinus Torvalds * Note: We also have to make sure that the mft bitmap at least covers
22911da177e4SLinus Torvalds * the first 24 mft records as they are special and whilst they may not
22921da177e4SLinus Torvalds * be in use, we do not allocate from them.
22931da177e4SLinus Torvalds */
229407a4e2daSAnton Altaparmakov read_lock_irqsave(&mft_ni->size_lock, flags);
22951da177e4SLinus Torvalds ll = mft_ni->initialized_size >> vol->mft_record_size_bits;
229607a4e2daSAnton Altaparmakov read_unlock_irqrestore(&mft_ni->size_lock, flags);
229707a4e2daSAnton Altaparmakov read_lock_irqsave(&mftbmp_ni->size_lock, flags);
229807a4e2daSAnton Altaparmakov old_data_initialized = mftbmp_ni->initialized_size;
229907a4e2daSAnton Altaparmakov read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
230007a4e2daSAnton Altaparmakov if (old_data_initialized << 3 > ll && old_data_initialized > 3) {
23011da177e4SLinus Torvalds bit = ll;
23021da177e4SLinus Torvalds if (bit < 24)
23031da177e4SLinus Torvalds bit = 24;
23041da177e4SLinus Torvalds if (unlikely(bit >= (1ll << 32)))
23051da177e4SLinus Torvalds goto max_err_out;
23061da177e4SLinus Torvalds ntfs_debug("Found free record (#2), bit 0x%llx.",
23071da177e4SLinus Torvalds (long long)bit);
23081da177e4SLinus Torvalds goto found_free_rec;
23091da177e4SLinus Torvalds }
23101da177e4SLinus Torvalds /*
23111da177e4SLinus Torvalds * The mft bitmap needs to be expanded until it covers the first unused
23121da177e4SLinus Torvalds * mft record that we can allocate.
23131da177e4SLinus Torvalds * Note: The smallest mft record we allocate is mft record 24.
23141da177e4SLinus Torvalds */
231507a4e2daSAnton Altaparmakov bit = old_data_initialized << 3;
23161da177e4SLinus Torvalds if (unlikely(bit >= (1ll << 32)))
23171da177e4SLinus Torvalds goto max_err_out;
231807a4e2daSAnton Altaparmakov read_lock_irqsave(&mftbmp_ni->size_lock, flags);
231907a4e2daSAnton Altaparmakov old_data_size = mftbmp_ni->allocated_size;
23201da177e4SLinus Torvalds ntfs_debug("Status of mftbmp before extension: allocated_size 0x%llx, "
23211da177e4SLinus Torvalds "data_size 0x%llx, initialized_size 0x%llx.",
232207a4e2daSAnton Altaparmakov (long long)old_data_size,
232307a4e2daSAnton Altaparmakov (long long)i_size_read(vol->mftbmp_ino),
232407a4e2daSAnton Altaparmakov (long long)old_data_initialized);
232507a4e2daSAnton Altaparmakov read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
232607a4e2daSAnton Altaparmakov if (old_data_initialized + 8 > old_data_size) {
23271da177e4SLinus Torvalds /* Need to extend bitmap by one more cluster. */
23281da177e4SLinus Torvalds ntfs_debug("mftbmp: initialized_size + 8 > allocated_size.");
23291da177e4SLinus Torvalds err = ntfs_mft_bitmap_extend_allocation_nolock(vol);
23301da177e4SLinus Torvalds if (unlikely(err)) {
23311da177e4SLinus Torvalds up_write(&vol->mftbmp_lock);
23321da177e4SLinus Torvalds goto err_out;
23331da177e4SLinus Torvalds }
233407a4e2daSAnton Altaparmakov #ifdef DEBUG
233507a4e2daSAnton Altaparmakov read_lock_irqsave(&mftbmp_ni->size_lock, flags);
23361da177e4SLinus Torvalds ntfs_debug("Status of mftbmp after allocation extension: "
23371da177e4SLinus Torvalds "allocated_size 0x%llx, data_size 0x%llx, "
23381da177e4SLinus Torvalds "initialized_size 0x%llx.",
23391da177e4SLinus Torvalds (long long)mftbmp_ni->allocated_size,
234007a4e2daSAnton Altaparmakov (long long)i_size_read(vol->mftbmp_ino),
23411da177e4SLinus Torvalds (long long)mftbmp_ni->initialized_size);
234207a4e2daSAnton Altaparmakov read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
234307a4e2daSAnton Altaparmakov #endif /* DEBUG */
23441da177e4SLinus Torvalds }
23451da177e4SLinus Torvalds /*
23461da177e4SLinus Torvalds * We now have sufficient allocated space, extend the initialized_size
23471da177e4SLinus Torvalds * as well as the data_size if necessary and fill the new space with
23481da177e4SLinus Torvalds * zeroes.
23491da177e4SLinus Torvalds */
23501da177e4SLinus Torvalds err = ntfs_mft_bitmap_extend_initialized_nolock(vol);
23511da177e4SLinus Torvalds if (unlikely(err)) {
23521da177e4SLinus Torvalds up_write(&vol->mftbmp_lock);
23531da177e4SLinus Torvalds goto err_out;
23541da177e4SLinus Torvalds }
235507a4e2daSAnton Altaparmakov #ifdef DEBUG
235607a4e2daSAnton Altaparmakov read_lock_irqsave(&mftbmp_ni->size_lock, flags);
235725985edcSLucas De Marchi ntfs_debug("Status of mftbmp after initialized extension: "
23581da177e4SLinus Torvalds "allocated_size 0x%llx, data_size 0x%llx, "
23591da177e4SLinus Torvalds "initialized_size 0x%llx.",
23601da177e4SLinus Torvalds (long long)mftbmp_ni->allocated_size,
236107a4e2daSAnton Altaparmakov (long long)i_size_read(vol->mftbmp_ino),
23621da177e4SLinus Torvalds (long long)mftbmp_ni->initialized_size);
236307a4e2daSAnton Altaparmakov read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
236407a4e2daSAnton Altaparmakov #endif /* DEBUG */
23651da177e4SLinus Torvalds ntfs_debug("Found free record (#3), bit 0x%llx.", (long long)bit);
23661da177e4SLinus Torvalds found_free_rec:
23671da177e4SLinus Torvalds /* @bit is the found free mft record, allocate it in the mft bitmap. */
23681da177e4SLinus Torvalds ntfs_debug("At found_free_rec.");
23691da177e4SLinus Torvalds err = ntfs_bitmap_set_bit(vol->mftbmp_ino, bit);
23701da177e4SLinus Torvalds if (unlikely(err)) {
23711da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to allocate bit in mft bitmap.");
23721da177e4SLinus Torvalds up_write(&vol->mftbmp_lock);
23731da177e4SLinus Torvalds goto err_out;
23741da177e4SLinus Torvalds }
23751da177e4SLinus Torvalds ntfs_debug("Set bit 0x%llx in mft bitmap.", (long long)bit);
23761da177e4SLinus Torvalds have_alloc_rec:
23771da177e4SLinus Torvalds /*
23781da177e4SLinus Torvalds * The mft bitmap is now uptodate. Deal with mft data attribute now.
23791da177e4SLinus Torvalds * Note, we keep hold of the mft bitmap lock for writing until all
23801da177e4SLinus Torvalds * modifications to the mft data attribute are complete, too, as they
23811da177e4SLinus Torvalds * will impact decisions for mft bitmap and mft record allocation done
23821da177e4SLinus Torvalds * by a parallel allocation and if the lock is not maintained a
23831da177e4SLinus Torvalds * parallel allocation could allocate the same mft record as this one.
23841da177e4SLinus Torvalds */
23851da177e4SLinus Torvalds ll = (bit + 1) << vol->mft_record_size_bits;
238607a4e2daSAnton Altaparmakov read_lock_irqsave(&mft_ni->size_lock, flags);
238707a4e2daSAnton Altaparmakov old_data_initialized = mft_ni->initialized_size;
238807a4e2daSAnton Altaparmakov read_unlock_irqrestore(&mft_ni->size_lock, flags);
238907a4e2daSAnton Altaparmakov if (ll <= old_data_initialized) {
23901da177e4SLinus Torvalds ntfs_debug("Allocated mft record already initialized.");
23911da177e4SLinus Torvalds goto mft_rec_already_initialized;
23921da177e4SLinus Torvalds }
23931da177e4SLinus Torvalds ntfs_debug("Initializing allocated mft record.");
23941da177e4SLinus Torvalds /*
23951da177e4SLinus Torvalds * The mft record is outside the initialized data. Extend the mft data
23961da177e4SLinus Torvalds * attribute until it covers the allocated record. The loop is only
23971da177e4SLinus Torvalds * actually traversed more than once when a freshly formatted volume is
23981da177e4SLinus Torvalds * first written to so it optimizes away nicely in the common case.
23991da177e4SLinus Torvalds */
240007a4e2daSAnton Altaparmakov read_lock_irqsave(&mft_ni->size_lock, flags);
24011da177e4SLinus Torvalds ntfs_debug("Status of mft data before extension: "
24021da177e4SLinus Torvalds "allocated_size 0x%llx, data_size 0x%llx, "
24031da177e4SLinus Torvalds "initialized_size 0x%llx.",
24043834c3f2SAnton Altaparmakov (long long)mft_ni->allocated_size,
240507a4e2daSAnton Altaparmakov (long long)i_size_read(vol->mft_ino),
24061da177e4SLinus Torvalds (long long)mft_ni->initialized_size);
24073834c3f2SAnton Altaparmakov while (ll > mft_ni->allocated_size) {
240807a4e2daSAnton Altaparmakov read_unlock_irqrestore(&mft_ni->size_lock, flags);
24091da177e4SLinus Torvalds err = ntfs_mft_data_extend_allocation_nolock(vol);
24101da177e4SLinus Torvalds if (unlikely(err)) {
24111da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to extend mft data "
24121da177e4SLinus Torvalds "allocation.");
24131da177e4SLinus Torvalds goto undo_mftbmp_alloc_nolock;
24141da177e4SLinus Torvalds }
241507a4e2daSAnton Altaparmakov read_lock_irqsave(&mft_ni->size_lock, flags);
24161da177e4SLinus Torvalds ntfs_debug("Status of mft data after allocation extension: "
24171da177e4SLinus Torvalds "allocated_size 0x%llx, data_size 0x%llx, "
24181da177e4SLinus Torvalds "initialized_size 0x%llx.",
24191da177e4SLinus Torvalds (long long)mft_ni->allocated_size,
242007a4e2daSAnton Altaparmakov (long long)i_size_read(vol->mft_ino),
24211da177e4SLinus Torvalds (long long)mft_ni->initialized_size);
24221da177e4SLinus Torvalds }
24233834c3f2SAnton Altaparmakov read_unlock_irqrestore(&mft_ni->size_lock, flags);
24241da177e4SLinus Torvalds /*
24251da177e4SLinus Torvalds * Extend mft data initialized size (and data size of course) to reach
24261da177e4SLinus Torvalds * the allocated mft record, formatting the mft records allong the way.
24271da177e4SLinus Torvalds * Note: We only modify the ntfs_inode structure as that is all that is
24281da177e4SLinus Torvalds * needed by ntfs_mft_record_format(). We will update the attribute
24291da177e4SLinus Torvalds * record itself in one fell swoop later on.
24301da177e4SLinus Torvalds */
243107a4e2daSAnton Altaparmakov write_lock_irqsave(&mft_ni->size_lock, flags);
24321da177e4SLinus Torvalds old_data_initialized = mft_ni->initialized_size;
24331da177e4SLinus Torvalds old_data_size = vol->mft_ino->i_size;
24341da177e4SLinus Torvalds while (ll > mft_ni->initialized_size) {
24351da177e4SLinus Torvalds s64 new_initialized_size, mft_no;
24361da177e4SLinus Torvalds
24371da177e4SLinus Torvalds new_initialized_size = mft_ni->initialized_size +
24381da177e4SLinus Torvalds vol->mft_record_size;
24391da177e4SLinus Torvalds mft_no = mft_ni->initialized_size >> vol->mft_record_size_bits;
244007a4e2daSAnton Altaparmakov if (new_initialized_size > i_size_read(vol->mft_ino))
244107a4e2daSAnton Altaparmakov i_size_write(vol->mft_ino, new_initialized_size);
244207a4e2daSAnton Altaparmakov write_unlock_irqrestore(&mft_ni->size_lock, flags);
24431da177e4SLinus Torvalds ntfs_debug("Initializing mft record 0x%llx.",
24441da177e4SLinus Torvalds (long long)mft_no);
24451da177e4SLinus Torvalds err = ntfs_mft_record_format(vol, mft_no);
24461da177e4SLinus Torvalds if (unlikely(err)) {
24471da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to format mft record.");
24481da177e4SLinus Torvalds goto undo_data_init;
24491da177e4SLinus Torvalds }
245007a4e2daSAnton Altaparmakov write_lock_irqsave(&mft_ni->size_lock, flags);
24511da177e4SLinus Torvalds mft_ni->initialized_size = new_initialized_size;
24521da177e4SLinus Torvalds }
245307a4e2daSAnton Altaparmakov write_unlock_irqrestore(&mft_ni->size_lock, flags);
2454c49c3111SRichard Knutsson record_formatted = true;
24551da177e4SLinus Torvalds /* Update the mft data attribute record to reflect the new sizes. */
24561da177e4SLinus Torvalds m = map_mft_record(mft_ni);
24571da177e4SLinus Torvalds if (IS_ERR(m)) {
24581da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to map mft record.");
24591da177e4SLinus Torvalds err = PTR_ERR(m);
24601da177e4SLinus Torvalds goto undo_data_init;
24611da177e4SLinus Torvalds }
24621da177e4SLinus Torvalds ctx = ntfs_attr_get_search_ctx(mft_ni, m);
24631da177e4SLinus Torvalds if (unlikely(!ctx)) {
24641da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to get search context.");
24651da177e4SLinus Torvalds err = -ENOMEM;
24661da177e4SLinus Torvalds unmap_mft_record(mft_ni);
24671da177e4SLinus Torvalds goto undo_data_init;
24681da177e4SLinus Torvalds }
24691da177e4SLinus Torvalds err = ntfs_attr_lookup(mft_ni->type, mft_ni->name, mft_ni->name_len,
24701da177e4SLinus Torvalds CASE_SENSITIVE, 0, NULL, 0, ctx);
24711da177e4SLinus Torvalds if (unlikely(err)) {
24721da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to find first attribute extent of "
24731da177e4SLinus Torvalds "mft data attribute.");
24741da177e4SLinus Torvalds ntfs_attr_put_search_ctx(ctx);
24751da177e4SLinus Torvalds unmap_mft_record(mft_ni);
24761da177e4SLinus Torvalds goto undo_data_init;
24771da177e4SLinus Torvalds }
24781da177e4SLinus Torvalds a = ctx->attr;
247907a4e2daSAnton Altaparmakov read_lock_irqsave(&mft_ni->size_lock, flags);
24801da177e4SLinus Torvalds a->data.non_resident.initialized_size =
24811da177e4SLinus Torvalds cpu_to_sle64(mft_ni->initialized_size);
248207a4e2daSAnton Altaparmakov a->data.non_resident.data_size =
248307a4e2daSAnton Altaparmakov cpu_to_sle64(i_size_read(vol->mft_ino));
248407a4e2daSAnton Altaparmakov read_unlock_irqrestore(&mft_ni->size_lock, flags);
24851da177e4SLinus Torvalds /* Ensure the changes make it to disk. */
24861da177e4SLinus Torvalds flush_dcache_mft_record_page(ctx->ntfs_ino);
24871da177e4SLinus Torvalds mark_mft_record_dirty(ctx->ntfs_ino);
24881da177e4SLinus Torvalds ntfs_attr_put_search_ctx(ctx);
24891da177e4SLinus Torvalds unmap_mft_record(mft_ni);
249007a4e2daSAnton Altaparmakov read_lock_irqsave(&mft_ni->size_lock, flags);
24911da177e4SLinus Torvalds ntfs_debug("Status of mft data after mft record initialization: "
24921da177e4SLinus Torvalds "allocated_size 0x%llx, data_size 0x%llx, "
24931da177e4SLinus Torvalds "initialized_size 0x%llx.",
24941da177e4SLinus Torvalds (long long)mft_ni->allocated_size,
249507a4e2daSAnton Altaparmakov (long long)i_size_read(vol->mft_ino),
24961da177e4SLinus Torvalds (long long)mft_ni->initialized_size);
249707a4e2daSAnton Altaparmakov BUG_ON(i_size_read(vol->mft_ino) > mft_ni->allocated_size);
249807a4e2daSAnton Altaparmakov BUG_ON(mft_ni->initialized_size > i_size_read(vol->mft_ino));
249907a4e2daSAnton Altaparmakov read_unlock_irqrestore(&mft_ni->size_lock, flags);
25001da177e4SLinus Torvalds mft_rec_already_initialized:
25011da177e4SLinus Torvalds /*
25021da177e4SLinus Torvalds * We can finally drop the mft bitmap lock as the mft data attribute
25031da177e4SLinus Torvalds * has been fully updated. The only disparity left is that the
25041da177e4SLinus Torvalds * allocated mft record still needs to be marked as in use to match the
25051da177e4SLinus Torvalds * set bit in the mft bitmap but this is actually not a problem since
25061da177e4SLinus Torvalds * this mft record is not referenced from anywhere yet and the fact
25071da177e4SLinus Torvalds * that it is allocated in the mft bitmap means that no-one will try to
25081da177e4SLinus Torvalds * allocate it either.
25091da177e4SLinus Torvalds */
25101da177e4SLinus Torvalds up_write(&vol->mftbmp_lock);
25111da177e4SLinus Torvalds /*
25121da177e4SLinus Torvalds * We now have allocated and initialized the mft record. Calculate the
25131da177e4SLinus Torvalds * index of and the offset within the page cache page the record is in.
25141da177e4SLinus Torvalds */
251509cbfeafSKirill A. Shutemov index = bit << vol->mft_record_size_bits >> PAGE_SHIFT;
251609cbfeafSKirill A. Shutemov ofs = (bit << vol->mft_record_size_bits) & ~PAGE_MASK;
25171da177e4SLinus Torvalds /* Read, map, and pin the page containing the mft record. */
25181da177e4SLinus Torvalds page = ntfs_map_page(vol->mft_ino->i_mapping, index);
2519801678c5SHirofumi Nakagawa if (IS_ERR(page)) {
25201da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to map page containing allocated "
25211da177e4SLinus Torvalds "mft record 0x%llx.", (long long)bit);
25221da177e4SLinus Torvalds err = PTR_ERR(page);
25231da177e4SLinus Torvalds goto undo_mftbmp_alloc;
25241da177e4SLinus Torvalds }
25251da177e4SLinus Torvalds lock_page(page);
25261da177e4SLinus Torvalds BUG_ON(!PageUptodate(page));
25271da177e4SLinus Torvalds ClearPageUptodate(page);
25281da177e4SLinus Torvalds m = (MFT_RECORD*)((u8*)page_address(page) + ofs);
25291da177e4SLinus Torvalds /* If we just formatted the mft record no need to do it again. */
25301da177e4SLinus Torvalds if (!record_formatted) {
25311da177e4SLinus Torvalds /* Sanity check that the mft record is really not in use. */
25321da177e4SLinus Torvalds if (ntfs_is_file_record(m->magic) &&
25331da177e4SLinus Torvalds (m->flags & MFT_RECORD_IN_USE)) {
25341da177e4SLinus Torvalds ntfs_error(vol->sb, "Mft record 0x%llx was marked "
25351da177e4SLinus Torvalds "free in mft bitmap but is marked "
25361da177e4SLinus Torvalds "used itself. Corrupt filesystem. "
25371da177e4SLinus Torvalds "Unmount and run chkdsk.",
25381da177e4SLinus Torvalds (long long)bit);
25391da177e4SLinus Torvalds err = -EIO;
25401da177e4SLinus Torvalds SetPageUptodate(page);
25411da177e4SLinus Torvalds unlock_page(page);
25421da177e4SLinus Torvalds ntfs_unmap_page(page);
25431da177e4SLinus Torvalds NVolSetErrors(vol);
25441da177e4SLinus Torvalds goto undo_mftbmp_alloc;
25451da177e4SLinus Torvalds }
25461da177e4SLinus Torvalds /*
25471da177e4SLinus Torvalds * We need to (re-)format the mft record, preserving the
25481da177e4SLinus Torvalds * sequence number if it is not zero as well as the update
25491da177e4SLinus Torvalds * sequence number if it is not zero or -1 (0xffff). This
25501da177e4SLinus Torvalds * means we do not need to care whether or not something went
25511da177e4SLinus Torvalds * wrong with the previous mft record.
25521da177e4SLinus Torvalds */
25531da177e4SLinus Torvalds seq_no = m->sequence_number;
25541da177e4SLinus Torvalds usn = *(le16*)((u8*)m + le16_to_cpu(m->usa_ofs));
25551da177e4SLinus Torvalds err = ntfs_mft_record_layout(vol, bit, m);
25561da177e4SLinus Torvalds if (unlikely(err)) {
25571da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to layout allocated mft "
25581da177e4SLinus Torvalds "record 0x%llx.", (long long)bit);
25591da177e4SLinus Torvalds SetPageUptodate(page);
25601da177e4SLinus Torvalds unlock_page(page);
25611da177e4SLinus Torvalds ntfs_unmap_page(page);
25621da177e4SLinus Torvalds goto undo_mftbmp_alloc;
25631da177e4SLinus Torvalds }
25641da177e4SLinus Torvalds if (seq_no)
25651da177e4SLinus Torvalds m->sequence_number = seq_no;
25661da177e4SLinus Torvalds if (usn && le16_to_cpu(usn) != 0xffff)
25671da177e4SLinus Torvalds *(le16*)((u8*)m + le16_to_cpu(m->usa_ofs)) = usn;
25681da177e4SLinus Torvalds }
25691da177e4SLinus Torvalds /* Set the mft record itself in use. */
25701da177e4SLinus Torvalds m->flags |= MFT_RECORD_IN_USE;
25711da177e4SLinus Torvalds if (S_ISDIR(mode))
25721da177e4SLinus Torvalds m->flags |= MFT_RECORD_IS_DIRECTORY;
25731da177e4SLinus Torvalds flush_dcache_page(page);
25741da177e4SLinus Torvalds SetPageUptodate(page);
25751da177e4SLinus Torvalds if (base_ni) {
2576af5eb745SAnton Altaparmakov MFT_RECORD *m_tmp;
2577af5eb745SAnton Altaparmakov
25781da177e4SLinus Torvalds /*
25791da177e4SLinus Torvalds * Setup the base mft record in the extent mft record. This
25801da177e4SLinus Torvalds * completes initialization of the allocated extent mft record
25811da177e4SLinus Torvalds * and we can simply use it with map_extent_mft_record().
25821da177e4SLinus Torvalds */
25831da177e4SLinus Torvalds m->base_mft_record = MK_LE_MREF(base_ni->mft_no,
25841da177e4SLinus Torvalds base_ni->seq_no);
25851da177e4SLinus Torvalds /*
25861da177e4SLinus Torvalds * Allocate an extent inode structure for the new mft record,
25871da177e4SLinus Torvalds * attach it to the base inode @base_ni and map, pin, and lock
25881da177e4SLinus Torvalds * its, i.e. the allocated, mft record.
25891da177e4SLinus Torvalds */
2590af5eb745SAnton Altaparmakov m_tmp = map_extent_mft_record(base_ni, bit, &ni);
2591af5eb745SAnton Altaparmakov if (IS_ERR(m_tmp)) {
25921da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to map allocated extent "
25931da177e4SLinus Torvalds "mft record 0x%llx.", (long long)bit);
2594af5eb745SAnton Altaparmakov err = PTR_ERR(m_tmp);
25951da177e4SLinus Torvalds /* Set the mft record itself not in use. */
25961da177e4SLinus Torvalds m->flags &= cpu_to_le16(
25971da177e4SLinus Torvalds ~le16_to_cpu(MFT_RECORD_IN_USE));
25981da177e4SLinus Torvalds flush_dcache_page(page);
25991da177e4SLinus Torvalds /* Make sure the mft record is written out to disk. */
26001da177e4SLinus Torvalds mark_ntfs_record_dirty(page, ofs);
26011da177e4SLinus Torvalds unlock_page(page);
26021da177e4SLinus Torvalds ntfs_unmap_page(page);
26031da177e4SLinus Torvalds goto undo_mftbmp_alloc;
26041da177e4SLinus Torvalds }
2605af5eb745SAnton Altaparmakov BUG_ON(m != m_tmp);
26061da177e4SLinus Torvalds /*
26071da177e4SLinus Torvalds * Make sure the allocated mft record is written out to disk.
26081da177e4SLinus Torvalds * No need to set the inode dirty because the caller is going
26091da177e4SLinus Torvalds * to do that anyway after finishing with the new extent mft
26101da177e4SLinus Torvalds * record (e.g. at a minimum a new attribute will be added to
26111da177e4SLinus Torvalds * the mft record.
26121da177e4SLinus Torvalds */
26131da177e4SLinus Torvalds mark_ntfs_record_dirty(page, ofs);
26141da177e4SLinus Torvalds unlock_page(page);
26151da177e4SLinus Torvalds /*
26161da177e4SLinus Torvalds * Need to unmap the page since map_extent_mft_record() mapped
26171da177e4SLinus Torvalds * it as well so we have it mapped twice at the moment.
26181da177e4SLinus Torvalds */
26191da177e4SLinus Torvalds ntfs_unmap_page(page);
26201da177e4SLinus Torvalds } else {
26211da177e4SLinus Torvalds /*
26221da177e4SLinus Torvalds * Allocate a new VFS inode and set it up. NOTE: @vi->i_nlink
26231da177e4SLinus Torvalds * is set to 1 but the mft record->link_count is 0. The caller
26241da177e4SLinus Torvalds * needs to bear this in mind.
26251da177e4SLinus Torvalds */
26261da177e4SLinus Torvalds vi = new_inode(vol->sb);
26271da177e4SLinus Torvalds if (unlikely(!vi)) {
26281da177e4SLinus Torvalds err = -ENOMEM;
26291da177e4SLinus Torvalds /* Set the mft record itself not in use. */
26301da177e4SLinus Torvalds m->flags &= cpu_to_le16(
26311da177e4SLinus Torvalds ~le16_to_cpu(MFT_RECORD_IN_USE));
26321da177e4SLinus Torvalds flush_dcache_page(page);
26331da177e4SLinus Torvalds /* Make sure the mft record is written out to disk. */
26341da177e4SLinus Torvalds mark_ntfs_record_dirty(page, ofs);
26351da177e4SLinus Torvalds unlock_page(page);
26361da177e4SLinus Torvalds ntfs_unmap_page(page);
26371da177e4SLinus Torvalds goto undo_mftbmp_alloc;
26381da177e4SLinus Torvalds }
26391da177e4SLinus Torvalds vi->i_ino = bit;
26401da177e4SLinus Torvalds
26411da177e4SLinus Torvalds /* The owner and group come from the ntfs volume. */
26421da177e4SLinus Torvalds vi->i_uid = vol->uid;
26431da177e4SLinus Torvalds vi->i_gid = vol->gid;
26441da177e4SLinus Torvalds
26451da177e4SLinus Torvalds /* Initialize the ntfs specific part of @vi. */
26461da177e4SLinus Torvalds ntfs_init_big_inode(vi);
26471da177e4SLinus Torvalds ni = NTFS_I(vi);
26481da177e4SLinus Torvalds /*
26491da177e4SLinus Torvalds * Set the appropriate mode, attribute type, and name. For
26501da177e4SLinus Torvalds * directories, also setup the index values to the defaults.
26511da177e4SLinus Torvalds */
26521da177e4SLinus Torvalds if (S_ISDIR(mode)) {
26531da177e4SLinus Torvalds vi->i_mode = S_IFDIR | S_IRWXUGO;
26541da177e4SLinus Torvalds vi->i_mode &= ~vol->dmask;
26551da177e4SLinus Torvalds
26561da177e4SLinus Torvalds NInoSetMstProtected(ni);
26571da177e4SLinus Torvalds ni->type = AT_INDEX_ALLOCATION;
26581da177e4SLinus Torvalds ni->name = I30;
26591da177e4SLinus Torvalds ni->name_len = 4;
26601da177e4SLinus Torvalds
26611da177e4SLinus Torvalds ni->itype.index.block_size = 4096;
2662b9a2838cSAkinobu Mita ni->itype.index.block_size_bits = ntfs_ffs(4096) - 1;
26631da177e4SLinus Torvalds ni->itype.index.collation_rule = COLLATION_FILE_NAME;
26641da177e4SLinus Torvalds if (vol->cluster_size <= ni->itype.index.block_size) {
26651da177e4SLinus Torvalds ni->itype.index.vcn_size = vol->cluster_size;
26661da177e4SLinus Torvalds ni->itype.index.vcn_size_bits =
26671da177e4SLinus Torvalds vol->cluster_size_bits;
26681da177e4SLinus Torvalds } else {
26691da177e4SLinus Torvalds ni->itype.index.vcn_size = vol->sector_size;
26701da177e4SLinus Torvalds ni->itype.index.vcn_size_bits =
26711da177e4SLinus Torvalds vol->sector_size_bits;
26721da177e4SLinus Torvalds }
26731da177e4SLinus Torvalds } else {
26741da177e4SLinus Torvalds vi->i_mode = S_IFREG | S_IRWXUGO;
26751da177e4SLinus Torvalds vi->i_mode &= ~vol->fmask;
26761da177e4SLinus Torvalds
26771da177e4SLinus Torvalds ni->type = AT_DATA;
26781da177e4SLinus Torvalds ni->name = NULL;
26791da177e4SLinus Torvalds ni->name_len = 0;
26801da177e4SLinus Torvalds }
26811da177e4SLinus Torvalds if (IS_RDONLY(vi))
26821da177e4SLinus Torvalds vi->i_mode &= ~S_IWUGO;
26831da177e4SLinus Torvalds
26841da177e4SLinus Torvalds /* Set the inode times to the current time. */
2685*03870d27SJeff Layton vi->i_atime = vi->i_mtime = inode_set_ctime_current(vi);
26861da177e4SLinus Torvalds /*
26871da177e4SLinus Torvalds * Set the file size to 0, the ntfs inode sizes are set to 0 by
26881da177e4SLinus Torvalds * the call to ntfs_init_big_inode() below.
26891da177e4SLinus Torvalds */
26901da177e4SLinus Torvalds vi->i_size = 0;
26911da177e4SLinus Torvalds vi->i_blocks = 0;
26921da177e4SLinus Torvalds
26931da177e4SLinus Torvalds /* Set the sequence number. */
26941da177e4SLinus Torvalds vi->i_generation = ni->seq_no = le16_to_cpu(m->sequence_number);
26951da177e4SLinus Torvalds /*
26961da177e4SLinus Torvalds * Manually map, pin, and lock the mft record as we already
26971da177e4SLinus Torvalds * have its page mapped and it is very easy to do.
26981da177e4SLinus Torvalds */
26991da177e4SLinus Torvalds atomic_inc(&ni->count);
27004e5e529aSIngo Molnar mutex_lock(&ni->mrec_lock);
27011da177e4SLinus Torvalds ni->page = page;
27021da177e4SLinus Torvalds ni->page_ofs = ofs;
27031da177e4SLinus Torvalds /*
27041da177e4SLinus Torvalds * Make sure the allocated mft record is written out to disk.
27051da177e4SLinus Torvalds * NOTE: We do not set the ntfs inode dirty because this would
27061da177e4SLinus Torvalds * fail in ntfs_write_inode() because the inode does not have a
27071da177e4SLinus Torvalds * standard information attribute yet. Also, there is no need
27081da177e4SLinus Torvalds * to set the inode dirty because the caller is going to do
27091da177e4SLinus Torvalds * that anyway after finishing with the new mft record (e.g. at
27101da177e4SLinus Torvalds * a minimum some new attributes will be added to the mft
27111da177e4SLinus Torvalds * record.
27121da177e4SLinus Torvalds */
27131da177e4SLinus Torvalds mark_ntfs_record_dirty(page, ofs);
27141da177e4SLinus Torvalds unlock_page(page);
27151da177e4SLinus Torvalds
27161da177e4SLinus Torvalds /* Add the inode to the inode hash for the superblock. */
27171da177e4SLinus Torvalds insert_inode_hash(vi);
27181da177e4SLinus Torvalds
27191da177e4SLinus Torvalds /* Update the default mft allocation position. */
27201da177e4SLinus Torvalds vol->mft_data_pos = bit + 1;
27211da177e4SLinus Torvalds }
27221da177e4SLinus Torvalds /*
27231da177e4SLinus Torvalds * Return the opened, allocated inode of the allocated mft record as
27241da177e4SLinus Torvalds * well as the mapped, pinned, and locked mft record.
27251da177e4SLinus Torvalds */
27261da177e4SLinus Torvalds ntfs_debug("Returning opened, allocated %sinode 0x%llx.",
27271da177e4SLinus Torvalds base_ni ? "extent " : "", (long long)bit);
27281da177e4SLinus Torvalds *mrec = m;
27291da177e4SLinus Torvalds return ni;
27301da177e4SLinus Torvalds undo_data_init:
273107a4e2daSAnton Altaparmakov write_lock_irqsave(&mft_ni->size_lock, flags);
27321da177e4SLinus Torvalds mft_ni->initialized_size = old_data_initialized;
273307a4e2daSAnton Altaparmakov i_size_write(vol->mft_ino, old_data_size);
273407a4e2daSAnton Altaparmakov write_unlock_irqrestore(&mft_ni->size_lock, flags);
27351da177e4SLinus Torvalds goto undo_mftbmp_alloc_nolock;
27361da177e4SLinus Torvalds undo_mftbmp_alloc:
27371da177e4SLinus Torvalds down_write(&vol->mftbmp_lock);
27381da177e4SLinus Torvalds undo_mftbmp_alloc_nolock:
27391da177e4SLinus Torvalds if (ntfs_bitmap_clear_bit(vol->mftbmp_ino, bit)) {
27401da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to clear bit in mft bitmap.%s", es);
27411da177e4SLinus Torvalds NVolSetErrors(vol);
27421da177e4SLinus Torvalds }
27431da177e4SLinus Torvalds up_write(&vol->mftbmp_lock);
27441da177e4SLinus Torvalds err_out:
27451da177e4SLinus Torvalds return ERR_PTR(err);
27461da177e4SLinus Torvalds max_err_out:
27471da177e4SLinus Torvalds ntfs_warning(vol->sb, "Cannot allocate mft record because the maximum "
27481da177e4SLinus Torvalds "number of inodes (2^32) has already been reached.");
27491da177e4SLinus Torvalds up_write(&vol->mftbmp_lock);
27501da177e4SLinus Torvalds return ERR_PTR(-ENOSPC);
27511da177e4SLinus Torvalds }
27521da177e4SLinus Torvalds
27531da177e4SLinus Torvalds /**
27541da177e4SLinus Torvalds * ntfs_extent_mft_record_free - free an extent mft record on an ntfs volume
27551da177e4SLinus Torvalds * @ni: ntfs inode of the mapped extent mft record to free
27561da177e4SLinus Torvalds * @m: mapped extent mft record of the ntfs inode @ni
27571da177e4SLinus Torvalds *
27581da177e4SLinus Torvalds * Free the mapped extent mft record @m of the extent ntfs inode @ni.
27591da177e4SLinus Torvalds *
27601da177e4SLinus Torvalds * Note that this function unmaps the mft record and closes and destroys @ni
27611da177e4SLinus Torvalds * internally and hence you cannot use either @ni nor @m any more after this
27621da177e4SLinus Torvalds * function returns success.
27631da177e4SLinus Torvalds *
27641da177e4SLinus Torvalds * On success return 0 and on error return -errno. @ni and @m are still valid
27651da177e4SLinus Torvalds * in this case and have not been freed.
27661da177e4SLinus Torvalds *
27671da177e4SLinus Torvalds * For some errors an error message is displayed and the success code 0 is
27681da177e4SLinus Torvalds * returned and the volume is then left dirty on umount. This makes sense in
27691da177e4SLinus Torvalds * case we could not rollback the changes that were already done since the
27701da177e4SLinus Torvalds * caller no longer wants to reference this mft record so it does not matter to
27711da177e4SLinus Torvalds * the caller if something is wrong with it as long as it is properly detached
27721da177e4SLinus Torvalds * from the base inode.
27731da177e4SLinus Torvalds */
ntfs_extent_mft_record_free(ntfs_inode * ni,MFT_RECORD * m)27741da177e4SLinus Torvalds int ntfs_extent_mft_record_free(ntfs_inode *ni, MFT_RECORD *m)
27751da177e4SLinus Torvalds {
27761da177e4SLinus Torvalds unsigned long mft_no = ni->mft_no;
27771da177e4SLinus Torvalds ntfs_volume *vol = ni->vol;
27781da177e4SLinus Torvalds ntfs_inode *base_ni;
27791da177e4SLinus Torvalds ntfs_inode **extent_nis;
27801da177e4SLinus Torvalds int i, err;
27811da177e4SLinus Torvalds le16 old_seq_no;
27821da177e4SLinus Torvalds u16 seq_no;
27831da177e4SLinus Torvalds
27841da177e4SLinus Torvalds BUG_ON(NInoAttr(ni));
27851da177e4SLinus Torvalds BUG_ON(ni->nr_extents != -1);
27861da177e4SLinus Torvalds
27874e5e529aSIngo Molnar mutex_lock(&ni->extent_lock);
27881da177e4SLinus Torvalds base_ni = ni->ext.base_ntfs_ino;
27894e5e529aSIngo Molnar mutex_unlock(&ni->extent_lock);
27901da177e4SLinus Torvalds
27911da177e4SLinus Torvalds BUG_ON(base_ni->nr_extents <= 0);
27921da177e4SLinus Torvalds
27931da177e4SLinus Torvalds ntfs_debug("Entering for extent inode 0x%lx, base inode 0x%lx.\n",
27941da177e4SLinus Torvalds mft_no, base_ni->mft_no);
27951da177e4SLinus Torvalds
27964e5e529aSIngo Molnar mutex_lock(&base_ni->extent_lock);
27971da177e4SLinus Torvalds
27981da177e4SLinus Torvalds /* Make sure we are holding the only reference to the extent inode. */
27991da177e4SLinus Torvalds if (atomic_read(&ni->count) > 2) {
28001da177e4SLinus Torvalds ntfs_error(vol->sb, "Tried to free busy extent inode 0x%lx, "
28011da177e4SLinus Torvalds "not freeing.", base_ni->mft_no);
28024e5e529aSIngo Molnar mutex_unlock(&base_ni->extent_lock);
28031da177e4SLinus Torvalds return -EBUSY;
28041da177e4SLinus Torvalds }
28051da177e4SLinus Torvalds
28061da177e4SLinus Torvalds /* Dissociate the ntfs inode from the base inode. */
28071da177e4SLinus Torvalds extent_nis = base_ni->ext.extent_ntfs_inos;
28081da177e4SLinus Torvalds err = -ENOENT;
28091da177e4SLinus Torvalds for (i = 0; i < base_ni->nr_extents; i++) {
28101da177e4SLinus Torvalds if (ni != extent_nis[i])
28111da177e4SLinus Torvalds continue;
28121da177e4SLinus Torvalds extent_nis += i;
28131da177e4SLinus Torvalds base_ni->nr_extents--;
28141da177e4SLinus Torvalds memmove(extent_nis, extent_nis + 1, (base_ni->nr_extents - i) *
28151da177e4SLinus Torvalds sizeof(ntfs_inode*));
28161da177e4SLinus Torvalds err = 0;
28171da177e4SLinus Torvalds break;
28181da177e4SLinus Torvalds }
28191da177e4SLinus Torvalds
28204e5e529aSIngo Molnar mutex_unlock(&base_ni->extent_lock);
28211da177e4SLinus Torvalds
28221da177e4SLinus Torvalds if (unlikely(err)) {
28231da177e4SLinus Torvalds ntfs_error(vol->sb, "Extent inode 0x%lx is not attached to "
28241da177e4SLinus Torvalds "its base inode 0x%lx.", mft_no,
28251da177e4SLinus Torvalds base_ni->mft_no);
28261da177e4SLinus Torvalds BUG();
28271da177e4SLinus Torvalds }
28281da177e4SLinus Torvalds
28291da177e4SLinus Torvalds /*
28301da177e4SLinus Torvalds * The extent inode is no longer attached to the base inode so no one
28311da177e4SLinus Torvalds * can get a reference to it any more.
28321da177e4SLinus Torvalds */
28331da177e4SLinus Torvalds
28341da177e4SLinus Torvalds /* Mark the mft record as not in use. */
283563cd8854SHarvey Harrison m->flags &= ~MFT_RECORD_IN_USE;
28361da177e4SLinus Torvalds
28371da177e4SLinus Torvalds /* Increment the sequence number, skipping zero, if it is not zero. */
28381da177e4SLinus Torvalds old_seq_no = m->sequence_number;
28391da177e4SLinus Torvalds seq_no = le16_to_cpu(old_seq_no);
28401da177e4SLinus Torvalds if (seq_no == 0xffff)
28411da177e4SLinus Torvalds seq_no = 1;
28421da177e4SLinus Torvalds else if (seq_no)
28431da177e4SLinus Torvalds seq_no++;
28441da177e4SLinus Torvalds m->sequence_number = cpu_to_le16(seq_no);
28451da177e4SLinus Torvalds
28461da177e4SLinus Torvalds /*
28471da177e4SLinus Torvalds * Set the ntfs inode dirty and write it out. We do not need to worry
28481da177e4SLinus Torvalds * about the base inode here since whatever caused the extent mft
28491da177e4SLinus Torvalds * record to be freed is guaranteed to do it already.
28501da177e4SLinus Torvalds */
28511da177e4SLinus Torvalds NInoSetDirty(ni);
28521da177e4SLinus Torvalds err = write_mft_record(ni, m, 0);
28531da177e4SLinus Torvalds if (unlikely(err)) {
28541da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to write mft record 0x%lx, not "
28551da177e4SLinus Torvalds "freeing.", mft_no);
28561da177e4SLinus Torvalds goto rollback;
28571da177e4SLinus Torvalds }
28581da177e4SLinus Torvalds rollback_error:
28591da177e4SLinus Torvalds /* Unmap and throw away the now freed extent inode. */
28601da177e4SLinus Torvalds unmap_extent_mft_record(ni);
28611da177e4SLinus Torvalds ntfs_clear_extent_inode(ni);
28621da177e4SLinus Torvalds
28631da177e4SLinus Torvalds /* Clear the bit in the $MFT/$BITMAP corresponding to this record. */
28641da177e4SLinus Torvalds down_write(&vol->mftbmp_lock);
28651da177e4SLinus Torvalds err = ntfs_bitmap_clear_bit(vol->mftbmp_ino, mft_no);
28661da177e4SLinus Torvalds up_write(&vol->mftbmp_lock);
28671da177e4SLinus Torvalds if (unlikely(err)) {
28681da177e4SLinus Torvalds /*
28691da177e4SLinus Torvalds * The extent inode is gone but we failed to deallocate it in
28701da177e4SLinus Torvalds * the mft bitmap. Just emit a warning and leave the volume
28711da177e4SLinus Torvalds * dirty on umount.
28721da177e4SLinus Torvalds */
28731da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to clear bit in mft bitmap.%s", es);
28741da177e4SLinus Torvalds NVolSetErrors(vol);
28751da177e4SLinus Torvalds }
28761da177e4SLinus Torvalds return 0;
28771da177e4SLinus Torvalds rollback:
28781da177e4SLinus Torvalds /* Rollback what we did... */
28794e5e529aSIngo Molnar mutex_lock(&base_ni->extent_lock);
28801da177e4SLinus Torvalds extent_nis = base_ni->ext.extent_ntfs_inos;
28811da177e4SLinus Torvalds if (!(base_ni->nr_extents & 3)) {
28821da177e4SLinus Torvalds int new_size = (base_ni->nr_extents + 4) * sizeof(ntfs_inode*);
28831da177e4SLinus Torvalds
2884f52720caSPanagiotis Issaris extent_nis = kmalloc(new_size, GFP_NOFS);
28851da177e4SLinus Torvalds if (unlikely(!extent_nis)) {
28861da177e4SLinus Torvalds ntfs_error(vol->sb, "Failed to allocate internal "
28871da177e4SLinus Torvalds "buffer during rollback.%s", es);
28884e5e529aSIngo Molnar mutex_unlock(&base_ni->extent_lock);
28891da177e4SLinus Torvalds NVolSetErrors(vol);
28901da177e4SLinus Torvalds goto rollback_error;
28911da177e4SLinus Torvalds }
28921da177e4SLinus Torvalds if (base_ni->nr_extents) {
28931da177e4SLinus Torvalds BUG_ON(!base_ni->ext.extent_ntfs_inos);
28941da177e4SLinus Torvalds memcpy(extent_nis, base_ni->ext.extent_ntfs_inos,
28951da177e4SLinus Torvalds new_size - 4 * sizeof(ntfs_inode*));
28961da177e4SLinus Torvalds kfree(base_ni->ext.extent_ntfs_inos);
28971da177e4SLinus Torvalds }
28981da177e4SLinus Torvalds base_ni->ext.extent_ntfs_inos = extent_nis;
28991da177e4SLinus Torvalds }
29001da177e4SLinus Torvalds m->flags |= MFT_RECORD_IN_USE;
29011da177e4SLinus Torvalds m->sequence_number = old_seq_no;
29021da177e4SLinus Torvalds extent_nis[base_ni->nr_extents++] = ni;
29034e5e529aSIngo Molnar mutex_unlock(&base_ni->extent_lock);
29041da177e4SLinus Torvalds mark_mft_record_dirty(ni);
29051da177e4SLinus Torvalds return err;
29061da177e4SLinus Torvalds }
29071da177e4SLinus Torvalds #endif /* NTFS_RW */
2908