blob: 7e361da770b3029917feaf064a54a3fd6a31f5b7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
2 * aops.c - NTFS kernel address space operations and page cache handling.
3 * Part of the Linux-NTFS project.
4 *
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00005 * Copyright (c) 2001-2006 Anton Altaparmakov
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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/mm.h>
26#include <linux/pagemap.h>
27#include <linux/swap.h>
28#include <linux/buffer_head.h>
29#include <linux/writeback.h>
Andrew Mortonb4012a92005-09-10 00:25:47 -070030#include <linux/bit_spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include "aops.h"
33#include "attrib.h"
34#include "debug.h"
35#include "inode.h"
36#include "mft.h"
37#include "runlist.h"
38#include "types.h"
39#include "ntfs.h"
40
41/**
42 * ntfs_end_buffer_async_read - async io completion for reading attributes
43 * @bh: buffer head on which io is completed
44 * @uptodate: whether @bh is now uptodate or not
45 *
46 * Asynchronous I/O completion handler for reading pages belonging to the
47 * attribute address space of an inode. The inodes can either be files or
48 * directories or they can be fake inodes describing some attribute.
49 *
50 * If NInoMstProtected(), perform the post read mst fixups when all IO on the
51 * page has been completed and mark the page uptodate or set the error bit on
52 * the page. To determine the size of the records that need fixing up, we
53 * cheat a little bit by setting the index_block_size in ntfs_inode to the ntfs
54 * record size, and index_block_size_bits, to the log(base 2) of the ntfs
55 * record size.
56 */
57static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
58{
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 unsigned long flags;
Anton Altaparmakove6046352005-09-08 22:13:02 +010060 struct buffer_head *first, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 struct page *page;
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +010062 struct inode *vi;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 ntfs_inode *ni;
64 int page_uptodate = 1;
65
66 page = bh->b_page;
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +010067 vi = page->mapping->host;
68 ni = NTFS_I(vi);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 if (likely(uptodate)) {
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +010071 loff_t i_size;
72 s64 file_ofs, init_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 set_buffer_uptodate(bh);
75
76 file_ofs = ((s64)page->index << PAGE_CACHE_SHIFT) +
77 bh_offset(bh);
Anton Altaparmakov07a4e2d2005-01-12 13:08:26 +000078 read_lock_irqsave(&ni->size_lock, flags);
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +010079 init_size = ni->initialized_size;
80 i_size = i_size_read(vi);
Anton Altaparmakov07a4e2d2005-01-12 13:08:26 +000081 read_unlock_irqrestore(&ni->size_lock, flags);
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +010082 if (unlikely(init_size > i_size)) {
83 /* Race with shrinking truncate. */
84 init_size = i_size;
85 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 /* Check for the current buffer head overflowing. */
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +010087 if (unlikely(file_ofs + bh->b_size > init_size)) {
88 u8 *kaddr;
89 int ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +010091 ofs = 0;
92 if (file_ofs < init_size)
93 ofs = init_size - file_ofs;
94 kaddr = kmap_atomic(page, KM_BIO_SRC_IRQ);
95 memset(kaddr + bh_offset(bh) + ofs, 0,
96 bh->b_size - ofs);
97 kunmap_atomic(kaddr, KM_BIO_SRC_IRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 flush_dcache_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
100 } else {
101 clear_buffer_uptodate(bh);
Anton Altaparmakove6046352005-09-08 22:13:02 +0100102 SetPageError(page);
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +0100103 ntfs_error(ni->vol->sb, "Buffer I/O error, logical block "
104 "0x%llx.", (unsigned long long)bh->b_blocknr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
Anton Altaparmakove6046352005-09-08 22:13:02 +0100106 first = page_buffers(page);
107 local_irq_save(flags);
108 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 clear_buffer_async_read(bh);
110 unlock_buffer(bh);
111 tmp = bh;
112 do {
113 if (!buffer_uptodate(tmp))
114 page_uptodate = 0;
115 if (buffer_async_read(tmp)) {
116 if (likely(buffer_locked(tmp)))
117 goto still_busy;
118 /* Async buffers must be locked. */
119 BUG();
120 }
121 tmp = tmp->b_this_page;
122 } while (tmp != bh);
Anton Altaparmakove6046352005-09-08 22:13:02 +0100123 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
124 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 /*
126 * If none of the buffers had errors then we can set the page uptodate,
127 * but we first have to perform the post read mst fixups, if the
128 * attribute is mst protected, i.e. if NInoMstProteced(ni) is true.
129 * Note we ignore fixup errors as those are detected when
130 * map_mft_record() is called which gives us per record granularity
131 * rather than per page granularity.
132 */
133 if (!NInoMstProtected(ni)) {
134 if (likely(page_uptodate && !PageError(page)))
135 SetPageUptodate(page);
136 } else {
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +0100137 u8 *kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 unsigned int i, recs;
139 u32 rec_size;
140
141 rec_size = ni->itype.index.block_size;
142 recs = PAGE_CACHE_SIZE / rec_size;
143 /* Should have been verified before we got here... */
144 BUG_ON(!recs);
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +0100145 kaddr = kmap_atomic(page, KM_BIO_SRC_IRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 for (i = 0; i < recs; i++)
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +0100147 post_read_mst_fixup((NTFS_RECORD*)(kaddr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 i * rec_size), rec_size);
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +0100149 kunmap_atomic(kaddr, KM_BIO_SRC_IRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 flush_dcache_page(page);
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +0000151 if (likely(page_uptodate && !PageError(page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 SetPageUptodate(page);
153 }
154 unlock_page(page);
155 return;
156still_busy:
Anton Altaparmakove6046352005-09-08 22:13:02 +0100157 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
158 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return;
160}
161
162/**
163 * ntfs_read_block - fill a @page of an address space with data
164 * @page: page cache page to fill with data
165 *
166 * Fill the page @page of the address space belonging to the @page->host inode.
167 * We read each buffer asynchronously and when all buffers are read in, our io
168 * completion handler ntfs_end_buffer_read_async(), if required, automatically
169 * applies the mst fixups to the page before finally marking it uptodate and
170 * unlocking it.
171 *
172 * We only enforce allocated_size limit because i_size is checked for in
173 * generic_file_read().
174 *
175 * Return 0 on success and -errno on error.
176 *
177 * Contains an adapted version of fs/buffer.c::block_read_full_page().
178 */
179static int ntfs_read_block(struct page *page)
180{
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +0100181 loff_t i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 VCN vcn;
183 LCN lcn;
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +0100184 s64 init_size;
185 struct inode *vi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 ntfs_inode *ni;
187 ntfs_volume *vol;
188 runlist_element *rl;
189 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
190 sector_t iblock, lblock, zblock;
Anton Altaparmakov07a4e2d2005-01-12 13:08:26 +0000191 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 unsigned int blocksize, vcn_ofs;
193 int i, nr;
194 unsigned char blocksize_bits;
195
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +0100196 vi = page->mapping->host;
197 ni = NTFS_I(vi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 vol = ni->vol;
199
200 /* $MFT/$DATA must have its complete runlist in memory at all times. */
201 BUG_ON(!ni->runlist.rl && !ni->mft_no && !NInoAttr(ni));
202
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000203 blocksize = vol->sb->s_blocksize;
204 blocksize_bits = vol->sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Anton Altaparmakova01ac532005-09-08 22:08:11 +0100206 if (!page_has_buffers(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 create_empty_buffers(page, blocksize, 0);
Anton Altaparmakova01ac532005-09-08 22:08:11 +0100208 if (unlikely(!page_has_buffers(page))) {
209 unlock_page(page);
210 return -ENOMEM;
211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
Anton Altaparmakova01ac532005-09-08 22:08:11 +0100213 bh = head = page_buffers(page);
214 BUG_ON(!bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +0100216 /*
217 * We may be racing with truncate. To avoid some of the problems we
218 * now take a snapshot of the various sizes and use those for the whole
219 * of the function. In case of an extending truncate it just means we
220 * may leave some buffers unmapped which are now allocated. This is
221 * not a problem since these buffers will just get mapped when a write
222 * occurs. In case of a shrinking truncate, we will detect this later
223 * on due to the runlist being incomplete and if the page is being
224 * fully truncated, truncate will throw it away as soon as we unlock
225 * it so no need to worry what we do with it.
226 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 iblock = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
Anton Altaparmakov07a4e2d2005-01-12 13:08:26 +0000228 read_lock_irqsave(&ni->size_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 lblock = (ni->allocated_size + blocksize - 1) >> blocksize_bits;
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +0100230 init_size = ni->initialized_size;
231 i_size = i_size_read(vi);
Anton Altaparmakov07a4e2d2005-01-12 13:08:26 +0000232 read_unlock_irqrestore(&ni->size_lock, flags);
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +0100233 if (unlikely(init_size > i_size)) {
234 /* Race with shrinking truncate. */
235 init_size = i_size;
236 }
237 zblock = (init_size + blocksize - 1) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 /* Loop through all the buffers in the page. */
240 rl = NULL;
241 nr = i = 0;
242 do {
243 u8 *kaddr;
Anton Altaparmakov8273d5d2005-09-08 22:00:33 +0100244 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 if (unlikely(buffer_uptodate(bh)))
247 continue;
248 if (unlikely(buffer_mapped(bh))) {
249 arr[nr++] = bh;
250 continue;
251 }
Anton Altaparmakov8273d5d2005-09-08 22:00:33 +0100252 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 bh->b_bdev = vol->sb->s_bdev;
254 /* Is the block within the allowed limits? */
255 if (iblock < lblock) {
256 BOOL is_retry = FALSE;
257
258 /* Convert iblock into corresponding vcn and offset. */
259 vcn = (VCN)iblock << blocksize_bits >>
260 vol->cluster_size_bits;
261 vcn_ofs = ((VCN)iblock << blocksize_bits) &
262 vol->cluster_size_mask;
263 if (!rl) {
264lock_retry_remap:
265 down_read(&ni->runlist.lock);
266 rl = ni->runlist.rl;
267 }
268 if (likely(rl != NULL)) {
269 /* Seek to element containing target vcn. */
270 while (rl->length && rl[1].vcn <= vcn)
271 rl++;
272 lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
273 } else
274 lcn = LCN_RL_NOT_MAPPED;
275 /* Successful remap. */
276 if (lcn >= 0) {
277 /* Setup buffer head to correct block. */
278 bh->b_blocknr = ((lcn << vol->cluster_size_bits)
279 + vcn_ofs) >> blocksize_bits;
280 set_buffer_mapped(bh);
281 /* Only read initialized data blocks. */
282 if (iblock < zblock) {
283 arr[nr++] = bh;
284 continue;
285 }
286 /* Fully non-initialized data block, zero it. */
287 goto handle_zblock;
288 }
289 /* It is a hole, need to zero it. */
290 if (lcn == LCN_HOLE)
291 goto handle_hole;
292 /* If first try and runlist unmapped, map and retry. */
293 if (!is_retry && lcn == LCN_RL_NOT_MAPPED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 is_retry = TRUE;
295 /*
296 * Attempt to map runlist, dropping lock for
297 * the duration.
298 */
299 up_read(&ni->runlist.lock);
300 err = ntfs_map_runlist(ni, vcn);
301 if (likely(!err))
302 goto lock_retry_remap;
303 rl = NULL;
Anton Altaparmakov9f993fe2005-06-25 16:15:36 +0100304 } else if (!rl)
305 up_read(&ni->runlist.lock);
Anton Altaparmakov8273d5d2005-09-08 22:00:33 +0100306 /*
307 * If buffer is outside the runlist, treat it as a
308 * hole. This can happen due to concurrent truncate
309 * for example.
310 */
311 if (err == -ENOENT || lcn == LCN_ENOENT) {
312 err = 0;
313 goto handle_hole;
314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 /* Hard error, zero out region. */
Anton Altaparmakov8273d5d2005-09-08 22:00:33 +0100316 if (!err)
317 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 bh->b_blocknr = -1;
319 SetPageError(page);
320 ntfs_error(vol->sb, "Failed to read from inode 0x%lx, "
321 "attribute type 0x%x, vcn 0x%llx, "
322 "offset 0x%x because its location on "
323 "disk could not be determined%s "
Anton Altaparmakov8273d5d2005-09-08 22:00:33 +0100324 "(error code %i).", ni->mft_no,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 ni->type, (unsigned long long)vcn,
326 vcn_ofs, is_retry ? " even after "
Anton Altaparmakov8273d5d2005-09-08 22:00:33 +0100327 "retrying" : "", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329 /*
330 * Either iblock was outside lblock limits or
331 * ntfs_rl_vcn_to_lcn() returned error. Just zero that portion
332 * of the page and set the buffer uptodate.
333 */
334handle_hole:
335 bh->b_blocknr = -1UL;
336 clear_buffer_mapped(bh);
337handle_zblock:
338 kaddr = kmap_atomic(page, KM_USER0);
339 memset(kaddr + i * blocksize, 0, blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 kunmap_atomic(kaddr, KM_USER0);
Anton Altaparmakov8273d5d2005-09-08 22:00:33 +0100341 flush_dcache_page(page);
342 if (likely(!err))
343 set_buffer_uptodate(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 } while (i++, iblock++, (bh = bh->b_this_page) != head);
345
346 /* Release the lock if we took it. */
347 if (rl)
348 up_read(&ni->runlist.lock);
349
350 /* Check we have at least one buffer ready for i/o. */
351 if (nr) {
352 struct buffer_head *tbh;
353
354 /* Lock the buffers. */
355 for (i = 0; i < nr; i++) {
356 tbh = arr[i];
357 lock_buffer(tbh);
358 tbh->b_end_io = ntfs_end_buffer_async_read;
359 set_buffer_async_read(tbh);
360 }
361 /* Finally, start i/o on the buffers. */
362 for (i = 0; i < nr; i++) {
363 tbh = arr[i];
364 if (likely(!buffer_uptodate(tbh)))
365 submit_bh(READ, tbh);
366 else
367 ntfs_end_buffer_async_read(tbh, 1);
368 }
369 return 0;
370 }
371 /* No i/o was scheduled on any of the buffers. */
372 if (likely(!PageError(page)))
373 SetPageUptodate(page);
374 else /* Signal synchronous i/o error. */
375 nr = -EIO;
376 unlock_page(page);
377 return nr;
378}
379
380/**
381 * ntfs_readpage - fill a @page of a @file with data from the device
382 * @file: open file to which the page @page belongs or NULL
383 * @page: page cache page to fill with data
384 *
385 * For non-resident attributes, ntfs_readpage() fills the @page of the open
386 * file @file by calling the ntfs version of the generic block_read_full_page()
387 * function, ntfs_read_block(), which in turn creates and reads in the buffers
388 * associated with the page asynchronously.
389 *
390 * For resident attributes, OTOH, ntfs_readpage() fills @page by copying the
391 * data from the mft record (which at this stage is most likely in memory) and
392 * fills the remainder with zeroes. Thus, in this case, I/O is synchronous, as
393 * even if the mft record is not cached at this point in time, we need to wait
394 * for it to be read in before we can do the copy.
395 *
396 * Return 0 on success and -errno on error.
397 */
398static int ntfs_readpage(struct file *file, struct page *page)
399{
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +0100400 loff_t i_size;
401 struct inode *vi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 ntfs_inode *ni, *base_ni;
403 u8 *kaddr;
404 ntfs_attr_search_ctx *ctx;
405 MFT_RECORD *mrec;
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +0000406 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 u32 attr_len;
408 int err = 0;
409
Anton Altaparmakov905685f2005-03-10 11:06:19 +0000410retry_readpage:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 BUG_ON(!PageLocked(page));
412 /*
413 * This can potentially happen because we clear PageUptodate() during
414 * ntfs_writepage() of MstProtected() attributes.
415 */
416 if (PageUptodate(page)) {
417 unlock_page(page);
418 return 0;
419 }
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +0100420 vi = page->mapping->host;
421 ni = NTFS_I(vi);
Anton Altaparmakov311120e2005-09-08 22:04:20 +0100422 /*
423 * Only $DATA attributes can be encrypted and only unnamed $DATA
424 * attributes can be compressed. Index root can have the flags set but
425 * this means to create compressed/encrypted files, not that the
Anton Altaparmakov4e64c882005-09-19 09:38:41 +0100426 * attribute is compressed/encrypted. Note we need to check for
427 * AT_INDEX_ALLOCATION since this is the type of both directory and
428 * index inodes.
Anton Altaparmakov311120e2005-09-08 22:04:20 +0100429 */
Anton Altaparmakov4e64c882005-09-19 09:38:41 +0100430 if (ni->type != AT_INDEX_ALLOCATION) {
Anton Altaparmakov311120e2005-09-08 22:04:20 +0100431 /* If attribute is encrypted, deny access, just like NT4. */
432 if (NInoEncrypted(ni)) {
433 BUG_ON(ni->type != AT_DATA);
434 err = -EACCES;
435 goto err_out;
436 }
437 /* Compressed data streams are handled in compress.c. */
438 if (NInoNonResident(ni) && NInoCompressed(ni)) {
439 BUG_ON(ni->type != AT_DATA);
440 BUG_ON(ni->name_len);
441 return ntfs_read_compressed_block(page);
442 }
443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 /* NInoNonResident() == NInoIndexAllocPresent() */
445 if (NInoNonResident(ni)) {
Anton Altaparmakov311120e2005-09-08 22:04:20 +0100446 /* Normal, non-resident data stream. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return ntfs_read_block(page);
448 }
449 /*
450 * Attribute is resident, implying it is not compressed or encrypted.
451 * This also means the attribute is smaller than an mft record and
452 * hence smaller than a page, so can simply zero out any pages with
Anton Altaparmakov311120e2005-09-08 22:04:20 +0100453 * index above 0. Note the attribute can actually be marked compressed
454 * but if it is resident the actual data is not compressed so we are
455 * ok to ignore the compressed flag here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 */
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +0000457 if (unlikely(page->index > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 kaddr = kmap_atomic(page, KM_USER0);
459 memset(kaddr, 0, PAGE_CACHE_SIZE);
460 flush_dcache_page(page);
461 kunmap_atomic(kaddr, KM_USER0);
462 goto done;
463 }
464 if (!NInoAttr(ni))
465 base_ni = ni;
466 else
467 base_ni = ni->ext.base_ntfs_ino;
468 /* Map, pin, and lock the mft record. */
469 mrec = map_mft_record(base_ni);
470 if (IS_ERR(mrec)) {
471 err = PTR_ERR(mrec);
472 goto err_out;
473 }
Anton Altaparmakov905685f2005-03-10 11:06:19 +0000474 /*
475 * If a parallel write made the attribute non-resident, drop the mft
476 * record and retry the readpage.
477 */
478 if (unlikely(NInoNonResident(ni))) {
479 unmap_mft_record(base_ni);
480 goto retry_readpage;
481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 ctx = ntfs_attr_get_search_ctx(base_ni, mrec);
483 if (unlikely(!ctx)) {
484 err = -ENOMEM;
485 goto unm_err_out;
486 }
487 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
488 CASE_SENSITIVE, 0, NULL, 0, ctx);
489 if (unlikely(err))
490 goto put_unm_err_out;
491 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +0000492 read_lock_irqsave(&ni->size_lock, flags);
493 if (unlikely(attr_len > ni->initialized_size))
494 attr_len = ni->initialized_size;
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +0100495 i_size = i_size_read(vi);
Anton Altaparmakovb6ad6c52005-02-15 10:08:43 +0000496 read_unlock_irqrestore(&ni->size_lock, flags);
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +0100497 if (unlikely(attr_len > i_size)) {
498 /* Race with shrinking truncate. */
499 attr_len = i_size;
500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 kaddr = kmap_atomic(page, KM_USER0);
502 /* Copy the data to the page. */
503 memcpy(kaddr, (u8*)ctx->attr +
504 le16_to_cpu(ctx->attr->data.resident.value_offset),
505 attr_len);
506 /* Zero the remainder of the page. */
507 memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
508 flush_dcache_page(page);
509 kunmap_atomic(kaddr, KM_USER0);
510put_unm_err_out:
511 ntfs_attr_put_search_ctx(ctx);
512unm_err_out:
513 unmap_mft_record(base_ni);
514done:
515 SetPageUptodate(page);
516err_out:
517 unlock_page(page);
518 return err;
519}
520
521#ifdef NTFS_RW
522
523/**
524 * ntfs_write_block - write a @page to the backing store
525 * @page: page cache page to write out
526 * @wbc: writeback control structure
527 *
528 * This function is for writing pages belonging to non-resident, non-mst
529 * protected attributes to their backing store.
530 *
531 * For a page with buffers, map and write the dirty buffers asynchronously
532 * under page writeback. For a page without buffers, create buffers for the
533 * page, then proceed as above.
534 *
535 * If a page doesn't have buffers the page dirty state is definitive. If a page
536 * does have buffers, the page dirty state is just a hint, and the buffer dirty
537 * state is definitive. (A hint which has rules: dirty buffers against a clean
538 * page is illegal. Other combinations are legal and need to be handled. In
539 * particular a dirty page containing clean buffers for example.)
540 *
541 * Return 0 on success and -errno on error.
542 *
543 * Based on ntfs_read_block() and __block_write_full_page().
544 */
545static int ntfs_write_block(struct page *page, struct writeback_control *wbc)
546{
547 VCN vcn;
548 LCN lcn;
Anton Altaparmakov07a4e2d2005-01-12 13:08:26 +0000549 s64 initialized_size;
550 loff_t i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 sector_t block, dblock, iblock;
552 struct inode *vi;
553 ntfs_inode *ni;
554 ntfs_volume *vol;
555 runlist_element *rl;
556 struct buffer_head *bh, *head;
Anton Altaparmakov07a4e2d2005-01-12 13:08:26 +0000557 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 unsigned int blocksize, vcn_ofs;
559 int err;
560 BOOL need_end_writeback;
561 unsigned char blocksize_bits;
562
563 vi = page->mapping->host;
564 ni = NTFS_I(vi);
565 vol = ni->vol;
566
567 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
568 "0x%lx.", ni->mft_no, ni->type, page->index);
569
570 BUG_ON(!NInoNonResident(ni));
571 BUG_ON(NInoMstProtected(ni));
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000572 blocksize = vol->sb->s_blocksize;
573 blocksize_bits = vol->sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (!page_has_buffers(page)) {
575 BUG_ON(!PageUptodate(page));
576 create_empty_buffers(page, blocksize,
577 (1 << BH_Uptodate) | (1 << BH_Dirty));
Anton Altaparmakova01ac532005-09-08 22:08:11 +0100578 if (unlikely(!page_has_buffers(page))) {
579 ntfs_warning(vol->sb, "Error allocating page "
580 "buffers. Redirtying page so we try "
581 "again later.");
582 /*
583 * Put the page back on mapping->dirty_pages, but leave
584 * its buffers' dirty state as-is.
585 */
586 redirty_page_for_writepage(wbc, page);
587 unlock_page(page);
588 return 0;
589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591 bh = head = page_buffers(page);
Anton Altaparmakova01ac532005-09-08 22:08:11 +0100592 BUG_ON(!bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 /* NOTE: Different naming scheme to ntfs_read_block()! */
595
596 /* The first block in the page. */
597 block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
598
Anton Altaparmakov07a4e2d2005-01-12 13:08:26 +0000599 read_lock_irqsave(&ni->size_lock, flags);
600 i_size = i_size_read(vi);
601 initialized_size = ni->initialized_size;
602 read_unlock_irqrestore(&ni->size_lock, flags);
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /* The first out of bounds block for the data size. */
Anton Altaparmakov07a4e2d2005-01-12 13:08:26 +0000605 dblock = (i_size + blocksize - 1) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 /* The last (fully or partially) initialized block. */
Anton Altaparmakov07a4e2d2005-01-12 13:08:26 +0000608 iblock = initialized_size >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 /*
611 * Be very careful. We have no exclusion from __set_page_dirty_buffers
612 * here, and the (potentially unmapped) buffers may become dirty at
613 * any time. If a buffer becomes dirty here after we've inspected it
614 * then we just miss that fact, and the page stays dirty.
615 *
616 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
617 * handle that here by just cleaning them.
618 */
619
620 /*
621 * Loop through all the buffers in the page, mapping all the dirty
622 * buffers to disk addresses and handling any aliases from the
623 * underlying block device's mapping.
624 */
625 rl = NULL;
626 err = 0;
627 do {
628 BOOL is_retry = FALSE;
629
630 if (unlikely(block >= dblock)) {
631 /*
632 * Mapped buffers outside i_size will occur, because
633 * this page can be outside i_size when there is a
634 * truncate in progress. The contents of such buffers
635 * were zeroed by ntfs_writepage().
636 *
637 * FIXME: What about the small race window where
638 * ntfs_writepage() has not done any clearing because
639 * the page was within i_size but before we get here,
640 * vmtruncate() modifies i_size?
641 */
642 clear_buffer_dirty(bh);
643 set_buffer_uptodate(bh);
644 continue;
645 }
646
647 /* Clean buffers are not written out, so no need to map them. */
648 if (!buffer_dirty(bh))
649 continue;
650
651 /* Make sure we have enough initialized size. */
652 if (unlikely((block >= iblock) &&
Anton Altaparmakov07a4e2d2005-01-12 13:08:26 +0000653 (initialized_size < i_size))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /*
655 * If this page is fully outside initialized size, zero
656 * out all pages between the current initialized size
657 * and the current page. Just use ntfs_readpage() to do
658 * the zeroing transparently.
659 */
660 if (block > iblock) {
661 // TODO:
662 // For each page do:
663 // - read_cache_page()
664 // Again for each page do:
665 // - wait_on_page_locked()
666 // - Check (PageUptodate(page) &&
667 // !PageError(page))
668 // Update initialized size in the attribute and
669 // in the inode.
670 // Again, for each page do:
671 // __set_page_dirty_buffers();
672 // page_cache_release()
673 // We don't need to wait on the writes.
674 // Update iblock.
675 }
676 /*
677 * The current page straddles initialized size. Zero
678 * all non-uptodate buffers and set them uptodate (and
679 * dirty?). Note, there aren't any non-uptodate buffers
680 * if the page is uptodate.
681 * FIXME: For an uptodate page, the buffers may need to
682 * be written out because they were not initialized on
683 * disk before.
684 */
685 if (!PageUptodate(page)) {
686 // TODO:
687 // Zero any non-uptodate buffers up to i_size.
688 // Set them uptodate and dirty.
689 }
690 // TODO:
691 // Update initialized size in the attribute and in the
692 // inode (up to i_size).
693 // Update iblock.
694 // FIXME: This is inefficient. Try to batch the two
695 // size changes to happen in one go.
696 ntfs_error(vol->sb, "Writing beyond initialized size "
697 "is not supported yet. Sorry.");
698 err = -EOPNOTSUPP;
699 break;
700 // Do NOT set_buffer_new() BUT DO clear buffer range
701 // outside write request range.
702 // set_buffer_uptodate() on complete buffers as well as
703 // set_buffer_dirty().
704 }
705
706 /* No need to map buffers that are already mapped. */
707 if (buffer_mapped(bh))
708 continue;
709
710 /* Unmapped, dirty buffer. Need to map it. */
711 bh->b_bdev = vol->sb->s_bdev;
712
713 /* Convert block into corresponding vcn and offset. */
714 vcn = (VCN)block << blocksize_bits;
715 vcn_ofs = vcn & vol->cluster_size_mask;
716 vcn >>= vol->cluster_size_bits;
717 if (!rl) {
718lock_retry_remap:
719 down_read(&ni->runlist.lock);
720 rl = ni->runlist.rl;
721 }
722 if (likely(rl != NULL)) {
723 /* Seek to element containing target vcn. */
724 while (rl->length && rl[1].vcn <= vcn)
725 rl++;
726 lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
727 } else
728 lcn = LCN_RL_NOT_MAPPED;
729 /* Successful remap. */
730 if (lcn >= 0) {
731 /* Setup buffer head to point to correct block. */
732 bh->b_blocknr = ((lcn << vol->cluster_size_bits) +
733 vcn_ofs) >> blocksize_bits;
734 set_buffer_mapped(bh);
735 continue;
736 }
737 /* It is a hole, need to instantiate it. */
738 if (lcn == LCN_HOLE) {
Anton Altaparmakov8dcdeba2005-09-08 21:25:48 +0100739 u8 *kaddr;
740 unsigned long *bpos, *bend;
741
742 /* Check if the buffer is zero. */
743 kaddr = kmap_atomic(page, KM_USER0);
744 bpos = (unsigned long *)(kaddr + bh_offset(bh));
745 bend = (unsigned long *)((u8*)bpos + blocksize);
746 do {
747 if (unlikely(*bpos))
748 break;
749 } while (likely(++bpos < bend));
750 kunmap_atomic(kaddr, KM_USER0);
751 if (bpos == bend) {
752 /*
753 * Buffer is zero and sparse, no need to write
754 * it.
755 */
756 bh->b_blocknr = -1;
757 clear_buffer_dirty(bh);
758 continue;
759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 // TODO: Instantiate the hole.
761 // clear_buffer_new(bh);
762 // unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
763 ntfs_error(vol->sb, "Writing into sparse regions is "
764 "not supported yet. Sorry.");
765 err = -EOPNOTSUPP;
766 break;
767 }
768 /* If first try and runlist unmapped, map and retry. */
769 if (!is_retry && lcn == LCN_RL_NOT_MAPPED) {
770 is_retry = TRUE;
771 /*
772 * Attempt to map runlist, dropping lock for
773 * the duration.
774 */
775 up_read(&ni->runlist.lock);
776 err = ntfs_map_runlist(ni, vcn);
777 if (likely(!err))
778 goto lock_retry_remap;
779 rl = NULL;
Anton Altaparmakov9f993fe2005-06-25 16:15:36 +0100780 } else if (!rl)
781 up_read(&ni->runlist.lock);
Anton Altaparmakov8273d5d2005-09-08 22:00:33 +0100782 /*
783 * If buffer is outside the runlist, truncate has cut it out
784 * of the runlist. Just clean and clear the buffer and set it
785 * uptodate so it can get discarded by the VM.
786 */
787 if (err == -ENOENT || lcn == LCN_ENOENT) {
788 u8 *kaddr;
789
790 bh->b_blocknr = -1;
791 clear_buffer_dirty(bh);
792 kaddr = kmap_atomic(page, KM_USER0);
793 memset(kaddr + bh_offset(bh), 0, blocksize);
794 kunmap_atomic(kaddr, KM_USER0);
795 flush_dcache_page(page);
796 set_buffer_uptodate(bh);
797 err = 0;
798 continue;
799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 /* Failed to map the buffer, even after retrying. */
Anton Altaparmakov8273d5d2005-09-08 22:00:33 +0100801 if (!err)
802 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 bh->b_blocknr = -1;
804 ntfs_error(vol->sb, "Failed to write to inode 0x%lx, "
805 "attribute type 0x%x, vcn 0x%llx, offset 0x%x "
806 "because its location on disk could not be "
Anton Altaparmakov8273d5d2005-09-08 22:00:33 +0100807 "determined%s (error code %i).", ni->mft_no,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 ni->type, (unsigned long long)vcn,
809 vcn_ofs, is_retry ? " even after "
Anton Altaparmakov8273d5d2005-09-08 22:00:33 +0100810 "retrying" : "", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 break;
812 } while (block++, (bh = bh->b_this_page) != head);
813
814 /* Release the lock if we took it. */
815 if (rl)
816 up_read(&ni->runlist.lock);
817
818 /* For the error case, need to reset bh to the beginning. */
819 bh = head;
820
Anton Altaparmakov54b02eb2005-09-08 21:43:47 +0100821 /* Just an optimization, so ->readpage() is not called later. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (unlikely(!PageUptodate(page))) {
823 int uptodate = 1;
824 do {
825 if (!buffer_uptodate(bh)) {
826 uptodate = 0;
827 bh = head;
828 break;
829 }
830 } while ((bh = bh->b_this_page) != head);
831 if (uptodate)
832 SetPageUptodate(page);
833 }
834
835 /* Setup all mapped, dirty buffers for async write i/o. */
836 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (buffer_mapped(bh) && buffer_dirty(bh)) {
838 lock_buffer(bh);
839 if (test_clear_buffer_dirty(bh)) {
840 BUG_ON(!buffer_uptodate(bh));
841 mark_buffer_async_write(bh);
842 } else
843 unlock_buffer(bh);
844 } else if (unlikely(err)) {
845 /*
846 * For the error case. The buffer may have been set
847 * dirty during attachment to a dirty page.
848 */
849 if (err != -ENOMEM)
850 clear_buffer_dirty(bh);
851 }
852 } while ((bh = bh->b_this_page) != head);
853
854 if (unlikely(err)) {
855 // TODO: Remove the -EOPNOTSUPP check later on...
856 if (unlikely(err == -EOPNOTSUPP))
857 err = 0;
858 else if (err == -ENOMEM) {
859 ntfs_warning(vol->sb, "Error allocating memory. "
860 "Redirtying page so we try again "
861 "later.");
862 /*
863 * Put the page back on mapping->dirty_pages, but
864 * leave its buffer's dirty state as-is.
865 */
866 redirty_page_for_writepage(wbc, page);
867 err = 0;
868 } else
869 SetPageError(page);
870 }
871
872 BUG_ON(PageWriteback(page));
873 set_page_writeback(page); /* Keeps try_to_free_buffers() away. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Anton Altaparmakov54b02eb2005-09-08 21:43:47 +0100875 /* Submit the prepared buffers for i/o. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 need_end_writeback = TRUE;
877 do {
878 struct buffer_head *next = bh->b_this_page;
879 if (buffer_async_write(bh)) {
880 submit_bh(WRITE, bh);
881 need_end_writeback = FALSE;
882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 bh = next;
884 } while (bh != head);
Anton Altaparmakov54b02eb2005-09-08 21:43:47 +0100885 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 /* If no i/o was started, need to end_page_writeback(). */
888 if (unlikely(need_end_writeback))
889 end_page_writeback(page);
890
891 ntfs_debug("Done.");
892 return err;
893}
894
895/**
896 * ntfs_write_mst_block - write a @page to the backing store
897 * @page: page cache page to write out
898 * @wbc: writeback control structure
899 *
900 * This function is for writing pages belonging to non-resident, mst protected
901 * attributes to their backing store. The only supported attributes are index
902 * allocation and $MFT/$DATA. Both directory inodes and index inodes are
903 * supported for the index allocation case.
904 *
905 * The page must remain locked for the duration of the write because we apply
906 * the mst fixups, write, and then undo the fixups, so if we were to unlock the
907 * page before undoing the fixups, any other user of the page will see the
908 * page contents as corrupt.
909 *
910 * We clear the page uptodate flag for the duration of the function to ensure
911 * exclusion for the $MFT/$DATA case against someone mapping an mft record we
912 * are about to apply the mst fixups to.
913 *
914 * Return 0 on success and -errno on error.
915 *
916 * Based on ntfs_write_block(), ntfs_mft_writepage(), and
917 * write_mft_record_nolock().
918 */
919static int ntfs_write_mst_block(struct page *page,
920 struct writeback_control *wbc)
921{
922 sector_t block, dblock, rec_block;
923 struct inode *vi = page->mapping->host;
924 ntfs_inode *ni = NTFS_I(vi);
925 ntfs_volume *vol = ni->vol;
926 u8 *kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 unsigned int rec_size = ni->itype.index.block_size;
928 ntfs_inode *locked_nis[PAGE_CACHE_SIZE / rec_size];
929 struct buffer_head *bh, *head, *tbh, *rec_start_bh;
Anton Altaparmakovd53ee322005-04-06 16:11:20 +0100930 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 runlist_element *rl;
Anton Altaparmakovd53ee322005-04-06 16:11:20 +0100932 int i, nr_locked_nis, nr_recs, nr_bhs, max_bhs, bhs_per_rec, err, err2;
933 unsigned bh_size, rec_size_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 BOOL sync, is_mft, page_is_dirty, rec_is_dirty;
Anton Altaparmakovd53ee322005-04-06 16:11:20 +0100935 unsigned char bh_size_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
938 "0x%lx.", vi->i_ino, ni->type, page->index);
939 BUG_ON(!NInoNonResident(ni));
940 BUG_ON(!NInoMstProtected(ni));
941 is_mft = (S_ISREG(vi->i_mode) && !vi->i_ino);
942 /*
943 * NOTE: ntfs_write_mst_block() would be called for $MFTMirr if a page
944 * in its page cache were to be marked dirty. However this should
945 * never happen with the current driver and considering we do not
946 * handle this case here we do want to BUG(), at least for now.
947 */
948 BUG_ON(!(is_mft || S_ISDIR(vi->i_mode) ||
949 (NInoAttr(ni) && ni->type == AT_INDEX_ALLOCATION)));
Anton Altaparmakov78af34f2006-02-24 10:32:33 +0000950 bh_size = vol->sb->s_blocksize;
951 bh_size_bits = vol->sb->s_blocksize_bits;
Anton Altaparmakovd53ee322005-04-06 16:11:20 +0100952 max_bhs = PAGE_CACHE_SIZE / bh_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 BUG_ON(!max_bhs);
Anton Altaparmakovd53ee322005-04-06 16:11:20 +0100954 BUG_ON(max_bhs > MAX_BUF_PER_PAGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 /* Were we called for sync purposes? */
957 sync = (wbc->sync_mode == WB_SYNC_ALL);
958
959 /* Make sure we have mapped buffers. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 bh = head = page_buffers(page);
961 BUG_ON(!bh);
962
963 rec_size_bits = ni->itype.index.block_size_bits;
964 BUG_ON(!(PAGE_CACHE_SIZE >> rec_size_bits));
965 bhs_per_rec = rec_size >> bh_size_bits;
966 BUG_ON(!bhs_per_rec);
967
968 /* The first block in the page. */
969 rec_block = block = (sector_t)page->index <<
970 (PAGE_CACHE_SHIFT - bh_size_bits);
971
972 /* The first out of bounds block for the data size. */
Anton Altaparmakov07a4e2d2005-01-12 13:08:26 +0000973 dblock = (i_size_read(vi) + bh_size - 1) >> bh_size_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 rl = NULL;
976 err = err2 = nr_bhs = nr_recs = nr_locked_nis = 0;
977 page_is_dirty = rec_is_dirty = FALSE;
978 rec_start_bh = NULL;
979 do {
980 BOOL is_retry = FALSE;
981
982 if (likely(block < rec_block)) {
983 if (unlikely(block >= dblock)) {
984 clear_buffer_dirty(bh);
Anton Altaparmakov946929d2005-01-13 15:26:29 +0000985 set_buffer_uptodate(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 continue;
987 }
988 /*
989 * This block is not the first one in the record. We
990 * ignore the buffer's dirty state because we could
991 * have raced with a parallel mark_ntfs_record_dirty().
992 */
993 if (!rec_is_dirty)
994 continue;
995 if (unlikely(err2)) {
996 if (err2 != -ENOMEM)
997 clear_buffer_dirty(bh);
998 continue;
999 }
1000 } else /* if (block == rec_block) */ {
1001 BUG_ON(block > rec_block);
1002 /* This block is the first one in the record. */
1003 rec_block += bhs_per_rec;
1004 err2 = 0;
1005 if (unlikely(block >= dblock)) {
1006 clear_buffer_dirty(bh);
1007 continue;
1008 }
1009 if (!buffer_dirty(bh)) {
1010 /* Clean records are not written out. */
1011 rec_is_dirty = FALSE;
1012 continue;
1013 }
1014 rec_is_dirty = TRUE;
1015 rec_start_bh = bh;
1016 }
1017 /* Need to map the buffer if it is not mapped already. */
1018 if (unlikely(!buffer_mapped(bh))) {
1019 VCN vcn;
1020 LCN lcn;
1021 unsigned int vcn_ofs;
1022
Anton Altaparmakov481d0372005-08-16 19:42:56 +01001023 bh->b_bdev = vol->sb->s_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 /* Obtain the vcn and offset of the current block. */
1025 vcn = (VCN)block << bh_size_bits;
1026 vcn_ofs = vcn & vol->cluster_size_mask;
1027 vcn >>= vol->cluster_size_bits;
1028 if (!rl) {
1029lock_retry_remap:
1030 down_read(&ni->runlist.lock);
1031 rl = ni->runlist.rl;
1032 }
1033 if (likely(rl != NULL)) {
1034 /* Seek to element containing target vcn. */
1035 while (rl->length && rl[1].vcn <= vcn)
1036 rl++;
1037 lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
1038 } else
1039 lcn = LCN_RL_NOT_MAPPED;
1040 /* Successful remap. */
1041 if (likely(lcn >= 0)) {
1042 /* Setup buffer head to correct block. */
1043 bh->b_blocknr = ((lcn <<
1044 vol->cluster_size_bits) +
1045 vcn_ofs) >> bh_size_bits;
1046 set_buffer_mapped(bh);
1047 } else {
1048 /*
1049 * Remap failed. Retry to map the runlist once
1050 * unless we are working on $MFT which always
1051 * has the whole of its runlist in memory.
1052 */
1053 if (!is_mft && !is_retry &&
1054 lcn == LCN_RL_NOT_MAPPED) {
1055 is_retry = TRUE;
1056 /*
1057 * Attempt to map runlist, dropping
1058 * lock for the duration.
1059 */
1060 up_read(&ni->runlist.lock);
1061 err2 = ntfs_map_runlist(ni, vcn);
1062 if (likely(!err2))
1063 goto lock_retry_remap;
1064 if (err2 == -ENOMEM)
1065 page_is_dirty = TRUE;
1066 lcn = err2;
Anton Altaparmakov9f993fe2005-06-25 16:15:36 +01001067 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 err2 = -EIO;
Anton Altaparmakov9f993fe2005-06-25 16:15:36 +01001069 if (!rl)
1070 up_read(&ni->runlist.lock);
1071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 /* Hard error. Abort writing this record. */
1073 if (!err || err == -ENOMEM)
1074 err = err2;
1075 bh->b_blocknr = -1;
1076 ntfs_error(vol->sb, "Cannot write ntfs record "
1077 "0x%llx (inode 0x%lx, "
1078 "attribute type 0x%x) because "
1079 "its location on disk could "
1080 "not be determined (error "
Randy Dunlap89075472005-03-03 11:19:53 +00001081 "code %lli).",
1082 (long long)block <<
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 bh_size_bits >>
1084 vol->mft_record_size_bits,
1085 ni->mft_no, ni->type,
1086 (long long)lcn);
1087 /*
1088 * If this is not the first buffer, remove the
1089 * buffers in this record from the list of
1090 * buffers to write and clear their dirty bit
1091 * if not error -ENOMEM.
1092 */
1093 if (rec_start_bh != bh) {
1094 while (bhs[--nr_bhs] != rec_start_bh)
1095 ;
1096 if (err2 != -ENOMEM) {
1097 do {
1098 clear_buffer_dirty(
1099 rec_start_bh);
1100 } while ((rec_start_bh =
1101 rec_start_bh->
1102 b_this_page) !=
1103 bh);
1104 }
1105 }
1106 continue;
1107 }
1108 }
1109 BUG_ON(!buffer_uptodate(bh));
1110 BUG_ON(nr_bhs >= max_bhs);
1111 bhs[nr_bhs++] = bh;
1112 } while (block++, (bh = bh->b_this_page) != head);
1113 if (unlikely(rl))
1114 up_read(&ni->runlist.lock);
1115 /* If there were no dirty buffers, we are done. */
1116 if (!nr_bhs)
1117 goto done;
1118 /* Map the page so we can access its contents. */
1119 kaddr = kmap(page);
1120 /* Clear the page uptodate flag whilst the mst fixups are applied. */
1121 BUG_ON(!PageUptodate(page));
1122 ClearPageUptodate(page);
1123 for (i = 0; i < nr_bhs; i++) {
1124 unsigned int ofs;
1125
1126 /* Skip buffers which are not at the beginning of records. */
1127 if (i % bhs_per_rec)
1128 continue;
1129 tbh = bhs[i];
1130 ofs = bh_offset(tbh);
1131 if (is_mft) {
1132 ntfs_inode *tni;
1133 unsigned long mft_no;
1134
1135 /* Get the mft record number. */
1136 mft_no = (((s64)page->index << PAGE_CACHE_SHIFT) + ofs)
1137 >> rec_size_bits;
1138 /* Check whether to write this mft record. */
1139 tni = NULL;
1140 if (!ntfs_may_write_mft_record(vol, mft_no,
1141 (MFT_RECORD*)(kaddr + ofs), &tni)) {
1142 /*
1143 * The record should not be written. This
1144 * means we need to redirty the page before
1145 * returning.
1146 */
1147 page_is_dirty = TRUE;
1148 /*
1149 * Remove the buffers in this mft record from
1150 * the list of buffers to write.
1151 */
1152 do {
1153 bhs[i] = NULL;
1154 } while (++i % bhs_per_rec);
1155 continue;
1156 }
1157 /*
1158 * The record should be written. If a locked ntfs
1159 * inode was returned, add it to the array of locked
1160 * ntfs inodes.
1161 */
1162 if (tni)
1163 locked_nis[nr_locked_nis++] = tni;
1164 }
1165 /* Apply the mst protection fixups. */
1166 err2 = pre_write_mst_fixup((NTFS_RECORD*)(kaddr + ofs),
1167 rec_size);
1168 if (unlikely(err2)) {
1169 if (!err || err == -ENOMEM)
1170 err = -EIO;
1171 ntfs_error(vol->sb, "Failed to apply mst fixups "
1172 "(inode 0x%lx, attribute type 0x%x, "
1173 "page index 0x%lx, page offset 0x%x)!"
1174 " Unmount and run chkdsk.", vi->i_ino,
1175 ni->type, page->index, ofs);
1176 /*
1177 * Mark all the buffers in this record clean as we do
1178 * not want to write corrupt data to disk.
1179 */
1180 do {
1181 clear_buffer_dirty(bhs[i]);
1182 bhs[i] = NULL;
1183 } while (++i % bhs_per_rec);
1184 continue;
1185 }
1186 nr_recs++;
1187 }
1188 /* If no records are to be written out, we are done. */
1189 if (!nr_recs)
1190 goto unm_done;
1191 flush_dcache_page(page);
1192 /* Lock buffers and start synchronous write i/o on them. */
1193 for (i = 0; i < nr_bhs; i++) {
1194 tbh = bhs[i];
1195 if (!tbh)
1196 continue;
1197 if (unlikely(test_set_buffer_locked(tbh)))
1198 BUG();
1199 /* The buffer dirty state is now irrelevant, just clean it. */
1200 clear_buffer_dirty(tbh);
1201 BUG_ON(!buffer_uptodate(tbh));
1202 BUG_ON(!buffer_mapped(tbh));
1203 get_bh(tbh);
1204 tbh->b_end_io = end_buffer_write_sync;
1205 submit_bh(WRITE, tbh);
1206 }
1207 /* Synchronize the mft mirror now if not @sync. */
1208 if (is_mft && !sync)
1209 goto do_mirror;
1210do_wait:
1211 /* Wait on i/o completion of buffers. */
1212 for (i = 0; i < nr_bhs; i++) {
1213 tbh = bhs[i];
1214 if (!tbh)
1215 continue;
1216 wait_on_buffer(tbh);
1217 if (unlikely(!buffer_uptodate(tbh))) {
1218 ntfs_error(vol->sb, "I/O error while writing ntfs "
1219 "record buffer (inode 0x%lx, "
1220 "attribute type 0x%x, page index "
1221 "0x%lx, page offset 0x%lx)! Unmount "
1222 "and run chkdsk.", vi->i_ino, ni->type,
1223 page->index, bh_offset(tbh));
1224 if (!err || err == -ENOMEM)
1225 err = -EIO;
1226 /*
1227 * Set the buffer uptodate so the page and buffer
1228 * states do not become out of sync.
1229 */
1230 set_buffer_uptodate(tbh);
1231 }
1232 }
1233 /* If @sync, now synchronize the mft mirror. */
1234 if (is_mft && sync) {
1235do_mirror:
1236 for (i = 0; i < nr_bhs; i++) {
1237 unsigned long mft_no;
1238 unsigned int ofs;
1239
1240 /*
1241 * Skip buffers which are not at the beginning of
1242 * records.
1243 */
1244 if (i % bhs_per_rec)
1245 continue;
1246 tbh = bhs[i];
1247 /* Skip removed buffers (and hence records). */
1248 if (!tbh)
1249 continue;
1250 ofs = bh_offset(tbh);
1251 /* Get the mft record number. */
1252 mft_no = (((s64)page->index << PAGE_CACHE_SHIFT) + ofs)
1253 >> rec_size_bits;
1254 if (mft_no < vol->mftmirr_size)
1255 ntfs_sync_mft_mirror(vol, mft_no,
1256 (MFT_RECORD*)(kaddr + ofs),
1257 sync);
1258 }
1259 if (!sync)
1260 goto do_wait;
1261 }
1262 /* Remove the mst protection fixups again. */
1263 for (i = 0; i < nr_bhs; i++) {
1264 if (!(i % bhs_per_rec)) {
1265 tbh = bhs[i];
1266 if (!tbh)
1267 continue;
1268 post_write_mst_fixup((NTFS_RECORD*)(kaddr +
1269 bh_offset(tbh)));
1270 }
1271 }
1272 flush_dcache_page(page);
1273unm_done:
1274 /* Unlock any locked inodes. */
1275 while (nr_locked_nis-- > 0) {
1276 ntfs_inode *tni, *base_tni;
1277
1278 tni = locked_nis[nr_locked_nis];
1279 /* Get the base inode. */
1280 down(&tni->extent_lock);
1281 if (tni->nr_extents >= 0)
1282 base_tni = tni;
1283 else {
1284 base_tni = tni->ext.base_ntfs_ino;
1285 BUG_ON(!base_tni);
1286 }
1287 up(&tni->extent_lock);
1288 ntfs_debug("Unlocking %s inode 0x%lx.",
1289 tni == base_tni ? "base" : "extent",
1290 tni->mft_no);
1291 up(&tni->mrec_lock);
1292 atomic_dec(&tni->count);
1293 iput(VFS_I(base_tni));
1294 }
1295 SetPageUptodate(page);
1296 kunmap(page);
1297done:
1298 if (unlikely(err && err != -ENOMEM)) {
1299 /*
1300 * Set page error if there is only one ntfs record in the page.
1301 * Otherwise we would loose per-record granularity.
1302 */
1303 if (ni->itype.index.block_size == PAGE_CACHE_SIZE)
1304 SetPageError(page);
1305 NVolSetErrors(vol);
1306 }
1307 if (page_is_dirty) {
1308 ntfs_debug("Page still contains one or more dirty ntfs "
1309 "records. Redirtying the page starting at "
1310 "record 0x%lx.", page->index <<
1311 (PAGE_CACHE_SHIFT - rec_size_bits));
1312 redirty_page_for_writepage(wbc, page);
1313 unlock_page(page);
1314 } else {
1315 /*
1316 * Keep the VM happy. This must be done otherwise the
1317 * radix-tree tag PAGECACHE_TAG_DIRTY remains set even though
1318 * the page is clean.
1319 */
1320 BUG_ON(PageWriteback(page));
1321 set_page_writeback(page);
1322 unlock_page(page);
1323 end_page_writeback(page);
1324 }
1325 if (likely(!err))
1326 ntfs_debug("Done.");
1327 return err;
1328}
1329
1330/**
1331 * ntfs_writepage - write a @page to the backing store
1332 * @page: page cache page to write out
1333 * @wbc: writeback control structure
1334 *
1335 * This is called from the VM when it wants to have a dirty ntfs page cache
1336 * page cleaned. The VM has already locked the page and marked it clean.
1337 *
1338 * For non-resident attributes, ntfs_writepage() writes the @page by calling
1339 * the ntfs version of the generic block_write_full_page() function,
1340 * ntfs_write_block(), which in turn if necessary creates and writes the
1341 * buffers associated with the page asynchronously.
1342 *
1343 * For resident attributes, OTOH, ntfs_writepage() writes the @page by copying
1344 * the data to the mft record (which at this stage is most likely in memory).
1345 * The mft record is then marked dirty and written out asynchronously via the
1346 * vfs inode dirty code path for the inode the mft record belongs to or via the
1347 * vm page dirty code path for the page the mft record is in.
1348 *
1349 * Based on ntfs_readpage() and fs/buffer.c::block_write_full_page().
1350 *
1351 * Return 0 on success and -errno on error.
1352 */
1353static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
1354{
1355 loff_t i_size;
Anton Altaparmakov149f0c52005-01-12 13:52:30 +00001356 struct inode *vi = page->mapping->host;
1357 ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 char *kaddr;
Anton Altaparmakov149f0c52005-01-12 13:52:30 +00001359 ntfs_attr_search_ctx *ctx = NULL;
1360 MFT_RECORD *m = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 u32 attr_len;
1362 int err;
1363
Anton Altaparmakov905685f2005-03-10 11:06:19 +00001364retry_writepage:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 i_size = i_size_read(vi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 /* Is the page fully outside i_size? (truncate in progress) */
1368 if (unlikely(page->index >= (i_size + PAGE_CACHE_SIZE - 1) >>
1369 PAGE_CACHE_SHIFT)) {
1370 /*
1371 * The page may have dirty, unmapped buffers. Make them
1372 * freeable here, so the page does not leak.
1373 */
1374 block_invalidatepage(page, 0);
1375 unlock_page(page);
1376 ntfs_debug("Write outside i_size - truncated?");
1377 return 0;
1378 }
Anton Altaparmakovbd45fdd2005-09-08 21:38:05 +01001379 /*
1380 * Only $DATA attributes can be encrypted and only unnamed $DATA
1381 * attributes can be compressed. Index root can have the flags set but
1382 * this means to create compressed/encrypted files, not that the
Anton Altaparmakov4e64c882005-09-19 09:38:41 +01001383 * attribute is compressed/encrypted. Note we need to check for
1384 * AT_INDEX_ALLOCATION since this is the type of both directory and
1385 * index inodes.
Anton Altaparmakovbd45fdd2005-09-08 21:38:05 +01001386 */
Anton Altaparmakov4e64c882005-09-19 09:38:41 +01001387 if (ni->type != AT_INDEX_ALLOCATION) {
Anton Altaparmakovbd45fdd2005-09-08 21:38:05 +01001388 /* If file is encrypted, deny access, just like NT4. */
1389 if (NInoEncrypted(ni)) {
1390 unlock_page(page);
1391 BUG_ON(ni->type != AT_DATA);
Anton Altaparmakov7d0ffdb2005-10-19 12:21:19 +01001392 ntfs_debug("Denying write access to encrypted file.");
Anton Altaparmakovbd45fdd2005-09-08 21:38:05 +01001393 return -EACCES;
1394 }
1395 /* Compressed data streams are handled in compress.c. */
1396 if (NInoNonResident(ni) && NInoCompressed(ni)) {
1397 BUG_ON(ni->type != AT_DATA);
1398 BUG_ON(ni->name_len);
1399 // TODO: Implement and replace this with
1400 // return ntfs_write_compressed_block(page);
1401 unlock_page(page);
1402 ntfs_error(vi->i_sb, "Writing to compressed files is "
1403 "not supported yet. Sorry.");
1404 return -EOPNOTSUPP;
1405 }
1406 // TODO: Implement and remove this check.
1407 if (NInoNonResident(ni) && NInoSparse(ni)) {
1408 unlock_page(page);
1409 ntfs_error(vi->i_sb, "Writing to sparse files is not "
1410 "supported yet. Sorry.");
1411 return -EOPNOTSUPP;
1412 }
1413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 /* NInoNonResident() == NInoIndexAllocPresent() */
1415 if (NInoNonResident(ni)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 /* We have to zero every time due to mmap-at-end-of-file. */
1417 if (page->index >= (i_size >> PAGE_CACHE_SHIFT)) {
1418 /* The page straddles i_size. */
1419 unsigned int ofs = i_size & ~PAGE_CACHE_MASK;
1420 kaddr = kmap_atomic(page, KM_USER0);
1421 memset(kaddr + ofs, 0, PAGE_CACHE_SIZE - ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 kunmap_atomic(kaddr, KM_USER0);
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +01001423 flush_dcache_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
1425 /* Handle mst protected attributes. */
1426 if (NInoMstProtected(ni))
1427 return ntfs_write_mst_block(page, wbc);
Anton Altaparmakovbd45fdd2005-09-08 21:38:05 +01001428 /* Normal, non-resident data stream. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return ntfs_write_block(page, wbc);
1430 }
1431 /*
Anton Altaparmakovbd45fdd2005-09-08 21:38:05 +01001432 * Attribute is resident, implying it is not compressed, encrypted, or
1433 * mst protected. This also means the attribute is smaller than an mft
1434 * record and hence smaller than a page, so can simply return error on
1435 * any pages with index above 0. Note the attribute can actually be
1436 * marked compressed but if it is resident the actual data is not
1437 * compressed so we are ok to ignore the compressed flag here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 */
1439 BUG_ON(page_has_buffers(page));
1440 BUG_ON(!PageUptodate(page));
1441 if (unlikely(page->index > 0)) {
1442 ntfs_error(vi->i_sb, "BUG()! page->index (0x%lx) > 0. "
1443 "Aborting write.", page->index);
1444 BUG_ON(PageWriteback(page));
1445 set_page_writeback(page);
1446 unlock_page(page);
1447 end_page_writeback(page);
1448 return -EIO;
1449 }
1450 if (!NInoAttr(ni))
1451 base_ni = ni;
1452 else
1453 base_ni = ni->ext.base_ntfs_ino;
1454 /* Map, pin, and lock the mft record. */
1455 m = map_mft_record(base_ni);
1456 if (IS_ERR(m)) {
1457 err = PTR_ERR(m);
1458 m = NULL;
1459 ctx = NULL;
1460 goto err_out;
1461 }
Anton Altaparmakov905685f2005-03-10 11:06:19 +00001462 /*
1463 * If a parallel write made the attribute non-resident, drop the mft
1464 * record and retry the writepage.
1465 */
1466 if (unlikely(NInoNonResident(ni))) {
1467 unmap_mft_record(base_ni);
1468 goto retry_writepage;
1469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 ctx = ntfs_attr_get_search_ctx(base_ni, m);
1471 if (unlikely(!ctx)) {
1472 err = -ENOMEM;
1473 goto err_out;
1474 }
1475 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
1476 CASE_SENSITIVE, 0, NULL, 0, ctx);
1477 if (unlikely(err))
1478 goto err_out;
1479 /*
1480 * Keep the VM happy. This must be done otherwise the radix-tree tag
1481 * PAGECACHE_TAG_DIRTY remains set even though the page is clean.
1482 */
1483 BUG_ON(PageWriteback(page));
1484 set_page_writeback(page);
1485 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
Anton Altaparmakov07a4e2d2005-01-12 13:08:26 +00001487 i_size = i_size_read(vi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 if (unlikely(attr_len > i_size)) {
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +01001489 /* Race with shrinking truncate or a failed truncate. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 attr_len = i_size;
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +01001491 /*
1492 * If the truncate failed, fix it up now. If a concurrent
1493 * truncate, we do its job, so it does not have to do anything.
1494 */
1495 err = ntfs_resident_attr_value_resize(ctx->mrec, ctx->attr,
1496 attr_len);
1497 /* Shrinking cannot fail. */
1498 BUG_ON(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
Anton Altaparmakovf40661b2005-01-13 16:03:38 +00001500 kaddr = kmap_atomic(page, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 /* Copy the data from the page to the mft record. */
1502 memcpy((u8*)ctx->attr +
1503 le16_to_cpu(ctx->attr->data.resident.value_offset),
1504 kaddr, attr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 /* Zero out of bounds area in the page cache page. */
1506 memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 kunmap_atomic(kaddr, KM_USER0);
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +01001508 flush_dcache_page(page);
Anton Altaparmakov7d0ffdb2005-10-19 12:21:19 +01001509 flush_dcache_mft_record_page(ctx->ntfs_ino);
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +01001510 /* We are done with the page. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 end_page_writeback(page);
Anton Altaparmakovf6098cf2005-09-19 09:41:39 +01001512 /* Finally, mark the mft record dirty, so it gets written back. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 mark_mft_record_dirty(ctx->ntfs_ino);
1514 ntfs_attr_put_search_ctx(ctx);
1515 unmap_mft_record(base_ni);
1516 return 0;
1517err_out:
1518 if (err == -ENOMEM) {
1519 ntfs_warning(vi->i_sb, "Error allocating memory. Redirtying "
1520 "page so we try again later.");
1521 /*
1522 * Put the page back on mapping->dirty_pages, but leave its
1523 * buffers' dirty state as-is.
1524 */
1525 redirty_page_for_writepage(wbc, page);
1526 err = 0;
1527 } else {
1528 ntfs_error(vi->i_sb, "Resident attribute write failed with "
Anton Altaparmakov149f0c52005-01-12 13:52:30 +00001529 "error %i.", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 SetPageError(page);
Anton Altaparmakov149f0c52005-01-12 13:52:30 +00001531 NVolSetErrors(ni->vol);
1532 make_bad_inode(vi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
1534 unlock_page(page);
1535 if (ctx)
1536 ntfs_attr_put_search_ctx(ctx);
1537 if (m)
1538 unmap_mft_record(base_ni);
1539 return err;
1540}
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542#endif /* NTFS_RW */
1543
1544/**
1545 * ntfs_aops - general address space operations for inodes and attributes
1546 */
1547struct address_space_operations ntfs_aops = {
1548 .readpage = ntfs_readpage, /* Fill page with data. */
1549 .sync_page = block_sync_page, /* Currently, just unplugs the
1550 disk request queue. */
1551#ifdef NTFS_RW
1552 .writepage = ntfs_writepage, /* Write dirty page to disk. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553#endif /* NTFS_RW */
1554};
1555
1556/**
1557 * ntfs_mst_aops - general address space operations for mst protecteed inodes
1558 * and attributes
1559 */
1560struct address_space_operations ntfs_mst_aops = {
1561 .readpage = ntfs_readpage, /* Fill page with data. */
1562 .sync_page = block_sync_page, /* Currently, just unplugs the
1563 disk request queue. */
1564#ifdef NTFS_RW
1565 .writepage = ntfs_writepage, /* Write dirty page to disk. */
1566 .set_page_dirty = __set_page_dirty_nobuffers, /* Set the page dirty
1567 without touching the buffers
1568 belonging to the page. */
1569#endif /* NTFS_RW */
1570};
1571
1572#ifdef NTFS_RW
1573
1574/**
1575 * mark_ntfs_record_dirty - mark an ntfs record dirty
1576 * @page: page containing the ntfs record to mark dirty
1577 * @ofs: byte offset within @page at which the ntfs record begins
1578 *
1579 * Set the buffers and the page in which the ntfs record is located dirty.
1580 *
1581 * The latter also marks the vfs inode the ntfs record belongs to dirty
1582 * (I_DIRTY_PAGES only).
1583 *
1584 * If the page does not have buffers, we create them and set them uptodate.
1585 * The page may not be locked which is why we need to handle the buffers under
1586 * the mapping->private_lock. Once the buffers are marked dirty we no longer
1587 * need the lock since try_to_free_buffers() does not free dirty buffers.
1588 */
1589void mark_ntfs_record_dirty(struct page *page, const unsigned int ofs) {
1590 struct address_space *mapping = page->mapping;
1591 ntfs_inode *ni = NTFS_I(mapping->host);
1592 struct buffer_head *bh, *head, *buffers_to_free = NULL;
1593 unsigned int end, bh_size, bh_ofs;
1594
1595 BUG_ON(!PageUptodate(page));
1596 end = ofs + ni->itype.index.block_size;
Anton Altaparmakov78af34f2006-02-24 10:32:33 +00001597 bh_size = VFS_I(ni)->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 spin_lock(&mapping->private_lock);
1599 if (unlikely(!page_has_buffers(page))) {
1600 spin_unlock(&mapping->private_lock);
1601 bh = head = alloc_page_buffers(page, bh_size, 1);
1602 spin_lock(&mapping->private_lock);
1603 if (likely(!page_has_buffers(page))) {
1604 struct buffer_head *tail;
1605
1606 do {
1607 set_buffer_uptodate(bh);
1608 tail = bh;
1609 bh = bh->b_this_page;
1610 } while (bh);
1611 tail->b_this_page = head;
1612 attach_page_buffers(page, head);
1613 } else
1614 buffers_to_free = bh;
1615 }
1616 bh = head = page_buffers(page);
Anton Altaparmakova01ac532005-09-08 22:08:11 +01001617 BUG_ON(!bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 do {
1619 bh_ofs = bh_offset(bh);
1620 if (bh_ofs + bh_size <= ofs)
1621 continue;
1622 if (unlikely(bh_ofs >= end))
1623 break;
1624 set_buffer_dirty(bh);
1625 } while ((bh = bh->b_this_page) != head);
1626 spin_unlock(&mapping->private_lock);
1627 __set_page_dirty_nobuffers(page);
1628 if (unlikely(buffers_to_free)) {
1629 do {
1630 bh = buffers_to_free->b_this_page;
1631 free_buffer_head(buffers_to_free);
1632 buffers_to_free = bh;
1633 } while (buffers_to_free);
1634 }
1635}
1636
1637#endif /* NTFS_RW */