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