Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 19 | #include <linux/fs.h> |
| 20 | #include <linux/pagemap.h> |
| 21 | #include <linux/highmem.h> |
| 22 | #include <linux/time.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/string.h> |
| 25 | #include <linux/smp_lock.h> |
| 26 | #include <linux/backing-dev.h> |
| 27 | #include <linux/mpage.h> |
| 28 | #include <linux/swap.h> |
| 29 | #include <linux/writeback.h> |
| 30 | #include <linux/statfs.h> |
| 31 | #include <linux/compat.h> |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 32 | #include <linux/version.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 33 | #include "ctree.h" |
| 34 | #include "disk-io.h" |
| 35 | #include "transaction.h" |
| 36 | #include "btrfs_inode.h" |
| 37 | #include "ioctl.h" |
| 38 | #include "print-tree.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 39 | #include "tree-log.h" |
| 40 | #include "locking.h" |
Jeff Mahoney | 12fa8ec | 2008-05-02 15:03:58 -0400 | [diff] [blame] | 41 | #include "compat.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 42 | |
| 43 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 44 | /* simple helper to fault in pages and copy. This should go away |
| 45 | * and be replaced with calls into generic code. |
| 46 | */ |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 47 | static int noinline btrfs_copy_from_user(loff_t pos, int num_pages, |
| 48 | int write_bytes, |
| 49 | struct page **prepared_pages, |
| 50 | const char __user * buf) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 51 | { |
| 52 | long page_fault = 0; |
| 53 | int i; |
| 54 | int offset = pos & (PAGE_CACHE_SIZE - 1); |
| 55 | |
| 56 | for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) { |
| 57 | size_t count = min_t(size_t, |
| 58 | PAGE_CACHE_SIZE - offset, write_bytes); |
| 59 | struct page *page = prepared_pages[i]; |
| 60 | fault_in_pages_readable(buf, count); |
| 61 | |
| 62 | /* Copy data from userspace to the current page */ |
| 63 | kmap(page); |
| 64 | page_fault = __copy_from_user(page_address(page) + offset, |
| 65 | buf, count); |
| 66 | /* Flush processor's dcache for this page */ |
| 67 | flush_dcache_page(page); |
| 68 | kunmap(page); |
| 69 | buf += count; |
| 70 | write_bytes -= count; |
| 71 | |
| 72 | if (page_fault) |
| 73 | break; |
| 74 | } |
| 75 | return page_fault ? -EFAULT : 0; |
| 76 | } |
| 77 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 78 | /* |
| 79 | * unlocks pages after btrfs_file_write is done with them |
| 80 | */ |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 81 | static void noinline btrfs_drop_pages(struct page **pages, size_t num_pages) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 82 | { |
| 83 | size_t i; |
| 84 | for (i = 0; i < num_pages; i++) { |
| 85 | if (!pages[i]) |
| 86 | break; |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 87 | /* page checked is some magic around finding pages that |
| 88 | * have been modified without going through btrfs_set_page_dirty |
| 89 | * clear it here |
| 90 | */ |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 91 | ClearPageChecked(pages[i]); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 92 | unlock_page(pages[i]); |
| 93 | mark_page_accessed(pages[i]); |
| 94 | page_cache_release(pages[i]); |
| 95 | } |
| 96 | } |
| 97 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 98 | /* this does all the hard work for inserting an inline extent into |
| 99 | * the btree. Any existing inline extent is extended as required to make room, |
| 100 | * otherwise things are inserted as required into the btree |
| 101 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 102 | static int noinline insert_inline_extent(struct btrfs_trans_handle *trans, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 103 | struct btrfs_root *root, struct inode *inode, |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 104 | u64 offset, size_t size, |
| 105 | struct page **pages, size_t page_offset, |
| 106 | int num_pages) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 107 | { |
| 108 | struct btrfs_key key; |
| 109 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 110 | struct extent_buffer *leaf; |
| 111 | char *kaddr; |
| 112 | unsigned long ptr; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 113 | struct btrfs_file_extent_item *ei; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 114 | struct page *page; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 115 | u32 datasize; |
| 116 | int err = 0; |
| 117 | int ret; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 118 | int i; |
| 119 | ssize_t cur_size; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 120 | |
| 121 | path = btrfs_alloc_path(); |
| 122 | if (!path) |
| 123 | return -ENOMEM; |
| 124 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 125 | btrfs_set_trans_block_group(trans, inode); |
| 126 | |
| 127 | key.objectid = inode->i_ino; |
| 128 | key.offset = offset; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 129 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 130 | |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 131 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 132 | if (ret < 0) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 133 | err = ret; |
| 134 | goto fail; |
| 135 | } |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 136 | if (ret == 1) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 137 | struct btrfs_key found_key; |
| 138 | |
| 139 | if (path->slots[0] == 0) |
| 140 | goto insert; |
| 141 | |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 142 | path->slots[0]--; |
| 143 | leaf = path->nodes[0]; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 144 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 145 | |
| 146 | if (found_key.objectid != inode->i_ino) |
| 147 | goto insert; |
| 148 | |
| 149 | if (found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 150 | goto insert; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 151 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 152 | struct btrfs_file_extent_item); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 153 | |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 154 | if (btrfs_file_extent_type(leaf, ei) != |
| 155 | BTRFS_FILE_EXTENT_INLINE) { |
| 156 | goto insert; |
| 157 | } |
| 158 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 159 | ret = 0; |
| 160 | } |
| 161 | if (ret == 0) { |
| 162 | u32 found_size; |
Yan | 18f16f7 | 2007-10-25 15:42:57 -0400 | [diff] [blame] | 163 | u64 found_end; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 164 | |
| 165 | leaf = path->nodes[0]; |
| 166 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 167 | struct btrfs_file_extent_item); |
| 168 | |
| 169 | if (btrfs_file_extent_type(leaf, ei) != |
| 170 | BTRFS_FILE_EXTENT_INLINE) { |
| 171 | err = ret; |
| 172 | btrfs_print_leaf(root, leaf); |
| 173 | printk("found wasn't inline offset %Lu inode %lu\n", |
| 174 | offset, inode->i_ino); |
| 175 | goto fail; |
| 176 | } |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 177 | found_size = btrfs_file_extent_inline_len(leaf, |
| 178 | btrfs_item_nr(leaf, path->slots[0])); |
Yan | 18f16f7 | 2007-10-25 15:42:57 -0400 | [diff] [blame] | 179 | found_end = key.offset + found_size; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 180 | |
Yan | 18f16f7 | 2007-10-25 15:42:57 -0400 | [diff] [blame] | 181 | if (found_end < offset + size) { |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 182 | btrfs_release_path(root, path); |
| 183 | ret = btrfs_search_slot(trans, root, &key, path, |
Yan | 18f16f7 | 2007-10-25 15:42:57 -0400 | [diff] [blame] | 184 | offset + size - found_end, 1); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 185 | BUG_ON(ret != 0); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 186 | |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 187 | ret = btrfs_extend_item(trans, root, path, |
Yan | 18f16f7 | 2007-10-25 15:42:57 -0400 | [diff] [blame] | 188 | offset + size - found_end); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 189 | if (ret) { |
| 190 | err = ret; |
| 191 | goto fail; |
| 192 | } |
| 193 | leaf = path->nodes[0]; |
| 194 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 195 | struct btrfs_file_extent_item); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame^] | 196 | inode_add_bytes(inode, offset + size - found_end); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 197 | } |
Yan | 18f16f7 | 2007-10-25 15:42:57 -0400 | [diff] [blame] | 198 | if (found_end < offset) { |
| 199 | ptr = btrfs_file_extent_inline_start(ei) + found_size; |
| 200 | memset_extent_buffer(leaf, 0, ptr, offset - found_end); |
| 201 | } |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 202 | } else { |
| 203 | insert: |
| 204 | btrfs_release_path(root, path); |
Yan | 18f16f7 | 2007-10-25 15:42:57 -0400 | [diff] [blame] | 205 | datasize = offset + size - key.offset; |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame^] | 206 | inode_add_bytes(inode, datasize); |
Yan | 18f16f7 | 2007-10-25 15:42:57 -0400 | [diff] [blame] | 207 | datasize = btrfs_file_extent_calc_inline_size(datasize); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 208 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 209 | datasize); |
| 210 | if (ret) { |
| 211 | err = ret; |
| 212 | printk("got bad ret %d\n", ret); |
| 213 | goto fail; |
| 214 | } |
| 215 | leaf = path->nodes[0]; |
| 216 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 217 | struct btrfs_file_extent_item); |
| 218 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 219 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
| 220 | } |
Yan | 18f16f7 | 2007-10-25 15:42:57 -0400 | [diff] [blame] | 221 | ptr = btrfs_file_extent_inline_start(ei) + offset - key.offset; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 222 | |
| 223 | cur_size = size; |
| 224 | i = 0; |
| 225 | while (size > 0) { |
| 226 | page = pages[i]; |
| 227 | kaddr = kmap_atomic(page, KM_USER0); |
Jens Axboe | ae2f541 | 2007-10-19 09:22:59 -0400 | [diff] [blame] | 228 | cur_size = min_t(size_t, PAGE_CACHE_SIZE - page_offset, size); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 229 | write_extent_buffer(leaf, kaddr + page_offset, ptr, cur_size); |
| 230 | kunmap_atomic(kaddr, KM_USER0); |
| 231 | page_offset = 0; |
| 232 | ptr += cur_size; |
| 233 | size -= cur_size; |
| 234 | if (i >= num_pages) { |
| 235 | printk("i %d num_pages %d\n", i, num_pages); |
| 236 | } |
| 237 | i++; |
| 238 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 239 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 240 | fail: |
| 241 | btrfs_free_path(path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 242 | return err; |
| 243 | } |
| 244 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 245 | /* |
| 246 | * after copy_from_user, pages need to be dirtied and we need to make |
| 247 | * sure holes are created between the current EOF and the start of |
| 248 | * any next extents (if required). |
| 249 | * |
| 250 | * this also makes the decision about creating an inline extent vs |
| 251 | * doing real data extents, marking pages dirty and delalloc as required. |
| 252 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 253 | static int noinline dirty_and_release_pages(struct btrfs_trans_handle *trans, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 254 | struct btrfs_root *root, |
| 255 | struct file *file, |
| 256 | struct page **pages, |
| 257 | size_t num_pages, |
| 258 | loff_t pos, |
| 259 | size_t write_bytes) |
| 260 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 261 | int err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 262 | int i; |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 263 | struct inode *inode = fdentry(file)->d_inode; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 264 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 265 | u64 hint_byte; |
| 266 | u64 num_bytes; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 267 | u64 start_pos; |
| 268 | u64 end_of_last_block; |
| 269 | u64 end_pos = pos + write_bytes; |
Yan | dcfec0d | 2007-11-06 10:26:26 -0500 | [diff] [blame] | 270 | u64 inline_size; |
Chris Mason | ee6e650 | 2008-07-17 12:54:40 -0400 | [diff] [blame] | 271 | int did_inline = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 272 | loff_t isize = i_size_read(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 273 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 274 | start_pos = pos & ~((u64)root->sectorsize - 1); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 275 | num_bytes = (write_bytes + pos - start_pos + |
| 276 | root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 277 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 278 | end_of_last_block = start_pos + num_bytes - 1; |
| 279 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 280 | lock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS); |
Chris Mason | 37d1aee | 2008-07-31 10:48:37 -0400 | [diff] [blame] | 281 | trans = btrfs_join_transaction(root, 1); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 282 | if (!trans) { |
| 283 | err = -ENOMEM; |
| 284 | goto out_unlock; |
| 285 | } |
| 286 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 287 | hint_byte = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 288 | |
| 289 | if ((end_of_last_block & 4095) == 0) { |
Christoph Hellwig | 9433063 | 2007-09-10 20:02:22 -0400 | [diff] [blame] | 290 | printk("strange end of last %Lu %zu %Lu\n", start_pos, write_bytes, end_of_last_block); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 291 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 292 | set_extent_uptodate(io_tree, start_pos, end_of_last_block, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 293 | |
| 294 | /* FIXME...EIEIO, ENOSPC and more */ |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 295 | /* insert any holes we need to create */ |
Chris Mason | 1b1e213 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 296 | if (isize < start_pos) { |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 297 | u64 last_pos_in_file; |
| 298 | u64 hole_size; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 299 | u64 mask = root->sectorsize - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 300 | last_pos_in_file = (isize + mask) & ~mask; |
Chris Mason | 1b1e213 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 301 | hole_size = (start_pos - last_pos_in_file + mask) & ~mask; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 302 | if (hole_size > 0) { |
| 303 | btrfs_wait_ordered_range(inode, last_pos_in_file, |
| 304 | last_pos_in_file + hole_size); |
Chris Mason | ee6e650 | 2008-07-17 12:54:40 -0400 | [diff] [blame] | 305 | mutex_lock(&BTRFS_I(inode)->extent_mutex); |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 306 | err = btrfs_drop_extents(trans, root, inode, |
| 307 | last_pos_in_file, |
| 308 | last_pos_in_file + hole_size, |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 309 | last_pos_in_file, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 310 | &hint_byte); |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 311 | if (err) |
| 312 | goto failed; |
| 313 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 314 | err = btrfs_insert_file_extent(trans, root, |
| 315 | inode->i_ino, |
| 316 | last_pos_in_file, |
Sage Weil | f2eb0a2 | 2008-05-02 14:43:14 -0400 | [diff] [blame] | 317 | 0, 0, hole_size, 0); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 318 | btrfs_drop_extent_cache(inode, last_pos_in_file, |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 319 | last_pos_in_file + hole_size - 1, 0); |
Chris Mason | ee6e650 | 2008-07-17 12:54:40 -0400 | [diff] [blame] | 320 | mutex_unlock(&BTRFS_I(inode)->extent_mutex); |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 321 | btrfs_check_file(root, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 322 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 323 | if (err) |
| 324 | goto failed; |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * either allocate an extent for the new bytes or setup the key |
| 329 | * to show we are doing inline data in the extent |
| 330 | */ |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 331 | inline_size = end_pos; |
| 332 | if (isize >= BTRFS_MAX_INLINE_DATA_SIZE(root) || |
Chris Mason | 6f568d3 | 2008-01-29 16:03:38 -0500 | [diff] [blame] | 333 | inline_size > root->fs_info->max_inline || |
| 334 | (inline_size & (root->sectorsize -1)) == 0 || |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 335 | inline_size >= BTRFS_MAX_INLINE_DATA_SIZE(root)) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 336 | /* check for reserved extents on each page, we don't want |
| 337 | * to reset the delalloc bit on things that already have |
| 338 | * extents reserved. |
| 339 | */ |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 340 | btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 341 | for (i = 0; i < num_pages; i++) { |
| 342 | struct page *p = pages[i]; |
| 343 | SetPageUptodate(p); |
Chris Mason | 247e743 | 2008-07-17 12:53:51 -0400 | [diff] [blame] | 344 | ClearPageChecked(p); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 345 | set_page_dirty(p); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 346 | } |
| 347 | } else { |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 348 | u64 aligned_end; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 349 | /* step one, delete the existing extents in this range */ |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 350 | aligned_end = (pos + write_bytes + root->sectorsize - 1) & |
| 351 | ~((u64)root->sectorsize - 1); |
Chris Mason | ee6e650 | 2008-07-17 12:54:40 -0400 | [diff] [blame] | 352 | mutex_lock(&BTRFS_I(inode)->extent_mutex); |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 353 | err = btrfs_drop_extents(trans, root, inode, start_pos, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 354 | aligned_end, aligned_end, &hint_byte); |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 355 | if (err) |
| 356 | goto failed; |
Yan | dcfec0d | 2007-11-06 10:26:26 -0500 | [diff] [blame] | 357 | if (isize > inline_size) |
| 358 | inline_size = min_t(u64, isize, aligned_end); |
| 359 | inline_size -= start_pos; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 360 | err = insert_inline_extent(trans, root, inode, start_pos, |
Yan | dcfec0d | 2007-11-06 10:26:26 -0500 | [diff] [blame] | 361 | inline_size, pages, 0, num_pages); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 362 | btrfs_drop_extent_cache(inode, start_pos, aligned_end - 1, 0); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 363 | BUG_ON(err); |
Chris Mason | ee6e650 | 2008-07-17 12:54:40 -0400 | [diff] [blame] | 364 | mutex_unlock(&BTRFS_I(inode)->extent_mutex); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 365 | |
| 366 | /* |
| 367 | * an ugly way to do all the prop accounting around |
| 368 | * the page bits and mapping tags |
| 369 | */ |
| 370 | set_page_writeback(pages[0]); |
| 371 | end_page_writeback(pages[0]); |
Chris Mason | ee6e650 | 2008-07-17 12:54:40 -0400 | [diff] [blame] | 372 | did_inline = 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 373 | } |
| 374 | if (end_pos > isize) { |
| 375 | i_size_write(inode, end_pos); |
Chris Mason | ee6e650 | 2008-07-17 12:54:40 -0400 | [diff] [blame] | 376 | if (did_inline) |
| 377 | BTRFS_I(inode)->disk_i_size = end_pos; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 378 | btrfs_update_inode(trans, root, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 379 | } |
| 380 | failed: |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 381 | err = btrfs_end_transaction(trans, root); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 382 | out_unlock: |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 383 | unlock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 384 | return err; |
| 385 | } |
| 386 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 387 | /* |
| 388 | * this drops all the extents in the cache that intersect the range |
| 389 | * [start, end]. Existing extents are split as required. |
| 390 | */ |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 391 | int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end, |
| 392 | int skip_pinned) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 393 | { |
| 394 | struct extent_map *em; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 395 | struct extent_map *split = NULL; |
| 396 | struct extent_map *split2 = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 397 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 398 | u64 len = end - start + 1; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 399 | int ret; |
| 400 | int testend = 1; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 401 | unsigned long flags; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 402 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 403 | WARN_ON(end < start); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 404 | if (end == (u64)-1) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 405 | len = (u64)-1; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 406 | testend = 0; |
| 407 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 408 | while(1) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 409 | if (!split) |
| 410 | split = alloc_extent_map(GFP_NOFS); |
| 411 | if (!split2) |
| 412 | split2 = alloc_extent_map(GFP_NOFS); |
| 413 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 414 | spin_lock(&em_tree->lock); |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 415 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 416 | if (!em) { |
| 417 | spin_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 418 | break; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 419 | } |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 420 | flags = em->flags; |
| 421 | if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) { |
| 422 | spin_unlock(&em_tree->lock); |
| 423 | if (em->start <= start && |
| 424 | (!testend || em->start + em->len >= start + len)) { |
| 425 | free_extent_map(em); |
| 426 | break; |
| 427 | } |
| 428 | if (start < em->start) { |
| 429 | len = em->start - start; |
| 430 | } else { |
| 431 | len = start + len - (em->start + em->len); |
| 432 | start = em->start + em->len; |
| 433 | } |
| 434 | free_extent_map(em); |
| 435 | continue; |
| 436 | } |
Chris Mason | 3ce7e67 | 2008-07-31 15:42:54 -0400 | [diff] [blame] | 437 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 438 | remove_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 439 | |
| 440 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 441 | em->start < start) { |
| 442 | split->start = em->start; |
| 443 | split->len = start - em->start; |
| 444 | split->block_start = em->block_start; |
| 445 | split->bdev = em->bdev; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 446 | split->flags = flags; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 447 | ret = add_extent_mapping(em_tree, split); |
| 448 | BUG_ON(ret); |
| 449 | free_extent_map(split); |
| 450 | split = split2; |
| 451 | split2 = NULL; |
| 452 | } |
| 453 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 454 | testend && em->start + em->len > start + len) { |
| 455 | u64 diff = start + len - em->start; |
| 456 | |
| 457 | split->start = start + len; |
| 458 | split->len = em->start + em->len - (start + len); |
| 459 | split->bdev = em->bdev; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 460 | split->flags = flags; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 461 | |
| 462 | split->block_start = em->block_start + diff; |
| 463 | |
| 464 | ret = add_extent_mapping(em_tree, split); |
| 465 | BUG_ON(ret); |
| 466 | free_extent_map(split); |
| 467 | split = NULL; |
| 468 | } |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 469 | spin_unlock(&em_tree->lock); |
| 470 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 471 | /* once for us */ |
| 472 | free_extent_map(em); |
| 473 | /* once for the tree*/ |
| 474 | free_extent_map(em); |
| 475 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 476 | if (split) |
| 477 | free_extent_map(split); |
| 478 | if (split2) |
| 479 | free_extent_map(split2); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 480 | return 0; |
| 481 | } |
| 482 | |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 483 | int btrfs_check_file(struct btrfs_root *root, struct inode *inode) |
| 484 | { |
| 485 | return 0; |
| 486 | #if 0 |
| 487 | struct btrfs_path *path; |
| 488 | struct btrfs_key found_key; |
| 489 | struct extent_buffer *leaf; |
| 490 | struct btrfs_file_extent_item *extent; |
| 491 | u64 last_offset = 0; |
| 492 | int nritems; |
| 493 | int slot; |
| 494 | int found_type; |
| 495 | int ret; |
| 496 | int err = 0; |
| 497 | u64 extent_end = 0; |
| 498 | |
| 499 | path = btrfs_alloc_path(); |
| 500 | ret = btrfs_lookup_file_extent(NULL, root, path, inode->i_ino, |
| 501 | last_offset, 0); |
| 502 | while(1) { |
| 503 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 504 | if (path->slots[0] >= nritems) { |
| 505 | ret = btrfs_next_leaf(root, path); |
| 506 | if (ret) |
| 507 | goto out; |
| 508 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 509 | } |
| 510 | slot = path->slots[0]; |
| 511 | leaf = path->nodes[0]; |
| 512 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 513 | if (found_key.objectid != inode->i_ino) |
| 514 | break; |
| 515 | if (found_key.type != BTRFS_EXTENT_DATA_KEY) |
| 516 | goto out; |
| 517 | |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 518 | if (found_key.offset < last_offset) { |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 519 | WARN_ON(1); |
| 520 | btrfs_print_leaf(root, leaf); |
| 521 | printk("inode %lu found offset %Lu expected %Lu\n", |
| 522 | inode->i_ino, found_key.offset, last_offset); |
| 523 | err = 1; |
| 524 | goto out; |
| 525 | } |
| 526 | extent = btrfs_item_ptr(leaf, slot, |
| 527 | struct btrfs_file_extent_item); |
| 528 | found_type = btrfs_file_extent_type(leaf, extent); |
| 529 | if (found_type == BTRFS_FILE_EXTENT_REG) { |
| 530 | extent_end = found_key.offset + |
| 531 | btrfs_file_extent_num_bytes(leaf, extent); |
| 532 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 533 | struct btrfs_item *item; |
| 534 | item = btrfs_item_nr(leaf, slot); |
| 535 | extent_end = found_key.offset + |
| 536 | btrfs_file_extent_inline_len(leaf, item); |
| 537 | extent_end = (extent_end + root->sectorsize - 1) & |
| 538 | ~((u64)root->sectorsize -1 ); |
| 539 | } |
| 540 | last_offset = extent_end; |
| 541 | path->slots[0]++; |
| 542 | } |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 543 | if (0 && last_offset < inode->i_size) { |
Chris Mason | 5f56406 | 2008-01-22 16:47:59 -0500 | [diff] [blame] | 544 | WARN_ON(1); |
| 545 | btrfs_print_leaf(root, leaf); |
| 546 | printk("inode %lu found offset %Lu size %Lu\n", inode->i_ino, |
| 547 | last_offset, inode->i_size); |
| 548 | err = 1; |
| 549 | |
| 550 | } |
| 551 | out: |
| 552 | btrfs_free_path(path); |
| 553 | return err; |
| 554 | #endif |
| 555 | } |
| 556 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 557 | /* |
| 558 | * this is very complex, but the basic idea is to drop all extents |
| 559 | * in the range start - end. hint_block is filled in with a block number |
| 560 | * that would be a good hint to the block allocator for this file. |
| 561 | * |
| 562 | * If an extent intersects the range but is not entirely inside the range |
| 563 | * it is either truncated or split. Anything entirely inside the range |
| 564 | * is deleted from the tree. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 565 | * |
| 566 | * inline_limit is used to tell this code which offsets in the file to keep |
| 567 | * if they contain inline extents. |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 568 | */ |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 569 | int noinline btrfs_drop_extents(struct btrfs_trans_handle *trans, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 570 | struct btrfs_root *root, struct inode *inode, |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 571 | u64 start, u64 end, u64 inline_limit, u64 *hint_byte) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 572 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 573 | u64 extent_end = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 574 | u64 search_start = start; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 575 | u64 leaf_start; |
| 576 | u64 root_gen; |
| 577 | u64 root_owner; |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 578 | struct extent_buffer *leaf; |
| 579 | struct btrfs_file_extent_item *extent; |
| 580 | struct btrfs_path *path; |
| 581 | struct btrfs_key key; |
| 582 | struct btrfs_file_extent_item old; |
| 583 | int keep; |
| 584 | int slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 585 | int bookend; |
| 586 | int found_type; |
| 587 | int found_extent; |
| 588 | int found_inline; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 589 | int recow; |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 590 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 591 | |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 592 | btrfs_drop_extent_cache(inode, start, end - 1, 0); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 593 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 594 | path = btrfs_alloc_path(); |
| 595 | if (!path) |
| 596 | return -ENOMEM; |
| 597 | while(1) { |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 598 | recow = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 599 | btrfs_release_path(root, path); |
| 600 | ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, |
| 601 | search_start, -1); |
| 602 | if (ret < 0) |
| 603 | goto out; |
| 604 | if (ret > 0) { |
| 605 | if (path->slots[0] == 0) { |
| 606 | ret = 0; |
| 607 | goto out; |
| 608 | } |
| 609 | path->slots[0]--; |
| 610 | } |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 611 | next_slot: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 612 | keep = 0; |
| 613 | bookend = 0; |
| 614 | found_extent = 0; |
| 615 | found_inline = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 616 | leaf_start = 0; |
| 617 | root_gen = 0; |
| 618 | root_owner = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 619 | extent = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 620 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 621 | slot = path->slots[0]; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 622 | ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 623 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Yan | 7261009 | 2008-02-05 15:40:36 -0500 | [diff] [blame] | 624 | if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY && |
| 625 | key.offset >= end) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 626 | goto out; |
| 627 | } |
Yan | 7261009 | 2008-02-05 15:40:36 -0500 | [diff] [blame] | 628 | if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY || |
| 629 | key.objectid != inode->i_ino) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 630 | goto out; |
| 631 | } |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 632 | if (recow) { |
| 633 | search_start = key.offset; |
| 634 | continue; |
| 635 | } |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 636 | if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { |
| 637 | extent = btrfs_item_ptr(leaf, slot, |
| 638 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 639 | found_type = btrfs_file_extent_type(leaf, extent); |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 640 | if (found_type == BTRFS_FILE_EXTENT_REG) { |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 641 | extent_end = |
| 642 | btrfs_file_extent_disk_bytenr(leaf, |
| 643 | extent); |
| 644 | if (extent_end) |
| 645 | *hint_byte = extent_end; |
| 646 | |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 647 | extent_end = key.offset + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 648 | btrfs_file_extent_num_bytes(leaf, extent); |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 649 | found_extent = 1; |
| 650 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 651 | struct btrfs_item *item; |
| 652 | item = btrfs_item_nr(leaf, slot); |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 653 | found_inline = 1; |
| 654 | extent_end = key.offset + |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 655 | btrfs_file_extent_inline_len(leaf, item); |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 656 | } |
| 657 | } else { |
| 658 | extent_end = search_start; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | /* we found nothing we can drop */ |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 662 | if ((!found_extent && !found_inline) || |
| 663 | search_start >= extent_end) { |
| 664 | int nextret; |
| 665 | u32 nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 666 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 667 | if (slot >= nritems - 1) { |
| 668 | nextret = btrfs_next_leaf(root, path); |
| 669 | if (nextret) |
| 670 | goto out; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 671 | recow = 1; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 672 | } else { |
| 673 | path->slots[0]++; |
| 674 | } |
| 675 | goto next_slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 676 | } |
| 677 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 678 | if (found_inline) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 679 | u64 mask = root->sectorsize - 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 680 | search_start = (extent_end + mask) & ~mask; |
| 681 | } else |
| 682 | search_start = extent_end; |
Yan | 6e3b966 | 2007-12-14 11:14:42 -0500 | [diff] [blame] | 683 | if (end <= extent_end && start >= key.offset && found_inline) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 684 | *hint_byte = EXTENT_MAP_INLINE; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 685 | goto out; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 686 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 687 | |
| 688 | if (found_extent) { |
| 689 | read_extent_buffer(leaf, &old, (unsigned long)extent, |
| 690 | sizeof(old)); |
| 691 | root_gen = btrfs_header_generation(leaf); |
| 692 | root_owner = btrfs_header_owner(leaf); |
| 693 | leaf_start = leaf->start; |
| 694 | } |
| 695 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 696 | if (end < extent_end && end >= key.offset) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 697 | bookend = 1; |
Yan | 0181e58 | 2008-01-30 14:39:54 -0500 | [diff] [blame] | 698 | if (found_inline && start <= key.offset) |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 699 | keep = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 700 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 701 | /* truncate existing extent */ |
| 702 | if (start > key.offset) { |
| 703 | u64 new_num; |
| 704 | u64 old_num; |
| 705 | keep = 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 706 | WARN_ON(start & (root->sectorsize - 1)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 707 | if (found_extent) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 708 | new_num = start - key.offset; |
| 709 | old_num = btrfs_file_extent_num_bytes(leaf, |
| 710 | extent); |
| 711 | *hint_byte = |
| 712 | btrfs_file_extent_disk_bytenr(leaf, |
| 713 | extent); |
| 714 | if (btrfs_file_extent_disk_bytenr(leaf, |
| 715 | extent)) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame^] | 716 | inode_sub_bytes(inode, old_num - |
| 717 | new_num); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 718 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 719 | btrfs_set_file_extent_num_bytes(leaf, extent, |
| 720 | new_num); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 721 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 722 | } else if (key.offset < inline_limit && |
| 723 | (end > extent_end) && |
| 724 | (inline_limit < extent_end)) { |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 725 | u32 new_size; |
| 726 | new_size = btrfs_file_extent_calc_inline_size( |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 727 | inline_limit - key.offset); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame^] | 728 | inode_sub_bytes(inode, extent_end - |
| 729 | inline_limit); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 730 | btrfs_truncate_item(trans, root, path, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 731 | new_size, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 732 | } |
| 733 | } |
| 734 | /* delete the entire extent */ |
| 735 | if (!keep) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame^] | 736 | if (found_inline) |
| 737 | inode_sub_bytes(inode, extent_end - |
| 738 | key.offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 739 | ret = btrfs_del_item(trans, root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 740 | /* TODO update progress marker and return */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 741 | BUG_ON(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 742 | extent = NULL; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 743 | btrfs_release_path(root, path); |
| 744 | /* the extent will be freed later */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 745 | } |
Yan | 0181e58 | 2008-01-30 14:39:54 -0500 | [diff] [blame] | 746 | if (bookend && found_inline && start <= key.offset) { |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 747 | u32 new_size; |
| 748 | new_size = btrfs_file_extent_calc_inline_size( |
Yan | 0181e58 | 2008-01-30 14:39:54 -0500 | [diff] [blame] | 749 | extent_end - end); |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame^] | 750 | inode_sub_bytes(inode, end - key.offset); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 751 | ret = btrfs_truncate_item(trans, root, path, |
| 752 | new_size, 0); |
| 753 | BUG_ON(ret); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 754 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 755 | /* create bookend, splitting the extent in two */ |
| 756 | if (bookend && found_extent) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 757 | u64 disk_bytenr; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 758 | struct btrfs_key ins; |
| 759 | ins.objectid = inode->i_ino; |
| 760 | ins.offset = end; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 761 | btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 762 | btrfs_release_path(root, path); |
| 763 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
| 764 | sizeof(*extent)); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 765 | BUG_ON(ret); |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 766 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 767 | leaf = path->nodes[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 768 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 769 | struct btrfs_file_extent_item); |
| 770 | write_extent_buffer(leaf, &old, |
| 771 | (unsigned long)extent, sizeof(old)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 772 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 773 | btrfs_set_file_extent_offset(leaf, extent, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 774 | le64_to_cpu(old.offset) + end - key.offset); |
| 775 | WARN_ON(le64_to_cpu(old.num_bytes) < |
| 776 | (extent_end - end)); |
| 777 | btrfs_set_file_extent_num_bytes(leaf, extent, |
| 778 | extent_end - end); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 779 | btrfs_set_file_extent_type(leaf, extent, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 780 | BTRFS_FILE_EXTENT_REG); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 781 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 782 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 783 | |
| 784 | disk_bytenr = le64_to_cpu(old.disk_bytenr); |
| 785 | if (disk_bytenr != 0) { |
| 786 | ret = btrfs_inc_extent_ref(trans, root, |
| 787 | disk_bytenr, |
| 788 | le64_to_cpu(old.disk_num_bytes), |
| 789 | leaf->start, |
| 790 | root->root_key.objectid, |
| 791 | trans->transid, |
| 792 | ins.objectid, ins.offset); |
| 793 | BUG_ON(ret); |
| 794 | } |
| 795 | btrfs_release_path(root, path); |
| 796 | if (disk_bytenr != 0) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame^] | 797 | inode_add_bytes(inode, extent_end - end); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 798 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | if (found_extent && !keep) { |
| 802 | u64 disk_bytenr = le64_to_cpu(old.disk_bytenr); |
| 803 | |
| 804 | if (disk_bytenr != 0) { |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame^] | 805 | inode_sub_bytes(inode, |
| 806 | le64_to_cpu(old.num_bytes)); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 807 | ret = btrfs_free_extent(trans, root, |
| 808 | disk_bytenr, |
| 809 | le64_to_cpu(old.disk_num_bytes), |
| 810 | leaf_start, root_owner, |
| 811 | root_gen, key.objectid, |
| 812 | key.offset, 0); |
| 813 | BUG_ON(ret); |
| 814 | *hint_byte = disk_bytenr; |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | if (search_start >= end) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 819 | ret = 0; |
| 820 | goto out; |
| 821 | } |
| 822 | } |
| 823 | out: |
| 824 | btrfs_free_path(path); |
Chris Mason | 9069218 | 2008-02-08 13:49:28 -0500 | [diff] [blame] | 825 | btrfs_check_file(root, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 826 | return ret; |
| 827 | } |
| 828 | |
| 829 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 830 | * this gets pages into the page cache and locks them down, it also properly |
| 831 | * waits for data=ordered extents to finish before allowing the pages to be |
| 832 | * modified. |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 833 | */ |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 834 | static int noinline prepare_pages(struct btrfs_root *root, struct file *file, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 835 | struct page **pages, size_t num_pages, |
| 836 | loff_t pos, unsigned long first_index, |
| 837 | unsigned long last_index, size_t write_bytes) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 838 | { |
| 839 | int i; |
| 840 | unsigned long index = pos >> PAGE_CACHE_SHIFT; |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 841 | struct inode *inode = fdentry(file)->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 842 | int err = 0; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 843 | u64 start_pos; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 844 | u64 last_pos; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 845 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 846 | start_pos = pos & ~((u64)root->sectorsize - 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 847 | last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 848 | |
| 849 | memset(pages, 0, num_pages * sizeof(struct page *)); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 850 | again: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 851 | for (i = 0; i < num_pages; i++) { |
| 852 | pages[i] = grab_cache_page(inode->i_mapping, index + i); |
| 853 | if (!pages[i]) { |
| 854 | err = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 855 | BUG_ON(1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 856 | } |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 857 | wait_on_page_writeback(pages[i]); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 858 | } |
Chris Mason | 0762704 | 2008-02-19 11:29:24 -0500 | [diff] [blame] | 859 | if (start_pos < inode->i_size) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 860 | struct btrfs_ordered_extent *ordered; |
Chris Mason | d99cb30 | 2008-02-19 12:55:05 -0500 | [diff] [blame] | 861 | lock_extent(&BTRFS_I(inode)->io_tree, |
| 862 | start_pos, last_pos - 1, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 863 | ordered = btrfs_lookup_first_ordered_extent(inode, last_pos -1); |
| 864 | if (ordered && |
| 865 | ordered->file_offset + ordered->len > start_pos && |
| 866 | ordered->file_offset < last_pos) { |
| 867 | btrfs_put_ordered_extent(ordered); |
| 868 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 869 | start_pos, last_pos - 1, GFP_NOFS); |
| 870 | for (i = 0; i < num_pages; i++) { |
| 871 | unlock_page(pages[i]); |
| 872 | page_cache_release(pages[i]); |
| 873 | } |
| 874 | btrfs_wait_ordered_range(inode, start_pos, |
| 875 | last_pos - start_pos); |
| 876 | goto again; |
| 877 | } |
| 878 | if (ordered) |
| 879 | btrfs_put_ordered_extent(ordered); |
| 880 | |
Chris Mason | 0762704 | 2008-02-19 11:29:24 -0500 | [diff] [blame] | 881 | clear_extent_bits(&BTRFS_I(inode)->io_tree, start_pos, |
| 882 | last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC, |
| 883 | GFP_NOFS); |
Chris Mason | d99cb30 | 2008-02-19 12:55:05 -0500 | [diff] [blame] | 884 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 885 | start_pos, last_pos - 1, GFP_NOFS); |
Chris Mason | 0762704 | 2008-02-19 11:29:24 -0500 | [diff] [blame] | 886 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 887 | for (i = 0; i < num_pages; i++) { |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 888 | clear_page_dirty_for_io(pages[i]); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 889 | set_page_extent_mapped(pages[i]); |
| 890 | WARN_ON(!PageLocked(pages[i])); |
| 891 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 892 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | static ssize_t btrfs_file_write(struct file *file, const char __user *buf, |
| 896 | size_t count, loff_t *ppos) |
| 897 | { |
| 898 | loff_t pos; |
Chris Mason | 2ff3e9b | 2007-10-29 14:36:41 -0400 | [diff] [blame] | 899 | loff_t start_pos; |
| 900 | ssize_t num_written = 0; |
| 901 | ssize_t err = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 902 | int ret = 0; |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 903 | struct inode *inode = fdentry(file)->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 904 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 905 | struct page **pages = NULL; |
| 906 | int nrptrs; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 907 | struct page *pinned[2]; |
| 908 | unsigned long first_index; |
| 909 | unsigned long last_index; |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 910 | int will_write; |
| 911 | |
| 912 | will_write = ((file->f_flags & O_SYNC) || IS_SYNC(inode) || |
| 913 | (file->f_flags & O_DIRECT)); |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 914 | |
| 915 | nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE, |
| 916 | PAGE_CACHE_SIZE / (sizeof(struct page *))); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 917 | pinned[0] = NULL; |
| 918 | pinned[1] = NULL; |
Chris Mason | 2ff3e9b | 2007-10-29 14:36:41 -0400 | [diff] [blame] | 919 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 920 | pos = *ppos; |
Chris Mason | 2ff3e9b | 2007-10-29 14:36:41 -0400 | [diff] [blame] | 921 | start_pos = pos; |
| 922 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 923 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); |
| 924 | current->backing_dev_info = inode->i_mapping->backing_dev_info; |
| 925 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); |
| 926 | if (err) |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 927 | goto out_nolock; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 928 | if (count == 0) |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 929 | goto out_nolock; |
Chris Mason | 2b1f55b | 2008-09-24 11:48:04 -0400 | [diff] [blame] | 930 | |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 931 | err = file_remove_suid(file); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 932 | if (err) |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 933 | goto out_nolock; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 934 | file_update_time(file); |
| 935 | |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 936 | pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 937 | |
| 938 | mutex_lock(&inode->i_mutex); |
| 939 | first_index = pos >> PAGE_CACHE_SHIFT; |
| 940 | last_index = (pos + count) >> PAGE_CACHE_SHIFT; |
| 941 | |
| 942 | /* |
Chris Mason | 409c611 | 2008-04-22 09:24:20 -0400 | [diff] [blame] | 943 | * if this is a nodatasum mount, force summing off for the inode |
| 944 | * all the time. That way a later mount with summing on won't |
| 945 | * get confused |
| 946 | */ |
| 947 | if (btrfs_test_opt(root, NODATASUM)) |
| 948 | btrfs_set_flag(inode, NODATASUM); |
| 949 | |
| 950 | /* |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 951 | * there are lots of better ways to do this, but this code |
| 952 | * makes sure the first and last page in the file range are |
| 953 | * up to date and ready for cow |
| 954 | */ |
| 955 | if ((pos & (PAGE_CACHE_SIZE - 1))) { |
| 956 | pinned[0] = grab_cache_page(inode->i_mapping, first_index); |
| 957 | if (!PageUptodate(pinned[0])) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 958 | ret = btrfs_readpage(NULL, pinned[0]); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 959 | BUG_ON(ret); |
| 960 | wait_on_page_locked(pinned[0]); |
| 961 | } else { |
| 962 | unlock_page(pinned[0]); |
| 963 | } |
| 964 | } |
| 965 | if ((pos + count) & (PAGE_CACHE_SIZE - 1)) { |
| 966 | pinned[1] = grab_cache_page(inode->i_mapping, last_index); |
| 967 | if (!PageUptodate(pinned[1])) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 968 | ret = btrfs_readpage(NULL, pinned[1]); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 969 | BUG_ON(ret); |
| 970 | wait_on_page_locked(pinned[1]); |
| 971 | } else { |
| 972 | unlock_page(pinned[1]); |
| 973 | } |
| 974 | } |
| 975 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 976 | while(count > 0) { |
| 977 | size_t offset = pos & (PAGE_CACHE_SIZE - 1); |
Chris Mason | 11bd143 | 2007-06-22 14:16:24 -0400 | [diff] [blame] | 978 | size_t write_bytes = min(count, nrptrs * |
| 979 | (size_t)PAGE_CACHE_SIZE - |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 980 | offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 981 | size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >> |
| 982 | PAGE_CACHE_SHIFT; |
| 983 | |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 984 | WARN_ON(num_pages > nrptrs); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 985 | memset(pages, 0, sizeof(pages)); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 986 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 987 | ret = btrfs_check_free_space(root, write_bytes, 0); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 988 | if (ret) |
| 989 | goto out; |
| 990 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 991 | ret = prepare_pages(root, file, pages, num_pages, |
| 992 | pos, first_index, last_index, |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 993 | write_bytes); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 994 | if (ret) |
| 995 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 996 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 997 | ret = btrfs_copy_from_user(pos, num_pages, |
| 998 | write_bytes, pages, buf); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 999 | if (ret) { |
| 1000 | btrfs_drop_pages(pages, num_pages); |
| 1001 | goto out; |
| 1002 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1003 | |
| 1004 | ret = dirty_and_release_pages(NULL, root, file, pages, |
| 1005 | num_pages, pos, write_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1006 | btrfs_drop_pages(pages, num_pages); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1007 | if (ret) |
| 1008 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1009 | |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 1010 | if (will_write) { |
| 1011 | btrfs_fdatawrite_range(inode->i_mapping, pos, |
| 1012 | pos + write_bytes - 1, |
| 1013 | WB_SYNC_NONE); |
| 1014 | } else { |
| 1015 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, |
| 1016 | num_pages); |
| 1017 | if (num_pages < |
| 1018 | (root->leafsize >> PAGE_CACHE_SHIFT) + 1) |
| 1019 | btrfs_btree_balance_dirty(root, 1); |
| 1020 | btrfs_throttle(root); |
| 1021 | } |
| 1022 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1023 | buf += write_bytes; |
| 1024 | count -= write_bytes; |
| 1025 | pos += write_bytes; |
| 1026 | num_written += write_bytes; |
| 1027 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1028 | cond_resched(); |
| 1029 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1030 | out: |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1031 | mutex_unlock(&inode->i_mutex); |
Chris Mason | 5b92ee7 | 2008-01-03 13:46:11 -0500 | [diff] [blame] | 1032 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1033 | out_nolock: |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 1034 | kfree(pages); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1035 | if (pinned[0]) |
| 1036 | page_cache_release(pinned[0]); |
| 1037 | if (pinned[1]) |
| 1038 | page_cache_release(pinned[1]); |
| 1039 | *ppos = pos; |
Chris Mason | 2ff3e9b | 2007-10-29 14:36:41 -0400 | [diff] [blame] | 1040 | |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 1041 | if (num_written > 0 && will_write) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1042 | struct btrfs_trans_handle *trans; |
| 1043 | |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 1044 | err = btrfs_wait_ordered_range(inode, start_pos, num_written); |
| 1045 | if (err) |
Chris Mason | 2ff3e9b | 2007-10-29 14:36:41 -0400 | [diff] [blame] | 1046 | num_written = err; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1047 | |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 1048 | if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) { |
| 1049 | trans = btrfs_start_transaction(root, 1); |
| 1050 | ret = btrfs_log_dentry_safe(trans, root, |
| 1051 | file->f_dentry); |
| 1052 | if (ret == 0) { |
| 1053 | btrfs_sync_log(trans, root); |
| 1054 | btrfs_end_transaction(trans, root); |
| 1055 | } else { |
| 1056 | btrfs_commit_transaction(trans, root); |
| 1057 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1058 | } |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 1059 | if (file->f_flags & O_DIRECT) { |
| 1060 | invalidate_mapping_pages(inode->i_mapping, |
| 1061 | start_pos >> PAGE_CACHE_SHIFT, |
| 1062 | (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT); |
| 1063 | } |
Chris Mason | 2ff3e9b | 2007-10-29 14:36:41 -0400 | [diff] [blame] | 1064 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1065 | current->backing_dev_info = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1066 | return num_written ? num_written : err; |
| 1067 | } |
| 1068 | |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 1069 | int btrfs_release_file(struct inode * inode, struct file * filp) |
Mingming | e1b81e6 | 2008-05-27 10:55:43 -0400 | [diff] [blame] | 1070 | { |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 1071 | if (filp->private_data) |
| 1072 | btrfs_ioctl_trans_end(filp); |
Mingming | e1b81e6 | 2008-05-27 10:55:43 -0400 | [diff] [blame] | 1073 | return 0; |
| 1074 | } |
| 1075 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1076 | /* |
| 1077 | * fsync call for both files and directories. This logs the inode into |
| 1078 | * the tree log instead of forcing full commits whenever possible. |
| 1079 | * |
| 1080 | * It needs to call filemap_fdatawait so that all ordered extent updates are |
| 1081 | * in the metadata btree are up to date for copying to the log. |
| 1082 | * |
| 1083 | * It drops the inode mutex before doing the tree log commit. This is an |
| 1084 | * important optimization for directories because holding the mutex prevents |
| 1085 | * new operations on the dir while we write to disk. |
| 1086 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1087 | int btrfs_sync_file(struct file *file, struct dentry *dentry, int datasync) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1088 | { |
| 1089 | struct inode *inode = dentry->d_inode; |
| 1090 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1091 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1092 | struct btrfs_trans_handle *trans; |
| 1093 | |
| 1094 | /* |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1095 | * check the transaction that last modified this inode |
| 1096 | * and see if its already been committed |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1097 | */ |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1098 | if (!BTRFS_I(inode)->last_trans) |
| 1099 | goto out; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1100 | |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1101 | mutex_lock(&root->fs_info->trans_mutex); |
| 1102 | if (BTRFS_I(inode)->last_trans <= |
| 1103 | root->fs_info->last_trans_committed) { |
| 1104 | BTRFS_I(inode)->last_trans = 0; |
| 1105 | mutex_unlock(&root->fs_info->trans_mutex); |
| 1106 | goto out; |
| 1107 | } |
| 1108 | mutex_unlock(&root->fs_info->trans_mutex); |
| 1109 | |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 1110 | root->fs_info->tree_log_batch++; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1111 | filemap_fdatawait(inode->i_mapping); |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 1112 | root->fs_info->tree_log_batch++; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1113 | |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1114 | /* |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1115 | * ok we haven't committed the transaction yet, lets do a commit |
| 1116 | */ |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 1117 | if (file->private_data) |
| 1118 | btrfs_ioctl_trans_end(file); |
| 1119 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1120 | trans = btrfs_start_transaction(root, 1); |
| 1121 | if (!trans) { |
| 1122 | ret = -ENOMEM; |
| 1123 | goto out; |
| 1124 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1125 | |
| 1126 | ret = btrfs_log_dentry_safe(trans, root, file->f_dentry); |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 1127 | if (ret < 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1128 | goto out; |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | /* we've logged all the items and now have a consistent |
| 1132 | * version of the file in the log. It is possible that |
| 1133 | * someone will come in and modify the file, but that's |
| 1134 | * fine because the log is consistent on disk, and we |
| 1135 | * have references to all of the file's extents |
| 1136 | * |
| 1137 | * It is possible that someone will come in and log the |
| 1138 | * file again, but that will end up using the synchronization |
| 1139 | * inside btrfs_sync_log to keep things safe. |
| 1140 | */ |
| 1141 | mutex_unlock(&file->f_dentry->d_inode->i_mutex); |
| 1142 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1143 | if (ret > 0) { |
| 1144 | ret = btrfs_commit_transaction(trans, root); |
| 1145 | } else { |
| 1146 | btrfs_sync_log(trans, root); |
| 1147 | ret = btrfs_end_transaction(trans, root); |
| 1148 | } |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 1149 | mutex_lock(&file->f_dentry->d_inode->i_mutex); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1150 | out: |
| 1151 | return ret > 0 ? EIO : ret; |
| 1152 | } |
| 1153 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1154 | static struct vm_operations_struct btrfs_file_vm_ops = { |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 1155 | .fault = filemap_fault, |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1156 | .page_mkwrite = btrfs_page_mkwrite, |
| 1157 | }; |
| 1158 | |
| 1159 | static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma) |
| 1160 | { |
| 1161 | vma->vm_ops = &btrfs_file_vm_ops; |
| 1162 | file_accessed(filp); |
| 1163 | return 0; |
| 1164 | } |
| 1165 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1166 | struct file_operations btrfs_file_operations = { |
| 1167 | .llseek = generic_file_llseek, |
| 1168 | .read = do_sync_read, |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1169 | .aio_read = generic_file_aio_read, |
Chris Mason | e9906a9 | 2007-12-14 12:56:58 -0500 | [diff] [blame] | 1170 | .splice_read = generic_file_splice_read, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1171 | .write = btrfs_file_write, |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1172 | .mmap = btrfs_file_mmap, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1173 | .open = generic_file_open, |
Mingming | e1b81e6 | 2008-05-27 10:55:43 -0400 | [diff] [blame] | 1174 | .release = btrfs_release_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1175 | .fsync = btrfs_sync_file, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 1176 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1177 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 1178 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1179 | #endif |
| 1180 | }; |