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 | b6ad6c5 | 2005-02-15 10:08:43 +0000 | [diff] [blame] | 5 | * Copyright (c) 2001-2005 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> |
| 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 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | unsigned long flags; |
Anton Altaparmakov | e604635 | 2005-09-08 22:13:02 +0100 | [diff] [blame^] | 59 | struct buffer_head *first, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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)) { |
Anton Altaparmakov | 07a4e2d | 2005-01-12 13:08:26 +0000 | [diff] [blame] | 68 | s64 file_ofs, initialized_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | set_buffer_uptodate(bh); |
| 71 | |
| 72 | file_ofs = ((s64)page->index << PAGE_CACHE_SHIFT) + |
| 73 | bh_offset(bh); |
Anton Altaparmakov | 07a4e2d | 2005-01-12 13:08:26 +0000 | [diff] [blame] | 74 | read_lock_irqsave(&ni->size_lock, flags); |
| 75 | initialized_size = ni->initialized_size; |
| 76 | read_unlock_irqrestore(&ni->size_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | /* Check for the current buffer head overflowing. */ |
Anton Altaparmakov | 07a4e2d | 2005-01-12 13:08:26 +0000 | [diff] [blame] | 78 | if (file_ofs + bh->b_size > initialized_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | char *addr; |
| 80 | int ofs = 0; |
| 81 | |
Anton Altaparmakov | 07a4e2d | 2005-01-12 13:08:26 +0000 | [diff] [blame] | 82 | if (file_ofs < initialized_size) |
| 83 | ofs = initialized_size - file_ofs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Anton Altaparmakov | e604635 | 2005-09-08 22:13:02 +0100 | [diff] [blame^] | 91 | SetPageError(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | ntfs_error(ni->vol->sb, "Buffer I/O error, logical block %llu.", |
| 93 | (unsigned long long)bh->b_blocknr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } |
Anton Altaparmakov | e604635 | 2005-09-08 22:13:02 +0100 | [diff] [blame^] | 95 | first = page_buffers(page); |
| 96 | local_irq_save(flags); |
| 97 | bit_spin_lock(BH_Uptodate_Lock, &first->b_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Anton Altaparmakov | e604635 | 2005-09-08 22:13:02 +0100 | [diff] [blame^] | 112 | bit_spin_unlock(BH_Uptodate_Lock, &first->b_state); |
| 113 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Anton Altaparmakov | b6ad6c5 | 2005-02-15 10:08:43 +0000 | [diff] [blame] | 140 | if (likely(page_uptodate && !PageError(page))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | SetPageUptodate(page); |
| 142 | } |
| 143 | unlock_page(page); |
| 144 | return; |
| 145 | still_busy: |
Anton Altaparmakov | e604635 | 2005-09-08 22:13:02 +0100 | [diff] [blame^] | 146 | bit_spin_unlock(BH_Uptodate_Lock, &first->b_state); |
| 147 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Anton Altaparmakov | 07a4e2d | 2005-01-12 13:08:26 +0000 | [diff] [blame] | 177 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Anton Altaparmakov | a01ac53 | 2005-09-08 22:08:11 +0100 | [diff] [blame] | 191 | if (!page_has_buffers(page)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | create_empty_buffers(page, blocksize, 0); |
Anton Altaparmakov | a01ac53 | 2005-09-08 22:08:11 +0100 | [diff] [blame] | 193 | if (unlikely(!page_has_buffers(page))) { |
| 194 | unlock_page(page); |
| 195 | return -ENOMEM; |
| 196 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | } |
Anton Altaparmakov | a01ac53 | 2005-09-08 22:08:11 +0100 | [diff] [blame] | 198 | bh = head = page_buffers(page); |
| 199 | BUG_ON(!bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
| 201 | iblock = (s64)page->index << (PAGE_CACHE_SHIFT - blocksize_bits); |
Anton Altaparmakov | 07a4e2d | 2005-01-12 13:08:26 +0000 | [diff] [blame] | 202 | read_lock_irqsave(&ni->size_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | lblock = (ni->allocated_size + blocksize - 1) >> blocksize_bits; |
| 204 | zblock = (ni->initialized_size + blocksize - 1) >> blocksize_bits; |
Anton Altaparmakov | 07a4e2d | 2005-01-12 13:08:26 +0000 | [diff] [blame] | 205 | read_unlock_irqrestore(&ni->size_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
| 207 | /* Loop through all the buffers in the page. */ |
| 208 | rl = NULL; |
| 209 | nr = i = 0; |
| 210 | do { |
| 211 | u8 *kaddr; |
Anton Altaparmakov | 8273d5d | 2005-09-08 22:00:33 +0100 | [diff] [blame] | 212 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
| 214 | if (unlikely(buffer_uptodate(bh))) |
| 215 | continue; |
| 216 | if (unlikely(buffer_mapped(bh))) { |
| 217 | arr[nr++] = bh; |
| 218 | continue; |
| 219 | } |
Anton Altaparmakov | 8273d5d | 2005-09-08 22:00:33 +0100 | [diff] [blame] | 220 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Anton Altaparmakov | 9f993fe | 2005-06-25 16:15:36 +0100 | [diff] [blame] | 272 | } else if (!rl) |
| 273 | up_read(&ni->runlist.lock); |
Anton Altaparmakov | 8273d5d | 2005-09-08 22:00:33 +0100 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | /* Hard error, zero out region. */ |
Anton Altaparmakov | 8273d5d | 2005-09-08 22:00:33 +0100 | [diff] [blame] | 284 | if (!err) |
| 285 | err = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 " |
Anton Altaparmakov | 8273d5d | 2005-09-08 22:00:33 +0100 | [diff] [blame] | 292 | "(error code %i).", ni->mft_no, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | ni->type, (unsigned long long)vcn, |
| 294 | vcn_ofs, is_retry ? " even after " |
Anton Altaparmakov | 8273d5d | 2005-09-08 22:00:33 +0100 | [diff] [blame] | 295 | "retrying" : "", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | kunmap_atomic(kaddr, KM_USER0); |
Anton Altaparmakov | 8273d5d | 2005-09-08 22:00:33 +0100 | [diff] [blame] | 309 | flush_dcache_page(page); |
| 310 | if (likely(!err)) |
| 311 | set_buffer_uptodate(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | ntfs_inode *ni, *base_ni; |
| 369 | u8 *kaddr; |
| 370 | ntfs_attr_search_ctx *ctx; |
| 371 | MFT_RECORD *mrec; |
Anton Altaparmakov | b6ad6c5 | 2005-02-15 10:08:43 +0000 | [diff] [blame] | 372 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | u32 attr_len; |
| 374 | int err = 0; |
| 375 | |
Anton Altaparmakov | 905685f | 2005-03-10 11:06:19 +0000 | [diff] [blame] | 376 | retry_readpage: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Anton Altaparmakov | 311120e | 2005-09-08 22:04:20 +0100 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | /* NInoNonResident() == NInoIndexAllocPresent() */ |
| 408 | if (NInoNonResident(ni)) { |
Anton Altaparmakov | 311120e | 2005-09-08 22:04:20 +0100 | [diff] [blame] | 409 | /* Normal, non-resident data stream. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Anton Altaparmakov | 311120e | 2005-09-08 22:04:20 +0100 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | */ |
Anton Altaparmakov | b6ad6c5 | 2005-02-15 10:08:43 +0000 | [diff] [blame] | 420 | if (unlikely(page->index > 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Anton Altaparmakov | 905685f | 2005-03-10 11:06:19 +0000 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Anton Altaparmakov | b6ad6c5 | 2005-02-15 10:08:43 +0000 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Anton Altaparmakov | 07a4e2d | 2005-01-12 13:08:26 +0000 | [diff] [blame] | 507 | s64 initialized_size; |
| 508 | loff_t i_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Anton Altaparmakov | 07a4e2d | 2005-01-12 13:08:26 +0000 | [diff] [blame] | 515 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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)); |
Anton Altaparmakov | a01ac53 | 2005-09-08 22:08:11 +0100 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | } |
| 551 | bh = head = page_buffers(page); |
Anton Altaparmakov | a01ac53 | 2005-09-08 22:08:11 +0100 | [diff] [blame] | 552 | BUG_ON(!bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Anton Altaparmakov | 07a4e2d | 2005-01-12 13:08:26 +0000 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | /* The first out of bounds block for the data size. */ |
Anton Altaparmakov | 07a4e2d | 2005-01-12 13:08:26 +0000 | [diff] [blame] | 565 | dblock = (i_size + blocksize - 1) >> blocksize_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | |
| 567 | /* The last (fully or partially) initialized block. */ |
Anton Altaparmakov | 07a4e2d | 2005-01-12 13:08:26 +0000 | [diff] [blame] | 568 | iblock = initialized_size >> blocksize_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) && |
Anton Altaparmakov | 07a4e2d | 2005-01-12 13:08:26 +0000 | [diff] [blame] | 613 | (initialized_size < i_size))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Anton Altaparmakov | 8dcdeba | 2005-09-08 21:25:48 +0100 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Anton Altaparmakov | 9f993fe | 2005-06-25 16:15:36 +0100 | [diff] [blame] | 740 | } else if (!rl) |
| 741 | up_read(&ni->runlist.lock); |
Anton Altaparmakov | 8273d5d | 2005-09-08 22:00:33 +0100 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | /* Failed to map the buffer, even after retrying. */ |
Anton Altaparmakov | 8273d5d | 2005-09-08 22:00:33 +0100 | [diff] [blame] | 761 | if (!err) |
| 762 | err = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 " |
Anton Altaparmakov | 8273d5d | 2005-09-08 22:00:33 +0100 | [diff] [blame] | 767 | "determined%s (error code %i).", ni->mft_no, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | ni->type, (unsigned long long)vcn, |
| 769 | vcn_ofs, is_retry ? " even after " |
Anton Altaparmakov | 8273d5d | 2005-09-08 22:00:33 +0100 | [diff] [blame] | 770 | "retrying" : "", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Anton Altaparmakov | 54b02eb | 2005-09-08 21:43:47 +0100 | [diff] [blame] | 781 | /* Just an optimization, so ->readpage() is not called later. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | |
Anton Altaparmakov | 54b02eb | 2005-09-08 21:43:47 +0100 | [diff] [blame] | 835 | /* Submit the prepared buffers for i/o. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | bh = next; |
| 844 | } while (bh != head); |
Anton Altaparmakov | 54b02eb | 2005-09-08 21:43:47 +0100 | [diff] [blame] | 845 | unlock_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Anton Altaparmakov | d53ee32 | 2005-04-06 16:11:20 +0100 | [diff] [blame] | 890 | struct buffer_head *bhs[MAX_BUF_PER_PAGE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | runlist_element *rl; |
Anton Altaparmakov | d53ee32 | 2005-04-06 16:11:20 +0100 | [diff] [blame] | 892 | int i, nr_locked_nis, nr_recs, nr_bhs, max_bhs, bhs_per_rec, err, err2; |
| 893 | unsigned bh_size, rec_size_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | BOOL sync, is_mft, page_is_dirty, rec_is_dirty; |
Anton Altaparmakov | d53ee32 | 2005-04-06 16:11:20 +0100 | [diff] [blame] | 895 | unsigned char bh_size_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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))); |
Anton Altaparmakov | d53ee32 | 2005-04-06 16:11:20 +0100 | [diff] [blame] | 910 | bh_size_bits = vi->i_blkbits; |
| 911 | bh_size = 1 << bh_size_bits; |
| 912 | max_bhs = PAGE_CACHE_SIZE / bh_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | BUG_ON(!max_bhs); |
Anton Altaparmakov | d53ee32 | 2005-04-06 16:11:20 +0100 | [diff] [blame] | 914 | BUG_ON(max_bhs > MAX_BUF_PER_PAGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | |
| 916 | /* Were we called for sync purposes? */ |
| 917 | sync = (wbc->sync_mode == WB_SYNC_ALL); |
| 918 | |
| 919 | /* Make sure we have mapped buffers. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. */ |
Anton Altaparmakov | 07a4e2d | 2005-01-12 13:08:26 +0000 | [diff] [blame] | 933 | dblock = (i_size_read(vi) + bh_size - 1) >> bh_size_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Anton Altaparmakov | 946929d | 2005-01-13 15:26:29 +0000 | [diff] [blame] | 945 | set_buffer_uptodate(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Anton Altaparmakov | 481d037 | 2005-08-16 19:42:56 +0100 | [diff] [blame] | 983 | bh->b_bdev = vol->sb->s_bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Anton Altaparmakov | 9f993fe | 2005-06-25 16:15:36 +0100 | [diff] [blame] | 1027 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | err2 = -EIO; |
Anton Altaparmakov | 9f993fe | 2005-06-25 16:15:36 +0100 | [diff] [blame] | 1029 | if (!rl) |
| 1030 | up_read(&ni->runlist.lock); |
| 1031 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 " |
Randy Dunlap | 8907547d | 2005-03-03 11:19:53 +0000 | [diff] [blame] | 1041 | "code %lli).", |
| 1042 | (long long)block << |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Anton Altaparmakov | 149f0c5 | 2005-01-12 13:52:30 +0000 | [diff] [blame] | 1316 | struct inode *vi = page->mapping->host; |
| 1317 | ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | char *kaddr; |
Anton Altaparmakov | 149f0c5 | 2005-01-12 13:52:30 +0000 | [diff] [blame] | 1319 | ntfs_attr_search_ctx *ctx = NULL; |
| 1320 | MFT_RECORD *m = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | u32 attr_len; |
| 1322 | int err; |
| 1323 | |
Anton Altaparmakov | 905685f | 2005-03-10 11:06:19 +0000 | [diff] [blame] | 1324 | retry_writepage: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | BUG_ON(!PageLocked(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | i_size = i_size_read(vi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Anton Altaparmakov | bd45fdd | 2005-09-08 21:38:05 +0100 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | /* NInoNonResident() == NInoIndexAllocPresent() */ |
| 1374 | if (NInoNonResident(ni)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Anton Altaparmakov | bd45fdd | 2005-09-08 21:38:05 +0100 | [diff] [blame] | 1387 | /* Normal, non-resident data stream. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | return ntfs_write_block(page, wbc); |
| 1389 | } |
| 1390 | /* |
Anton Altaparmakov | bd45fdd | 2005-09-08 21:38:05 +0100 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Anton Altaparmakov | 905685f | 2005-03-10 11:06:19 +0000 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | /* |
Anton Altaparmakov | bd45fdd | 2005-09-08 21:38:05 +0100 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | attr_len = le32_to_cpu(ctx->attr->data.resident.value_length); |
Anton Altaparmakov | 07a4e2d | 2005-01-12 13:08:26 +0000 | [diff] [blame] | 1454 | i_size = i_size_read(vi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | if (unlikely(attr_len > i_size)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | attr_len = i_size; |
Anton Altaparmakov | f40661b | 2005-01-13 16:03:38 +0000 | [diff] [blame] | 1457 | ctx->attr->data.resident.value_length = cpu_to_le32(attr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | } |
Anton Altaparmakov | f40661b | 2005-01-13 16:03:38 +0000 | [diff] [blame] | 1459 | kaddr = kmap_atomic(page, KM_USER0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 " |
Anton Altaparmakov | 149f0c5 | 2005-01-12 13:52:30 +0000 | [diff] [blame] | 1489 | "error %i.", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | SetPageError(page); |
Anton Altaparmakov | 149f0c5 | 2005-01-12 13:52:30 +0000 | [diff] [blame] | 1491 | NVolSetErrors(ni->vol); |
| 1492 | make_bad_inode(vi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Anton Altaparmakov | 07a4e2d | 2005-01-12 13:08:26 +0000 | [diff] [blame] | 1511 | s64 initialized_size; |
| 1512 | loff_t i_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Anton Altaparmakov | 07a4e2d | 2005-01-12 13:08:26 +0000 | [diff] [blame] | 1519 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Anton Altaparmakov | 07a4e2d | 2005-01-12 13:08:26 +0000 | [diff] [blame] | 1551 | read_lock_irqsave(&ni->size_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | /* |
Anton Altaparmakov | b6ad6c5 | 2005-02-15 10:08:43 +0000 | [diff] [blame] | 1553 | * The first out of bounds block for the allocated size. No need to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Anton Altaparmakov | 07a4e2d | 2005-01-12 13:08:26 +0000 | [diff] [blame] | 1559 | i_size = i_size_read(vi); |
| 1560 | initialized_size = ni->initialized_size; |
| 1561 | read_unlock_irqrestore(&ni->size_lock, flags); |
| 1562 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | /* The last (fully or partially) initialized block. */ |
Anton Altaparmakov | 07a4e2d | 2005-01-12 13:08:26 +0000 | [diff] [blame] | 1564 | iblock = initialized_size >> blocksize_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) && |
Anton Altaparmakov | 07a4e2d | 2005-01-12 13:08:26 +0000 | [diff] [blame] | 1611 | (initialized_size < i_size))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Anton Altaparmakov | 9f993fe | 2005-06-25 16:15:36 +0100 | [diff] [blame] | 1715 | if (!rl) |
| 1716 | up_read(&ni->runlist.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | lcn = err; |
Anton Altaparmakov | 9f993fe | 2005-06-25 16:15:36 +0100 | [diff] [blame] | 1731 | } else if (!rl) |
| 1732 | up_read(&ni->runlist.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | /* |
| 1734 | * Failed to map the buffer, even after |
| 1735 | * retrying. |
| 1736 | */ |
| 1737 | bh->b_blocknr = -1; |
| 1738 | ntfs_error(vol->sb, "Failed to write to inode " |
| 1739 | "0x%lx, attribute type 0x%x, " |
| 1740 | "vcn 0x%llx, offset 0x%x " |
| 1741 | "because its location on disk " |
| 1742 | "could not be determined%s " |
| 1743 | "(error code %lli).", |
| 1744 | ni->mft_no, ni->type, |
| 1745 | (unsigned long long)vcn, |
| 1746 | vcn_ofs, is_retry ? " even " |
| 1747 | "after retrying" : "", |
| 1748 | (long long)lcn); |
| 1749 | if (!err) |
| 1750 | err = -EIO; |
| 1751 | goto err_out; |
| 1752 | } |
| 1753 | /* We now have a successful remap, i.e. lcn >= 0. */ |
| 1754 | |
| 1755 | /* Setup buffer head to correct block. */ |
| 1756 | bh->b_blocknr = ((lcn << vol->cluster_size_bits) |
| 1757 | + vcn_ofs) >> blocksize_bits; |
| 1758 | set_buffer_mapped(bh); |
| 1759 | |
| 1760 | // FIXME: Something analogous to this is needed for |
| 1761 | // each newly allocated block, i.e. BH_New. |
| 1762 | // FIXME: Might need to take this out of the |
| 1763 | // if (!buffer_mapped(bh)) {}, depending on how we |
| 1764 | // implement things during the allocated_size and |
| 1765 | // initialized_size extension code above. |
| 1766 | if (buffer_new(bh)) { |
| 1767 | clear_buffer_new(bh); |
| 1768 | unmap_underlying_metadata(bh->b_bdev, |
| 1769 | bh->b_blocknr); |
| 1770 | if (PageUptodate(page)) { |
| 1771 | set_buffer_uptodate(bh); |
| 1772 | continue; |
| 1773 | } |
| 1774 | /* |
| 1775 | * Page is _not_ uptodate, zero surrounding |
| 1776 | * region. NOTE: This is how we decide if to |
| 1777 | * zero or not! |
| 1778 | */ |
| 1779 | if (block_end > to || block_start < from) { |
| 1780 | void *kaddr; |
| 1781 | |
| 1782 | kaddr = kmap_atomic(page, KM_USER0); |
| 1783 | if (block_end > to) |
| 1784 | memset(kaddr + to, 0, |
| 1785 | block_end - to); |
| 1786 | if (block_start < from) |
| 1787 | memset(kaddr + block_start, 0, |
| 1788 | from - |
| 1789 | block_start); |
| 1790 | flush_dcache_page(page); |
| 1791 | kunmap_atomic(kaddr, KM_USER0); |
| 1792 | } |
| 1793 | continue; |
| 1794 | } |
| 1795 | } |
| 1796 | /* @bh is mapped, set it uptodate if the page is uptodate. */ |
| 1797 | if (PageUptodate(page)) { |
| 1798 | if (!buffer_uptodate(bh)) |
| 1799 | set_buffer_uptodate(bh); |
| 1800 | continue; |
| 1801 | } |
| 1802 | /* |
| 1803 | * The page is not uptodate. The buffer is mapped. If it is not |
| 1804 | * uptodate, and it is only partially being written to, we need |
| 1805 | * to read the buffer in before the write, i.e. right now. |
| 1806 | */ |
| 1807 | if (!buffer_uptodate(bh) && |
| 1808 | (block_start < from || block_end > to)) { |
| 1809 | ll_rw_block(READ, 1, &bh); |
| 1810 | *wait_bh++ = bh; |
| 1811 | } |
| 1812 | } while (block++, block_start = block_end, |
| 1813 | (bh = bh->b_this_page) != head); |
| 1814 | |
| 1815 | /* Release the lock if we took it. */ |
| 1816 | if (rl) { |
| 1817 | up_read(&ni->runlist.lock); |
| 1818 | rl = NULL; |
| 1819 | } |
| 1820 | |
| 1821 | /* If we issued read requests, let them complete. */ |
| 1822 | while (wait_bh > wait) { |
| 1823 | wait_on_buffer(*--wait_bh); |
| 1824 | if (!buffer_uptodate(*wait_bh)) |
| 1825 | return -EIO; |
| 1826 | } |
| 1827 | |
| 1828 | ntfs_debug("Done."); |
| 1829 | return 0; |
| 1830 | err_out: |
| 1831 | /* |
| 1832 | * Zero out any newly allocated blocks to avoid exposing stale data. |
| 1833 | * If BH_New is set, we know that the block was newly allocated in the |
| 1834 | * above loop. |
| 1835 | * FIXME: What about initialized_size increments? Have we done all the |
| 1836 | * required zeroing above? If not this error handling is broken, and |
| 1837 | * in particular the if (block_end <= from) check is completely bogus. |
| 1838 | */ |
| 1839 | bh = head; |
| 1840 | block_start = 0; |
| 1841 | is_retry = FALSE; |
| 1842 | do { |
| 1843 | block_end = block_start + blocksize; |
| 1844 | if (block_end <= from) |
| 1845 | continue; |
| 1846 | if (block_start >= to) |
| 1847 | break; |
| 1848 | if (buffer_new(bh)) { |
| 1849 | void *kaddr; |
| 1850 | |
| 1851 | clear_buffer_new(bh); |
| 1852 | kaddr = kmap_atomic(page, KM_USER0); |
| 1853 | memset(kaddr + block_start, 0, bh->b_size); |
| 1854 | kunmap_atomic(kaddr, KM_USER0); |
| 1855 | set_buffer_uptodate(bh); |
| 1856 | mark_buffer_dirty(bh); |
| 1857 | is_retry = TRUE; |
| 1858 | } |
| 1859 | } while (block_start = block_end, (bh = bh->b_this_page) != head); |
| 1860 | if (is_retry) |
| 1861 | flush_dcache_page(page); |
| 1862 | if (rl) |
| 1863 | up_read(&ni->runlist.lock); |
| 1864 | return err; |
| 1865 | } |
| 1866 | |
| 1867 | /** |
| 1868 | * ntfs_prepare_write - prepare a page for receiving data |
| 1869 | * |
| 1870 | * This is called from generic_file_write() with i_sem held on the inode |
| 1871 | * (@page->mapping->host). The @page is locked but not kmap()ped. The source |
| 1872 | * data has not yet been copied into the @page. |
| 1873 | * |
| 1874 | * Need to extend the attribute/fill in holes if necessary, create blocks and |
| 1875 | * make partially overwritten blocks uptodate, |
| 1876 | * |
| 1877 | * i_size is not to be modified yet. |
| 1878 | * |
| 1879 | * Return 0 on success or -errno on error. |
| 1880 | * |
| 1881 | * Should be using block_prepare_write() [support for sparse files] or |
| 1882 | * cont_prepare_write() [no support for sparse files]. Cannot do that due to |
| 1883 | * ntfs specifics but can look at them for implementation guidance. |
| 1884 | * |
| 1885 | * Note: In the range, @from is inclusive and @to is exclusive, i.e. @from is |
| 1886 | * the first byte in the page that will be written to and @to is the first byte |
| 1887 | * after the last byte that will be written to. |
| 1888 | */ |
| 1889 | static int ntfs_prepare_write(struct file *file, struct page *page, |
| 1890 | unsigned from, unsigned to) |
| 1891 | { |
| 1892 | s64 new_size; |
Anton Altaparmakov | f40661b | 2005-01-13 16:03:38 +0000 | [diff] [blame] | 1893 | loff_t i_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | struct inode *vi = page->mapping->host; |
| 1895 | ntfs_inode *base_ni = NULL, *ni = NTFS_I(vi); |
| 1896 | ntfs_volume *vol = ni->vol; |
| 1897 | ntfs_attr_search_ctx *ctx = NULL; |
| 1898 | MFT_RECORD *m = NULL; |
| 1899 | ATTR_RECORD *a; |
| 1900 | u8 *kaddr; |
| 1901 | u32 attr_len; |
| 1902 | int err; |
| 1903 | |
| 1904 | ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index " |
| 1905 | "0x%lx, from = %u, to = %u.", vi->i_ino, ni->type, |
| 1906 | page->index, from, to); |
| 1907 | BUG_ON(!PageLocked(page)); |
| 1908 | BUG_ON(from > PAGE_CACHE_SIZE); |
| 1909 | BUG_ON(to > PAGE_CACHE_SIZE); |
| 1910 | BUG_ON(from > to); |
| 1911 | BUG_ON(NInoMstProtected(ni)); |
| 1912 | /* |
| 1913 | * If a previous ntfs_truncate() failed, repeat it and abort if it |
| 1914 | * fails again. |
| 1915 | */ |
| 1916 | if (unlikely(NInoTruncateFailed(ni))) { |
| 1917 | down_write(&vi->i_alloc_sem); |
| 1918 | err = ntfs_truncate(vi); |
| 1919 | up_write(&vi->i_alloc_sem); |
| 1920 | if (err || NInoTruncateFailed(ni)) { |
| 1921 | if (!err) |
| 1922 | err = -EIO; |
| 1923 | goto err_out; |
| 1924 | } |
| 1925 | } |
| 1926 | /* If the attribute is not resident, deal with it elsewhere. */ |
| 1927 | if (NInoNonResident(ni)) { |
| 1928 | /* |
| 1929 | * Only unnamed $DATA attributes can be compressed, encrypted, |
| 1930 | * and/or sparse. |
| 1931 | */ |
| 1932 | if (ni->type == AT_DATA && !ni->name_len) { |
| 1933 | /* If file is encrypted, deny access, just like NT4. */ |
| 1934 | if (NInoEncrypted(ni)) { |
| 1935 | ntfs_debug("Denying write access to encrypted " |
| 1936 | "file."); |
| 1937 | return -EACCES; |
| 1938 | } |
| 1939 | /* Compressed data streams are handled in compress.c. */ |
| 1940 | if (NInoCompressed(ni)) { |
| 1941 | // TODO: Implement and replace this check with |
| 1942 | // return ntfs_write_compressed_block(page); |
| 1943 | ntfs_error(vi->i_sb, "Writing to compressed " |
| 1944 | "files is not supported yet. " |
| 1945 | "Sorry."); |
| 1946 | return -EOPNOTSUPP; |
| 1947 | } |
| 1948 | // TODO: Implement and remove this check. |
| 1949 | if (NInoSparse(ni)) { |
| 1950 | ntfs_error(vi->i_sb, "Writing to sparse files " |
| 1951 | "is not supported yet. Sorry."); |
| 1952 | return -EOPNOTSUPP; |
| 1953 | } |
| 1954 | } |
| 1955 | /* Normal data stream. */ |
| 1956 | return ntfs_prepare_nonresident_write(page, from, to); |
| 1957 | } |
| 1958 | /* |
| 1959 | * Attribute is resident, implying it is not compressed, encrypted, or |
| 1960 | * sparse. |
| 1961 | */ |
| 1962 | BUG_ON(page_has_buffers(page)); |
| 1963 | new_size = ((s64)page->index << PAGE_CACHE_SHIFT) + to; |
| 1964 | /* If we do not need to resize the attribute allocation we are done. */ |
Anton Altaparmakov | 07a4e2d | 2005-01-12 13:08:26 +0000 | [diff] [blame] | 1965 | if (new_size <= i_size_read(vi)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | /* Map, pin, and lock the (base) mft record. */ |
| 1968 | if (!NInoAttr(ni)) |
| 1969 | base_ni = ni; |
| 1970 | else |
| 1971 | base_ni = ni->ext.base_ntfs_ino; |
| 1972 | m = map_mft_record(base_ni); |
| 1973 | if (IS_ERR(m)) { |
| 1974 | err = PTR_ERR(m); |
| 1975 | m = NULL; |
| 1976 | ctx = NULL; |
| 1977 | goto err_out; |
| 1978 | } |
| 1979 | ctx = ntfs_attr_get_search_ctx(base_ni, m); |
| 1980 | if (unlikely(!ctx)) { |
| 1981 | err = -ENOMEM; |
| 1982 | goto err_out; |
| 1983 | } |
| 1984 | err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, |
| 1985 | CASE_SENSITIVE, 0, NULL, 0, ctx); |
| 1986 | if (unlikely(err)) { |
| 1987 | if (err == -ENOENT) |
| 1988 | err = -EIO; |
| 1989 | goto err_out; |
| 1990 | } |
| 1991 | m = ctx->mrec; |
| 1992 | a = ctx->attr; |
| 1993 | /* The total length of the attribute value. */ |
| 1994 | attr_len = le32_to_cpu(a->data.resident.value_length); |
Anton Altaparmakov | 946929d | 2005-01-13 15:26:29 +0000 | [diff] [blame] | 1995 | /* Fix an eventual previous failure of ntfs_commit_write(). */ |
Anton Altaparmakov | f40661b | 2005-01-13 16:03:38 +0000 | [diff] [blame] | 1996 | i_size = i_size_read(vi); |
| 1997 | if (unlikely(attr_len > i_size)) { |
| 1998 | attr_len = i_size; |
Anton Altaparmakov | 946929d | 2005-01-13 15:26:29 +0000 | [diff] [blame] | 1999 | a->data.resident.value_length = cpu_to_le32(attr_len); |
Anton Altaparmakov | 946929d | 2005-01-13 15:26:29 +0000 | [diff] [blame] | 2000 | } |
Anton Altaparmakov | 946929d | 2005-01-13 15:26:29 +0000 | [diff] [blame] | 2001 | /* If we do not need to resize the attribute allocation we are done. */ |
| 2002 | if (new_size <= attr_len) |
| 2003 | goto done_unm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | /* Check if new size is allowed in $AttrDef. */ |
| 2005 | err = ntfs_attr_size_bounds_check(vol, ni->type, new_size); |
| 2006 | if (unlikely(err)) { |
| 2007 | if (err == -ERANGE) { |
| 2008 | ntfs_error(vol->sb, "Write would cause the inode " |
| 2009 | "0x%lx to exceed the maximum size for " |
| 2010 | "its attribute type (0x%x). Aborting " |
| 2011 | "write.", vi->i_ino, |
| 2012 | le32_to_cpu(ni->type)); |
| 2013 | } else { |
| 2014 | ntfs_error(vol->sb, "Inode 0x%lx has unknown " |
| 2015 | "attribute type 0x%x. Aborting " |
| 2016 | "write.", vi->i_ino, |
| 2017 | le32_to_cpu(ni->type)); |
| 2018 | err = -EIO; |
| 2019 | } |
| 2020 | goto err_out2; |
| 2021 | } |
| 2022 | /* |
| 2023 | * Extend the attribute record to be able to store the new attribute |
| 2024 | * size. |
| 2025 | */ |
| 2026 | if (new_size >= vol->mft_record_size || ntfs_attr_record_resize(m, a, |
| 2027 | le16_to_cpu(a->data.resident.value_offset) + |
| 2028 | new_size)) { |
| 2029 | /* Not enough space in the mft record. */ |
| 2030 | ntfs_error(vol->sb, "Not enough space in the mft record for " |
| 2031 | "the resized attribute value. This is not " |
| 2032 | "supported yet. Aborting write."); |
| 2033 | err = -EOPNOTSUPP; |
| 2034 | goto err_out2; |
| 2035 | } |
| 2036 | /* |
| 2037 | * We have enough space in the mft record to fit the write. This |
| 2038 | * implies the attribute is smaller than the mft record and hence the |
| 2039 | * attribute must be in a single page and hence page->index must be 0. |
| 2040 | */ |
| 2041 | BUG_ON(page->index); |
| 2042 | /* |
| 2043 | * If the beginning of the write is past the old size, enlarge the |
| 2044 | * attribute value up to the beginning of the write and fill it with |
| 2045 | * zeroes. |
| 2046 | */ |
| 2047 | if (from > attr_len) { |
| 2048 | memset((u8*)a + le16_to_cpu(a->data.resident.value_offset) + |
| 2049 | attr_len, 0, from - attr_len); |
| 2050 | a->data.resident.value_length = cpu_to_le32(from); |
| 2051 | /* Zero the corresponding area in the page as well. */ |
| 2052 | if (PageUptodate(page)) { |
| 2053 | kaddr = kmap_atomic(page, KM_USER0); |
| 2054 | memset(kaddr + attr_len, 0, from - attr_len); |
| 2055 | kunmap_atomic(kaddr, KM_USER0); |
| 2056 | flush_dcache_page(page); |
| 2057 | } |
| 2058 | } |
| 2059 | flush_dcache_mft_record_page(ctx->ntfs_ino); |
| 2060 | mark_mft_record_dirty(ctx->ntfs_ino); |
Anton Altaparmakov | 946929d | 2005-01-13 15:26:29 +0000 | [diff] [blame] | 2061 | done_unm: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | ntfs_attr_put_search_ctx(ctx); |
| 2063 | unmap_mft_record(base_ni); |
| 2064 | /* |
| 2065 | * Because resident attributes are handled by memcpy() to/from the |
| 2066 | * corresponding MFT record, and because this form of i/o is byte |
| 2067 | * aligned rather than block aligned, there is no need to bring the |
| 2068 | * page uptodate here as in the non-resident case where we need to |
| 2069 | * bring the buffers straddled by the write uptodate before |
| 2070 | * generic_file_write() does the copying from userspace. |
| 2071 | * |
| 2072 | * We thus defer the uptodate bringing of the page region outside the |
| 2073 | * region written to to ntfs_commit_write(), which makes the code |
| 2074 | * simpler and saves one atomic kmap which is good. |
| 2075 | */ |
| 2076 | done: |
| 2077 | ntfs_debug("Done."); |
| 2078 | return 0; |
| 2079 | err_out: |
| 2080 | if (err == -ENOMEM) |
| 2081 | ntfs_warning(vi->i_sb, "Error allocating memory required to " |
| 2082 | "prepare the write."); |
| 2083 | else { |
| 2084 | ntfs_error(vi->i_sb, "Resident attribute prepare write failed " |
| 2085 | "with error %i.", err); |
| 2086 | NVolSetErrors(vol); |
| 2087 | make_bad_inode(vi); |
| 2088 | } |
| 2089 | err_out2: |
| 2090 | if (ctx) |
| 2091 | ntfs_attr_put_search_ctx(ctx); |
| 2092 | if (m) |
| 2093 | unmap_mft_record(base_ni); |
| 2094 | return err; |
| 2095 | } |
| 2096 | |
| 2097 | /** |
| 2098 | * ntfs_commit_nonresident_write - |
| 2099 | * |
| 2100 | */ |
| 2101 | static int ntfs_commit_nonresident_write(struct page *page, |
| 2102 | unsigned from, unsigned to) |
| 2103 | { |
| 2104 | s64 pos = ((s64)page->index << PAGE_CACHE_SHIFT) + to; |
| 2105 | struct inode *vi = page->mapping->host; |
| 2106 | struct buffer_head *bh, *head; |
| 2107 | unsigned int block_start, block_end, blocksize; |
| 2108 | BOOL partial; |
| 2109 | |
| 2110 | ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index " |
| 2111 | "0x%lx, from = %u, to = %u.", vi->i_ino, |
| 2112 | NTFS_I(vi)->type, page->index, from, to); |
| 2113 | blocksize = 1 << vi->i_blkbits; |
| 2114 | |
| 2115 | // FIXME: We need a whole slew of special cases in here for compressed |
| 2116 | // files for example... |
| 2117 | // For now, we know ntfs_prepare_write() would have failed so we can't |
| 2118 | // get here in any of the cases which we have to special case, so we |
| 2119 | // are just a ripped off, unrolled generic_commit_write(). |
| 2120 | |
| 2121 | bh = head = page_buffers(page); |
| 2122 | block_start = 0; |
| 2123 | partial = FALSE; |
| 2124 | do { |
| 2125 | block_end = block_start + blocksize; |
| 2126 | if (block_end <= from || block_start >= to) { |
| 2127 | if (!buffer_uptodate(bh)) |
| 2128 | partial = TRUE; |
| 2129 | } else { |
| 2130 | set_buffer_uptodate(bh); |
| 2131 | mark_buffer_dirty(bh); |
| 2132 | } |
| 2133 | } while (block_start = block_end, (bh = bh->b_this_page) != head); |
| 2134 | /* |
| 2135 | * If this is a partial write which happened to make all buffers |
| 2136 | * uptodate then we can optimize away a bogus ->readpage() for the next |
| 2137 | * read(). Here we 'discover' whether the page went uptodate as a |
| 2138 | * result of this (potentially partial) write. |
| 2139 | */ |
| 2140 | if (!partial) |
| 2141 | SetPageUptodate(page); |
| 2142 | /* |
| 2143 | * Not convinced about this at all. See disparity comment above. For |
| 2144 | * now we know ntfs_prepare_write() would have failed in the write |
| 2145 | * exceeds i_size case, so this will never trigger which is fine. |
| 2146 | */ |
Anton Altaparmakov | 07a4e2d | 2005-01-12 13:08:26 +0000 | [diff] [blame] | 2147 | if (pos > i_size_read(vi)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | ntfs_error(vi->i_sb, "Writing beyond the existing file size is " |
| 2149 | "not supported yet. Sorry."); |
| 2150 | return -EOPNOTSUPP; |
| 2151 | // vi->i_size = pos; |
| 2152 | // mark_inode_dirty(vi); |
| 2153 | } |
| 2154 | ntfs_debug("Done."); |
| 2155 | return 0; |
| 2156 | } |
| 2157 | |
| 2158 | /** |
| 2159 | * ntfs_commit_write - commit the received data |
| 2160 | * |
| 2161 | * This is called from generic_file_write() with i_sem held on the inode |
| 2162 | * (@page->mapping->host). The @page is locked but not kmap()ped. The source |
| 2163 | * data has already been copied into the @page. ntfs_prepare_write() has been |
| 2164 | * called before the data copied and it returned success so we can take the |
| 2165 | * results of various BUG checks and some error handling for granted. |
| 2166 | * |
| 2167 | * Need to mark modified blocks dirty so they get written out later when |
| 2168 | * ntfs_writepage() is invoked by the VM. |
| 2169 | * |
| 2170 | * Return 0 on success or -errno on error. |
| 2171 | * |
| 2172 | * Should be using generic_commit_write(). This marks buffers uptodate and |
| 2173 | * dirty, sets the page uptodate if all buffers in the page are uptodate, and |
| 2174 | * updates i_size if the end of io is beyond i_size. In that case, it also |
| 2175 | * marks the inode dirty. |
| 2176 | * |
| 2177 | * Cannot use generic_commit_write() due to ntfs specialities but can look at |
| 2178 | * it for implementation guidance. |
| 2179 | * |
| 2180 | * If things have gone as outlined in ntfs_prepare_write(), then we do not |
| 2181 | * need to do any page content modifications here at all, except in the write |
| 2182 | * to resident attribute case, where we need to do the uptodate bringing here |
| 2183 | * which we combine with the copying into the mft record which means we save |
| 2184 | * one atomic kmap. |
| 2185 | */ |
| 2186 | static int ntfs_commit_write(struct file *file, struct page *page, |
| 2187 | unsigned from, unsigned to) |
| 2188 | { |
| 2189 | struct inode *vi = page->mapping->host; |
| 2190 | ntfs_inode *base_ni, *ni = NTFS_I(vi); |
| 2191 | char *kaddr, *kattr; |
| 2192 | ntfs_attr_search_ctx *ctx; |
| 2193 | MFT_RECORD *m; |
| 2194 | ATTR_RECORD *a; |
| 2195 | u32 attr_len; |
| 2196 | int err; |
| 2197 | |
| 2198 | ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, page index " |
| 2199 | "0x%lx, from = %u, to = %u.", vi->i_ino, ni->type, |
| 2200 | page->index, from, to); |
| 2201 | /* If the attribute is not resident, deal with it elsewhere. */ |
| 2202 | if (NInoNonResident(ni)) { |
| 2203 | /* Only unnamed $DATA attributes can be compressed/encrypted. */ |
| 2204 | if (ni->type == AT_DATA && !ni->name_len) { |
| 2205 | /* Encrypted files need separate handling. */ |
| 2206 | if (NInoEncrypted(ni)) { |
| 2207 | // We never get here at present! |
| 2208 | BUG(); |
| 2209 | } |
| 2210 | /* Compressed data streams are handled in compress.c. */ |
| 2211 | if (NInoCompressed(ni)) { |
| 2212 | // TODO: Implement this! |
| 2213 | // return ntfs_write_compressed_block(page); |
| 2214 | // We never get here at present! |
| 2215 | BUG(); |
| 2216 | } |
| 2217 | } |
| 2218 | /* Normal data stream. */ |
| 2219 | return ntfs_commit_nonresident_write(page, from, to); |
| 2220 | } |
| 2221 | /* |
| 2222 | * Attribute is resident, implying it is not compressed, encrypted, or |
| 2223 | * sparse. |
| 2224 | */ |
| 2225 | if (!NInoAttr(ni)) |
| 2226 | base_ni = ni; |
| 2227 | else |
| 2228 | base_ni = ni->ext.base_ntfs_ino; |
| 2229 | /* Map, pin, and lock the mft record. */ |
| 2230 | m = map_mft_record(base_ni); |
| 2231 | if (IS_ERR(m)) { |
| 2232 | err = PTR_ERR(m); |
| 2233 | m = NULL; |
| 2234 | ctx = NULL; |
| 2235 | goto err_out; |
| 2236 | } |
| 2237 | ctx = ntfs_attr_get_search_ctx(base_ni, m); |
| 2238 | if (unlikely(!ctx)) { |
| 2239 | err = -ENOMEM; |
| 2240 | goto err_out; |
| 2241 | } |
| 2242 | err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, |
| 2243 | CASE_SENSITIVE, 0, NULL, 0, ctx); |
| 2244 | if (unlikely(err)) { |
| 2245 | if (err == -ENOENT) |
| 2246 | err = -EIO; |
| 2247 | goto err_out; |
| 2248 | } |
| 2249 | a = ctx->attr; |
| 2250 | /* The total length of the attribute value. */ |
| 2251 | attr_len = le32_to_cpu(a->data.resident.value_length); |
| 2252 | BUG_ON(from > attr_len); |
| 2253 | kattr = (u8*)a + le16_to_cpu(a->data.resident.value_offset); |
| 2254 | kaddr = kmap_atomic(page, KM_USER0); |
| 2255 | /* Copy the received data from the page to the mft record. */ |
| 2256 | memcpy(kattr + from, kaddr + from, to - from); |
| 2257 | /* Update the attribute length if necessary. */ |
| 2258 | if (to > attr_len) { |
| 2259 | attr_len = to; |
| 2260 | a->data.resident.value_length = cpu_to_le32(attr_len); |
| 2261 | } |
| 2262 | /* |
| 2263 | * If the page is not uptodate, bring the out of bounds area(s) |
| 2264 | * uptodate by copying data from the mft record to the page. |
| 2265 | */ |
| 2266 | if (!PageUptodate(page)) { |
| 2267 | if (from > 0) |
| 2268 | memcpy(kaddr, kattr, from); |
| 2269 | if (to < attr_len) |
| 2270 | memcpy(kaddr + to, kattr + to, attr_len - to); |
| 2271 | /* Zero the region outside the end of the attribute value. */ |
| 2272 | if (attr_len < PAGE_CACHE_SIZE) |
| 2273 | memset(kaddr + attr_len, 0, PAGE_CACHE_SIZE - attr_len); |
| 2274 | /* |
| 2275 | * The probability of not having done any of the above is |
| 2276 | * extremely small, so we just flush unconditionally. |
| 2277 | */ |
| 2278 | flush_dcache_page(page); |
| 2279 | SetPageUptodate(page); |
| 2280 | } |
| 2281 | kunmap_atomic(kaddr, KM_USER0); |
| 2282 | /* Update i_size if necessary. */ |
Anton Altaparmakov | 07a4e2d | 2005-01-12 13:08:26 +0000 | [diff] [blame] | 2283 | if (i_size_read(vi) < attr_len) { |
| 2284 | unsigned long flags; |
| 2285 | |
| 2286 | write_lock_irqsave(&ni->size_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | ni->allocated_size = ni->initialized_size = attr_len; |
| 2288 | i_size_write(vi, attr_len); |
Anton Altaparmakov | 07a4e2d | 2005-01-12 13:08:26 +0000 | [diff] [blame] | 2289 | write_unlock_irqrestore(&ni->size_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | } |
| 2291 | /* Mark the mft record dirty, so it gets written back. */ |
| 2292 | flush_dcache_mft_record_page(ctx->ntfs_ino); |
| 2293 | mark_mft_record_dirty(ctx->ntfs_ino); |
| 2294 | ntfs_attr_put_search_ctx(ctx); |
| 2295 | unmap_mft_record(base_ni); |
| 2296 | ntfs_debug("Done."); |
| 2297 | return 0; |
| 2298 | err_out: |
| 2299 | if (err == -ENOMEM) { |
| 2300 | ntfs_warning(vi->i_sb, "Error allocating memory required to " |
| 2301 | "commit the write."); |
| 2302 | if (PageUptodate(page)) { |
| 2303 | ntfs_warning(vi->i_sb, "Page is uptodate, setting " |
| 2304 | "dirty so the write will be retried " |
| 2305 | "later on by the VM."); |
| 2306 | /* |
| 2307 | * Put the page on mapping->dirty_pages, but leave its |
| 2308 | * buffers' dirty state as-is. |
| 2309 | */ |
| 2310 | __set_page_dirty_nobuffers(page); |
| 2311 | err = 0; |
| 2312 | } else |
| 2313 | ntfs_error(vi->i_sb, "Page is not uptodate. Written " |
| 2314 | "data has been lost."); |
| 2315 | } else { |
| 2316 | ntfs_error(vi->i_sb, "Resident attribute commit write failed " |
| 2317 | "with error %i.", err); |
| 2318 | NVolSetErrors(ni->vol); |
| 2319 | make_bad_inode(vi); |
| 2320 | } |
| 2321 | if (ctx) |
| 2322 | ntfs_attr_put_search_ctx(ctx); |
| 2323 | if (m) |
| 2324 | unmap_mft_record(base_ni); |
| 2325 | return err; |
| 2326 | } |
| 2327 | |
| 2328 | #endif /* NTFS_RW */ |
| 2329 | |
| 2330 | /** |
| 2331 | * ntfs_aops - general address space operations for inodes and attributes |
| 2332 | */ |
| 2333 | struct address_space_operations ntfs_aops = { |
| 2334 | .readpage = ntfs_readpage, /* Fill page with data. */ |
| 2335 | .sync_page = block_sync_page, /* Currently, just unplugs the |
| 2336 | disk request queue. */ |
| 2337 | #ifdef NTFS_RW |
| 2338 | .writepage = ntfs_writepage, /* Write dirty page to disk. */ |
| 2339 | .prepare_write = ntfs_prepare_write, /* Prepare page and buffers |
| 2340 | ready to receive data. */ |
| 2341 | .commit_write = ntfs_commit_write, /* Commit received data. */ |
| 2342 | #endif /* NTFS_RW */ |
| 2343 | }; |
| 2344 | |
| 2345 | /** |
| 2346 | * ntfs_mst_aops - general address space operations for mst protecteed inodes |
| 2347 | * and attributes |
| 2348 | */ |
| 2349 | struct address_space_operations ntfs_mst_aops = { |
| 2350 | .readpage = ntfs_readpage, /* Fill page with data. */ |
| 2351 | .sync_page = block_sync_page, /* Currently, just unplugs the |
| 2352 | disk request queue. */ |
| 2353 | #ifdef NTFS_RW |
| 2354 | .writepage = ntfs_writepage, /* Write dirty page to disk. */ |
| 2355 | .set_page_dirty = __set_page_dirty_nobuffers, /* Set the page dirty |
| 2356 | without touching the buffers |
| 2357 | belonging to the page. */ |
| 2358 | #endif /* NTFS_RW */ |
| 2359 | }; |
| 2360 | |
| 2361 | #ifdef NTFS_RW |
| 2362 | |
| 2363 | /** |
| 2364 | * mark_ntfs_record_dirty - mark an ntfs record dirty |
| 2365 | * @page: page containing the ntfs record to mark dirty |
| 2366 | * @ofs: byte offset within @page at which the ntfs record begins |
| 2367 | * |
| 2368 | * Set the buffers and the page in which the ntfs record is located dirty. |
| 2369 | * |
| 2370 | * The latter also marks the vfs inode the ntfs record belongs to dirty |
| 2371 | * (I_DIRTY_PAGES only). |
| 2372 | * |
| 2373 | * If the page does not have buffers, we create them and set them uptodate. |
| 2374 | * The page may not be locked which is why we need to handle the buffers under |
| 2375 | * the mapping->private_lock. Once the buffers are marked dirty we no longer |
| 2376 | * need the lock since try_to_free_buffers() does not free dirty buffers. |
| 2377 | */ |
| 2378 | void mark_ntfs_record_dirty(struct page *page, const unsigned int ofs) { |
| 2379 | struct address_space *mapping = page->mapping; |
| 2380 | ntfs_inode *ni = NTFS_I(mapping->host); |
| 2381 | struct buffer_head *bh, *head, *buffers_to_free = NULL; |
| 2382 | unsigned int end, bh_size, bh_ofs; |
| 2383 | |
| 2384 | BUG_ON(!PageUptodate(page)); |
| 2385 | end = ofs + ni->itype.index.block_size; |
| 2386 | bh_size = 1 << VFS_I(ni)->i_blkbits; |
| 2387 | spin_lock(&mapping->private_lock); |
| 2388 | if (unlikely(!page_has_buffers(page))) { |
| 2389 | spin_unlock(&mapping->private_lock); |
| 2390 | bh = head = alloc_page_buffers(page, bh_size, 1); |
| 2391 | spin_lock(&mapping->private_lock); |
| 2392 | if (likely(!page_has_buffers(page))) { |
| 2393 | struct buffer_head *tail; |
| 2394 | |
| 2395 | do { |
| 2396 | set_buffer_uptodate(bh); |
| 2397 | tail = bh; |
| 2398 | bh = bh->b_this_page; |
| 2399 | } while (bh); |
| 2400 | tail->b_this_page = head; |
| 2401 | attach_page_buffers(page, head); |
| 2402 | } else |
| 2403 | buffers_to_free = bh; |
| 2404 | } |
| 2405 | bh = head = page_buffers(page); |
Anton Altaparmakov | a01ac53 | 2005-09-08 22:08:11 +0100 | [diff] [blame] | 2406 | BUG_ON(!bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | do { |
| 2408 | bh_ofs = bh_offset(bh); |
| 2409 | if (bh_ofs + bh_size <= ofs) |
| 2410 | continue; |
| 2411 | if (unlikely(bh_ofs >= end)) |
| 2412 | break; |
| 2413 | set_buffer_dirty(bh); |
| 2414 | } while ((bh = bh->b_this_page) != head); |
| 2415 | spin_unlock(&mapping->private_lock); |
| 2416 | __set_page_dirty_nobuffers(page); |
| 2417 | if (unlikely(buffers_to_free)) { |
| 2418 | do { |
| 2419 | bh = buffers_to_free->b_this_page; |
| 2420 | free_buffer_head(buffers_to_free); |
| 2421 | buffers_to_free = bh; |
| 2422 | } while (buffers_to_free); |
| 2423 | } |
| 2424 | } |
| 2425 | |
| 2426 | #endif /* NTFS_RW */ |