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> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 25 | #include <linux/backing-dev.h> |
| 26 | #include <linux/mpage.h> |
| 27 | #include <linux/swap.h> |
| 28 | #include <linux/writeback.h> |
| 29 | #include <linux/statfs.h> |
| 30 | #include <linux/compat.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 31 | #include <linux/slab.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 32 | #include "ctree.h" |
| 33 | #include "disk-io.h" |
| 34 | #include "transaction.h" |
| 35 | #include "btrfs_inode.h" |
| 36 | #include "ioctl.h" |
| 37 | #include "print-tree.h" |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 38 | #include "tree-log.h" |
| 39 | #include "locking.h" |
Jeff Mahoney | 12fa8ec | 2008-05-02 15:03:58 -0400 | [diff] [blame] | 40 | #include "compat.h" |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 41 | |
| 42 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 43 | /* simple helper to fault in pages and copy. This should go away |
| 44 | * and be replaced with calls into generic code. |
| 45 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 46 | static noinline int btrfs_copy_from_user(loff_t pos, int num_pages, |
Chris Mason | a1b32a5 | 2008-09-05 16:09:51 -0400 | [diff] [blame] | 47 | int write_bytes, |
| 48 | struct page **prepared_pages, |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 49 | struct iov_iter *i) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 50 | { |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 51 | size_t copied = 0; |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 52 | int pg = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 53 | int offset = pos & (PAGE_CACHE_SIZE - 1); |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 54 | int total_copied = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 55 | |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 56 | while (write_bytes > 0) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 57 | size_t count = min_t(size_t, |
| 58 | PAGE_CACHE_SIZE - offset, write_bytes); |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 59 | struct page *page = prepared_pages[pg]; |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 60 | /* |
| 61 | * Copy data from userspace to the current page |
| 62 | * |
| 63 | * Disable pagefault to avoid recursive lock since |
| 64 | * the pages are already locked |
| 65 | */ |
| 66 | pagefault_disable(); |
| 67 | copied = iov_iter_copy_from_user_atomic(page, i, offset, count); |
| 68 | pagefault_enable(); |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 69 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 70 | /* Flush processor's dcache for this page */ |
| 71 | flush_dcache_page(page); |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 72 | iov_iter_advance(i, copied); |
| 73 | write_bytes -= copied; |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 74 | total_copied += copied; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 75 | |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 76 | /* Return to btrfs_file_aio_write to fault page */ |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 77 | if (unlikely(copied == 0)) { |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 78 | break; |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | if (unlikely(copied < PAGE_CACHE_SIZE - offset)) { |
| 82 | offset += copied; |
| 83 | } else { |
| 84 | pg++; |
| 85 | offset = 0; |
| 86 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 87 | } |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 88 | return total_copied; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 89 | } |
| 90 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 91 | /* |
| 92 | * unlocks pages after btrfs_file_write is done with them |
| 93 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 94 | static noinline void btrfs_drop_pages(struct page **pages, size_t num_pages) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 95 | { |
| 96 | size_t i; |
| 97 | for (i = 0; i < num_pages; i++) { |
| 98 | if (!pages[i]) |
| 99 | break; |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 100 | /* page checked is some magic around finding pages that |
| 101 | * have been modified without going through btrfs_set_page_dirty |
| 102 | * clear it here |
| 103 | */ |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 104 | ClearPageChecked(pages[i]); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 105 | unlock_page(pages[i]); |
| 106 | mark_page_accessed(pages[i]); |
| 107 | page_cache_release(pages[i]); |
| 108 | } |
| 109 | } |
| 110 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 111 | /* |
| 112 | * after copy_from_user, pages need to be dirtied and we need to make |
| 113 | * sure holes are created between the current EOF and the start of |
| 114 | * any next extents (if required). |
| 115 | * |
| 116 | * this also makes the decision about creating an inline extent vs |
| 117 | * doing real data extents, marking pages dirty and delalloc as required. |
| 118 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 119 | static noinline int dirty_and_release_pages(struct btrfs_trans_handle *trans, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 120 | struct btrfs_root *root, |
| 121 | struct file *file, |
| 122 | struct page **pages, |
| 123 | size_t num_pages, |
| 124 | loff_t pos, |
| 125 | size_t write_bytes) |
| 126 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 127 | int err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 128 | int i; |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 129 | struct inode *inode = fdentry(file)->d_inode; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 130 | u64 num_bytes; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 131 | u64 start_pos; |
| 132 | u64 end_of_last_block; |
| 133 | u64 end_pos = pos + write_bytes; |
| 134 | loff_t isize = i_size_read(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 135 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 136 | start_pos = pos & ~((u64)root->sectorsize - 1); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 137 | num_bytes = (write_bytes + pos - start_pos + |
| 138 | root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 139 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 140 | end_of_last_block = start_pos + num_bytes - 1; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 141 | err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block, |
| 142 | NULL); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 143 | BUG_ON(err); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 144 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 145 | for (i = 0; i < num_pages; i++) { |
| 146 | struct page *p = pages[i]; |
| 147 | SetPageUptodate(p); |
| 148 | ClearPageChecked(p); |
| 149 | set_page_dirty(p); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 150 | } |
| 151 | if (end_pos > isize) { |
| 152 | i_size_write(inode, end_pos); |
Chris Mason | f597bb1 | 2009-06-27 21:06:22 -0400 | [diff] [blame] | 153 | /* we've only changed i_size in ram, and we haven't updated |
| 154 | * the disk i_size. There is no need to log the inode |
| 155 | * at this time. |
| 156 | */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 157 | } |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 158 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 159 | } |
| 160 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 161 | /* |
| 162 | * this drops all the extents in the cache that intersect the range |
| 163 | * [start, end]. Existing extents are split as required. |
| 164 | */ |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 165 | int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end, |
| 166 | int skip_pinned) |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 167 | { |
| 168 | struct extent_map *em; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 169 | struct extent_map *split = NULL; |
| 170 | struct extent_map *split2 = NULL; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 171 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 172 | u64 len = end - start + 1; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 173 | int ret; |
| 174 | int testend = 1; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 175 | unsigned long flags; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 176 | int compressed = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 177 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 178 | WARN_ON(end < start); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 179 | if (end == (u64)-1) { |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 180 | len = (u64)-1; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 181 | testend = 0; |
| 182 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 183 | while (1) { |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 184 | if (!split) |
| 185 | split = alloc_extent_map(GFP_NOFS); |
| 186 | if (!split2) |
| 187 | split2 = alloc_extent_map(GFP_NOFS); |
| 188 | |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 189 | write_lock(&em_tree->lock); |
Yan | 39b5637 | 2008-02-15 10:40:50 -0500 | [diff] [blame] | 190 | em = lookup_extent_mapping(em_tree, start, len); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 191 | if (!em) { |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 192 | write_unlock(&em_tree->lock); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 193 | break; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 194 | } |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 195 | flags = em->flags; |
| 196 | if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) { |
Yan, Zheng | 55ef689 | 2009-11-12 09:36:44 +0000 | [diff] [blame] | 197 | if (testend && em->start + em->len >= start + len) { |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 198 | free_extent_map(em); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 199 | write_unlock(&em_tree->lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 200 | break; |
| 201 | } |
Yan, Zheng | 55ef689 | 2009-11-12 09:36:44 +0000 | [diff] [blame] | 202 | start = em->start + em->len; |
| 203 | if (testend) |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 204 | len = start + len - (em->start + em->len); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 205 | free_extent_map(em); |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 206 | write_unlock(&em_tree->lock); |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 207 | continue; |
| 208 | } |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 209 | compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags); |
Chris Mason | 3ce7e67 | 2008-07-31 15:42:54 -0400 | [diff] [blame] | 210 | clear_bit(EXTENT_FLAG_PINNED, &em->flags); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 211 | remove_extent_mapping(em_tree, em); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 212 | |
| 213 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 214 | em->start < start) { |
| 215 | split->start = em->start; |
| 216 | split->len = start - em->start; |
Yan Zheng | ff5b7ee | 2008-11-10 07:34:43 -0500 | [diff] [blame] | 217 | split->orig_start = em->orig_start; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 218 | split->block_start = em->block_start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 219 | |
| 220 | if (compressed) |
| 221 | split->block_len = em->block_len; |
| 222 | else |
| 223 | split->block_len = split->len; |
| 224 | |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 225 | split->bdev = em->bdev; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 226 | split->flags = flags; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame^] | 227 | split->compress_type = em->compress_type; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 228 | ret = add_extent_mapping(em_tree, split); |
| 229 | BUG_ON(ret); |
| 230 | free_extent_map(split); |
| 231 | split = split2; |
| 232 | split2 = NULL; |
| 233 | } |
| 234 | if (em->block_start < EXTENT_MAP_LAST_BYTE && |
| 235 | testend && em->start + em->len > start + len) { |
| 236 | u64 diff = start + len - em->start; |
| 237 | |
| 238 | split->start = start + len; |
| 239 | split->len = em->start + em->len - (start + len); |
| 240 | split->bdev = em->bdev; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 241 | split->flags = flags; |
Li Zefan | 261507a0 | 2010-12-17 14:21:50 +0800 | [diff] [blame^] | 242 | split->compress_type = em->compress_type; |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 243 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 244 | if (compressed) { |
| 245 | split->block_len = em->block_len; |
| 246 | split->block_start = em->block_start; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 247 | split->orig_start = em->orig_start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 248 | } else { |
| 249 | split->block_len = split->len; |
| 250 | split->block_start = em->block_start + diff; |
Chris Mason | 445a694 | 2008-11-10 11:53:33 -0500 | [diff] [blame] | 251 | split->orig_start = split->start; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 252 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 253 | |
| 254 | ret = add_extent_mapping(em_tree, split); |
| 255 | BUG_ON(ret); |
| 256 | free_extent_map(split); |
| 257 | split = NULL; |
| 258 | } |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 259 | write_unlock(&em_tree->lock); |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 260 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 261 | /* once for us */ |
| 262 | free_extent_map(em); |
| 263 | /* once for the tree*/ |
| 264 | free_extent_map(em); |
| 265 | } |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 266 | if (split) |
| 267 | free_extent_map(split); |
| 268 | if (split2) |
| 269 | free_extent_map(split2); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 270 | return 0; |
| 271 | } |
| 272 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 273 | /* |
| 274 | * this is very complex, but the basic idea is to drop all extents |
| 275 | * in the range start - end. hint_block is filled in with a block number |
| 276 | * that would be a good hint to the block allocator for this file. |
| 277 | * |
| 278 | * If an extent intersects the range but is not entirely inside the range |
| 279 | * it is either truncated or split. Anything entirely inside the range |
| 280 | * is deleted from the tree. |
| 281 | */ |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 282 | int btrfs_drop_extents(struct btrfs_trans_handle *trans, struct inode *inode, |
| 283 | u64 start, u64 end, u64 *hint_byte, int drop_cache) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 284 | { |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 285 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 286 | struct extent_buffer *leaf; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 287 | struct btrfs_file_extent_item *fi; |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 288 | struct btrfs_path *path; |
| 289 | struct btrfs_key key; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 290 | struct btrfs_key new_key; |
| 291 | u64 search_start = start; |
| 292 | u64 disk_bytenr = 0; |
| 293 | u64 num_bytes = 0; |
| 294 | u64 extent_offset = 0; |
| 295 | u64 extent_end = 0; |
| 296 | int del_nr = 0; |
| 297 | int del_slot = 0; |
| 298 | int extent_type; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 299 | int recow; |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 300 | int ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 301 | |
Chris Mason | a1ed835 | 2009-09-11 12:27:37 -0400 | [diff] [blame] | 302 | if (drop_cache) |
| 303 | btrfs_drop_extent_cache(inode, start, end - 1, 0); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 304 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 305 | path = btrfs_alloc_path(); |
| 306 | if (!path) |
| 307 | return -ENOMEM; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 308 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 309 | while (1) { |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 310 | recow = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 311 | ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, |
| 312 | search_start, -1); |
| 313 | if (ret < 0) |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 314 | break; |
| 315 | if (ret > 0 && path->slots[0] > 0 && search_start == start) { |
| 316 | leaf = path->nodes[0]; |
| 317 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1); |
| 318 | if (key.objectid == inode->i_ino && |
| 319 | key.type == BTRFS_EXTENT_DATA_KEY) |
| 320 | path->slots[0]--; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 321 | } |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 322 | ret = 0; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 323 | next_slot: |
| 324 | leaf = path->nodes[0]; |
| 325 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 326 | BUG_ON(del_nr > 0); |
| 327 | ret = btrfs_next_leaf(root, path); |
| 328 | if (ret < 0) |
| 329 | break; |
| 330 | if (ret > 0) { |
| 331 | ret = 0; |
| 332 | break; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 333 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 334 | leaf = path->nodes[0]; |
| 335 | recow = 1; |
| 336 | } |
| 337 | |
| 338 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 339 | if (key.objectid > inode->i_ino || |
| 340 | key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end) |
| 341 | break; |
| 342 | |
| 343 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 344 | struct btrfs_file_extent_item); |
| 345 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 346 | |
| 347 | if (extent_type == BTRFS_FILE_EXTENT_REG || |
| 348 | extent_type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 349 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 350 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 351 | extent_offset = btrfs_file_extent_offset(leaf, fi); |
| 352 | extent_end = key.offset + |
| 353 | btrfs_file_extent_num_bytes(leaf, fi); |
| 354 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 355 | extent_end = key.offset + |
| 356 | btrfs_file_extent_inline_len(leaf, fi); |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 357 | } else { |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 358 | WARN_ON(1); |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 359 | extent_end = search_start; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 360 | } |
| 361 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 362 | if (extent_end <= search_start) { |
| 363 | path->slots[0]++; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 364 | goto next_slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 365 | } |
| 366 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 367 | search_start = max(key.offset, start); |
| 368 | if (recow) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 369 | btrfs_release_path(root, path); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 370 | continue; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 371 | } |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 372 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 373 | /* |
| 374 | * | - range to drop - | |
| 375 | * | -------- extent -------- | |
| 376 | */ |
| 377 | if (start > key.offset && end < extent_end) { |
| 378 | BUG_ON(del_nr > 0); |
| 379 | BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE); |
| 380 | |
| 381 | memcpy(&new_key, &key, sizeof(new_key)); |
| 382 | new_key.offset = start; |
| 383 | ret = btrfs_duplicate_item(trans, root, path, |
| 384 | &new_key); |
| 385 | if (ret == -EAGAIN) { |
| 386 | btrfs_release_path(root, path); |
| 387 | continue; |
| 388 | } |
| 389 | if (ret < 0) |
| 390 | break; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 391 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 392 | leaf = path->nodes[0]; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 393 | fi = btrfs_item_ptr(leaf, path->slots[0] - 1, |
| 394 | struct btrfs_file_extent_item); |
| 395 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 396 | start - key.offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 397 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 398 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 399 | struct btrfs_file_extent_item); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 400 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 401 | extent_offset += start - key.offset; |
| 402 | btrfs_set_file_extent_offset(leaf, fi, extent_offset); |
| 403 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 404 | extent_end - start); |
| 405 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 406 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 407 | if (disk_bytenr > 0) { |
| 408 | ret = btrfs_inc_extent_ref(trans, root, |
| 409 | disk_bytenr, num_bytes, 0, |
| 410 | root->root_key.objectid, |
| 411 | new_key.objectid, |
| 412 | start - extent_offset); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 413 | BUG_ON(ret); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 414 | *hint_byte = disk_bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 415 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 416 | key.offset = start; |
| 417 | } |
| 418 | /* |
| 419 | * | ---- range to drop ----- | |
| 420 | * | -------- extent -------- | |
| 421 | */ |
| 422 | if (start <= key.offset && end < extent_end) { |
| 423 | BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE); |
| 424 | |
| 425 | memcpy(&new_key, &key, sizeof(new_key)); |
| 426 | new_key.offset = end; |
| 427 | btrfs_set_item_key_safe(trans, root, path, &new_key); |
| 428 | |
| 429 | extent_offset += end - key.offset; |
| 430 | btrfs_set_file_extent_offset(leaf, fi, extent_offset); |
| 431 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 432 | extent_end - end); |
| 433 | btrfs_mark_buffer_dirty(leaf); |
| 434 | if (disk_bytenr > 0) { |
| 435 | inode_sub_bytes(inode, end - key.offset); |
| 436 | *hint_byte = disk_bytenr; |
| 437 | } |
| 438 | break; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 439 | } |
| 440 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 441 | search_start = extent_end; |
| 442 | /* |
| 443 | * | ---- range to drop ----- | |
| 444 | * | -------- extent -------- | |
| 445 | */ |
| 446 | if (start > key.offset && end >= extent_end) { |
| 447 | BUG_ON(del_nr > 0); |
| 448 | BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE); |
| 449 | |
| 450 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 451 | start - key.offset); |
| 452 | btrfs_mark_buffer_dirty(leaf); |
| 453 | if (disk_bytenr > 0) { |
| 454 | inode_sub_bytes(inode, extent_end - start); |
| 455 | *hint_byte = disk_bytenr; |
| 456 | } |
| 457 | if (end == extent_end) |
| 458 | break; |
| 459 | |
| 460 | path->slots[0]++; |
| 461 | goto next_slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 462 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 463 | |
| 464 | /* |
| 465 | * | ---- range to drop ----- | |
| 466 | * | ------ extent ------ | |
| 467 | */ |
| 468 | if (start <= key.offset && end >= extent_end) { |
| 469 | if (del_nr == 0) { |
| 470 | del_slot = path->slots[0]; |
| 471 | del_nr = 1; |
| 472 | } else { |
| 473 | BUG_ON(del_slot + del_nr != path->slots[0]); |
| 474 | del_nr++; |
| 475 | } |
| 476 | |
| 477 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 478 | inode_sub_bytes(inode, |
| 479 | extent_end - key.offset); |
| 480 | extent_end = ALIGN(extent_end, |
| 481 | root->sectorsize); |
| 482 | } else if (disk_bytenr > 0) { |
| 483 | ret = btrfs_free_extent(trans, root, |
| 484 | disk_bytenr, num_bytes, 0, |
| 485 | root->root_key.objectid, |
| 486 | key.objectid, key.offset - |
| 487 | extent_offset); |
| 488 | BUG_ON(ret); |
| 489 | inode_sub_bytes(inode, |
| 490 | extent_end - key.offset); |
| 491 | *hint_byte = disk_bytenr; |
| 492 | } |
| 493 | |
| 494 | if (end == extent_end) |
| 495 | break; |
| 496 | |
| 497 | if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) { |
| 498 | path->slots[0]++; |
| 499 | goto next_slot; |
| 500 | } |
| 501 | |
| 502 | ret = btrfs_del_items(trans, root, path, del_slot, |
| 503 | del_nr); |
| 504 | BUG_ON(ret); |
| 505 | |
| 506 | del_nr = 0; |
| 507 | del_slot = 0; |
| 508 | |
| 509 | btrfs_release_path(root, path); |
| 510 | continue; |
| 511 | } |
| 512 | |
| 513 | BUG_ON(1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 514 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 515 | |
| 516 | if (del_nr > 0) { |
| 517 | ret = btrfs_del_items(trans, root, path, del_slot, del_nr); |
| 518 | BUG_ON(ret); |
| 519 | } |
| 520 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 521 | btrfs_free_path(path); |
| 522 | return ret; |
| 523 | } |
| 524 | |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 525 | static int extent_mergeable(struct extent_buffer *leaf, int slot, |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 526 | u64 objectid, u64 bytenr, u64 orig_offset, |
| 527 | u64 *start, u64 *end) |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 528 | { |
| 529 | struct btrfs_file_extent_item *fi; |
| 530 | struct btrfs_key key; |
| 531 | u64 extent_end; |
| 532 | |
| 533 | if (slot < 0 || slot >= btrfs_header_nritems(leaf)) |
| 534 | return 0; |
| 535 | |
| 536 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 537 | if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY) |
| 538 | return 0; |
| 539 | |
| 540 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 541 | if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG || |
| 542 | btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr || |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 543 | btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset || |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 544 | btrfs_file_extent_compression(leaf, fi) || |
| 545 | btrfs_file_extent_encryption(leaf, fi) || |
| 546 | btrfs_file_extent_other_encoding(leaf, fi)) |
| 547 | return 0; |
| 548 | |
| 549 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 550 | if ((*start && *start != key.offset) || (*end && *end != extent_end)) |
| 551 | return 0; |
| 552 | |
| 553 | *start = key.offset; |
| 554 | *end = extent_end; |
| 555 | return 1; |
| 556 | } |
| 557 | |
| 558 | /* |
| 559 | * Mark extent in the range start - end as written. |
| 560 | * |
| 561 | * This changes extent type from 'pre-allocated' to 'regular'. If only |
| 562 | * part of extent is marked as written, the extent will be split into |
| 563 | * two or three. |
| 564 | */ |
| 565 | int btrfs_mark_extent_written(struct btrfs_trans_handle *trans, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 566 | struct inode *inode, u64 start, u64 end) |
| 567 | { |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 568 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 569 | struct extent_buffer *leaf; |
| 570 | struct btrfs_path *path; |
| 571 | struct btrfs_file_extent_item *fi; |
| 572 | struct btrfs_key key; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 573 | struct btrfs_key new_key; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 574 | u64 bytenr; |
| 575 | u64 num_bytes; |
| 576 | u64 extent_end; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 577 | u64 orig_offset; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 578 | u64 other_start; |
| 579 | u64 other_end; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 580 | u64 split; |
| 581 | int del_nr = 0; |
| 582 | int del_slot = 0; |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 583 | int recow; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 584 | int ret; |
| 585 | |
| 586 | btrfs_drop_extent_cache(inode, start, end - 1, 0); |
| 587 | |
| 588 | path = btrfs_alloc_path(); |
| 589 | BUG_ON(!path); |
| 590 | again: |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 591 | recow = 0; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 592 | split = start; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 593 | key.objectid = inode->i_ino; |
| 594 | key.type = BTRFS_EXTENT_DATA_KEY; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 595 | key.offset = split; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 596 | |
| 597 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 598 | if (ret > 0 && path->slots[0] > 0) |
| 599 | path->slots[0]--; |
| 600 | |
| 601 | leaf = path->nodes[0]; |
| 602 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 603 | BUG_ON(key.objectid != inode->i_ino || |
| 604 | key.type != BTRFS_EXTENT_DATA_KEY); |
| 605 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 606 | struct btrfs_file_extent_item); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 607 | BUG_ON(btrfs_file_extent_type(leaf, fi) != |
| 608 | BTRFS_FILE_EXTENT_PREALLOC); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 609 | extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi); |
| 610 | BUG_ON(key.offset > start || extent_end < end); |
| 611 | |
| 612 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 613 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 614 | orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 615 | memcpy(&new_key, &key, sizeof(new_key)); |
| 616 | |
| 617 | if (start == key.offset && end < extent_end) { |
| 618 | other_start = 0; |
| 619 | other_end = start; |
| 620 | if (extent_mergeable(leaf, path->slots[0] - 1, |
| 621 | inode->i_ino, bytenr, orig_offset, |
| 622 | &other_start, &other_end)) { |
| 623 | new_key.offset = end; |
| 624 | btrfs_set_item_key_safe(trans, root, path, &new_key); |
| 625 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 626 | struct btrfs_file_extent_item); |
| 627 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 628 | extent_end - end); |
| 629 | btrfs_set_file_extent_offset(leaf, fi, |
| 630 | end - orig_offset); |
| 631 | fi = btrfs_item_ptr(leaf, path->slots[0] - 1, |
| 632 | struct btrfs_file_extent_item); |
| 633 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 634 | end - other_start); |
| 635 | btrfs_mark_buffer_dirty(leaf); |
| 636 | goto out; |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | if (start > key.offset && end == extent_end) { |
| 641 | other_start = end; |
| 642 | other_end = 0; |
| 643 | if (extent_mergeable(leaf, path->slots[0] + 1, |
| 644 | inode->i_ino, bytenr, orig_offset, |
| 645 | &other_start, &other_end)) { |
| 646 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 647 | struct btrfs_file_extent_item); |
| 648 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 649 | start - key.offset); |
| 650 | path->slots[0]++; |
| 651 | new_key.offset = start; |
| 652 | btrfs_set_item_key_safe(trans, root, path, &new_key); |
| 653 | |
| 654 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 655 | struct btrfs_file_extent_item); |
| 656 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 657 | other_end - start); |
| 658 | btrfs_set_file_extent_offset(leaf, fi, |
| 659 | start - orig_offset); |
| 660 | btrfs_mark_buffer_dirty(leaf); |
| 661 | goto out; |
| 662 | } |
| 663 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 664 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 665 | while (start > key.offset || end < extent_end) { |
| 666 | if (key.offset == start) |
| 667 | split = end; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 668 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 669 | new_key.offset = split; |
| 670 | ret = btrfs_duplicate_item(trans, root, path, &new_key); |
| 671 | if (ret == -EAGAIN) { |
| 672 | btrfs_release_path(root, path); |
| 673 | goto again; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 674 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 675 | BUG_ON(ret < 0); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 676 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 677 | leaf = path->nodes[0]; |
| 678 | fi = btrfs_item_ptr(leaf, path->slots[0] - 1, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 679 | struct btrfs_file_extent_item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 680 | btrfs_set_file_extent_num_bytes(leaf, fi, |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 681 | split - key.offset); |
| 682 | |
| 683 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 684 | struct btrfs_file_extent_item); |
| 685 | |
| 686 | btrfs_set_file_extent_offset(leaf, fi, split - orig_offset); |
| 687 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 688 | extent_end - split); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 689 | btrfs_mark_buffer_dirty(leaf); |
| 690 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 691 | ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0, |
| 692 | root->root_key.objectid, |
| 693 | inode->i_ino, orig_offset); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 694 | BUG_ON(ret); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 695 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 696 | if (split == start) { |
| 697 | key.offset = start; |
| 698 | } else { |
| 699 | BUG_ON(start != key.offset); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 700 | path->slots[0]--; |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 701 | extent_end = end; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 702 | } |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 703 | recow = 1; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 704 | } |
| 705 | |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 706 | other_start = end; |
| 707 | other_end = 0; |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 708 | if (extent_mergeable(leaf, path->slots[0] + 1, |
| 709 | inode->i_ino, bytenr, orig_offset, |
| 710 | &other_start, &other_end)) { |
| 711 | if (recow) { |
| 712 | btrfs_release_path(root, path); |
| 713 | goto again; |
| 714 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 715 | extent_end = other_end; |
| 716 | del_slot = path->slots[0] + 1; |
| 717 | del_nr++; |
| 718 | ret = btrfs_free_extent(trans, root, bytenr, num_bytes, |
| 719 | 0, root->root_key.objectid, |
| 720 | inode->i_ino, orig_offset); |
| 721 | BUG_ON(ret); |
| 722 | } |
| 723 | other_start = 0; |
| 724 | other_end = start; |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 725 | if (extent_mergeable(leaf, path->slots[0] - 1, |
| 726 | inode->i_ino, bytenr, orig_offset, |
| 727 | &other_start, &other_end)) { |
| 728 | if (recow) { |
| 729 | btrfs_release_path(root, path); |
| 730 | goto again; |
| 731 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 732 | key.offset = other_start; |
| 733 | del_slot = path->slots[0]; |
| 734 | del_nr++; |
| 735 | ret = btrfs_free_extent(trans, root, bytenr, num_bytes, |
| 736 | 0, root->root_key.objectid, |
| 737 | inode->i_ino, orig_offset); |
| 738 | BUG_ON(ret); |
| 739 | } |
| 740 | if (del_nr == 0) { |
Shaohua Li | 3f6fae9 | 2010-02-11 07:43:00 +0000 | [diff] [blame] | 741 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 742 | struct btrfs_file_extent_item); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 743 | btrfs_set_file_extent_type(leaf, fi, |
| 744 | BTRFS_FILE_EXTENT_REG); |
| 745 | btrfs_mark_buffer_dirty(leaf); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 746 | } else { |
Shaohua Li | 3f6fae9 | 2010-02-11 07:43:00 +0000 | [diff] [blame] | 747 | fi = btrfs_item_ptr(leaf, del_slot - 1, |
| 748 | struct btrfs_file_extent_item); |
Yan, Zheng | 6c7d54a | 2010-01-15 08:43:09 +0000 | [diff] [blame] | 749 | btrfs_set_file_extent_type(leaf, fi, |
| 750 | BTRFS_FILE_EXTENT_REG); |
| 751 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 752 | extent_end - key.offset); |
| 753 | btrfs_mark_buffer_dirty(leaf); |
| 754 | |
| 755 | ret = btrfs_del_items(trans, root, path, del_slot, del_nr); |
| 756 | BUG_ON(ret); |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 757 | } |
Yan, Zheng | 920bbbf | 2009-11-12 09:34:08 +0000 | [diff] [blame] | 758 | out: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 759 | btrfs_free_path(path); |
| 760 | return 0; |
| 761 | } |
| 762 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 763 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 764 | * this gets pages into the page cache and locks them down, it also properly |
| 765 | * waits for data=ordered extents to finish before allowing the pages to be |
| 766 | * modified. |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 767 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 768 | static noinline int prepare_pages(struct btrfs_root *root, struct file *file, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 769 | struct page **pages, size_t num_pages, |
| 770 | loff_t pos, unsigned long first_index, |
| 771 | unsigned long last_index, size_t write_bytes) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 772 | { |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 773 | struct extent_state *cached_state = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 774 | int i; |
| 775 | unsigned long index = pos >> PAGE_CACHE_SHIFT; |
Chris Mason | 6da6aba | 2007-12-18 16:15:09 -0500 | [diff] [blame] | 776 | struct inode *inode = fdentry(file)->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 777 | int err = 0; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 778 | u64 start_pos; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 779 | u64 last_pos; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 780 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 781 | start_pos = pos & ~((u64)root->sectorsize - 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 782 | last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 783 | |
Yan Zheng | 9036c10 | 2008-10-30 14:19:41 -0400 | [diff] [blame] | 784 | if (start_pos > inode->i_size) { |
| 785 | err = btrfs_cont_expand(inode, start_pos); |
| 786 | if (err) |
| 787 | return err; |
| 788 | } |
| 789 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 790 | memset(pages, 0, num_pages * sizeof(struct page *)); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 791 | again: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 792 | for (i = 0; i < num_pages; i++) { |
| 793 | pages[i] = grab_cache_page(inode->i_mapping, index + i); |
| 794 | if (!pages[i]) { |
| 795 | err = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 796 | BUG_ON(1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 797 | } |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 798 | wait_on_page_writeback(pages[i]); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 799 | } |
Chris Mason | 0762704 | 2008-02-19 11:29:24 -0500 | [diff] [blame] | 800 | if (start_pos < inode->i_size) { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 801 | struct btrfs_ordered_extent *ordered; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 802 | lock_extent_bits(&BTRFS_I(inode)->io_tree, |
| 803 | start_pos, last_pos - 1, 0, &cached_state, |
| 804 | GFP_NOFS); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 805 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 806 | last_pos - 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 807 | if (ordered && |
| 808 | ordered->file_offset + ordered->len > start_pos && |
| 809 | ordered->file_offset < last_pos) { |
| 810 | btrfs_put_ordered_extent(ordered); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 811 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
| 812 | start_pos, last_pos - 1, |
| 813 | &cached_state, GFP_NOFS); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 814 | for (i = 0; i < num_pages; i++) { |
| 815 | unlock_page(pages[i]); |
| 816 | page_cache_release(pages[i]); |
| 817 | } |
| 818 | btrfs_wait_ordered_range(inode, start_pos, |
| 819 | last_pos - start_pos); |
| 820 | goto again; |
| 821 | } |
| 822 | if (ordered) |
| 823 | btrfs_put_ordered_extent(ordered); |
| 824 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 825 | clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos, |
Josef Bacik | 32c00af | 2009-10-08 13:34:05 -0400 | [diff] [blame] | 826 | last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 827 | EXTENT_DO_ACCOUNTING, 0, 0, &cached_state, |
Chris Mason | 0762704 | 2008-02-19 11:29:24 -0500 | [diff] [blame] | 828 | GFP_NOFS); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 829 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
| 830 | start_pos, last_pos - 1, &cached_state, |
| 831 | GFP_NOFS); |
Chris Mason | 0762704 | 2008-02-19 11:29:24 -0500 | [diff] [blame] | 832 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 833 | for (i = 0; i < num_pages; i++) { |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 834 | clear_page_dirty_for_io(pages[i]); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 835 | set_page_extent_mapped(pages[i]); |
| 836 | WARN_ON(!PageLocked(pages[i])); |
| 837 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 838 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 839 | } |
| 840 | |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 841 | static ssize_t btrfs_file_aio_write(struct kiocb *iocb, |
| 842 | const struct iovec *iov, |
| 843 | unsigned long nr_segs, loff_t pos) |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 844 | { |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 845 | struct file *file = iocb->ki_filp; |
| 846 | struct inode *inode = fdentry(file)->d_inode; |
| 847 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 848 | struct page *pinned[2]; |
| 849 | struct page **pages = NULL; |
| 850 | struct iov_iter i; |
| 851 | loff_t *ppos = &iocb->ki_pos; |
Chris Mason | 2ff3e9b | 2007-10-29 14:36:41 -0400 | [diff] [blame] | 852 | loff_t start_pos; |
| 853 | ssize_t num_written = 0; |
| 854 | ssize_t err = 0; |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 855 | size_t count; |
| 856 | size_t ocount; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 857 | int ret = 0; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 858 | int nrptrs; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 859 | unsigned long first_index; |
| 860 | unsigned long last_index; |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 861 | int will_write; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 862 | int buffered = 0; |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 863 | int copied = 0; |
| 864 | int dirty_pages = 0; |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 865 | |
Christoph Hellwig | 6b2f3d1 | 2009-10-27 11:05:28 +0100 | [diff] [blame] | 866 | will_write = ((file->f_flags & O_DSYNC) || IS_SYNC(inode) || |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 867 | (file->f_flags & O_DIRECT)); |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 868 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 869 | pinned[0] = NULL; |
| 870 | pinned[1] = NULL; |
Chris Mason | 2ff3e9b | 2007-10-29 14:36:41 -0400 | [diff] [blame] | 871 | |
Chris Mason | 2ff3e9b | 2007-10-29 14:36:41 -0400 | [diff] [blame] | 872 | start_pos = pos; |
| 873 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 874 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); |
Chris Mason | 2b1f55b | 2008-09-24 11:48:04 -0400 | [diff] [blame] | 875 | |
Chris Mason | ab93dbe | 2009-10-01 12:29:10 -0400 | [diff] [blame] | 876 | mutex_lock(&inode->i_mutex); |
| 877 | |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 878 | err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ); |
| 879 | if (err) |
| 880 | goto out; |
| 881 | count = ocount; |
| 882 | |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 883 | current->backing_dev_info = inode->i_mapping->backing_dev_info; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 884 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 885 | if (err) |
Chris Mason | ab93dbe | 2009-10-01 12:29:10 -0400 | [diff] [blame] | 886 | goto out; |
| 887 | |
Jeff Mahoney | 12fa8ec | 2008-05-02 15:03:58 -0400 | [diff] [blame] | 888 | if (count == 0) |
Chris Mason | ab93dbe | 2009-10-01 12:29:10 -0400 | [diff] [blame] | 889 | goto out; |
Sven Wegener | 0ee0fda | 2008-07-30 16:54:26 -0400 | [diff] [blame] | 890 | |
| 891 | err = file_remove_suid(file); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 892 | if (err) |
Chris Mason | ab93dbe | 2009-10-01 12:29:10 -0400 | [diff] [blame] | 893 | goto out; |
| 894 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 895 | file_update_time(file); |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 896 | BTRFS_I(inode)->sequence++; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 897 | |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 898 | if (unlikely(file->f_flags & O_DIRECT)) { |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 899 | num_written = generic_file_direct_write(iocb, iov, &nr_segs, |
| 900 | pos, ppos, count, |
| 901 | ocount); |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 902 | /* |
| 903 | * the generic O_DIRECT will update in-memory i_size after the |
| 904 | * DIOs are done. But our endio handlers that update the on |
| 905 | * disk i_size never update past the in memory i_size. So we |
| 906 | * need one more update here to catch any additions to the |
| 907 | * file |
| 908 | */ |
| 909 | if (inode->i_size != BTRFS_I(inode)->disk_i_size) { |
| 910 | btrfs_ordered_update_i_size(inode, inode->i_size, NULL); |
| 911 | mark_inode_dirty(inode); |
| 912 | } |
| 913 | |
| 914 | if (num_written < 0) { |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 915 | ret = num_written; |
| 916 | num_written = 0; |
| 917 | goto out; |
| 918 | } else if (num_written == count) { |
| 919 | /* pick up pos changes done by the generic code */ |
| 920 | pos = *ppos; |
| 921 | goto out; |
| 922 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 923 | /* |
| 924 | * We are going to do buffered for the rest of the range, so we |
| 925 | * need to make sure to invalidate the buffered pages when we're |
| 926 | * done. |
| 927 | */ |
| 928 | buffered = 1; |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 929 | pos += num_written; |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 930 | } |
| 931 | |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 932 | iov_iter_init(&i, iov, nr_segs, count, num_written); |
| 933 | nrptrs = min((iov_iter_count(&i) + PAGE_CACHE_SIZE - 1) / |
| 934 | PAGE_CACHE_SIZE, PAGE_CACHE_SIZE / |
| 935 | (sizeof(struct page *))); |
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 | |
Chris Mason | ab93dbe | 2009-10-01 12:29:10 -0400 | [diff] [blame] | 938 | /* generic_write_checks can change our pos */ |
| 939 | start_pos = pos; |
| 940 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 941 | first_index = pos >> PAGE_CACHE_SHIFT; |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 942 | last_index = (pos + iov_iter_count(&i)) >> PAGE_CACHE_SHIFT; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 943 | |
| 944 | /* |
| 945 | * there are lots of better ways to do this, but this code |
| 946 | * makes sure the first and last page in the file range are |
| 947 | * up to date and ready for cow |
| 948 | */ |
| 949 | if ((pos & (PAGE_CACHE_SIZE - 1))) { |
| 950 | pinned[0] = grab_cache_page(inode->i_mapping, first_index); |
| 951 | if (!PageUptodate(pinned[0])) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 952 | ret = btrfs_readpage(NULL, pinned[0]); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 953 | BUG_ON(ret); |
| 954 | wait_on_page_locked(pinned[0]); |
| 955 | } else { |
| 956 | unlock_page(pinned[0]); |
| 957 | } |
| 958 | } |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 959 | if ((pos + iov_iter_count(&i)) & (PAGE_CACHE_SIZE - 1)) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 960 | pinned[1] = grab_cache_page(inode->i_mapping, last_index); |
| 961 | if (!PageUptodate(pinned[1])) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 962 | ret = btrfs_readpage(NULL, pinned[1]); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 963 | BUG_ON(ret); |
| 964 | wait_on_page_locked(pinned[1]); |
| 965 | } else { |
| 966 | unlock_page(pinned[1]); |
| 967 | } |
| 968 | } |
| 969 | |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 970 | while (iov_iter_count(&i) > 0) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 971 | size_t offset = pos & (PAGE_CACHE_SIZE - 1); |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 972 | size_t write_bytes = min(iov_iter_count(&i), |
| 973 | nrptrs * (size_t)PAGE_CACHE_SIZE - |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 974 | offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 975 | size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >> |
| 976 | PAGE_CACHE_SHIFT; |
| 977 | |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 978 | WARN_ON(num_pages > nrptrs); |
yanhai zhu | 9aead43 | 2009-01-05 15:49:11 -0500 | [diff] [blame] | 979 | memset(pages, 0, sizeof(struct page *) * nrptrs); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 980 | |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 981 | /* |
| 982 | * Fault pages before locking them in prepare_pages |
| 983 | * to avoid recursive lock |
| 984 | */ |
| 985 | if (unlikely(iov_iter_fault_in_readable(&i, write_bytes))) { |
| 986 | ret = -EFAULT; |
| 987 | goto out; |
| 988 | } |
| 989 | |
| 990 | ret = btrfs_delalloc_reserve_space(inode, |
| 991 | num_pages << PAGE_CACHE_SHIFT); |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 992 | if (ret) |
| 993 | goto out; |
| 994 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 995 | ret = prepare_pages(root, file, pages, num_pages, |
| 996 | pos, first_index, last_index, |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 997 | write_bytes); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 998 | if (ret) { |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 999 | btrfs_delalloc_release_space(inode, |
| 1000 | num_pages << PAGE_CACHE_SHIFT); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1001 | goto out; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1002 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1003 | |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1004 | copied = btrfs_copy_from_user(pos, num_pages, |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 1005 | write_bytes, pages, &i); |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1006 | dirty_pages = (copied + PAGE_CACHE_SIZE - 1) >> |
| 1007 | PAGE_CACHE_SHIFT; |
| 1008 | |
| 1009 | if (num_pages > dirty_pages) { |
| 1010 | if (copied > 0) |
| 1011 | atomic_inc( |
| 1012 | &BTRFS_I(inode)->outstanding_extents); |
| 1013 | btrfs_delalloc_release_space(inode, |
| 1014 | (num_pages - dirty_pages) << |
| 1015 | PAGE_CACHE_SHIFT); |
| 1016 | } |
| 1017 | |
| 1018 | if (copied > 0) { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 1019 | dirty_and_release_pages(NULL, root, file, pages, |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1020 | dirty_pages, pos, copied); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1021 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1022 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1023 | btrfs_drop_pages(pages, num_pages); |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1024 | |
| 1025 | if (copied > 0) { |
| 1026 | if (will_write) { |
| 1027 | filemap_fdatawrite_range(inode->i_mapping, pos, |
| 1028 | pos + copied - 1); |
| 1029 | } else { |
| 1030 | balance_dirty_pages_ratelimited_nr( |
| 1031 | inode->i_mapping, |
| 1032 | dirty_pages); |
| 1033 | if (dirty_pages < |
| 1034 | (root->leafsize >> PAGE_CACHE_SHIFT) + 1) |
| 1035 | btrfs_btree_balance_dirty(root, 1); |
| 1036 | btrfs_throttle(root); |
| 1037 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1038 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1039 | |
Xin Zhong | 914ee29 | 2010-12-09 09:30:14 +0000 | [diff] [blame] | 1040 | pos += copied; |
| 1041 | num_written += copied; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1042 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1043 | cond_resched(); |
| 1044 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1045 | out: |
Chris Mason | 1832a6d | 2007-12-21 16:27:21 -0500 | [diff] [blame] | 1046 | mutex_unlock(&inode->i_mutex); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1047 | if (ret) |
| 1048 | err = ret; |
Chris Mason | 5b92ee7 | 2008-01-03 13:46:11 -0500 | [diff] [blame] | 1049 | |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 1050 | kfree(pages); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1051 | if (pinned[0]) |
| 1052 | page_cache_release(pinned[0]); |
| 1053 | if (pinned[1]) |
| 1054 | page_cache_release(pinned[1]); |
| 1055 | *ppos = pos; |
Chris Mason | 2ff3e9b | 2007-10-29 14:36:41 -0400 | [diff] [blame] | 1056 | |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 1057 | /* |
| 1058 | * we want to make sure fsync finds this change |
| 1059 | * but we haven't joined a transaction running right now. |
| 1060 | * |
| 1061 | * Later on, someone is sure to update the inode and get the |
| 1062 | * real transid recorded. |
| 1063 | * |
| 1064 | * We set last_trans now to the fs_info generation + 1, |
| 1065 | * this will either be one more than the running transaction |
| 1066 | * or the generation used for the next transaction if there isn't |
| 1067 | * one running right now. |
| 1068 | */ |
| 1069 | BTRFS_I(inode)->last_trans = root->fs_info->generation + 1; |
| 1070 | |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 1071 | if (num_written > 0 && will_write) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1072 | struct btrfs_trans_handle *trans; |
| 1073 | |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 1074 | err = btrfs_wait_ordered_range(inode, start_pos, num_written); |
| 1075 | if (err) |
Chris Mason | 2ff3e9b | 2007-10-29 14:36:41 -0400 | [diff] [blame] | 1076 | num_written = err; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1077 | |
Christoph Hellwig | 6b2f3d1 | 2009-10-27 11:05:28 +0100 | [diff] [blame] | 1078 | if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) { |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1079 | trans = btrfs_start_transaction(root, 0); |
Josef Bacik | 495e867 | 2010-11-19 20:36:10 +0000 | [diff] [blame] | 1080 | if (IS_ERR(trans)) { |
| 1081 | num_written = PTR_ERR(trans); |
| 1082 | goto done; |
| 1083 | } |
| 1084 | mutex_lock(&inode->i_mutex); |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 1085 | ret = btrfs_log_dentry_safe(trans, root, |
| 1086 | file->f_dentry); |
Josef Bacik | 495e867 | 2010-11-19 20:36:10 +0000 | [diff] [blame] | 1087 | mutex_unlock(&inode->i_mutex); |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 1088 | if (ret == 0) { |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 1089 | ret = btrfs_sync_log(trans, root); |
| 1090 | if (ret == 0) |
| 1091 | btrfs_end_transaction(trans, root); |
| 1092 | else |
| 1093 | btrfs_commit_transaction(trans, root); |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 1094 | } else if (ret != BTRFS_NO_LOG_SYNC) { |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 1095 | btrfs_commit_transaction(trans, root); |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 1096 | } else { |
| 1097 | btrfs_end_transaction(trans, root); |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 1098 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1099 | } |
Josef Bacik | 4b46fce | 2010-05-23 11:00:55 -0400 | [diff] [blame] | 1100 | if (file->f_flags & O_DIRECT && buffered) { |
Chris Mason | cb843a6 | 2008-10-03 12:30:02 -0400 | [diff] [blame] | 1101 | invalidate_mapping_pages(inode->i_mapping, |
| 1102 | start_pos >> PAGE_CACHE_SHIFT, |
| 1103 | (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT); |
| 1104 | } |
Chris Mason | 2ff3e9b | 2007-10-29 14:36:41 -0400 | [diff] [blame] | 1105 | } |
Josef Bacik | 495e867 | 2010-11-19 20:36:10 +0000 | [diff] [blame] | 1106 | done: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1107 | current->backing_dev_info = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1108 | return num_written ? num_written : err; |
| 1109 | } |
| 1110 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1111 | int btrfs_release_file(struct inode *inode, struct file *filp) |
Mingming | e1b81e6 | 2008-05-27 10:55:43 -0400 | [diff] [blame] | 1112 | { |
Chris Mason | 5a3f23d | 2009-03-31 13:27:11 -0400 | [diff] [blame] | 1113 | /* |
| 1114 | * ordered_data_close is set by settattr when we are about to truncate |
| 1115 | * a file from a non-zero size to a zero size. This tries to |
| 1116 | * flush down new bytes that may have been written if the |
| 1117 | * application were using truncate to replace a file in place. |
| 1118 | */ |
| 1119 | if (BTRFS_I(inode)->ordered_data_close) { |
| 1120 | BTRFS_I(inode)->ordered_data_close = 0; |
| 1121 | btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode); |
| 1122 | if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT) |
| 1123 | filemap_flush(inode->i_mapping); |
| 1124 | } |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 1125 | if (filp->private_data) |
| 1126 | btrfs_ioctl_trans_end(filp); |
Mingming | e1b81e6 | 2008-05-27 10:55:43 -0400 | [diff] [blame] | 1127 | return 0; |
| 1128 | } |
| 1129 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1130 | /* |
| 1131 | * fsync call for both files and directories. This logs the inode into |
| 1132 | * the tree log instead of forcing full commits whenever possible. |
| 1133 | * |
| 1134 | * It needs to call filemap_fdatawait so that all ordered extent updates are |
| 1135 | * in the metadata btree are up to date for copying to the log. |
| 1136 | * |
| 1137 | * It drops the inode mutex before doing the tree log commit. This is an |
| 1138 | * important optimization for directories because holding the mutex prevents |
| 1139 | * new operations on the dir while we write to disk. |
| 1140 | */ |
Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 1141 | int btrfs_sync_file(struct file *file, int datasync) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1142 | { |
Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 1143 | struct dentry *dentry = file->f_path.dentry; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1144 | struct inode *inode = dentry->d_inode; |
| 1145 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1146 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1147 | struct btrfs_trans_handle *trans; |
| 1148 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 1149 | |
| 1150 | /* we wait first, since the writeback may change the inode */ |
| 1151 | root->log_batch++; |
| 1152 | /* the VFS called filemap_fdatawrite for us */ |
| 1153 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
| 1154 | root->log_batch++; |
| 1155 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1156 | /* |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1157 | * check the transaction that last modified this inode |
| 1158 | * and see if its already been committed |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1159 | */ |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1160 | if (!BTRFS_I(inode)->last_trans) |
| 1161 | goto out; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1162 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 1163 | /* |
| 1164 | * if the last transaction that changed this file was before |
| 1165 | * the current transaction, we can bail out now without any |
| 1166 | * syncing |
| 1167 | */ |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 1168 | mutex_lock(&root->fs_info->trans_mutex); |
| 1169 | if (BTRFS_I(inode)->last_trans <= |
| 1170 | root->fs_info->last_trans_committed) { |
| 1171 | BTRFS_I(inode)->last_trans = 0; |
| 1172 | mutex_unlock(&root->fs_info->trans_mutex); |
| 1173 | goto out; |
| 1174 | } |
| 1175 | mutex_unlock(&root->fs_info->trans_mutex); |
| 1176 | |
| 1177 | /* |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1178 | * ok we haven't committed the transaction yet, lets do a commit |
| 1179 | */ |
Dan Carpenter | 6f902af | 2010-05-29 09:49:07 +0000 | [diff] [blame] | 1180 | if (file->private_data) |
Sage Weil | 6bf13c0 | 2008-06-10 10:07:39 -0400 | [diff] [blame] | 1181 | btrfs_ioctl_trans_end(file); |
| 1182 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 1183 | trans = btrfs_start_transaction(root, 0); |
| 1184 | if (IS_ERR(trans)) { |
| 1185 | ret = PTR_ERR(trans); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1186 | goto out; |
| 1187 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1188 | |
Chris Mason | 2cfbd50 | 2009-02-20 10:55:10 -0500 | [diff] [blame] | 1189 | ret = btrfs_log_dentry_safe(trans, root, dentry); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1190 | if (ret < 0) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1191 | goto out; |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 1192 | |
| 1193 | /* we've logged all the items and now have a consistent |
| 1194 | * version of the file in the log. It is possible that |
| 1195 | * someone will come in and modify the file, but that's |
| 1196 | * fine because the log is consistent on disk, and we |
| 1197 | * have references to all of the file's extents |
| 1198 | * |
| 1199 | * It is possible that someone will come in and log the |
| 1200 | * file again, but that will end up using the synchronization |
| 1201 | * inside btrfs_sync_log to keep things safe. |
| 1202 | */ |
Chris Mason | 2cfbd50 | 2009-02-20 10:55:10 -0500 | [diff] [blame] | 1203 | mutex_unlock(&dentry->d_inode->i_mutex); |
Chris Mason | 49eb7e4 | 2008-09-11 15:53:12 -0400 | [diff] [blame] | 1204 | |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 1205 | if (ret != BTRFS_NO_LOG_SYNC) { |
| 1206 | if (ret > 0) { |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 1207 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 257c62e | 2009-10-13 13:21:08 -0400 | [diff] [blame] | 1208 | } else { |
| 1209 | ret = btrfs_sync_log(trans, root); |
| 1210 | if (ret == 0) |
| 1211 | ret = btrfs_end_transaction(trans, root); |
| 1212 | else |
| 1213 | ret = btrfs_commit_transaction(trans, root); |
| 1214 | } |
| 1215 | } else { |
| 1216 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1217 | } |
Chris Mason | 2cfbd50 | 2009-02-20 10:55:10 -0500 | [diff] [blame] | 1218 | mutex_lock(&dentry->d_inode->i_mutex); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1219 | out: |
Roel Kluin | 014e4ac | 2010-01-29 10:42:11 +0000 | [diff] [blame] | 1220 | return ret > 0 ? -EIO : ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1221 | } |
| 1222 | |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 1223 | static const struct vm_operations_struct btrfs_file_vm_ops = { |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 1224 | .fault = filemap_fault, |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1225 | .page_mkwrite = btrfs_page_mkwrite, |
| 1226 | }; |
| 1227 | |
| 1228 | static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma) |
| 1229 | { |
Miao Xie | 058a457 | 2010-05-20 07:21:50 +0000 | [diff] [blame] | 1230 | struct address_space *mapping = filp->f_mapping; |
| 1231 | |
| 1232 | if (!mapping->a_ops->readpage) |
| 1233 | return -ENOEXEC; |
| 1234 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1235 | file_accessed(filp); |
Miao Xie | 058a457 | 2010-05-20 07:21:50 +0000 | [diff] [blame] | 1236 | vma->vm_ops = &btrfs_file_vm_ops; |
| 1237 | vma->vm_flags |= VM_CAN_NONLINEAR; |
| 1238 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1239 | return 0; |
| 1240 | } |
| 1241 | |
Alexey Dobriyan | 828c095 | 2009-10-01 15:43:56 -0700 | [diff] [blame] | 1242 | const struct file_operations btrfs_file_operations = { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1243 | .llseek = generic_file_llseek, |
| 1244 | .read = do_sync_read, |
Miao Xie | 4a001071 | 2010-06-07 03:38:51 +0000 | [diff] [blame] | 1245 | .write = do_sync_write, |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1246 | .aio_read = generic_file_aio_read, |
Chris Mason | e9906a9 | 2007-12-14 12:56:58 -0500 | [diff] [blame] | 1247 | .splice_read = generic_file_splice_read, |
Josef Bacik | 11c65dc | 2010-05-23 11:07:21 -0400 | [diff] [blame] | 1248 | .aio_write = btrfs_file_aio_write, |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1249 | .mmap = btrfs_file_mmap, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1250 | .open = generic_file_open, |
Mingming | e1b81e6 | 2008-05-27 10:55:43 -0400 | [diff] [blame] | 1251 | .release = btrfs_release_file, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1252 | .fsync = btrfs_sync_file, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 1253 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1254 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 1255 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1256 | #endif |
| 1257 | }; |