1/**
2 * aops.c - NTFS kernel address space operations and page cache handling.
3 *	    Part of the Linux-NTFS project.
4 *
5 * Copyright (c) 2001-2006 Anton Altaparmakov
6 * Copyright (c) 2002 Richard Russon
7 *
8 * This program/include file is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as published
10 * by the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program/include file is distributed in the hope that it will be
14 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program (in the main directory of the Linux-NTFS
20 * distribution in the file COPYING); if not, write to the Free Software
21 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22 */
23
24#include <linux/errno.h>
25#include <linux/fs.h>
26#include <linux/mm.h>
27#include <linux/pagemap.h>
28#include <linux/swap.h>
29#include <linux/buffer_head.h>
30#include <linux/writeback.h>
31#include <linux/bit_spinlock.h>
32
33#include "aops.h"
34#include "attrib.h"
35#include "debug.h"
36#include "inode.h"
37#include "mft.h"
38#include "runlist.h"
39#include "types.h"
40#include "ntfs.h"
41
42/**
43 * ntfs_end_buffer_async_read - async io completion for reading attributes
44 * @bh:		buffer head on which io is completed
45 * @uptodate:	whether @bh is now uptodate or not
46 *
47 * Asynchronous I/O completion handler for reading pages belonging to the
48 * attribute address space of an inode.  The inodes can either be files or
49 * directories or they can be fake inodes describing some attribute.
50 *
51 * If NInoMstProtected(), perform the post read mst fixups when all IO on the
52 * page has been completed and mark the page uptodate or set the error bit on
53 * the page.  To determine the size of the records that need fixing up, we
54 * cheat a little bit by setting the index_block_size in ntfs_inode to the ntfs
55 * record size, and index_block_size_bits, to the log(base 2) of the ntfs
56 * record size.
57 */
58static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
59{
60	unsigned long flags;
61	struct buffer_head *first, *tmp;
62	struct page *page;
63	struct inode *vi;
64	ntfs_inode *ni;
65	int page_uptodate = 1;
66
67	page = bh->b_page;
68	vi = page->mapping->host;
69	ni = NTFS_I(vi);
70
71	if (likely(uptodate)) {
72		loff_t i_size;
73		s64 file_ofs, init_size;
74
75		set_buffer_uptodate(bh);
76
77		file_ofs = ((s64)page->index << PAGE_CACHE_SHIFT) +
78				bh_offset(bh);
79		read_lock_irqsave(&ni->size_lock, flags);
80		init_size = ni->initialized_size;
81		i_size = i_size_read(vi);
82		read_unlock_irqrestore(&ni->size_lock, flags);
83		if (unlikely(init_size > i_size)) {
84			/* Race with shrinking truncate. */
85			init_size = i_size;
86		}
87		/* Check for the current buffer head overflowing. */
88		if (unlikely(file_ofs + bh->b_size > init_size)) {
89			int ofs;
90
91			ofs = 0;
92			if (file_ofs < init_size)
93				ofs = init_size - file_ofs;
94			local_irq_save(flags);
95			zero_user_page(page, bh_offset(bh) + ofs,
96					 bh->b_size - ofs, KM_BIO_SRC_IRQ);
97			local_irq_restore(flags);
98		}
99	} else {
100		clear_buffer_uptodate(bh);
101		SetPageError(page);
102		ntfs_error(ni->vol->sb, "Buffer I/O error, logical block "
103				"0x%llx.", (unsigned long long)bh->b_blocknr);
104	}
105	first = page_buffers(page);
106	local_irq_save(flags);
107	bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
108	clear_buffer_async_read(bh);
109	unlock_buffer(bh);
110	tmp = bh;
111	do {
112		if (!buffer_uptodate(tmp))
113			page_uptodate = 0;
114		if (buffer_async_read(tmp)) {
115			if (likely(buffer_locked(tmp)))
116				goto still_busy;
117			/* Async buffers must be locked. */
118			BUG();
119		}
120		tmp = tmp->b_this_page;
121	} while (tmp != bh);
122	bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
123	local_irq_restore(flags);
124	/*
125	 * If none of the buffers had errors then we can set the page uptodate,
126	 * but we first have to perform the post read mst fixups, if the
127	 * attribute is mst protected, i.e. if NInoMstProteced(ni) is true.
128	 * Note we ignore fixup errors as those are detected when
129	 * map_mft_record() is called which gives us per record granularity
130	 * rather than per page granularity.
131	 */
132	if (!NInoMstProtected(ni)) {
133		if (likely(page_uptodate && !PageError(page)))
134			SetPageUptodate(page);
135	} else {
136		u8 *kaddr;
137		unsigned int i, recs;
138		u32 rec_size;
139
140		rec_size = ni->itype.index.block_size;
141		recs = PAGE_CACHE_SIZE / rec_size;
142		/* Should have been verified before we got here... */
143		BUG_ON(!recs);
144		local_irq_save(flags);
145		kaddr = kmap_atomic(page, KM_BIO_SRC_IRQ);
146		for (i = 0; i < recs; i++)
147			post_read_mst_fixup((NTFS_RECORD*)(kaddr +
148					i * rec_size), rec_size);
149		kunmap_atomic(kaddr, KM_BIO_SRC_IRQ);
150		local_irq_restore(flags);
151		flush_dcache_page(page);
152		if (likely(page_uptodate && !PageError(page)))
153			SetPageUptodate(page);
154	}
155	unlock_page(page);
156	return;
157still_busy:
158	bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
159	local_irq_restore(flags);
160	return;
161}
162
163/**
164 * ntfs_read_block - fill a @page of an address space with data
165 * @page:	page cache page to fill with data
166 *
167 * Fill the page @page of the address space belonging to the @page->host inode.
168 * We read each buffer asynchronously and when all buffers are read in, our io
169 * completion handler ntfs_end_buffer_read_async(), if required, automatically
170 * applies the mst fixups to the page before finally marking it uptodate and
171 * unlocking it.
172 *
173 * We only enforce allocated_size limit because i_size is checked for in
174 * generic_file_read().
175 *
176 * Return 0 on success and -errno on error.
177 *
178 * Contains an adapted version of fs/buffer.c::block_read_full_page().
179 */
180static int ntfs_read_block(struct page *page)
181{
182	loff_t i_size;
183	VCN vcn;
184	LCN lcn;
185	s64 init_size;
186	struct inode *vi;
187	ntfs_inode *ni;
188	ntfs_volume *vol;
189	runlist_element *rl;
190	struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
191	sector_t iblock, lblock, zblock;
192	unsigned long flags;
193	unsigned int blocksize, vcn_ofs;
194	int i, nr;
195	unsigned char blocksize_bits;
196
197	vi = page->mapping->host;
198	ni = NTFS_I(vi);
199	vol = ni->vol;
200
201	/* $MFT/$DATA must have its complete runlist in memory at all times. */
202	BUG_ON(!ni->runlist.rl && !ni->mft_no && !NInoAttr(ni));
203
204	blocksize = vol->sb->s_blocksize;
205	blocksize_bits = vol->sb->s_blocksize_bits;
206
207	if (!page_has_buffers(page)) {
208		create_empty_buffers(page, blocksize, 0);
209		if (unlikely(!page_has_buffers(page))) {
210			unlock_page(page);
211			return -ENOMEM;
212		}
213	}
214	bh = head = page_buffers(page);
215	BUG_ON(!bh);
216
217	/*
218	 * We may be racing with truncate.  To avoid some of the problems we
219	 * now take a snapshot of the various sizes and use those for the whole
220	 * of the function.  In case of an extending truncate it just means we
221	 * may leave some buffers unmapped which are now allocated.  This is
222	 * not a problem since these buffers will just get mapped when a write
223	 * occurs.  In case of a shrinking truncate, we will detect this later
224	 * on due to the runlist being incomplete and if the page is being
225	 * fully truncated, truncate will throw it away as soon as we unlock
226	 * it so no need to worry what we do with it.
227	 */
228	iblock = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
229	read_lock_irqsave(&ni->size_lock, flags);
230	lblock = (ni->allocated_size + blocksize - 1) >> blocksize_bits;
231	init_size = ni->initialized_size;
232	i_size = i_size_read(vi);
233	read_unlock_irqrestore(&ni->size_lock, flags);
234	if (unlikely(init_size > i_size)) {
235		/* Race with shrinking truncate. */
236		init_size = i_size;
237	}
238	zblock = (init_size + blocksize - 1) >> blocksize_bits;
239
240	/* Loop through all the buffers in the page. */
241	rl = NULL;
242	nr = i = 0;
243	do {
244		int err = 0;
245
246		if (unlikely(buffer_uptodate(bh)))
247			continue;
248		if (unlikely(buffer_mapped(bh))) {
249			arr[nr++] = bh;
250			continue;
251		}
252		bh->b_bdev = vol->sb->s_bdev;
253		/* Is the block within the allowed limits? */
254		if (iblock < lblock) {
255			bool is_retry = false;
256
257			/* Convert iblock into corresponding vcn and offset. */
258			vcn = (VCN)iblock << blocksize_bits >>
259					vol->cluster_size_bits;
260			vcn_ofs = ((VCN)iblock << blocksize_bits) &
261					vol->cluster_size_mask;
262			if (!rl) {
263lock_retry_remap:
264				down_read(&ni->runlist.lock);
265				rl = ni->runlist.rl;
266			}
267			if (likely(rl != NULL)) {
268				/* Seek to element containing target vcn. */
269				while (rl->length && rl[1].vcn <= vcn)
270					rl++;
271				lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
272			} else
273				lcn = LCN_RL_NOT_MAPPED;
274			/* Successful remap. */
275			if (lcn >= 0) {
276				/* Setup buffer head to correct block. */
277				bh->b_blocknr = ((lcn << vol->cluster_size_bits)
278						+ vcn_ofs) >> blocksize_bits;
279				set_buffer_mapped(bh);
280				/* Only read initialized data blocks. */
281				if (iblock < zblock) {
282					arr[nr++] = bh;
283					continue;
284				}
285				/* Fully non-initialized data block, zero it. */
286				goto handle_zblock;
287			}
288			/* It is a hole, need to zero it. */
289			if (lcn == LCN_HOLE)
290				goto handle_hole;
291			/* If first try and runlist unmapped, map and retry. */
292			if (!is_retry && lcn == LCN_RL_NOT_MAPPED) {
293				is_retry = true;
294				/*
295				 * Attempt to map runlist, dropping lock for
296				 * the duration.
297				 */
298				up_read(&ni->runlist.lock);
299				err = ntfs_map_runlist(ni, vcn);
300				if (likely(!err))
301					goto lock_retry_remap;
302				rl = NULL;
303			} else if (!rl)
304				up_read(&ni->runlist.lock);
305			/*
306			 * If buffer is outside the runlist, treat it as a
307			 * hole.  This can happen due to concurrent truncate
308			 * for example.
309			 */
310			if (err == -ENOENT || lcn == LCN_ENOENT) {
311				err = 0;
312				goto handle_hole;
313			}
314			/* Hard error, zero out region. */
315			if (!err)
316				err = -EIO;
317			bh->b_blocknr = -1;
318			SetPageError(page);
319			ntfs_error(vol->sb, "Failed to read from inode 0x%lx, "
320					"attribute type 0x%x, vcn 0x%llx, "
321					"offset 0x%x because its location on "
322					"disk could not be determined%s "
323					"(error code %i).", ni->mft_no,
324					ni->type, (unsigned long long)vcn,
325					vcn_ofs, is_retry ? " even after "
326					"retrying" : "", err);
327		}
328		/*
329		 * Either iblock was outside lblock limits or
330		 * ntfs_rl_vcn_to_lcn() returned error.  Just zero that portion
331		 * of the page and set the buffer uptodate.
332		 */
333handle_hole:
334		bh->b_blocknr = -1UL;
335		clear_buffer_mapped(bh);
336handle_zblock:
337		zero_user_page(page, i * blocksize, blocksize, KM_USER0);
338		if (likely(!err))
339			set_buffer_uptodate(bh);
340	} while (i++, iblock++, (bh = bh->b_this_page) != head);
341
342	/* Release the lock if we took it. */
343	if (rl)
344		up_read(&ni->runlist.lock);
345
346	/* Check we have at least one buffer ready for i/o. */
347	if (nr) {
348		struct buffer_head *tbh;
349
350		/* Lock the buffers. */
351		for (i = 0; i < nr; i++) {
352			tbh = arr[i];
353			lock_buffer(tbh);
354			tbh->b_end_io = ntfs_end_buffer_async_read;
355			set_buffer_async_read(tbh);
356		}
357		/* Finally, start i/o on the buffers. */
358		for (i = 0; i < nr; i++) {
359			tbh = arr[i];
360			if (likely(!buffer_uptodate(tbh)))
361				submit_bh(READ, tbh);
362			else
363				ntfs_end_buffer_async_read(tbh, 1);
364		}
365		return 0;
366	}
367	/* No i/o was scheduled on any of the buffers. */
368	if (likely(!PageError(page)))
369		SetPageUptodate(page);
370	else /* Signal synchronous i/o error. */
371		nr = -EIO;
372	unlock_page(page);
373	return nr;
374}
375
376/**
377 * ntfs_readpage - fill a @page of a @file with data from the device
378 * @file:	open file to which the page @page belongs or NULL
379 * @page:	page cache page to fill with data
380 *
381 * For non-resident attributes, ntfs_readpage() fills the @page of the open
382 * file @file by calling the ntfs version of the generic block_read_full_page()
383 * function, ntfs_read_block(), which in turn creates and reads in the buffers
384 * associated with the page asynchronously.
385 *
386 * For resident attributes, OTOH, ntfs_readpage() fills @page by copying the
387 * data from the mft record (which at this stage is most likely in memory) and
388 * fills the remainder with zeroes. Thus, in this case, I/O is synchronous, as
389 * even if the mft record is not cached at this point in time, we need to wait
390 * for it to be read in before we can do the copy.
391 *
392 * Return 0 on success and -errno on error.
393 */
394static int ntfs_readpage(struct file *file, struct page *page)
395{
396	loff_t i_size;
397	struct inode *vi;
398	ntfs_inode *ni, *base_ni;
399	u8 *kaddr;
400	ntfs_attr_search_ctx *ctx;
401	MFT_RECORD *mrec;
402	unsigned long flags;
403	u32 attr_len;
404	int err = 0;
405
406retry_readpage:
407	BUG_ON(!PageLocked(page));
408	/*
409	 * This can potentially happen because we clear PageUptodate() during
410	 * ntfs_writepage() of MstProtected() attributes.
411	 */
412	if (PageUptodate(page)) {
413		unlock_page(page);
414		return 0;
415	}
416	vi = page->mapping->host;
417	ni = NTFS_I(vi);
418	/*
419	 * Only $DATA attributes can be encrypted and only unnamed $DATA
420	 * attributes can be compressed.  Index root can have the flags set but
421	 * this means to create compressed/encrypted files, not that the
422	 * attribute is compressed/encrypted.  Note we need to check for
423	 * AT_INDEX_ALLOCATION since this is the type of both directory and
424	 * index inodes.
425	 */
426	if (ni->type != AT_INDEX_ALLOCATION) {
427		/* If attribute is encrypted, deny access, just like NT4. */
428		if (NInoEncrypted(ni)) {
429			BUG_ON(ni->type != AT_DATA);
430			err = -EACCES;
431			goto err_out;
432		}
433		/* Compressed data streams are handled in compress.c. */
434		if (NInoNonResident(ni) && NInoCompressed(ni)) {
435			BUG_ON(ni->type != AT_DATA);
436			BUG_ON(ni->name_len);
437			return ntfs_read_compressed_block(page);
438		}
439	}
440	/* NInoNonResident() == NInoIndexAllocPresent() */
441	if (NInoNonResident(ni)) {
442		/* Normal, non-resident data stream. */
443		return ntfs_read_block(page);
444	}
445	/*
446	 * Attribute is resident, implying it is not compressed or encrypted.
447	 * This also means the attribute is smaller than an mft record and
448	 * hence smaller than a page, so can simply zero out any pages with
449	 * index above 0.  Note the attribute can actually be marked compressed
450	 * but if it is resident the actual data is not compressed so we are
451	 * ok to ignore the compressed flag here.
452	 */
453	if (unlikely(page->index > 0)) {
454		zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0);
455		goto done;
456	}
457	if (!NInoAttr(ni))
458		base_ni = ni;
459	else
460		base_ni = ni->ext.base_ntfs_ino;
461	/* Map, pin, and lock the mft record. */
462	mrec = map_mft_record(base_ni);
463	if (IS_ERR(mrec)) {
464		err = PTR_ERR(mrec);
465		goto err_out;
466	}
467	/*
468	 * If a parallel write made the attribute non-resident, drop the mft
469	 * record and retry the readpage.
470	 */
471	if (unlikely(NInoNonResident(ni))) {
472		unmap_mft_record(base_ni);
473		goto retry_readpage;
474	}
475	ctx = ntfs_attr_get_search_ctx(base_ni, mrec);
476	if (unlikely(!ctx)) {
477		err = -ENOMEM;
478		goto unm_err_out;
479	}
480	err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
481			CASE_SENSITIVE, 0, NULL, 0, ctx);
482	if (unlikely(err))
483		goto put_unm_err_out;
484	attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
485	read_lock_irqsave(&ni->size_lock, flags);
486	if (unlikely(attr_len > ni->initialized_size))
487		attr_len = ni->initialized_size;
488	i_size = i_size_read(vi);
489	read_unlock_irqrestore(&ni->size_lock, flags);
490	if (unlikely(attr_len > i_size)) {
491		/* Race with shrinking truncate. */
492		attr_len = i_size;
493	}
494	kaddr = kmap_atomic(page, KM_USER0);
495	/* Copy the data to the page. */
496	memcpy(kaddr, (u8*)ctx->attr +
497			le16_to_cpu(ctx->attr->data.resident.value_offset),
498			attr_len);
499	/* Zero the remainder of the page. */
500	memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
501	flush_dcache_page(page);
502	kunmap_atomic(kaddr, KM_USER0);
503put_unm_err_out:
504	ntfs_attr_put_search_ctx(ctx);
505unm_err_out:
506	unmap_mft_record(base_ni);
507done:
508	SetPageUptodate(page);
509err_out:
510	unlock_page(page);
511	return err;
512}
513
514#ifdef NTFS_RW
515
516/**
517 * ntfs_write_block - write a @page to the backing store
518 * @page:	page cache page to write out
519 * @wbc:	writeback control structure
520 *
521 * This function is for writing pages belonging to non-resident, non-mst
522 * protected attributes to their backing store.
523 *
524 * For a page with buffers, map and write the dirty buffers asynchronously
525 * under page writeback. For a page without buffers, create buffers for the
526 * page, then proceed as above.
527 *
528 * If a page doesn't have buffers the page dirty state is definitive. If a page
529 * does have buffers, the page dirty state is just a hint, and the buffer dirty
530 * state is definitive. (A hint which has rules: dirty buffers against a clean
531 * page is illegal. Other combinations are legal and need to be handled. In
532 * particular a dirty page containing clean buffers for example.)
533 *
534 * Return 0 on success and -errno on error.
535 *
536 * Based on ntfs_read_block() and __block_write_full_page().
537 */
538static int ntfs_write_block(struct page *page, struct writeback_control *wbc)
539{
540	VCN vcn;
541	LCN lcn;
542	s64 initialized_size;
543	loff_t i_size;
544	sector_t block, dblock, iblock;
545	struct inode *vi;
546	ntfs_inode *ni;
547	ntfs_volume *vol;
548	runlist_element *rl;
549	struct buffer_head *bh, *head;
550	unsigned long flags;
551	unsigned int blocksize, vcn_ofs;
552	int err;
553	bool need_end_writeback;
554	unsigned char blocksize_bits;
555
556	vi = page->mapping->host;
557	ni = NTFS_I(vi);
558	vol = ni->vol;
559
560	ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
561			"0x%lx.", ni->mft_no, ni->type, page->index);
562
563	BUG_ON(!NInoNonResident(ni));
564	BUG_ON(NInoMstProtected(ni));
565	blocksize = vol->sb->s_blocksize;
566	blocksize_bits = vol->sb->s_blocksize_bits;
567	if (!page_has_buffers(page)) {
568		BUG_ON(!PageUptodate(page));
569		create_empty_buffers(page, blocksize,
570				(1 << BH_Uptodate) | (1 << BH_Dirty));
571		if (unlikely(!page_has_buffers(page))) {
572			ntfs_warning(vol->sb, "Error allocating page "
573					"buffers.  Redirtying page so we try "
574					"again later.");
575			/*
576			 * Put the page back on mapping->dirty_pages, but leave
577			 * its buffers' dirty state as-is.
578			 */
579			redirty_page_for_writepage(wbc, page);
580			unlock_page(page);
581			return 0;
582		}
583	}
584	bh = head = page_buffers(page);
585	BUG_ON(!bh);
586
587	/* NOTE: Different naming scheme to ntfs_read_block()! */
588
589	/* The first block in the page. */
590	block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
591
592	read_lock_irqsave(&ni->size_lock, flags);
593	i_size = i_size_read(vi);
594	initialized_size = ni->initialized_size;
595	read_unlock_irqrestore(&ni->size_lock, flags);
596
597	/* The first out of bounds block for the data size. */
598	dblock = (i_size + blocksize - 1) >> blocksize_bits;
599
600	/* The last (fully or partially) initialized block. */
601	iblock = initialized_size >> blocksize_bits;
602
603	/*
604	 * Be very careful.  We have no exclusion from __set_page_dirty_buffers
605	 * here, and the (potentially unmapped) buffers may become dirty at
606	 * any time.  If a buffer becomes dirty here after we've inspected it
607	 * then we just miss that fact, and the page stays dirty.
608	 *
609	 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
610	 * handle that here by just cleaning them.
611	 */
612
613	/*
614	 * Loop through all the buffers in the page, mapping all the dirty
615	 * buffers to disk addresses and handling any aliases from the
616	 * underlying block device's mapping.
617	 */
618	rl = NULL;
619	err = 0;
620	do {
621		bool is_retry = false;
622
623		if (unlikely(block >= dblock)) {
624			clear_buffer_dirty(bh);
625			set_buffer_uptodate(bh);
626			continue;
627		}
628
629		/* Clean buffers are not written out, so no need to map them. */
630		if (!buffer_dirty(bh))
631			continue;
632
633		/* Make sure we have enough initialized size. */
634		if (unlikely((block >= iblock) &&
635				(initialized_size < i_size))) {
636			/*
637			 * If this page is fully outside initialized size, zero
638			 * out all pages between the current initialized size
639			 * and the current page. Just use ntfs_readpage() to do
640			 * the zeroing transparently.
641			 */
642			if (block > iblock) {
643				// TODO:
644				// For each page do:
645				// - read_cache_page()
646				// Again for each page do:
647				// - wait_on_page_locked()
648				// - Check (PageUptodate(page) &&
649				//			!PageError(page))
650				// Update initialized size in the attribute and
651				// in the inode.
652				// Again, for each page do:
653				//	__set_page_dirty_buffers();
654				// page_cache_release()
655				// We don't need to wait on the writes.
656				// Update iblock.
657			}
658			if (!PageUptodate(page)) {
659				// TODO:
660				// Zero any non-uptodate buffers up to i_size.
661				// Set them uptodate and dirty.
662			}
663			// TODO:
664			// Update initialized size in the attribute and in the
665			// inode (up to i_size).
666			// Update iblock.
667			// size changes to happen in one go.
668			ntfs_error(vol->sb, "Writing beyond initialized size "
669					"is not supported yet. Sorry.");
670			err = -EOPNOTSUPP;
671			break;
672			// Do NOT set_buffer_new() BUT DO clear buffer range
673			// outside write request range.
674			// set_buffer_uptodate() on complete buffers as well as
675			// set_buffer_dirty().
676		}
677
678		/* No need to map buffers that are already mapped. */
679		if (buffer_mapped(bh))
680			continue;
681
682		/* Unmapped, dirty buffer. Need to map it. */
683		bh->b_bdev = vol->sb->s_bdev;
684
685		/* Convert block into corresponding vcn and offset. */
686		vcn = (VCN)block << blocksize_bits;
687		vcn_ofs = vcn & vol->cluster_size_mask;
688		vcn >>= vol->cluster_size_bits;
689		if (!rl) {
690lock_retry_remap:
691			down_read(&ni->runlist.lock);
692			rl = ni->runlist.rl;
693		}
694		if (likely(rl != NULL)) {
695			/* Seek to element containing target vcn. */
696			while (rl->length && rl[1].vcn <= vcn)
697				rl++;
698			lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
699		} else
700			lcn = LCN_RL_NOT_MAPPED;
701		/* Successful remap. */
702		if (lcn >= 0) {
703			/* Setup buffer head to point to correct block. */
704			bh->b_blocknr = ((lcn << vol->cluster_size_bits) +
705					vcn_ofs) >> blocksize_bits;
706			set_buffer_mapped(bh);
707			continue;
708		}
709		/* It is a hole, need to instantiate it. */
710		if (lcn == LCN_HOLE) {
711			u8 *kaddr;
712			unsigned long *bpos, *bend;
713
714			/* Check if the buffer is zero. */
715			kaddr = kmap_atomic(page, KM_USER0);
716			bpos = (unsigned long *)(kaddr + bh_offset(bh));
717			bend = (unsigned long *)((u8*)bpos + blocksize);
718			do {
719				if (unlikely(*bpos))
720					break;
721			} while (likely(++bpos < bend));
722			kunmap_atomic(kaddr, KM_USER0);
723			if (bpos == bend) {
724				/*
725				 * Buffer is zero and sparse, no need to write
726				 * it.
727				 */
728				bh->b_blocknr = -1;
729				clear_buffer_dirty(bh);
730				continue;
731			}
732			// TODO: Instantiate the hole.
733			// clear_buffer_new(bh);
734			// unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
735			ntfs_error(vol->sb, "Writing into sparse regions is "
736					"not supported yet. Sorry.");
737			err = -EOPNOTSUPP;
738			break;
739		}
740		/* If first try and runlist unmapped, map and retry. */
741		if (!is_retry && lcn == LCN_RL_NOT_MAPPED) {
742			is_retry = true;
743			/*
744			 * Attempt to map runlist, dropping lock for
745			 * the duration.
746			 */
747			up_read(&ni->runlist.lock);
748			err = ntfs_map_runlist(ni, vcn);
749			if (likely(!err))
750				goto lock_retry_remap;
751			rl = NULL;
752		} else if (!rl)
753			up_read(&ni->runlist.lock);
754		/*
755		 * If buffer is outside the runlist, truncate has cut it out
756		 * of the runlist.  Just clean and clear the buffer and set it
757		 * uptodate so it can get discarded by the VM.
758		 */
759		if (err == -ENOENT || lcn == LCN_ENOENT) {
760			bh->b_blocknr = -1;
761			clear_buffer_dirty(bh);
762			zero_user_page(page, bh_offset(bh), blocksize,
763					KM_USER0);
764			set_buffer_uptodate(bh);
765			err = 0;
766			continue;
767		}
768		/* Failed to map the buffer, even after retrying. */
769		if (!err)
770			err = -EIO;
771		bh->b_blocknr = -1;
772		ntfs_error(vol->sb, "Failed to write to inode 0x%lx, "
773				"attribute type 0x%x, vcn 0x%llx, offset 0x%x "
774				"because its location on disk could not be "
775				"determined%s (error code %i).", ni->mft_no,
776				ni->type, (unsigned long long)vcn,
777				vcn_ofs, is_retry ? " even after "
778				"retrying" : "", err);
779		break;
780	} while (block++, (bh = bh->b_this_page) != head);
781
782	/* Release the lock if we took it. */
783	if (rl)
784		up_read(&ni->runlist.lock);
785
786	/* For the error case, need to reset bh to the beginning. */
787	bh = head;
788
789	/* Just an optimization, so ->readpage() is not called later. */
790	if (unlikely(!PageUptodate(page))) {
791		int uptodate = 1;
792		do {
793			if (!buffer_uptodate(bh)) {
794				uptodate = 0;
795				bh = head;
796				break;
797			}
798		} while ((bh = bh->b_this_page) != head);
799		if (uptodate)
800			SetPageUptodate(page);
801	}
802
803	/* Setup all mapped, dirty buffers for async write i/o. */
804	do {
805		if (buffer_mapped(bh) && buffer_dirty(bh)) {
806			lock_buffer(bh);
807			if (test_clear_buffer_dirty(bh)) {
808				BUG_ON(!buffer_uptodate(bh));
809				mark_buffer_async_write(bh);
810			} else
811				unlock_buffer(bh);
812		} else if (unlikely(err)) {
813			/*
814			 * For the error case. The buffer may have been set
815			 * dirty during attachment to a dirty page.
816			 */
817			if (err != -ENOMEM)
818				clear_buffer_dirty(bh);
819		}
820	} while ((bh = bh->b_this_page) != head);
821
822	if (unlikely(err)) {
823		// TODO: Remove the -EOPNOTSUPP check later on...
824		if (unlikely(err == -EOPNOTSUPP))
825			err = 0;
826		else if (err == -ENOMEM) {
827			ntfs_warning(vol->sb, "Error allocating memory. "
828					"Redirtying page so we try again "
829					"later.");
830			/*
831			 * Put the page back on mapping->dirty_pages, but
832			 * leave its buffer's dirty state as-is.
833			 */
834			redirty_page_for_writepage(wbc, page);
835			err = 0;
836		} else
837			SetPageError(page);
838	}
839
840	BUG_ON(PageWriteback(page));
841	set_page_writeback(page);	/* Keeps try_to_free_buffers() away. */
842
843	/* Submit the prepared buffers for i/o. */
844	need_end_writeback = true;
845	do {
846		struct buffer_head *next = bh->b_this_page;
847		if (buffer_async_write(bh)) {
848			submit_bh(WRITE, bh);
849			need_end_writeback = false;
850		}
851		bh = next;
852	} while (bh != head);
853	unlock_page(page);
854
855	/* If no i/o was started, need to end_page_writeback(). */
856	if (unlikely(need_end_writeback))
857		end_page_writeback(page);
858
859	ntfs_debug("Done.");
860	return err;
861}
862
863/**
864 * ntfs_write_mst_block - write a @page to the backing store
865 * @page:	page cache page to write out
866 * @wbc:	writeback control structure
867 *
868 * This function is for writing pages belonging to non-resident, mst protected
869 * attributes to their backing store.  The only supported attributes are index
870 * allocation and $MFT/$DATA.  Both directory inodes and index inodes are
871 * supported for the index allocation case.
872 *
873 * The page must remain locked for the duration of the write because we apply
874 * the mst fixups, write, and then undo the fixups, so if we were to unlock the
875 * page before undoing the fixups, any other user of the page will see the
876 * page contents as corrupt.
877 *
878 * We clear the page uptodate flag for the duration of the function to ensure
879 * exclusion for the $MFT/$DATA case against someone mapping an mft record we
880 * are about to apply the mst fixups to.
881 *
882 * Return 0 on success and -errno on error.
883 *
884 * Based on ntfs_write_block(), ntfs_mft_writepage(), and
885 * write_mft_record_nolock().
886 */
887static int ntfs_write_mst_block(struct page *page,
888		struct writeback_control *wbc)
889{
890	sector_t block, dblock, rec_block;
891	struct inode *vi = page->mapping->host;
892	ntfs_inode *ni = NTFS_I(vi);
893	ntfs_volume *vol = ni->vol;
894	u8 *kaddr;
895	unsigned int rec_size = ni->itype.index.block_size;
896	ntfs_inode *locked_nis[PAGE_CACHE_SIZE / rec_size];
897	struct buffer_head *bh, *head, *tbh, *rec_start_bh;
898	struct buffer_head *bhs[MAX_BUF_PER_PAGE];
899	runlist_element *rl;
900	int i, nr_locked_nis, nr_recs, nr_bhs, max_bhs, bhs_per_rec, err, err2;
901	unsigned bh_size, rec_size_bits;
902	bool sync, is_mft, page_is_dirty, rec_is_dirty;
903	unsigned char bh_size_bits;
904
905	ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
906			"0x%lx.", vi->i_ino, ni->type, page->index);
907	BUG_ON(!NInoNonResident(ni));
908	BUG_ON(!NInoMstProtected(ni));
909	is_mft = (S_ISREG(vi->i_mode) && !vi->i_ino);
910	/*
911	 * NOTE: ntfs_write_mst_block() would be called for $MFTMirr if a page
912	 * in its page cache were to be marked dirty.  However this should
913	 * never happen with the current driver and considering we do not
914	 * handle this case here we do want to BUG(), at least for now.
915	 */
916	BUG_ON(!(is_mft || S_ISDIR(vi->i_mode) ||
917			(NInoAttr(ni) && ni->type == AT_INDEX_ALLOCATION)));
918	bh_size = vol->sb->s_blocksize;
919	bh_size_bits = vol->sb->s_blocksize_bits;
920	max_bhs = PAGE_CACHE_SIZE / bh_size;
921	BUG_ON(!max_bhs);
922	BUG_ON(max_bhs > MAX_BUF_PER_PAGE);
923
924	/* Were we called for sync purposes? */
925	sync = (wbc->sync_mode == WB_SYNC_ALL);
926
927	/* Make sure we have mapped buffers. */
928	bh = head = page_buffers(page);
929	BUG_ON(!bh);
930
931	rec_size_bits = ni->itype.index.block_size_bits;
932	BUG_ON(!(PAGE_CACHE_SIZE >> rec_size_bits));
933	bhs_per_rec = rec_size >> bh_size_bits;
934	BUG_ON(!bhs_per_rec);
935
936	/* The first block in the page. */
937	rec_block = block = (sector_t)page->index <<
938			(PAGE_CACHE_SHIFT - bh_size_bits);
939
940	/* The first out of bounds block for the data size. */
941	dblock = (i_size_read(vi) + bh_size - 1) >> bh_size_bits;
942
943	rl = NULL;
944	err = err2 = nr_bhs = nr_recs = nr_locked_nis = 0;
945	page_is_dirty = rec_is_dirty = false;
946	rec_start_bh = NULL;
947	do {
948		bool is_retry = false;
949
950		if (likely(block < rec_block)) {
951			if (unlikely(block >= dblock)) {
952				clear_buffer_dirty(bh);
953				set_buffer_uptodate(bh);
954				continue;
955			}
956			/*
957			 * This block is not the first one in the record.  We
958			 * ignore the buffer's dirty state because we could
959			 * have raced with a parallel mark_ntfs_record_dirty().
960			 */
961			if (!rec_is_dirty)
962				continue;
963			if (unlikely(err2)) {
964				if (err2 != -ENOMEM)
965					clear_buffer_dirty(bh);
966				continue;
967			}
968		} else /* if (block == rec_block) */ {
969			BUG_ON(block > rec_block);
970			/* This block is the first one in the record. */
971			rec_block += bhs_per_rec;
972			err2 = 0;
973			if (unlikely(block >= dblock)) {
974				clear_buffer_dirty(bh);
975				continue;
976			}
977			if (!buffer_dirty(bh)) {
978				/* Clean records are not written out. */
979				rec_is_dirty = false;
980				continue;
981			}
982			rec_is_dirty = true;
983			rec_start_bh = bh;
984		}
985		/* Need to map the buffer if it is not mapped already. */
986		if (unlikely(!buffer_mapped(bh))) {
987			VCN vcn;
988			LCN lcn;
989			unsigned int vcn_ofs;
990
991			bh->b_bdev = vol->sb->s_bdev;
992			/* Obtain the vcn and offset of the current block. */
993			vcn = (VCN)block << bh_size_bits;
994			vcn_ofs = vcn & vol->cluster_size_mask;
995			vcn >>= vol->cluster_size_bits;
996			if (!rl) {
997lock_retry_remap:
998				down_read(&ni->runlist.lock);
999				rl = ni->runlist.rl;
1000			}
1001			if (likely(rl != NULL)) {
1002				/* Seek to element containing target vcn. */
1003				while (rl->length && rl[1].vcn <= vcn)
1004					rl++;
1005				lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
1006			} else
1007				lcn = LCN_RL_NOT_MAPPED;
1008			/* Successful remap. */
1009			if (likely(lcn >= 0)) {
1010				/* Setup buffer head to correct block. */
1011				bh->b_blocknr = ((lcn <<
1012						vol->cluster_size_bits) +
1013						vcn_ofs) >> bh_size_bits;
1014				set_buffer_mapped(bh);
1015			} else {
1016				/*
1017				 * Remap failed.  Retry to map the runlist once
1018				 * unless we are working on $MFT which always
1019				 * has the whole of its runlist in memory.
1020				 */
1021				if (!is_mft && !is_retry &&
1022						lcn == LCN_RL_NOT_MAPPED) {
1023					is_retry = true;
1024					/*
1025					 * Attempt to map runlist, dropping
1026					 * lock for the duration.
1027					 */
1028					up_read(&ni->runlist.lock);
1029					err2 = ntfs_map_runlist(ni, vcn);
1030					if (likely(!err2))
1031						goto lock_retry_remap;
1032					if (err2 == -ENOMEM)
1033						page_is_dirty = true;
1034					lcn = err2;
1035				} else {
1036					err2 = -EIO;
1037					if (!rl)
1038						up_read(&ni->runlist.lock);
1039				}
1040				/* Hard error.  Abort writing this record. */
1041				if (!err || err == -ENOMEM)
1042					err = err2;
1043				bh->b_blocknr = -1;
1044				ntfs_error(vol->sb, "Cannot write ntfs record "
1045						"0x%llx (inode 0x%lx, "
1046						"attribute type 0x%x) because "
1047						"its location on disk could "
1048						"not be determined (error "
1049						"code %lli).",
1050						(long long)block <<
1051						bh_size_bits >>
1052						vol->mft_record_size_bits,
1053						ni->mft_no, ni->type,
1054						(long long)lcn);
1055				/*
1056				 * If this is not the first buffer, remove the
1057				 * buffers in this record from the list of
1058				 * buffers to write and clear their dirty bit
1059				 * if not error -ENOMEM.
1060				 */
1061				if (rec_start_bh != bh) {
1062					while (bhs[--nr_bhs] != rec_start_bh)
1063						;
1064					if (err2 != -ENOMEM) {
1065						do {
1066							clear_buffer_dirty(
1067								rec_start_bh);
1068						} while ((rec_start_bh =
1069								rec_start_bh->
1070								b_this_page) !=
1071								bh);
1072					}
1073				}
1074				continue;
1075			}
1076		}
1077		BUG_ON(!buffer_uptodate(bh));
1078		BUG_ON(nr_bhs >= max_bhs);
1079		bhs[nr_bhs++] = bh;
1080	} while (block++, (bh = bh->b_this_page) != head);
1081	if (unlikely(rl))
1082		up_read(&ni->runlist.lock);
1083	/* If there were no dirty buffers, we are done. */
1084	if (!nr_bhs)
1085		goto done;
1086	/* Map the page so we can access its contents. */
1087	kaddr = kmap(page);
1088	/* Clear the page uptodate flag whilst the mst fixups are applied. */
1089	BUG_ON(!PageUptodate(page));
1090	ClearPageUptodate(page);
1091	for (i = 0; i < nr_bhs; i++) {
1092		unsigned int ofs;
1093
1094		/* Skip buffers which are not at the beginning of records. */
1095		if (i % bhs_per_rec)
1096			continue;
1097		tbh = bhs[i];
1098		ofs = bh_offset(tbh);
1099		if (is_mft) {
1100			ntfs_inode *tni;
1101			unsigned long mft_no;
1102
1103			/* Get the mft record number. */
1104			mft_no = (((s64)page->index << PAGE_CACHE_SHIFT) + ofs)
1105					>> rec_size_bits;
1106			/* Check whether to write this mft record. */
1107			tni = NULL;
1108			if (!ntfs_may_write_mft_record(vol, mft_no,
1109					(MFT_RECORD*)(kaddr + ofs), &tni)) {
1110				/*
1111				 * The record should not be written.  This
1112				 * means we need to redirty the page before
1113				 * returning.
1114				 */
1115				page_is_dirty = true;
1116				/*
1117				 * Remove the buffers in this mft record from
1118				 * the list of buffers to write.
1119				 */
1120				do {
1121					bhs[i] = NULL;
1122				} while (++i % bhs_per_rec);
1123				continue;
1124			}
1125			/*
1126			 * The record should be written.  If a locked ntfs
1127			 * inode was returned, add it to the array of locked
1128			 * ntfs inodes.
1129			 */
1130			if (tni)
1131				locked_nis[nr_locked_nis++] = tni;
1132		}
1133		/* Apply the mst protection fixups. */
1134		err2 = pre_write_mst_fixup((NTFS_RECORD*)(kaddr + ofs),
1135				rec_size);
1136		if (unlikely(err2)) {
1137			if (!err || err == -ENOMEM)
1138				err = -EIO;
1139			ntfs_error(vol->sb, "Failed to apply mst fixups "
1140					"(inode 0x%lx, attribute type 0x%x, "
1141					"page index 0x%lx, page offset 0x%x)!"
1142					"  Unmount and run chkdsk.", vi->i_ino,
1143					ni->type, page->index, ofs);
1144			/*
1145			 * Mark all the buffers in this record clean as we do
1146			 * not want to write corrupt data to disk.
1147			 */
1148			do {
1149				clear_buffer_dirty(bhs[i]);
1150				bhs[i] = NULL;
1151			} while (++i % bhs_per_rec);
1152			continue;
1153		}
1154		nr_recs++;
1155	}
1156	/* If no records are to be written out, we are done. */
1157	if (!nr_recs)
1158		goto unm_done;
1159	flush_dcache_page(page);
1160	/* Lock buffers and start synchronous write i/o on them. */
1161	for (i = 0; i < nr_bhs; i++) {
1162		tbh = bhs[i];
1163		if (!tbh)
1164			continue;
1165		if (unlikely(test_set_buffer_locked(tbh)))
1166			BUG();
1167		/* The buffer dirty state is now irrelevant, just clean it. */
1168		clear_buffer_dirty(tbh);
1169		BUG_ON(!buffer_uptodate(tbh));
1170		BUG_ON(!buffer_mapped(tbh));
1171		get_bh(tbh);
1172		tbh->b_end_io = end_buffer_write_sync;
1173		submit_bh(WRITE, tbh);
1174	}
1175	/* Synchronize the mft mirror now if not @sync. */
1176	if (is_mft && !sync)
1177		goto do_mirror;
1178do_wait:
1179	/* Wait on i/o completion of buffers. */
1180	for (i = 0; i < nr_bhs; i++) {
1181		tbh = bhs[i];
1182		if (!tbh)
1183			continue;
1184		wait_on_buffer(tbh);
1185		if (unlikely(!buffer_uptodate(tbh))) {
1186			ntfs_error(vol->sb, "I/O error while writing ntfs "
1187					"record buffer (inode 0x%lx, "
1188					"attribute type 0x%x, page index "
1189					"0x%lx, page offset 0x%lx)!  Unmount "
1190					"and run chkdsk.", vi->i_ino, ni->type,
1191					page->index, bh_offset(tbh));
1192			if (!err || err == -ENOMEM)
1193				err = -EIO;
1194			/*
1195			 * Set the buffer uptodate so the page and buffer
1196			 * states do not become out of sync.
1197			 */
1198			set_buffer_uptodate(tbh);
1199		}
1200	}
1201	/* If @sync, now synchronize the mft mirror. */
1202	if (is_mft && sync) {
1203do_mirror:
1204		for (i = 0; i < nr_bhs; i++) {
1205			unsigned long mft_no;
1206			unsigned int ofs;
1207
1208			/*
1209			 * Skip buffers which are not at the beginning of
1210			 * records.
1211			 */
1212			if (i % bhs_per_rec)
1213				continue;
1214			tbh = bhs[i];
1215			/* Skip removed buffers (and hence records). */
1216			if (!tbh)
1217				continue;
1218			ofs = bh_offset(tbh);
1219			/* Get the mft record number. */
1220			mft_no = (((s64)page->index << PAGE_CACHE_SHIFT) + ofs)
1221					>> rec_size_bits;
1222			if (mft_no < vol->mftmirr_size)
1223				ntfs_sync_mft_mirror(vol, mft_no,
1224						(MFT_RECORD*)(kaddr + ofs),
1225						sync);
1226		}
1227		if (!sync)
1228			goto do_wait;
1229	}
1230	/* Remove the mst protection fixups again. */
1231	for (i = 0; i < nr_bhs; i++) {
1232		if (!(i % bhs_per_rec)) {
1233			tbh = bhs[i];
1234			if (!tbh)
1235				continue;
1236			post_write_mst_fixup((NTFS_RECORD*)(kaddr +
1237					bh_offset(tbh)));
1238		}
1239	}
1240	flush_dcache_page(page);
1241unm_done:
1242	/* Unlock any locked inodes. */
1243	while (nr_locked_nis-- > 0) {
1244		ntfs_inode *tni, *base_tni;
1245
1246		tni = locked_nis[nr_locked_nis];
1247		/* Get the base inode. */
1248		mutex_lock(&tni->extent_lock);
1249		if (tni->nr_extents >= 0)
1250			base_tni = tni;
1251		else {
1252			base_tni = tni->ext.base_ntfs_ino;
1253			BUG_ON(!base_tni);
1254		}
1255		mutex_unlock(&tni->extent_lock);
1256		ntfs_debug("Unlocking %s inode 0x%lx.",
1257				tni == base_tni ? "base" : "extent",
1258				tni->mft_no);
1259		mutex_unlock(&tni->mrec_lock);
1260		atomic_dec(&tni->count);
1261		iput(VFS_I(base_tni));
1262	}
1263	SetPageUptodate(page);
1264	kunmap(page);
1265done:
1266	if (unlikely(err && err != -ENOMEM)) {
1267		/*
1268		 * Set page error if there is only one ntfs record in the page.
1269		 * Otherwise we would loose per-record granularity.
1270		 */
1271		if (ni->itype.index.block_size == PAGE_CACHE_SIZE)
1272			SetPageError(page);
1273		NVolSetErrors(vol);
1274	}
1275	if (page_is_dirty) {
1276		ntfs_debug("Page still contains one or more dirty ntfs "
1277				"records.  Redirtying the page starting at "
1278				"record 0x%lx.", page->index <<
1279				(PAGE_CACHE_SHIFT - rec_size_bits));
1280		redirty_page_for_writepage(wbc, page);
1281		unlock_page(page);
1282	} else {
1283		/*
1284		 * Keep the VM happy.  This must be done otherwise the
1285		 * radix-tree tag PAGECACHE_TAG_DIRTY remains set even though
1286		 * the page is clean.
1287		 */
1288		BUG_ON(PageWriteback(page));
1289		set_page_writeback(page);
1290		unlock_page(page);
1291		end_page_writeback(page);
1292	}
1293	if (likely(!err))
1294		ntfs_debug("Done.");
1295	return err;
1296}
1297
1298/**
1299 * ntfs_writepage - write a @page to the backing store
1300 * @page:	page cache page to write out
1301 * @wbc:	writeback control structure
1302 *
1303 * This is called from the VM when it wants to have a dirty ntfs page cache
1304 * page cleaned.  The VM has already locked the page and marked it clean.
1305 *
1306 * For non-resident attributes, ntfs_writepage() writes the @page by calling
1307 * the ntfs version of the generic block_write_full_page() function,
1308 * ntfs_write_block(), which in turn if necessary creates and writes the
1309 * buffers associated with the page asynchronously.
1310 *
1311 * For resident attributes, OTOH, ntfs_writepage() writes the @page by copying
1312 * the data to the mft record (which at this stage is most likely in memory).
1313 * The mft record is then marked dirty and written out asynchronously via the
1314 * vfs inode dirty code path for the inode the mft record belongs to or via the
1315 * vm page dirty code path for the page the mft record is in.
1316 *
1317 * Based on ntfs_readpage() and fs/buffer.c::block_write_full_page().
1318 *
1319 * Return 0 on success and -errno on error.
1320 */
1321static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
1322{
1323	loff_t i_size;
1324	struct inode *vi = page->mapping->host;
1325	ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi);
1326	char *kaddr;
1327	ntfs_attr_search_ctx *ctx = NULL;
1328	MFT_RECORD *m = NULL;
1329	u32 attr_len;
1330	int err;
1331
1332retry_writepage:
1333	BUG_ON(!PageLocked(page));
1334	i_size = i_size_read(vi);
1335	/* Is the page fully outside i_size? (truncate in progress) */
1336	if (unlikely(page->index >= (i_size + PAGE_CACHE_SIZE - 1) >>
1337			PAGE_CACHE_SHIFT)) {
1338		/*
1339		 * The page may have dirty, unmapped buffers.  Make them
1340		 * freeable here, so the page does not leak.
1341		 */
1342		block_invalidatepage(page, 0);
1343		unlock_page(page);
1344		ntfs_debug("Write outside i_size - truncated?");
1345		return 0;
1346	}
1347	/*
1348	 * Only $DATA attributes can be encrypted and only unnamed $DATA
1349	 * attributes can be compressed.  Index root can have the flags set but
1350	 * this means to create compressed/encrypted files, not that the
1351	 * attribute is compressed/encrypted.  Note we need to check for
1352	 * AT_INDEX_ALLOCATION since this is the type of both directory and
1353	 * index inodes.
1354	 */
1355	if (ni->type != AT_INDEX_ALLOCATION) {
1356		/* If file is encrypted, deny access, just like NT4. */
1357		if (NInoEncrypted(ni)) {
1358			unlock_page(page);
1359			BUG_ON(ni->type != AT_DATA);
1360			ntfs_debug("Denying write access to encrypted file.");
1361			return -EACCES;
1362		}
1363		/* Compressed data streams are handled in compress.c. */
1364		if (NInoNonResident(ni) && NInoCompressed(ni)) {
1365			BUG_ON(ni->type != AT_DATA);
1366			BUG_ON(ni->name_len);
1367			// TODO: Implement and replace this with
1368			// return ntfs_write_compressed_block(page);
1369			unlock_page(page);
1370			ntfs_error(vi->i_sb, "Writing to compressed files is "
1371					"not supported yet.  Sorry.");
1372			return -EOPNOTSUPP;
1373		}
1374		// TODO: Implement and remove this check.
1375		if (NInoNonResident(ni) && NInoSparse(ni)) {
1376			unlock_page(page);
1377			ntfs_error(vi->i_sb, "Writing to sparse files is not "
1378					"supported yet.  Sorry.");
1379			return -EOPNOTSUPP;
1380		}
1381	}
1382	/* NInoNonResident() == NInoIndexAllocPresent() */
1383	if (NInoNonResident(ni)) {
1384		/* We have to zero every time due to mmap-at-end-of-file. */
1385		if (page->index >= (i_size >> PAGE_CACHE_SHIFT)) {
1386			/* The page straddles i_size. */
1387			unsigned int ofs = i_size & ~PAGE_CACHE_MASK;
1388			zero_user_page(page, ofs, PAGE_CACHE_SIZE - ofs,
1389					KM_USER0);
1390		}
1391		/* Handle mst protected attributes. */
1392		if (NInoMstProtected(ni))
1393			return ntfs_write_mst_block(page, wbc);
1394		/* Normal, non-resident data stream. */
1395		return ntfs_write_block(page, wbc);
1396	}
1397	/*
1398	 * Attribute is resident, implying it is not compressed, encrypted, or
1399	 * mst protected.  This also means the attribute is smaller than an mft
1400	 * record and hence smaller than a page, so can simply return error on
1401	 * any pages with index above 0.  Note the attribute can actually be
1402	 * marked compressed but if it is resident the actual data is not
1403	 * compressed so we are ok to ignore the compressed flag here.
1404	 */
1405	BUG_ON(page_has_buffers(page));
1406	BUG_ON(!PageUptodate(page));
1407	if (unlikely(page->index > 0)) {
1408		ntfs_error(vi->i_sb, "BUG()! page->index (0x%lx) > 0.  "
1409				"Aborting write.", page->index);
1410		BUG_ON(PageWriteback(page));
1411		set_page_writeback(page);
1412		unlock_page(page);
1413		end_page_writeback(page);
1414		return -EIO;
1415	}
1416	if (!NInoAttr(ni))
1417		base_ni = ni;
1418	else
1419		base_ni = ni->ext.base_ntfs_ino;
1420	/* Map, pin, and lock the mft record. */
1421	m = map_mft_record(base_ni);
1422	if (IS_ERR(m)) {
1423		err = PTR_ERR(m);
1424		m = NULL;
1425		ctx = NULL;
1426		goto err_out;
1427	}
1428	/*
1429	 * If a parallel write made the attribute non-resident, drop the mft
1430	 * record and retry the writepage.
1431	 */
1432	if (unlikely(NInoNonResident(ni))) {
1433		unmap_mft_record(base_ni);
1434		goto retry_writepage;
1435	}
1436	ctx = ntfs_attr_get_search_ctx(base_ni, m);
1437	if (unlikely(!ctx)) {
1438		err = -ENOMEM;
1439		goto err_out;
1440	}
1441	err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
1442			CASE_SENSITIVE, 0, NULL, 0, ctx);
1443	if (unlikely(err))
1444		goto err_out;
1445	/*
1446	 * Keep the VM happy.  This must be done otherwise the radix-tree tag
1447	 * PAGECACHE_TAG_DIRTY remains set even though the page is clean.
1448	 */
1449	BUG_ON(PageWriteback(page));
1450	set_page_writeback(page);
1451	unlock_page(page);
1452	attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
1453	i_size = i_size_read(vi);
1454	if (unlikely(attr_len > i_size)) {
1455		/* Race with shrinking truncate or a failed truncate. */
1456		attr_len = i_size;
1457		/*
1458		 * If the truncate failed, fix it up now.  If a concurrent
1459		 * truncate, we do its job, so it does not have to do anything.
1460		 */
1461		err = ntfs_resident_attr_value_resize(ctx->mrec, ctx->attr,
1462				attr_len);
1463		/* Shrinking cannot fail. */
1464		BUG_ON(err);
1465	}
1466	kaddr = kmap_atomic(page, KM_USER0);
1467	/* Copy the data from the page to the mft record. */
1468	memcpy((u8*)ctx->attr +
1469			le16_to_cpu(ctx->attr->data.resident.value_offset),
1470			kaddr, attr_len);
1471	/* Zero out of bounds area in the page cache page. */
1472	memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
1473	kunmap_atomic(kaddr, KM_USER0);
1474	flush_dcache_page(page);
1475	flush_dcache_mft_record_page(ctx->ntfs_ino);
1476	/* We are done with the page. */
1477	end_page_writeback(page);
1478	/* Finally, mark the mft record dirty, so it gets written back. */
1479	mark_mft_record_dirty(ctx->ntfs_ino);
1480	ntfs_attr_put_search_ctx(ctx);
1481	unmap_mft_record(base_ni);
1482	return 0;
1483err_out:
1484	if (err == -ENOMEM) {
1485		ntfs_warning(vi->i_sb, "Error allocating memory. Redirtying "
1486				"page so we try again later.");
1487		/*
1488		 * Put the page back on mapping->dirty_pages, but leave its
1489		 * buffers' dirty state as-is.
1490		 */
1491		redirty_page_for_writepage(wbc, page);
1492		err = 0;
1493	} else {
1494		ntfs_error(vi->i_sb, "Resident attribute write failed with "
1495				"error %i.", err);
1496		SetPageError(page);
1497		NVolSetErrors(ni->vol);
1498	}
1499	unlock_page(page);
1500	if (ctx)
1501		ntfs_attr_put_search_ctx(ctx);
1502	if (m)
1503		unmap_mft_record(base_ni);
1504	return err;
1505}
1506
1507#endif	/* NTFS_RW */
1508
1509/**
1510 * ntfs_aops - general address space operations for inodes and attributes
1511 */
1512const struct address_space_operations ntfs_aops = {
1513	.readpage	= ntfs_readpage,	/* Fill page with data. */
1514	.sync_page	= block_sync_page,	/* Currently, just unplugs the
1515						   disk request queue. */
1516#ifdef NTFS_RW
1517	.writepage	= ntfs_writepage,	/* Write dirty page to disk. */
1518#endif /* NTFS_RW */
1519	.migratepage	= buffer_migrate_page,	/* Move a page cache page from
1520						   one physical page to an
1521						   other. */
1522};
1523
1524/**
1525 * ntfs_mst_aops - general address space operations for mst protecteed inodes
1526 *		   and attributes
1527 */
1528const struct address_space_operations ntfs_mst_aops = {
1529	.readpage	= ntfs_readpage,	/* Fill page with data. */
1530	.sync_page	= block_sync_page,	/* Currently, just unplugs the
1531						   disk request queue. */
1532#ifdef NTFS_RW
1533	.writepage	= ntfs_writepage,	/* Write dirty page to disk. */
1534	.set_page_dirty	= __set_page_dirty_nobuffers,	/* Set the page dirty
1535						   without touching the buffers
1536						   belonging to the page. */
1537#endif /* NTFS_RW */
1538	.migratepage	= buffer_migrate_page,	/* Move a page cache page from
1539						   one physical page to an
1540						   other. */
1541};
1542
1543#ifdef NTFS_RW
1544
1545/**
1546 * mark_ntfs_record_dirty - mark an ntfs record dirty
1547 * @page:	page containing the ntfs record to mark dirty
1548 * @ofs:	byte offset within @page at which the ntfs record begins
1549 *
1550 * Set the buffers and the page in which the ntfs record is located dirty.
1551 *
1552 * The latter also marks the vfs inode the ntfs record belongs to dirty
1553 * (I_DIRTY_PAGES only).
1554 *
1555 * If the page does not have buffers, we create them and set them uptodate.
1556 * The page may not be locked which is why we need to handle the buffers under
1557 * the mapping->private_lock.  Once the buffers are marked dirty we no longer
1558 * need the lock since try_to_free_buffers() does not free dirty buffers.
1559 */
1560void mark_ntfs_record_dirty(struct page *page, const unsigned int ofs) {
1561	struct address_space *mapping = page->mapping;
1562	ntfs_inode *ni = NTFS_I(mapping->host);
1563	struct buffer_head *bh, *head, *buffers_to_free = NULL;
1564	unsigned int end, bh_size, bh_ofs;
1565
1566	BUG_ON(!PageUptodate(page));
1567	end = ofs + ni->itype.index.block_size;
1568	bh_size = VFS_I(ni)->i_sb->s_blocksize;
1569	spin_lock(&mapping->private_lock);
1570	if (unlikely(!page_has_buffers(page))) {
1571		spin_unlock(&mapping->private_lock);
1572		bh = head = alloc_page_buffers(page, bh_size, 1);
1573		spin_lock(&mapping->private_lock);
1574		if (likely(!page_has_buffers(page))) {
1575			struct buffer_head *tail;
1576
1577			do {
1578				set_buffer_uptodate(bh);
1579				tail = bh;
1580				bh = bh->b_this_page;
1581			} while (bh);
1582			tail->b_this_page = head;
1583			attach_page_buffers(page, head);
1584		} else
1585			buffers_to_free = bh;
1586	}
1587	bh = head = page_buffers(page);
1588	BUG_ON(!bh);
1589	do {
1590		bh_ofs = bh_offset(bh);
1591		if (bh_ofs + bh_size <= ofs)
1592			continue;
1593		if (unlikely(bh_ofs >= end))
1594			break;
1595		set_buffer_dirty(bh);
1596	} while ((bh = bh->b_this_page) != head);
1597	spin_unlock(&mapping->private_lock);
1598	__set_page_dirty_nobuffers(page);
1599	if (unlikely(buffers_to_free)) {
1600		do {
1601			bh = buffers_to_free->b_this_page;
1602			free_buffer_head(buffers_to_free);
1603			buffers_to_free = bh;
1604		} while (buffers_to_free);
1605	}
1606}
1607
1608#endif /* NTFS_RW */
1609