Merge branch 'master' of /usr/src/linux-2.6
[linux-2.6] / fs / ntfs / aops.c
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-2005 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/mm.h>
26 #include <linux/pagemap.h>
27 #include <linux/swap.h>
28 #include <linux/buffer_head.h>
29 #include <linux/writeback.h>
30
31 #include "aops.h"
32 #include "attrib.h"
33 #include "debug.h"
34 #include "inode.h"
35 #include "mft.h"
36 #include "runlist.h"
37 #include "types.h"
38 #include "ntfs.h"
39
40 /**
41  * ntfs_end_buffer_async_read - async io completion for reading attributes
42  * @bh:         buffer head on which io is completed
43  * @uptodate:   whether @bh is now uptodate or not
44  *
45  * Asynchronous I/O completion handler for reading pages belonging to the
46  * attribute address space of an inode.  The inodes can either be files or
47  * directories or they can be fake inodes describing some attribute.
48  *
49  * If NInoMstProtected(), perform the post read mst fixups when all IO on the
50  * page has been completed and mark the page uptodate or set the error bit on
51  * the page.  To determine the size of the records that need fixing up, we
52  * cheat a little bit by setting the index_block_size in ntfs_inode to the ntfs
53  * record size, and index_block_size_bits, to the log(base 2) of the ntfs
54  * record size.
55  */
56 static void ntfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
57 {
58         unsigned long flags;
59         struct buffer_head *first, *tmp;
60         struct page *page;
61         ntfs_inode *ni;
62         int page_uptodate = 1;
63
64         page = bh->b_page;
65         ni = NTFS_I(page->mapping->host);
66
67         if (likely(uptodate)) {
68                 s64 file_ofs, initialized_size;
69
70                 set_buffer_uptodate(bh);
71
72                 file_ofs = ((s64)page->index << PAGE_CACHE_SHIFT) +
73                                 bh_offset(bh);
74                 read_lock_irqsave(&ni->size_lock, flags);
75                 initialized_size = ni->initialized_size;
76                 read_unlock_irqrestore(&ni->size_lock, flags);
77                 /* Check for the current buffer head overflowing. */
78                 if (file_ofs + bh->b_size > initialized_size) {
79                         char *addr;
80                         int ofs = 0;
81
82                         if (file_ofs < initialized_size)
83                                 ofs = initialized_size - file_ofs;
84                         addr = kmap_atomic(page, KM_BIO_SRC_IRQ);
85                         memset(addr + bh_offset(bh) + ofs, 0, bh->b_size - ofs);
86                         flush_dcache_page(page);
87                         kunmap_atomic(addr, KM_BIO_SRC_IRQ);
88                 }
89         } else {
90                 clear_buffer_uptodate(bh);
91                 SetPageError(page);
92                 ntfs_error(ni->vol->sb, "Buffer I/O error, logical block %llu.",
93                                 (unsigned long long)bh->b_blocknr);
94         }
95         first = page_buffers(page);
96         local_irq_save(flags);
97         bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
98         clear_buffer_async_read(bh);
99         unlock_buffer(bh);
100         tmp = bh;
101         do {
102                 if (!buffer_uptodate(tmp))
103                         page_uptodate = 0;
104                 if (buffer_async_read(tmp)) {
105                         if (likely(buffer_locked(tmp)))
106                                 goto still_busy;
107                         /* Async buffers must be locked. */
108                         BUG();
109                 }
110                 tmp = tmp->b_this_page;
111         } while (tmp != bh);
112         bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
113         local_irq_restore(flags);
114         /*
115          * If none of the buffers had errors then we can set the page uptodate,
116          * but we first have to perform the post read mst fixups, if the
117          * attribute is mst protected, i.e. if NInoMstProteced(ni) is true.
118          * Note we ignore fixup errors as those are detected when
119          * map_mft_record() is called which gives us per record granularity
120          * rather than per page granularity.
121          */
122         if (!NInoMstProtected(ni)) {
123                 if (likely(page_uptodate && !PageError(page)))
124                         SetPageUptodate(page);
125         } else {
126                 char *addr;
127                 unsigned int i, recs;
128                 u32 rec_size;
129
130                 rec_size = ni->itype.index.block_size;
131                 recs = PAGE_CACHE_SIZE / rec_size;
132                 /* Should have been verified before we got here... */
133                 BUG_ON(!recs);
134                 addr = kmap_atomic(page, KM_BIO_SRC_IRQ);
135                 for (i = 0; i < recs; i++)
136                         post_read_mst_fixup((NTFS_RECORD*)(addr +
137                                         i * rec_size), rec_size);
138                 flush_dcache_page(page);
139                 kunmap_atomic(addr, KM_BIO_SRC_IRQ);
140                 if (likely(page_uptodate && !PageError(page)))
141                         SetPageUptodate(page);
142         }
143         unlock_page(page);
144         return;
145 still_busy:
146         bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
147         local_irq_restore(flags);
148         return;
149 }
150
151 /**
152  * ntfs_read_block - fill a @page of an address space with data
153  * @page:       page cache page to fill with data
154  *
155  * Fill the page @page of the address space belonging to the @page->host inode.
156  * We read each buffer asynchronously and when all buffers are read in, our io
157  * completion handler ntfs_end_buffer_read_async(), if required, automatically
158  * applies the mst fixups to the page before finally marking it uptodate and
159  * unlocking it.
160  *
161  * We only enforce allocated_size limit because i_size is checked for in
162  * generic_file_read().
163  *
164  * Return 0 on success and -errno on error.
165  *
166  * Contains an adapted version of fs/buffer.c::block_read_full_page().
167  */
168 static int ntfs_read_block(struct page *page)
169 {
170         VCN vcn;
171         LCN lcn;
172         ntfs_inode *ni;
173         ntfs_volume *vol;
174         runlist_element *rl;
175         struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
176         sector_t iblock, lblock, zblock;
177         unsigned long flags;
178         unsigned int blocksize, vcn_ofs;
179         int i, nr;
180         unsigned char blocksize_bits;
181
182         ni = NTFS_I(page->mapping->host);
183         vol = ni->vol;
184
185         /* $MFT/$DATA must have its complete runlist in memory at all times. */
186         BUG_ON(!ni->runlist.rl && !ni->mft_no && !NInoAttr(ni));
187
188         blocksize_bits = VFS_I(ni)->i_blkbits;
189         blocksize = 1 << blocksize_bits;
190
191         if (!page_has_buffers(page)) {
192                 create_empty_buffers(page, blocksize, 0);
193                 if (unlikely(!page_has_buffers(page))) {
194                         unlock_page(page);
195                         return -ENOMEM;
196                 }
197         }
198         bh = head = page_buffers(page);
199         BUG_ON(!bh);
200
201         iblock = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
202         read_lock_irqsave(&ni->size_lock, flags);
203         lblock = (ni->allocated_size + blocksize - 1) >> blocksize_bits;
204         zblock = (ni->initialized_size + blocksize - 1) >> blocksize_bits;
205         read_unlock_irqrestore(&ni->size_lock, flags);
206
207         /* Loop through all the buffers in the page. */
208         rl = NULL;
209         nr = i = 0;
210         do {
211                 u8 *kaddr;
212                 int err;
213
214                 if (unlikely(buffer_uptodate(bh)))
215                         continue;
216                 if (unlikely(buffer_mapped(bh))) {
217                         arr[nr++] = bh;
218                         continue;
219                 }
220                 err = 0;
221                 bh->b_bdev = vol->sb->s_bdev;
222                 /* Is the block within the allowed limits? */
223                 if (iblock < lblock) {
224                         BOOL is_retry = FALSE;
225
226                         /* Convert iblock into corresponding vcn and offset. */
227                         vcn = (VCN)iblock << blocksize_bits >>
228                                         vol->cluster_size_bits;
229                         vcn_ofs = ((VCN)iblock << blocksize_bits) &
230                                         vol->cluster_size_mask;
231                         if (!rl) {
232 lock_retry_remap:
233                                 down_read(&ni->runlist.lock);
234                                 rl = ni->runlist.rl;
235                         }
236                         if (likely(rl != NULL)) {
237                                 /* Seek to element containing target vcn. */
238                                 while (rl->length && rl[1].vcn <= vcn)
239                                         rl++;
240                                 lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
241                         } else
242                                 lcn = LCN_RL_NOT_MAPPED;
243                         /* Successful remap. */
244                         if (lcn >= 0) {
245                                 /* Setup buffer head to correct block. */
246                                 bh->b_blocknr = ((lcn << vol->cluster_size_bits)
247                                                 + vcn_ofs) >> blocksize_bits;
248                                 set_buffer_mapped(bh);
249                                 /* Only read initialized data blocks. */
250                                 if (iblock < zblock) {
251                                         arr[nr++] = bh;
252                                         continue;
253                                 }
254                                 /* Fully non-initialized data block, zero it. */
255                                 goto handle_zblock;
256                         }
257                         /* It is a hole, need to zero it. */
258                         if (lcn == LCN_HOLE)
259                                 goto handle_hole;
260                         /* If first try and runlist unmapped, map and retry. */
261                         if (!is_retry && lcn == LCN_RL_NOT_MAPPED) {
262                                 is_retry = TRUE;
263                                 /*
264                                  * Attempt to map runlist, dropping lock for
265                                  * the duration.
266                                  */
267                                 up_read(&ni->runlist.lock);
268                                 err = ntfs_map_runlist(ni, vcn);
269                                 if (likely(!err))
270                                         goto lock_retry_remap;
271                                 rl = NULL;
272                         } else if (!rl)
273                                 up_read(&ni->runlist.lock);
274                         /*
275                          * If buffer is outside the runlist, treat it as a
276                          * hole.  This can happen due to concurrent truncate
277                          * for example.
278                          */
279                         if (err == -ENOENT || lcn == LCN_ENOENT) {
280                                 err = 0;
281                                 goto handle_hole;
282                         }
283                         /* Hard error, zero out region. */
284                         if (!err)
285                                 err = -EIO;
286                         bh->b_blocknr = -1;
287                         SetPageError(page);
288                         ntfs_error(vol->sb, "Failed to read from inode 0x%lx, "
289                                         "attribute type 0x%x, vcn 0x%llx, "
290                                         "offset 0x%x because its location on "
291                                         "disk could not be determined%s "
292                                         "(error code %i).", ni->mft_no,
293                                         ni->type, (unsigned long long)vcn,
294                                         vcn_ofs, is_retry ? " even after "
295                                         "retrying" : "", err);
296                 }
297                 /*
298                  * Either iblock was outside lblock limits or
299                  * ntfs_rl_vcn_to_lcn() returned error.  Just zero that portion
300                  * of the page and set the buffer uptodate.
301                  */
302 handle_hole:
303                 bh->b_blocknr = -1UL;
304                 clear_buffer_mapped(bh);
305 handle_zblock:
306                 kaddr = kmap_atomic(page, KM_USER0);
307                 memset(kaddr + i * blocksize, 0, blocksize);
308                 kunmap_atomic(kaddr, KM_USER0);
309                 flush_dcache_page(page);
310                 if (likely(!err))
311                         set_buffer_uptodate(bh);
312         } while (i++, iblock++, (bh = bh->b_this_page) != head);
313
314         /* Release the lock if we took it. */
315         if (rl)
316                 up_read(&ni->runlist.lock);
317
318         /* Check we have at least one buffer ready for i/o. */
319         if (nr) {
320                 struct buffer_head *tbh;
321
322                 /* Lock the buffers. */
323                 for (i = 0; i < nr; i++) {
324                         tbh = arr[i];
325                         lock_buffer(tbh);
326                         tbh->b_end_io = ntfs_end_buffer_async_read;
327                         set_buffer_async_read(tbh);
328                 }
329                 /* Finally, start i/o on the buffers. */
330                 for (i = 0; i < nr; i++) {
331                         tbh = arr[i];
332                         if (likely(!buffer_uptodate(tbh)))
333                                 submit_bh(READ, tbh);
334                         else
335                                 ntfs_end_buffer_async_read(tbh, 1);
336                 }
337                 return 0;
338         }
339         /* No i/o was scheduled on any of the buffers. */
340         if (likely(!PageError(page)))
341                 SetPageUptodate(page);
342         else /* Signal synchronous i/o error. */
343                 nr = -EIO;
344         unlock_page(page);
345         return nr;
346 }
347
348 /**
349  * ntfs_readpage - fill a @page of a @file with data from the device
350  * @file:       open file to which the page @page belongs or NULL
351  * @page:       page cache page to fill with data
352  *
353  * For non-resident attributes, ntfs_readpage() fills the @page of the open
354  * file @file by calling the ntfs version of the generic block_read_full_page()
355  * function, ntfs_read_block(), which in turn creates and reads in the buffers
356  * associated with the page asynchronously.
357  *
358  * For resident attributes, OTOH, ntfs_readpage() fills @page by copying the
359  * data from the mft record (which at this stage is most likely in memory) and
360  * fills the remainder with zeroes. Thus, in this case, I/O is synchronous, as
361  * even if the mft record is not cached at this point in time, we need to wait
362  * for it to be read in before we can do the copy.
363  *
364  * Return 0 on success and -errno on error.
365  */
366 static int ntfs_readpage(struct file *file, struct page *page)
367 {
368         ntfs_inode *ni, *base_ni;
369         u8 *kaddr;
370         ntfs_attr_search_ctx *ctx;
371         MFT_RECORD *mrec;
372         unsigned long flags;
373         u32 attr_len;
374         int err = 0;
375
376 retry_readpage:
377         BUG_ON(!PageLocked(page));
378         /*
379          * This can potentially happen because we clear PageUptodate() during
380          * ntfs_writepage() of MstProtected() attributes.
381          */
382         if (PageUptodate(page)) {
383                 unlock_page(page);
384                 return 0;
385         }
386         ni = NTFS_I(page->mapping->host);
387         /*
388          * Only $DATA attributes can be encrypted and only unnamed $DATA
389          * attributes can be compressed.  Index root can have the flags set but
390          * this means to create compressed/encrypted files, not that the
391          * attribute is compressed/encrypted.
392          */
393         if (ni->type != AT_INDEX_ROOT) {
394                 /* If attribute is encrypted, deny access, just like NT4. */
395                 if (NInoEncrypted(ni)) {
396                         BUG_ON(ni->type != AT_DATA);
397                         err = -EACCES;
398                         goto err_out;
399                 }
400                 /* Compressed data streams are handled in compress.c. */
401                 if (NInoNonResident(ni) && NInoCompressed(ni)) {
402                         BUG_ON(ni->type != AT_DATA);
403                         BUG_ON(ni->name_len);
404                         return ntfs_read_compressed_block(page);
405                 }
406         }
407         /* NInoNonResident() == NInoIndexAllocPresent() */
408         if (NInoNonResident(ni)) {
409                 /* Normal, non-resident data stream. */
410                 return ntfs_read_block(page);
411         }
412         /*
413          * Attribute is resident, implying it is not compressed or encrypted.
414          * This also means the attribute is smaller than an mft record and
415          * hence smaller than a page, so can simply zero out any pages with
416          * index above 0.  Note the attribute can actually be marked compressed
417          * but if it is resident the actual data is not compressed so we are
418          * ok to ignore the compressed flag here.
419          */
420         if (unlikely(page->index > 0)) {
421                 kaddr = kmap_atomic(page, KM_USER0);
422                 memset(kaddr, 0, PAGE_CACHE_SIZE);
423                 flush_dcache_page(page);
424                 kunmap_atomic(kaddr, KM_USER0);
425                 goto done;
426         }
427         if (!NInoAttr(ni))
428                 base_ni = ni;
429         else
430                 base_ni = ni->ext.base_ntfs_ino;
431         /* Map, pin, and lock the mft record. */
432         mrec = map_mft_record(base_ni);
433         if (IS_ERR(mrec)) {
434                 err = PTR_ERR(mrec);
435                 goto err_out;
436         }
437         /*
438          * If a parallel write made the attribute non-resident, drop the mft
439          * record and retry the readpage.
440          */
441         if (unlikely(NInoNonResident(ni))) {
442                 unmap_mft_record(base_ni);
443                 goto retry_readpage;
444         }
445         ctx = ntfs_attr_get_search_ctx(base_ni, mrec);
446         if (unlikely(!ctx)) {
447                 err = -ENOMEM;
448                 goto unm_err_out;
449         }
450         err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
451                         CASE_SENSITIVE, 0, NULL, 0, ctx);
452         if (unlikely(err))
453                 goto put_unm_err_out;
454         attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
455         read_lock_irqsave(&ni->size_lock, flags);
456         if (unlikely(attr_len > ni->initialized_size))
457                 attr_len = ni->initialized_size;
458         read_unlock_irqrestore(&ni->size_lock, flags);
459         kaddr = kmap_atomic(page, KM_USER0);
460         /* Copy the data to the page. */
461         memcpy(kaddr, (u8*)ctx->attr +
462                         le16_to_cpu(ctx->attr->data.resident.value_offset),
463                         attr_len);
464         /* Zero the remainder of the page. */
465         memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
466         flush_dcache_page(page);
467         kunmap_atomic(kaddr, KM_USER0);
468 put_unm_err_out:
469         ntfs_attr_put_search_ctx(ctx);
470 unm_err_out:
471         unmap_mft_record(base_ni);
472 done:
473         SetPageUptodate(page);
474 err_out:
475         unlock_page(page);
476         return err;
477 }
478
479 #ifdef NTFS_RW
480
481 /**
482  * ntfs_write_block - write a @page to the backing store
483  * @page:       page cache page to write out
484  * @wbc:        writeback control structure
485  *
486  * This function is for writing pages belonging to non-resident, non-mst
487  * protected attributes to their backing store.
488  *
489  * For a page with buffers, map and write the dirty buffers asynchronously
490  * under page writeback. For a page without buffers, create buffers for the
491  * page, then proceed as above.
492  *
493  * If a page doesn't have buffers the page dirty state is definitive. If a page
494  * does have buffers, the page dirty state is just a hint, and the buffer dirty
495  * state is definitive. (A hint which has rules: dirty buffers against a clean
496  * page is illegal. Other combinations are legal and need to be handled. In
497  * particular a dirty page containing clean buffers for example.)
498  *
499  * Return 0 on success and -errno on error.
500  *
501  * Based on ntfs_read_block() and __block_write_full_page().
502  */
503 static int ntfs_write_block(struct page *page, struct writeback_control *wbc)
504 {
505         VCN vcn;
506         LCN lcn;
507         s64 initialized_size;
508         loff_t i_size;
509         sector_t block, dblock, iblock;
510         struct inode *vi;
511         ntfs_inode *ni;
512         ntfs_volume *vol;
513         runlist_element *rl;
514         struct buffer_head *bh, *head;
515         unsigned long flags;
516         unsigned int blocksize, vcn_ofs;
517         int err;
518         BOOL need_end_writeback;
519         unsigned char blocksize_bits;
520
521         vi = page->mapping->host;
522         ni = NTFS_I(vi);
523         vol = ni->vol;
524
525         ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
526                         "0x%lx.", ni->mft_no, ni->type, page->index);
527
528         BUG_ON(!NInoNonResident(ni));
529         BUG_ON(NInoMstProtected(ni));
530
531         blocksize_bits = vi->i_blkbits;
532         blocksize = 1 << blocksize_bits;
533
534         if (!page_has_buffers(page)) {
535                 BUG_ON(!PageUptodate(page));
536                 create_empty_buffers(page, blocksize,
537                                 (1 << BH_Uptodate) | (1 << BH_Dirty));
538                 if (unlikely(!page_has_buffers(page))) {
539                         ntfs_warning(vol->sb, "Error allocating page "
540                                         "buffers.  Redirtying page so we try "
541                                         "again later.");
542                         /*
543                          * Put the page back on mapping->dirty_pages, but leave
544                          * its buffers' dirty state as-is.
545                          */
546                         redirty_page_for_writepage(wbc, page);
547                         unlock_page(page);
548                         return 0;
549                 }
550         }
551         bh = head = page_buffers(page);
552         BUG_ON(!bh);
553
554         /* NOTE: Different naming scheme to ntfs_read_block()! */
555
556         /* The first block in the page. */
557         block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
558
559         read_lock_irqsave(&ni->size_lock, flags);
560         i_size = i_size_read(vi);
561         initialized_size = ni->initialized_size;
562         read_unlock_irqrestore(&ni->size_lock, flags);
563
564         /* The first out of bounds block for the data size. */
565         dblock = (i_size + blocksize - 1) >> blocksize_bits;
566
567         /* The last (fully or partially) initialized block. */
568         iblock = initialized_size >> blocksize_bits;
569
570         /*
571          * Be very careful.  We have no exclusion from __set_page_dirty_buffers
572          * here, and the (potentially unmapped) buffers may become dirty at
573          * any time.  If a buffer becomes dirty here after we've inspected it
574          * then we just miss that fact, and the page stays dirty.
575          *
576          * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
577          * handle that here by just cleaning them.
578          */
579
580         /*
581          * Loop through all the buffers in the page, mapping all the dirty
582          * buffers to disk addresses and handling any aliases from the
583          * underlying block device's mapping.
584          */
585         rl = NULL;
586         err = 0;
587         do {
588                 BOOL is_retry = FALSE;
589
590                 if (unlikely(block >= dblock)) {
591                         /*
592                          * Mapped buffers outside i_size will occur, because
593                          * this page can be outside i_size when there is a
594                          * truncate in progress. The contents of such buffers
595                          * were zeroed by ntfs_writepage().
596                          *
597                          * FIXME: What about the small race window where
598                          * ntfs_writepage() has not done any clearing because
599                          * the page was within i_size but before we get here,
600                          * vmtruncate() modifies i_size?
601                          */
602                         clear_buffer_dirty(bh);
603                         set_buffer_uptodate(bh);
604                         continue;
605                 }
606
607                 /* Clean buffers are not written out, so no need to map them. */
608                 if (!buffer_dirty(bh))
609                         continue;
610
611                 /* Make sure we have enough initialized size. */
612                 if (unlikely((block >= iblock) &&
613                                 (initialized_size < i_size))) {
614                         /*
615                          * If this page is fully outside initialized size, zero
616                          * out all pages between the current initialized size
617                          * and the current page. Just use ntfs_readpage() to do
618                          * the zeroing transparently.
619                          */
620                         if (block > iblock) {
621                                 // TODO:
622                                 // For each page do:
623                                 // - read_cache_page()
624                                 // Again for each page do:
625                                 // - wait_on_page_locked()
626                                 // - Check (PageUptodate(page) &&
627                                 //                      !PageError(page))
628                                 // Update initialized size in the attribute and
629                                 // in the inode.
630                                 // Again, for each page do:
631                                 //      __set_page_dirty_buffers();
632                                 // page_cache_release()
633                                 // We don't need to wait on the writes.
634                                 // Update iblock.
635                         }
636                         /*
637                          * The current page straddles initialized size. Zero
638                          * all non-uptodate buffers and set them uptodate (and
639                          * dirty?). Note, there aren't any non-uptodate buffers
640                          * if the page is uptodate.
641                          * FIXME: For an uptodate page, the buffers may need to
642                          * be written out because they were not initialized on
643                          * disk before.
644                          */
645                         if (!PageUptodate(page)) {
646                                 // TODO:
647                                 // Zero any non-uptodate buffers up to i_size.
648                                 // Set them uptodate and dirty.
649                         }
650                         // TODO:
651                         // Update initialized size in the attribute and in the
652                         // inode (up to i_size).
653                         // Update iblock.
654                         // FIXME: This is inefficient. Try to batch the two
655                         // size changes to happen in one go.
656                         ntfs_error(vol->sb, "Writing beyond initialized size "
657                                         "is not supported yet. Sorry.");
658                         err = -EOPNOTSUPP;
659                         break;
660                         // Do NOT set_buffer_new() BUT DO clear buffer range
661                         // outside write request range.
662                         // set_buffer_uptodate() on complete buffers as well as
663                         // set_buffer_dirty().
664                 }
665
666                 /* No need to map buffers that are already mapped. */
667                 if (buffer_mapped(bh))
668                         continue;
669
670                 /* Unmapped, dirty buffer. Need to map it. */
671                 bh->b_bdev = vol->sb->s_bdev;
672
673                 /* Convert block into corresponding vcn and offset. */
674                 vcn = (VCN)block << blocksize_bits;
675                 vcn_ofs = vcn & vol->cluster_size_mask;
676                 vcn >>= vol->cluster_size_bits;
677                 if (!rl) {
678 lock_retry_remap:
679                         down_read(&ni->runlist.lock);
680                         rl = ni->runlist.rl;
681                 }
682                 if (likely(rl != NULL)) {
683                         /* Seek to element containing target vcn. */
684                         while (rl->length && rl[1].vcn <= vcn)
685                                 rl++;
686                         lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
687                 } else
688                         lcn = LCN_RL_NOT_MAPPED;
689                 /* Successful remap. */
690                 if (lcn >= 0) {
691                         /* Setup buffer head to point to correct block. */
692                         bh->b_blocknr = ((lcn << vol->cluster_size_bits) +
693                                         vcn_ofs) >> blocksize_bits;
694                         set_buffer_mapped(bh);
695                         continue;
696                 }
697                 /* It is a hole, need to instantiate it. */
698                 if (lcn == LCN_HOLE) {
699                         u8 *kaddr;
700                         unsigned long *bpos, *bend;
701
702                         /* Check if the buffer is zero. */
703                         kaddr = kmap_atomic(page, KM_USER0);
704                         bpos = (unsigned long *)(kaddr + bh_offset(bh));
705                         bend = (unsigned long *)((u8*)bpos + blocksize);
706                         do {
707                                 if (unlikely(*bpos))
708                                         break;
709                         } while (likely(++bpos < bend));
710                         kunmap_atomic(kaddr, KM_USER0);
711                         if (bpos == bend) {
712                                 /*
713                                  * Buffer is zero and sparse, no need to write
714                                  * it.
715                                  */
716                                 bh->b_blocknr = -1;
717                                 clear_buffer_dirty(bh);
718                                 continue;
719                         }
720                         // TODO: Instantiate the hole.
721                         // clear_buffer_new(bh);
722                         // unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
723                         ntfs_error(vol->sb, "Writing into sparse regions is "
724                                         "not supported yet. Sorry.");
725                         err = -EOPNOTSUPP;
726                         break;
727                 }
728                 /* If first try and runlist unmapped, map and retry. */
729                 if (!is_retry && lcn == LCN_RL_NOT_MAPPED) {
730                         is_retry = TRUE;
731                         /*
732                          * Attempt to map runlist, dropping lock for
733                          * the duration.
734                          */
735                         up_read(&ni->runlist.lock);
736                         err = ntfs_map_runlist(ni, vcn);
737                         if (likely(!err))
738                                 goto lock_retry_remap;
739                         rl = NULL;
740                 } else if (!rl)
741                         up_read(&ni->runlist.lock);
742                 /*
743                  * If buffer is outside the runlist, truncate has cut it out
744                  * of the runlist.  Just clean and clear the buffer and set it
745                  * uptodate so it can get discarded by the VM.
746                  */
747                 if (err == -ENOENT || lcn == LCN_ENOENT) {
748                         u8 *kaddr;
749
750                         bh->b_blocknr = -1;
751                         clear_buffer_dirty(bh);
752                         kaddr = kmap_atomic(page, KM_USER0);
753                         memset(kaddr + bh_offset(bh), 0, blocksize);
754                         kunmap_atomic(kaddr, KM_USER0);
755                         flush_dcache_page(page);
756                         set_buffer_uptodate(bh);
757                         err = 0;
758                         continue;
759                 }
760                 /* Failed to map the buffer, even after retrying. */
761                 if (!err)
762                         err = -EIO;
763                 bh->b_blocknr = -1;
764                 ntfs_error(vol->sb, "Failed to write to inode 0x%lx, "
765                                 "attribute type 0x%x, vcn 0x%llx, offset 0x%x "
766                                 "because its location on disk could not be "
767                                 "determined%s (error code %i).", ni->mft_no,
768                                 ni->type, (unsigned long long)vcn,
769                                 vcn_ofs, is_retry ? " even after "
770                                 "retrying" : "", err);
771                 break;
772         } while (block++, (bh = bh->b_this_page) != head);
773
774         /* Release the lock if we took it. */
775         if (rl)
776                 up_read(&ni->runlist.lock);
777
778         /* For the error case, need to reset bh to the beginning. */
779         bh = head;
780
781         /* Just an optimization, so ->readpage() is not called later. */
782         if (unlikely(!PageUptodate(page))) {
783                 int uptodate = 1;
784                 do {
785                         if (!buffer_uptodate(bh)) {
786                                 uptodate = 0;
787                                 bh = head;
788                                 break;
789                         }
790                 } while ((bh = bh->b_this_page) != head);
791                 if (uptodate)
792                         SetPageUptodate(page);
793         }
794
795         /* Setup all mapped, dirty buffers for async write i/o. */
796         do {
797                 if (buffer_mapped(bh) && buffer_dirty(bh)) {
798                         lock_buffer(bh);
799                         if (test_clear_buffer_dirty(bh)) {
800                                 BUG_ON(!buffer_uptodate(bh));
801                                 mark_buffer_async_write(bh);
802                         } else
803                                 unlock_buffer(bh);
804                 } else if (unlikely(err)) {
805                         /*
806                          * For the error case. The buffer may have been set
807                          * dirty during attachment to a dirty page.
808                          */
809                         if (err != -ENOMEM)
810                                 clear_buffer_dirty(bh);
811                 }
812         } while ((bh = bh->b_this_page) != head);
813
814         if (unlikely(err)) {
815                 // TODO: Remove the -EOPNOTSUPP check later on...
816                 if (unlikely(err == -EOPNOTSUPP))
817                         err = 0;
818                 else if (err == -ENOMEM) {
819                         ntfs_warning(vol->sb, "Error allocating memory. "
820                                         "Redirtying page so we try again "
821                                         "later.");
822                         /*
823                          * Put the page back on mapping->dirty_pages, but
824                          * leave its buffer's dirty state as-is.
825                          */
826                         redirty_page_for_writepage(wbc, page);
827                         err = 0;
828                 } else
829                         SetPageError(page);
830         }
831
832         BUG_ON(PageWriteback(page));
833         set_page_writeback(page);       /* Keeps try_to_free_buffers() away. */
834
835         /* Submit the prepared buffers for i/o. */
836         need_end_writeback = TRUE;
837         do {
838                 struct buffer_head *next = bh->b_this_page;
839                 if (buffer_async_write(bh)) {
840                         submit_bh(WRITE, bh);
841                         need_end_writeback = FALSE;
842                 }
843                 bh = next;
844         } while (bh != head);
845         unlock_page(page);
846
847         /* If no i/o was started, need to end_page_writeback(). */
848         if (unlikely(need_end_writeback))
849                 end_page_writeback(page);
850
851         ntfs_debug("Done.");
852         return err;
853 }
854
855 /**
856  * ntfs_write_mst_block - write a @page to the backing store
857  * @page:       page cache page to write out
858  * @wbc:        writeback control structure
859  *
860  * This function is for writing pages belonging to non-resident, mst protected
861  * attributes to their backing store.  The only supported attributes are index
862  * allocation and $MFT/$DATA.  Both directory inodes and index inodes are
863  * supported for the index allocation case.
864  *
865  * The page must remain locked for the duration of the write because we apply
866  * the mst fixups, write, and then undo the fixups, so if we were to unlock the
867  * page before undoing the fixups, any other user of the page will see the
868  * page contents as corrupt.
869  *
870  * We clear the page uptodate flag for the duration of the function to ensure
871  * exclusion for the $MFT/$DATA case against someone mapping an mft record we
872  * are about to apply the mst fixups to.
873  *
874  * Return 0 on success and -errno on error.
875  *
876  * Based on ntfs_write_block(), ntfs_mft_writepage(), and
877  * write_mft_record_nolock().
878  */
879 static int ntfs_write_mst_block(struct page *page,
880                 struct writeback_control *wbc)
881 {
882         sector_t block, dblock, rec_block;
883         struct inode *vi = page->mapping->host;
884         ntfs_inode *ni = NTFS_I(vi);
885         ntfs_volume *vol = ni->vol;
886         u8 *kaddr;
887         unsigned int rec_size = ni->itype.index.block_size;
888         ntfs_inode *locked_nis[PAGE_CACHE_SIZE / rec_size];
889         struct buffer_head *bh, *head, *tbh, *rec_start_bh;
890         struct buffer_head *bhs[MAX_BUF_PER_PAGE];
891         runlist_element *rl;
892         int i, nr_locked_nis, nr_recs, nr_bhs, max_bhs, bhs_per_rec, err, err2;
893         unsigned bh_size, rec_size_bits;
894         BOOL sync, is_mft, page_is_dirty, rec_is_dirty;
895         unsigned char bh_size_bits;
896
897         ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
898                         "0x%lx.", vi->i_ino, ni->type, page->index);
899         BUG_ON(!NInoNonResident(ni));
900         BUG_ON(!NInoMstProtected(ni));
901         is_mft = (S_ISREG(vi->i_mode) && !vi->i_ino);
902         /*
903          * NOTE: ntfs_write_mst_block() would be called for $MFTMirr if a page
904          * in its page cache were to be marked dirty.  However this should
905          * never happen with the current driver and considering we do not
906          * handle this case here we do want to BUG(), at least for now.
907          */
908         BUG_ON(!(is_mft || S_ISDIR(vi->i_mode) ||
909                         (NInoAttr(ni) && ni->type == AT_INDEX_ALLOCATION)));
910         bh_size_bits = vi->i_blkbits;
911         bh_size = 1 << bh_size_bits;
912         max_bhs = PAGE_CACHE_SIZE / bh_size;
913         BUG_ON(!max_bhs);
914         BUG_ON(max_bhs > MAX_BUF_PER_PAGE);
915
916         /* Were we called for sync purposes? */
917         sync = (wbc->sync_mode == WB_SYNC_ALL);
918
919         /* Make sure we have mapped buffers. */
920         bh = head = page_buffers(page);
921         BUG_ON(!bh);
922
923         rec_size_bits = ni->itype.index.block_size_bits;
924         BUG_ON(!(PAGE_CACHE_SIZE >> rec_size_bits));
925         bhs_per_rec = rec_size >> bh_size_bits;
926         BUG_ON(!bhs_per_rec);
927
928         /* The first block in the page. */
929         rec_block = block = (sector_t)page->index <<
930                         (PAGE_CACHE_SHIFT - bh_size_bits);
931
932         /* The first out of bounds block for the data size. */
933         dblock = (i_size_read(vi) + bh_size - 1) >> bh_size_bits;
934
935         rl = NULL;
936         err = err2 = nr_bhs = nr_recs = nr_locked_nis = 0;
937         page_is_dirty = rec_is_dirty = FALSE;
938         rec_start_bh = NULL;
939         do {
940                 BOOL is_retry = FALSE;
941
942                 if (likely(block < rec_block)) {
943                         if (unlikely(block >= dblock)) {
944                                 clear_buffer_dirty(bh);
945                                 set_buffer_uptodate(bh);
946                                 continue;
947                         }
948                         /*
949                          * This block is not the first one in the record.  We
950                          * ignore the buffer's dirty state because we could
951                          * have raced with a parallel mark_ntfs_record_dirty().
952                          */
953                         if (!rec_is_dirty)
954                                 continue;
955                         if (unlikely(err2)) {
956                                 if (err2 != -ENOMEM)
957                                         clear_buffer_dirty(bh);
958                                 continue;
959                         }
960                 } else /* if (block == rec_block) */ {
961                         BUG_ON(block > rec_block);
962                         /* This block is the first one in the record. */
963                         rec_block += bhs_per_rec;
964                         err2 = 0;
965                         if (unlikely(block >= dblock)) {
966                                 clear_buffer_dirty(bh);
967                                 continue;
968                         }
969                         if (!buffer_dirty(bh)) {
970                                 /* Clean records are not written out. */
971                                 rec_is_dirty = FALSE;
972                                 continue;
973                         }
974                         rec_is_dirty = TRUE;
975                         rec_start_bh = bh;
976                 }
977                 /* Need to map the buffer if it is not mapped already. */
978                 if (unlikely(!buffer_mapped(bh))) {
979                         VCN vcn;
980                         LCN lcn;
981                         unsigned int vcn_ofs;
982
983                         bh->b_bdev = vol->sb->s_bdev;
984                         /* Obtain the vcn and offset of the current block. */
985                         vcn = (VCN)block << bh_size_bits;
986                         vcn_ofs = vcn & vol->cluster_size_mask;
987                         vcn >>= vol->cluster_size_bits;
988                         if (!rl) {
989 lock_retry_remap:
990                                 down_read(&ni->runlist.lock);
991                                 rl = ni->runlist.rl;
992                         }
993                         if (likely(rl != NULL)) {
994                                 /* Seek to element containing target vcn. */
995                                 while (rl->length && rl[1].vcn <= vcn)
996                                         rl++;
997                                 lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
998                         } else
999                                 lcn = LCN_RL_NOT_MAPPED;
1000                         /* Successful remap. */
1001                         if (likely(lcn >= 0)) {
1002                                 /* Setup buffer head to correct block. */
1003                                 bh->b_blocknr = ((lcn <<
1004                                                 vol->cluster_size_bits) +
1005                                                 vcn_ofs) >> bh_size_bits;
1006                                 set_buffer_mapped(bh);
1007                         } else {
1008                                 /*
1009                                  * Remap failed.  Retry to map the runlist once
1010                                  * unless we are working on $MFT which always
1011                                  * has the whole of its runlist in memory.
1012                                  */
1013                                 if (!is_mft && !is_retry &&
1014                                                 lcn == LCN_RL_NOT_MAPPED) {
1015                                         is_retry = TRUE;
1016                                         /*
1017                                          * Attempt to map runlist, dropping
1018                                          * lock for the duration.
1019                                          */
1020                                         up_read(&ni->runlist.lock);
1021                                         err2 = ntfs_map_runlist(ni, vcn);
1022                                         if (likely(!err2))
1023                                                 goto lock_retry_remap;
1024                                         if (err2 == -ENOMEM)
1025                                                 page_is_dirty = TRUE;
1026                                         lcn = err2;
1027                                 } else {
1028                                         err2 = -EIO;
1029                                         if (!rl)
1030                                                 up_read(&ni->runlist.lock);
1031                                 }
1032                                 /* Hard error.  Abort writing this record. */
1033                                 if (!err || err == -ENOMEM)
1034                                         err = err2;
1035                                 bh->b_blocknr = -1;
1036                                 ntfs_error(vol->sb, "Cannot write ntfs record "
1037                                                 "0x%llx (inode 0x%lx, "
1038                                                 "attribute type 0x%x) because "
1039                                                 "its location on disk could "
1040                                                 "not be determined (error "
1041                                                 "code %lli).",
1042                                                 (long long)block <<
1043                                                 bh_size_bits >>
1044                                                 vol->mft_record_size_bits,
1045                                                 ni->mft_no, ni->type,
1046                                                 (long long)lcn);
1047                                 /*
1048                                  * If this is not the first buffer, remove the
1049                                  * buffers in this record from the list of
1050                                  * buffers to write and clear their dirty bit
1051                                  * if not error -ENOMEM.
1052                                  */
1053                                 if (rec_start_bh != bh) {
1054                                         while (bhs[--nr_bhs] != rec_start_bh)
1055                                                 ;
1056                                         if (err2 != -ENOMEM) {
1057                                                 do {
1058                                                         clear_buffer_dirty(
1059                                                                 rec_start_bh);
1060                                                 } while ((rec_start_bh =
1061                                                                 rec_start_bh->
1062                                                                 b_this_page) !=
1063                                                                 bh);
1064                                         }
1065                                 }
1066                                 continue;
1067                         }
1068                 }
1069                 BUG_ON(!buffer_uptodate(bh));
1070                 BUG_ON(nr_bhs >= max_bhs);
1071                 bhs[nr_bhs++] = bh;
1072         } while (block++, (bh = bh->b_this_page) != head);
1073         if (unlikely(rl))
1074                 up_read(&ni->runlist.lock);
1075         /* If there were no dirty buffers, we are done. */
1076         if (!nr_bhs)
1077                 goto done;
1078         /* Map the page so we can access its contents. */
1079         kaddr = kmap(page);
1080         /* Clear the page uptodate flag whilst the mst fixups are applied. */
1081         BUG_ON(!PageUptodate(page));
1082         ClearPageUptodate(page);
1083         for (i = 0; i < nr_bhs; i++) {
1084                 unsigned int ofs;
1085
1086                 /* Skip buffers which are not at the beginning of records. */
1087                 if (i % bhs_per_rec)
1088                         continue;
1089                 tbh = bhs[i];
1090                 ofs = bh_offset(tbh);
1091                 if (is_mft) {
1092                         ntfs_inode *tni;
1093                         unsigned long mft_no;
1094
1095                         /* Get the mft record number. */
1096                         mft_no = (((s64)page->index << PAGE_CACHE_SHIFT) + ofs)
1097                                         >> rec_size_bits;
1098                         /* Check whether to write this mft record. */
1099                         tni = NULL;
1100                         if (!ntfs_may_write_mft_record(vol, mft_no,
1101                                         (MFT_RECORD*)(kaddr + ofs), &tni)) {
1102                                 /*
1103                                  * The record should not be written.  This
1104                                  * means we need to redirty the page before
1105                                  * returning.
1106                                  */
1107                                 page_is_dirty = TRUE;
1108                                 /*
1109                                  * Remove the buffers in this mft record from
1110                                  * the list of buffers to write.
1111                                  */
1112                                 do {
1113                                         bhs[i] = NULL;
1114                                 } while (++i % bhs_per_rec);
1115                                 continue;
1116                         }
1117                         /*
1118                          * The record should be written.  If a locked ntfs
1119                          * inode was returned, add it to the array of locked
1120                          * ntfs inodes.
1121                          */
1122                         if (tni)
1123                                 locked_nis[nr_locked_nis++] = tni;
1124                 }
1125                 /* Apply the mst protection fixups. */
1126                 err2 = pre_write_mst_fixup((NTFS_RECORD*)(kaddr + ofs),
1127                                 rec_size);
1128                 if (unlikely(err2)) {
1129                         if (!err || err == -ENOMEM)
1130                                 err = -EIO;
1131                         ntfs_error(vol->sb, "Failed to apply mst fixups "
1132                                         "(inode 0x%lx, attribute type 0x%x, "
1133                                         "page index 0x%lx, page offset 0x%x)!"
1134                                         "  Unmount and run chkdsk.", vi->i_ino,
1135                                         ni->type, page->index, ofs);
1136                         /*
1137                          * Mark all the buffers in this record clean as we do
1138                          * not want to write corrupt data to disk.
1139                          */
1140                         do {
1141                                 clear_buffer_dirty(bhs[i]);
1142                                 bhs[i] = NULL;
1143                         } while (++i % bhs_per_rec);
1144                         continue;
1145                 }
1146                 nr_recs++;
1147         }
1148         /* If no records are to be written out, we are done. */
1149         if (!nr_recs)
1150                 goto unm_done;
1151         flush_dcache_page(page);
1152         /* Lock buffers and start synchronous write i/o on them. */
1153         for (i = 0; i < nr_bhs; i++) {
1154                 tbh = bhs[i];
1155                 if (!tbh)
1156                         continue;
1157                 if (unlikely(test_set_buffer_locked(tbh)))
1158                         BUG();
1159                 /* The buffer dirty state is now irrelevant, just clean it. */
1160                 clear_buffer_dirty(tbh);
1161                 BUG_ON(!buffer_uptodate(tbh));
1162                 BUG_ON(!buffer_mapped(tbh));
1163                 get_bh(tbh);
1164                 tbh->b_end_io = end_buffer_write_sync;
1165                 submit_bh(WRITE, tbh);
1166         }
1167         /* Synchronize the mft mirror now if not @sync. */
1168         if (is_mft && !sync)
1169                 goto do_mirror;
1170 do_wait:
1171         /* Wait on i/o completion of buffers. */
1172         for (i = 0; i < nr_bhs; i++) {
1173                 tbh = bhs[i];
1174                 if (!tbh)
1175                         continue;
1176                 wait_on_buffer(tbh);
1177                 if (unlikely(!buffer_uptodate(tbh))) {
1178                         ntfs_error(vol->sb, "I/O error while writing ntfs "
1179                                         "record buffer (inode 0x%lx, "
1180                                         "attribute type 0x%x, page index "
1181                                         "0x%lx, page offset 0x%lx)!  Unmount "
1182                                         "and run chkdsk.", vi->i_ino, ni->type,
1183                                         page->index, bh_offset(tbh));
1184                         if (!err || err == -ENOMEM)
1185                                 err = -EIO;
1186                         /*
1187                          * Set the buffer uptodate so the page and buffer
1188                          * states do not become out of sync.
1189                          */
1190                         set_buffer_uptodate(tbh);
1191                 }
1192         }
1193         /* If @sync, now synchronize the mft mirror. */
1194         if (is_mft && sync) {
1195 do_mirror:
1196                 for (i = 0; i < nr_bhs; i++) {
1197                         unsigned long mft_no;
1198                         unsigned int ofs;
1199
1200                         /*
1201                          * Skip buffers which are not at the beginning of
1202                          * records.
1203                          */
1204                         if (i % bhs_per_rec)
1205                                 continue;
1206                         tbh = bhs[i];
1207                         /* Skip removed buffers (and hence records). */
1208                         if (!tbh)
1209                                 continue;
1210                         ofs = bh_offset(tbh);
1211                         /* Get the mft record number. */
1212                         mft_no = (((s64)page->index << PAGE_CACHE_SHIFT) + ofs)
1213                                         >> rec_size_bits;
1214                         if (mft_no < vol->mftmirr_size)
1215                                 ntfs_sync_mft_mirror(vol, mft_no,
1216                                                 (MFT_RECORD*)(kaddr + ofs),
1217                                                 sync);
1218                 }
1219                 if (!sync)
1220                         goto do_wait;
1221         }
1222         /* Remove the mst protection fixups again. */
1223         for (i = 0; i < nr_bhs; i++) {
1224                 if (!(i % bhs_per_rec)) {
1225                         tbh = bhs[i];
1226                         if (!tbh)
1227                                 continue;
1228                         post_write_mst_fixup((NTFS_RECORD*)(kaddr +
1229                                         bh_offset(tbh)));
1230                 }
1231         }
1232         flush_dcache_page(page);
1233 unm_done:
1234         /* Unlock any locked inodes. */
1235         while (nr_locked_nis-- > 0) {
1236                 ntfs_inode *tni, *base_tni;
1237                 
1238                 tni = locked_nis[nr_locked_nis];
1239                 /* Get the base inode. */
1240                 down(&tni->extent_lock);
1241                 if (tni->nr_extents >= 0)
1242                         base_tni = tni;
1243                 else {
1244                         base_tni = tni->ext.base_ntfs_ino;
1245                         BUG_ON(!base_tni);
1246                 }
1247                 up(&tni->extent_lock);
1248                 ntfs_debug("Unlocking %s inode 0x%lx.",
1249                                 tni == base_tni ? "base" : "extent",
1250                                 tni->mft_no);
1251                 up(&tni->mrec_lock);
1252                 atomic_dec(&tni->count);
1253                 iput(VFS_I(base_tni));
1254         }
1255         SetPageUptodate(page);
1256         kunmap(page);
1257 done:
1258         if (unlikely(err && err != -ENOMEM)) {
1259                 /*
1260                  * Set page error if there is only one ntfs record in the page.
1261                  * Otherwise we would loose per-record granularity.
1262                  */
1263                 if (ni->itype.index.block_size == PAGE_CACHE_SIZE)
1264                         SetPageError(page);
1265                 NVolSetErrors(vol);
1266         }
1267         if (page_is_dirty) {
1268                 ntfs_debug("Page still contains one or more dirty ntfs "
1269                                 "records.  Redirtying the page starting at "
1270                                 "record 0x%lx.", page->index <<
1271                                 (PAGE_CACHE_SHIFT - rec_size_bits));
1272                 redirty_page_for_writepage(wbc, page);
1273                 unlock_page(page);
1274         } else {
1275                 /*
1276                  * Keep the VM happy.  This must be done otherwise the
1277                  * radix-tree tag PAGECACHE_TAG_DIRTY remains set even though
1278                  * the page is clean.
1279                  */
1280                 BUG_ON(PageWriteback(page));
1281                 set_page_writeback(page);
1282                 unlock_page(page);
1283                 end_page_writeback(page);
1284         }
1285         if (likely(!err))
1286                 ntfs_debug("Done.");
1287         return err;
1288 }
1289
1290 /**
1291  * ntfs_writepage - write a @page to the backing store
1292  * @page:       page cache page to write out
1293  * @wbc:        writeback control structure
1294  *
1295  * This is called from the VM when it wants to have a dirty ntfs page cache
1296  * page cleaned.  The VM has already locked the page and marked it clean.
1297  *
1298  * For non-resident attributes, ntfs_writepage() writes the @page by calling
1299  * the ntfs version of the generic block_write_full_page() function,
1300  * ntfs_write_block(), which in turn if necessary creates and writes the
1301  * buffers associated with the page asynchronously.
1302  *
1303  * For resident attributes, OTOH, ntfs_writepage() writes the @page by copying
1304  * the data to the mft record (which at this stage is most likely in memory).
1305  * The mft record is then marked dirty and written out asynchronously via the
1306  * vfs inode dirty code path for the inode the mft record belongs to or via the
1307  * vm page dirty code path for the page the mft record is in.
1308  *
1309  * Based on ntfs_readpage() and fs/buffer.c::block_write_full_page().
1310  *
1311  * Return 0 on success and -errno on error.
1312  */
1313 static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
1314 {
1315         loff_t i_size;
1316         struct inode *vi = page->mapping->host;
1317         ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi);
1318         char *kaddr;
1319         ntfs_attr_search_ctx *ctx = NULL;
1320         MFT_RECORD *m = NULL;
1321         u32 attr_len;
1322         int err;
1323
1324 retry_writepage:
1325         BUG_ON(!PageLocked(page));
1326         i_size = i_size_read(vi);
1327         /* Is the page fully outside i_size? (truncate in progress) */
1328         if (unlikely(page->index >= (i_size + PAGE_CACHE_SIZE - 1) >>
1329                         PAGE_CACHE_SHIFT)) {
1330                 /*
1331                  * The page may have dirty, unmapped buffers.  Make them
1332                  * freeable here, so the page does not leak.
1333                  */
1334                 block_invalidatepage(page, 0);
1335                 unlock_page(page);
1336                 ntfs_debug("Write outside i_size - truncated?");
1337                 return 0;
1338         }
1339         /*
1340          * Only $DATA attributes can be encrypted and only unnamed $DATA
1341          * attributes can be compressed.  Index root can have the flags set but
1342          * this means to create compressed/encrypted files, not that the
1343          * attribute is compressed/encrypted.
1344          */
1345         if (ni->type != AT_INDEX_ROOT) {
1346                 /* If file is encrypted, deny access, just like NT4. */
1347                 if (NInoEncrypted(ni)) {
1348                         unlock_page(page);
1349                         BUG_ON(ni->type != AT_DATA);
1350                         ntfs_debug("Denying write access to encrypted "
1351                                         "file.");
1352                         return -EACCES;
1353                 }
1354                 /* Compressed data streams are handled in compress.c. */
1355                 if (NInoNonResident(ni) && NInoCompressed(ni)) {
1356                         BUG_ON(ni->type != AT_DATA);
1357                         BUG_ON(ni->name_len);
1358                         // TODO: Implement and replace this with
1359                         // return ntfs_write_compressed_block(page);
1360                         unlock_page(page);
1361                         ntfs_error(vi->i_sb, "Writing to compressed files is "
1362                                         "not supported yet.  Sorry.");
1363                         return -EOPNOTSUPP;
1364                 }
1365                 // TODO: Implement and remove this check.
1366                 if (NInoNonResident(ni) && NInoSparse(ni)) {
1367                         unlock_page(page);
1368                         ntfs_error(vi->i_sb, "Writing to sparse files is not "
1369                                         "supported yet.  Sorry.");
1370                         return -EOPNOTSUPP;
1371                 }
1372         }
1373         /* NInoNonResident() == NInoIndexAllocPresent() */
1374         if (NInoNonResident(ni)) {
1375                 /* We have to zero every time due to mmap-at-end-of-file. */
1376                 if (page->index >= (i_size >> PAGE_CACHE_SHIFT)) {
1377                         /* The page straddles i_size. */
1378                         unsigned int ofs = i_size & ~PAGE_CACHE_MASK;
1379                         kaddr = kmap_atomic(page, KM_USER0);
1380                         memset(kaddr + ofs, 0, PAGE_CACHE_SIZE - ofs);
1381                         flush_dcache_page(page);
1382                         kunmap_atomic(kaddr, KM_USER0);
1383                 }
1384                 /* Handle mst protected attributes. */
1385                 if (NInoMstProtected(ni))
1386                         return ntfs_write_mst_block(page, wbc);
1387                 /* Normal, non-resident data stream. */
1388                 return ntfs_write_block(page, wbc);
1389         }
1390         /*
1391          * Attribute is resident, implying it is not compressed, encrypted, or
1392          * mst protected.  This also means the attribute is smaller than an mft
1393          * record and hence smaller than a page, so can simply return error on
1394          * any pages with index above 0.  Note the attribute can actually be
1395          * marked compressed but if it is resident the actual data is not
1396          * compressed so we are ok to ignore the compressed flag here.
1397          */
1398         BUG_ON(page_has_buffers(page));
1399         BUG_ON(!PageUptodate(page));
1400         if (unlikely(page->index > 0)) {
1401                 ntfs_error(vi->i_sb, "BUG()! page->index (0x%lx) > 0.  "
1402                                 "Aborting write.", page->index);
1403                 BUG_ON(PageWriteback(page));
1404                 set_page_writeback(page);
1405                 unlock_page(page);
1406                 end_page_writeback(page);
1407                 return -EIO;
1408         }
1409         if (!NInoAttr(ni))
1410                 base_ni = ni;
1411         else
1412                 base_ni = ni->ext.base_ntfs_ino;
1413         /* Map, pin, and lock the mft record. */
1414         m = map_mft_record(base_ni);
1415         if (IS_ERR(m)) {
1416                 err = PTR_ERR(m);
1417                 m = NULL;
1418                 ctx = NULL;
1419                 goto err_out;
1420         }
1421         /*
1422          * If a parallel write made the attribute non-resident, drop the mft
1423          * record and retry the writepage.
1424          */
1425         if (unlikely(NInoNonResident(ni))) {
1426                 unmap_mft_record(base_ni);
1427                 goto retry_writepage;
1428         }
1429         ctx = ntfs_attr_get_search_ctx(base_ni, m);
1430         if (unlikely(!ctx)) {
1431                 err = -ENOMEM;
1432                 goto err_out;
1433         }
1434         err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
1435                         CASE_SENSITIVE, 0, NULL, 0, ctx);
1436         if (unlikely(err))
1437                 goto err_out;
1438         /*
1439          * Keep the VM happy.  This must be done otherwise the radix-tree tag
1440          * PAGECACHE_TAG_DIRTY remains set even though the page is clean.
1441          */
1442         BUG_ON(PageWriteback(page));
1443         set_page_writeback(page);
1444         unlock_page(page);
1445         /*
1446          * Here, we do not need to zero the out of bounds area everytime
1447          * because the below memcpy() already takes care of the
1448          * mmap-at-end-of-file requirements.  If the file is converted to a
1449          * non-resident one, then the code path use is switched to the
1450          * non-resident one where the zeroing happens on each ntfs_writepage()
1451          * invocation.
1452          */
1453         attr_len = le32_to_cpu(ctx->attr->data.resident.value_length);
1454         i_size = i_size_read(vi);
1455         if (unlikely(attr_len > i_size)) {
1456                 attr_len = i_size;
1457                 ctx->attr->data.resident.value_length = cpu_to_le32(attr_len);
1458         }
1459         kaddr = kmap_atomic(page, KM_USER0);
1460         /* Copy the data from the page to the mft record. */
1461         memcpy((u8*)ctx->attr +
1462                         le16_to_cpu(ctx->attr->data.resident.value_offset),
1463                         kaddr, attr_len);
1464         flush_dcache_mft_record_page(ctx->ntfs_ino);
1465         /* Zero out of bounds area in the page cache page. */
1466         memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
1467         flush_dcache_page(page);
1468         kunmap_atomic(kaddr, KM_USER0);
1469
1470         end_page_writeback(page);
1471
1472         /* Mark the mft record dirty, so it gets written back. */
1473         mark_mft_record_dirty(ctx->ntfs_ino);
1474         ntfs_attr_put_search_ctx(ctx);
1475         unmap_mft_record(base_ni);
1476         return 0;
1477 err_out:
1478         if (err == -ENOMEM) {
1479                 ntfs_warning(vi->i_sb, "Error allocating memory. Redirtying "
1480                                 "page so we try again later.");
1481                 /*
1482                  * Put the page back on mapping->dirty_pages, but leave its
1483                  * buffers' dirty state as-is.
1484                  */
1485                 redirty_page_for_writepage(wbc, page);
1486                 err = 0;
1487         } else {
1488                 ntfs_error(vi->i_sb, "Resident attribute write failed with "
1489                                 "error %i.", err);
1490                 SetPageError(page);
1491                 NVolSetErrors(ni->vol);
1492                 make_bad_inode(vi);
1493         }
1494         unlock_page(page);
1495         if (ctx)
1496                 ntfs_attr_put_search_ctx(ctx);
1497         if (m)
1498                 unmap_mft_record(base_ni);
1499         return err;
1500 }
1501
1502 /**
1503  * ntfs_prepare_nonresident_write -
1504  *
1505  */
1506 static int ntfs_prepare_nonresident_write(struct page *page,
1507                 unsigned from, unsigned to)
1508 {
1509         VCN vcn;
1510         LCN lcn;
1511         s64 initialized_size;
1512         loff_t i_size;
1513         sector_t block, ablock, iblock;
1514         struct inode *vi;
1515         ntfs_inode *ni;
1516         ntfs_volume *vol;
1517         runlist_element *rl;
1518         struct buffer_head *bh, *head, *wait[2], **wait_bh = wait;
1519         unsigned long flags;
1520         unsigned int vcn_ofs, block_start, block_end, blocksize;
1521         int err;
1522         BOOL is_retry;
1523         unsigned char blocksize_bits;
1524
1525         vi = page->mapping->host;
1526         ni = NTFS_I(vi);
1527         vol = ni->vol;
1528
1529         ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
1530                         "0x%lx, from = %u, to = %u.", ni->mft_no, ni->type,
1531                         page->index, from, to);
1532
1533         BUG_ON(!NInoNonResident(ni));
1534
1535         blocksize_bits = vi->i_blkbits;
1536         blocksize = 1 << blocksize_bits;
1537
1538         /*
1539          * create_empty_buffers() will create uptodate/dirty buffers if the
1540          * page is uptodate/dirty.
1541          */
1542         if (!page_has_buffers(page))
1543                 create_empty_buffers(page, blocksize, 0);
1544         bh = head = page_buffers(page);
1545         if (unlikely(!bh))
1546                 return -ENOMEM;
1547
1548         /* The first block in the page. */
1549         block = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits);
1550
1551         read_lock_irqsave(&ni->size_lock, flags);
1552         /*
1553          * The first out of bounds block for the allocated size.  No need to
1554          * round up as allocated_size is in multiples of cluster size and the
1555          * minimum cluster size is 512 bytes, which is equal to the smallest
1556          * blocksize.
1557          */
1558         ablock = ni->allocated_size >> blocksize_bits;
1559         i_size = i_size_read(vi);
1560         initialized_size = ni->initialized_size;
1561         read_unlock_irqrestore(&ni->size_lock, flags);
1562
1563         /* The last (fully or partially) initialized block. */
1564         iblock = initialized_size >> blocksize_bits;
1565
1566         /* Loop through all the buffers in the page. */
1567         block_start = 0;
1568         rl = NULL;
1569         err = 0;
1570         do {
1571                 block_end = block_start + blocksize;
1572                 /*
1573                  * If buffer @bh is outside the write, just mark it uptodate
1574                  * if the page is uptodate and continue with the next buffer.
1575                  */
1576                 if (block_end <= from || block_start >= to) {
1577                         if (PageUptodate(page)) {
1578                                 if (!buffer_uptodate(bh))
1579                                         set_buffer_uptodate(bh);
1580                         }
1581                         continue;
1582                 }
1583                 /*
1584                  * @bh is at least partially being written to.
1585                  * Make sure it is not marked as new.
1586                  */
1587                 //if (buffer_new(bh))
1588                 //      clear_buffer_new(bh);
1589
1590                 if (block >= ablock) {
1591                         // TODO: block is above allocated_size, need to
1592                         // allocate it. Best done in one go to accommodate not
1593                         // only block but all above blocks up to and including:
1594                         // ((page->index << PAGE_CACHE_SHIFT) + to + blocksize
1595                         // - 1) >> blobksize_bits. Obviously will need to round
1596                         // up to next cluster boundary, too. This should be
1597                         // done with a helper function, so it can be reused.
1598                         ntfs_error(vol->sb, "Writing beyond allocated size "
1599                                         "is not supported yet. Sorry.");
1600                         err = -EOPNOTSUPP;
1601                         goto err_out;
1602                         // Need to update ablock.
1603                         // Need to set_buffer_new() on all block bhs that are
1604                         // newly allocated.
1605                 }
1606                 /*
1607                  * Now we have enough allocated size to fulfill the whole
1608                  * request, i.e. block < ablock is true.
1609                  */
1610                 if (unlikely((block >= iblock) &&
1611                                 (initialized_size < i_size))) {
1612                         /*
1613                          * If this page is fully outside initialized size, zero
1614                          * out all pages between the current initialized size
1615                          * and the current page. Just use ntfs_readpage() to do
1616                          * the zeroing transparently.
1617                          */
1618                         if (block > iblock) {
1619                                 // TODO:
1620                                 // For each page do:
1621                                 // - read_cache_page()
1622                                 // Again for each page do:
1623                                 // - wait_on_page_locked()
1624                                 // - Check (PageUptodate(page) &&
1625                                 //                      !PageError(page))
1626                                 // Update initialized size in the attribute and
1627                                 // in the inode.
1628                                 // Again, for each page do:
1629                                 //      __set_page_dirty_buffers();
1630                                 // page_cache_release()
1631                                 // We don't need to wait on the writes.
1632                                 // Update iblock.
1633                         }
1634                         /*
1635                          * The current page straddles initialized size. Zero
1636                          * all non-uptodate buffers and set them uptodate (and
1637                          * dirty?). Note, there aren't any non-uptodate buffers
1638                          * if the page is uptodate.
1639                          * FIXME: For an uptodate page, the buffers may need to
1640                          * be written out because they were not initialized on
1641                          * disk before.
1642                          */
1643                         if (!PageUptodate(page)) {
1644                                 // TODO:
1645                                 // Zero any non-uptodate buffers up to i_size.
1646                                 // Set them uptodate and dirty.
1647                         }
1648                         // TODO:
1649                         // Update initialized size in the attribute and in the
1650                         // inode (up to i_size).
1651                         // Update iblock.
1652                         // FIXME: This is inefficient. Try to batch the two
1653                         // size changes to happen in one go.
1654                         ntfs_error(vol->sb, "Writing beyond initialized size "
1655                                         "is not supported yet. Sorry.");
1656                         err = -EOPNOTSUPP;
1657                         goto err_out;
1658                         // Do NOT set_buffer_new() BUT DO clear buffer range
1659                         // outside write request range.
1660                         // set_buffer_uptodate() on complete buffers as well as
1661                         // set_buffer_dirty().
1662                 }
1663
1664                 /* Need to map unmapped buffers. */
1665                 if (!buffer_mapped(bh)) {
1666                         /* Unmapped buffer. Need to map it. */
1667                         bh->b_bdev = vol->sb->s_bdev;
1668
1669                         /* Convert block into corresponding vcn and offset. */
1670                         vcn = (VCN)block << blocksize_bits >>
1671                                         vol->cluster_size_bits;
1672                         vcn_ofs = ((VCN)block << blocksize_bits) &
1673                                         vol->cluster_size_mask;
1674
1675                         is_retry = FALSE;
1676                         if (!rl) {
1677 lock_retry_remap:
1678                                 down_read(&ni->runlist.lock);
1679                                 rl = ni->runlist.rl;
1680                         }
1681                         if (likely(rl != NULL)) {
1682                                 /* Seek to element containing target vcn. */
1683                                 while (rl->length && rl[1].vcn <= vcn)
1684                                         rl++;
1685                                 lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
1686                         } else
1687                                 lcn = LCN_RL_NOT_MAPPED;
1688                         if (unlikely(lcn < 0)) {
1689                                 /*
1690                                  * We extended the attribute allocation above.
1691                                  * If we hit an ENOENT here it means that the
1692                                  * allocation was insufficient which is a bug.
1693                                  */
1694                                 BUG_ON(lcn == LCN_ENOENT);
1695
1696                                 /* It is a hole, need to instantiate it. */
1697                                 if (lcn == LCN_HOLE) {
1698                                         // TODO: Instantiate the hole.
1699                                         // clear_buffer_new(bh);
1700                                         // unmap_underlying_metadata(bh->b_bdev,
1701                                         //              bh->b_blocknr);
1702                                         // For non-uptodate buffers, need to
1703                                         // zero out the region outside the
1704                                         // request in this bh or all bhs,
1705                                         // depending on what we implemented
1706                                         // above.
1707                                         // Need to flush_dcache_page().
1708                                         // Or could use set_buffer_new()
1709                                         // instead?
1710                                         ntfs_error(vol->sb, "Writing into "
1711                                                         "sparse regions is "
1712                                                         "not supported yet. "
1713                                                         "Sorry.");
1714                                         err = -EOPNOTSUPP;
1715                                         if (!rl)
1716                                                 up_read(&ni->runlist.lock);
1717                                         goto err_out;
1718                                 } else if (!is_retry &&
1719                                                 lcn == LCN_RL_NOT_MAPPED) {
1720                                         is_retry = TRUE;
1721                                         /*
1722                                          * Attempt to map runlist, dropping
1723                                          * lock for the duration.
1724                                          */
1725                                         up_read(&ni->runlist.lock);
1726                                         err = ntfs_map_runlist(ni, vcn);
1727                                         if (likely(!err))
1728                                                 goto lock_retry_remap;
1729                                         rl = NULL;
1730                                 } else if (!rl)
1731                                         up_read(&ni->runlist.lock);
1732                                 /*
1733                                  * Failed to map the buffer, even after
1734                                  * retrying.
1735                                  */
1736                                 if (!err)
1737                                         err = -EIO;
1738                                 bh->b_blocknr = -1;
1739                                 ntfs_error(vol->sb, "Failed to write to inode "
1740                                                 "0x%lx, attribute type 0x%x, "
1741                                                 "vcn 0x%llx, offset 0x%x "
1742                                                 "because its location on disk "
1743                                                 "could not be determined%s "
1744                                                 "(error code %i).",
1745                                                 ni->mft_no, ni->type,
1746                                                 (unsigned long long)vcn,
1747                                                 vcn_ofs, is_retry ? " even "
1748                                                 "after retrying" : "", err);
1749                                 goto err_out;
1750                         }
1751                         /* We now have a successful remap, i.e. lcn >= 0. */
1752
1753                         /* Setup buffer head to correct block. */
1754                         bh->b_blocknr = ((lcn << vol->cluster_size_bits)
1755                                         + vcn_ofs) >> blocksize_bits;
1756                         set_buffer_mapped(bh);
1757
1758                         // FIXME: Something analogous to this is needed for
1759                         // each newly allocated block, i.e. BH_New.
1760                         // FIXME: Might need to take this out of the
1761                         // if (!buffer_mapped(bh)) {}, depending on how we
1762                         // implement things during the allocated_size and
1763                         // initialized_size extension code above.
1764                         if (buffer_new(bh)) {
1765                                 clear_buffer_new(bh);
1766                                 unmap_underlying_metadata(bh->b_bdev,
1767                                                 bh->b_blocknr);
1768                                 if (PageUptodate(page)) {
1769                                         set_buffer_uptodate(bh);
1770                                         continue;
1771                                 }
1772                                 /*
1773                                  * Page is _not_ uptodate, zero surrounding
1774                                  * region. NOTE: This is how we decide if to
1775                                  * zero or not!
1776                                  */
1777                                 if (block_end > to || block_start < from) {
1778                                         void *kaddr;
1779
1780                                         kaddr = kmap_atomic(page, KM_USER0);
1781                                         if (block_end > to)
1782                                                 memset(kaddr + to, 0,
1783                                                                 block_end - to);
1784                                         if (block_start < from)
1785                                                 memset(kaddr + block_start, 0,
1786                                                                 from -
1787                                                                 block_start);
1788                                         flush_dcache_page(page);
1789                                         kunmap_atomic(kaddr, KM_USER0);
1790                                 }
1791                                 continue;
1792                         }
1793                 }
1794                 /* @bh is mapped, set it uptodate if the page is uptodate. */
1795                 if (PageUptodate(page)) {
1796                         if (!buffer_uptodate(bh))
1797                                 set_buffer_uptodate(bh);
1798                         continue;
1799                 }
1800                 /*
1801                  * The page is not uptodate. The buffer is mapped. If it is not
1802                  * uptodate, and it is only partially being written to, we need
1803                  * to read the buffer in before the write, i.e. right now.
1804                  */
1805                 if (!buffer_uptodate(bh) &&
1806                                 (block_start < from || block_end > to)) {
1807                         ll_rw_block(READ, 1, &bh);
1808                         *wait_bh++ = bh;
1809                 }
1810         } while (block++, block_start = block_end,
1811                         (bh = bh->b_this_page) != head);
1812
1813         /* Release the lock if we took it. */
1814         if (rl) {
1815                 up_read(&ni->runlist.lock);
1816                 rl = NULL;
1817         }
1818
1819         /* If we issued read requests, let them complete. */
1820         while (wait_bh > wait) {
1821                 wait_on_buffer(*--wait_bh);
1822                 if (!buffer_uptodate(*wait_bh))
1823                         return -EIO;
1824         }
1825
1826         ntfs_debug("Done.");
1827         return 0;
1828 err_out:
1829         /*
1830          * Zero out any newly allocated blocks to avoid exposing stale data.
1831          * If BH_New is set, we know that the block was newly allocated in the
1832          * above loop.
1833          * FIXME: What about initialized_size increments? Have we done all the
1834          * required zeroing above? If not this error handling is broken, and
1835          * in particular the if (block_end <= from) check is completely bogus.
1836          */
1837         bh = head;
1838         block_start = 0;
1839         is_retry = FALSE;
1840         do {
1841                 block_end = block_start + blocksize;
1842                 if (block_end <= from)
1843                         continue;
1844                 if (block_start >= to)
1845                         break;
1846                 if (buffer_new(bh)) {
1847                         void *kaddr;
1848
1849                         clear_buffer_new(bh);
1850                         kaddr = kmap_atomic(page, KM_USER0);
1851                         memset(kaddr + block_start, 0, bh->b_size);
1852                         kunmap_atomic(kaddr, KM_USER0);
1853                         set_buffer_uptodate(bh);
1854                         mark_buffer_dirty(bh);
1855                         is_retry = TRUE;
1856                 }
1857         } while (block_start = block_end, (bh = bh->b_this_page) != head);
1858         if (is_retry)
1859                 flush_dcache_page(page);
1860         if (rl)
1861                 up_read(&ni->runlist.lock);
1862         return err;
1863 }
1864
1865 /**
1866  * ntfs_prepare_write - prepare a page for receiving data
1867  *
1868  * This is called from generic_file_write() with i_sem held on the inode
1869  * (@page->mapping->host).  The @page is locked but not kmap()ped.  The source
1870  * data has not yet been copied into the @page.
1871  *
1872  * Need to extend the attribute/fill in holes if necessary, create blocks and
1873  * make partially overwritten blocks uptodate,
1874  *
1875  * i_size is not to be modified yet.
1876  *
1877  * Return 0 on success or -errno on error.
1878  *
1879  * Should be using block_prepare_write() [support for sparse files] or
1880  * cont_prepare_write() [no support for sparse files].  Cannot do that due to
1881  * ntfs specifics but can look at them for implementation guidance.
1882  *
1883  * Note: In the range, @from is inclusive and @to is exclusive, i.e. @from is
1884  * the first byte in the page that will be written to and @to is the first byte
1885  * after the last byte that will be written to.
1886  */
1887 static int ntfs_prepare_write(struct file *file, struct page *page,
1888                 unsigned from, unsigned to)
1889 {
1890         s64 new_size;
1891         loff_t i_size;
1892         struct inode *vi = page->mapping->host;
1893         ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi);
1894         ntfs_volume *vol = ni->vol;
1895         ntfs_attr_search_ctx *ctx = NULL;
1896         MFT_RECORD *m = NULL;
1897         ATTR_RECORD *a;
1898         u8 *kaddr;
1899         u32 attr_len;
1900         int err;
1901
1902         ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
1903                         "0x%lx, from = %u, to = %u.", vi->i_ino, ni->type,
1904                         page->index, from, to);
1905         BUG_ON(!PageLocked(page));
1906         BUG_ON(from > PAGE_CACHE_SIZE);
1907         BUG_ON(to > PAGE_CACHE_SIZE);
1908         BUG_ON(from > to);
1909         BUG_ON(NInoMstProtected(ni));
1910         /*
1911          * If a previous ntfs_truncate() failed, repeat it and abort if it
1912          * fails again.
1913          */
1914         if (unlikely(NInoTruncateFailed(ni))) {
1915                 down_write(&vi->i_alloc_sem);
1916                 err = ntfs_truncate(vi);
1917                 up_write(&vi->i_alloc_sem);
1918                 if (err || NInoTruncateFailed(ni)) {
1919                         if (!err)
1920                                 err = -EIO;
1921                         goto err_out;
1922                 }
1923         }
1924         /* If the attribute is not resident, deal with it elsewhere. */
1925         if (NInoNonResident(ni)) {
1926                 /*
1927                  * Only unnamed $DATA attributes can be compressed, encrypted,
1928                  * and/or sparse.
1929                  */
1930                 if (ni->type == AT_DATA && !ni->name_len) {
1931                         /* If file is encrypted, deny access, just like NT4. */
1932                         if (NInoEncrypted(ni)) {
1933                                 ntfs_debug("Denying write access to encrypted "
1934                                                 "file.");
1935                                 return -EACCES;
1936                         }
1937                         /* Compressed data streams are handled in compress.c. */
1938                         if (NInoCompressed(ni)) {
1939                                 // TODO: Implement and replace this check with
1940                                 // return ntfs_write_compressed_block(page);
1941                                 ntfs_error(vi->i_sb, "Writing to compressed "
1942                                                 "files is not supported yet. "
1943                                                 "Sorry.");
1944                                 return -EOPNOTSUPP;
1945                         }
1946                         // TODO: Implement and remove this check.
1947                         if (NInoSparse(ni)) {
1948                                 ntfs_error(vi->i_sb, "Writing to sparse files "
1949                                                 "is not supported yet. Sorry.");
1950                                 return -EOPNOTSUPP;
1951                         }
1952                 }
1953                 /* Normal data stream. */
1954                 return ntfs_prepare_nonresident_write(page, from, to);
1955         }
1956         /*
1957          * Attribute is resident, implying it is not compressed, encrypted, or
1958          * sparse.
1959          */
1960         BUG_ON(page_has_buffers(page));
1961         new_size = ((s64)page->index << PAGE_CACHE_SHIFT) + to;
1962         /* If we do not need to resize the attribute allocation we are done. */
1963         if (new_size <= i_size_read(vi))
1964                 goto done;
1965         /* Map, pin, and lock the (base) mft record. */
1966         if (!NInoAttr(ni))
1967                 base_ni = ni;
1968         else
1969                 base_ni = ni->ext.base_ntfs_ino;
1970         m = map_mft_record(base_ni);
1971         if (IS_ERR(m)) {
1972                 err = PTR_ERR(m);
1973                 m = NULL;
1974                 ctx = NULL;
1975                 goto err_out;
1976         }
1977         ctx = ntfs_attr_get_search_ctx(base_ni, m);
1978         if (unlikely(!ctx)) {
1979                 err = -ENOMEM;
1980                 goto err_out;
1981         }
1982         err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
1983                         CASE_SENSITIVE, 0, NULL, 0, ctx);
1984         if (unlikely(err)) {
1985                 if (err == -ENOENT)
1986                         err = -EIO;
1987                 goto err_out;
1988         }
1989         m = ctx->mrec;
1990         a = ctx->attr;
1991         /* The total length of the attribute value. */
1992         attr_len = le32_to_cpu(a->data.resident.value_length);
1993         /* Fix an eventual previous failure of ntfs_commit_write(). */
1994         i_size = i_size_read(vi);
1995         if (unlikely(attr_len > i_size)) {
1996                 attr_len = i_size;
1997                 a->data.resident.value_length = cpu_to_le32(attr_len);
1998         }
1999         /* If we do not need to resize the attribute allocation we are done. */
2000         if (new_size <= attr_len)
2001                 goto done_unm;
2002         /* Check if new size is allowed in $AttrDef. */
2003         err = ntfs_attr_size_bounds_check(vol, ni->type, new_size);
2004         if (unlikely(err)) {
2005                 if (err == -ERANGE) {
2006                         ntfs_error(vol->sb, "Write would cause the inode "
2007                                         "0x%lx to exceed the maximum size for "
2008                                         "its attribute type (0x%x).  Aborting "
2009                                         "write.", vi->i_ino,
2010                                         le32_to_cpu(ni->type));
2011                 } else {
2012                         ntfs_error(vol->sb, "Inode 0x%lx has unknown "
2013                                         "attribute type 0x%x.  Aborting "
2014                                         "write.", vi->i_ino,
2015                                         le32_to_cpu(ni->type));
2016                         err = -EIO;
2017                 }
2018                 goto err_out2;
2019         }
2020         /*
2021          * Extend the attribute record to be able to store the new attribute
2022          * size.
2023          */
2024         if (new_size >= vol->mft_record_size || ntfs_attr_record_resize(m, a,
2025                         le16_to_cpu(a->data.resident.value_offset) +
2026                         new_size)) {
2027                 /* Not enough space in the mft record. */
2028                 ntfs_error(vol->sb, "Not enough space in the mft record for "
2029                                 "the resized attribute value.  This is not "
2030                                 "supported yet.  Aborting write.");
2031                 err = -EOPNOTSUPP;
2032                 goto err_out2;
2033         }
2034         /*
2035          * We have enough space in the mft record to fit the write.  This
2036          * implies the attribute is smaller than the mft record and hence the
2037          * attribute must be in a single page and hence page->index must be 0.
2038          */
2039         BUG_ON(page->index);
2040         /*
2041          * If the beginning of the write is past the old size, enlarge the
2042          * attribute value up to the beginning of the write and fill it with
2043          * zeroes.
2044          */
2045         if (from > attr_len) {
2046                 memset((u8*)a + le16_to_cpu(a->data.resident.value_offset) +
2047                                 attr_len, 0, from - attr_len);
2048                 a->data.resident.value_length = cpu_to_le32(from);
2049                 /* Zero the corresponding area in the page as well. */
2050                 if (PageUptodate(page)) {
2051                         kaddr = kmap_atomic(page, KM_USER0);
2052                         memset(kaddr + attr_len, 0, from - attr_len);
2053                         kunmap_atomic(kaddr, KM_USER0);
2054                         flush_dcache_page(page);
2055                 }
2056         }
2057         flush_dcache_mft_record_page(ctx->ntfs_ino);
2058         mark_mft_record_dirty(ctx->ntfs_ino);
2059 done_unm:
2060         ntfs_attr_put_search_ctx(ctx);
2061         unmap_mft_record(base_ni);
2062         /*
2063          * Because resident attributes are handled by memcpy() to/from the
2064          * corresponding MFT record, and because this form of i/o is byte
2065          * aligned rather than block aligned, there is no need to bring the
2066          * page uptodate here as in the non-resident case where we need to
2067          * bring the buffers straddled by the write uptodate before
2068          * generic_file_write() does the copying from userspace.
2069          *
2070          * We thus defer the uptodate bringing of the page region outside the
2071          * region written to to ntfs_commit_write(), which makes the code
2072          * simpler and saves one atomic kmap which is good.
2073          */
2074 done:
2075         ntfs_debug("Done.");
2076         return 0;
2077 err_out:
2078         if (err == -ENOMEM)
2079                 ntfs_warning(vi->i_sb, "Error allocating memory required to "
2080                                 "prepare the write.");
2081         else {
2082                 ntfs_error(vi->i_sb, "Resident attribute prepare write failed "
2083                                 "with error %i.", err);
2084                 NVolSetErrors(vol);
2085                 make_bad_inode(vi);
2086         }
2087 err_out2:
2088         if (ctx)
2089                 ntfs_attr_put_search_ctx(ctx);
2090         if (m)
2091                 unmap_mft_record(base_ni);
2092         return err;
2093 }
2094
2095 /**
2096  * ntfs_commit_nonresident_write -
2097  *
2098  */
2099 static int ntfs_commit_nonresident_write(struct page *page,
2100                 unsigned from, unsigned to)
2101 {
2102         s64 pos = ((s64)page->index << PAGE_CACHE_SHIFT) + to;
2103         struct inode *vi = page->mapping->host;
2104         struct buffer_head *bh, *head;
2105         unsigned int block_start, block_end, blocksize;
2106         BOOL partial;
2107
2108         ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
2109                         "0x%lx, from = %u, to = %u.", vi->i_ino,
2110                         NTFS_I(vi)->type, page->index, from, to);
2111         blocksize = 1 << vi->i_blkbits;
2112
2113         // FIXME: We need a whole slew of special cases in here for compressed
2114         // files for example...
2115         // For now, we know ntfs_prepare_write() would have failed so we can't
2116         // get here in any of the cases which we have to special case, so we
2117         // are just a ripped off, unrolled generic_commit_write().
2118
2119         bh = head = page_buffers(page);
2120         block_start = 0;
2121         partial = FALSE;
2122         do {
2123                 block_end = block_start + blocksize;
2124                 if (block_end <= from || block_start >= to) {
2125                         if (!buffer_uptodate(bh))
2126                                 partial = TRUE;
2127                 } else {
2128                         set_buffer_uptodate(bh);
2129                         mark_buffer_dirty(bh);
2130                 }
2131         } while (block_start = block_end, (bh = bh->b_this_page) != head);
2132         /*
2133          * If this is a partial write which happened to make all buffers
2134          * uptodate then we can optimize away a bogus ->readpage() for the next
2135          * read().  Here we 'discover' whether the page went uptodate as a
2136          * result of this (potentially partial) write.
2137          */
2138         if (!partial)
2139                 SetPageUptodate(page);
2140         /*
2141          * Not convinced about this at all.  See disparity comment above.  For
2142          * now we know ntfs_prepare_write() would have failed in the write
2143          * exceeds i_size case, so this will never trigger which is fine.
2144          */
2145         if (pos > i_size_read(vi)) {
2146                 ntfs_error(vi->i_sb, "Writing beyond the existing file size is "
2147                                 "not supported yet.  Sorry.");
2148                 return -EOPNOTSUPP;
2149                 // vi->i_size = pos;
2150                 // mark_inode_dirty(vi);
2151         }
2152         ntfs_debug("Done.");
2153         return 0;
2154 }
2155
2156 /**
2157  * ntfs_commit_write - commit the received data
2158  *
2159  * This is called from generic_file_write() with i_sem held on the inode
2160  * (@page->mapping->host).  The @page is locked but not kmap()ped.  The source
2161  * data has already been copied into the @page.  ntfs_prepare_write() has been
2162  * called before the data copied and it returned success so we can take the
2163  * results of various BUG checks and some error handling for granted.
2164  *
2165  * Need to mark modified blocks dirty so they get written out later when
2166  * ntfs_writepage() is invoked by the VM.
2167  *
2168  * Return 0 on success or -errno on error.
2169  *
2170  * Should be using generic_commit_write().  This marks buffers uptodate and
2171  * dirty, sets the page uptodate if all buffers in the page are uptodate, and
2172  * updates i_size if the end of io is beyond i_size.  In that case, it also
2173  * marks the inode dirty.
2174  *
2175  * Cannot use generic_commit_write() due to ntfs specialities but can look at
2176  * it for implementation guidance.
2177  *
2178  * If things have gone as outlined in ntfs_prepare_write(), then we do not
2179  * need to do any page content modifications here at all, except in the write
2180  * to resident attribute case, where we need to do the uptodate bringing here
2181  * which we combine with the copying into the mft record which means we save
2182  * one atomic kmap.
2183  */
2184 static int ntfs_commit_write(struct file *file, struct page *page,
2185                 unsigned from, unsigned to)
2186 {
2187         struct inode *vi = page->mapping->host;
2188         ntfs_inode *base_ni, *ni = NTFS_I(vi);
2189         char *kaddr, *kattr;
2190         ntfs_attr_search_ctx *ctx;
2191         MFT_RECORD *m;
2192         ATTR_RECORD *a;
2193         u32 attr_len;
2194         int err;
2195
2196         ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index "
2197                         "0x%lx, from = %u, to = %u.", vi->i_ino, ni->type,
2198                         page->index, from, to);
2199         /* If the attribute is not resident, deal with it elsewhere. */
2200         if (NInoNonResident(ni)) {
2201                 /* Only unnamed $DATA attributes can be compressed/encrypted. */
2202                 if (ni->type == AT_DATA && !ni->name_len) {
2203                         /* Encrypted files need separate handling. */
2204                         if (NInoEncrypted(ni)) {
2205                                 // We never get here at present!
2206                                 BUG();
2207                         }
2208                         /* Compressed data streams are handled in compress.c. */
2209                         if (NInoCompressed(ni)) {
2210                                 // TODO: Implement this!
2211                                 // return ntfs_write_compressed_block(page);
2212                                 // We never get here at present!
2213                                 BUG();
2214                         }
2215                 }
2216                 /* Normal data stream. */
2217                 return ntfs_commit_nonresident_write(page, from, to);
2218         }
2219         /*
2220          * Attribute is resident, implying it is not compressed, encrypted, or
2221          * sparse.
2222          */
2223         if (!NInoAttr(ni))
2224                 base_ni = ni;
2225         else
2226                 base_ni = ni->ext.base_ntfs_ino;
2227         /* Map, pin, and lock the mft record. */
2228         m = map_mft_record(base_ni);
2229         if (IS_ERR(m)) {
2230                 err = PTR_ERR(m);
2231                 m = NULL;
2232                 ctx = NULL;
2233                 goto err_out;
2234         }
2235         ctx = ntfs_attr_get_search_ctx(base_ni, m);
2236         if (unlikely(!ctx)) {
2237                 err = -ENOMEM;
2238                 goto err_out;
2239         }
2240         err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
2241                         CASE_SENSITIVE, 0, NULL, 0, ctx);
2242         if (unlikely(err)) {
2243                 if (err == -ENOENT)
2244                         err = -EIO;
2245                 goto err_out;
2246         }
2247         a = ctx->attr;
2248         /* The total length of the attribute value. */
2249         attr_len = le32_to_cpu(a->data.resident.value_length);
2250         BUG_ON(from > attr_len);
2251         kattr = (u8*)a + le16_to_cpu(a->data.resident.value_offset);
2252         kaddr = kmap_atomic(page, KM_USER0);
2253         /* Copy the received data from the page to the mft record. */
2254         memcpy(kattr + from, kaddr + from, to - from);
2255         /* Update the attribute length if necessary. */
2256         if (to > attr_len) {
2257                 attr_len = to;
2258                 a->data.resident.value_length = cpu_to_le32(attr_len);
2259         }
2260         /*
2261          * If the page is not uptodate, bring the out of bounds area(s)
2262          * uptodate by copying data from the mft record to the page.
2263          */
2264         if (!PageUptodate(page)) {
2265                 if (from > 0)
2266                         memcpy(kaddr, kattr, from);
2267                 if (to < attr_len)
2268                         memcpy(kaddr + to, kattr + to, attr_len - to);
2269                 /* Zero the region outside the end of the attribute value. */
2270                 if (attr_len < PAGE_CACHE_SIZE)
2271                         memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len);
2272                 /*
2273                  * The probability of not having done any of the above is
2274                  * extremely small, so we just flush unconditionally.
2275                  */
2276                 flush_dcache_page(page);
2277                 SetPageUptodate(page);
2278         }
2279         kunmap_atomic(kaddr, KM_USER0);
2280         /* Update i_size if necessary. */
2281         if (i_size_read(vi) < attr_len) {
2282                 unsigned long flags;
2283
2284                 write_lock_irqsave(&ni->size_lock, flags);
2285                 ni->allocated_size = ni->initialized_size = attr_len;
2286                 i_size_write(vi, attr_len);
2287                 write_unlock_irqrestore(&ni->size_lock, flags);
2288         }
2289         /* Mark the mft record dirty, so it gets written back. */
2290         flush_dcache_mft_record_page(ctx->ntfs_ino);
2291         mark_mft_record_dirty(ctx->ntfs_ino);
2292         ntfs_attr_put_search_ctx(ctx);
2293         unmap_mft_record(base_ni);
2294         ntfs_debug("Done.");
2295         return 0;
2296 err_out:
2297         if (err == -ENOMEM) {
2298                 ntfs_warning(vi->i_sb, "Error allocating memory required to "
2299                                 "commit the write.");
2300                 if (PageUptodate(page)) {
2301                         ntfs_warning(vi->i_sb, "Page is uptodate, setting "
2302                                         "dirty so the write will be retried "
2303                                         "later on by the VM.");
2304                         /*
2305                          * Put the page on mapping->dirty_pages, but leave its
2306                          * buffers' dirty state as-is.
2307                          */
2308                         __set_page_dirty_nobuffers(page);
2309                         err = 0;
2310                 } else
2311                         ntfs_error(vi->i_sb, "Page is not uptodate.  Written "
2312                                         "data has been lost.");
2313         } else {
2314                 ntfs_error(vi->i_sb, "Resident attribute commit write failed "
2315                                 "with error %i.", err);
2316                 NVolSetErrors(ni->vol);
2317                 make_bad_inode(vi);
2318         }
2319         if (ctx)
2320                 ntfs_attr_put_search_ctx(ctx);
2321         if (m)
2322                 unmap_mft_record(base_ni);
2323         return err;
2324 }
2325
2326 #endif  /* NTFS_RW */
2327
2328 /**
2329  * ntfs_aops - general address space operations for inodes and attributes
2330  */
2331 struct address_space_operations ntfs_aops = {
2332         .readpage       = ntfs_readpage,        /* Fill page with data. */
2333         .sync_page      = block_sync_page,      /* Currently, just unplugs the
2334                                                    disk request queue. */
2335 #ifdef NTFS_RW
2336         .writepage      = ntfs_writepage,       /* Write dirty page to disk. */
2337         .prepare_write  = ntfs_prepare_write,   /* Prepare page and buffers
2338                                                    ready to receive data. */
2339         .commit_write   = ntfs_commit_write,    /* Commit received data. */
2340 #endif /* NTFS_RW */
2341 };
2342
2343 /**
2344  * ntfs_mst_aops - general address space operations for mst protecteed inodes
2345  *                 and attributes
2346  */
2347 struct address_space_operations ntfs_mst_aops = {
2348         .readpage       = ntfs_readpage,        /* Fill page with data. */
2349         .sync_page      = block_sync_page,      /* Currently, just unplugs the
2350                                                    disk request queue. */
2351 #ifdef NTFS_RW
2352         .writepage      = ntfs_writepage,       /* Write dirty page to disk. */
2353         .set_page_dirty = __set_page_dirty_nobuffers,   /* Set the page dirty
2354                                                    without touching the buffers
2355                                                    belonging to the page. */
2356 #endif /* NTFS_RW */
2357 };
2358
2359 #ifdef NTFS_RW
2360
2361 /**
2362  * mark_ntfs_record_dirty - mark an ntfs record dirty
2363  * @page:       page containing the ntfs record to mark dirty
2364  * @ofs:        byte offset within @page at which the ntfs record begins
2365  *
2366  * Set the buffers and the page in which the ntfs record is located dirty.
2367  *
2368  * The latter also marks the vfs inode the ntfs record belongs to dirty
2369  * (I_DIRTY_PAGES only).
2370  *
2371  * If the page does not have buffers, we create them and set them uptodate.
2372  * The page may not be locked which is why we need to handle the buffers under
2373  * the mapping->private_lock.  Once the buffers are marked dirty we no longer
2374  * need the lock since try_to_free_buffers() does not free dirty buffers.
2375  */
2376 void mark_ntfs_record_dirty(struct page *page, const unsigned int ofs) {
2377         struct address_space *mapping = page->mapping;
2378         ntfs_inode *ni = NTFS_I(mapping->host);
2379         struct buffer_head *bh, *head, *buffers_to_free = NULL;
2380         unsigned int end, bh_size, bh_ofs;
2381
2382         BUG_ON(!PageUptodate(page));
2383         end = ofs + ni->itype.index.block_size;
2384         bh_size = 1 << VFS_I(ni)->i_blkbits;
2385         spin_lock(&mapping->private_lock);
2386         if (unlikely(!page_has_buffers(page))) {
2387                 spin_unlock(&mapping->private_lock);
2388                 bh = head = alloc_page_buffers(page, bh_size, 1);
2389                 spin_lock(&mapping->private_lock);
2390                 if (likely(!page_has_buffers(page))) {
2391                         struct buffer_head *tail;
2392
2393                         do {
2394                                 set_buffer_uptodate(bh);
2395                                 tail = bh;
2396                                 bh = bh->b_this_page;
2397                         } while (bh);
2398                         tail->b_this_page = head;
2399                         attach_page_buffers(page, head);
2400                 } else
2401                         buffers_to_free = bh;
2402         }
2403         bh = head = page_buffers(page);
2404         BUG_ON(!bh);
2405         do {
2406                 bh_ofs = bh_offset(bh);
2407                 if (bh_ofs + bh_size <= ofs)
2408                         continue;
2409                 if (unlikely(bh_ofs >= end))
2410                         break;
2411                 set_buffer_dirty(bh);
2412         } while ((bh = bh->b_this_page) != head);
2413         spin_unlock(&mapping->private_lock);
2414         __set_page_dirty_nobuffers(page);
2415         if (unlikely(buffers_to_free)) {
2416                 do {
2417                         bh = buffers_to_free->b_this_page;
2418                         free_buffer_head(buffers_to_free);
2419                         buffers_to_free = bh;
2420                 } while (buffers_to_free);
2421         }
2422 }
2423
2424 #endif /* NTFS_RW */