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