blob: ab22ca4f237fcaad91aae7b54bbc675a4d639d30 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
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 Mason39279cc2007-06-12 06:35:45 -040019#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 Mason39279cc2007-06-12 06:35:45 -040025#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 Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Chris Mason39279cc2007-06-12 06:35:45 -040032#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 Masone02119d2008-09-05 16:13:11 -040038#include "tree-log.h"
39#include "locking.h"
Jeff Mahoney12fa8ec2008-05-02 15:03:58 -040040#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040041
42
Chris Masond352ac62008-09-29 15:18:18 -040043/* simple helper to fault in pages and copy. This should go away
44 * and be replaced with calls into generic code.
45 */
Chris Masond3977122009-01-05 21:25:51 -050046static noinline int btrfs_copy_from_user(loff_t pos, int num_pages,
Chris Masona1b32a52008-09-05 16:09:51 -040047 int write_bytes,
48 struct page **prepared_pages,
Josef Bacik11c65dc2010-05-23 11:07:21 -040049 struct iov_iter *i)
Chris Mason39279cc2007-06-12 06:35:45 -040050{
Xin Zhong914ee292010-12-09 09:30:14 +000051 size_t copied = 0;
Josef Bacik11c65dc2010-05-23 11:07:21 -040052 int pg = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040053 int offset = pos & (PAGE_CACHE_SIZE - 1);
Xin Zhong914ee292010-12-09 09:30:14 +000054 int total_copied = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040055
Josef Bacik11c65dc2010-05-23 11:07:21 -040056 while (write_bytes > 0) {
Chris Mason39279cc2007-06-12 06:35:45 -040057 size_t count = min_t(size_t,
58 PAGE_CACHE_SIZE - offset, write_bytes);
Josef Bacik11c65dc2010-05-23 11:07:21 -040059 struct page *page = prepared_pages[pg];
Xin Zhong914ee292010-12-09 09:30:14 +000060 /*
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 Bacik11c65dc2010-05-23 11:07:21 -040069
Chris Mason39279cc2007-06-12 06:35:45 -040070 /* Flush processor's dcache for this page */
71 flush_dcache_page(page);
Chris Mason31339ac2011-03-07 11:10:24 -050072
73 /*
74 * if we get a partial write, we can end up with
75 * partially up to date pages. These add
76 * a lot of complexity, so make sure they don't
77 * happen by forcing this copy to be retried.
78 *
79 * The rest of the btrfs_file_write code will fall
80 * back to page at a time copies after we return 0.
81 */
82 if (!PageUptodate(page) && copied < count)
83 copied = 0;
84
Josef Bacik11c65dc2010-05-23 11:07:21 -040085 iov_iter_advance(i, copied);
86 write_bytes -= copied;
Xin Zhong914ee292010-12-09 09:30:14 +000087 total_copied += copied;
Chris Mason39279cc2007-06-12 06:35:45 -040088
Xin Zhong914ee292010-12-09 09:30:14 +000089 /* Return to btrfs_file_aio_write to fault page */
Josef Bacik11c65dc2010-05-23 11:07:21 -040090 if (unlikely(copied == 0)) {
Xin Zhong914ee292010-12-09 09:30:14 +000091 break;
Josef Bacik11c65dc2010-05-23 11:07:21 -040092 }
93
94 if (unlikely(copied < PAGE_CACHE_SIZE - offset)) {
95 offset += copied;
96 } else {
97 pg++;
98 offset = 0;
99 }
Chris Mason39279cc2007-06-12 06:35:45 -0400100 }
Xin Zhong914ee292010-12-09 09:30:14 +0000101 return total_copied;
Chris Mason39279cc2007-06-12 06:35:45 -0400102}
103
Chris Masond352ac62008-09-29 15:18:18 -0400104/*
105 * unlocks pages after btrfs_file_write is done with them
106 */
Chris Masond3977122009-01-05 21:25:51 -0500107static noinline void btrfs_drop_pages(struct page **pages, size_t num_pages)
Chris Mason39279cc2007-06-12 06:35:45 -0400108{
109 size_t i;
110 for (i = 0; i < num_pages; i++) {
111 if (!pages[i])
112 break;
Chris Masond352ac62008-09-29 15:18:18 -0400113 /* page checked is some magic around finding pages that
114 * have been modified without going through btrfs_set_page_dirty
115 * clear it here
116 */
Chris Mason4a096752008-07-21 10:29:44 -0400117 ClearPageChecked(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -0400118 unlock_page(pages[i]);
119 mark_page_accessed(pages[i]);
120 page_cache_release(pages[i]);
121 }
122}
123
Chris Masond352ac62008-09-29 15:18:18 -0400124/*
125 * after copy_from_user, pages need to be dirtied and we need to make
126 * sure holes are created between the current EOF and the start of
127 * any next extents (if required).
128 *
129 * this also makes the decision about creating an inline extent vs
130 * doing real data extents, marking pages dirty and delalloc as required.
131 */
Chris Masond3977122009-01-05 21:25:51 -0500132static noinline int dirty_and_release_pages(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400133 struct btrfs_root *root,
134 struct file *file,
135 struct page **pages,
136 size_t num_pages,
137 loff_t pos,
138 size_t write_bytes)
139{
Chris Mason39279cc2007-06-12 06:35:45 -0400140 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400141 int i;
Chris Mason6da6aba2007-12-18 16:15:09 -0500142 struct inode *inode = fdentry(file)->d_inode;
Chris Masondb945352007-10-15 16:15:53 -0400143 u64 num_bytes;
Chris Masona52d9a82007-08-27 16:49:44 -0400144 u64 start_pos;
145 u64 end_of_last_block;
146 u64 end_pos = pos + write_bytes;
147 loff_t isize = i_size_read(inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400148
Chris Mason5f39d392007-10-15 16:14:19 -0400149 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -0400150 num_bytes = (write_bytes + pos - start_pos +
151 root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
Chris Mason39279cc2007-06-12 06:35:45 -0400152
Chris Masondb945352007-10-15 16:15:53 -0400153 end_of_last_block = start_pos + num_bytes - 1;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000154 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
155 NULL);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400156 BUG_ON(err);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400157
Chris Masonc8b97812008-10-29 14:49:59 -0400158 for (i = 0; i < num_pages; i++) {
159 struct page *p = pages[i];
160 SetPageUptodate(p);
161 ClearPageChecked(p);
162 set_page_dirty(p);
Chris Masona52d9a82007-08-27 16:49:44 -0400163 }
164 if (end_pos > isize) {
165 i_size_write(inode, end_pos);
Chris Masonf597bb12009-06-27 21:06:22 -0400166 /* we've only changed i_size in ram, and we haven't updated
167 * the disk i_size. There is no need to log the inode
168 * at this time.
169 */
Chris Mason39279cc2007-06-12 06:35:45 -0400170 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400171 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400172}
173
Chris Masond352ac62008-09-29 15:18:18 -0400174/*
175 * this drops all the extents in the cache that intersect the range
176 * [start, end]. Existing extents are split as required.
177 */
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400178int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
179 int skip_pinned)
Chris Masona52d9a82007-08-27 16:49:44 -0400180{
181 struct extent_map *em;
Chris Mason3b951512008-04-17 11:29:12 -0400182 struct extent_map *split = NULL;
183 struct extent_map *split2 = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400184 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan39b56372008-02-15 10:40:50 -0500185 u64 len = end - start + 1;
Chris Mason3b951512008-04-17 11:29:12 -0400186 int ret;
187 int testend = 1;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400188 unsigned long flags;
Chris Masonc8b97812008-10-29 14:49:59 -0400189 int compressed = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400190
Chris Masone6dcd2d2008-07-17 12:53:50 -0400191 WARN_ON(end < start);
Chris Mason3b951512008-04-17 11:29:12 -0400192 if (end == (u64)-1) {
Yan39b56372008-02-15 10:40:50 -0500193 len = (u64)-1;
Chris Mason3b951512008-04-17 11:29:12 -0400194 testend = 0;
195 }
Chris Masond3977122009-01-05 21:25:51 -0500196 while (1) {
Chris Mason3b951512008-04-17 11:29:12 -0400197 if (!split)
198 split = alloc_extent_map(GFP_NOFS);
199 if (!split2)
200 split2 = alloc_extent_map(GFP_NOFS);
Tsutomu Itohc26a9202011-02-14 00:45:29 +0000201 BUG_ON(!split || !split2);
Chris Mason3b951512008-04-17 11:29:12 -0400202
Chris Mason890871b2009-09-02 16:24:52 -0400203 write_lock(&em_tree->lock);
Yan39b56372008-02-15 10:40:50 -0500204 em = lookup_extent_mapping(em_tree, start, len);
Chris Masond1310b22008-01-24 16:13:08 -0500205 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -0400206 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -0400207 break;
Chris Masond1310b22008-01-24 16:13:08 -0500208 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400209 flags = em->flags;
210 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
Yan, Zheng55ef6892009-11-12 09:36:44 +0000211 if (testend && em->start + em->len >= start + len) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400212 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400213 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400214 break;
215 }
Yan, Zheng55ef6892009-11-12 09:36:44 +0000216 start = em->start + em->len;
217 if (testend)
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400218 len = start + len - (em->start + em->len);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400219 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400220 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400221 continue;
222 }
Chris Masonc8b97812008-10-29 14:49:59 -0400223 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Mason3ce7e672008-07-31 15:42:54 -0400224 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400225 remove_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -0400226
227 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
228 em->start < start) {
229 split->start = em->start;
230 split->len = start - em->start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -0500231 split->orig_start = em->orig_start;
Chris Mason3b951512008-04-17 11:29:12 -0400232 split->block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400233
234 if (compressed)
235 split->block_len = em->block_len;
236 else
237 split->block_len = split->len;
238
Chris Mason3b951512008-04-17 11:29:12 -0400239 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400240 split->flags = flags;
Li Zefan261507a02010-12-17 14:21:50 +0800241 split->compress_type = em->compress_type;
Chris Mason3b951512008-04-17 11:29:12 -0400242 ret = add_extent_mapping(em_tree, split);
243 BUG_ON(ret);
244 free_extent_map(split);
245 split = split2;
246 split2 = NULL;
247 }
248 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
249 testend && em->start + em->len > start + len) {
250 u64 diff = start + len - em->start;
251
252 split->start = start + len;
253 split->len = em->start + em->len - (start + len);
254 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400255 split->flags = flags;
Li Zefan261507a02010-12-17 14:21:50 +0800256 split->compress_type = em->compress_type;
Chris Mason3b951512008-04-17 11:29:12 -0400257
Chris Masonc8b97812008-10-29 14:49:59 -0400258 if (compressed) {
259 split->block_len = em->block_len;
260 split->block_start = em->block_start;
Chris Mason445a6942008-11-10 11:53:33 -0500261 split->orig_start = em->orig_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400262 } else {
263 split->block_len = split->len;
264 split->block_start = em->block_start + diff;
Chris Mason445a6942008-11-10 11:53:33 -0500265 split->orig_start = split->start;
Chris Masonc8b97812008-10-29 14:49:59 -0400266 }
Chris Mason3b951512008-04-17 11:29:12 -0400267
268 ret = add_extent_mapping(em_tree, split);
269 BUG_ON(ret);
270 free_extent_map(split);
271 split = NULL;
272 }
Chris Mason890871b2009-09-02 16:24:52 -0400273 write_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500274
Chris Masona52d9a82007-08-27 16:49:44 -0400275 /* once for us */
276 free_extent_map(em);
277 /* once for the tree*/
278 free_extent_map(em);
279 }
Chris Mason3b951512008-04-17 11:29:12 -0400280 if (split)
281 free_extent_map(split);
282 if (split2)
283 free_extent_map(split2);
Chris Masona52d9a82007-08-27 16:49:44 -0400284 return 0;
285}
286
Chris Mason39279cc2007-06-12 06:35:45 -0400287/*
288 * this is very complex, but the basic idea is to drop all extents
289 * in the range start - end. hint_block is filled in with a block number
290 * that would be a good hint to the block allocator for this file.
291 *
292 * If an extent intersects the range but is not entirely inside the range
293 * it is either truncated or split. Anything entirely inside the range
294 * is deleted from the tree.
295 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000296int btrfs_drop_extents(struct btrfs_trans_handle *trans, struct inode *inode,
297 u64 start, u64 end, u64 *hint_byte, int drop_cache)
Chris Mason39279cc2007-06-12 06:35:45 -0400298{
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000299 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason00f5c792007-11-30 10:09:33 -0500300 struct extent_buffer *leaf;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000301 struct btrfs_file_extent_item *fi;
Chris Mason00f5c792007-11-30 10:09:33 -0500302 struct btrfs_path *path;
303 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000304 struct btrfs_key new_key;
305 u64 search_start = start;
306 u64 disk_bytenr = 0;
307 u64 num_bytes = 0;
308 u64 extent_offset = 0;
309 u64 extent_end = 0;
310 int del_nr = 0;
311 int del_slot = 0;
312 int extent_type;
Chris Masonccd467d2007-06-28 15:57:36 -0400313 int recow;
Chris Mason00f5c792007-11-30 10:09:33 -0500314 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -0400315
Chris Masona1ed8352009-09-11 12:27:37 -0400316 if (drop_cache)
317 btrfs_drop_extent_cache(inode, start, end - 1, 0);
Chris Masona52d9a82007-08-27 16:49:44 -0400318
Chris Mason39279cc2007-06-12 06:35:45 -0400319 path = btrfs_alloc_path();
320 if (!path)
321 return -ENOMEM;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000322
Chris Masond3977122009-01-05 21:25:51 -0500323 while (1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400324 recow = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400325 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
326 search_start, -1);
327 if (ret < 0)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000328 break;
329 if (ret > 0 && path->slots[0] > 0 && search_start == start) {
330 leaf = path->nodes[0];
331 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
332 if (key.objectid == inode->i_ino &&
333 key.type == BTRFS_EXTENT_DATA_KEY)
334 path->slots[0]--;
Chris Mason39279cc2007-06-12 06:35:45 -0400335 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400336 ret = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000337next_slot:
338 leaf = path->nodes[0];
339 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
340 BUG_ON(del_nr > 0);
341 ret = btrfs_next_leaf(root, path);
342 if (ret < 0)
343 break;
344 if (ret > 0) {
345 ret = 0;
346 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400347 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000348 leaf = path->nodes[0];
349 recow = 1;
350 }
351
352 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
353 if (key.objectid > inode->i_ino ||
354 key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
355 break;
356
357 fi = btrfs_item_ptr(leaf, path->slots[0],
358 struct btrfs_file_extent_item);
359 extent_type = btrfs_file_extent_type(leaf, fi);
360
361 if (extent_type == BTRFS_FILE_EXTENT_REG ||
362 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
363 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
364 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
365 extent_offset = btrfs_file_extent_offset(leaf, fi);
366 extent_end = key.offset +
367 btrfs_file_extent_num_bytes(leaf, fi);
368 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
369 extent_end = key.offset +
370 btrfs_file_extent_inline_len(leaf, fi);
Chris Mason8c2383c2007-06-18 09:57:58 -0400371 } else {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000372 WARN_ON(1);
Chris Mason8c2383c2007-06-18 09:57:58 -0400373 extent_end = search_start;
Chris Mason39279cc2007-06-12 06:35:45 -0400374 }
375
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000376 if (extent_end <= search_start) {
377 path->slots[0]++;
Chris Mason8c2383c2007-06-18 09:57:58 -0400378 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400379 }
380
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000381 search_start = max(key.offset, start);
382 if (recow) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400383 btrfs_release_path(root, path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000384 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400385 }
Chris Mason771ed682008-11-06 22:02:51 -0500386
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000387 /*
388 * | - range to drop - |
389 * | -------- extent -------- |
390 */
391 if (start > key.offset && end < extent_end) {
392 BUG_ON(del_nr > 0);
393 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
394
395 memcpy(&new_key, &key, sizeof(new_key));
396 new_key.offset = start;
397 ret = btrfs_duplicate_item(trans, root, path,
398 &new_key);
399 if (ret == -EAGAIN) {
400 btrfs_release_path(root, path);
401 continue;
402 }
403 if (ret < 0)
404 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400405
Chris Mason5f39d392007-10-15 16:14:19 -0400406 leaf = path->nodes[0];
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000407 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
408 struct btrfs_file_extent_item);
409 btrfs_set_file_extent_num_bytes(leaf, fi,
410 start - key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400411
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000412 fi = btrfs_item_ptr(leaf, path->slots[0],
413 struct btrfs_file_extent_item);
Chris Masonc8b97812008-10-29 14:49:59 -0400414
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000415 extent_offset += start - key.offset;
416 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
417 btrfs_set_file_extent_num_bytes(leaf, fi,
418 extent_end - start);
419 btrfs_mark_buffer_dirty(leaf);
Chris Masondb945352007-10-15 16:15:53 -0400420
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000421 if (disk_bytenr > 0) {
422 ret = btrfs_inc_extent_ref(trans, root,
423 disk_bytenr, num_bytes, 0,
424 root->root_key.objectid,
425 new_key.objectid,
426 start - extent_offset);
Zheng Yan31840ae2008-09-23 13:14:14 -0400427 BUG_ON(ret);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000428 *hint_byte = disk_bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -0400429 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000430 key.offset = start;
431 }
432 /*
433 * | ---- range to drop ----- |
434 * | -------- extent -------- |
435 */
436 if (start <= key.offset && end < extent_end) {
437 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
438
439 memcpy(&new_key, &key, sizeof(new_key));
440 new_key.offset = end;
441 btrfs_set_item_key_safe(trans, root, path, &new_key);
442
443 extent_offset += end - key.offset;
444 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
445 btrfs_set_file_extent_num_bytes(leaf, fi,
446 extent_end - end);
447 btrfs_mark_buffer_dirty(leaf);
448 if (disk_bytenr > 0) {
449 inode_sub_bytes(inode, end - key.offset);
450 *hint_byte = disk_bytenr;
451 }
452 break;
Zheng Yan31840ae2008-09-23 13:14:14 -0400453 }
454
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000455 search_start = extent_end;
456 /*
457 * | ---- range to drop ----- |
458 * | -------- extent -------- |
459 */
460 if (start > key.offset && end >= extent_end) {
461 BUG_ON(del_nr > 0);
462 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
463
464 btrfs_set_file_extent_num_bytes(leaf, fi,
465 start - key.offset);
466 btrfs_mark_buffer_dirty(leaf);
467 if (disk_bytenr > 0) {
468 inode_sub_bytes(inode, extent_end - start);
469 *hint_byte = disk_bytenr;
470 }
471 if (end == extent_end)
472 break;
473
474 path->slots[0]++;
475 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400476 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000477
478 /*
479 * | ---- range to drop ----- |
480 * | ------ extent ------ |
481 */
482 if (start <= key.offset && end >= extent_end) {
483 if (del_nr == 0) {
484 del_slot = path->slots[0];
485 del_nr = 1;
486 } else {
487 BUG_ON(del_slot + del_nr != path->slots[0]);
488 del_nr++;
489 }
490
491 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
492 inode_sub_bytes(inode,
493 extent_end - key.offset);
494 extent_end = ALIGN(extent_end,
495 root->sectorsize);
496 } else if (disk_bytenr > 0) {
497 ret = btrfs_free_extent(trans, root,
498 disk_bytenr, num_bytes, 0,
499 root->root_key.objectid,
500 key.objectid, key.offset -
501 extent_offset);
502 BUG_ON(ret);
503 inode_sub_bytes(inode,
504 extent_end - key.offset);
505 *hint_byte = disk_bytenr;
506 }
507
508 if (end == extent_end)
509 break;
510
511 if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
512 path->slots[0]++;
513 goto next_slot;
514 }
515
516 ret = btrfs_del_items(trans, root, path, del_slot,
517 del_nr);
518 BUG_ON(ret);
519
520 del_nr = 0;
521 del_slot = 0;
522
523 btrfs_release_path(root, path);
524 continue;
525 }
526
527 BUG_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -0400528 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000529
530 if (del_nr > 0) {
531 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
532 BUG_ON(ret);
533 }
534
Chris Mason39279cc2007-06-12 06:35:45 -0400535 btrfs_free_path(path);
536 return ret;
537}
538
Yan Zhengd899e052008-10-30 14:25:28 -0400539static int extent_mergeable(struct extent_buffer *leaf, int slot,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000540 u64 objectid, u64 bytenr, u64 orig_offset,
541 u64 *start, u64 *end)
Yan Zhengd899e052008-10-30 14:25:28 -0400542{
543 struct btrfs_file_extent_item *fi;
544 struct btrfs_key key;
545 u64 extent_end;
546
547 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
548 return 0;
549
550 btrfs_item_key_to_cpu(leaf, &key, slot);
551 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
552 return 0;
553
554 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
555 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
556 btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000557 btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
Yan Zhengd899e052008-10-30 14:25:28 -0400558 btrfs_file_extent_compression(leaf, fi) ||
559 btrfs_file_extent_encryption(leaf, fi) ||
560 btrfs_file_extent_other_encoding(leaf, fi))
561 return 0;
562
563 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
564 if ((*start && *start != key.offset) || (*end && *end != extent_end))
565 return 0;
566
567 *start = key.offset;
568 *end = extent_end;
569 return 1;
570}
571
572/*
573 * Mark extent in the range start - end as written.
574 *
575 * This changes extent type from 'pre-allocated' to 'regular'. If only
576 * part of extent is marked as written, the extent will be split into
577 * two or three.
578 */
579int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
Yan Zhengd899e052008-10-30 14:25:28 -0400580 struct inode *inode, u64 start, u64 end)
581{
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000582 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zhengd899e052008-10-30 14:25:28 -0400583 struct extent_buffer *leaf;
584 struct btrfs_path *path;
585 struct btrfs_file_extent_item *fi;
586 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000587 struct btrfs_key new_key;
Yan Zhengd899e052008-10-30 14:25:28 -0400588 u64 bytenr;
589 u64 num_bytes;
590 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400591 u64 orig_offset;
Yan Zhengd899e052008-10-30 14:25:28 -0400592 u64 other_start;
593 u64 other_end;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000594 u64 split;
595 int del_nr = 0;
596 int del_slot = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000597 int recow;
Yan Zhengd899e052008-10-30 14:25:28 -0400598 int ret;
599
600 btrfs_drop_extent_cache(inode, start, end - 1, 0);
601
602 path = btrfs_alloc_path();
603 BUG_ON(!path);
604again:
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000605 recow = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000606 split = start;
Yan Zhengd899e052008-10-30 14:25:28 -0400607 key.objectid = inode->i_ino;
608 key.type = BTRFS_EXTENT_DATA_KEY;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000609 key.offset = split;
Yan Zhengd899e052008-10-30 14:25:28 -0400610
611 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
612 if (ret > 0 && path->slots[0] > 0)
613 path->slots[0]--;
614
615 leaf = path->nodes[0];
616 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
617 BUG_ON(key.objectid != inode->i_ino ||
618 key.type != BTRFS_EXTENT_DATA_KEY);
619 fi = btrfs_item_ptr(leaf, path->slots[0],
620 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000621 BUG_ON(btrfs_file_extent_type(leaf, fi) !=
622 BTRFS_FILE_EXTENT_PREALLOC);
Yan Zhengd899e052008-10-30 14:25:28 -0400623 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
624 BUG_ON(key.offset > start || extent_end < end);
625
626 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
627 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400628 orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000629 memcpy(&new_key, &key, sizeof(new_key));
630
631 if (start == key.offset && end < extent_end) {
632 other_start = 0;
633 other_end = start;
634 if (extent_mergeable(leaf, path->slots[0] - 1,
635 inode->i_ino, bytenr, orig_offset,
636 &other_start, &other_end)) {
637 new_key.offset = end;
638 btrfs_set_item_key_safe(trans, root, path, &new_key);
639 fi = btrfs_item_ptr(leaf, path->slots[0],
640 struct btrfs_file_extent_item);
641 btrfs_set_file_extent_num_bytes(leaf, fi,
642 extent_end - end);
643 btrfs_set_file_extent_offset(leaf, fi,
644 end - orig_offset);
645 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
646 struct btrfs_file_extent_item);
647 btrfs_set_file_extent_num_bytes(leaf, fi,
648 end - other_start);
649 btrfs_mark_buffer_dirty(leaf);
650 goto out;
651 }
652 }
653
654 if (start > key.offset && end == extent_end) {
655 other_start = end;
656 other_end = 0;
657 if (extent_mergeable(leaf, path->slots[0] + 1,
658 inode->i_ino, bytenr, orig_offset,
659 &other_start, &other_end)) {
660 fi = btrfs_item_ptr(leaf, path->slots[0],
661 struct btrfs_file_extent_item);
662 btrfs_set_file_extent_num_bytes(leaf, fi,
663 start - key.offset);
664 path->slots[0]++;
665 new_key.offset = start;
666 btrfs_set_item_key_safe(trans, root, path, &new_key);
667
668 fi = btrfs_item_ptr(leaf, path->slots[0],
669 struct btrfs_file_extent_item);
670 btrfs_set_file_extent_num_bytes(leaf, fi,
671 other_end - start);
672 btrfs_set_file_extent_offset(leaf, fi,
673 start - orig_offset);
674 btrfs_mark_buffer_dirty(leaf);
675 goto out;
676 }
677 }
Yan Zhengd899e052008-10-30 14:25:28 -0400678
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000679 while (start > key.offset || end < extent_end) {
680 if (key.offset == start)
681 split = end;
Yan Zhengd899e052008-10-30 14:25:28 -0400682
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000683 new_key.offset = split;
684 ret = btrfs_duplicate_item(trans, root, path, &new_key);
685 if (ret == -EAGAIN) {
686 btrfs_release_path(root, path);
687 goto again;
Yan Zhengd899e052008-10-30 14:25:28 -0400688 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000689 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -0400690
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000691 leaf = path->nodes[0];
692 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
Yan Zhengd899e052008-10-30 14:25:28 -0400693 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -0400694 btrfs_set_file_extent_num_bytes(leaf, fi,
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000695 split - key.offset);
696
697 fi = btrfs_item_ptr(leaf, path->slots[0],
698 struct btrfs_file_extent_item);
699
700 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
701 btrfs_set_file_extent_num_bytes(leaf, fi,
702 extent_end - split);
Yan Zhengd899e052008-10-30 14:25:28 -0400703 btrfs_mark_buffer_dirty(leaf);
704
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000705 ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
706 root->root_key.objectid,
707 inode->i_ino, orig_offset);
Yan Zhengd899e052008-10-30 14:25:28 -0400708 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -0400709
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000710 if (split == start) {
711 key.offset = start;
712 } else {
713 BUG_ON(start != key.offset);
Yan Zhengd899e052008-10-30 14:25:28 -0400714 path->slots[0]--;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000715 extent_end = end;
Yan Zhengd899e052008-10-30 14:25:28 -0400716 }
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000717 recow = 1;
Yan Zhengd899e052008-10-30 14:25:28 -0400718 }
719
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000720 other_start = end;
721 other_end = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000722 if (extent_mergeable(leaf, path->slots[0] + 1,
723 inode->i_ino, bytenr, orig_offset,
724 &other_start, &other_end)) {
725 if (recow) {
726 btrfs_release_path(root, path);
727 goto again;
728 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000729 extent_end = other_end;
730 del_slot = path->slots[0] + 1;
731 del_nr++;
732 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
733 0, root->root_key.objectid,
734 inode->i_ino, orig_offset);
735 BUG_ON(ret);
736 }
737 other_start = 0;
738 other_end = start;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000739 if (extent_mergeable(leaf, path->slots[0] - 1,
740 inode->i_ino, bytenr, orig_offset,
741 &other_start, &other_end)) {
742 if (recow) {
743 btrfs_release_path(root, path);
744 goto again;
745 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000746 key.offset = other_start;
747 del_slot = path->slots[0];
748 del_nr++;
749 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
750 0, root->root_key.objectid,
751 inode->i_ino, orig_offset);
752 BUG_ON(ret);
753 }
754 if (del_nr == 0) {
Shaohua Li3f6fae92010-02-11 07:43:00 +0000755 fi = btrfs_item_ptr(leaf, path->slots[0],
756 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000757 btrfs_set_file_extent_type(leaf, fi,
758 BTRFS_FILE_EXTENT_REG);
759 btrfs_mark_buffer_dirty(leaf);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000760 } else {
Shaohua Li3f6fae92010-02-11 07:43:00 +0000761 fi = btrfs_item_ptr(leaf, del_slot - 1,
762 struct btrfs_file_extent_item);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000763 btrfs_set_file_extent_type(leaf, fi,
764 BTRFS_FILE_EXTENT_REG);
765 btrfs_set_file_extent_num_bytes(leaf, fi,
766 extent_end - key.offset);
767 btrfs_mark_buffer_dirty(leaf);
768
769 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
770 BUG_ON(ret);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000771 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000772out:
Yan Zhengd899e052008-10-30 14:25:28 -0400773 btrfs_free_path(path);
774 return 0;
775}
776
Chris Mason39279cc2007-06-12 06:35:45 -0400777/*
Chris Masonb1bf8622011-02-28 09:52:08 -0500778 * on error we return an unlocked page and the error value
779 * on success we return a locked page and 0
780 */
781static int prepare_uptodate_page(struct page *page, u64 pos)
782{
783 int ret = 0;
784
785 if ((pos & (PAGE_CACHE_SIZE - 1)) && !PageUptodate(page)) {
786 ret = btrfs_readpage(NULL, page);
787 if (ret)
788 return ret;
789 lock_page(page);
790 if (!PageUptodate(page)) {
791 unlock_page(page);
792 return -EIO;
793 }
794 }
795 return 0;
796}
797
798/*
Chris Masond352ac62008-09-29 15:18:18 -0400799 * this gets pages into the page cache and locks them down, it also properly
800 * waits for data=ordered extents to finish before allowing the pages to be
801 * modified.
Chris Mason39279cc2007-06-12 06:35:45 -0400802 */
Chris Masond3977122009-01-05 21:25:51 -0500803static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
Chris Mason98ed5172008-01-03 10:01:48 -0500804 struct page **pages, size_t num_pages,
805 loff_t pos, unsigned long first_index,
806 unsigned long last_index, size_t write_bytes)
Chris Mason39279cc2007-06-12 06:35:45 -0400807{
Josef Bacik2ac55d42010-02-03 19:33:23 +0000808 struct extent_state *cached_state = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -0400809 int i;
810 unsigned long index = pos >> PAGE_CACHE_SHIFT;
Chris Mason6da6aba2007-12-18 16:15:09 -0500811 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400812 int err = 0;
Chris Masonb1bf8622011-02-28 09:52:08 -0500813 int faili = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -0400814 u64 start_pos;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400815 u64 last_pos;
Chris Mason8c2383c2007-06-18 09:57:58 -0400816
Chris Mason5f39d392007-10-15 16:14:19 -0400817 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400818 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -0400819
Yan Zheng9036c102008-10-30 14:19:41 -0400820 if (start_pos > inode->i_size) {
821 err = btrfs_cont_expand(inode, start_pos);
822 if (err)
823 return err;
824 }
825
Chris Mason39279cc2007-06-12 06:35:45 -0400826 memset(pages, 0, num_pages * sizeof(struct page *));
Chris Masone6dcd2d2008-07-17 12:53:50 -0400827again:
Chris Mason39279cc2007-06-12 06:35:45 -0400828 for (i = 0; i < num_pages; i++) {
829 pages[i] = grab_cache_page(inode->i_mapping, index + i);
830 if (!pages[i]) {
Chris Masonb1bf8622011-02-28 09:52:08 -0500831 faili = i - 1;
832 err = -ENOMEM;
833 goto fail;
834 }
835
836 if (i == 0)
837 err = prepare_uptodate_page(pages[i], pos);
838 if (i == num_pages - 1)
839 err = prepare_uptodate_page(pages[i],
840 pos + write_bytes);
841 if (err) {
842 page_cache_release(pages[i]);
843 faili = i - 1;
844 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -0400845 }
Chris Masonccd467d2007-06-28 15:57:36 -0400846 wait_on_page_writeback(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -0400847 }
Chris Masonb1bf8622011-02-28 09:52:08 -0500848 err = 0;
Chris Mason07627042008-02-19 11:29:24 -0500849 if (start_pos < inode->i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400850 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000851 lock_extent_bits(&BTRFS_I(inode)->io_tree,
852 start_pos, last_pos - 1, 0, &cached_state,
853 GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500854 ordered = btrfs_lookup_first_ordered_extent(inode,
855 last_pos - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400856 if (ordered &&
857 ordered->file_offset + ordered->len > start_pos &&
858 ordered->file_offset < last_pos) {
859 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +0000860 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
861 start_pos, last_pos - 1,
862 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400863 for (i = 0; i < num_pages; i++) {
864 unlock_page(pages[i]);
865 page_cache_release(pages[i]);
866 }
867 btrfs_wait_ordered_range(inode, start_pos,
868 last_pos - start_pos);
869 goto again;
870 }
871 if (ordered)
872 btrfs_put_ordered_extent(ordered);
873
Josef Bacik2ac55d42010-02-03 19:33:23 +0000874 clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
Josef Bacik32c00af2009-10-08 13:34:05 -0400875 last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +0000876 EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
Chris Mason07627042008-02-19 11:29:24 -0500877 GFP_NOFS);
Josef Bacik2ac55d42010-02-03 19:33:23 +0000878 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
879 start_pos, last_pos - 1, &cached_state,
880 GFP_NOFS);
Chris Mason07627042008-02-19 11:29:24 -0500881 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400882 for (i = 0; i < num_pages; i++) {
Chris Masonf87f0572008-08-01 11:27:23 -0400883 clear_page_dirty_for_io(pages[i]);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400884 set_page_extent_mapped(pages[i]);
885 WARN_ON(!PageLocked(pages[i]));
886 }
Chris Mason39279cc2007-06-12 06:35:45 -0400887 return 0;
Chris Masonb1bf8622011-02-28 09:52:08 -0500888fail:
889 while (faili >= 0) {
890 unlock_page(pages[faili]);
891 page_cache_release(pages[faili]);
892 faili--;
893 }
894 return err;
895
Chris Mason39279cc2007-06-12 06:35:45 -0400896}
897
Josef Bacik11c65dc2010-05-23 11:07:21 -0400898static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
899 const struct iovec *iov,
900 unsigned long nr_segs, loff_t pos)
Josef Bacik4b46fce2010-05-23 11:00:55 -0400901{
Josef Bacik11c65dc2010-05-23 11:07:21 -0400902 struct file *file = iocb->ki_filp;
903 struct inode *inode = fdentry(file)->d_inode;
904 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400905 struct page **pages = NULL;
906 struct iov_iter i;
907 loff_t *ppos = &iocb->ki_pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400908 loff_t start_pos;
909 ssize_t num_written = 0;
910 ssize_t err = 0;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400911 size_t count;
912 size_t ocount;
Chris Mason39279cc2007-06-12 06:35:45 -0400913 int ret = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -0400914 int nrptrs;
Chris Mason39279cc2007-06-12 06:35:45 -0400915 unsigned long first_index;
916 unsigned long last_index;
Chris Masoncb843a62008-10-03 12:30:02 -0400917 int will_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400918 int buffered = 0;
Xin Zhong914ee292010-12-09 09:30:14 +0000919 int copied = 0;
920 int dirty_pages = 0;
Chris Masoncb843a62008-10-03 12:30:02 -0400921
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +0100922 will_write = ((file->f_flags & O_DSYNC) || IS_SYNC(inode) ||
Chris Masoncb843a62008-10-03 12:30:02 -0400923 (file->f_flags & O_DIRECT));
Chris Mason8c2383c2007-06-18 09:57:58 -0400924
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400925 start_pos = pos;
926
Chris Mason39279cc2007-06-12 06:35:45 -0400927 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
Chris Mason2b1f55b2008-09-24 11:48:04 -0400928
Chris Masonab93dbe2009-10-01 12:29:10 -0400929 mutex_lock(&inode->i_mutex);
930
Josef Bacik11c65dc2010-05-23 11:07:21 -0400931 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
932 if (err)
933 goto out;
934 count = ocount;
935
Chris Mason1832a6d2007-12-21 16:27:21 -0500936 current->backing_dev_info = inode->i_mapping->backing_dev_info;
Chris Mason39279cc2007-06-12 06:35:45 -0400937 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
Chris Mason1832a6d2007-12-21 16:27:21 -0500938 if (err)
Chris Masonab93dbe2009-10-01 12:29:10 -0400939 goto out;
940
Jeff Mahoney12fa8ec2008-05-02 15:03:58 -0400941 if (count == 0)
Chris Masonab93dbe2009-10-01 12:29:10 -0400942 goto out;
Sven Wegener0ee0fda2008-07-30 16:54:26 -0400943
944 err = file_remove_suid(file);
Chris Mason39279cc2007-06-12 06:35:45 -0400945 if (err)
Chris Masonab93dbe2009-10-01 12:29:10 -0400946 goto out;
947
liuboacce9522011-01-06 19:30:25 +0800948 /*
949 * If BTRFS flips readonly due to some impossible error
950 * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR),
951 * although we have opened a file as writable, we have
952 * to stop this write operation to ensure FS consistency.
953 */
954 if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
955 err = -EROFS;
956 goto out;
957 }
958
Chris Mason39279cc2007-06-12 06:35:45 -0400959 file_update_time(file);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400960 BTRFS_I(inode)->sequence++;
Chris Mason39279cc2007-06-12 06:35:45 -0400961
Josef Bacik4b46fce2010-05-23 11:00:55 -0400962 if (unlikely(file->f_flags & O_DIRECT)) {
Josef Bacik11c65dc2010-05-23 11:07:21 -0400963 num_written = generic_file_direct_write(iocb, iov, &nr_segs,
964 pos, ppos, count,
965 ocount);
Josef Bacik11c65dc2010-05-23 11:07:21 -0400966 /*
967 * the generic O_DIRECT will update in-memory i_size after the
968 * DIOs are done. But our endio handlers that update the on
969 * disk i_size never update past the in memory i_size. So we
970 * need one more update here to catch any additions to the
971 * file
972 */
973 if (inode->i_size != BTRFS_I(inode)->disk_i_size) {
974 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
975 mark_inode_dirty(inode);
976 }
977
978 if (num_written < 0) {
Josef Bacik11c65dc2010-05-23 11:07:21 -0400979 ret = num_written;
980 num_written = 0;
981 goto out;
982 } else if (num_written == count) {
983 /* pick up pos changes done by the generic code */
984 pos = *ppos;
985 goto out;
986 }
Josef Bacik4b46fce2010-05-23 11:00:55 -0400987 /*
988 * We are going to do buffered for the rest of the range, so we
989 * need to make sure to invalidate the buffered pages when we're
990 * done.
991 */
992 buffered = 1;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400993 pos += num_written;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400994 }
995
Josef Bacik11c65dc2010-05-23 11:07:21 -0400996 iov_iter_init(&i, iov, nr_segs, count, num_written);
997 nrptrs = min((iov_iter_count(&i) + PAGE_CACHE_SIZE - 1) /
998 PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
999 (sizeof(struct page *)));
Chris Mason8c2383c2007-06-18 09:57:58 -04001000 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
liubo2a29edc2011-01-26 06:22:08 +00001001 if (!pages) {
1002 ret = -ENOMEM;
1003 goto out;
1004 }
Chris Mason39279cc2007-06-12 06:35:45 -04001005
Chris Masonab93dbe2009-10-01 12:29:10 -04001006 /* generic_write_checks can change our pos */
1007 start_pos = pos;
1008
Chris Mason39279cc2007-06-12 06:35:45 -04001009 first_index = pos >> PAGE_CACHE_SHIFT;
Josef Bacik11c65dc2010-05-23 11:07:21 -04001010 last_index = (pos + iov_iter_count(&i)) >> PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -04001011
Josef Bacik11c65dc2010-05-23 11:07:21 -04001012 while (iov_iter_count(&i) > 0) {
Chris Mason39279cc2007-06-12 06:35:45 -04001013 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
Josef Bacik11c65dc2010-05-23 11:07:21 -04001014 size_t write_bytes = min(iov_iter_count(&i),
1015 nrptrs * (size_t)PAGE_CACHE_SIZE -
Chris Mason8c2383c2007-06-18 09:57:58 -04001016 offset);
Yan, Zheng3a909832011-01-18 13:34:40 +08001017 size_t num_pages = (write_bytes + offset +
1018 PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -04001019
Chris Mason8c2383c2007-06-18 09:57:58 -04001020 WARN_ON(num_pages > nrptrs);
yanhai zhu9aead432009-01-05 15:49:11 -05001021 memset(pages, 0, sizeof(struct page *) * nrptrs);
Chris Mason1832a6d2007-12-21 16:27:21 -05001022
Xin Zhong914ee292010-12-09 09:30:14 +00001023 /*
1024 * Fault pages before locking them in prepare_pages
1025 * to avoid recursive lock
1026 */
1027 if (unlikely(iov_iter_fault_in_readable(&i, write_bytes))) {
1028 ret = -EFAULT;
1029 goto out;
1030 }
1031
1032 ret = btrfs_delalloc_reserve_space(inode,
1033 num_pages << PAGE_CACHE_SHIFT);
Chris Mason1832a6d2007-12-21 16:27:21 -05001034 if (ret)
1035 goto out;
1036
Chris Mason39279cc2007-06-12 06:35:45 -04001037 ret = prepare_pages(root, file, pages, num_pages,
1038 pos, first_index, last_index,
Chris Mason8c2383c2007-06-18 09:57:58 -04001039 write_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05001040 if (ret) {
Xin Zhong914ee292010-12-09 09:30:14 +00001041 btrfs_delalloc_release_space(inode,
1042 num_pages << PAGE_CACHE_SHIFT);
Chris Mason54aa1f42007-06-22 14:16:25 -04001043 goto out;
Josef Bacik6a632092009-02-20 11:00:09 -05001044 }
Chris Mason39279cc2007-06-12 06:35:45 -04001045
Xin Zhong914ee292010-12-09 09:30:14 +00001046 copied = btrfs_copy_from_user(pos, num_pages,
Josef Bacik11c65dc2010-05-23 11:07:21 -04001047 write_bytes, pages, &i);
Chris Masonb1bf8622011-02-28 09:52:08 -05001048
1049 /*
1050 * if we have trouble faulting in the pages, fall
1051 * back to one page at a time
1052 */
1053 if (copied < write_bytes)
1054 nrptrs = 1;
1055
1056 if (copied == 0)
1057 dirty_pages = 0;
1058 else
1059 dirty_pages = (copied + offset +
1060 PAGE_CACHE_SIZE - 1) >>
1061 PAGE_CACHE_SHIFT;
Xin Zhong914ee292010-12-09 09:30:14 +00001062
1063 if (num_pages > dirty_pages) {
1064 if (copied > 0)
1065 atomic_inc(
1066 &BTRFS_I(inode)->outstanding_extents);
1067 btrfs_delalloc_release_space(inode,
1068 (num_pages - dirty_pages) <<
1069 PAGE_CACHE_SHIFT);
1070 }
1071
1072 if (copied > 0) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001073 dirty_and_release_pages(NULL, root, file, pages,
Xin Zhong914ee292010-12-09 09:30:14 +00001074 dirty_pages, pos, copied);
Chris Mason54aa1f42007-06-22 14:16:25 -04001075 }
Chris Mason39279cc2007-06-12 06:35:45 -04001076
Chris Mason39279cc2007-06-12 06:35:45 -04001077 btrfs_drop_pages(pages, num_pages);
Xin Zhong914ee292010-12-09 09:30:14 +00001078
1079 if (copied > 0) {
1080 if (will_write) {
1081 filemap_fdatawrite_range(inode->i_mapping, pos,
1082 pos + copied - 1);
1083 } else {
1084 balance_dirty_pages_ratelimited_nr(
1085 inode->i_mapping,
1086 dirty_pages);
1087 if (dirty_pages <
1088 (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
1089 btrfs_btree_balance_dirty(root, 1);
1090 btrfs_throttle(root);
1091 }
Josef Bacik6a632092009-02-20 11:00:09 -05001092 }
Chris Mason39279cc2007-06-12 06:35:45 -04001093
Xin Zhong914ee292010-12-09 09:30:14 +00001094 pos += copied;
1095 num_written += copied;
Chris Mason39279cc2007-06-12 06:35:45 -04001096
Chris Mason39279cc2007-06-12 06:35:45 -04001097 cond_resched();
1098 }
Chris Mason39279cc2007-06-12 06:35:45 -04001099out:
Chris Mason1832a6d2007-12-21 16:27:21 -05001100 mutex_unlock(&inode->i_mutex);
Josef Bacik6a632092009-02-20 11:00:09 -05001101 if (ret)
1102 err = ret;
Chris Mason5b92ee72008-01-03 13:46:11 -05001103
Chris Mason8c2383c2007-06-18 09:57:58 -04001104 kfree(pages);
Chris Mason39279cc2007-06-12 06:35:45 -04001105 *ppos = pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001106
Chris Mason5a3f23d2009-03-31 13:27:11 -04001107 /*
1108 * we want to make sure fsync finds this change
1109 * but we haven't joined a transaction running right now.
1110 *
1111 * Later on, someone is sure to update the inode and get the
1112 * real transid recorded.
1113 *
1114 * We set last_trans now to the fs_info generation + 1,
1115 * this will either be one more than the running transaction
1116 * or the generation used for the next transaction if there isn't
1117 * one running right now.
1118 */
1119 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
1120
Chris Masoncb843a62008-10-03 12:30:02 -04001121 if (num_written > 0 && will_write) {
Chris Masone02119d2008-09-05 16:13:11 -04001122 struct btrfs_trans_handle *trans;
1123
Chris Masoncb843a62008-10-03 12:30:02 -04001124 err = btrfs_wait_ordered_range(inode, start_pos, num_written);
1125 if (err)
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001126 num_written = err;
Chris Masone02119d2008-09-05 16:13:11 -04001127
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +01001128 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001129 trans = btrfs_start_transaction(root, 0);
Josef Bacik495e8672010-11-19 20:36:10 +00001130 if (IS_ERR(trans)) {
1131 num_written = PTR_ERR(trans);
1132 goto done;
1133 }
1134 mutex_lock(&inode->i_mutex);
Chris Masoncb843a62008-10-03 12:30:02 -04001135 ret = btrfs_log_dentry_safe(trans, root,
1136 file->f_dentry);
Josef Bacik495e8672010-11-19 20:36:10 +00001137 mutex_unlock(&inode->i_mutex);
Chris Masoncb843a62008-10-03 12:30:02 -04001138 if (ret == 0) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001139 ret = btrfs_sync_log(trans, root);
1140 if (ret == 0)
1141 btrfs_end_transaction(trans, root);
1142 else
1143 btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001144 } else if (ret != BTRFS_NO_LOG_SYNC) {
Chris Masoncb843a62008-10-03 12:30:02 -04001145 btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001146 } else {
1147 btrfs_end_transaction(trans, root);
Chris Masoncb843a62008-10-03 12:30:02 -04001148 }
Chris Masone02119d2008-09-05 16:13:11 -04001149 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04001150 if (file->f_flags & O_DIRECT && buffered) {
Chris Masoncb843a62008-10-03 12:30:02 -04001151 invalidate_mapping_pages(inode->i_mapping,
1152 start_pos >> PAGE_CACHE_SHIFT,
1153 (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
1154 }
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001155 }
Josef Bacik495e8672010-11-19 20:36:10 +00001156done:
Chris Mason39279cc2007-06-12 06:35:45 -04001157 current->backing_dev_info = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001158 return num_written ? num_written : err;
1159}
1160
Chris Masond3977122009-01-05 21:25:51 -05001161int btrfs_release_file(struct inode *inode, struct file *filp)
Mingminge1b81e62008-05-27 10:55:43 -04001162{
Chris Mason5a3f23d2009-03-31 13:27:11 -04001163 /*
1164 * ordered_data_close is set by settattr when we are about to truncate
1165 * a file from a non-zero size to a zero size. This tries to
1166 * flush down new bytes that may have been written if the
1167 * application were using truncate to replace a file in place.
1168 */
1169 if (BTRFS_I(inode)->ordered_data_close) {
1170 BTRFS_I(inode)->ordered_data_close = 0;
1171 btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
1172 if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
1173 filemap_flush(inode->i_mapping);
1174 }
Sage Weil6bf13c02008-06-10 10:07:39 -04001175 if (filp->private_data)
1176 btrfs_ioctl_trans_end(filp);
Mingminge1b81e62008-05-27 10:55:43 -04001177 return 0;
1178}
1179
Chris Masond352ac62008-09-29 15:18:18 -04001180/*
1181 * fsync call for both files and directories. This logs the inode into
1182 * the tree log instead of forcing full commits whenever possible.
1183 *
1184 * It needs to call filemap_fdatawait so that all ordered extent updates are
1185 * in the metadata btree are up to date for copying to the log.
1186 *
1187 * It drops the inode mutex before doing the tree log commit. This is an
1188 * important optimization for directories because holding the mutex prevents
1189 * new operations on the dir while we write to disk.
1190 */
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001191int btrfs_sync_file(struct file *file, int datasync)
Chris Mason39279cc2007-06-12 06:35:45 -04001192{
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001193 struct dentry *dentry = file->f_path.dentry;
Chris Mason39279cc2007-06-12 06:35:45 -04001194 struct inode *inode = dentry->d_inode;
1195 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001196 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001197 struct btrfs_trans_handle *trans;
1198
Chris Mason257c62e2009-10-13 13:21:08 -04001199
1200 /* we wait first, since the writeback may change the inode */
1201 root->log_batch++;
1202 /* the VFS called filemap_fdatawrite for us */
1203 btrfs_wait_ordered_range(inode, 0, (u64)-1);
1204 root->log_batch++;
1205
Chris Mason39279cc2007-06-12 06:35:45 -04001206 /*
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001207 * check the transaction that last modified this inode
1208 * and see if its already been committed
Chris Mason39279cc2007-06-12 06:35:45 -04001209 */
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001210 if (!BTRFS_I(inode)->last_trans)
1211 goto out;
Chris Masona2135012008-06-25 16:01:30 -04001212
Chris Mason257c62e2009-10-13 13:21:08 -04001213 /*
1214 * if the last transaction that changed this file was before
1215 * the current transaction, we can bail out now without any
1216 * syncing
1217 */
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001218 mutex_lock(&root->fs_info->trans_mutex);
1219 if (BTRFS_I(inode)->last_trans <=
1220 root->fs_info->last_trans_committed) {
1221 BTRFS_I(inode)->last_trans = 0;
1222 mutex_unlock(&root->fs_info->trans_mutex);
1223 goto out;
1224 }
1225 mutex_unlock(&root->fs_info->trans_mutex);
1226
1227 /*
Chris Masona52d9a82007-08-27 16:49:44 -04001228 * ok we haven't committed the transaction yet, lets do a commit
1229 */
Dan Carpenter6f902af2010-05-29 09:49:07 +00001230 if (file->private_data)
Sage Weil6bf13c02008-06-10 10:07:39 -04001231 btrfs_ioctl_trans_end(file);
1232
Yan, Zhenga22285a2010-05-16 10:48:46 -04001233 trans = btrfs_start_transaction(root, 0);
1234 if (IS_ERR(trans)) {
1235 ret = PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04001236 goto out;
1237 }
Chris Masone02119d2008-09-05 16:13:11 -04001238
Chris Mason2cfbd502009-02-20 10:55:10 -05001239 ret = btrfs_log_dentry_safe(trans, root, dentry);
Chris Masond3977122009-01-05 21:25:51 -05001240 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04001241 goto out;
Chris Mason49eb7e42008-09-11 15:53:12 -04001242
1243 /* we've logged all the items and now have a consistent
1244 * version of the file in the log. It is possible that
1245 * someone will come in and modify the file, but that's
1246 * fine because the log is consistent on disk, and we
1247 * have references to all of the file's extents
1248 *
1249 * It is possible that someone will come in and log the
1250 * file again, but that will end up using the synchronization
1251 * inside btrfs_sync_log to keep things safe.
1252 */
Chris Mason2cfbd502009-02-20 10:55:10 -05001253 mutex_unlock(&dentry->d_inode->i_mutex);
Chris Mason49eb7e42008-09-11 15:53:12 -04001254
Chris Mason257c62e2009-10-13 13:21:08 -04001255 if (ret != BTRFS_NO_LOG_SYNC) {
1256 if (ret > 0) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001257 ret = btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001258 } else {
1259 ret = btrfs_sync_log(trans, root);
1260 if (ret == 0)
1261 ret = btrfs_end_transaction(trans, root);
1262 else
1263 ret = btrfs_commit_transaction(trans, root);
1264 }
1265 } else {
1266 ret = btrfs_end_transaction(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001267 }
Chris Mason2cfbd502009-02-20 10:55:10 -05001268 mutex_lock(&dentry->d_inode->i_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001269out:
Roel Kluin014e4ac2010-01-29 10:42:11 +00001270 return ret > 0 ? -EIO : ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001271}
1272
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001273static const struct vm_operations_struct btrfs_file_vm_ops = {
Chris Mason92fee662007-07-25 12:31:35 -04001274 .fault = filemap_fault,
Chris Mason9ebefb182007-06-15 13:50:00 -04001275 .page_mkwrite = btrfs_page_mkwrite,
1276};
1277
1278static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
1279{
Miao Xie058a4572010-05-20 07:21:50 +00001280 struct address_space *mapping = filp->f_mapping;
1281
1282 if (!mapping->a_ops->readpage)
1283 return -ENOEXEC;
1284
Chris Mason9ebefb182007-06-15 13:50:00 -04001285 file_accessed(filp);
Miao Xie058a4572010-05-20 07:21:50 +00001286 vma->vm_ops = &btrfs_file_vm_ops;
1287 vma->vm_flags |= VM_CAN_NONLINEAR;
1288
Chris Mason9ebefb182007-06-15 13:50:00 -04001289 return 0;
1290}
1291
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001292const struct file_operations btrfs_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04001293 .llseek = generic_file_llseek,
1294 .read = do_sync_read,
Miao Xie4a0010712010-06-07 03:38:51 +00001295 .write = do_sync_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04001296 .aio_read = generic_file_aio_read,
Chris Masone9906a92007-12-14 12:56:58 -05001297 .splice_read = generic_file_splice_read,
Josef Bacik11c65dc2010-05-23 11:07:21 -04001298 .aio_write = btrfs_file_aio_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04001299 .mmap = btrfs_file_mmap,
Chris Mason39279cc2007-06-12 06:35:45 -04001300 .open = generic_file_open,
Mingminge1b81e62008-05-27 10:55:43 -04001301 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04001302 .fsync = btrfs_sync_file,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001303 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001304#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001305 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001306#endif
1307};