VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

More than 9999 Commits   32 Branches   54 Tags
a1d312de7780e (Thomas Gleixner    2019-05-22 09:51:42 +0200    1) // SPDX-License-Identifier: GPL-2.0-or-later
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700    2) /**
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700    3)  * mft.c - NTFS kernel mft record operations. Part of the Linux-NTFS project.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700    4)  *
9b556248ecb05 (Anton Altaparmakov 2012-02-24 09:17:09 +0000    5)  * Copyright (c) 2001-2012 Anton Altaparmakov and Tuxera Inc.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700    6)  * Copyright (c) 2002 Richard Russon
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700    7)  */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700    8) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700    9) #include <linux/buffer_head.h>
5a0e3ad6af866 (Tejun Heo          2010-03-24 17:04:11 +0900   10) #include <linux/slab.h>
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   11) #include <linux/swap.h>
be297968da22c (Christoph Hellwig  2016-11-01 07:40:16 -0600   12) #include <linux/bio.h>
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   13) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   14) #include "attrib.h"
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   15) #include "aops.h"
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   16) #include "bitmap.h"
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   17) #include "debug.h"
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   18) #include "dir.h"
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   19) #include "lcnalloc.h"
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   20) #include "malloc.h"
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   21) #include "mft.h"
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   22) #include "ntfs.h"
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   23) 
ab62ef82ea49b (Kees Cook          2018-08-17 15:44:14 -0700   24) #define MAX_BHS	(PAGE_SIZE / NTFS_BLOCK_SIZE)
ab62ef82ea49b (Kees Cook          2018-08-17 15:44:14 -0700   25) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   26) /**
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   27)  * map_mft_record_page - map the page in which a specific mft record resides
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   28)  * @ni:		ntfs inode whose mft record page to map
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   29)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   30)  * This maps the page in which the mft record of the ntfs inode @ni is situated
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   31)  * and returns a pointer to the mft record within the mapped page.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   32)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   33)  * Return value needs to be checked with IS_ERR() and if that is true PTR_ERR()
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   34)  * contains the negative error code returned.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   35)  */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   36) static inline MFT_RECORD *map_mft_record_page(ntfs_inode *ni)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   37) {
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000   38) 	loff_t i_size;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   39) 	ntfs_volume *vol = ni->vol;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   40) 	struct inode *mft_vi = vol->mft_ino;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   41) 	struct page *page;
69b41e3c0223b (Anton Altaparmakov 2005-10-04 14:01:14 +0100   42) 	unsigned long index, end_index;
69b41e3c0223b (Anton Altaparmakov 2005-10-04 14:01:14 +0100   43) 	unsigned ofs;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   44) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   45) 	BUG_ON(ni->page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   46) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   47) 	 * The index into the page cache and the offset within the page cache
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   48) 	 * page of the wanted mft record. FIXME: We need to check for
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   49) 	 * overflowing the unsigned long, but I don't think we would ever get
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   50) 	 * here if the volume was that big...
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   51) 	 */
c394e458b6963 (Anton Altaparmakov 2005-10-04 13:08:53 +0100   52) 	index = (u64)ni->mft_no << vol->mft_record_size_bits >>
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300   53) 			PAGE_SHIFT;
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300   54) 	ofs = (ni->mft_no << vol->mft_record_size_bits) & ~PAGE_MASK;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   55) 
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000   56) 	i_size = i_size_read(mft_vi);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   57) 	/* The maximum valid index into the page cache for $MFT's data. */
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300   58) 	end_index = i_size >> PAGE_SHIFT;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   59) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   60) 	/* If the wanted index is out of bounds the mft record doesn't exist. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   61) 	if (unlikely(index >= end_index)) {
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300   62) 		if (index > end_index || (i_size & ~PAGE_MASK) < ofs +
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000   63) 				vol->mft_record_size) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   64) 			page = ERR_PTR(-ENOENT);
25985edcedea6 (Lucas De Marchi    2011-03-30 22:57:33 -0300   65) 			ntfs_error(vol->sb, "Attempt to read mft record 0x%lx, "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   66) 					"which is beyond the end of the mft.  "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   67) 					"This is probably a bug in the ntfs "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   68) 					"driver.", ni->mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   69) 			goto err_out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   70) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   71) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   72) 	/* Read, map, and pin the page. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   73) 	page = ntfs_map_page(mft_vi->i_mapping, index);
cc22c800e15b0 (Denis Efremov      2019-09-25 16:49:43 -0700   74) 	if (!IS_ERR(page)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   75) 		/* Catch multi sector transfer fixup errors. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   76) 		if (likely(ntfs_is_mft_recordp((le32*)(page_address(page) +
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   77) 				ofs)))) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   78) 			ni->page = page;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   79) 			ni->page_ofs = ofs;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   80) 			return page_address(page) + ofs;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   81) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   82) 		ntfs_error(vol->sb, "Mft record 0x%lx is corrupt.  "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   83) 				"Run chkdsk.", ni->mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   84) 		ntfs_unmap_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   85) 		page = ERR_PTR(-EIO);
f95c4018fd4b0 (Anton Altaparmakov 2006-03-23 15:59:32 +0000   86) 		NVolSetErrors(vol);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   87) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   88) err_out:
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   89) 	ni->page = NULL;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   90) 	ni->page_ofs = 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   91) 	return (void*)page;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   92) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   93) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   94) /**
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   95)  * map_mft_record - map, pin and lock an mft record
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   96)  * @ni:		ntfs inode whose MFT record to map
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   97)  *
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000   98)  * First, take the mrec_lock mutex.  We might now be sleeping, while waiting
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000   99)  * for the mutex if it was already locked by someone else.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  100)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  101)  * The page of the record is mapped using map_mft_record_page() before being
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  102)  * returned to the caller.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  103)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  104)  * This in turn uses ntfs_map_page() to get the page containing the wanted mft
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  105)  * record (it in turn calls read_cache_page() which reads it in from disk if
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  106)  * necessary, increments the use count on the page so that it cannot disappear
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  107)  * under us and returns a reference to the page cache page).
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  108)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  109)  * If read_cache_page() invokes ntfs_readpage() to load the page from disk, it
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  110)  * sets PG_locked and clears PG_uptodate on the page. Once I/O has completed
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  111)  * and the post-read mst fixups on each mft record in the page have been
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  112)  * performed, the page gets PG_uptodate set and PG_locked cleared (this is done
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  113)  * in our asynchronous I/O completion handler end_buffer_read_mft_async()).
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  114)  * ntfs_map_page() waits for PG_locked to become clear and checks if
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  115)  * PG_uptodate is set and returns an error code if not. This provides
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  116)  * sufficient protection against races when reading/using the page.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  117)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  118)  * However there is the write mapping to think about. Doing the above described
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  119)  * checking here will be fine, because when initiating the write we will set
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  120)  * PG_locked and clear PG_uptodate making sure nobody is touching the page
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  121)  * contents. Doing the locking this way means that the commit to disk code in
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  122)  * the page cache code paths is automatically sufficiently locked with us as
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  123)  * we will not touch a page that has been locked or is not uptodate. The only
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  124)  * locking problem then is them locking the page while we are accessing it.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  125)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  126)  * So that code will end up having to own the mrec_lock of all mft
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  127)  * records/inodes present in the page before I/O can proceed. In that case we
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  128)  * wouldn't need to bother with PG_locked and PG_uptodate as nobody will be
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000  129)  * accessing anything without owning the mrec_lock mutex.  But we do need to
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000  130)  * use them because of the read_cache_page() invocation and the code becomes so
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000  131)  * much simpler this way that it is well worth it.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  132)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  133)  * The mft record is now ours and we return a pointer to it. You need to check
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  134)  * the returned pointer with IS_ERR() and if that is true, PTR_ERR() will return
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  135)  * the error code.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  136)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  137)  * NOTE: Caller is responsible for setting the mft record dirty before calling
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  138)  * unmap_mft_record(). This is obviously only necessary if the caller really
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  139)  * modified the mft record...
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  140)  * Q: Do we want to recycle one of the VFS inode state bits instead?
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  141)  * A: No, the inode ones mean we want to change the mft record, not we want to
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  142)  * write it out.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  143)  */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  144) MFT_RECORD *map_mft_record(ntfs_inode *ni)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  145) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  146) 	MFT_RECORD *m;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  147) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  148) 	ntfs_debug("Entering for mft_no 0x%lx.", ni->mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  149) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  150) 	/* Make sure the ntfs inode doesn't go away. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  151) 	atomic_inc(&ni->count);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  152) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  153) 	/* Serialize access to this mft record. */
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000  154) 	mutex_lock(&ni->mrec_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  155) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  156) 	m = map_mft_record_page(ni);
cc22c800e15b0 (Denis Efremov      2019-09-25 16:49:43 -0700  157) 	if (!IS_ERR(m))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  158) 		return m;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  159) 
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000  160) 	mutex_unlock(&ni->mrec_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  161) 	atomic_dec(&ni->count);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  162) 	ntfs_error(ni->vol->sb, "Failed with error code %lu.", -PTR_ERR(m));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  163) 	return m;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  164) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  165) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  166) /**
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  167)  * unmap_mft_record_page - unmap the page in which a specific mft record resides
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  168)  * @ni:		ntfs inode whose mft record page to unmap
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  169)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  170)  * This unmaps the page in which the mft record of the ntfs inode @ni is
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  171)  * situated and returns. This is a NOOP if highmem is not configured.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  172)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  173)  * The unmap happens via ntfs_unmap_page() which in turn decrements the use
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  174)  * count on the page thus releasing it from the pinned state.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  175)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  176)  * We do not actually unmap the page from memory of course, as that will be
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  177)  * done by the page cache code itself when memory pressure increases or
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  178)  * whatever.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  179)  */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  180) static inline void unmap_mft_record_page(ntfs_inode *ni)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  181) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  182) 	BUG_ON(!ni->page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  183) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  184) 	// TODO: If dirty, blah...
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  185) 	ntfs_unmap_page(ni->page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  186) 	ni->page = NULL;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  187) 	ni->page_ofs = 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  188) 	return;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  189) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  190) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  191) /**
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  192)  * unmap_mft_record - release a mapped mft record
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  193)  * @ni:		ntfs inode whose MFT record to unmap
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  194)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  195)  * We release the page mapping and the mrec_lock mutex which unmaps the mft
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  196)  * record and releases it for others to get hold of. We also release the ntfs
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  197)  * inode by decrementing the ntfs inode reference count.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  198)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  199)  * NOTE: If caller has modified the mft record, it is imperative to set the mft
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  200)  * record dirty BEFORE calling unmap_mft_record().
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  201)  */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  202) void unmap_mft_record(ntfs_inode *ni)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  203) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  204) 	struct page *page = ni->page;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  205) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  206) 	BUG_ON(!page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  207) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  208) 	ntfs_debug("Entering for mft_no 0x%lx.", ni->mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  209) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  210) 	unmap_mft_record_page(ni);
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000  211) 	mutex_unlock(&ni->mrec_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  212) 	atomic_dec(&ni->count);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  213) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  214) 	 * If pure ntfs_inode, i.e. no vfs inode attached, we leave it to
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  215) 	 * ntfs_clear_extent_inode() in the extent inode case, and to the
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  216) 	 * caller in the non-extent, yet pure ntfs inode case, to do the actual
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  217) 	 * tear down of all structures and freeing of all allocated memory.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  218) 	 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  219) 	return;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  220) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  221) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  222) /**
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  223)  * map_extent_mft_record - load an extent inode and attach it to its base
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  224)  * @base_ni:	base ntfs inode
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  225)  * @mref:	mft reference of the extent inode to load
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  226)  * @ntfs_ino:	on successful return, pointer to the ntfs_inode structure
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  227)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  228)  * Load the extent mft record @mref and attach it to its base inode @base_ni.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  229)  * Return the mapped extent mft record if IS_ERR(result) is false.  Otherwise
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  230)  * PTR_ERR(result) gives the negative error code.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  231)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  232)  * On successful return, @ntfs_ino contains a pointer to the ntfs_inode
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  233)  * structure of the mapped extent inode.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  234)  */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  235) MFT_RECORD *map_extent_mft_record(ntfs_inode *base_ni, MFT_REF mref,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  236) 		ntfs_inode **ntfs_ino)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  237) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  238) 	MFT_RECORD *m;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  239) 	ntfs_inode *ni = NULL;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  240) 	ntfs_inode **extent_nis = NULL;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  241) 	int i;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  242) 	unsigned long mft_no = MREF(mref);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  243) 	u16 seq_no = MSEQNO(mref);
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700  244) 	bool destroy_ni = false;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  245) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  246) 	ntfs_debug("Mapping extent mft record 0x%lx (base mft record 0x%lx).",
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  247) 			mft_no, base_ni->mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  248) 	/* Make sure the base ntfs inode doesn't go away. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  249) 	atomic_inc(&base_ni->count);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  250) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  251) 	 * Check if this extent inode has already been added to the base inode,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  252) 	 * in which case just return it. If not found, add it to the base
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  253) 	 * inode before returning it.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  254) 	 */
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000  255) 	mutex_lock(&base_ni->extent_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  256) 	if (base_ni->nr_extents > 0) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  257) 		extent_nis = base_ni->ext.extent_ntfs_inos;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  258) 		for (i = 0; i < base_ni->nr_extents; i++) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  259) 			if (mft_no != extent_nis[i]->mft_no)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  260) 				continue;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  261) 			ni = extent_nis[i];
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  262) 			/* Make sure the ntfs inode doesn't go away. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  263) 			atomic_inc(&ni->count);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  264) 			break;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  265) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  266) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  267) 	if (likely(ni != NULL)) {
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000  268) 		mutex_unlock(&base_ni->extent_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  269) 		atomic_dec(&base_ni->count);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  270) 		/* We found the record; just have to map and return it. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  271) 		m = map_mft_record(ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  272) 		/* map_mft_record() has incremented this on success. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  273) 		atomic_dec(&ni->count);
cc22c800e15b0 (Denis Efremov      2019-09-25 16:49:43 -0700  274) 		if (!IS_ERR(m)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  275) 			/* Verify the sequence number. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  276) 			if (likely(le16_to_cpu(m->sequence_number) == seq_no)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  277) 				ntfs_debug("Done 1.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  278) 				*ntfs_ino = ni;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  279) 				return m;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  280) 			}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  281) 			unmap_mft_record(ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  282) 			ntfs_error(base_ni->vol->sb, "Found stale extent mft "
b6ad6c52fe36a (Anton Altaparmakov 2005-02-15 10:08:43 +0000  283) 					"reference! Corrupt filesystem. "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  284) 					"Run chkdsk.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  285) 			return ERR_PTR(-EIO);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  286) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  287) map_err_out:
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  288) 		ntfs_error(base_ni->vol->sb, "Failed to map extent "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  289) 				"mft record, error code %ld.", -PTR_ERR(m));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  290) 		return m;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  291) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  292) 	/* Record wasn't there. Get a new ntfs inode and initialize it. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  293) 	ni = ntfs_new_extent_inode(base_ni->vol->sb, mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  294) 	if (unlikely(!ni)) {
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000  295) 		mutex_unlock(&base_ni->extent_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  296) 		atomic_dec(&base_ni->count);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  297) 		return ERR_PTR(-ENOMEM);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  298) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  299) 	ni->vol = base_ni->vol;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  300) 	ni->seq_no = seq_no;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  301) 	ni->nr_extents = -1;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  302) 	ni->ext.base_ntfs_ino = base_ni;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  303) 	/* Now map the record. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  304) 	m = map_mft_record(ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  305) 	if (IS_ERR(m)) {
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000  306) 		mutex_unlock(&base_ni->extent_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  307) 		atomic_dec(&base_ni->count);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  308) 		ntfs_clear_extent_inode(ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  309) 		goto map_err_out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  310) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  311) 	/* Verify the sequence number if it is present. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  312) 	if (seq_no && (le16_to_cpu(m->sequence_number) != seq_no)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  313) 		ntfs_error(base_ni->vol->sb, "Found stale extent mft "
b6ad6c52fe36a (Anton Altaparmakov 2005-02-15 10:08:43 +0000  314) 				"reference! Corrupt filesystem. Run chkdsk.");
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700  315) 		destroy_ni = true;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  316) 		m = ERR_PTR(-EIO);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  317) 		goto unm_err_out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  318) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  319) 	/* Attach extent inode to base inode, reallocating memory if needed. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  320) 	if (!(base_ni->nr_extents & 3)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  321) 		ntfs_inode **tmp;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  322) 		int new_size = (base_ni->nr_extents + 4) * sizeof(ntfs_inode *);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  323) 
f52720ca5f485 (Panagiotis Issaris 2006-09-27 01:49:39 -0700  324) 		tmp = kmalloc(new_size, GFP_NOFS);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  325) 		if (unlikely(!tmp)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  326) 			ntfs_error(base_ni->vol->sb, "Failed to allocate "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  327) 					"internal buffer.");
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700  328) 			destroy_ni = true;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  329) 			m = ERR_PTR(-ENOMEM);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  330) 			goto unm_err_out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  331) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  332) 		if (base_ni->nr_extents) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  333) 			BUG_ON(!base_ni->ext.extent_ntfs_inos);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  334) 			memcpy(tmp, base_ni->ext.extent_ntfs_inos, new_size -
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  335) 					4 * sizeof(ntfs_inode *));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  336) 			kfree(base_ni->ext.extent_ntfs_inos);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  337) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  338) 		base_ni->ext.extent_ntfs_inos = tmp;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  339) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  340) 	base_ni->ext.extent_ntfs_inos[base_ni->nr_extents++] = ni;
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000  341) 	mutex_unlock(&base_ni->extent_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  342) 	atomic_dec(&base_ni->count);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  343) 	ntfs_debug("Done 2.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  344) 	*ntfs_ino = ni;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  345) 	return m;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  346) unm_err_out:
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  347) 	unmap_mft_record(ni);
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000  348) 	mutex_unlock(&base_ni->extent_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  349) 	atomic_dec(&base_ni->count);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  350) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  351) 	 * If the extent inode was not attached to the base inode we need to
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  352) 	 * release it or we will leak memory.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  353) 	 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  354) 	if (destroy_ni)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  355) 		ntfs_clear_extent_inode(ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  356) 	return m;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  357) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  358) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  359) #ifdef NTFS_RW
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  360) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  361) /**
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  362)  * __mark_mft_record_dirty - set the mft record and the page containing it dirty
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  363)  * @ni:		ntfs inode describing the mapped mft record
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  364)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  365)  * Internal function.  Users should call mark_mft_record_dirty() instead.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  366)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  367)  * Set the mapped (extent) mft record of the (base or extent) ntfs inode @ni,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  368)  * as well as the page containing the mft record, dirty.  Also, mark the base
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  369)  * vfs inode dirty.  This ensures that any changes to the mft record are
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  370)  * written out to disk.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  371)  *
2c2acd2d19359 (Christoph Hellwig  2018-02-21 07:54:47 -0800  372)  * NOTE:  We only set I_DIRTY_DATASYNC (and not I_DIRTY_PAGES)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  373)  * on the base vfs inode, because even though file data may have been modified,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  374)  * it is dirty in the inode meta data rather than the data page cache of the
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  375)  * inode, and thus there are no data pages that need writing out.  Therefore, a
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  376)  * full mark_inode_dirty() is overkill.  A mark_inode_dirty_sync(), on the
ebbbf757c6b85 (Jan Kara           2009-08-18 18:13:58 +0200  377)  * other hand, is not sufficient, because ->write_inode needs to be called even
ebbbf757c6b85 (Jan Kara           2009-08-18 18:13:58 +0200  378)  * in case of fdatasync. This needs to happen or the file data would not
ebbbf757c6b85 (Jan Kara           2009-08-18 18:13:58 +0200  379)  * necessarily hit the device synchronously, even though the vfs inode has the
ebbbf757c6b85 (Jan Kara           2009-08-18 18:13:58 +0200  380)  * O_SYNC flag set.  Also, I_DIRTY_DATASYNC simply "feels" better than just
ebbbf757c6b85 (Jan Kara           2009-08-18 18:13:58 +0200  381)  * I_DIRTY_SYNC, since the file data has not actually hit the block device yet,
ebbbf757c6b85 (Jan Kara           2009-08-18 18:13:58 +0200  382)  * which is not what I_DIRTY_SYNC on its own would suggest.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  383)  */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  384) void __mark_mft_record_dirty(ntfs_inode *ni)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  385) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  386) 	ntfs_inode *base_ni;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  387) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  388) 	ntfs_debug("Entering for inode 0x%lx.", ni->mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  389) 	BUG_ON(NInoAttr(ni));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  390) 	mark_ntfs_record_dirty(ni->page, ni->page_ofs);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  391) 	/* Determine the base vfs inode and mark it dirty, too. */
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000  392) 	mutex_lock(&ni->extent_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  393) 	if (likely(ni->nr_extents >= 0))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  394) 		base_ni = ni;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  395) 	else
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  396) 		base_ni = ni->ext.base_ntfs_ino;
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000  397) 	mutex_unlock(&ni->extent_lock);
2c2acd2d19359 (Christoph Hellwig  2018-02-21 07:54:47 -0800  398) 	__mark_inode_dirty(VFS_I(base_ni), I_DIRTY_DATASYNC);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  399) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  400) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  401) static const char *ntfs_please_email = "Please email "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  402) 		"linux-ntfs-dev@lists.sourceforge.net and say that you saw "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  403) 		"this message.  Thank you.";
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  404) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  405) /**
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  406)  * ntfs_sync_mft_mirror_umount - synchronise an mft record to the mft mirror
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  407)  * @vol:	ntfs volume on which the mft record to synchronize resides
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  408)  * @mft_no:	mft record number of mft record to synchronize
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  409)  * @m:		mapped, mst protected (extent) mft record to synchronize
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  410)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  411)  * Write the mapped, mst protected (extent) mft record @m with mft record
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  412)  * number @mft_no to the mft mirror ($MFTMirr) of the ntfs volume @vol,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  413)  * bypassing the page cache and the $MFTMirr inode itself.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  414)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  415)  * This function is only for use at umount time when the mft mirror inode has
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  416)  * already been disposed off.  We BUG() if we are called while the mft mirror
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  417)  * inode is still attached to the volume.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  418)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  419)  * On success return 0.  On error return -errno.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  420)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  421)  * NOTE:  This function is not implemented yet as I am not convinced it can
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  422)  * actually be triggered considering the sequence of commits we do in super.c::
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  423)  * ntfs_put_super().  But just in case we provide this place holder as the
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  424)  * alternative would be either to BUG() or to get a NULL pointer dereference
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  425)  * and Oops.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  426)  */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  427) static int ntfs_sync_mft_mirror_umount(ntfs_volume *vol,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  428) 		const unsigned long mft_no, MFT_RECORD *m)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  429) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  430) 	BUG_ON(vol->mftmirr_ino);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  431) 	ntfs_error(vol->sb, "Umount time mft mirror syncing is not "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  432) 			"implemented yet.  %s", ntfs_please_email);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  433) 	return -EOPNOTSUPP;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  434) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  435) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  436) /**
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  437)  * ntfs_sync_mft_mirror - synchronize an mft record to the mft mirror
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  438)  * @vol:	ntfs volume on which the mft record to synchronize resides
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  439)  * @mft_no:	mft record number of mft record to synchronize
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  440)  * @m:		mapped, mst protected (extent) mft record to synchronize
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  441)  * @sync:	if true, wait for i/o completion
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  442)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  443)  * Write the mapped, mst protected (extent) mft record @m with mft record
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  444)  * number @mft_no to the mft mirror ($MFTMirr) of the ntfs volume @vol.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  445)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  446)  * On success return 0.  On error return -errno and set the volume errors flag
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  447)  * in the ntfs volume @vol.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  448)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  449)  * NOTE:  We always perform synchronous i/o and ignore the @sync parameter.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  450)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  451)  * TODO:  If @sync is false, want to do truly asynchronous i/o, i.e. just
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  452)  * schedule i/o via ->writepage or do it via kntfsd or whatever.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  453)  */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  454) int ntfs_sync_mft_mirror(ntfs_volume *vol, const unsigned long mft_no,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  455) 		MFT_RECORD *m, int sync)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  456) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  457) 	struct page *page;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  458) 	unsigned int blocksize = vol->sb->s_blocksize;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  459) 	int max_bhs = vol->mft_record_size / blocksize;
ab62ef82ea49b (Kees Cook          2018-08-17 15:44:14 -0700  460) 	struct buffer_head *bhs[MAX_BHS];
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  461) 	struct buffer_head *bh, *head;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  462) 	u8 *kmirr;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  463) 	runlist_element *rl;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  464) 	unsigned int block_start, block_end, m_start, m_end, page_ofs;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  465) 	int i_bhs, nr_bhs, err = 0;
78af34f03d33d (Anton Altaparmakov 2006-02-24 10:32:33 +0000  466) 	unsigned char blocksize_bits = vol->sb->s_blocksize_bits;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  467) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  468) 	ntfs_debug("Entering for inode 0x%lx.", mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  469) 	BUG_ON(!max_bhs);
ab62ef82ea49b (Kees Cook          2018-08-17 15:44:14 -0700  470) 	if (WARN_ON(max_bhs > MAX_BHS))
ab62ef82ea49b (Kees Cook          2018-08-17 15:44:14 -0700  471) 		return -EINVAL;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  472) 	if (unlikely(!vol->mftmirr_ino)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  473) 		/* This could happen during umount... */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  474) 		err = ntfs_sync_mft_mirror_umount(vol, mft_no, m);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  475) 		if (likely(!err))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  476) 			return err;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  477) 		goto err_out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  478) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  479) 	/* Get the page containing the mirror copy of the mft record @m. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  480) 	page = ntfs_map_page(vol->mftmirr_ino->i_mapping, mft_no >>
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300  481) 			(PAGE_SHIFT - vol->mft_record_size_bits));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  482) 	if (IS_ERR(page)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  483) 		ntfs_error(vol->sb, "Failed to map mft mirror page.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  484) 		err = PTR_ERR(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  485) 		goto err_out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  486) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  487) 	lock_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  488) 	BUG_ON(!PageUptodate(page));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  489) 	ClearPageUptodate(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  490) 	/* Offset of the mft mirror record inside the page. */
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300  491) 	page_ofs = (mft_no << vol->mft_record_size_bits) & ~PAGE_MASK;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  492) 	/* The address in the page of the mirror copy of the mft record @m. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  493) 	kmirr = page_address(page) + page_ofs;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  494) 	/* Copy the mst protected mft record to the mirror. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  495) 	memcpy(kmirr, m, vol->mft_record_size);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  496) 	/* Create uptodate buffers if not present. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  497) 	if (unlikely(!page_has_buffers(page))) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  498) 		struct buffer_head *tail;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  499) 
640ab98fb3629 (Jens Axboe         2017-09-27 05:40:16 -0600  500) 		bh = head = alloc_page_buffers(page, blocksize, true);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  501) 		do {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  502) 			set_buffer_uptodate(bh);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  503) 			tail = bh;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  504) 			bh = bh->b_this_page;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  505) 		} while (bh);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  506) 		tail->b_this_page = head;
14ed109e3f3da (Guoqing Jiang      2020-06-01 21:47:57 -0700  507) 		attach_page_private(page, head);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  508) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  509) 	bh = head = page_buffers(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  510) 	BUG_ON(!bh);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  511) 	rl = NULL;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  512) 	nr_bhs = 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  513) 	block_start = 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  514) 	m_start = kmirr - (u8*)page_address(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  515) 	m_end = m_start + vol->mft_record_size;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  516) 	do {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  517) 		block_end = block_start + blocksize;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  518) 		/* If the buffer is outside the mft record, skip it. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  519) 		if (block_end <= m_start)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  520) 			continue;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  521) 		if (unlikely(block_start >= m_end))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  522) 			break;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  523) 		/* Need to map the buffer if it is not mapped already. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  524) 		if (unlikely(!buffer_mapped(bh))) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  525) 			VCN vcn;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  526) 			LCN lcn;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  527) 			unsigned int vcn_ofs;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  528) 
e74589ac250e4 (Anton Altaparmakov 2005-08-16 16:38:28 +0100  529) 			bh->b_bdev = vol->sb->s_bdev;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  530) 			/* Obtain the vcn and offset of the current block. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  531) 			vcn = ((VCN)mft_no << vol->mft_record_size_bits) +
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  532) 					(block_start - m_start);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  533) 			vcn_ofs = vcn & vol->cluster_size_mask;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  534) 			vcn >>= vol->cluster_size_bits;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  535) 			if (!rl) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  536) 				down_read(&NTFS_I(vol->mftmirr_ino)->
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  537) 						runlist.lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  538) 				rl = NTFS_I(vol->mftmirr_ino)->runlist.rl;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  539) 				/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  540) 				 * $MFTMirr always has the whole of its runlist
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  541) 				 * in memory.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  542) 				 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  543) 				BUG_ON(!rl);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  544) 			}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  545) 			/* Seek to element containing target vcn. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  546) 			while (rl->length && rl[1].vcn <= vcn)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  547) 				rl++;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  548) 			lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  549) 			/* For $MFTMirr, only lcn >= 0 is a successful remap. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  550) 			if (likely(lcn >= 0)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  551) 				/* Setup buffer head to correct block. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  552) 				bh->b_blocknr = ((lcn <<
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  553) 						vol->cluster_size_bits) +
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  554) 						vcn_ofs) >> blocksize_bits;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  555) 				set_buffer_mapped(bh);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  556) 			} else {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  557) 				bh->b_blocknr = -1;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  558) 				ntfs_error(vol->sb, "Cannot write mft mirror "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  559) 						"record 0x%lx because its "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  560) 						"location on disk could not "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  561) 						"be determined (error code "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  562) 						"%lli).", mft_no,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  563) 						(long long)lcn);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  564) 				err = -EIO;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  565) 			}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  566) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  567) 		BUG_ON(!buffer_uptodate(bh));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  568) 		BUG_ON(!nr_bhs && (m_start != block_start));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  569) 		BUG_ON(nr_bhs >= max_bhs);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  570) 		bhs[nr_bhs++] = bh;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  571) 		BUG_ON((nr_bhs >= max_bhs) && (m_end != block_end));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  572) 	} while (block_start = block_end, (bh = bh->b_this_page) != head);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  573) 	if (unlikely(rl))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  574) 		up_read(&NTFS_I(vol->mftmirr_ino)->runlist.lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  575) 	if (likely(!err)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  576) 		/* Lock buffers and start synchronous write i/o on them. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  577) 		for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  578) 			struct buffer_head *tbh = bhs[i_bhs];
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  579) 
ca5de404ff036 (Nicholas Piggin    2008-08-02 12:02:13 +0200  580) 			if (!trylock_buffer(tbh))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  581) 				BUG();
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  582) 			BUG_ON(!buffer_uptodate(tbh));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  583) 			clear_buffer_dirty(tbh);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  584) 			get_bh(tbh);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  585) 			tbh->b_end_io = end_buffer_write_sync;
2a222ca992c35 (Mike Christie      2016-06-05 14:31:43 -0500  586) 			submit_bh(REQ_OP_WRITE, 0, tbh);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  587) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  588) 		/* Wait on i/o completion of buffers. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  589) 		for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  590) 			struct buffer_head *tbh = bhs[i_bhs];
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  591) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  592) 			wait_on_buffer(tbh);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  593) 			if (unlikely(!buffer_uptodate(tbh))) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  594) 				err = -EIO;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  595) 				/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  596) 				 * Set the buffer uptodate so the page and
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  597) 				 * buffer states do not become out of sync.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  598) 				 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  599) 				set_buffer_uptodate(tbh);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  600) 			}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  601) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  602) 	} else /* if (unlikely(err)) */ {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  603) 		/* Clean the buffers. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  604) 		for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  605) 			clear_buffer_dirty(bhs[i_bhs]);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  606) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  607) 	/* Current state: all buffers are clean, unlocked, and uptodate. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  608) 	/* Remove the mst protection fixups again. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  609) 	post_write_mst_fixup((NTFS_RECORD*)kmirr);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  610) 	flush_dcache_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  611) 	SetPageUptodate(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  612) 	unlock_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  613) 	ntfs_unmap_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  614) 	if (likely(!err)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  615) 		ntfs_debug("Done.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  616) 	} else {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  617) 		ntfs_error(vol->sb, "I/O error while writing mft mirror "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  618) 				"record 0x%lx!", mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  619) err_out:
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  620) 		ntfs_error(vol->sb, "Failed to synchronize $MFTMirr (error "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  621) 				"code %i).  Volume will be left marked dirty "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  622) 				"on umount.  Run ntfsfix on the partition "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  623) 				"after umounting to correct this.", -err);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  624) 		NVolSetErrors(vol);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  625) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  626) 	return err;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  627) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  628) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  629) /**
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  630)  * write_mft_record_nolock - write out a mapped (extent) mft record
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  631)  * @ni:		ntfs inode describing the mapped (extent) mft record
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  632)  * @m:		mapped (extent) mft record to write
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  633)  * @sync:	if true, wait for i/o completion
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  634)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  635)  * Write the mapped (extent) mft record @m described by the (regular or extent)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  636)  * ntfs inode @ni to backing store.  If the mft record @m has a counterpart in
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  637)  * the mft mirror, that is also updated.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  638)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  639)  * We only write the mft record if the ntfs inode @ni is dirty and the first
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  640)  * buffer belonging to its mft record is dirty, too.  We ignore the dirty state
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  641)  * of subsequent buffers because we could have raced with
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  642)  * fs/ntfs/aops.c::mark_ntfs_record_dirty().
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  643)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  644)  * On success, clean the mft record and return 0.  On error, leave the mft
a778f217328a7 (Anton Altaparmakov 2006-03-23 16:18:23 +0000  645)  * record dirty and return -errno.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  646)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  647)  * NOTE:  We always perform synchronous i/o and ignore the @sync parameter.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  648)  * However, if the mft record has a counterpart in the mft mirror and @sync is
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  649)  * true, we write the mft record, wait for i/o completion, and only then write
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  650)  * the mft mirror copy.  This ensures that if the system crashes either the mft
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  651)  * or the mft mirror will contain a self-consistent mft record @m.  If @sync is
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  652)  * false on the other hand, we start i/o on both and then wait for completion
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  653)  * on them.  This provides a speedup but no longer guarantees that you will end
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  654)  * up with a self-consistent mft record in the case of a crash but if you asked
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  655)  * for asynchronous writing you probably do not care about that anyway.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  656)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  657)  * TODO:  If @sync is false, want to do truly asynchronous i/o, i.e. just
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  658)  * schedule i/o via ->writepage or do it via kntfsd or whatever.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  659)  */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  660) int write_mft_record_nolock(ntfs_inode *ni, MFT_RECORD *m, int sync)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  661) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  662) 	ntfs_volume *vol = ni->vol;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  663) 	struct page *page = ni->page;
78af34f03d33d (Anton Altaparmakov 2006-02-24 10:32:33 +0000  664) 	unsigned int blocksize = vol->sb->s_blocksize;
78af34f03d33d (Anton Altaparmakov 2006-02-24 10:32:33 +0000  665) 	unsigned char blocksize_bits = vol->sb->s_blocksize_bits;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  666) 	int max_bhs = vol->mft_record_size / blocksize;
ab62ef82ea49b (Kees Cook          2018-08-17 15:44:14 -0700  667) 	struct buffer_head *bhs[MAX_BHS];
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  668) 	struct buffer_head *bh, *head;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  669) 	runlist_element *rl;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  670) 	unsigned int block_start, block_end, m_start, m_end;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  671) 	int i_bhs, nr_bhs, err = 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  672) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  673) 	ntfs_debug("Entering for inode 0x%lx.", ni->mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  674) 	BUG_ON(NInoAttr(ni));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  675) 	BUG_ON(!max_bhs);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  676) 	BUG_ON(!PageLocked(page));
ab62ef82ea49b (Kees Cook          2018-08-17 15:44:14 -0700  677) 	if (WARN_ON(max_bhs > MAX_BHS)) {
ab62ef82ea49b (Kees Cook          2018-08-17 15:44:14 -0700  678) 		err = -EINVAL;
ab62ef82ea49b (Kees Cook          2018-08-17 15:44:14 -0700  679) 		goto err_out;
ab62ef82ea49b (Kees Cook          2018-08-17 15:44:14 -0700  680) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  681) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  682) 	 * If the ntfs_inode is clean no need to do anything.  If it is dirty,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  683) 	 * mark it as clean now so that it can be redirtied later on if needed.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  684) 	 * There is no danger of races since the caller is holding the locks
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  685) 	 * for the mft record @m and the page it is in.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  686) 	 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  687) 	if (!NInoTestClearDirty(ni))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  688) 		goto done;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  689) 	bh = head = page_buffers(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  690) 	BUG_ON(!bh);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  691) 	rl = NULL;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  692) 	nr_bhs = 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  693) 	block_start = 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  694) 	m_start = ni->page_ofs;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  695) 	m_end = m_start + vol->mft_record_size;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  696) 	do {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  697) 		block_end = block_start + blocksize;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  698) 		/* If the buffer is outside the mft record, skip it. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  699) 		if (block_end <= m_start)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  700) 			continue;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  701) 		if (unlikely(block_start >= m_end))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  702) 			break;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  703) 		/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  704) 		 * If this block is not the first one in the record, we ignore
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  705) 		 * the buffer's dirty state because we could have raced with a
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  706) 		 * parallel mark_ntfs_record_dirty().
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  707) 		 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  708) 		if (block_start == m_start) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  709) 			/* This block is the first one in the record. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  710) 			if (!buffer_dirty(bh)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  711) 				BUG_ON(nr_bhs);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  712) 				/* Clean records are not written out. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  713) 				break;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  714) 			}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  715) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  716) 		/* Need to map the buffer if it is not mapped already. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  717) 		if (unlikely(!buffer_mapped(bh))) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  718) 			VCN vcn;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  719) 			LCN lcn;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  720) 			unsigned int vcn_ofs;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  721) 
e74589ac250e4 (Anton Altaparmakov 2005-08-16 16:38:28 +0100  722) 			bh->b_bdev = vol->sb->s_bdev;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  723) 			/* Obtain the vcn and offset of the current block. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  724) 			vcn = ((VCN)ni->mft_no << vol->mft_record_size_bits) +
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  725) 					(block_start - m_start);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  726) 			vcn_ofs = vcn & vol->cluster_size_mask;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  727) 			vcn >>= vol->cluster_size_bits;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  728) 			if (!rl) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  729) 				down_read(&NTFS_I(vol->mft_ino)->runlist.lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  730) 				rl = NTFS_I(vol->mft_ino)->runlist.rl;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  731) 				BUG_ON(!rl);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  732) 			}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  733) 			/* Seek to element containing target vcn. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  734) 			while (rl->length && rl[1].vcn <= vcn)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  735) 				rl++;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  736) 			lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  737) 			/* For $MFT, only lcn >= 0 is a successful remap. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  738) 			if (likely(lcn >= 0)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  739) 				/* Setup buffer head to correct block. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  740) 				bh->b_blocknr = ((lcn <<
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  741) 						vol->cluster_size_bits) +
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  742) 						vcn_ofs) >> blocksize_bits;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  743) 				set_buffer_mapped(bh);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  744) 			} else {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  745) 				bh->b_blocknr = -1;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  746) 				ntfs_error(vol->sb, "Cannot write mft record "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  747) 						"0x%lx because its location "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  748) 						"on disk could not be "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  749) 						"determined (error code %lli).",
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  750) 						ni->mft_no, (long long)lcn);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  751) 				err = -EIO;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  752) 			}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  753) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  754) 		BUG_ON(!buffer_uptodate(bh));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  755) 		BUG_ON(!nr_bhs && (m_start != block_start));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  756) 		BUG_ON(nr_bhs >= max_bhs);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  757) 		bhs[nr_bhs++] = bh;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  758) 		BUG_ON((nr_bhs >= max_bhs) && (m_end != block_end));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  759) 	} while (block_start = block_end, (bh = bh->b_this_page) != head);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  760) 	if (unlikely(rl))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  761) 		up_read(&NTFS_I(vol->mft_ino)->runlist.lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  762) 	if (!nr_bhs)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  763) 		goto done;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  764) 	if (unlikely(err))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  765) 		goto cleanup_out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  766) 	/* Apply the mst protection fixups. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  767) 	err = pre_write_mst_fixup((NTFS_RECORD*)m, vol->mft_record_size);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  768) 	if (err) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  769) 		ntfs_error(vol->sb, "Failed to apply mst fixups!");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  770) 		goto cleanup_out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  771) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  772) 	flush_dcache_mft_record_page(ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  773) 	/* Lock buffers and start synchronous write i/o on them. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  774) 	for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  775) 		struct buffer_head *tbh = bhs[i_bhs];
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  776) 
ca5de404ff036 (Nicholas Piggin    2008-08-02 12:02:13 +0200  777) 		if (!trylock_buffer(tbh))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  778) 			BUG();
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  779) 		BUG_ON(!buffer_uptodate(tbh));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  780) 		clear_buffer_dirty(tbh);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  781) 		get_bh(tbh);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  782) 		tbh->b_end_io = end_buffer_write_sync;
2a222ca992c35 (Mike Christie      2016-06-05 14:31:43 -0500  783) 		submit_bh(REQ_OP_WRITE, 0, tbh);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  784) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  785) 	/* Synchronize the mft mirror now if not @sync. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  786) 	if (!sync && ni->mft_no < vol->mftmirr_size)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  787) 		ntfs_sync_mft_mirror(vol, ni->mft_no, m, sync);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  788) 	/* Wait on i/o completion of buffers. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  789) 	for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  790) 		struct buffer_head *tbh = bhs[i_bhs];
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  791) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  792) 		wait_on_buffer(tbh);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  793) 		if (unlikely(!buffer_uptodate(tbh))) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  794) 			err = -EIO;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  795) 			/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  796) 			 * Set the buffer uptodate so the page and buffer
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  797) 			 * states do not become out of sync.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  798) 			 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  799) 			if (PageUptodate(page))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  800) 				set_buffer_uptodate(tbh);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  801) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  802) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  803) 	/* If @sync, now synchronize the mft mirror. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  804) 	if (sync && ni->mft_no < vol->mftmirr_size)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  805) 		ntfs_sync_mft_mirror(vol, ni->mft_no, m, sync);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  806) 	/* Remove the mst protection fixups again. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  807) 	post_write_mst_fixup((NTFS_RECORD*)m);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  808) 	flush_dcache_mft_record_page(ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  809) 	if (unlikely(err)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  810) 		/* I/O error during writing.  This is really bad! */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  811) 		ntfs_error(vol->sb, "I/O error while writing mft record "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  812) 				"0x%lx!  Marking base inode as bad.  You "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  813) 				"should unmount the volume and run chkdsk.",
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  814) 				ni->mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  815) 		goto err_out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  816) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  817) done:
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  818) 	ntfs_debug("Done.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  819) 	return 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  820) cleanup_out:
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  821) 	/* Clean the buffers. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  822) 	for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  823) 		clear_buffer_dirty(bhs[i_bhs]);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  824) err_out:
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  825) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  826) 	 * Current state: all buffers are clean, unlocked, and uptodate.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  827) 	 * The caller should mark the base inode as bad so that no more i/o
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  828) 	 * happens.  ->clear_inode() will still be invoked so all extent inodes
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  829) 	 * and other allocated memory will be freed.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  830) 	 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  831) 	if (err == -ENOMEM) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  832) 		ntfs_error(vol->sb, "Not enough memory to write mft record.  "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  833) 				"Redirtying so the write is retried later.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  834) 		mark_mft_record_dirty(ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  835) 		err = 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  836) 	} else
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  837) 		NVolSetErrors(vol);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  838) 	return err;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  839) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  840) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  841) /**
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  842)  * ntfs_may_write_mft_record - check if an mft record may be written out
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  843)  * @vol:	[IN]  ntfs volume on which the mft record to check resides
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  844)  * @mft_no:	[IN]  mft record number of the mft record to check
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  845)  * @m:		[IN]  mapped mft record to check
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  846)  * @locked_ni:	[OUT] caller has to unlock this ntfs inode if one is returned
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  847)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  848)  * Check if the mapped (base or extent) mft record @m with mft record number
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  849)  * @mft_no belonging to the ntfs volume @vol may be written out.  If necessary
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  850)  * and possible the ntfs inode of the mft record is locked and the base vfs
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  851)  * inode is pinned.  The locked ntfs inode is then returned in @locked_ni.  The
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  852)  * caller is responsible for unlocking the ntfs inode and unpinning the base
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  853)  * vfs inode.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  854)  *
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700  855)  * Return 'true' if the mft record may be written out and 'false' if not.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  856)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  857)  * The caller has locked the page and cleared the uptodate flag on it which
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  858)  * means that we can safely write out any dirty mft records that do not have
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  859)  * their inodes in icache as determined by ilookup5() as anyone
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  860)  * opening/creating such an inode would block when attempting to map the mft
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  861)  * record in read_cache_page() until we are finished with the write out.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  862)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  863)  * Here is a description of the tests we perform:
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  864)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  865)  * If the inode is found in icache we know the mft record must be a base mft
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700  866)  * record.  If it is dirty, we do not write it and return 'false' as the vfs
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  867)  * inode write paths will result in the access times being updated which would
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  868)  * cause the base mft record to be redirtied and written out again.  (We know
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  869)  * the access time update will modify the base mft record because Windows
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  870)  * chkdsk complains if the standard information attribute is not in the base
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  871)  * mft record.)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  872)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  873)  * If the inode is in icache and not dirty, we attempt to lock the mft record
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  874)  * and if we find the lock was already taken, it is not safe to write the mft
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700  875)  * record and we return 'false'.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  876)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  877)  * If we manage to obtain the lock we have exclusive access to the mft record,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  878)  * which also allows us safe writeout of the mft record.  We then set
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700  879)  * @locked_ni to the locked ntfs inode and return 'true'.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  880)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  881)  * Note we cannot just lock the mft record and sleep while waiting for the lock
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  882)  * because this would deadlock due to lock reversal (normally the mft record is
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  883)  * locked before the page is locked but we already have the page locked here
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  884)  * when we try to lock the mft record).
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  885)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  886)  * If the inode is not in icache we need to perform further checks.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  887)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  888)  * If the mft record is not a FILE record or it is a base mft record, we can
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700  889)  * safely write it and return 'true'.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  890)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  891)  * We now know the mft record is an extent mft record.  We check if the inode
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  892)  * corresponding to its base mft record is in icache and obtain a reference to
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700  893)  * it if it is.  If it is not, we can safely write it and return 'true'.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  894)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  895)  * We now have the base inode for the extent mft record.  We check if it has an
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  896)  * ntfs inode for the extent mft record attached and if not it is safe to write
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700  897)  * the extent mft record and we return 'true'.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  898)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  899)  * The ntfs inode for the extent mft record is attached to the base inode so we
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  900)  * attempt to lock the extent mft record and if we find the lock was already
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700  901)  * taken, it is not safe to write the extent mft record and we return 'false'.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  902)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  903)  * If we manage to obtain the lock we have exclusive access to the extent mft
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  904)  * record, which also allows us safe writeout of the extent mft record.  We
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  905)  * set the ntfs inode of the extent mft record clean and then set @locked_ni to
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700  906)  * the now locked ntfs inode and return 'true'.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  907)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  908)  * Note, the reason for actually writing dirty mft records here and not just
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  909)  * relying on the vfs inode dirty code paths is that we can have mft records
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  910)  * modified without them ever having actual inodes in memory.  Also we can have
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  911)  * dirty mft records with clean ntfs inodes in memory.  None of the described
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  912)  * cases would result in the dirty mft records being written out if we only
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  913)  * relied on the vfs inode dirty code paths.  And these cases can really occur
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  914)  * during allocation of new mft records and in particular when the
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  915)  * initialized_size of the $MFT/$DATA attribute is extended and the new space
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  916)  * is initialized using ntfs_mft_record_format().  The clean inode can then
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  917)  * appear if the mft record is reused for a new inode before it got written
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  918)  * out.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  919)  */
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700  920) bool ntfs_may_write_mft_record(ntfs_volume *vol, const unsigned long mft_no,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  921) 		const MFT_RECORD *m, ntfs_inode **locked_ni)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  922) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  923) 	struct super_block *sb = vol->sb;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  924) 	struct inode *mft_vi = vol->mft_ino;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  925) 	struct inode *vi;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  926) 	ntfs_inode *ni, *eni, **extent_nis;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  927) 	int i;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  928) 	ntfs_attr na;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  929) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  930) 	ntfs_debug("Entering for inode 0x%lx.", mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  931) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  932) 	 * Normally we do not return a locked inode so set @locked_ni to NULL.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  933) 	 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  934) 	BUG_ON(!locked_ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  935) 	*locked_ni = NULL;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  936) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  937) 	 * Check if the inode corresponding to this mft record is in the VFS
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  938) 	 * inode cache and obtain a reference to it if it is.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  939) 	 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  940) 	ntfs_debug("Looking for inode 0x%lx in icache.", mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  941) 	na.mft_no = mft_no;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  942) 	na.name = NULL;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  943) 	na.name_len = 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  944) 	na.type = AT_UNUSED;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  945) 	/*
ba6d2377c85c9 (Anton Altaparmakov 2005-06-26 22:12:02 +0100  946) 	 * Optimize inode 0, i.e. $MFT itself, since we have it in memory and
ba6d2377c85c9 (Anton Altaparmakov 2005-06-26 22:12:02 +0100  947) 	 * we get here for it rather often.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  948) 	 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  949) 	if (!mft_no) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  950) 		/* Balance the below iput(). */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  951) 		vi = igrab(mft_vi);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  952) 		BUG_ON(vi != mft_vi);
ba6d2377c85c9 (Anton Altaparmakov 2005-06-26 22:12:02 +0100  953) 	} else {
ba6d2377c85c9 (Anton Altaparmakov 2005-06-26 22:12:02 +0100  954) 		/*
ba6d2377c85c9 (Anton Altaparmakov 2005-06-26 22:12:02 +0100  955) 		 * Have to use ilookup5_nowait() since ilookup5() waits for the
ba6d2377c85c9 (Anton Altaparmakov 2005-06-26 22:12:02 +0100  956) 		 * inode lock which causes ntfs to deadlock when a concurrent
ba6d2377c85c9 (Anton Altaparmakov 2005-06-26 22:12:02 +0100  957) 		 * inode write via the inode dirty code paths and the page
ba6d2377c85c9 (Anton Altaparmakov 2005-06-26 22:12:02 +0100  958) 		 * dirty code path of the inode dirty code path when writing
ba6d2377c85c9 (Anton Altaparmakov 2005-06-26 22:12:02 +0100  959) 		 * $MFT occurs.
ba6d2377c85c9 (Anton Altaparmakov 2005-06-26 22:12:02 +0100  960) 		 */
1146f7e2dc15a (Luca Stefani       2020-08-06 23:17:53 -0700  961) 		vi = ilookup5_nowait(sb, mft_no, ntfs_test_inode, &na);
ba6d2377c85c9 (Anton Altaparmakov 2005-06-26 22:12:02 +0100  962) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  963) 	if (vi) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  964) 		ntfs_debug("Base inode 0x%lx is in icache.", mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  965) 		/* The inode is in icache. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  966) 		ni = NTFS_I(vi);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  967) 		/* Take a reference to the ntfs inode. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  968) 		atomic_inc(&ni->count);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  969) 		/* If the inode is dirty, do not write this record. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  970) 		if (NInoDirty(ni)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  971) 			ntfs_debug("Inode 0x%lx is dirty, do not write it.",
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  972) 					mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  973) 			atomic_dec(&ni->count);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  974) 			iput(vi);
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700  975) 			return false;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  976) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  977) 		ntfs_debug("Inode 0x%lx is not dirty.", mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  978) 		/* The inode is not dirty, try to take the mft record lock. */
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000  979) 		if (unlikely(!mutex_trylock(&ni->mrec_lock))) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  980) 			ntfs_debug("Mft record 0x%lx is already locked, do "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  981) 					"not write it.", mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  982) 			atomic_dec(&ni->count);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  983) 			iput(vi);
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700  984) 			return false;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  985) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  986) 		ntfs_debug("Managed to lock mft record 0x%lx, write it.",
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  987) 				mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  988) 		/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  989) 		 * The write has to occur while we hold the mft record lock so
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  990) 		 * return the locked ntfs inode.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  991) 		 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  992) 		*locked_ni = ni;
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700  993) 		return true;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  994) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  995) 	ntfs_debug("Inode 0x%lx is not in icache.", mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  996) 	/* The inode is not in icache. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  997) 	/* Write the record if it is not a mft record (type "FILE"). */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  998) 	if (!ntfs_is_mft_record(m->magic)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  999) 		ntfs_debug("Mft record 0x%lx is not a FILE record, write it.",
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1000) 				mft_no);
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700 1001) 		return true;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1002) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1003) 	/* Write the mft record if it is a base inode. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1004) 	if (!m->base_mft_record) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1005) 		ntfs_debug("Mft record 0x%lx is a base record, write it.",
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1006) 				mft_no);
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700 1007) 		return true;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1008) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1009) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1010) 	 * This is an extent mft record.  Check if the inode corresponding to
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1011) 	 * its base mft record is in icache and obtain a reference to it if it
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1012) 	 * is.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1013) 	 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1014) 	na.mft_no = MREF_LE(m->base_mft_record);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1015) 	ntfs_debug("Mft record 0x%lx is an extent record.  Looking for base "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1016) 			"inode 0x%lx in icache.", mft_no, na.mft_no);
ba6d2377c85c9 (Anton Altaparmakov 2005-06-26 22:12:02 +0100 1017) 	if (!na.mft_no) {
ba6d2377c85c9 (Anton Altaparmakov 2005-06-26 22:12:02 +0100 1018) 		/* Balance the below iput(). */
ba6d2377c85c9 (Anton Altaparmakov 2005-06-26 22:12:02 +0100 1019) 		vi = igrab(mft_vi);
ba6d2377c85c9 (Anton Altaparmakov 2005-06-26 22:12:02 +0100 1020) 		BUG_ON(vi != mft_vi);
ba6d2377c85c9 (Anton Altaparmakov 2005-06-26 22:12:02 +0100 1021) 	} else
1146f7e2dc15a (Luca Stefani       2020-08-06 23:17:53 -0700 1022) 		vi = ilookup5_nowait(sb, na.mft_no, ntfs_test_inode,
ba6d2377c85c9 (Anton Altaparmakov 2005-06-26 22:12:02 +0100 1023) 				&na);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1024) 	if (!vi) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1025) 		/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1026) 		 * The base inode is not in icache, write this extent mft
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1027) 		 * record.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1028) 		 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1029) 		ntfs_debug("Base inode 0x%lx is not in icache, write the "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1030) 				"extent record.", na.mft_no);
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700 1031) 		return true;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1032) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1033) 	ntfs_debug("Base inode 0x%lx is in icache.", na.mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1034) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1035) 	 * The base inode is in icache.  Check if it has the extent inode
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1036) 	 * corresponding to this extent mft record attached.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1037) 	 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1038) 	ni = NTFS_I(vi);
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000 1039) 	mutex_lock(&ni->extent_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1040) 	if (ni->nr_extents <= 0) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1041) 		/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1042) 		 * The base inode has no attached extent inodes, write this
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1043) 		 * extent mft record.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1044) 		 */
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000 1045) 		mutex_unlock(&ni->extent_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1046) 		iput(vi);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1047) 		ntfs_debug("Base inode 0x%lx has no attached extent inodes, "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1048) 				"write the extent record.", na.mft_no);
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700 1049) 		return true;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1050) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1051) 	/* Iterate over the attached extent inodes. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1052) 	extent_nis = ni->ext.extent_ntfs_inos;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1053) 	for (eni = NULL, i = 0; i < ni->nr_extents; ++i) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1054) 		if (mft_no == extent_nis[i]->mft_no) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1055) 			/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1056) 			 * Found the extent inode corresponding to this extent
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1057) 			 * mft record.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1058) 			 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1059) 			eni = extent_nis[i];
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1060) 			break;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1061) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1062) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1063) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1064) 	 * If the extent inode was not attached to the base inode, write this
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1065) 	 * extent mft record.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1066) 	 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1067) 	if (!eni) {
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000 1068) 		mutex_unlock(&ni->extent_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1069) 		iput(vi);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1070) 		ntfs_debug("Extent inode 0x%lx is not attached to its base "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1071) 				"inode 0x%lx, write the extent record.",
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1072) 				mft_no, na.mft_no);
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700 1073) 		return true;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1074) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1075) 	ntfs_debug("Extent inode 0x%lx is attached to its base inode 0x%lx.",
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1076) 			mft_no, na.mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1077) 	/* Take a reference to the extent ntfs inode. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1078) 	atomic_inc(&eni->count);
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000 1079) 	mutex_unlock(&ni->extent_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1080) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1081) 	 * Found the extent inode coresponding to this extent mft record.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1082) 	 * Try to take the mft record lock.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1083) 	 */
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000 1084) 	if (unlikely(!mutex_trylock(&eni->mrec_lock))) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1085) 		atomic_dec(&eni->count);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1086) 		iput(vi);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1087) 		ntfs_debug("Extent mft record 0x%lx is already locked, do "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1088) 				"not write it.", mft_no);
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700 1089) 		return false;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1090) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1091) 	ntfs_debug("Managed to lock extent mft record 0x%lx, write it.",
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1092) 			mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1093) 	if (NInoTestClearDirty(eni))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1094) 		ntfs_debug("Extent inode 0x%lx is dirty, marking it clean.",
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1095) 				mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1096) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1097) 	 * The write has to occur while we hold the mft record lock so return
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1098) 	 * the locked extent ntfs inode.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1099) 	 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1100) 	*locked_ni = eni;
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700 1101) 	return true;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1102) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1103) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1104) static const char *es = "  Leaving inconsistent metadata.  Unmount and run "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1105) 		"chkdsk.";
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1106) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1107) /**
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1108)  * ntfs_mft_bitmap_find_and_alloc_free_rec_nolock - see name
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1109)  * @vol:	volume on which to search for a free mft record
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1110)  * @base_ni:	open base inode if allocating an extent mft record or NULL
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1111)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1112)  * Search for a free mft record in the mft bitmap attribute on the ntfs volume
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1113)  * @vol.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1114)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1115)  * If @base_ni is NULL start the search at the default allocator position.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1116)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1117)  * If @base_ni is not NULL start the search at the mft record after the base
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1118)  * mft record @base_ni.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1119)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1120)  * Return the free mft record on success and -errno on error.  An error code of
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1121)  * -ENOSPC means that there are no free mft records in the currently
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1122)  * initialized mft bitmap.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1123)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1124)  * Locking: Caller must hold vol->mftbmp_lock for writing.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1125)  */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1126) static int ntfs_mft_bitmap_find_and_alloc_free_rec_nolock(ntfs_volume *vol,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1127) 		ntfs_inode *base_ni)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1128) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1129) 	s64 pass_end, ll, data_pos, pass_start, ofs, bit;
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1130) 	unsigned long flags;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1131) 	struct address_space *mftbmp_mapping;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1132) 	u8 *buf, *byte;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1133) 	struct page *page;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1134) 	unsigned int page_ofs, size;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1135) 	u8 pass, b;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1136) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1137) 	ntfs_debug("Searching for free mft record in the currently "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1138) 			"initialized mft bitmap.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1139) 	mftbmp_mapping = vol->mftbmp_ino->i_mapping;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1140) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1141) 	 * Set the end of the pass making sure we do not overflow the mft
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1142) 	 * bitmap.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1143) 	 */
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1144) 	read_lock_irqsave(&NTFS_I(vol->mft_ino)->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1145) 	pass_end = NTFS_I(vol->mft_ino)->allocated_size >>
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1146) 			vol->mft_record_size_bits;
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1147) 	read_unlock_irqrestore(&NTFS_I(vol->mft_ino)->size_lock, flags);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1148) 	read_lock_irqsave(&NTFS_I(vol->mftbmp_ino)->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1149) 	ll = NTFS_I(vol->mftbmp_ino)->initialized_size << 3;
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1150) 	read_unlock_irqrestore(&NTFS_I(vol->mftbmp_ino)->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1151) 	if (pass_end > ll)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1152) 		pass_end = ll;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1153) 	pass = 1;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1154) 	if (!base_ni)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1155) 		data_pos = vol->mft_data_pos;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1156) 	else
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1157) 		data_pos = base_ni->mft_no + 1;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1158) 	if (data_pos < 24)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1159) 		data_pos = 24;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1160) 	if (data_pos >= pass_end) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1161) 		data_pos = 24;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1162) 		pass = 2;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1163) 		/* This happens on a freshly formatted volume. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1164) 		if (data_pos >= pass_end)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1165) 			return -ENOSPC;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1166) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1167) 	pass_start = data_pos;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1168) 	ntfs_debug("Starting bitmap search: pass %u, pass_start 0x%llx, "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1169) 			"pass_end 0x%llx, data_pos 0x%llx.", pass,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1170) 			(long long)pass_start, (long long)pass_end,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1171) 			(long long)data_pos);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1172) 	/* Loop until a free mft record is found. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1173) 	for (; pass <= 2;) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1174) 		/* Cap size to pass_end. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1175) 		ofs = data_pos >> 3;
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 1176) 		page_ofs = ofs & ~PAGE_MASK;
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 1177) 		size = PAGE_SIZE - page_ofs;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1178) 		ll = ((pass_end + 7) >> 3) - ofs;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1179) 		if (size > ll)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1180) 			size = ll;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1181) 		size <<= 3;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1182) 		/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1183) 		 * If we are still within the active pass, search the next page
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1184) 		 * for a zero bit.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1185) 		 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1186) 		if (size) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1187) 			page = ntfs_map_page(mftbmp_mapping,
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 1188) 					ofs >> PAGE_SHIFT);
801678c5a3b4c (Hirofumi Nakagawa  2008-04-29 01:03:09 -0700 1189) 			if (IS_ERR(page)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1190) 				ntfs_error(vol->sb, "Failed to read mft "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1191) 						"bitmap, aborting.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1192) 				return PTR_ERR(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1193) 			}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1194) 			buf = (u8*)page_address(page) + page_ofs;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1195) 			bit = data_pos & 7;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1196) 			data_pos &= ~7ull;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1197) 			ntfs_debug("Before inner for loop: size 0x%x, "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1198) 					"data_pos 0x%llx, bit 0x%llx", size,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1199) 					(long long)data_pos, (long long)bit);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1200) 			for (; bit < size && data_pos + bit < pass_end;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1201) 					bit &= ~7ull, bit += 8) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1202) 				byte = buf + (bit >> 3);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1203) 				if (*byte == 0xff)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1204) 					continue;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1205) 				b = ffz((unsigned long)*byte);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1206) 				if (b < 8 && b >= (bit & 7)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1207) 					ll = data_pos + (bit & ~7ull) + b;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1208) 					if (unlikely(ll > (1ll << 32))) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1209) 						ntfs_unmap_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1210) 						return -ENOSPC;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1211) 					}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1212) 					*byte |= 1 << b;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1213) 					flush_dcache_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1214) 					set_page_dirty(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1215) 					ntfs_unmap_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1216) 					ntfs_debug("Done.  (Found and "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1217) 							"allocated mft record "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1218) 							"0x%llx.)",
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1219) 							(long long)ll);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1220) 					return ll;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1221) 				}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1222) 			}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1223) 			ntfs_debug("After inner for loop: size 0x%x, "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1224) 					"data_pos 0x%llx, bit 0x%llx", size,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1225) 					(long long)data_pos, (long long)bit);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1226) 			data_pos += size;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1227) 			ntfs_unmap_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1228) 			/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1229) 			 * If the end of the pass has not been reached yet,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1230) 			 * continue searching the mft bitmap for a zero bit.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1231) 			 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1232) 			if (data_pos < pass_end)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1233) 				continue;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1234) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1235) 		/* Do the next pass. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1236) 		if (++pass == 2) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1237) 			/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1238) 			 * Starting the second pass, in which we scan the first
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1239) 			 * part of the zone which we omitted earlier.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1240) 			 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1241) 			pass_end = pass_start;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1242) 			data_pos = pass_start = 24;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1243) 			ntfs_debug("pass %i, pass_start 0x%llx, pass_end "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1244) 					"0x%llx.", pass, (long long)pass_start,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1245) 					(long long)pass_end);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1246) 			if (data_pos >= pass_end)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1247) 				break;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1248) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1249) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1250) 	/* No free mft records in currently initialized mft bitmap. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1251) 	ntfs_debug("Done.  (No free mft records left in currently initialized "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1252) 			"mft bitmap.)");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1253) 	return -ENOSPC;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1254) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1255) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1256) /**
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1257)  * ntfs_mft_bitmap_extend_allocation_nolock - extend mft bitmap by a cluster
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1258)  * @vol:	volume on which to extend the mft bitmap attribute
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1259)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1260)  * Extend the mft bitmap attribute on the ntfs volume @vol by one cluster.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1261)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1262)  * Note: Only changes allocated_size, i.e. does not touch initialized_size or
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1263)  * data_size.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1264)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1265)  * Return 0 on success and -errno on error.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1266)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1267)  * Locking: - Caller must hold vol->mftbmp_lock for writing.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1268)  *	    - This function takes NTFS_I(vol->mftbmp_ino)->runlist.lock for
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1269)  *	      writing and releases it before returning.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1270)  *	    - This function takes vol->lcnbmp_lock for writing and releases it
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1271)  *	      before returning.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1272)  */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1273) static int ntfs_mft_bitmap_extend_allocation_nolock(ntfs_volume *vol)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1274) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1275) 	LCN lcn;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1276) 	s64 ll;
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1277) 	unsigned long flags;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1278) 	struct page *page;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1279) 	ntfs_inode *mft_ni, *mftbmp_ni;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1280) 	runlist_element *rl, *rl2 = NULL;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1281) 	ntfs_attr_search_ctx *ctx = NULL;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1282) 	MFT_RECORD *mrec;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1283) 	ATTR_RECORD *a = NULL;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1284) 	int ret, mp_size;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1285) 	u32 old_alen = 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1286) 	u8 *b, tb;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1287) 	struct {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1288) 		u8 added_cluster:1;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1289) 		u8 added_run:1;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1290) 		u8 mp_rebuilt:1;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1291) 	} status = { 0, 0, 0 };
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1292) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1293) 	ntfs_debug("Extending mft bitmap allocation.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1294) 	mft_ni = NTFS_I(vol->mft_ino);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1295) 	mftbmp_ni = NTFS_I(vol->mftbmp_ino);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1296) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1297) 	 * Determine the last lcn of the mft bitmap.  The allocated size of the
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1298) 	 * mft bitmap cannot be zero so we are ok to do this.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1299) 	 */
b6ad6c52fe36a (Anton Altaparmakov 2005-02-15 10:08:43 +0000 1300) 	down_write(&mftbmp_ni->runlist.lock);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1301) 	read_lock_irqsave(&mftbmp_ni->size_lock, flags);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1302) 	ll = mftbmp_ni->allocated_size;
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1303) 	read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
c0c1cc0e46b36 (Anton Altaparmakov 2005-03-07 21:43:38 +0000 1304) 	rl = ntfs_attr_find_vcn_nolock(mftbmp_ni,
69b41e3c0223b (Anton Altaparmakov 2005-10-04 14:01:14 +0100 1305) 			(ll - 1) >> vol->cluster_size_bits, NULL);
cc22c800e15b0 (Denis Efremov      2019-09-25 16:49:43 -0700 1306) 	if (IS_ERR(rl) || unlikely(!rl->length || rl->lcn < 0)) {
b6ad6c52fe36a (Anton Altaparmakov 2005-02-15 10:08:43 +0000 1307) 		up_write(&mftbmp_ni->runlist.lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1308) 		ntfs_error(vol->sb, "Failed to determine last allocated "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1309) 				"cluster of mft bitmap attribute.");
b6ad6c52fe36a (Anton Altaparmakov 2005-02-15 10:08:43 +0000 1310) 		if (!IS_ERR(rl))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1311) 			ret = -EIO;
b6ad6c52fe36a (Anton Altaparmakov 2005-02-15 10:08:43 +0000 1312) 		else
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1313) 			ret = PTR_ERR(rl);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1314) 		return ret;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1315) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1316) 	lcn = rl->lcn + rl->length;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1317) 	ntfs_debug("Last lcn of mft bitmap attribute is 0x%llx.",
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1318) 			(long long)lcn);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1319) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1320) 	 * Attempt to get the cluster following the last allocated cluster by
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1321) 	 * hand as it may be in the MFT zone so the allocator would not give it
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1322) 	 * to us.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1323) 	 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1324) 	ll = lcn >> 3;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1325) 	page = ntfs_map_page(vol->lcnbmp_ino->i_mapping,
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 1326) 			ll >> PAGE_SHIFT);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1327) 	if (IS_ERR(page)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1328) 		up_write(&mftbmp_ni->runlist.lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1329) 		ntfs_error(vol->sb, "Failed to read from lcn bitmap.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1330) 		return PTR_ERR(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1331) 	}
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 1332) 	b = (u8*)page_address(page) + (ll & ~PAGE_MASK);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1333) 	tb = 1 << (lcn & 7ull);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1334) 	down_write(&vol->lcnbmp_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1335) 	if (*b != 0xff && !(*b & tb)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1336) 		/* Next cluster is free, allocate it. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1337) 		*b |= tb;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1338) 		flush_dcache_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1339) 		set_page_dirty(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1340) 		up_write(&vol->lcnbmp_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1341) 		ntfs_unmap_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1342) 		/* Update the mft bitmap runlist. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1343) 		rl->length++;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1344) 		rl[1].vcn++;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1345) 		status.added_cluster = 1;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1346) 		ntfs_debug("Appending one cluster to mft bitmap.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1347) 	} else {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1348) 		up_write(&vol->lcnbmp_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1349) 		ntfs_unmap_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1350) 		/* Allocate a cluster from the DATA_ZONE. */
fc0fa7dc7d243 (Anton Altaparmakov 2005-10-04 14:36:56 +0100 1351) 		rl2 = ntfs_cluster_alloc(vol, rl[1].vcn, 1, lcn, DATA_ZONE,
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700 1352) 				true);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1353) 		if (IS_ERR(rl2)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1354) 			up_write(&mftbmp_ni->runlist.lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1355) 			ntfs_error(vol->sb, "Failed to allocate a cluster for "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1356) 					"the mft bitmap.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1357) 			return PTR_ERR(rl2);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1358) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1359) 		rl = ntfs_runlists_merge(mftbmp_ni->runlist.rl, rl2);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1360) 		if (IS_ERR(rl)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1361) 			up_write(&mftbmp_ni->runlist.lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1362) 			ntfs_error(vol->sb, "Failed to merge runlists for mft "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1363) 					"bitmap.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1364) 			if (ntfs_cluster_free_from_rl(vol, rl2)) {
9b556248ecb05 (Anton Altaparmakov 2012-02-24 09:17:09 +0000 1365) 				ntfs_error(vol->sb, "Failed to deallocate "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1366) 						"allocated cluster.%s", es);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1367) 				NVolSetErrors(vol);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1368) 			}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1369) 			ntfs_free(rl2);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1370) 			return PTR_ERR(rl);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1371) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1372) 		mftbmp_ni->runlist.rl = rl;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1373) 		status.added_run = 1;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1374) 		ntfs_debug("Adding one run to mft bitmap.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1375) 		/* Find the last run in the new runlist. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1376) 		for (; rl[1].length; rl++)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1377) 			;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1378) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1379) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1380) 	 * Update the attribute record as well.  Note: @rl is the last
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1381) 	 * (non-terminator) runlist element of mft bitmap.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1382) 	 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1383) 	mrec = map_mft_record(mft_ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1384) 	if (IS_ERR(mrec)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1385) 		ntfs_error(vol->sb, "Failed to map mft record.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1386) 		ret = PTR_ERR(mrec);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1387) 		goto undo_alloc;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1388) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1389) 	ctx = ntfs_attr_get_search_ctx(mft_ni, mrec);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1390) 	if (unlikely(!ctx)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1391) 		ntfs_error(vol->sb, "Failed to get search context.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1392) 		ret = -ENOMEM;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1393) 		goto undo_alloc;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1394) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1395) 	ret = ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1396) 			mftbmp_ni->name_len, CASE_SENSITIVE, rl[1].vcn, NULL,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1397) 			0, ctx);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1398) 	if (unlikely(ret)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1399) 		ntfs_error(vol->sb, "Failed to find last attribute extent of "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1400) 				"mft bitmap attribute.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1401) 		if (ret == -ENOENT)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1402) 			ret = -EIO;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1403) 		goto undo_alloc;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1404) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1405) 	a = ctx->attr;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1406) 	ll = sle64_to_cpu(a->data.non_resident.lowest_vcn);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1407) 	/* Search back for the previous last allocated cluster of mft bitmap. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1408) 	for (rl2 = rl; rl2 > mftbmp_ni->runlist.rl; rl2--) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1409) 		if (ll >= rl2->vcn)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1410) 			break;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1411) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1412) 	BUG_ON(ll < rl2->vcn);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1413) 	BUG_ON(ll >= rl2->vcn + rl2->length);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1414) 	/* Get the size for the new mapping pairs array for this extent. */
fa3be92317c4a (Anton Altaparmakov 2005-06-25 17:15:36 +0100 1415) 	mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, -1);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1416) 	if (unlikely(mp_size <= 0)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1417) 		ntfs_error(vol->sb, "Get size for mapping pairs failed for "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1418) 				"mft bitmap attribute extent.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1419) 		ret = mp_size;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1420) 		if (!ret)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1421) 			ret = -EIO;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1422) 		goto undo_alloc;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1423) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1424) 	/* Expand the attribute record if necessary. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1425) 	old_alen = le32_to_cpu(a->length);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1426) 	ret = ntfs_attr_record_resize(ctx->mrec, a, mp_size +
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1427) 			le16_to_cpu(a->data.non_resident.mapping_pairs_offset));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1428) 	if (unlikely(ret)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1429) 		if (ret != -ENOSPC) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1430) 			ntfs_error(vol->sb, "Failed to resize attribute "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1431) 					"record for mft bitmap attribute.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1432) 			goto undo_alloc;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1433) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1434) 		// TODO: Deal with this by moving this extent to a new mft
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1435) 		// record or by starting a new extent in a new mft record or by
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1436) 		// moving other attributes out of this mft record.
b6ad6c52fe36a (Anton Altaparmakov 2005-02-15 10:08:43 +0000 1437) 		// Note: It will need to be a special mft record and if none of
b6ad6c52fe36a (Anton Altaparmakov 2005-02-15 10:08:43 +0000 1438) 		// those are available it gets rather complicated...
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1439) 		ntfs_error(vol->sb, "Not enough space in this mft record to "
25985edcedea6 (Lucas De Marchi    2011-03-30 22:57:33 -0300 1440) 				"accommodate extended mft bitmap attribute "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1441) 				"extent.  Cannot handle this yet.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1442) 		ret = -EOPNOTSUPP;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1443) 		goto undo_alloc;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1444) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1445) 	status.mp_rebuilt = 1;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1446) 	/* Generate the mapping pairs array directly into the attr record. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1447) 	ret = ntfs_mapping_pairs_build(vol, (u8*)a +
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1448) 			le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
fa3be92317c4a (Anton Altaparmakov 2005-06-25 17:15:36 +0100 1449) 			mp_size, rl2, ll, -1, NULL);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1450) 	if (unlikely(ret)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1451) 		ntfs_error(vol->sb, "Failed to build mapping pairs array for "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1452) 				"mft bitmap attribute.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1453) 		goto undo_alloc;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1454) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1455) 	/* Update the highest_vcn. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1456) 	a->data.non_resident.highest_vcn = cpu_to_sle64(rl[1].vcn - 1);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1457) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1458) 	 * We now have extended the mft bitmap allocated_size by one cluster.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1459) 	 * Reflect this in the ntfs_inode structure and the attribute record.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1460) 	 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1461) 	if (a->data.non_resident.lowest_vcn) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1462) 		/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1463) 		 * We are not in the first attribute extent, switch to it, but
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1464) 		 * first ensure the changes will make it to disk later.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1465) 		 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1466) 		flush_dcache_mft_record_page(ctx->ntfs_ino);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1467) 		mark_mft_record_dirty(ctx->ntfs_ino);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1468) 		ntfs_attr_reinit_search_ctx(ctx);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1469) 		ret = ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1470) 				mftbmp_ni->name_len, CASE_SENSITIVE, 0, NULL,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1471) 				0, ctx);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1472) 		if (unlikely(ret)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1473) 			ntfs_error(vol->sb, "Failed to find first attribute "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1474) 					"extent of mft bitmap attribute.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1475) 			goto restore_undo_alloc;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1476) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1477) 		a = ctx->attr;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1478) 	}
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1479) 	write_lock_irqsave(&mftbmp_ni->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1480) 	mftbmp_ni->allocated_size += vol->cluster_size;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1481) 	a->data.non_resident.allocated_size =
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1482) 			cpu_to_sle64(mftbmp_ni->allocated_size);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1483) 	write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1484) 	/* Ensure the changes make it to disk. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1485) 	flush_dcache_mft_record_page(ctx->ntfs_ino);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1486) 	mark_mft_record_dirty(ctx->ntfs_ino);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1487) 	ntfs_attr_put_search_ctx(ctx);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1488) 	unmap_mft_record(mft_ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1489) 	up_write(&mftbmp_ni->runlist.lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1490) 	ntfs_debug("Done.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1491) 	return 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1492) restore_undo_alloc:
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1493) 	ntfs_attr_reinit_search_ctx(ctx);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1494) 	if (ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1495) 			mftbmp_ni->name_len, CASE_SENSITIVE, rl[1].vcn, NULL,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1496) 			0, ctx)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1497) 		ntfs_error(vol->sb, "Failed to find last attribute extent of "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1498) 				"mft bitmap attribute.%s", es);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1499) 		write_lock_irqsave(&mftbmp_ni->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1500) 		mftbmp_ni->allocated_size += vol->cluster_size;
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1501) 		write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1502) 		ntfs_attr_put_search_ctx(ctx);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1503) 		unmap_mft_record(mft_ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1504) 		up_write(&mftbmp_ni->runlist.lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1505) 		/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1506) 		 * The only thing that is now wrong is ->allocated_size of the
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1507) 		 * base attribute extent which chkdsk should be able to fix.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1508) 		 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1509) 		NVolSetErrors(vol);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1510) 		return ret;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1511) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1512) 	a = ctx->attr;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1513) 	a->data.non_resident.highest_vcn = cpu_to_sle64(rl[1].vcn - 2);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1514) undo_alloc:
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1515) 	if (status.added_cluster) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1516) 		/* Truncate the last run in the runlist by one cluster. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1517) 		rl->length--;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1518) 		rl[1].vcn--;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1519) 	} else if (status.added_run) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1520) 		lcn = rl->lcn;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1521) 		/* Remove the last run from the runlist. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1522) 		rl->lcn = rl[1].lcn;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1523) 		rl->length = 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1524) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1525) 	/* Deallocate the cluster. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1526) 	down_write(&vol->lcnbmp_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1527) 	if (ntfs_bitmap_clear_bit(vol->lcnbmp_ino, lcn)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1528) 		ntfs_error(vol->sb, "Failed to free allocated cluster.%s", es);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1529) 		NVolSetErrors(vol);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1530) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1531) 	up_write(&vol->lcnbmp_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1532) 	if (status.mp_rebuilt) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1533) 		if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu(
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1534) 				a->data.non_resident.mapping_pairs_offset),
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1535) 				old_alen - le16_to_cpu(
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1536) 				a->data.non_resident.mapping_pairs_offset),
fa3be92317c4a (Anton Altaparmakov 2005-06-25 17:15:36 +0100 1537) 				rl2, ll, -1, NULL)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1538) 			ntfs_error(vol->sb, "Failed to restore mapping pairs "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1539) 					"array.%s", es);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1540) 			NVolSetErrors(vol);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1541) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1542) 		if (ntfs_attr_record_resize(ctx->mrec, a, old_alen)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1543) 			ntfs_error(vol->sb, "Failed to restore attribute "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1544) 					"record.%s", es);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1545) 			NVolSetErrors(vol);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1546) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1547) 		flush_dcache_mft_record_page(ctx->ntfs_ino);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1548) 		mark_mft_record_dirty(ctx->ntfs_ino);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1549) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1550) 	if (ctx)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1551) 		ntfs_attr_put_search_ctx(ctx);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1552) 	if (!IS_ERR(mrec))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1553) 		unmap_mft_record(mft_ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1554) 	up_write(&mftbmp_ni->runlist.lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1555) 	return ret;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1556) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1557) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1558) /**
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1559)  * ntfs_mft_bitmap_extend_initialized_nolock - extend mftbmp initialized data
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1560)  * @vol:	volume on which to extend the mft bitmap attribute
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1561)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1562)  * Extend the initialized portion of the mft bitmap attribute on the ntfs
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1563)  * volume @vol by 8 bytes.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1564)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1565)  * Note:  Only changes initialized_size and data_size, i.e. requires that
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1566)  * allocated_size is big enough to fit the new initialized_size.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1567)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1568)  * Return 0 on success and -error on error.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1569)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1570)  * Locking: Caller must hold vol->mftbmp_lock for writing.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1571)  */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1572) static int ntfs_mft_bitmap_extend_initialized_nolock(ntfs_volume *vol)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1573) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1574) 	s64 old_data_size, old_initialized_size;
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1575) 	unsigned long flags;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1576) 	struct inode *mftbmp_vi;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1577) 	ntfs_inode *mft_ni, *mftbmp_ni;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1578) 	ntfs_attr_search_ctx *ctx;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1579) 	MFT_RECORD *mrec;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1580) 	ATTR_RECORD *a;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1581) 	int ret;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1582) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1583) 	ntfs_debug("Extending mft bitmap initiailized (and data) size.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1584) 	mft_ni = NTFS_I(vol->mft_ino);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1585) 	mftbmp_vi = vol->mftbmp_ino;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1586) 	mftbmp_ni = NTFS_I(mftbmp_vi);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1587) 	/* Get the attribute record. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1588) 	mrec = map_mft_record(mft_ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1589) 	if (IS_ERR(mrec)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1590) 		ntfs_error(vol->sb, "Failed to map mft record.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1591) 		return PTR_ERR(mrec);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1592) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1593) 	ctx = ntfs_attr_get_search_ctx(mft_ni, mrec);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1594) 	if (unlikely(!ctx)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1595) 		ntfs_error(vol->sb, "Failed to get search context.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1596) 		ret = -ENOMEM;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1597) 		goto unm_err_out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1598) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1599) 	ret = ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1600) 			mftbmp_ni->name_len, CASE_SENSITIVE, 0, NULL, 0, ctx);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1601) 	if (unlikely(ret)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1602) 		ntfs_error(vol->sb, "Failed to find first attribute extent of "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1603) 				"mft bitmap attribute.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1604) 		if (ret == -ENOENT)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1605) 			ret = -EIO;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1606) 		goto put_err_out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1607) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1608) 	a = ctx->attr;
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1609) 	write_lock_irqsave(&mftbmp_ni->size_lock, flags);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1610) 	old_data_size = i_size_read(mftbmp_vi);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1611) 	old_initialized_size = mftbmp_ni->initialized_size;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1612) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1613) 	 * We can simply update the initialized_size before filling the space
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1614) 	 * with zeroes because the caller is holding the mft bitmap lock for
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1615) 	 * writing which ensures that no one else is trying to access the data.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1616) 	 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1617) 	mftbmp_ni->initialized_size += 8;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1618) 	a->data.non_resident.initialized_size =
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1619) 			cpu_to_sle64(mftbmp_ni->initialized_size);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1620) 	if (mftbmp_ni->initialized_size > old_data_size) {
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1621) 		i_size_write(mftbmp_vi, mftbmp_ni->initialized_size);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1622) 		a->data.non_resident.data_size =
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1623) 				cpu_to_sle64(mftbmp_ni->initialized_size);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1624) 	}
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1625) 	write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1626) 	/* Ensure the changes make it to disk. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1627) 	flush_dcache_mft_record_page(ctx->ntfs_ino);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1628) 	mark_mft_record_dirty(ctx->ntfs_ino);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1629) 	ntfs_attr_put_search_ctx(ctx);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1630) 	unmap_mft_record(mft_ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1631) 	/* Initialize the mft bitmap attribute value with zeroes. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1632) 	ret = ntfs_attr_set(mftbmp_ni, old_initialized_size, 8, 0);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1633) 	if (likely(!ret)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1634) 		ntfs_debug("Done.  (Wrote eight initialized bytes to mft "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1635) 				"bitmap.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1636) 		return 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1637) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1638) 	ntfs_error(vol->sb, "Failed to write to mft bitmap.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1639) 	/* Try to recover from the error. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1640) 	mrec = map_mft_record(mft_ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1641) 	if (IS_ERR(mrec)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1642) 		ntfs_error(vol->sb, "Failed to map mft record.%s", es);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1643) 		NVolSetErrors(vol);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1644) 		return ret;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1645) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1646) 	ctx = ntfs_attr_get_search_ctx(mft_ni, mrec);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1647) 	if (unlikely(!ctx)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1648) 		ntfs_error(vol->sb, "Failed to get search context.%s", es);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1649) 		NVolSetErrors(vol);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1650) 		goto unm_err_out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1651) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1652) 	if (ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1653) 			mftbmp_ni->name_len, CASE_SENSITIVE, 0, NULL, 0, ctx)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1654) 		ntfs_error(vol->sb, "Failed to find first attribute extent of "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1655) 				"mft bitmap attribute.%s", es);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1656) 		NVolSetErrors(vol);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1657) put_err_out:
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1658) 		ntfs_attr_put_search_ctx(ctx);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1659) unm_err_out:
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1660) 		unmap_mft_record(mft_ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1661) 		goto err_out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1662) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1663) 	a = ctx->attr;
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1664) 	write_lock_irqsave(&mftbmp_ni->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1665) 	mftbmp_ni->initialized_size = old_initialized_size;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1666) 	a->data.non_resident.initialized_size =
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1667) 			cpu_to_sle64(old_initialized_size);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1668) 	if (i_size_read(mftbmp_vi) != old_data_size) {
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1669) 		i_size_write(mftbmp_vi, old_data_size);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1670) 		a->data.non_resident.data_size = cpu_to_sle64(old_data_size);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1671) 	}
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1672) 	write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1673) 	flush_dcache_mft_record_page(ctx->ntfs_ino);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1674) 	mark_mft_record_dirty(ctx->ntfs_ino);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1675) 	ntfs_attr_put_search_ctx(ctx);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1676) 	unmap_mft_record(mft_ni);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1677) #ifdef DEBUG
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1678) 	read_lock_irqsave(&mftbmp_ni->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1679) 	ntfs_debug("Restored status of mftbmp: allocated_size 0x%llx, "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1680) 			"data_size 0x%llx, initialized_size 0x%llx.",
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1681) 			(long long)mftbmp_ni->allocated_size,
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1682) 			(long long)i_size_read(mftbmp_vi),
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1683) 			(long long)mftbmp_ni->initialized_size);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1684) 	read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1685) #endif /* DEBUG */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1686) err_out:
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1687) 	return ret;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1688) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1689) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1690) /**
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1691)  * ntfs_mft_data_extend_allocation_nolock - extend mft data attribute
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1692)  * @vol:	volume on which to extend the mft data attribute
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1693)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1694)  * Extend the mft data attribute on the ntfs volume @vol by 16 mft records
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1695)  * worth of clusters or if not enough space for this by one mft record worth
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1696)  * of clusters.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1697)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1698)  * Note:  Only changes allocated_size, i.e. does not touch initialized_size or
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1699)  * data_size.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1700)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1701)  * Return 0 on success and -errno on error.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1702)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1703)  * Locking: - Caller must hold vol->mftbmp_lock for writing.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1704)  *	    - This function takes NTFS_I(vol->mft_ino)->runlist.lock for
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1705)  *	      writing and releases it before returning.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1706)  *	    - This function calls functions which take vol->lcnbmp_lock for
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1707)  *	      writing and release it before returning.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1708)  */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1709) static int ntfs_mft_data_extend_allocation_nolock(ntfs_volume *vol)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1710) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1711) 	LCN lcn;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1712) 	VCN old_last_vcn;
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1713) 	s64 min_nr, nr, ll;
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1714) 	unsigned long flags;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1715) 	ntfs_inode *mft_ni;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1716) 	runlist_element *rl, *rl2;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1717) 	ntfs_attr_search_ctx *ctx = NULL;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1718) 	MFT_RECORD *mrec;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1719) 	ATTR_RECORD *a = NULL;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1720) 	int ret, mp_size;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1721) 	u32 old_alen = 0;
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700 1722) 	bool mp_rebuilt = false;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1723) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1724) 	ntfs_debug("Extending mft data allocation.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1725) 	mft_ni = NTFS_I(vol->mft_ino);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1726) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1727) 	 * Determine the preferred allocation location, i.e. the last lcn of
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1728) 	 * the mft data attribute.  The allocated size of the mft data
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1729) 	 * attribute cannot be zero so we are ok to do this.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1730) 	 */
b6ad6c52fe36a (Anton Altaparmakov 2005-02-15 10:08:43 +0000 1731) 	down_write(&mft_ni->runlist.lock);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1732) 	read_lock_irqsave(&mft_ni->size_lock, flags);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1733) 	ll = mft_ni->allocated_size;
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1734) 	read_unlock_irqrestore(&mft_ni->size_lock, flags);
c0c1cc0e46b36 (Anton Altaparmakov 2005-03-07 21:43:38 +0000 1735) 	rl = ntfs_attr_find_vcn_nolock(mft_ni,
69b41e3c0223b (Anton Altaparmakov 2005-10-04 14:01:14 +0100 1736) 			(ll - 1) >> vol->cluster_size_bits, NULL);
cc22c800e15b0 (Denis Efremov      2019-09-25 16:49:43 -0700 1737) 	if (IS_ERR(rl) || unlikely(!rl->length || rl->lcn < 0)) {
b6ad6c52fe36a (Anton Altaparmakov 2005-02-15 10:08:43 +0000 1738) 		up_write(&mft_ni->runlist.lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1739) 		ntfs_error(vol->sb, "Failed to determine last allocated "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1740) 				"cluster of mft data attribute.");
b6ad6c52fe36a (Anton Altaparmakov 2005-02-15 10:08:43 +0000 1741) 		if (!IS_ERR(rl))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1742) 			ret = -EIO;
b6ad6c52fe36a (Anton Altaparmakov 2005-02-15 10:08:43 +0000 1743) 		else
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1744) 			ret = PTR_ERR(rl);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1745) 		return ret;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1746) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1747) 	lcn = rl->lcn + rl->length;
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1748) 	ntfs_debug("Last lcn of mft data attribute is 0x%llx.", (long long)lcn);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1749) 	/* Minimum allocation is one mft record worth of clusters. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1750) 	min_nr = vol->mft_record_size >> vol->cluster_size_bits;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1751) 	if (!min_nr)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1752) 		min_nr = 1;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1753) 	/* Want to allocate 16 mft records worth of clusters. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1754) 	nr = vol->mft_record_size << 4 >> vol->cluster_size_bits;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1755) 	if (!nr)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1756) 		nr = min_nr;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1757) 	/* Ensure we do not go above 2^32-1 mft records. */
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1758) 	read_lock_irqsave(&mft_ni->size_lock, flags);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1759) 	ll = mft_ni->allocated_size;
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1760) 	read_unlock_irqrestore(&mft_ni->size_lock, flags);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1761) 	if (unlikely((ll + (nr << vol->cluster_size_bits)) >>
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1762) 			vol->mft_record_size_bits >= (1ll << 32))) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1763) 		nr = min_nr;
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1764) 		if (unlikely((ll + (nr << vol->cluster_size_bits)) >>
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1765) 				vol->mft_record_size_bits >= (1ll << 32))) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1766) 			ntfs_warning(vol->sb, "Cannot allocate mft record "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1767) 					"because the maximum number of inodes "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1768) 					"(2^32) has already been reached.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1769) 			up_write(&mft_ni->runlist.lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1770) 			return -ENOSPC;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1771) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1772) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1773) 	ntfs_debug("Trying mft data allocation with %s cluster count %lli.",
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1774) 			nr > min_nr ? "default" : "minimal", (long long)nr);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1775) 	old_last_vcn = rl[1].vcn;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1776) 	do {
fc0fa7dc7d243 (Anton Altaparmakov 2005-10-04 14:36:56 +0100 1777) 		rl2 = ntfs_cluster_alloc(vol, old_last_vcn, nr, lcn, MFT_ZONE,
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700 1778) 				true);
cc22c800e15b0 (Denis Efremov      2019-09-25 16:49:43 -0700 1779) 		if (!IS_ERR(rl2))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1780) 			break;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1781) 		if (PTR_ERR(rl2) != -ENOSPC || nr == min_nr) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1782) 			ntfs_error(vol->sb, "Failed to allocate the minimal "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1783) 					"number of clusters (%lli) for the "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1784) 					"mft data attribute.", (long long)nr);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1785) 			up_write(&mft_ni->runlist.lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1786) 			return PTR_ERR(rl2);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1787) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1788) 		/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1789) 		 * There is not enough space to do the allocation, but there
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1790) 		 * might be enough space to do a minimal allocation so try that
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1791) 		 * before failing.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1792) 		 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1793) 		nr = min_nr;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1794) 		ntfs_debug("Retrying mft data allocation with minimal cluster "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1795) 				"count %lli.", (long long)nr);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1796) 	} while (1);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1797) 	rl = ntfs_runlists_merge(mft_ni->runlist.rl, rl2);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1798) 	if (IS_ERR(rl)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1799) 		up_write(&mft_ni->runlist.lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1800) 		ntfs_error(vol->sb, "Failed to merge runlists for mft data "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1801) 				"attribute.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1802) 		if (ntfs_cluster_free_from_rl(vol, rl2)) {
9b556248ecb05 (Anton Altaparmakov 2012-02-24 09:17:09 +0000 1803) 			ntfs_error(vol->sb, "Failed to deallocate clusters "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1804) 					"from the mft data attribute.%s", es);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1805) 			NVolSetErrors(vol);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1806) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1807) 		ntfs_free(rl2);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1808) 		return PTR_ERR(rl);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1809) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1810) 	mft_ni->runlist.rl = rl;
8907547d4b099 (Randy Dunlap       2005-03-03 11:19:53 +0000 1811) 	ntfs_debug("Allocated %lli clusters.", (long long)nr);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1812) 	/* Find the last run in the new runlist. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1813) 	for (; rl[1].length; rl++)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1814) 		;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1815) 	/* Update the attribute record as well. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1816) 	mrec = map_mft_record(mft_ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1817) 	if (IS_ERR(mrec)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1818) 		ntfs_error(vol->sb, "Failed to map mft record.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1819) 		ret = PTR_ERR(mrec);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1820) 		goto undo_alloc;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1821) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1822) 	ctx = ntfs_attr_get_search_ctx(mft_ni, mrec);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1823) 	if (unlikely(!ctx)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1824) 		ntfs_error(vol->sb, "Failed to get search context.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1825) 		ret = -ENOMEM;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1826) 		goto undo_alloc;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1827) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1828) 	ret = ntfs_attr_lookup(mft_ni->type, mft_ni->name, mft_ni->name_len,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1829) 			CASE_SENSITIVE, rl[1].vcn, NULL, 0, ctx);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1830) 	if (unlikely(ret)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1831) 		ntfs_error(vol->sb, "Failed to find last attribute extent of "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1832) 				"mft data attribute.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1833) 		if (ret == -ENOENT)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1834) 			ret = -EIO;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1835) 		goto undo_alloc;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1836) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1837) 	a = ctx->attr;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1838) 	ll = sle64_to_cpu(a->data.non_resident.lowest_vcn);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1839) 	/* Search back for the previous last allocated cluster of mft bitmap. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1840) 	for (rl2 = rl; rl2 > mft_ni->runlist.rl; rl2--) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1841) 		if (ll >= rl2->vcn)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1842) 			break;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1843) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1844) 	BUG_ON(ll < rl2->vcn);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1845) 	BUG_ON(ll >= rl2->vcn + rl2->length);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1846) 	/* Get the size for the new mapping pairs array for this extent. */
fa3be92317c4a (Anton Altaparmakov 2005-06-25 17:15:36 +0100 1847) 	mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, -1);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1848) 	if (unlikely(mp_size <= 0)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1849) 		ntfs_error(vol->sb, "Get size for mapping pairs failed for "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1850) 				"mft data attribute extent.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1851) 		ret = mp_size;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1852) 		if (!ret)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1853) 			ret = -EIO;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1854) 		goto undo_alloc;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1855) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1856) 	/* Expand the attribute record if necessary. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1857) 	old_alen = le32_to_cpu(a->length);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1858) 	ret = ntfs_attr_record_resize(ctx->mrec, a, mp_size +
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1859) 			le16_to_cpu(a->data.non_resident.mapping_pairs_offset));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1860) 	if (unlikely(ret)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1861) 		if (ret != -ENOSPC) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1862) 			ntfs_error(vol->sb, "Failed to resize attribute "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1863) 					"record for mft data attribute.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1864) 			goto undo_alloc;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1865) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1866) 		// TODO: Deal with this by moving this extent to a new mft
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1867) 		// record or by starting a new extent in a new mft record or by
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1868) 		// moving other attributes out of this mft record.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1869) 		// Note: Use the special reserved mft records and ensure that
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1870) 		// this extent is not required to find the mft record in
b6ad6c52fe36a (Anton Altaparmakov 2005-02-15 10:08:43 +0000 1871) 		// question.  If no free special records left we would need to
b6ad6c52fe36a (Anton Altaparmakov 2005-02-15 10:08:43 +0000 1872) 		// move an existing record away, insert ours in its place, and
b6ad6c52fe36a (Anton Altaparmakov 2005-02-15 10:08:43 +0000 1873) 		// then place the moved record into the newly allocated space
b6ad6c52fe36a (Anton Altaparmakov 2005-02-15 10:08:43 +0000 1874) 		// and we would then need to update all references to this mft
b6ad6c52fe36a (Anton Altaparmakov 2005-02-15 10:08:43 +0000 1875) 		// record appropriately.  This is rather complicated...
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1876) 		ntfs_error(vol->sb, "Not enough space in this mft record to "
25985edcedea6 (Lucas De Marchi    2011-03-30 22:57:33 -0300 1877) 				"accommodate extended mft data attribute "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1878) 				"extent.  Cannot handle this yet.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1879) 		ret = -EOPNOTSUPP;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1880) 		goto undo_alloc;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1881) 	}
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700 1882) 	mp_rebuilt = true;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1883) 	/* Generate the mapping pairs array directly into the attr record. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1884) 	ret = ntfs_mapping_pairs_build(vol, (u8*)a +
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1885) 			le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
fa3be92317c4a (Anton Altaparmakov 2005-06-25 17:15:36 +0100 1886) 			mp_size, rl2, ll, -1, NULL);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1887) 	if (unlikely(ret)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1888) 		ntfs_error(vol->sb, "Failed to build mapping pairs array of "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1889) 				"mft data attribute.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1890) 		goto undo_alloc;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1891) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1892) 	/* Update the highest_vcn. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1893) 	a->data.non_resident.highest_vcn = cpu_to_sle64(rl[1].vcn - 1);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1894) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1895) 	 * We now have extended the mft data allocated_size by nr clusters.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1896) 	 * Reflect this in the ntfs_inode structure and the attribute record.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1897) 	 * @rl is the last (non-terminator) runlist element of mft data
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1898) 	 * attribute.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1899) 	 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1900) 	if (a->data.non_resident.lowest_vcn) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1901) 		/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1902) 		 * We are not in the first attribute extent, switch to it, but
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1903) 		 * first ensure the changes will make it to disk later.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1904) 		 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1905) 		flush_dcache_mft_record_page(ctx->ntfs_ino);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1906) 		mark_mft_record_dirty(ctx->ntfs_ino);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1907) 		ntfs_attr_reinit_search_ctx(ctx);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1908) 		ret = ntfs_attr_lookup(mft_ni->type, mft_ni->name,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1909) 				mft_ni->name_len, CASE_SENSITIVE, 0, NULL, 0,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1910) 				ctx);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1911) 		if (unlikely(ret)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1912) 			ntfs_error(vol->sb, "Failed to find first attribute "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1913) 					"extent of mft data attribute.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1914) 			goto restore_undo_alloc;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1915) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1916) 		a = ctx->attr;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1917) 	}
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1918) 	write_lock_irqsave(&mft_ni->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1919) 	mft_ni->allocated_size += nr << vol->cluster_size_bits;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1920) 	a->data.non_resident.allocated_size =
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1921) 			cpu_to_sle64(mft_ni->allocated_size);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1922) 	write_unlock_irqrestore(&mft_ni->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1923) 	/* Ensure the changes make it to disk. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1924) 	flush_dcache_mft_record_page(ctx->ntfs_ino);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1925) 	mark_mft_record_dirty(ctx->ntfs_ino);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1926) 	ntfs_attr_put_search_ctx(ctx);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1927) 	unmap_mft_record(mft_ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1928) 	up_write(&mft_ni->runlist.lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1929) 	ntfs_debug("Done.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1930) 	return 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1931) restore_undo_alloc:
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1932) 	ntfs_attr_reinit_search_ctx(ctx);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1933) 	if (ntfs_attr_lookup(mft_ni->type, mft_ni->name, mft_ni->name_len,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1934) 			CASE_SENSITIVE, rl[1].vcn, NULL, 0, ctx)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1935) 		ntfs_error(vol->sb, "Failed to find last attribute extent of "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1936) 				"mft data attribute.%s", es);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1937) 		write_lock_irqsave(&mft_ni->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1938) 		mft_ni->allocated_size += nr << vol->cluster_size_bits;
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 1939) 		write_unlock_irqrestore(&mft_ni->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1940) 		ntfs_attr_put_search_ctx(ctx);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1941) 		unmap_mft_record(mft_ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1942) 		up_write(&mft_ni->runlist.lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1943) 		/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1944) 		 * The only thing that is now wrong is ->allocated_size of the
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1945) 		 * base attribute extent which chkdsk should be able to fix.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1946) 		 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1947) 		NVolSetErrors(vol);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1948) 		return ret;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1949) 	}
511bea5ea2b2b (Anton Altaparmakov 2005-10-04 14:24:21 +0100 1950) 	ctx->attr->data.non_resident.highest_vcn =
511bea5ea2b2b (Anton Altaparmakov 2005-10-04 14:24:21 +0100 1951) 			cpu_to_sle64(old_last_vcn - 1);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1952) undo_alloc:
511bea5ea2b2b (Anton Altaparmakov 2005-10-04 14:24:21 +0100 1953) 	if (ntfs_cluster_free(mft_ni, old_last_vcn, -1, ctx) < 0) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1954) 		ntfs_error(vol->sb, "Failed to free clusters from mft data "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1955) 				"attribute.%s", es);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1956) 		NVolSetErrors(vol);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1957) 	}
511bea5ea2b2b (Anton Altaparmakov 2005-10-04 14:24:21 +0100 1958) 	a = ctx->attr;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1959) 	if (ntfs_rl_truncate_nolock(vol, &mft_ni->runlist, old_last_vcn)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1960) 		ntfs_error(vol->sb, "Failed to truncate mft data attribute "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1961) 				"runlist.%s", es);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1962) 		NVolSetErrors(vol);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1963) 	}
511bea5ea2b2b (Anton Altaparmakov 2005-10-04 14:24:21 +0100 1964) 	if (mp_rebuilt && !IS_ERR(ctx->mrec)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1965) 		if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu(
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1966) 				a->data.non_resident.mapping_pairs_offset),
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1967) 				old_alen - le16_to_cpu(
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1968) 				a->data.non_resident.mapping_pairs_offset),
fa3be92317c4a (Anton Altaparmakov 2005-06-25 17:15:36 +0100 1969) 				rl2, ll, -1, NULL)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1970) 			ntfs_error(vol->sb, "Failed to restore mapping pairs "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1971) 					"array.%s", es);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1972) 			NVolSetErrors(vol);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1973) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1974) 		if (ntfs_attr_record_resize(ctx->mrec, a, old_alen)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1975) 			ntfs_error(vol->sb, "Failed to restore attribute "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1976) 					"record.%s", es);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1977) 			NVolSetErrors(vol);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1978) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1979) 		flush_dcache_mft_record_page(ctx->ntfs_ino);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1980) 		mark_mft_record_dirty(ctx->ntfs_ino);
511bea5ea2b2b (Anton Altaparmakov 2005-10-04 14:24:21 +0100 1981) 	} else if (IS_ERR(ctx->mrec)) {
511bea5ea2b2b (Anton Altaparmakov 2005-10-04 14:24:21 +0100 1982) 		ntfs_error(vol->sb, "Failed to restore attribute search "
511bea5ea2b2b (Anton Altaparmakov 2005-10-04 14:24:21 +0100 1983) 				"context.%s", es);
511bea5ea2b2b (Anton Altaparmakov 2005-10-04 14:24:21 +0100 1984) 		NVolSetErrors(vol);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1985) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1986) 	if (ctx)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1987) 		ntfs_attr_put_search_ctx(ctx);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1988) 	if (!IS_ERR(mrec))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1989) 		unmap_mft_record(mft_ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1990) 	up_write(&mft_ni->runlist.lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1991) 	return ret;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1992) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1993) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1994) /**
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1995)  * ntfs_mft_record_layout - layout an mft record into a memory buffer
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1996)  * @vol:	volume to which the mft record will belong
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1997)  * @mft_no:	mft reference specifying the mft record number
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1998)  * @m:		destination buffer of size >= @vol->mft_record_size bytes
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 1999)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2000)  * Layout an empty, unused mft record with the mft record number @mft_no into
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2001)  * the buffer @m.  The volume @vol is needed because the mft record structure
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2002)  * was modified in NTFS 3.1 so we need to know which volume version this mft
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2003)  * record will be used on.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2004)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2005)  * Return 0 on success and -errno on error.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2006)  */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2007) static int ntfs_mft_record_layout(const ntfs_volume *vol, const s64 mft_no,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2008) 		MFT_RECORD *m)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2009) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2010) 	ATTR_RECORD *a;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2011) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2012) 	ntfs_debug("Entering for mft record 0x%llx.", (long long)mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2013) 	if (mft_no >= (1ll << 32)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2014) 		ntfs_error(vol->sb, "Mft record number 0x%llx exceeds "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2015) 				"maximum of 2^32.", (long long)mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2016) 		return -ERANGE;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2017) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2018) 	/* Start by clearing the whole mft record to gives us a clean slate. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2019) 	memset(m, 0, vol->mft_record_size);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2020) 	/* Aligned to 2-byte boundary. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2021) 	if (vol->major_ver < 3 || (vol->major_ver == 3 && !vol->minor_ver))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2022) 		m->usa_ofs = cpu_to_le16((sizeof(MFT_RECORD_OLD) + 1) & ~1);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2023) 	else {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2024) 		m->usa_ofs = cpu_to_le16((sizeof(MFT_RECORD) + 1) & ~1);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2025) 		/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2026) 		 * Set the NTFS 3.1+ specific fields while we know that the
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2027) 		 * volume version is 3.1+.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2028) 		 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2029) 		m->reserved = 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2030) 		m->mft_record_number = cpu_to_le32((u32)mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2031) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2032) 	m->magic = magic_FILE;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2033) 	if (vol->mft_record_size >= NTFS_BLOCK_SIZE)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2034) 		m->usa_count = cpu_to_le16(vol->mft_record_size /
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2035) 				NTFS_BLOCK_SIZE + 1);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2036) 	else {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2037) 		m->usa_count = cpu_to_le16(1);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2038) 		ntfs_warning(vol->sb, "Sector size is bigger than mft record "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2039) 				"size.  Setting usa_count to 1.  If chkdsk "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2040) 				"reports this as corruption, please email "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2041) 				"linux-ntfs-dev@lists.sourceforge.net stating "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2042) 				"that you saw this message and that the "
b6ad6c52fe36a (Anton Altaparmakov 2005-02-15 10:08:43 +0000 2043) 				"modified filesystem created was corrupt.  "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2044) 				"Thank you.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2045) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2046) 	/* Set the update sequence number to 1. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2047) 	*(le16*)((u8*)m + le16_to_cpu(m->usa_ofs)) = cpu_to_le16(1);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2048) 	m->lsn = 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2049) 	m->sequence_number = cpu_to_le16(1);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2050) 	m->link_count = 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2051) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2052) 	 * Place the attributes straight after the update sequence array,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2053) 	 * aligned to 8-byte boundary.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2054) 	 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2055) 	m->attrs_offset = cpu_to_le16((le16_to_cpu(m->usa_ofs) +
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2056) 			(le16_to_cpu(m->usa_count) << 1) + 7) & ~7);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2057) 	m->flags = 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2058) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2059) 	 * Using attrs_offset plus eight bytes (for the termination attribute).
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2060) 	 * attrs_offset is already aligned to 8-byte boundary, so no need to
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2061) 	 * align again.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2062) 	 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2063) 	m->bytes_in_use = cpu_to_le32(le16_to_cpu(m->attrs_offset) + 8);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2064) 	m->bytes_allocated = cpu_to_le32(vol->mft_record_size);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2065) 	m->base_mft_record = 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2066) 	m->next_attr_instance = 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2067) 	/* Add the termination attribute. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2068) 	a = (ATTR_RECORD*)((u8*)m + le16_to_cpu(m->attrs_offset));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2069) 	a->type = AT_END;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2070) 	a->length = 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2071) 	ntfs_debug("Done.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2072) 	return 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2073) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2074) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2075) /**
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2076)  * ntfs_mft_record_format - format an mft record on an ntfs volume
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2077)  * @vol:	volume on which to format the mft record
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2078)  * @mft_no:	mft record number to format
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2079)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2080)  * Format the mft record @mft_no in $MFT/$DATA, i.e. lay out an empty, unused
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2081)  * mft record into the appropriate place of the mft data attribute.  This is
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2082)  * used when extending the mft data attribute.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2083)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2084)  * Return 0 on success and -errno on error.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2085)  */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2086) static int ntfs_mft_record_format(const ntfs_volume *vol, const s64 mft_no)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2087) {
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2088) 	loff_t i_size;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2089) 	struct inode *mft_vi = vol->mft_ino;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2090) 	struct page *page;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2091) 	MFT_RECORD *m;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2092) 	pgoff_t index, end_index;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2093) 	unsigned int ofs;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2094) 	int err;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2095) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2096) 	ntfs_debug("Entering for mft record 0x%llx.", (long long)mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2097) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2098) 	 * The index into the page cache and the offset within the page cache
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2099) 	 * page of the wanted mft record.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2100) 	 */
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2101) 	index = mft_no << vol->mft_record_size_bits >> PAGE_SHIFT;
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2102) 	ofs = (mft_no << vol->mft_record_size_bits) & ~PAGE_MASK;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2103) 	/* The maximum valid index into the page cache for $MFT's data. */
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2104) 	i_size = i_size_read(mft_vi);
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2105) 	end_index = i_size >> PAGE_SHIFT;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2106) 	if (unlikely(index >= end_index)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2107) 		if (unlikely(index > end_index || ofs + vol->mft_record_size >=
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2108) 				(i_size & ~PAGE_MASK))) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2109) 			ntfs_error(vol->sb, "Tried to format non-existing mft "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2110) 					"record 0x%llx.", (long long)mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2111) 			return -ENOENT;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2112) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2113) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2114) 	/* Read, map, and pin the page containing the mft record. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2115) 	page = ntfs_map_page(mft_vi->i_mapping, index);
801678c5a3b4c (Hirofumi Nakagawa  2008-04-29 01:03:09 -0700 2116) 	if (IS_ERR(page)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2117) 		ntfs_error(vol->sb, "Failed to map page containing mft record "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2118) 				"to format 0x%llx.", (long long)mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2119) 		return PTR_ERR(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2120) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2121) 	lock_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2122) 	BUG_ON(!PageUptodate(page));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2123) 	ClearPageUptodate(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2124) 	m = (MFT_RECORD*)((u8*)page_address(page) + ofs);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2125) 	err = ntfs_mft_record_layout(vol, mft_no, m);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2126) 	if (unlikely(err)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2127) 		ntfs_error(vol->sb, "Failed to layout mft record 0x%llx.",
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2128) 				(long long)mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2129) 		SetPageUptodate(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2130) 		unlock_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2131) 		ntfs_unmap_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2132) 		return err;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2133) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2134) 	flush_dcache_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2135) 	SetPageUptodate(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2136) 	unlock_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2137) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2138) 	 * Make sure the mft record is written out to disk.  We could use
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2139) 	 * ilookup5() to check if an inode is in icache and so on but this is
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2140) 	 * unnecessary as ntfs_writepage() will write the dirty record anyway.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2141) 	 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2142) 	mark_ntfs_record_dirty(page, ofs);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2143) 	ntfs_unmap_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2144) 	ntfs_debug("Done.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2145) 	return 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2146) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2147) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2148) /**
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2149)  * ntfs_mft_record_alloc - allocate an mft record on an ntfs volume
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2150)  * @vol:	[IN]  volume on which to allocate the mft record
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2151)  * @mode:	[IN]  mode if want a file or directory, i.e. base inode or 0
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2152)  * @base_ni:	[IN]  open base inode if allocating an extent mft record or NULL
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2153)  * @mrec:	[OUT] on successful return this is the mapped mft record
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2154)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2155)  * Allocate an mft record in $MFT/$DATA of an open ntfs volume @vol.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2156)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2157)  * If @base_ni is NULL make the mft record a base mft record, i.e. a file or
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2158)  * direvctory inode, and allocate it at the default allocator position.  In
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2159)  * this case @mode is the file mode as given to us by the caller.  We in
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2160)  * particular use @mode to distinguish whether a file or a directory is being
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2161)  * created (S_IFDIR(mode) and S_IFREG(mode), respectively).
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2162)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2163)  * If @base_ni is not NULL make the allocated mft record an extent record,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2164)  * allocate it starting at the mft record after the base mft record and attach
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2165)  * the allocated and opened ntfs inode to the base inode @base_ni.  In this
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2166)  * case @mode must be 0 as it is meaningless for extent inodes.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2167)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2168)  * You need to check the return value with IS_ERR().  If false, the function
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2169)  * was successful and the return value is the now opened ntfs inode of the
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2170)  * allocated mft record.  *@mrec is then set to the allocated, mapped, pinned,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2171)  * and locked mft record.  If IS_ERR() is true, the function failed and the
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2172)  * error code is obtained from PTR_ERR(return value).  *@mrec is undefined in
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2173)  * this case.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2174)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2175)  * Allocation strategy:
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2176)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2177)  * To find a free mft record, we scan the mft bitmap for a zero bit.  To
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2178)  * optimize this we start scanning at the place specified by @base_ni or if
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2179)  * @base_ni is NULL we start where we last stopped and we perform wrap around
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2180)  * when we reach the end.  Note, we do not try to allocate mft records below
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2181)  * number 24 because numbers 0 to 15 are the defined system files anyway and 16
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2182)  * to 24 are special in that they are used for storing extension mft records
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2183)  * for the $DATA attribute of $MFT.  This is required to avoid the possibility
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2184)  * of creating a runlist with a circular dependency which once written to disk
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2185)  * can never be read in again.  Windows will only use records 16 to 24 for
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2186)  * normal files if the volume is completely out of space.  We never use them
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2187)  * which means that when the volume is really out of space we cannot create any
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2188)  * more files while Windows can still create up to 8 small files.  We can start
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2189)  * doing this at some later time, it does not matter much for now.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2190)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2191)  * When scanning the mft bitmap, we only search up to the last allocated mft
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2192)  * record.  If there are no free records left in the range 24 to number of
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2193)  * allocated mft records, then we extend the $MFT/$DATA attribute in order to
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2194)  * create free mft records.  We extend the allocated size of $MFT/$DATA by 16
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2195)  * records at a time or one cluster, if cluster size is above 16kiB.  If there
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2196)  * is not sufficient space to do this, we try to extend by a single mft record
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2197)  * or one cluster, if cluster size is above the mft record size.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2198)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2199)  * No matter how many mft records we allocate, we initialize only the first
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2200)  * allocated mft record, incrementing mft data size and initialized size
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2201)  * accordingly, open an ntfs_inode for it and return it to the caller, unless
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2202)  * there are less than 24 mft records, in which case we allocate and initialize
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2203)  * mft records until we reach record 24 which we consider as the first free mft
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2204)  * record for use by normal files.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2205)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2206)  * If during any stage we overflow the initialized data in the mft bitmap, we
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2207)  * extend the initialized size (and data size) by 8 bytes, allocating another
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2208)  * cluster if required.  The bitmap data size has to be at least equal to the
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2209)  * number of mft records in the mft, but it can be bigger, in which case the
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2210)  * superflous bits are padded with zeroes.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2211)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2212)  * Thus, when we return successfully (IS_ERR() is false), we will have:
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2213)  *	- initialized / extended the mft bitmap if necessary,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2214)  *	- initialized / extended the mft data if necessary,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2215)  *	- set the bit corresponding to the mft record being allocated in the
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2216)  *	  mft bitmap,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2217)  *	- opened an ntfs_inode for the allocated mft record, and we will have
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2218)  *	- returned the ntfs_inode as well as the allocated mapped, pinned, and
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2219)  *	  locked mft record.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2220)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2221)  * On error, the volume will be left in a consistent state and no record will
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2222)  * be allocated.  If rolling back a partial operation fails, we may leave some
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2223)  * inconsistent metadata in which case we set NVolErrors() so the volume is
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2224)  * left dirty when unmounted.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2225)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2226)  * Note, this function cannot make use of most of the normal functions, like
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2227)  * for example for attribute resizing, etc, because when the run list overflows
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2228)  * the base mft record and an attribute list is used, it is very important that
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2229)  * the extension mft records used to store the $DATA attribute of $MFT can be
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2230)  * reached without having to read the information contained inside them, as
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2231)  * this would make it impossible to find them in the first place after the
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2232)  * volume is unmounted.  $MFT/$BITMAP probably does not need to follow this
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2233)  * rule because the bitmap is not essential for finding the mft records, but on
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2234)  * the other hand, handling the bitmap in this special way would make life
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2235)  * easier because otherwise there might be circular invocations of functions
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2236)  * when reading the bitmap.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2237)  */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2238) ntfs_inode *ntfs_mft_record_alloc(ntfs_volume *vol, const int mode,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2239) 		ntfs_inode *base_ni, MFT_RECORD **mrec)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2240) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2241) 	s64 ll, bit, old_data_initialized, old_data_size;
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2242) 	unsigned long flags;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2243) 	struct inode *vi;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2244) 	struct page *page;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2245) 	ntfs_inode *mft_ni, *mftbmp_ni, *ni;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2246) 	ntfs_attr_search_ctx *ctx;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2247) 	MFT_RECORD *m;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2248) 	ATTR_RECORD *a;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2249) 	pgoff_t index;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2250) 	unsigned int ofs;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2251) 	int err;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2252) 	le16 seq_no, usn;
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700 2253) 	bool record_formatted = false;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2254) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2255) 	if (base_ni) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2256) 		ntfs_debug("Entering (allocating an extent mft record for "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2257) 				"base mft record 0x%llx).",
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2258) 				(long long)base_ni->mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2259) 		/* @mode and @base_ni are mutually exclusive. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2260) 		BUG_ON(mode);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2261) 	} else
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2262) 		ntfs_debug("Entering (allocating a base mft record).");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2263) 	if (mode) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2264) 		/* @mode and @base_ni are mutually exclusive. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2265) 		BUG_ON(base_ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2266) 		/* We only support creation of normal files and directories. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2267) 		if (!S_ISREG(mode) && !S_ISDIR(mode))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2268) 			return ERR_PTR(-EOPNOTSUPP);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2269) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2270) 	BUG_ON(!mrec);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2271) 	mft_ni = NTFS_I(vol->mft_ino);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2272) 	mftbmp_ni = NTFS_I(vol->mftbmp_ino);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2273) 	down_write(&vol->mftbmp_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2274) 	bit = ntfs_mft_bitmap_find_and_alloc_free_rec_nolock(vol, base_ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2275) 	if (bit >= 0) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2276) 		ntfs_debug("Found and allocated free record (#1), bit 0x%llx.",
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2277) 				(long long)bit);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2278) 		goto have_alloc_rec;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2279) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2280) 	if (bit != -ENOSPC) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2281) 		up_write(&vol->mftbmp_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2282) 		return ERR_PTR(bit);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2283) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2284) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2285) 	 * No free mft records left.  If the mft bitmap already covers more
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2286) 	 * than the currently used mft records, the next records are all free,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2287) 	 * so we can simply allocate the first unused mft record.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2288) 	 * Note: We also have to make sure that the mft bitmap at least covers
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2289) 	 * the first 24 mft records as they are special and whilst they may not
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2290) 	 * be in use, we do not allocate from them.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2291) 	 */
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2292) 	read_lock_irqsave(&mft_ni->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2293) 	ll = mft_ni->initialized_size >> vol->mft_record_size_bits;
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2294) 	read_unlock_irqrestore(&mft_ni->size_lock, flags);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2295) 	read_lock_irqsave(&mftbmp_ni->size_lock, flags);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2296) 	old_data_initialized = mftbmp_ni->initialized_size;
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2297) 	read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2298) 	if (old_data_initialized << 3 > ll && old_data_initialized > 3) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2299) 		bit = ll;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2300) 		if (bit < 24)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2301) 			bit = 24;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2302) 		if (unlikely(bit >= (1ll << 32)))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2303) 			goto max_err_out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2304) 		ntfs_debug("Found free record (#2), bit 0x%llx.",
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2305) 				(long long)bit);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2306) 		goto found_free_rec;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2307) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2308) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2309) 	 * The mft bitmap needs to be expanded until it covers the first unused
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2310) 	 * mft record that we can allocate.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2311) 	 * Note: The smallest mft record we allocate is mft record 24.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2312) 	 */
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2313) 	bit = old_data_initialized << 3;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2314) 	if (unlikely(bit >= (1ll << 32)))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2315) 		goto max_err_out;
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2316) 	read_lock_irqsave(&mftbmp_ni->size_lock, flags);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2317) 	old_data_size = mftbmp_ni->allocated_size;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2318) 	ntfs_debug("Status of mftbmp before extension: allocated_size 0x%llx, "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2319) 			"data_size 0x%llx, initialized_size 0x%llx.",
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2320) 			(long long)old_data_size,
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2321) 			(long long)i_size_read(vol->mftbmp_ino),
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2322) 			(long long)old_data_initialized);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2323) 	read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2324) 	if (old_data_initialized + 8 > old_data_size) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2325) 		/* Need to extend bitmap by one more cluster. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2326) 		ntfs_debug("mftbmp: initialized_size + 8 > allocated_size.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2327) 		err = ntfs_mft_bitmap_extend_allocation_nolock(vol);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2328) 		if (unlikely(err)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2329) 			up_write(&vol->mftbmp_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2330) 			goto err_out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2331) 		}
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2332) #ifdef DEBUG
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2333) 		read_lock_irqsave(&mftbmp_ni->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2334) 		ntfs_debug("Status of mftbmp after allocation extension: "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2335) 				"allocated_size 0x%llx, data_size 0x%llx, "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2336) 				"initialized_size 0x%llx.",
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2337) 				(long long)mftbmp_ni->allocated_size,
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2338) 				(long long)i_size_read(vol->mftbmp_ino),
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2339) 				(long long)mftbmp_ni->initialized_size);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2340) 		read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2341) #endif /* DEBUG */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2342) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2343) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2344) 	 * We now have sufficient allocated space, extend the initialized_size
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2345) 	 * as well as the data_size if necessary and fill the new space with
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2346) 	 * zeroes.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2347) 	 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2348) 	err = ntfs_mft_bitmap_extend_initialized_nolock(vol);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2349) 	if (unlikely(err)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2350) 		up_write(&vol->mftbmp_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2351) 		goto err_out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2352) 	}
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2353) #ifdef DEBUG
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2354) 	read_lock_irqsave(&mftbmp_ni->size_lock, flags);
25985edcedea6 (Lucas De Marchi    2011-03-30 22:57:33 -0300 2355) 	ntfs_debug("Status of mftbmp after initialized extension: "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2356) 			"allocated_size 0x%llx, data_size 0x%llx, "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2357) 			"initialized_size 0x%llx.",
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2358) 			(long long)mftbmp_ni->allocated_size,
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2359) 			(long long)i_size_read(vol->mftbmp_ino),
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2360) 			(long long)mftbmp_ni->initialized_size);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2361) 	read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2362) #endif /* DEBUG */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2363) 	ntfs_debug("Found free record (#3), bit 0x%llx.", (long long)bit);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2364) found_free_rec:
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2365) 	/* @bit is the found free mft record, allocate it in the mft bitmap. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2366) 	ntfs_debug("At found_free_rec.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2367) 	err = ntfs_bitmap_set_bit(vol->mftbmp_ino, bit);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2368) 	if (unlikely(err)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2369) 		ntfs_error(vol->sb, "Failed to allocate bit in mft bitmap.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2370) 		up_write(&vol->mftbmp_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2371) 		goto err_out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2372) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2373) 	ntfs_debug("Set bit 0x%llx in mft bitmap.", (long long)bit);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2374) have_alloc_rec:
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2375) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2376) 	 * The mft bitmap is now uptodate.  Deal with mft data attribute now.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2377) 	 * Note, we keep hold of the mft bitmap lock for writing until all
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2378) 	 * modifications to the mft data attribute are complete, too, as they
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2379) 	 * will impact decisions for mft bitmap and mft record allocation done
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2380) 	 * by a parallel allocation and if the lock is not maintained a
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2381) 	 * parallel allocation could allocate the same mft record as this one.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2382) 	 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2383) 	ll = (bit + 1) << vol->mft_record_size_bits;
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2384) 	read_lock_irqsave(&mft_ni->size_lock, flags);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2385) 	old_data_initialized = mft_ni->initialized_size;
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2386) 	read_unlock_irqrestore(&mft_ni->size_lock, flags);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2387) 	if (ll <= old_data_initialized) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2388) 		ntfs_debug("Allocated mft record already initialized.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2389) 		goto mft_rec_already_initialized;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2390) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2391) 	ntfs_debug("Initializing allocated mft record.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2392) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2393) 	 * The mft record is outside the initialized data.  Extend the mft data
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2394) 	 * attribute until it covers the allocated record.  The loop is only
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2395) 	 * actually traversed more than once when a freshly formatted volume is
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2396) 	 * first written to so it optimizes away nicely in the common case.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2397) 	 */
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2398) 	read_lock_irqsave(&mft_ni->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2399) 	ntfs_debug("Status of mft data before extension: "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2400) 			"allocated_size 0x%llx, data_size 0x%llx, "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2401) 			"initialized_size 0x%llx.",
3834c3f227725 (Anton Altaparmakov 2005-01-13 11:04:39 +0000 2402) 			(long long)mft_ni->allocated_size,
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2403) 			(long long)i_size_read(vol->mft_ino),
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2404) 			(long long)mft_ni->initialized_size);
3834c3f227725 (Anton Altaparmakov 2005-01-13 11:04:39 +0000 2405) 	while (ll > mft_ni->allocated_size) {
3834c3f227725 (Anton Altaparmakov 2005-01-13 11:04:39 +0000 2406) 		read_unlock_irqrestore(&mft_ni->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2407) 		err = ntfs_mft_data_extend_allocation_nolock(vol);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2408) 		if (unlikely(err)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2409) 			ntfs_error(vol->sb, "Failed to extend mft data "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2410) 					"allocation.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2411) 			goto undo_mftbmp_alloc_nolock;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2412) 		}
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2413) 		read_lock_irqsave(&mft_ni->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2414) 		ntfs_debug("Status of mft data after allocation extension: "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2415) 				"allocated_size 0x%llx, data_size 0x%llx, "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2416) 				"initialized_size 0x%llx.",
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2417) 				(long long)mft_ni->allocated_size,
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2418) 				(long long)i_size_read(vol->mft_ino),
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2419) 				(long long)mft_ni->initialized_size);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2420) 	}
3834c3f227725 (Anton Altaparmakov 2005-01-13 11:04:39 +0000 2421) 	read_unlock_irqrestore(&mft_ni->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2422) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2423) 	 * Extend mft data initialized size (and data size of course) to reach
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2424) 	 * the allocated mft record, formatting the mft records allong the way.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2425) 	 * Note: We only modify the ntfs_inode structure as that is all that is
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2426) 	 * needed by ntfs_mft_record_format().  We will update the attribute
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2427) 	 * record itself in one fell swoop later on.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2428) 	 */
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2429) 	write_lock_irqsave(&mft_ni->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2430) 	old_data_initialized = mft_ni->initialized_size;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2431) 	old_data_size = vol->mft_ino->i_size;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2432) 	while (ll > mft_ni->initialized_size) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2433) 		s64 new_initialized_size, mft_no;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2434) 		
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2435) 		new_initialized_size = mft_ni->initialized_size +
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2436) 				vol->mft_record_size;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2437) 		mft_no = mft_ni->initialized_size >> vol->mft_record_size_bits;
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2438) 		if (new_initialized_size > i_size_read(vol->mft_ino))
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2439) 			i_size_write(vol->mft_ino, new_initialized_size);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2440) 		write_unlock_irqrestore(&mft_ni->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2441) 		ntfs_debug("Initializing mft record 0x%llx.",
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2442) 				(long long)mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2443) 		err = ntfs_mft_record_format(vol, mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2444) 		if (unlikely(err)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2445) 			ntfs_error(vol->sb, "Failed to format mft record.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2446) 			goto undo_data_init;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2447) 		}
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2448) 		write_lock_irqsave(&mft_ni->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2449) 		mft_ni->initialized_size = new_initialized_size;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2450) 	}
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2451) 	write_unlock_irqrestore(&mft_ni->size_lock, flags);
c49c31115067b (Richard Knutsson   2006-09-30 23:27:12 -0700 2452) 	record_formatted = true;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2453) 	/* Update the mft data attribute record to reflect the new sizes. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2454) 	m = map_mft_record(mft_ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2455) 	if (IS_ERR(m)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2456) 		ntfs_error(vol->sb, "Failed to map mft record.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2457) 		err = PTR_ERR(m);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2458) 		goto undo_data_init;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2459) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2460) 	ctx = ntfs_attr_get_search_ctx(mft_ni, m);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2461) 	if (unlikely(!ctx)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2462) 		ntfs_error(vol->sb, "Failed to get search context.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2463) 		err = -ENOMEM;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2464) 		unmap_mft_record(mft_ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2465) 		goto undo_data_init;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2466) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2467) 	err = ntfs_attr_lookup(mft_ni->type, mft_ni->name, mft_ni->name_len,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2468) 			CASE_SENSITIVE, 0, NULL, 0, ctx);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2469) 	if (unlikely(err)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2470) 		ntfs_error(vol->sb, "Failed to find first attribute extent of "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2471) 				"mft data attribute.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2472) 		ntfs_attr_put_search_ctx(ctx);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2473) 		unmap_mft_record(mft_ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2474) 		goto undo_data_init;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2475) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2476) 	a = ctx->attr;
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2477) 	read_lock_irqsave(&mft_ni->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2478) 	a->data.non_resident.initialized_size =
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2479) 			cpu_to_sle64(mft_ni->initialized_size);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2480) 	a->data.non_resident.data_size =
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2481) 			cpu_to_sle64(i_size_read(vol->mft_ino));
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2482) 	read_unlock_irqrestore(&mft_ni->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2483) 	/* Ensure the changes make it to disk. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2484) 	flush_dcache_mft_record_page(ctx->ntfs_ino);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2485) 	mark_mft_record_dirty(ctx->ntfs_ino);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2486) 	ntfs_attr_put_search_ctx(ctx);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2487) 	unmap_mft_record(mft_ni);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2488) 	read_lock_irqsave(&mft_ni->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2489) 	ntfs_debug("Status of mft data after mft record initialization: "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2490) 			"allocated_size 0x%llx, data_size 0x%llx, "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2491) 			"initialized_size 0x%llx.",
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2492) 			(long long)mft_ni->allocated_size,
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2493) 			(long long)i_size_read(vol->mft_ino),
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2494) 			(long long)mft_ni->initialized_size);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2495) 	BUG_ON(i_size_read(vol->mft_ino) > mft_ni->allocated_size);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2496) 	BUG_ON(mft_ni->initialized_size > i_size_read(vol->mft_ino));
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2497) 	read_unlock_irqrestore(&mft_ni->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2498) mft_rec_already_initialized:
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2499) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2500) 	 * We can finally drop the mft bitmap lock as the mft data attribute
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2501) 	 * has been fully updated.  The only disparity left is that the
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2502) 	 * allocated mft record still needs to be marked as in use to match the
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2503) 	 * set bit in the mft bitmap but this is actually not a problem since
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2504) 	 * this mft record is not referenced from anywhere yet and the fact
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2505) 	 * that it is allocated in the mft bitmap means that no-one will try to
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2506) 	 * allocate it either.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2507) 	 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2508) 	up_write(&vol->mftbmp_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2509) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2510) 	 * We now have allocated and initialized the mft record.  Calculate the
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2511) 	 * index of and the offset within the page cache page the record is in.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2512) 	 */
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2513) 	index = bit << vol->mft_record_size_bits >> PAGE_SHIFT;
09cbfeaf1a5a6 (Kirill A. Shutemov 2016-04-01 15:29:47 +0300 2514) 	ofs = (bit << vol->mft_record_size_bits) & ~PAGE_MASK;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2515) 	/* Read, map, and pin the page containing the mft record. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2516) 	page = ntfs_map_page(vol->mft_ino->i_mapping, index);
801678c5a3b4c (Hirofumi Nakagawa  2008-04-29 01:03:09 -0700 2517) 	if (IS_ERR(page)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2518) 		ntfs_error(vol->sb, "Failed to map page containing allocated "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2519) 				"mft record 0x%llx.", (long long)bit);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2520) 		err = PTR_ERR(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2521) 		goto undo_mftbmp_alloc;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2522) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2523) 	lock_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2524) 	BUG_ON(!PageUptodate(page));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2525) 	ClearPageUptodate(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2526) 	m = (MFT_RECORD*)((u8*)page_address(page) + ofs);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2527) 	/* If we just formatted the mft record no need to do it again. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2528) 	if (!record_formatted) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2529) 		/* Sanity check that the mft record is really not in use. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2530) 		if (ntfs_is_file_record(m->magic) &&
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2531) 				(m->flags & MFT_RECORD_IN_USE)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2532) 			ntfs_error(vol->sb, "Mft record 0x%llx was marked "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2533) 					"free in mft bitmap but is marked "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2534) 					"used itself.  Corrupt filesystem.  "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2535) 					"Unmount and run chkdsk.",
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2536) 					(long long)bit);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2537) 			err = -EIO;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2538) 			SetPageUptodate(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2539) 			unlock_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2540) 			ntfs_unmap_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2541) 			NVolSetErrors(vol);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2542) 			goto undo_mftbmp_alloc;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2543) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2544) 		/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2545) 		 * We need to (re-)format the mft record, preserving the
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2546) 		 * sequence number if it is not zero as well as the update
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2547) 		 * sequence number if it is not zero or -1 (0xffff).  This
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2548) 		 * means we do not need to care whether or not something went
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2549) 		 * wrong with the previous mft record.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2550) 		 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2551) 		seq_no = m->sequence_number;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2552) 		usn = *(le16*)((u8*)m + le16_to_cpu(m->usa_ofs));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2553) 		err = ntfs_mft_record_layout(vol, bit, m);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2554) 		if (unlikely(err)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2555) 			ntfs_error(vol->sb, "Failed to layout allocated mft "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2556) 					"record 0x%llx.", (long long)bit);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2557) 			SetPageUptodate(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2558) 			unlock_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2559) 			ntfs_unmap_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2560) 			goto undo_mftbmp_alloc;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2561) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2562) 		if (seq_no)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2563) 			m->sequence_number = seq_no;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2564) 		if (usn && le16_to_cpu(usn) != 0xffff)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2565) 			*(le16*)((u8*)m + le16_to_cpu(m->usa_ofs)) = usn;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2566) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2567) 	/* Set the mft record itself in use. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2568) 	m->flags |= MFT_RECORD_IN_USE;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2569) 	if (S_ISDIR(mode))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2570) 		m->flags |= MFT_RECORD_IS_DIRECTORY;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2571) 	flush_dcache_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2572) 	SetPageUptodate(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2573) 	if (base_ni) {
af5eb745efe97 (Anton Altaparmakov 2011-01-28 20:45:28 +0000 2574) 		MFT_RECORD *m_tmp;
af5eb745efe97 (Anton Altaparmakov 2011-01-28 20:45:28 +0000 2575) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2576) 		/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2577) 		 * Setup the base mft record in the extent mft record.  This
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2578) 		 * completes initialization of the allocated extent mft record
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2579) 		 * and we can simply use it with map_extent_mft_record().
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2580) 		 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2581) 		m->base_mft_record = MK_LE_MREF(base_ni->mft_no,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2582) 				base_ni->seq_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2583) 		/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2584) 		 * Allocate an extent inode structure for the new mft record,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2585) 		 * attach it to the base inode @base_ni and map, pin, and lock
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2586) 		 * its, i.e. the allocated, mft record.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2587) 		 */
af5eb745efe97 (Anton Altaparmakov 2011-01-28 20:45:28 +0000 2588) 		m_tmp = map_extent_mft_record(base_ni, bit, &ni);
af5eb745efe97 (Anton Altaparmakov 2011-01-28 20:45:28 +0000 2589) 		if (IS_ERR(m_tmp)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2590) 			ntfs_error(vol->sb, "Failed to map allocated extent "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2591) 					"mft record 0x%llx.", (long long)bit);
af5eb745efe97 (Anton Altaparmakov 2011-01-28 20:45:28 +0000 2592) 			err = PTR_ERR(m_tmp);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2593) 			/* Set the mft record itself not in use. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2594) 			m->flags &= cpu_to_le16(
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2595) 					~le16_to_cpu(MFT_RECORD_IN_USE));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2596) 			flush_dcache_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2597) 			/* Make sure the mft record is written out to disk. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2598) 			mark_ntfs_record_dirty(page, ofs);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2599) 			unlock_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2600) 			ntfs_unmap_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2601) 			goto undo_mftbmp_alloc;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2602) 		}
af5eb745efe97 (Anton Altaparmakov 2011-01-28 20:45:28 +0000 2603) 		BUG_ON(m != m_tmp);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2604) 		/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2605) 		 * Make sure the allocated mft record is written out to disk.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2606) 		 * No need to set the inode dirty because the caller is going
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2607) 		 * to do that anyway after finishing with the new extent mft
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2608) 		 * record (e.g. at a minimum a new attribute will be added to
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2609) 		 * the mft record.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2610) 		 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2611) 		mark_ntfs_record_dirty(page, ofs);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2612) 		unlock_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2613) 		/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2614) 		 * Need to unmap the page since map_extent_mft_record() mapped
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2615) 		 * it as well so we have it mapped twice at the moment.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2616) 		 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2617) 		ntfs_unmap_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2618) 	} else {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2619) 		/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2620) 		 * Allocate a new VFS inode and set it up.  NOTE: @vi->i_nlink
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2621) 		 * is set to 1 but the mft record->link_count is 0.  The caller
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2622) 		 * needs to bear this in mind.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2623) 		 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2624) 		vi = new_inode(vol->sb);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2625) 		if (unlikely(!vi)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2626) 			err = -ENOMEM;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2627) 			/* Set the mft record itself not in use. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2628) 			m->flags &= cpu_to_le16(
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2629) 					~le16_to_cpu(MFT_RECORD_IN_USE));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2630) 			flush_dcache_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2631) 			/* Make sure the mft record is written out to disk. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2632) 			mark_ntfs_record_dirty(page, ofs);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2633) 			unlock_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2634) 			ntfs_unmap_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2635) 			goto undo_mftbmp_alloc;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2636) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2637) 		vi->i_ino = bit;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2638) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2639) 		/* The owner and group come from the ntfs volume. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2640) 		vi->i_uid = vol->uid;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2641) 		vi->i_gid = vol->gid;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2642) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2643) 		/* Initialize the ntfs specific part of @vi. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2644) 		ntfs_init_big_inode(vi);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2645) 		ni = NTFS_I(vi);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2646) 		/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2647) 		 * Set the appropriate mode, attribute type, and name.  For
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2648) 		 * directories, also setup the index values to the defaults.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2649) 		 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2650) 		if (S_ISDIR(mode)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2651) 			vi->i_mode = S_IFDIR | S_IRWXUGO;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2652) 			vi->i_mode &= ~vol->dmask;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2653) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2654) 			NInoSetMstProtected(ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2655) 			ni->type = AT_INDEX_ALLOCATION;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2656) 			ni->name = I30;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2657) 			ni->name_len = 4;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2658) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2659) 			ni->itype.index.block_size = 4096;
b9a2838cc26c4 (Akinobu Mita       2006-03-26 01:39:53 -0800 2660) 			ni->itype.index.block_size_bits = ntfs_ffs(4096) - 1;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2661) 			ni->itype.index.collation_rule = COLLATION_FILE_NAME;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2662) 			if (vol->cluster_size <= ni->itype.index.block_size) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2663) 				ni->itype.index.vcn_size = vol->cluster_size;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2664) 				ni->itype.index.vcn_size_bits =
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2665) 						vol->cluster_size_bits;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2666) 			} else {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2667) 				ni->itype.index.vcn_size = vol->sector_size;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2668) 				ni->itype.index.vcn_size_bits =
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2669) 						vol->sector_size_bits;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2670) 			}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2671) 		} else {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2672) 			vi->i_mode = S_IFREG | S_IRWXUGO;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2673) 			vi->i_mode &= ~vol->fmask;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2674) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2675) 			ni->type = AT_DATA;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2676) 			ni->name = NULL;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2677) 			ni->name_len = 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2678) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2679) 		if (IS_RDONLY(vi))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2680) 			vi->i_mode &= ~S_IWUGO;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2681) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2682) 		/* Set the inode times to the current time. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2683) 		vi->i_atime = vi->i_mtime = vi->i_ctime =
c2050a454c7f1 (Deepa Dinamani     2016-09-14 07:48:06 -0700 2684) 			current_time(vi);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2685) 		/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2686) 		 * Set the file size to 0, the ntfs inode sizes are set to 0 by
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2687) 		 * the call to ntfs_init_big_inode() below.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2688) 		 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2689) 		vi->i_size = 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2690) 		vi->i_blocks = 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2691) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2692) 		/* Set the sequence number. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2693) 		vi->i_generation = ni->seq_no = le16_to_cpu(m->sequence_number);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2694) 		/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2695) 		 * Manually map, pin, and lock the mft record as we already
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2696) 		 * have its page mapped and it is very easy to do.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2697) 		 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2698) 		atomic_inc(&ni->count);
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000 2699) 		mutex_lock(&ni->mrec_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2700) 		ni->page = page;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2701) 		ni->page_ofs = ofs;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2702) 		/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2703) 		 * Make sure the allocated mft record is written out to disk.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2704) 		 * NOTE: We do not set the ntfs inode dirty because this would
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2705) 		 * fail in ntfs_write_inode() because the inode does not have a
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2706) 		 * standard information attribute yet.  Also, there is no need
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2707) 		 * to set the inode dirty because the caller is going to do
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2708) 		 * that anyway after finishing with the new mft record (e.g. at
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2709) 		 * a minimum some new attributes will be added to the mft
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2710) 		 * record.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2711) 		 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2712) 		mark_ntfs_record_dirty(page, ofs);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2713) 		unlock_page(page);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2714) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2715) 		/* Add the inode to the inode hash for the superblock. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2716) 		insert_inode_hash(vi);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2717) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2718) 		/* Update the default mft allocation position. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2719) 		vol->mft_data_pos = bit + 1;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2720) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2721) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2722) 	 * Return the opened, allocated inode of the allocated mft record as
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2723) 	 * well as the mapped, pinned, and locked mft record.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2724) 	 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2725) 	ntfs_debug("Returning opened, allocated %sinode 0x%llx.",
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2726) 			base_ni ? "extent " : "", (long long)bit);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2727) 	*mrec = m;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2728) 	return ni;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2729) undo_data_init:
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2730) 	write_lock_irqsave(&mft_ni->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2731) 	mft_ni->initialized_size = old_data_initialized;
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2732) 	i_size_write(vol->mft_ino, old_data_size);
07a4e2da7dd3c (Anton Altaparmakov 2005-01-12 13:08:26 +0000 2733) 	write_unlock_irqrestore(&mft_ni->size_lock, flags);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2734) 	goto undo_mftbmp_alloc_nolock;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2735) undo_mftbmp_alloc:
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2736) 	down_write(&vol->mftbmp_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2737) undo_mftbmp_alloc_nolock:
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2738) 	if (ntfs_bitmap_clear_bit(vol->mftbmp_ino, bit)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2739) 		ntfs_error(vol->sb, "Failed to clear bit in mft bitmap.%s", es);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2740) 		NVolSetErrors(vol);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2741) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2742) 	up_write(&vol->mftbmp_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2743) err_out:
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2744) 	return ERR_PTR(err);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2745) max_err_out:
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2746) 	ntfs_warning(vol->sb, "Cannot allocate mft record because the maximum "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2747) 			"number of inodes (2^32) has already been reached.");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2748) 	up_write(&vol->mftbmp_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2749) 	return ERR_PTR(-ENOSPC);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2750) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2751) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2752) /**
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2753)  * ntfs_extent_mft_record_free - free an extent mft record on an ntfs volume
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2754)  * @ni:		ntfs inode of the mapped extent mft record to free
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2755)  * @m:		mapped extent mft record of the ntfs inode @ni
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2756)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2757)  * Free the mapped extent mft record @m of the extent ntfs inode @ni.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2758)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2759)  * Note that this function unmaps the mft record and closes and destroys @ni
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2760)  * internally and hence you cannot use either @ni nor @m any more after this
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2761)  * function returns success.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2762)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2763)  * On success return 0 and on error return -errno.  @ni and @m are still valid
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2764)  * in this case and have not been freed.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2765)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2766)  * For some errors an error message is displayed and the success code 0 is
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2767)  * returned and the volume is then left dirty on umount.  This makes sense in
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2768)  * case we could not rollback the changes that were already done since the
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2769)  * caller no longer wants to reference this mft record so it does not matter to
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2770)  * the caller if something is wrong with it as long as it is properly detached
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2771)  * from the base inode.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2772)  */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2773) int ntfs_extent_mft_record_free(ntfs_inode *ni, MFT_RECORD *m)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2774) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2775) 	unsigned long mft_no = ni->mft_no;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2776) 	ntfs_volume *vol = ni->vol;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2777) 	ntfs_inode *base_ni;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2778) 	ntfs_inode **extent_nis;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2779) 	int i, err;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2780) 	le16 old_seq_no;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2781) 	u16 seq_no;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2782) 	
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2783) 	BUG_ON(NInoAttr(ni));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2784) 	BUG_ON(ni->nr_extents != -1);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2785) 
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000 2786) 	mutex_lock(&ni->extent_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2787) 	base_ni = ni->ext.base_ntfs_ino;
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000 2788) 	mutex_unlock(&ni->extent_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2789) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2790) 	BUG_ON(base_ni->nr_extents <= 0);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2791) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2792) 	ntfs_debug("Entering for extent inode 0x%lx, base inode 0x%lx.\n",
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2793) 			mft_no, base_ni->mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2794) 
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000 2795) 	mutex_lock(&base_ni->extent_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2796) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2797) 	/* Make sure we are holding the only reference to the extent inode. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2798) 	if (atomic_read(&ni->count) > 2) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2799) 		ntfs_error(vol->sb, "Tried to free busy extent inode 0x%lx, "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2800) 				"not freeing.", base_ni->mft_no);
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000 2801) 		mutex_unlock(&base_ni->extent_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2802) 		return -EBUSY;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2803) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2804) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2805) 	/* Dissociate the ntfs inode from the base inode. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2806) 	extent_nis = base_ni->ext.extent_ntfs_inos;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2807) 	err = -ENOENT;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2808) 	for (i = 0; i < base_ni->nr_extents; i++) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2809) 		if (ni != extent_nis[i])
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2810) 			continue;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2811) 		extent_nis += i;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2812) 		base_ni->nr_extents--;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2813) 		memmove(extent_nis, extent_nis + 1, (base_ni->nr_extents - i) *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2814) 				sizeof(ntfs_inode*));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2815) 		err = 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2816) 		break;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2817) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2818) 
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000 2819) 	mutex_unlock(&base_ni->extent_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2820) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2821) 	if (unlikely(err)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2822) 		ntfs_error(vol->sb, "Extent inode 0x%lx is not attached to "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2823) 				"its base inode 0x%lx.", mft_no,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2824) 				base_ni->mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2825) 		BUG();
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2826) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2827) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2828) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2829) 	 * The extent inode is no longer attached to the base inode so no one
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2830) 	 * can get a reference to it any more.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2831) 	 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2832) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2833) 	/* Mark the mft record as not in use. */
63cd885426872 (Harvey Harrison    2009-03-31 15:23:52 -0700 2834) 	m->flags &= ~MFT_RECORD_IN_USE;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2835) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2836) 	/* Increment the sequence number, skipping zero, if it is not zero. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2837) 	old_seq_no = m->sequence_number;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2838) 	seq_no = le16_to_cpu(old_seq_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2839) 	if (seq_no == 0xffff)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2840) 		seq_no = 1;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2841) 	else if (seq_no)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2842) 		seq_no++;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2843) 	m->sequence_number = cpu_to_le16(seq_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2844) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2845) 	/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2846) 	 * Set the ntfs inode dirty and write it out.  We do not need to worry
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2847) 	 * about the base inode here since whatever caused the extent mft
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2848) 	 * record to be freed is guaranteed to do it already.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2849) 	 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2850) 	NInoSetDirty(ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2851) 	err = write_mft_record(ni, m, 0);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2852) 	if (unlikely(err)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2853) 		ntfs_error(vol->sb, "Failed to write mft record 0x%lx, not "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2854) 				"freeing.", mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2855) 		goto rollback;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2856) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2857) rollback_error:
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2858) 	/* Unmap and throw away the now freed extent inode. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2859) 	unmap_extent_mft_record(ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2860) 	ntfs_clear_extent_inode(ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2861) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2862) 	/* Clear the bit in the $MFT/$BITMAP corresponding to this record. */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2863) 	down_write(&vol->mftbmp_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2864) 	err = ntfs_bitmap_clear_bit(vol->mftbmp_ino, mft_no);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2865) 	up_write(&vol->mftbmp_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2866) 	if (unlikely(err)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2867) 		/*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2868) 		 * The extent inode is gone but we failed to deallocate it in
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2869) 		 * the mft bitmap.  Just emit a warning and leave the volume
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2870) 		 * dirty on umount.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2871) 		 */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2872) 		ntfs_error(vol->sb, "Failed to clear bit in mft bitmap.%s", es);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2873) 		NVolSetErrors(vol);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2874) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2875) 	return 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2876) rollback:
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2877) 	/* Rollback what we did... */
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000 2878) 	mutex_lock(&base_ni->extent_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2879) 	extent_nis = base_ni->ext.extent_ntfs_inos;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2880) 	if (!(base_ni->nr_extents & 3)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2881) 		int new_size = (base_ni->nr_extents + 4) * sizeof(ntfs_inode*);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2882) 
f52720ca5f485 (Panagiotis Issaris 2006-09-27 01:49:39 -0700 2883) 		extent_nis = kmalloc(new_size, GFP_NOFS);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2884) 		if (unlikely(!extent_nis)) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2885) 			ntfs_error(vol->sb, "Failed to allocate internal "
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2886) 					"buffer during rollback.%s", es);
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000 2887) 			mutex_unlock(&base_ni->extent_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2888) 			NVolSetErrors(vol);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2889) 			goto rollback_error;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2890) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2891) 		if (base_ni->nr_extents) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2892) 			BUG_ON(!base_ni->ext.extent_ntfs_inos);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2893) 			memcpy(extent_nis, base_ni->ext.extent_ntfs_inos,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2894) 					new_size - 4 * sizeof(ntfs_inode*));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2895) 			kfree(base_ni->ext.extent_ntfs_inos);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2896) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2897) 		base_ni->ext.extent_ntfs_inos = extent_nis;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2898) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2899) 	m->flags |= MFT_RECORD_IN_USE;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2900) 	m->sequence_number = old_seq_no;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2901) 	extent_nis[base_ni->nr_extents++] = ni;
4e5e529ad684f (Ingo Molnar        2006-03-23 16:57:48 +0000 2902) 	mutex_unlock(&base_ni->extent_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2903) 	mark_mft_record_dirty(ni);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2904) 	return err;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2905) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 2906) #endif /* NTFS_RW */