blob: 40fee137dd11cf4d927ee2b4b036aa3b0899aeab [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 Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040029#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040035#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050036#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040037#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040038#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050040#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040041#include "ctree.h"
42#include "disk-io.h"
43#include "transaction.h"
44#include "btrfs_inode.h"
45#include "ioctl.h"
46#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040047#include "volumes.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040048#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040049#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040050#include "tree-log.h"
Chris Masonc8b97812008-10-29 14:49:59 -040051#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050052#include "locking.h"
Chris Mason39279cc2007-06-12 06:35:45 -040053
54struct btrfs_iget_args {
55 u64 ino;
56 struct btrfs_root *root;
57};
58
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070059static const struct inode_operations btrfs_dir_inode_operations;
60static const struct inode_operations btrfs_symlink_inode_operations;
61static const struct inode_operations btrfs_dir_ro_inode_operations;
62static const struct inode_operations btrfs_special_inode_operations;
63static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070064static const struct address_space_operations btrfs_aops;
65static const struct address_space_operations btrfs_symlink_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070066static const struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050067static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040068
69static struct kmem_cache *btrfs_inode_cachep;
70struct kmem_cache *btrfs_trans_handle_cachep;
71struct kmem_cache *btrfs_transaction_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040072struct kmem_cache *btrfs_path_cachep;
73
74#define S_SHIFT 12
75static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
76 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
77 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
78 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
79 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
80 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
81 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
82 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
83};
84
Josef Bacik7b128762008-07-24 12:17:14 -040085static void btrfs_truncate(struct inode *inode);
Chris Masonc8b97812008-10-29 14:49:59 -040086static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
Chris Mason771ed682008-11-06 22:02:51 -050087static noinline int cow_file_range(struct inode *inode,
88 struct page *locked_page,
89 u64 start, u64 end, int *page_started,
90 unsigned long *nr_written, int unlock);
Josef Bacik7b128762008-07-24 12:17:14 -040091
Yan, Zhengf34f57a2009-11-12 09:35:27 +000092static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
93 struct inode *inode, struct inode *dir)
Jim Owens0279b4c2009-02-04 09:29:13 -050094{
95 int err;
96
Yan, Zhengf34f57a2009-11-12 09:35:27 +000097 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -050098 if (!err)
Yan, Zhengf34f57a2009-11-12 09:35:27 +000099 err = btrfs_xattr_security_init(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500100 return err;
101}
102
Chris Masond352ac62008-09-29 15:18:18 -0400103/*
Chris Masonc8b97812008-10-29 14:49:59 -0400104 * this does all the hard work for inserting an inline extent into
105 * the btree. The caller should have done a btrfs_drop_extents so that
106 * no overlapping inline items exist in the btree
107 */
Chris Masond3977122009-01-05 21:25:51 -0500108static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400109 struct btrfs_root *root, struct inode *inode,
110 u64 start, size_t size, size_t compressed_size,
111 struct page **compressed_pages)
112{
113 struct btrfs_key key;
114 struct btrfs_path *path;
115 struct extent_buffer *leaf;
116 struct page *page = NULL;
117 char *kaddr;
118 unsigned long ptr;
119 struct btrfs_file_extent_item *ei;
120 int err = 0;
121 int ret;
122 size_t cur_size = size;
123 size_t datasize;
124 unsigned long offset;
Li Zefan261507a02010-12-17 14:21:50 +0800125 int compress_type = BTRFS_COMPRESS_NONE;
Chris Masonc8b97812008-10-29 14:49:59 -0400126
127 if (compressed_size && compressed_pages) {
Li Zefan261507a02010-12-17 14:21:50 +0800128 compress_type = root->fs_info->compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -0400129 cur_size = compressed_size;
130 }
131
Chris Masond3977122009-01-05 21:25:51 -0500132 path = btrfs_alloc_path();
133 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400134 return -ENOMEM;
135
Chris Masonb9473432009-03-13 11:00:37 -0400136 path->leave_spinning = 1;
Chris Masonc8b97812008-10-29 14:49:59 -0400137 btrfs_set_trans_block_group(trans, inode);
138
139 key.objectid = inode->i_ino;
140 key.offset = start;
141 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400142 datasize = btrfs_file_extent_calc_inline_size(cur_size);
143
144 inode_add_bytes(inode, size);
145 ret = btrfs_insert_empty_item(trans, root, path, &key,
146 datasize);
147 BUG_ON(ret);
148 if (ret) {
149 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400150 goto fail;
151 }
152 leaf = path->nodes[0];
153 ei = btrfs_item_ptr(leaf, path->slots[0],
154 struct btrfs_file_extent_item);
155 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
156 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
157 btrfs_set_file_extent_encryption(leaf, ei, 0);
158 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
159 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
160 ptr = btrfs_file_extent_inline_start(ei);
161
Li Zefan261507a02010-12-17 14:21:50 +0800162 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400163 struct page *cpage;
164 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500165 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400166 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500167 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400168 PAGE_CACHE_SIZE);
169
Chris Masonb9473432009-03-13 11:00:37 -0400170 kaddr = kmap_atomic(cpage, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400171 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Chris Masonb9473432009-03-13 11:00:37 -0400172 kunmap_atomic(kaddr, KM_USER0);
Chris Masonc8b97812008-10-29 14:49:59 -0400173
174 i++;
175 ptr += cur_size;
176 compressed_size -= cur_size;
177 }
178 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800179 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400180 } else {
181 page = find_get_page(inode->i_mapping,
182 start >> PAGE_CACHE_SHIFT);
183 btrfs_set_file_extent_compression(leaf, ei, 0);
184 kaddr = kmap_atomic(page, KM_USER0);
185 offset = start & (PAGE_CACHE_SIZE - 1);
186 write_extent_buffer(leaf, kaddr + offset, ptr, size);
187 kunmap_atomic(kaddr, KM_USER0);
188 page_cache_release(page);
189 }
190 btrfs_mark_buffer_dirty(leaf);
191 btrfs_free_path(path);
192
Yan, Zhengc2167752009-11-12 09:34:21 +0000193 /*
194 * we're an inline extent, so nobody can
195 * extend the file past i_size without locking
196 * a page we already have locked.
197 *
198 * We must do any isize and inode updates
199 * before we unlock the pages. Otherwise we
200 * could end up racing with unlink.
201 */
Chris Masonc8b97812008-10-29 14:49:59 -0400202 BTRFS_I(inode)->disk_i_size = inode->i_size;
203 btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000204
Chris Masonc8b97812008-10-29 14:49:59 -0400205 return 0;
206fail:
207 btrfs_free_path(path);
208 return err;
209}
210
211
212/*
213 * conditionally insert an inline extent into the file. This
214 * does the checks required to make sure the data is small enough
215 * to fit as an inline extent.
216 */
Chris Mason7f366cf2009-03-12 20:12:45 -0400217static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400218 struct btrfs_root *root,
219 struct inode *inode, u64 start, u64 end,
220 size_t compressed_size,
221 struct page **compressed_pages)
222{
223 u64 isize = i_size_read(inode);
224 u64 actual_end = min(end + 1, isize);
225 u64 inline_len = actual_end - start;
226 u64 aligned_end = (end + root->sectorsize - 1) &
227 ~((u64)root->sectorsize - 1);
228 u64 hint_byte;
229 u64 data_len = inline_len;
230 int ret;
231
232 if (compressed_size)
233 data_len = compressed_size;
234
235 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400236 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400237 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
238 (!compressed_size &&
239 (actual_end & (root->sectorsize - 1)) == 0) ||
240 end + 1 < isize ||
241 data_len > root->fs_info->max_inline) {
242 return 1;
243 }
244
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000245 ret = btrfs_drop_extents(trans, inode, start, aligned_end,
Chris Masona1ed8352009-09-11 12:27:37 -0400246 &hint_byte, 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400247 BUG_ON(ret);
248
249 if (isize > actual_end)
250 inline_len = min_t(u64, isize, actual_end);
251 ret = insert_inline_extent(trans, root, inode, start,
252 inline_len, compressed_size,
253 compressed_pages);
254 BUG_ON(ret);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400255 btrfs_delalloc_release_metadata(inode, end + 1 - start);
Chris Masona1ed8352009-09-11 12:27:37 -0400256 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400257 return 0;
258}
259
Chris Mason771ed682008-11-06 22:02:51 -0500260struct async_extent {
261 u64 start;
262 u64 ram_size;
263 u64 compressed_size;
264 struct page **pages;
265 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800266 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500267 struct list_head list;
268};
269
270struct async_cow {
271 struct inode *inode;
272 struct btrfs_root *root;
273 struct page *locked_page;
274 u64 start;
275 u64 end;
276 struct list_head extents;
277 struct btrfs_work work;
278};
279
280static noinline int add_async_extent(struct async_cow *cow,
281 u64 start, u64 ram_size,
282 u64 compressed_size,
283 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800284 unsigned long nr_pages,
285 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500286{
287 struct async_extent *async_extent;
288
289 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
290 async_extent->start = start;
291 async_extent->ram_size = ram_size;
292 async_extent->compressed_size = compressed_size;
293 async_extent->pages = pages;
294 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800295 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500296 list_add_tail(&async_extent->list, &cow->extents);
297 return 0;
298}
299
Chris Masonc8b97812008-10-29 14:49:59 -0400300/*
Chris Mason771ed682008-11-06 22:02:51 -0500301 * we create compressed extents in two phases. The first
302 * phase compresses a range of pages that have already been
303 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400304 *
Chris Mason771ed682008-11-06 22:02:51 -0500305 * This is done inside an ordered work queue, and the compression
306 * is spread across many cpus. The actual IO submission is step
307 * two, and the ordered work queue takes care of making sure that
308 * happens in the same order things were put onto the queue by
309 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400310 *
Chris Mason771ed682008-11-06 22:02:51 -0500311 * If this code finds it can't get good compression, it puts an
312 * entry onto the work queue to write the uncompressed bytes. This
313 * makes sure that both compressed inodes and uncompressed inodes
314 * are written in the same order that pdflush sent them down.
Chris Masond352ac62008-09-29 15:18:18 -0400315 */
Chris Mason771ed682008-11-06 22:02:51 -0500316static noinline int compress_file_range(struct inode *inode,
317 struct page *locked_page,
318 u64 start, u64 end,
319 struct async_cow *async_cow,
320 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400321{
322 struct btrfs_root *root = BTRFS_I(inode)->root;
323 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400324 u64 num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400325 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400326 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500327 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400328 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400329 struct page **pages = NULL;
330 unsigned long nr_pages;
331 unsigned long nr_pages_ret = 0;
332 unsigned long total_compressed = 0;
333 unsigned long total_in = 0;
334 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500335 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400336 int i;
337 int will_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800338 int compress_type = root->fs_info->compress_type;
Chris Masonb888db22007-08-27 16:49:44 -0400339
Chris Mason42dc7ba2008-12-15 11:44:56 -0500340 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400341again:
342 will_compress = 0;
343 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
344 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
345
Chris Masonf03d9301f2009-02-04 09:31:06 -0500346 /*
347 * we don't want to send crud past the end of i_size through
348 * compression, that's just a waste of CPU time. So, if the
349 * end of the file is before the start of our current
350 * requested range of bytes, we bail out to the uncompressed
351 * cleanup code that can deal with all of this.
352 *
353 * It isn't really the fastest way to fix things, but this is a
354 * very uncommon corner.
355 */
356 if (actual_end <= start)
357 goto cleanup_and_bail_uncompressed;
358
Chris Masonc8b97812008-10-29 14:49:59 -0400359 total_compressed = actual_end - start;
360
361 /* we want to make sure that amount of ram required to uncompress
362 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500363 * of a compressed extent to 128k. This is a crucial number
364 * because it also controls how easily we can spread reads across
365 * cpus for decompression.
366 *
367 * We also want to make sure the amount of IO required to do
368 * a random read is reasonably small, so we limit the size of
369 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400370 */
371 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400372 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500373 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400374 total_in = 0;
375 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400376
Chris Mason771ed682008-11-06 22:02:51 -0500377 /*
378 * we do compression for mount -o compress and when the
379 * inode has not been flagged as nocompress. This flag can
380 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400381 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200382 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500383 (btrfs_test_opt(root, COMPRESS) ||
384 (BTRFS_I(inode)->force_compress))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400385 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400386 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Chris Mason179e29e2007-11-01 11:28:41 -0400387
Li Zefan261507a02010-12-17 14:21:50 +0800388 if (BTRFS_I(inode)->force_compress)
389 compress_type = BTRFS_I(inode)->force_compress;
390
391 ret = btrfs_compress_pages(compress_type,
392 inode->i_mapping, start,
393 total_compressed, pages,
394 nr_pages, &nr_pages_ret,
395 &total_in,
396 &total_compressed,
397 max_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400398
399 if (!ret) {
400 unsigned long offset = total_compressed &
401 (PAGE_CACHE_SIZE - 1);
402 struct page *page = pages[nr_pages_ret - 1];
403 char *kaddr;
404
405 /* zero the tail end of the last page, we might be
406 * sending it down to disk
407 */
408 if (offset) {
409 kaddr = kmap_atomic(page, KM_USER0);
410 memset(kaddr + offset, 0,
411 PAGE_CACHE_SIZE - offset);
412 kunmap_atomic(kaddr, KM_USER0);
413 }
414 will_compress = 1;
415 }
416 }
417 if (start == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500418 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000419 BUG_ON(IS_ERR(trans));
Chris Mason771ed682008-11-06 22:02:51 -0500420 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400421 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500422
Chris Masonc8b97812008-10-29 14:49:59 -0400423 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500424 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400425 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500426 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400427 */
428 ret = cow_file_range_inline(trans, root, inode,
429 start, end, 0, NULL);
430 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500431 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400432 ret = cow_file_range_inline(trans, root, inode,
433 start, end,
434 total_compressed, pages);
435 }
436 if (ret == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500437 /*
438 * inline extent creation worked, we don't need
439 * to create any more async work items. Unlock
440 * and free up our temp pages.
441 */
Chris Masonc8b97812008-10-29 14:49:59 -0400442 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400443 &BTRFS_I(inode)->io_tree,
444 start, end, NULL,
445 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400446 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400447 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000448
449 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400450 goto free_pages_out;
451 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000452 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400453 }
454
455 if (will_compress) {
456 /*
457 * we aren't doing an inline extent round the compressed size
458 * up to a block size boundary so the allocator does sane
459 * things
460 */
461 total_compressed = (total_compressed + blocksize - 1) &
462 ~(blocksize - 1);
463
464 /*
465 * one last check to make sure the compression is really a
466 * win, compare the page count read with the blocks on disk
467 */
468 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
469 ~(PAGE_CACHE_SIZE - 1);
470 if (total_compressed >= total_in) {
471 will_compress = 0;
472 } else {
Chris Masonc8b97812008-10-29 14:49:59 -0400473 num_bytes = total_in;
474 }
475 }
476 if (!will_compress && pages) {
477 /*
478 * the compression code ran but failed to make things smaller,
479 * free any pages it allocated and our page pointer array
480 */
481 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400482 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400483 page_cache_release(pages[i]);
484 }
485 kfree(pages);
486 pages = NULL;
487 total_compressed = 0;
488 nr_pages_ret = 0;
489
490 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500491 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
492 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500493 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500494 }
Chris Masonc8b97812008-10-29 14:49:59 -0400495 }
Chris Mason771ed682008-11-06 22:02:51 -0500496 if (will_compress) {
497 *num_added += 1;
498
499 /* the async work queues will take care of doing actual
500 * allocation on disk for these compressed pages,
501 * and will submit them to the elevator.
502 */
503 add_async_extent(async_cow, start, num_bytes,
Li Zefan261507a02010-12-17 14:21:50 +0800504 total_compressed, pages, nr_pages_ret,
505 compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500506
Yan, Zheng24ae6362010-12-06 07:02:36 +0000507 if (start + num_bytes < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500508 start += num_bytes;
509 pages = NULL;
510 cond_resched();
511 goto again;
512 }
513 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500514cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500515 /*
516 * No compression, but we still need to write the pages in
517 * the file we've been given so far. redirty the locked
518 * page if it corresponds to our extent and set things up
519 * for the async work queue to run cow_file_range to do
520 * the normal delalloc dance
521 */
522 if (page_offset(locked_page) >= start &&
523 page_offset(locked_page) <= end) {
524 __set_page_dirty_nobuffers(locked_page);
525 /* unlocked later on in the async handlers */
526 }
Li Zefan261507a02010-12-17 14:21:50 +0800527 add_async_extent(async_cow, start, end - start + 1,
528 0, NULL, 0, BTRFS_COMPRESS_NONE);
Chris Mason771ed682008-11-06 22:02:51 -0500529 *num_added += 1;
530 }
531
532out:
533 return 0;
534
535free_pages_out:
536 for (i = 0; i < nr_pages_ret; i++) {
537 WARN_ON(pages[i]->mapping);
538 page_cache_release(pages[i]);
539 }
Chris Masond3977122009-01-05 21:25:51 -0500540 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500541
542 goto out;
543}
544
545/*
546 * phase two of compressed writeback. This is the ordered portion
547 * of the code, which only gets called in the order the work was
548 * queued. We walk all the async extents created by compress_file_range
549 * and send them down to the disk.
550 */
551static noinline int submit_compressed_extents(struct inode *inode,
552 struct async_cow *async_cow)
553{
554 struct async_extent *async_extent;
555 u64 alloc_hint = 0;
556 struct btrfs_trans_handle *trans;
557 struct btrfs_key ins;
558 struct extent_map *em;
559 struct btrfs_root *root = BTRFS_I(inode)->root;
560 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
561 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500562 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500563
564 if (list_empty(&async_cow->extents))
565 return 0;
566
Chris Mason771ed682008-11-06 22:02:51 -0500567
Chris Masond3977122009-01-05 21:25:51 -0500568 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500569 async_extent = list_entry(async_cow->extents.next,
570 struct async_extent, list);
571 list_del(&async_extent->list);
572
573 io_tree = &BTRFS_I(inode)->io_tree;
574
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500575retry:
Chris Mason771ed682008-11-06 22:02:51 -0500576 /* did the compression code fall back to uncompressed IO? */
577 if (!async_extent->pages) {
578 int page_started = 0;
579 unsigned long nr_written = 0;
580
581 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000582 async_extent->start +
583 async_extent->ram_size - 1, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500584
585 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500586 ret = cow_file_range(inode, async_cow->locked_page,
587 async_extent->start,
588 async_extent->start +
589 async_extent->ram_size - 1,
590 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500591
592 /*
593 * if page_started, cow_file_range inserted an
594 * inline extent and took care of all the unlocking
595 * and IO for us. Otherwise, we need to submit
596 * all those pages down to the drive.
597 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500598 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500599 extent_write_locked_range(io_tree,
600 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500601 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500602 async_extent->ram_size - 1,
603 btrfs_get_extent,
604 WB_SYNC_ALL);
605 kfree(async_extent);
606 cond_resched();
607 continue;
608 }
609
610 lock_extent(io_tree, async_extent->start,
611 async_extent->start + async_extent->ram_size - 1,
612 GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500613
Yan, Zhengc2167752009-11-12 09:34:21 +0000614 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000615 BUG_ON(IS_ERR(trans));
Chris Mason771ed682008-11-06 22:02:51 -0500616 ret = btrfs_reserve_extent(trans, root,
617 async_extent->compressed_size,
618 async_extent->compressed_size,
619 0, alloc_hint,
620 (u64)-1, &ins, 1);
Yan, Zhengc2167752009-11-12 09:34:21 +0000621 btrfs_end_transaction(trans, root);
622
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500623 if (ret) {
624 int i;
625 for (i = 0; i < async_extent->nr_pages; i++) {
626 WARN_ON(async_extent->pages[i]->mapping);
627 page_cache_release(async_extent->pages[i]);
628 }
629 kfree(async_extent->pages);
630 async_extent->nr_pages = 0;
631 async_extent->pages = NULL;
632 unlock_extent(io_tree, async_extent->start,
633 async_extent->start +
634 async_extent->ram_size - 1, GFP_NOFS);
635 goto retry;
636 }
637
Yan, Zhengc2167752009-11-12 09:34:21 +0000638 /*
639 * here we're doing allocation and writeback of the
640 * compressed pages
641 */
642 btrfs_drop_extent_cache(inode, async_extent->start,
643 async_extent->start +
644 async_extent->ram_size - 1, 0);
645
Chris Mason771ed682008-11-06 22:02:51 -0500646 em = alloc_extent_map(GFP_NOFS);
647 em->start = async_extent->start;
648 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500649 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500650
651 em->block_start = ins.objectid;
652 em->block_len = ins.offset;
653 em->bdev = root->fs_info->fs_devices->latest_bdev;
Li Zefan261507a02010-12-17 14:21:50 +0800654 em->compress_type = async_extent->compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500655 set_bit(EXTENT_FLAG_PINNED, &em->flags);
656 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
657
Chris Masond3977122009-01-05 21:25:51 -0500658 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400659 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500660 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400661 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500662 if (ret != -EEXIST) {
663 free_extent_map(em);
664 break;
665 }
666 btrfs_drop_extent_cache(inode, async_extent->start,
667 async_extent->start +
668 async_extent->ram_size - 1, 0);
669 }
670
Li Zefan261507a02010-12-17 14:21:50 +0800671 ret = btrfs_add_ordered_extent_compress(inode,
672 async_extent->start,
673 ins.objectid,
674 async_extent->ram_size,
675 ins.offset,
676 BTRFS_ORDERED_COMPRESSED,
677 async_extent->compress_type);
Chris Mason771ed682008-11-06 22:02:51 -0500678 BUG_ON(ret);
679
Chris Mason771ed682008-11-06 22:02:51 -0500680 /*
681 * clear dirty, set writeback and unlock the pages.
682 */
683 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400684 &BTRFS_I(inode)->io_tree,
685 async_extent->start,
686 async_extent->start +
687 async_extent->ram_size - 1,
688 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
689 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400690 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400691 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500692
693 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500694 async_extent->start,
695 async_extent->ram_size,
696 ins.objectid,
697 ins.offset, async_extent->pages,
698 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500699
700 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -0500701 alloc_hint = ins.objectid + ins.offset;
702 kfree(async_extent);
703 cond_resched();
704 }
705
Chris Mason771ed682008-11-06 22:02:51 -0500706 return 0;
707}
708
Josef Bacik4b46fce2010-05-23 11:00:55 -0400709static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
710 u64 num_bytes)
711{
712 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
713 struct extent_map *em;
714 u64 alloc_hint = 0;
715
716 read_lock(&em_tree->lock);
717 em = search_extent_mapping(em_tree, start, num_bytes);
718 if (em) {
719 /*
720 * if block start isn't an actual block number then find the
721 * first block in this inode and use that as a hint. If that
722 * block is also bogus then just don't worry about it.
723 */
724 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
725 free_extent_map(em);
726 em = search_extent_mapping(em_tree, 0, 0);
727 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
728 alloc_hint = em->block_start;
729 if (em)
730 free_extent_map(em);
731 } else {
732 alloc_hint = em->block_start;
733 free_extent_map(em);
734 }
735 }
736 read_unlock(&em_tree->lock);
737
738 return alloc_hint;
739}
740
Chris Mason771ed682008-11-06 22:02:51 -0500741/*
742 * when extent_io.c finds a delayed allocation range in the file,
743 * the call backs end up in this code. The basic idea is to
744 * allocate extents on disk for the range, and create ordered data structs
745 * in ram to track those extents.
746 *
747 * locked_page is the page that writepage had locked already. We use
748 * it to make sure we don't do extra locks or unlocks.
749 *
750 * *page_started is set to one if we unlock locked_page and do everything
751 * required to start IO on it. It may be clean and already done with
752 * IO when we return.
753 */
754static noinline int cow_file_range(struct inode *inode,
755 struct page *locked_page,
756 u64 start, u64 end, int *page_started,
757 unsigned long *nr_written,
758 int unlock)
759{
760 struct btrfs_root *root = BTRFS_I(inode)->root;
761 struct btrfs_trans_handle *trans;
762 u64 alloc_hint = 0;
763 u64 num_bytes;
764 unsigned long ram_size;
765 u64 disk_num_bytes;
766 u64 cur_alloc_size;
767 u64 blocksize = root->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500768 struct btrfs_key ins;
769 struct extent_map *em;
770 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
771 int ret = 0;
772
Josef Bacik0cb59c92010-07-02 12:14:14 -0400773 BUG_ON(root == root->fs_info->tree_root);
Chris Mason771ed682008-11-06 22:02:51 -0500774 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +0000775 BUG_ON(IS_ERR(trans));
Chris Mason771ed682008-11-06 22:02:51 -0500776 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400777 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500778
Chris Mason771ed682008-11-06 22:02:51 -0500779 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
780 num_bytes = max(blocksize, num_bytes);
781 disk_num_bytes = num_bytes;
782 ret = 0;
783
784 if (start == 0) {
785 /* lets try to make an inline extent */
786 ret = cow_file_range_inline(trans, root, inode,
787 start, end, 0, NULL);
788 if (ret == 0) {
789 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400790 &BTRFS_I(inode)->io_tree,
791 start, end, NULL,
792 EXTENT_CLEAR_UNLOCK_PAGE |
793 EXTENT_CLEAR_UNLOCK |
794 EXTENT_CLEAR_DELALLOC |
795 EXTENT_CLEAR_DIRTY |
796 EXTENT_SET_WRITEBACK |
797 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000798
Chris Mason771ed682008-11-06 22:02:51 -0500799 *nr_written = *nr_written +
800 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
801 *page_started = 1;
802 ret = 0;
803 goto out;
804 }
805 }
Chris Masonc8b97812008-10-29 14:49:59 -0400806
807 BUG_ON(disk_num_bytes >
808 btrfs_super_total_bytes(&root->fs_info->super_copy));
809
Josef Bacik4b46fce2010-05-23 11:00:55 -0400810 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400811 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400812
Chris Masond3977122009-01-05 21:25:51 -0500813 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400814 unsigned long op;
815
Josef Bacik287a0ab2010-03-19 18:07:23 +0000816 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400817 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500818 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400819 (u64)-1, &ins, 1);
Chris Masond3977122009-01-05 21:25:51 -0500820 BUG_ON(ret);
821
Chris Masone6dcd2d2008-07-17 12:53:50 -0400822 em = alloc_extent_map(GFP_NOFS);
823 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500824 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500825 ram_size = ins.offset;
826 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400827
Chris Masone6dcd2d2008-07-17 12:53:50 -0400828 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400829 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400830 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400831 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400832
Chris Masond3977122009-01-05 21:25:51 -0500833 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400834 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400835 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400836 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400837 if (ret != -EEXIST) {
838 free_extent_map(em);
839 break;
840 }
841 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400842 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400843 }
844
Chris Mason98d20f62008-04-14 09:46:10 -0400845 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400846 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500847 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400848 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400849
Yan Zheng17d217f2008-12-12 10:03:38 -0500850 if (root->root_key.objectid ==
851 BTRFS_DATA_RELOC_TREE_OBJECTID) {
852 ret = btrfs_reloc_clone_csums(inode, start,
853 cur_alloc_size);
854 BUG_ON(ret);
855 }
856
Chris Masond3977122009-01-05 21:25:51 -0500857 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400858 break;
Chris Masond3977122009-01-05 21:25:51 -0500859
Chris Masonc8b97812008-10-29 14:49:59 -0400860 /* we're not doing compressed IO, don't unlock the first
861 * page (which the caller expects to stay locked), don't
862 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400863 *
864 * Do set the Private2 bit so we know this page was properly
865 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400866 */
Chris Masona791e352009-10-08 11:27:10 -0400867 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
868 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
869 EXTENT_SET_PRIVATE2;
870
Chris Masonc8b97812008-10-29 14:49:59 -0400871 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
872 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400873 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400874 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500875 num_bytes -= cur_alloc_size;
876 alloc_hint = ins.objectid + ins.offset;
877 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400878 }
Chris Masonb888db22007-08-27 16:49:44 -0400879out:
Chris Mason771ed682008-11-06 22:02:51 -0500880 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400881 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400882
Chris Masonbe20aa92007-12-17 20:14:01 -0500883 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500884}
Chris Masonc8b97812008-10-29 14:49:59 -0400885
Chris Mason771ed682008-11-06 22:02:51 -0500886/*
887 * work queue call back to started compression on a file and pages
888 */
889static noinline void async_cow_start(struct btrfs_work *work)
890{
891 struct async_cow *async_cow;
892 int num_added = 0;
893 async_cow = container_of(work, struct async_cow, work);
894
895 compress_file_range(async_cow->inode, async_cow->locked_page,
896 async_cow->start, async_cow->end, async_cow,
897 &num_added);
898 if (num_added == 0)
899 async_cow->inode = NULL;
900}
901
902/*
903 * work queue call back to submit previously compressed pages
904 */
905static noinline void async_cow_submit(struct btrfs_work *work)
906{
907 struct async_cow *async_cow;
908 struct btrfs_root *root;
909 unsigned long nr_pages;
910
911 async_cow = container_of(work, struct async_cow, work);
912
913 root = async_cow->root;
914 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
915 PAGE_CACHE_SHIFT;
916
917 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
918
919 if (atomic_read(&root->fs_info->async_delalloc_pages) <
920 5 * 1042 * 1024 &&
921 waitqueue_active(&root->fs_info->async_submit_wait))
922 wake_up(&root->fs_info->async_submit_wait);
923
Chris Masond3977122009-01-05 21:25:51 -0500924 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -0500925 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500926}
Chris Masonc8b97812008-10-29 14:49:59 -0400927
Chris Mason771ed682008-11-06 22:02:51 -0500928static noinline void async_cow_free(struct btrfs_work *work)
929{
930 struct async_cow *async_cow;
931 async_cow = container_of(work, struct async_cow, work);
932 kfree(async_cow);
933}
934
935static int cow_file_range_async(struct inode *inode, struct page *locked_page,
936 u64 start, u64 end, int *page_started,
937 unsigned long *nr_written)
938{
939 struct async_cow *async_cow;
940 struct btrfs_root *root = BTRFS_I(inode)->root;
941 unsigned long nr_pages;
942 u64 cur_end;
943 int limit = 10 * 1024 * 1042;
944
Chris Masona3429ab2009-10-08 12:30:20 -0400945 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
946 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500947 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500948 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
949 async_cow->inode = inode;
950 async_cow->root = root;
951 async_cow->locked_page = locked_page;
952 async_cow->start = start;
953
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200954 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -0500955 cur_end = end;
956 else
957 cur_end = min(end, start + 512 * 1024 - 1);
958
959 async_cow->end = cur_end;
960 INIT_LIST_HEAD(&async_cow->extents);
961
962 async_cow->work.func = async_cow_start;
963 async_cow->work.ordered_func = async_cow_submit;
964 async_cow->work.ordered_free = async_cow_free;
965 async_cow->work.flags = 0;
966
Chris Mason771ed682008-11-06 22:02:51 -0500967 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
968 PAGE_CACHE_SHIFT;
969 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
970
971 btrfs_queue_worker(&root->fs_info->delalloc_workers,
972 &async_cow->work);
973
974 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
975 wait_event(root->fs_info->async_submit_wait,
976 (atomic_read(&root->fs_info->async_delalloc_pages) <
977 limit));
978 }
979
Chris Masond3977122009-01-05 21:25:51 -0500980 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -0500981 atomic_read(&root->fs_info->async_delalloc_pages)) {
982 wait_event(root->fs_info->async_submit_wait,
983 (atomic_read(&root->fs_info->async_delalloc_pages) ==
984 0));
985 }
986
987 *nr_written += nr_pages;
988 start = cur_end + 1;
989 }
990 *page_started = 1;
991 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500992}
993
Chris Masond3977122009-01-05 21:25:51 -0500994static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -0500995 u64 bytenr, u64 num_bytes)
996{
997 int ret;
998 struct btrfs_ordered_sum *sums;
999 LIST_HEAD(list);
1000
Yan Zheng07d400a2009-01-06 11:42:00 -05001001 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
1002 bytenr + num_bytes - 1, &list);
Yan Zheng17d217f2008-12-12 10:03:38 -05001003 if (ret == 0 && list_empty(&list))
1004 return 0;
1005
1006 while (!list_empty(&list)) {
1007 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1008 list_del(&sums->list);
1009 kfree(sums);
1010 }
1011 return 1;
1012}
1013
Chris Masond352ac62008-09-29 15:18:18 -04001014/*
1015 * when nowcow writeback call back. This checks for snapshots or COW copies
1016 * of the extents that exist in the file, and COWs the file as required.
1017 *
1018 * If no cow copies or snapshots exist, we write directly to the existing
1019 * blocks on disk
1020 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001021static noinline int run_delalloc_nocow(struct inode *inode,
1022 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001023 u64 start, u64 end, int *page_started, int force,
1024 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001025{
Chris Masonbe20aa92007-12-17 20:14:01 -05001026 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001027 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001028 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001029 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001030 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001031 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001032 u64 cow_start;
1033 u64 cur_offset;
1034 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001035 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001036 u64 disk_bytenr;
1037 u64 num_bytes;
1038 int extent_type;
1039 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001040 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001041 int nocow;
1042 int check_prev = 1;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001043 bool nolock = false;
Chris Masonbe20aa92007-12-17 20:14:01 -05001044
1045 path = btrfs_alloc_path();
1046 BUG_ON(!path);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001047 if (root == root->fs_info->tree_root) {
1048 nolock = true;
1049 trans = btrfs_join_transaction_nolock(root, 1);
1050 } else {
1051 trans = btrfs_join_transaction(root, 1);
1052 }
Tsutomu Itoh3612b492011-01-25 02:51:38 +00001053 BUG_ON(IS_ERR(trans));
Chris Masonbe20aa92007-12-17 20:14:01 -05001054
Yan Zheng80ff3852008-10-30 14:20:02 -04001055 cow_start = (u64)-1;
1056 cur_offset = start;
1057 while (1) {
1058 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1059 cur_offset, 0);
1060 BUG_ON(ret < 0);
1061 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1062 leaf = path->nodes[0];
1063 btrfs_item_key_to_cpu(leaf, &found_key,
1064 path->slots[0] - 1);
1065 if (found_key.objectid == inode->i_ino &&
1066 found_key.type == BTRFS_EXTENT_DATA_KEY)
1067 path->slots[0]--;
1068 }
1069 check_prev = 0;
1070next_slot:
1071 leaf = path->nodes[0];
1072 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1073 ret = btrfs_next_leaf(root, path);
1074 if (ret < 0)
1075 BUG_ON(1);
1076 if (ret > 0)
1077 break;
1078 leaf = path->nodes[0];
1079 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001080
Yan Zheng80ff3852008-10-30 14:20:02 -04001081 nocow = 0;
1082 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001083 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001084 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001085
Yan Zheng80ff3852008-10-30 14:20:02 -04001086 if (found_key.objectid > inode->i_ino ||
1087 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1088 found_key.offset > end)
1089 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001090
Yan Zheng80ff3852008-10-30 14:20:02 -04001091 if (found_key.offset > cur_offset) {
1092 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001093 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001094 goto out_check;
1095 }
Chris Masonc31f8832008-01-08 15:46:31 -05001096
Yan Zheng80ff3852008-10-30 14:20:02 -04001097 fi = btrfs_item_ptr(leaf, path->slots[0],
1098 struct btrfs_file_extent_item);
1099 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001100
Yan Zhengd899e052008-10-30 14:25:28 -04001101 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1102 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001103 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001104 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001105 extent_end = found_key.offset +
1106 btrfs_file_extent_num_bytes(leaf, fi);
1107 if (extent_end <= start) {
1108 path->slots[0]++;
1109 goto next_slot;
1110 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001111 if (disk_bytenr == 0)
1112 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001113 if (btrfs_file_extent_compression(leaf, fi) ||
1114 btrfs_file_extent_encryption(leaf, fi) ||
1115 btrfs_file_extent_other_encoding(leaf, fi))
1116 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001117 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1118 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001119 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001120 goto out_check;
Yan Zheng17d217f2008-12-12 10:03:38 -05001121 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001122 found_key.offset -
1123 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001124 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001125 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001126 disk_bytenr += cur_offset - found_key.offset;
1127 num_bytes = min(end + 1, extent_end) - cur_offset;
1128 /*
1129 * force cow if csum exists in the range.
1130 * this ensure that csum for a given extent are
1131 * either valid or do not exist.
1132 */
1133 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1134 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001135 nocow = 1;
1136 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1137 extent_end = found_key.offset +
1138 btrfs_file_extent_inline_len(leaf, fi);
1139 extent_end = ALIGN(extent_end, root->sectorsize);
1140 } else {
1141 BUG_ON(1);
1142 }
1143out_check:
1144 if (extent_end <= start) {
1145 path->slots[0]++;
1146 goto next_slot;
1147 }
1148 if (!nocow) {
1149 if (cow_start == (u64)-1)
1150 cow_start = cur_offset;
1151 cur_offset = extent_end;
1152 if (cur_offset > end)
1153 break;
1154 path->slots[0]++;
1155 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001156 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001157
Yan Zheng7ea394f2008-08-05 13:05:02 -04001158 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001159 if (cow_start != (u64)-1) {
1160 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001161 found_key.offset - 1, page_started,
1162 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001163 BUG_ON(ret);
1164 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001165 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001166
Yan Zhengd899e052008-10-30 14:25:28 -04001167 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1168 struct extent_map *em;
1169 struct extent_map_tree *em_tree;
1170 em_tree = &BTRFS_I(inode)->extent_tree;
1171 em = alloc_extent_map(GFP_NOFS);
1172 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001173 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001174 em->len = num_bytes;
1175 em->block_len = num_bytes;
1176 em->block_start = disk_bytenr;
1177 em->bdev = root->fs_info->fs_devices->latest_bdev;
1178 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1179 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001180 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001181 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001182 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001183 if (ret != -EEXIST) {
1184 free_extent_map(em);
1185 break;
1186 }
1187 btrfs_drop_extent_cache(inode, em->start,
1188 em->start + em->len - 1, 0);
1189 }
1190 type = BTRFS_ORDERED_PREALLOC;
1191 } else {
1192 type = BTRFS_ORDERED_NOCOW;
1193 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001194
1195 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001196 num_bytes, num_bytes, type);
1197 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001198
Yan, Zhengefa56462010-05-16 10:49:59 -04001199 if (root->root_key.objectid ==
1200 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1201 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1202 num_bytes);
1203 BUG_ON(ret);
1204 }
1205
Yan Zhengd899e052008-10-30 14:25:28 -04001206 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001207 cur_offset, cur_offset + num_bytes - 1,
1208 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1209 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1210 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001211 cur_offset = extent_end;
1212 if (cur_offset > end)
1213 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001214 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001215 btrfs_release_path(root, path);
1216
1217 if (cur_offset <= end && cow_start == (u64)-1)
1218 cow_start = cur_offset;
1219 if (cow_start != (u64)-1) {
1220 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001221 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001222 BUG_ON(ret);
1223 }
1224
Josef Bacik0cb59c92010-07-02 12:14:14 -04001225 if (nolock) {
1226 ret = btrfs_end_transaction_nolock(trans, root);
1227 BUG_ON(ret);
1228 } else {
1229 ret = btrfs_end_transaction(trans, root);
1230 BUG_ON(ret);
1231 }
Yan Zheng7ea394f2008-08-05 13:05:02 -04001232 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001233 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001234}
1235
Chris Masond352ac62008-09-29 15:18:18 -04001236/*
1237 * extent_io.c call back to do delayed allocation processing
1238 */
Chris Masonc8b97812008-10-29 14:49:59 -04001239static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001240 u64 start, u64 end, int *page_started,
1241 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001242{
Chris Masonbe20aa92007-12-17 20:14:01 -05001243 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001244 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001245
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001246 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
Chris Masonc8b97812008-10-29 14:49:59 -04001247 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001248 page_started, 1, nr_written);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001249 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
Yan Zhengd899e052008-10-30 14:25:28 -04001250 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001251 page_started, 0, nr_written);
Chris Mason1e701a32010-03-11 09:42:04 -05001252 else if (!btrfs_test_opt(root, COMPRESS) &&
1253 !(BTRFS_I(inode)->force_compress))
Chris Mason7f366cf2009-03-12 20:12:45 -04001254 ret = cow_file_range(inode, locked_page, start, end,
1255 page_started, nr_written, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -05001256 else
Chris Mason771ed682008-11-06 22:02:51 -05001257 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001258 page_started, nr_written);
Chris Masonb888db22007-08-27 16:49:44 -04001259 return ret;
1260}
1261
Josef Bacik9ed74f22009-09-11 16:12:44 -04001262static int btrfs_split_extent_hook(struct inode *inode,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001263 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001264{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001265 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001266 if (!(orig->state & EXTENT_DELALLOC))
1267 return 0;
1268
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001269 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001270 return 0;
1271}
1272
1273/*
1274 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1275 * extents so we can keep track of new extents that are just merged onto old
1276 * extents, such as when we are doing sequential writes, so we can properly
1277 * account for the metadata space we'll need.
1278 */
1279static int btrfs_merge_extent_hook(struct inode *inode,
1280 struct extent_state *new,
1281 struct extent_state *other)
1282{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001283 /* not delalloc, ignore it */
1284 if (!(other->state & EXTENT_DELALLOC))
1285 return 0;
1286
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001287 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001288 return 0;
1289}
1290
Chris Masond352ac62008-09-29 15:18:18 -04001291/*
1292 * extent_io.c set_bit_hook, used to track delayed allocation
1293 * bytes in this file, and to maintain the list of inodes that
1294 * have pending delalloc work to be done.
1295 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001296static int btrfs_set_bit_hook(struct inode *inode,
1297 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001298{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001299
Chris Mason75eff682008-12-15 15:54:40 -05001300 /*
1301 * set_bit and clear bit hooks normally require _irqsave/restore
1302 * but in this case, we are only testeing for the DELALLOC
1303 * bit, which is only set or cleared with irqs on
1304 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001305 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001306 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001307 u64 len = state->end + 1 - state->start;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001308 int do_list = (root->root_key.objectid !=
1309 BTRFS_ROOT_TREE_OBJECTID);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001310
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001311 if (*bits & EXTENT_FIRST_DELALLOC)
1312 *bits &= ~EXTENT_FIRST_DELALLOC;
1313 else
1314 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001315
Chris Mason75eff682008-12-15 15:54:40 -05001316 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001317 BTRFS_I(inode)->delalloc_bytes += len;
1318 root->fs_info->delalloc_bytes += len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001319 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
Chris Masonea8c2812008-08-04 23:17:27 -04001320 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1321 &root->fs_info->delalloc_inodes);
1322 }
Chris Mason75eff682008-12-15 15:54:40 -05001323 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001324 }
1325 return 0;
1326}
1327
Chris Masond352ac62008-09-29 15:18:18 -04001328/*
1329 * extent_io.c clear_bit_hook, see set_bit_hook for why
1330 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001331static int btrfs_clear_bit_hook(struct inode *inode,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001332 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001333{
Chris Mason75eff682008-12-15 15:54:40 -05001334 /*
1335 * set_bit and clear bit hooks normally require _irqsave/restore
1336 * but in this case, we are only testeing for the DELALLOC
1337 * bit, which is only set or cleared with irqs on
1338 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001339 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001340 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001341 u64 len = state->end + 1 - state->start;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001342 int do_list = (root->root_key.objectid !=
1343 BTRFS_ROOT_TREE_OBJECTID);
Chris Masonbcbfce82008-04-22 13:26:47 -04001344
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001345 if (*bits & EXTENT_FIRST_DELALLOC)
1346 *bits &= ~EXTENT_FIRST_DELALLOC;
1347 else if (!(*bits & EXTENT_DO_ACCOUNTING))
1348 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
1349
1350 if (*bits & EXTENT_DO_ACCOUNTING)
1351 btrfs_delalloc_release_metadata(inode, len);
1352
Josef Bacik0cb59c92010-07-02 12:14:14 -04001353 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1354 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001355 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001356
Chris Mason75eff682008-12-15 15:54:40 -05001357 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001358 root->fs_info->delalloc_bytes -= len;
1359 BTRFS_I(inode)->delalloc_bytes -= len;
1360
Josef Bacik0cb59c92010-07-02 12:14:14 -04001361 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Chris Masonea8c2812008-08-04 23:17:27 -04001362 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1363 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1364 }
Chris Mason75eff682008-12-15 15:54:40 -05001365 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001366 }
1367 return 0;
1368}
1369
Chris Masond352ac62008-09-29 15:18:18 -04001370/*
1371 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1372 * we don't create bios that span stripes or chunks
1373 */
Chris Mason239b14b2008-03-24 15:02:07 -04001374int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001375 size_t size, struct bio *bio,
1376 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001377{
1378 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1379 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001380 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001381 u64 length = 0;
1382 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001383 int ret;
1384
Chris Mason771ed682008-11-06 22:02:51 -05001385 if (bio_flags & EXTENT_BIO_COMPRESSED)
1386 return 0;
1387
Chris Masonf2d8d742008-04-21 10:03:05 -04001388 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001389 map_tree = &root->fs_info->mapping_tree;
1390 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001391 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001392 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001393
Chris Masond3977122009-01-05 21:25:51 -05001394 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001395 return 1;
Andi Kleen411fc6b2010-10-29 15:14:31 -04001396 return ret;
Chris Mason239b14b2008-03-24 15:02:07 -04001397}
1398
Chris Masond352ac62008-09-29 15:18:18 -04001399/*
1400 * in order to insert checksums into the metadata in large chunks,
1401 * we wait until bio submission time. All the pages in the bio are
1402 * checksummed and sums are attached onto the ordered extent record.
1403 *
1404 * At IO completion time the cums attached on the ordered extent record
1405 * are inserted into the btree
1406 */
Chris Masond3977122009-01-05 21:25:51 -05001407static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1408 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001409 unsigned long bio_flags,
1410 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001411{
Chris Mason065631f2008-02-20 12:07:25 -05001412 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001413 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001414
Chris Masond20f7042008-12-08 16:58:54 -05001415 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001416 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001417 return 0;
1418}
Chris Masone0156402008-04-16 11:15:20 -04001419
Chris Mason4a69a412008-11-06 22:03:00 -05001420/*
1421 * in order to insert checksums into the metadata in large chunks,
1422 * we wait until bio submission time. All the pages in the bio are
1423 * checksummed and sums are attached onto the ordered extent record.
1424 *
1425 * At IO completion time the cums attached on the ordered extent record
1426 * are inserted into the btree
1427 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001428static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001429 int mirror_num, unsigned long bio_flags,
1430 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001431{
1432 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001433 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001434}
1435
Chris Masond352ac62008-09-29 15:18:18 -04001436/*
Chris Masoncad321a2008-12-17 14:51:42 -05001437 * extent_io.c submission hook. This does the right thing for csum calculation
1438 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001439 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001440static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001441 int mirror_num, unsigned long bio_flags,
1442 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001443{
1444 struct btrfs_root *root = BTRFS_I(inode)->root;
1445 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001446 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001447
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001448 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001449
Josef Bacik0cb59c92010-07-02 12:14:14 -04001450 if (root == root->fs_info->tree_root)
1451 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2);
1452 else
1453 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001454 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -05001455
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001456 if (!(rw & REQ_WRITE)) {
Chris Masond20f7042008-12-08 16:58:54 -05001457 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001458 return btrfs_submit_compressed_read(inode, bio,
1459 mirror_num, bio_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001460 } else if (!skip_sum)
1461 btrfs_lookup_bio_sums(root, inode, bio, NULL);
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001462 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001463 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001464 /* csum items have already been cloned */
1465 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1466 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001467 /* we're doing a write, do the async checksumming */
1468 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001469 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001470 bio_flags, bio_offset,
1471 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001472 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001473 }
1474
Chris Mason0b86a832008-03-24 15:01:56 -04001475mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001476 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001477}
Chris Mason6885f302008-02-20 16:11:05 -05001478
Chris Masond352ac62008-09-29 15:18:18 -04001479/*
1480 * given a list of ordered sums record them in the inode. This happens
1481 * at IO completion time based on sums calculated at bio submission time.
1482 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001483static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001484 struct inode *inode, u64 file_offset,
1485 struct list_head *list)
1486{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001487 struct btrfs_ordered_sum *sum;
1488
1489 btrfs_set_trans_block_group(trans, inode);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001490
1491 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001492 btrfs_csum_file_blocks(trans,
1493 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001494 }
1495 return 0;
1496}
1497
Josef Bacik2ac55d42010-02-03 19:33:23 +00001498int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1499 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001500{
Chris Masond3977122009-01-05 21:25:51 -05001501 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001502 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001503 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001504 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001505}
1506
Chris Masond352ac62008-09-29 15:18:18 -04001507/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001508struct btrfs_writepage_fixup {
1509 struct page *page;
1510 struct btrfs_work work;
1511};
1512
Christoph Hellwigb2950862008-12-02 09:54:17 -05001513static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001514{
1515 struct btrfs_writepage_fixup *fixup;
1516 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001517 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001518 struct page *page;
1519 struct inode *inode;
1520 u64 page_start;
1521 u64 page_end;
1522
1523 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1524 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001525again:
Chris Mason247e7432008-07-17 12:53:51 -04001526 lock_page(page);
1527 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1528 ClearPageChecked(page);
1529 goto out_page;
1530 }
1531
1532 inode = page->mapping->host;
1533 page_start = page_offset(page);
1534 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1535
Josef Bacik2ac55d42010-02-03 19:33:23 +00001536 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
1537 &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001538
1539 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001540 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001541 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001542
1543 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1544 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001545 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1546 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001547 unlock_page(page);
1548 btrfs_start_ordered_extent(inode, ordered, 1);
1549 goto again;
1550 }
Chris Mason247e7432008-07-17 12:53:51 -04001551
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001552 BUG();
Josef Bacik2ac55d42010-02-03 19:33:23 +00001553 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001554 ClearPageChecked(page);
1555out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001556 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1557 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001558out_page:
1559 unlock_page(page);
1560 page_cache_release(page);
Miao Xieb897abe2011-01-26 16:19:22 +08001561 kfree(fixup);
Chris Mason247e7432008-07-17 12:53:51 -04001562}
1563
1564/*
1565 * There are a few paths in the higher layers of the kernel that directly
1566 * set the page dirty bit without asking the filesystem if it is a
1567 * good idea. This causes problems because we want to make sure COW
1568 * properly happens and the data=ordered rules are followed.
1569 *
Chris Masonc8b97812008-10-29 14:49:59 -04001570 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001571 * hasn't been properly setup for IO. We kick off an async process
1572 * to fix it up. The async helper will wait for ordered extents, set
1573 * the delalloc bit and make it safe to write the page.
1574 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001575static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001576{
1577 struct inode *inode = page->mapping->host;
1578 struct btrfs_writepage_fixup *fixup;
1579 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001580
Chris Mason8b62b722009-09-02 16:53:46 -04001581 /* this page is properly in the ordered list */
1582 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001583 return 0;
1584
1585 if (PageChecked(page))
1586 return -EAGAIN;
1587
1588 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1589 if (!fixup)
1590 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001591
Chris Mason247e7432008-07-17 12:53:51 -04001592 SetPageChecked(page);
1593 page_cache_get(page);
1594 fixup->work.func = btrfs_writepage_fixup_worker;
1595 fixup->page = page;
1596 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1597 return -EAGAIN;
1598}
1599
Yan Zhengd899e052008-10-30 14:25:28 -04001600static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1601 struct inode *inode, u64 file_pos,
1602 u64 disk_bytenr, u64 disk_num_bytes,
1603 u64 num_bytes, u64 ram_bytes,
1604 u8 compression, u8 encryption,
1605 u16 other_encoding, int extent_type)
1606{
1607 struct btrfs_root *root = BTRFS_I(inode)->root;
1608 struct btrfs_file_extent_item *fi;
1609 struct btrfs_path *path;
1610 struct extent_buffer *leaf;
1611 struct btrfs_key ins;
1612 u64 hint;
1613 int ret;
1614
1615 path = btrfs_alloc_path();
1616 BUG_ON(!path);
1617
Chris Masonb9473432009-03-13 11:00:37 -04001618 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001619
1620 /*
1621 * we may be replacing one extent in the tree with another.
1622 * The new extent is pinned in the extent map, and we don't want
1623 * to drop it from the cache until it is completely in the btree.
1624 *
1625 * So, tell btrfs_drop_extents to leave this extent in the cache.
1626 * the caller is expected to unpin it and allow it to be merged
1627 * with the others.
1628 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001629 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1630 &hint, 0);
Yan Zhengd899e052008-10-30 14:25:28 -04001631 BUG_ON(ret);
1632
1633 ins.objectid = inode->i_ino;
1634 ins.offset = file_pos;
1635 ins.type = BTRFS_EXTENT_DATA_KEY;
1636 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1637 BUG_ON(ret);
1638 leaf = path->nodes[0];
1639 fi = btrfs_item_ptr(leaf, path->slots[0],
1640 struct btrfs_file_extent_item);
1641 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1642 btrfs_set_file_extent_type(leaf, fi, extent_type);
1643 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1644 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1645 btrfs_set_file_extent_offset(leaf, fi, 0);
1646 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1647 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1648 btrfs_set_file_extent_compression(leaf, fi, compression);
1649 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1650 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001651
1652 btrfs_unlock_up_safe(path, 1);
1653 btrfs_set_lock_blocking(leaf);
1654
Yan Zhengd899e052008-10-30 14:25:28 -04001655 btrfs_mark_buffer_dirty(leaf);
1656
1657 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001658
1659 ins.objectid = disk_bytenr;
1660 ins.offset = disk_num_bytes;
1661 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001662 ret = btrfs_alloc_reserved_file_extent(trans, root,
1663 root->root_key.objectid,
1664 inode->i_ino, file_pos, &ins);
Yan Zhengd899e052008-10-30 14:25:28 -04001665 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04001666 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001667
Yan Zhengd899e052008-10-30 14:25:28 -04001668 return 0;
1669}
1670
Chris Mason5d13a982009-03-13 11:41:46 -04001671/*
1672 * helper function for btrfs_finish_ordered_io, this
1673 * just reads in some of the csum leaves to prime them into ram
1674 * before we start the transaction. It limits the amount of btree
1675 * reads required while inside the transaction.
1676 */
Chris Masond352ac62008-09-29 15:18:18 -04001677/* as ordered data IO finishes, this gets called so we can finish
1678 * an ordered extent if the range of bytes in the file it covers are
1679 * fully written.
1680 */
Chris Mason211f90e2008-07-18 11:56:15 -04001681static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001682{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001683 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001684 struct btrfs_trans_handle *trans = NULL;
Chris Mason5d13a982009-03-13 11:41:46 -04001685 struct btrfs_ordered_extent *ordered_extent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001686 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001687 struct extent_state *cached_state = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08001688 int compress_type = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001689 int ret;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001690 bool nolock = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001691
Josef Bacik5a1a3df2010-02-02 20:51:14 +00001692 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1693 end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001694 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001695 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001696 BUG_ON(!ordered_extent);
Josef Bacikefd049f2010-02-02 20:50:10 +00001697
Josef Bacik0cb59c92010-07-02 12:14:14 -04001698 nolock = (root == root->fs_info->tree_root);
1699
Yan, Zhengc2167752009-11-12 09:34:21 +00001700 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1701 BUG_ON(!list_empty(&ordered_extent->list));
1702 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1703 if (!ret) {
Josef Bacik0cb59c92010-07-02 12:14:14 -04001704 if (nolock)
1705 trans = btrfs_join_transaction_nolock(root, 1);
1706 else
1707 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00001708 BUG_ON(IS_ERR(trans));
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001709 btrfs_set_trans_block_group(trans, inode);
1710 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001711 ret = btrfs_update_inode(trans, root, inode);
1712 BUG_ON(ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001713 }
1714 goto out;
1715 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001716
Josef Bacik2ac55d42010-02-03 19:33:23 +00001717 lock_extent_bits(io_tree, ordered_extent->file_offset,
1718 ordered_extent->file_offset + ordered_extent->len - 1,
1719 0, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001720
Josef Bacik0cb59c92010-07-02 12:14:14 -04001721 if (nolock)
1722 trans = btrfs_join_transaction_nolock(root, 1);
1723 else
1724 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00001725 BUG_ON(IS_ERR(trans));
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001726 btrfs_set_trans_block_group(trans, inode);
1727 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001728
Chris Masonc8b97812008-10-29 14:49:59 -04001729 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08001730 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04001731 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08001732 BUG_ON(compress_type);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001733 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001734 ordered_extent->file_offset,
1735 ordered_extent->file_offset +
1736 ordered_extent->len);
1737 BUG_ON(ret);
1738 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04001739 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04001740 ret = insert_reserved_file_extent(trans, inode,
1741 ordered_extent->file_offset,
1742 ordered_extent->start,
1743 ordered_extent->disk_len,
1744 ordered_extent->len,
1745 ordered_extent->len,
Li Zefan261507a02010-12-17 14:21:50 +08001746 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04001747 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001748 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1749 ordered_extent->file_offset,
1750 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001751 BUG_ON(ret);
1752 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001753 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1754 ordered_extent->file_offset +
1755 ordered_extent->len - 1, &cached_state, GFP_NOFS);
1756
Chris Masone6dcd2d2008-07-17 12:53:50 -04001757 add_pending_csums(trans, inode, ordered_extent->file_offset,
1758 &ordered_extent->list);
1759
Yan, Zhengc2167752009-11-12 09:34:21 +00001760 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1761 ret = btrfs_update_inode(trans, root, inode);
1762 BUG_ON(ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001763out:
Josef Bacik0cb59c92010-07-02 12:14:14 -04001764 if (nolock) {
1765 if (trans)
1766 btrfs_end_transaction_nolock(trans, root);
1767 } else {
1768 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
1769 if (trans)
1770 btrfs_end_transaction(trans, root);
1771 }
1772
Chris Masone6dcd2d2008-07-17 12:53:50 -04001773 /* once for us */
1774 btrfs_put_ordered_extent(ordered_extent);
1775 /* once for the tree */
1776 btrfs_put_ordered_extent(ordered_extent);
1777
Chris Masone6dcd2d2008-07-17 12:53:50 -04001778 return 0;
1779}
1780
Christoph Hellwigb2950862008-12-02 09:54:17 -05001781static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001782 struct extent_state *state, int uptodate)
1783{
Chris Mason8b62b722009-09-02 16:53:46 -04001784 ClearPagePrivate2(page);
Chris Mason211f90e2008-07-18 11:56:15 -04001785 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1786}
1787
Chris Masond352ac62008-09-29 15:18:18 -04001788/*
1789 * When IO fails, either with EIO or csum verification fails, we
1790 * try other mirrors that might have a good copy of the data. This
1791 * io_failure_record is used to record state as we go through all the
1792 * mirrors. If another mirror has good data, the page is set up to date
1793 * and things continue. If a good mirror can't be found, the original
1794 * bio end_io callback is called to indicate things have failed.
1795 */
Chris Mason7e383262008-04-09 16:28:12 -04001796struct io_failure_record {
1797 struct page *page;
1798 u64 start;
1799 u64 len;
1800 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001801 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001802 int last_mirror;
1803};
1804
Christoph Hellwigb2950862008-12-02 09:54:17 -05001805static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001806 struct page *page, u64 start, u64 end,
1807 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001808{
1809 struct io_failure_record *failrec = NULL;
1810 u64 private;
1811 struct extent_map *em;
1812 struct inode *inode = page->mapping->host;
1813 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001814 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001815 struct bio *bio;
1816 int num_copies;
1817 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001818 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001819 u64 logical;
1820
1821 ret = get_state_private(failure_tree, start, &private);
1822 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001823 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1824 if (!failrec)
1825 return -ENOMEM;
1826 failrec->start = start;
1827 failrec->len = end - start + 1;
1828 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001829 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001830
Chris Mason890871b2009-09-02 16:24:52 -04001831 read_lock(&em_tree->lock);
Chris Mason3b951512008-04-17 11:29:12 -04001832 em = lookup_extent_mapping(em_tree, start, failrec->len);
1833 if (em->start > start || em->start + em->len < start) {
1834 free_extent_map(em);
1835 em = NULL;
1836 }
Chris Mason890871b2009-09-02 16:24:52 -04001837 read_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001838
1839 if (!em || IS_ERR(em)) {
1840 kfree(failrec);
1841 return -EIO;
1842 }
1843 logical = start - em->start;
1844 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001845 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1846 logical = em->block_start;
1847 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08001848 extent_set_compress_type(&failrec->bio_flags,
1849 em->compress_type);
Chris Masond20f7042008-12-08 16:58:54 -05001850 }
Chris Mason7e383262008-04-09 16:28:12 -04001851 failrec->logical = logical;
1852 free_extent_map(em);
1853 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1854 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001855 set_state_private(failure_tree, start,
1856 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001857 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001858 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001859 }
1860 num_copies = btrfs_num_copies(
1861 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1862 failrec->logical, failrec->len);
1863 failrec->last_mirror++;
1864 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001865 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001866 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1867 failrec->start,
1868 EXTENT_LOCKED);
1869 if (state && state->start != failrec->start)
1870 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001871 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001872 }
1873 if (!state || failrec->last_mirror > num_copies) {
1874 set_state_private(failure_tree, failrec->start, 0);
1875 clear_extent_bits(failure_tree, failrec->start,
1876 failrec->start + failrec->len - 1,
1877 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1878 kfree(failrec);
1879 return -EIO;
1880 }
1881 bio = bio_alloc(GFP_NOFS, 1);
1882 bio->bi_private = state;
1883 bio->bi_end_io = failed_bio->bi_end_io;
1884 bio->bi_sector = failrec->logical >> 9;
1885 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001886 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001887
Chris Mason7e383262008-04-09 16:28:12 -04001888 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001889 if (failed_bio->bi_rw & REQ_WRITE)
Chris Mason1259ab72008-05-12 13:39:03 -04001890 rw = WRITE;
1891 else
1892 rw = READ;
1893
1894 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001895 failrec->last_mirror,
Chris Masoneaf25d92010-05-25 09:48:28 -04001896 failrec->bio_flags, 0);
Chris Mason1259ab72008-05-12 13:39:03 -04001897 return 0;
1898}
1899
Chris Masond352ac62008-09-29 15:18:18 -04001900/*
1901 * each time an IO finishes, we do a fast check in the IO failure tree
1902 * to see if we need to process or clean up an io_failure_record
1903 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001904static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001905{
1906 u64 private;
1907 u64 private_failure;
1908 struct io_failure_record *failure;
1909 int ret;
1910
1911 private = 0;
1912 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1913 (u64)-1, 1, EXTENT_DIRTY)) {
1914 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1915 start, &private_failure);
1916 if (ret == 0) {
1917 failure = (struct io_failure_record *)(unsigned long)
1918 private_failure;
1919 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1920 failure->start, 0);
1921 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1922 failure->start,
1923 failure->start + failure->len - 1,
1924 EXTENT_DIRTY | EXTENT_LOCKED,
1925 GFP_NOFS);
1926 kfree(failure);
1927 }
1928 }
Chris Mason7e383262008-04-09 16:28:12 -04001929 return 0;
1930}
1931
Chris Masond352ac62008-09-29 15:18:18 -04001932/*
1933 * when reads are done, we need to check csums to verify the data is correct
1934 * if there's a match, we allow the bio to finish. If not, we go through
1935 * the io_failure_record routines to find good copies
1936 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001937static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001938 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001939{
Chris Mason35ebb932007-10-30 16:56:53 -04001940 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001941 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001942 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001943 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001944 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001945 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001946 struct btrfs_root *root = BTRFS_I(inode)->root;
1947 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001948
Chris Masond20f7042008-12-08 16:58:54 -05001949 if (PageChecked(page)) {
1950 ClearPageChecked(page);
1951 goto good;
1952 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001953
1954 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Chris Masonb6cda9b2007-12-14 15:30:32 -05001955 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001956
Yan Zheng17d217f2008-12-12 10:03:38 -05001957 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04001958 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001959 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1960 GFP_NOFS);
1961 return 0;
1962 }
1963
Yanc2e639f2008-02-04 08:57:25 -05001964 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001965 private = state->private;
1966 ret = 0;
1967 } else {
1968 ret = get_state_private(io_tree, start, &private);
1969 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001970 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05001971 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001972 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001973
Chris Masonff79f812007-10-15 16:22:25 -04001974 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1975 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001976 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04001977 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001978
Chris Mason9ab86c82009-01-07 09:48:51 -05001979 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05001980good:
Chris Mason7e383262008-04-09 16:28:12 -04001981 /* if the io failure tree for this inode is non-empty,
1982 * check to see if we've recovered from a failed IO
1983 */
Chris Mason1259ab72008-05-12 13:39:03 -04001984 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04001985 return 0;
1986
1987zeroit:
Chris Mason193f2842009-04-27 07:29:05 -04001988 if (printk_ratelimit()) {
1989 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1990 "private %llu\n", page->mapping->host->i_ino,
1991 (unsigned long long)start, csum,
1992 (unsigned long long)private);
1993 }
Chris Masondb945352007-10-15 16:15:53 -04001994 memset(kaddr + offset, 1, end - start + 1);
1995 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05001996 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04001997 if (private == 0)
1998 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04001999 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04002000}
Chris Masonb888db22007-08-27 16:49:44 -04002001
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002002struct delayed_iput {
2003 struct list_head list;
2004 struct inode *inode;
2005};
2006
2007void btrfs_add_delayed_iput(struct inode *inode)
2008{
2009 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2010 struct delayed_iput *delayed;
2011
2012 if (atomic_add_unless(&inode->i_count, -1, 1))
2013 return;
2014
2015 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2016 delayed->inode = inode;
2017
2018 spin_lock(&fs_info->delayed_iput_lock);
2019 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2020 spin_unlock(&fs_info->delayed_iput_lock);
2021}
2022
2023void btrfs_run_delayed_iputs(struct btrfs_root *root)
2024{
2025 LIST_HEAD(list);
2026 struct btrfs_fs_info *fs_info = root->fs_info;
2027 struct delayed_iput *delayed;
2028 int empty;
2029
2030 spin_lock(&fs_info->delayed_iput_lock);
2031 empty = list_empty(&fs_info->delayed_iputs);
2032 spin_unlock(&fs_info->delayed_iput_lock);
2033 if (empty)
2034 return;
2035
2036 down_read(&root->fs_info->cleanup_work_sem);
2037 spin_lock(&fs_info->delayed_iput_lock);
2038 list_splice_init(&fs_info->delayed_iputs, &list);
2039 spin_unlock(&fs_info->delayed_iput_lock);
2040
2041 while (!list_empty(&list)) {
2042 delayed = list_entry(list.next, struct delayed_iput, list);
2043 list_del(&delayed->list);
2044 iput(delayed->inode);
2045 kfree(delayed);
2046 }
2047 up_read(&root->fs_info->cleanup_work_sem);
2048}
2049
Josef Bacik7b128762008-07-24 12:17:14 -04002050/*
Yan, Zhengd68fc572010-05-16 10:49:58 -04002051 * calculate extra metadata reservation when snapshotting a subvolume
2052 * contains orphan files.
2053 */
2054void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle *trans,
2055 struct btrfs_pending_snapshot *pending,
2056 u64 *bytes_to_reserve)
2057{
2058 struct btrfs_root *root;
2059 struct btrfs_block_rsv *block_rsv;
2060 u64 num_bytes;
2061 int index;
2062
2063 root = pending->root;
2064 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2065 return;
2066
2067 block_rsv = root->orphan_block_rsv;
2068
2069 /* orphan block reservation for the snapshot */
2070 num_bytes = block_rsv->size;
2071
2072 /*
2073 * after the snapshot is created, COWing tree blocks may use more
2074 * space than it frees. So we should make sure there is enough
2075 * reserved space.
2076 */
2077 index = trans->transid & 0x1;
2078 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2079 num_bytes += block_rsv->size -
2080 (block_rsv->reserved + block_rsv->freed[index]);
2081 }
2082
2083 *bytes_to_reserve += num_bytes;
2084}
2085
2086void btrfs_orphan_post_snapshot(struct btrfs_trans_handle *trans,
2087 struct btrfs_pending_snapshot *pending)
2088{
2089 struct btrfs_root *root = pending->root;
2090 struct btrfs_root *snap = pending->snap;
2091 struct btrfs_block_rsv *block_rsv;
2092 u64 num_bytes;
2093 int index;
2094 int ret;
2095
2096 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2097 return;
2098
2099 /* refill source subvolume's orphan block reservation */
2100 block_rsv = root->orphan_block_rsv;
2101 index = trans->transid & 0x1;
2102 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2103 num_bytes = block_rsv->size -
2104 (block_rsv->reserved + block_rsv->freed[index]);
2105 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2106 root->orphan_block_rsv,
2107 num_bytes);
2108 BUG_ON(ret);
2109 }
2110
2111 /* setup orphan block reservation for the snapshot */
2112 block_rsv = btrfs_alloc_block_rsv(snap);
2113 BUG_ON(!block_rsv);
2114
2115 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2116 snap->orphan_block_rsv = block_rsv;
2117
2118 num_bytes = root->orphan_block_rsv->size;
2119 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2120 block_rsv, num_bytes);
2121 BUG_ON(ret);
2122
2123#if 0
2124 /* insert orphan item for the snapshot */
2125 WARN_ON(!root->orphan_item_inserted);
2126 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2127 snap->root_key.objectid);
2128 BUG_ON(ret);
2129 snap->orphan_item_inserted = 1;
2130#endif
2131}
2132
2133enum btrfs_orphan_cleanup_state {
2134 ORPHAN_CLEANUP_STARTED = 1,
2135 ORPHAN_CLEANUP_DONE = 2,
2136};
2137
2138/*
2139 * This is called in transaction commmit time. If there are no orphan
2140 * files in the subvolume, it removes orphan item and frees block_rsv
2141 * structure.
2142 */
2143void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2144 struct btrfs_root *root)
2145{
2146 int ret;
2147
2148 if (!list_empty(&root->orphan_list) ||
2149 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2150 return;
2151
2152 if (root->orphan_item_inserted &&
2153 btrfs_root_refs(&root->root_item) > 0) {
2154 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2155 root->root_key.objectid);
2156 BUG_ON(ret);
2157 root->orphan_item_inserted = 0;
2158 }
2159
2160 if (root->orphan_block_rsv) {
2161 WARN_ON(root->orphan_block_rsv->size > 0);
2162 btrfs_free_block_rsv(root, root->orphan_block_rsv);
2163 root->orphan_block_rsv = NULL;
2164 }
2165}
2166
2167/*
Josef Bacik7b128762008-07-24 12:17:14 -04002168 * This creates an orphan entry for the given inode in case something goes
2169 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002170 *
2171 * NOTE: caller of this function should reserve 5 units of metadata for
2172 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002173 */
2174int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2175{
2176 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002177 struct btrfs_block_rsv *block_rsv = NULL;
2178 int reserve = 0;
2179 int insert = 0;
2180 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002181
Yan, Zhengd68fc572010-05-16 10:49:58 -04002182 if (!root->orphan_block_rsv) {
2183 block_rsv = btrfs_alloc_block_rsv(root);
2184 BUG_ON(!block_rsv);
Josef Bacik7b128762008-07-24 12:17:14 -04002185 }
2186
Yan, Zhengd68fc572010-05-16 10:49:58 -04002187 spin_lock(&root->orphan_lock);
2188 if (!root->orphan_block_rsv) {
2189 root->orphan_block_rsv = block_rsv;
2190 } else if (block_rsv) {
2191 btrfs_free_block_rsv(root, block_rsv);
2192 block_rsv = NULL;
2193 }
Josef Bacik7b128762008-07-24 12:17:14 -04002194
Yan, Zhengd68fc572010-05-16 10:49:58 -04002195 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
2196 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2197#if 0
2198 /*
2199 * For proper ENOSPC handling, we should do orphan
2200 * cleanup when mounting. But this introduces backward
2201 * compatibility issue.
2202 */
2203 if (!xchg(&root->orphan_item_inserted, 1))
2204 insert = 2;
2205 else
2206 insert = 1;
2207#endif
2208 insert = 1;
2209 } else {
2210 WARN_ON(!BTRFS_I(inode)->orphan_meta_reserved);
2211 }
Josef Bacik7b128762008-07-24 12:17:14 -04002212
Yan, Zhengd68fc572010-05-16 10:49:58 -04002213 if (!BTRFS_I(inode)->orphan_meta_reserved) {
2214 BTRFS_I(inode)->orphan_meta_reserved = 1;
2215 reserve = 1;
2216 }
2217 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002218
Yan, Zhengd68fc572010-05-16 10:49:58 -04002219 if (block_rsv)
2220 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2221
2222 /* grab metadata reservation from transaction handle */
2223 if (reserve) {
2224 ret = btrfs_orphan_reserve_metadata(trans, inode);
2225 BUG_ON(ret);
2226 }
2227
2228 /* insert an orphan item to track this unlinked/truncated file */
2229 if (insert >= 1) {
2230 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
2231 BUG_ON(ret);
2232 }
2233
2234 /* insert an orphan item to track subvolume contains orphan files */
2235 if (insert >= 2) {
2236 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2237 root->root_key.objectid);
2238 BUG_ON(ret);
2239 }
2240 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002241}
2242
2243/*
2244 * We have done the truncate/delete so we can go ahead and remove the orphan
2245 * item for this particular inode.
2246 */
2247int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2248{
2249 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002250 int delete_item = 0;
2251 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002252 int ret = 0;
2253
Yan, Zhengd68fc572010-05-16 10:49:58 -04002254 spin_lock(&root->orphan_lock);
2255 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
2256 list_del_init(&BTRFS_I(inode)->i_orphan);
2257 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002258 }
2259
Yan, Zhengd68fc572010-05-16 10:49:58 -04002260 if (BTRFS_I(inode)->orphan_meta_reserved) {
2261 BTRFS_I(inode)->orphan_meta_reserved = 0;
2262 release_rsv = 1;
2263 }
2264 spin_unlock(&root->orphan_lock);
2265
2266 if (trans && delete_item) {
2267 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
2268 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04002269 }
2270
Yan, Zhengd68fc572010-05-16 10:49:58 -04002271 if (release_rsv)
2272 btrfs_orphan_release_metadata(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002273
Yan, Zhengd68fc572010-05-16 10:49:58 -04002274 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002275}
2276
2277/*
2278 * this cleans up any orphans that may be left on the list from the last use
2279 * of this root.
2280 */
2281void btrfs_orphan_cleanup(struct btrfs_root *root)
2282{
2283 struct btrfs_path *path;
2284 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002285 struct btrfs_key key, found_key;
2286 struct btrfs_trans_handle *trans;
2287 struct inode *inode;
2288 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2289
Yan, Zhengd68fc572010-05-16 10:49:58 -04002290 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik7b128762008-07-24 12:17:14 -04002291 return;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002292
2293 path = btrfs_alloc_path();
2294 BUG_ON(!path);
Josef Bacik7b128762008-07-24 12:17:14 -04002295 path->reada = -1;
2296
2297 key.objectid = BTRFS_ORPHAN_OBJECTID;
2298 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2299 key.offset = (u64)-1;
2300
Josef Bacik7b128762008-07-24 12:17:14 -04002301 while (1) {
2302 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2303 if (ret < 0) {
2304 printk(KERN_ERR "Error searching slot for orphan: %d"
2305 "\n", ret);
2306 break;
2307 }
2308
2309 /*
2310 * if ret == 0 means we found what we were searching for, which
2311 * is weird, but possible, so only screw with path if we didnt
2312 * find the key and see if we have stuff that matches
2313 */
2314 if (ret > 0) {
2315 if (path->slots[0] == 0)
2316 break;
2317 path->slots[0]--;
2318 }
2319
2320 /* pull out the item */
2321 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002322 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2323
2324 /* make sure the item matches what we want */
2325 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2326 break;
2327 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2328 break;
2329
2330 /* release the path since we're done with it */
2331 btrfs_release_path(root, path);
2332
2333 /*
2334 * this is where we are basically btrfs_lookup, without the
2335 * crossing root thing. we store the inode number in the
2336 * offset of the orphan item.
2337 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002338 found_key.objectid = found_key.offset;
2339 found_key.type = BTRFS_INODE_ITEM_KEY;
2340 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002341 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002342 BUG_ON(IS_ERR(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04002343
Josef Bacik7b128762008-07-24 12:17:14 -04002344 /*
2345 * add this inode to the orphan list so btrfs_orphan_del does
2346 * the proper thing when we hit it
2347 */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002348 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002349 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002350 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002351
2352 /*
2353 * if this is a bad inode, means we actually succeeded in
2354 * removing the inode, but not the orphan record, which means
2355 * we need to manually delete the orphan since iput will just
2356 * do a destroy_inode
2357 */
2358 if (is_bad_inode(inode)) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04002359 trans = btrfs_start_transaction(root, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04002360 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002361 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04002362 iput(inode);
2363 continue;
2364 }
2365
2366 /* if we have links, this was a truncate, lets do that */
2367 if (inode->i_nlink) {
2368 nr_truncate++;
2369 btrfs_truncate(inode);
2370 } else {
2371 nr_unlink++;
2372 }
2373
2374 /* this will do delete_inode and everything for us */
2375 iput(inode);
2376 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002377 btrfs_free_path(path);
2378
2379 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2380
2381 if (root->orphan_block_rsv)
2382 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2383 (u64)-1);
2384
2385 if (root->orphan_block_rsv || root->orphan_item_inserted) {
2386 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00002387 BUG_ON(IS_ERR(trans));
Yan, Zhengd68fc572010-05-16 10:49:58 -04002388 btrfs_end_transaction(trans, root);
2389 }
Josef Bacik7b128762008-07-24 12:17:14 -04002390
2391 if (nr_unlink)
2392 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2393 if (nr_truncate)
2394 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik7b128762008-07-24 12:17:14 -04002395}
2396
Chris Masond352ac62008-09-29 15:18:18 -04002397/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002398 * very simple check to peek ahead in the leaf looking for xattrs. If we
2399 * don't find any xattrs, we know there can't be any acls.
2400 *
2401 * slot is the slot the inode is in, objectid is the objectid of the inode
2402 */
2403static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2404 int slot, u64 objectid)
2405{
2406 u32 nritems = btrfs_header_nritems(leaf);
2407 struct btrfs_key found_key;
2408 int scanned = 0;
2409
2410 slot++;
2411 while (slot < nritems) {
2412 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2413
2414 /* we found a different objectid, there must not be acls */
2415 if (found_key.objectid != objectid)
2416 return 0;
2417
2418 /* we found an xattr, assume we've got an acl */
2419 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2420 return 1;
2421
2422 /*
2423 * we found a key greater than an xattr key, there can't
2424 * be any acls later on
2425 */
2426 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2427 return 0;
2428
2429 slot++;
2430 scanned++;
2431
2432 /*
2433 * it goes inode, inode backrefs, xattrs, extents,
2434 * so if there are a ton of hard links to an inode there can
2435 * be a lot of backrefs. Don't waste time searching too hard,
2436 * this is just an optimization
2437 */
2438 if (scanned >= 8)
2439 break;
2440 }
2441 /* we hit the end of the leaf before we found an xattr or
2442 * something larger than an xattr. We have to assume the inode
2443 * has acls
2444 */
2445 return 1;
2446}
2447
2448/*
Chris Masond352ac62008-09-29 15:18:18 -04002449 * read an inode from the btree into the in-memory inode
2450 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002451static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002452{
2453 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002454 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002455 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002456 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002457 struct btrfs_root *root = BTRFS_I(inode)->root;
2458 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002459 int maybe_acls;
Chris Mason39279cc2007-06-12 06:35:45 -04002460 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04002461 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002462 int ret;
2463
2464 path = btrfs_alloc_path();
2465 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002466 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002467
Chris Mason39279cc2007-06-12 06:35:45 -04002468 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002469 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002470 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002471
Chris Mason5f39d392007-10-15 16:14:19 -04002472 leaf = path->nodes[0];
2473 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2474 struct btrfs_inode_item);
2475
2476 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2477 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2478 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2479 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002480 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002481
2482 tspec = btrfs_inode_atime(inode_item);
2483 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2484 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2485
2486 tspec = btrfs_inode_mtime(inode_item);
2487 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2488 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2489
2490 tspec = btrfs_inode_ctime(inode_item);
2491 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2492 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2493
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002494 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002495 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002496 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002497 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002498 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002499 rdev = btrfs_inode_rdev(leaf, inode_item);
2500
Josef Bacikaec74772008-07-24 12:12:38 -04002501 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002502 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002503
Chris Mason5f39d392007-10-15 16:14:19 -04002504 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002505
Chris Mason46a53cc2009-04-27 11:47:50 -04002506 /*
2507 * try to precache a NULL acl entry for files that don't have
2508 * any xattrs or acls
2509 */
2510 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
Al Viro72c04902009-06-24 16:58:48 -04002511 if (!maybe_acls)
2512 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002513
Yan Zhengd2fb3432008-12-11 16:30:39 -05002514 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2515 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002516 btrfs_free_path(path);
2517 inode_item = NULL;
2518
Chris Mason39279cc2007-06-12 06:35:45 -04002519 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002520 case S_IFREG:
2521 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002522 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002523 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002524 inode->i_fop = &btrfs_file_operations;
2525 inode->i_op = &btrfs_file_inode_operations;
2526 break;
2527 case S_IFDIR:
2528 inode->i_fop = &btrfs_dir_file_operations;
2529 if (root == root->fs_info->tree_root)
2530 inode->i_op = &btrfs_dir_ro_inode_operations;
2531 else
2532 inode->i_op = &btrfs_dir_inode_operations;
2533 break;
2534 case S_IFLNK:
2535 inode->i_op = &btrfs_symlink_inode_operations;
2536 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002537 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002538 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002539 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002540 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002541 init_special_inode(inode, inode->i_mode, rdev);
2542 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002543 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002544
2545 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002546 return;
2547
2548make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002549 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002550 make_bad_inode(inode);
2551}
2552
Chris Masond352ac62008-09-29 15:18:18 -04002553/*
2554 * given a leaf and an inode, copy the inode fields into the leaf
2555 */
Chris Masone02119d2008-09-05 16:13:11 -04002556static void fill_inode_item(struct btrfs_trans_handle *trans,
2557 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002558 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002559 struct inode *inode)
2560{
Chris Mason5f39d392007-10-15 16:14:19 -04002561 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2562 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002563 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002564 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2565 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2566
2567 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2568 inode->i_atime.tv_sec);
2569 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2570 inode->i_atime.tv_nsec);
2571
2572 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2573 inode->i_mtime.tv_sec);
2574 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2575 inode->i_mtime.tv_nsec);
2576
2577 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2578 inode->i_ctime.tv_sec);
2579 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2580 inode->i_ctime.tv_nsec);
2581
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002582 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002583 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002584 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002585 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002586 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002587 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002588 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Chris Mason39279cc2007-06-12 06:35:45 -04002589}
2590
Chris Masond352ac62008-09-29 15:18:18 -04002591/*
2592 * copy everything in the in-memory inode into the btree.
2593 */
Chris Masond3977122009-01-05 21:25:51 -05002594noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2595 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002596{
2597 struct btrfs_inode_item *inode_item;
2598 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002599 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002600 int ret;
2601
2602 path = btrfs_alloc_path();
2603 BUG_ON(!path);
Chris Masonb9473432009-03-13 11:00:37 -04002604 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002605 ret = btrfs_lookup_inode(trans, root, path,
2606 &BTRFS_I(inode)->location, 1);
2607 if (ret) {
2608 if (ret > 0)
2609 ret = -ENOENT;
2610 goto failed;
2611 }
2612
Chris Masonb4ce94d2009-02-04 09:25:08 -05002613 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002614 leaf = path->nodes[0];
2615 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002616 struct btrfs_inode_item);
2617
Chris Masone02119d2008-09-05 16:13:11 -04002618 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002619 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002620 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002621 ret = 0;
2622failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002623 btrfs_free_path(path);
2624 return ret;
2625}
2626
2627
Chris Masond352ac62008-09-29 15:18:18 -04002628/*
2629 * unlink helper that gets used here in inode.c and in the tree logging
2630 * recovery code. It remove a link in a directory with a given name, and
2631 * also drops the back refs in the inode to the directory
2632 */
Chris Masone02119d2008-09-05 16:13:11 -04002633int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2634 struct btrfs_root *root,
2635 struct inode *dir, struct inode *inode,
2636 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002637{
2638 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002639 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002640 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002641 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002642 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002643 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04002644
2645 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002646 if (!path) {
2647 ret = -ENOMEM;
2648 goto err;
2649 }
2650
Chris Masonb9473432009-03-13 11:00:37 -04002651 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002652 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2653 name, name_len, -1);
2654 if (IS_ERR(di)) {
2655 ret = PTR_ERR(di);
2656 goto err;
2657 }
2658 if (!di) {
2659 ret = -ENOENT;
2660 goto err;
2661 }
Chris Mason5f39d392007-10-15 16:14:19 -04002662 leaf = path->nodes[0];
2663 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002664 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002665 if (ret)
2666 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002667 btrfs_release_path(root, path);
2668
Josef Bacikaec74772008-07-24 12:12:38 -04002669 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04002670 inode->i_ino,
2671 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002672 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002673 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Josef Bacikaec74772008-07-24 12:12:38 -04002674 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04002675 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002676 goto err;
2677 }
2678
Chris Mason39279cc2007-06-12 06:35:45 -04002679 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002680 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002681 if (IS_ERR(di)) {
2682 ret = PTR_ERR(di);
2683 goto err;
2684 }
2685 if (!di) {
2686 ret = -ENOENT;
2687 goto err;
2688 }
2689 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002690 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002691
Chris Masone02119d2008-09-05 16:13:11 -04002692 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2693 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002694 BUG_ON(ret != 0 && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04002695
2696 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2697 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002698 if (ret == -ENOENT)
2699 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002700err:
2701 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002702 if (ret)
2703 goto out;
2704
2705 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2706 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2707 btrfs_update_inode(trans, root, dir);
2708 btrfs_drop_nlink(inode);
2709 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002710out:
Chris Mason39279cc2007-06-12 06:35:45 -04002711 return ret;
2712}
2713
Yan, Zhenga22285a2010-05-16 10:48:46 -04002714/* helper to check if there is any shared block in the path */
2715static int check_path_shared(struct btrfs_root *root,
2716 struct btrfs_path *path)
2717{
2718 struct extent_buffer *eb;
2719 int level;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00002720 u64 refs = 1;
Andi Kleen411fc6b2010-10-29 15:14:31 -04002721 int uninitialized_var(ret);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002722
2723 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2724 if (!path->nodes[level])
2725 break;
2726 eb = path->nodes[level];
2727 if (!btrfs_block_can_be_shared(root, eb))
2728 continue;
2729 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2730 &refs, NULL);
2731 if (refs > 1)
2732 return 1;
2733 }
Andi Kleen411fc6b2010-10-29 15:14:31 -04002734 return ret; /* XXX callers? */
Yan, Zhenga22285a2010-05-16 10:48:46 -04002735}
2736
2737/*
2738 * helper to start transaction for unlink and rmdir.
2739 *
2740 * unlink and rmdir are special in btrfs, they do not always free space.
2741 * so in enospc case, we should make sure they will free space before
2742 * allowing them to use the global metadata reservation.
2743 */
2744static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2745 struct dentry *dentry)
2746{
2747 struct btrfs_trans_handle *trans;
2748 struct btrfs_root *root = BTRFS_I(dir)->root;
2749 struct btrfs_path *path;
2750 struct btrfs_inode_ref *ref;
2751 struct btrfs_dir_item *di;
2752 struct inode *inode = dentry->d_inode;
2753 u64 index;
2754 int check_link = 1;
2755 int err = -ENOSPC;
2756 int ret;
2757
2758 trans = btrfs_start_transaction(root, 10);
2759 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2760 return trans;
2761
2762 if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
2763 return ERR_PTR(-ENOSPC);
2764
2765 /* check if there is someone else holds reference */
2766 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2767 return ERR_PTR(-ENOSPC);
2768
2769 if (atomic_read(&inode->i_count) > 2)
2770 return ERR_PTR(-ENOSPC);
2771
2772 if (xchg(&root->fs_info->enospc_unlink, 1))
2773 return ERR_PTR(-ENOSPC);
2774
2775 path = btrfs_alloc_path();
2776 if (!path) {
2777 root->fs_info->enospc_unlink = 0;
2778 return ERR_PTR(-ENOMEM);
2779 }
2780
2781 trans = btrfs_start_transaction(root, 0);
2782 if (IS_ERR(trans)) {
2783 btrfs_free_path(path);
2784 root->fs_info->enospc_unlink = 0;
2785 return trans;
2786 }
2787
2788 path->skip_locking = 1;
2789 path->search_commit_root = 1;
2790
2791 ret = btrfs_lookup_inode(trans, root, path,
2792 &BTRFS_I(dir)->location, 0);
2793 if (ret < 0) {
2794 err = ret;
2795 goto out;
2796 }
2797 if (ret == 0) {
2798 if (check_path_shared(root, path))
2799 goto out;
2800 } else {
2801 check_link = 0;
2802 }
2803 btrfs_release_path(root, path);
2804
2805 ret = btrfs_lookup_inode(trans, root, path,
2806 &BTRFS_I(inode)->location, 0);
2807 if (ret < 0) {
2808 err = ret;
2809 goto out;
2810 }
2811 if (ret == 0) {
2812 if (check_path_shared(root, path))
2813 goto out;
2814 } else {
2815 check_link = 0;
2816 }
2817 btrfs_release_path(root, path);
2818
2819 if (ret == 0 && S_ISREG(inode->i_mode)) {
2820 ret = btrfs_lookup_file_extent(trans, root, path,
2821 inode->i_ino, (u64)-1, 0);
2822 if (ret < 0) {
2823 err = ret;
2824 goto out;
2825 }
2826 BUG_ON(ret == 0);
2827 if (check_path_shared(root, path))
2828 goto out;
2829 btrfs_release_path(root, path);
2830 }
2831
2832 if (!check_link) {
2833 err = 0;
2834 goto out;
2835 }
2836
2837 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2838 dentry->d_name.name, dentry->d_name.len, 0);
2839 if (IS_ERR(di)) {
2840 err = PTR_ERR(di);
2841 goto out;
2842 }
2843 if (di) {
2844 if (check_path_shared(root, path))
2845 goto out;
2846 } else {
2847 err = 0;
2848 goto out;
2849 }
2850 btrfs_release_path(root, path);
2851
2852 ref = btrfs_lookup_inode_ref(trans, root, path,
2853 dentry->d_name.name, dentry->d_name.len,
2854 inode->i_ino, dir->i_ino, 0);
2855 if (IS_ERR(ref)) {
2856 err = PTR_ERR(ref);
2857 goto out;
2858 }
2859 BUG_ON(!ref);
2860 if (check_path_shared(root, path))
2861 goto out;
2862 index = btrfs_inode_ref_index(path->nodes[0], ref);
2863 btrfs_release_path(root, path);
2864
2865 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index,
2866 dentry->d_name.name, dentry->d_name.len, 0);
2867 if (IS_ERR(di)) {
2868 err = PTR_ERR(di);
2869 goto out;
2870 }
2871 BUG_ON(ret == -ENOENT);
2872 if (check_path_shared(root, path))
2873 goto out;
2874
2875 err = 0;
2876out:
2877 btrfs_free_path(path);
2878 if (err) {
2879 btrfs_end_transaction(trans, root);
2880 root->fs_info->enospc_unlink = 0;
2881 return ERR_PTR(err);
2882 }
2883
2884 trans->block_rsv = &root->fs_info->global_block_rsv;
2885 return trans;
2886}
2887
2888static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2889 struct btrfs_root *root)
2890{
2891 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
2892 BUG_ON(!root->fs_info->enospc_unlink);
2893 root->fs_info->enospc_unlink = 0;
2894 }
2895 btrfs_end_transaction_throttle(trans, root);
2896}
2897
Chris Mason39279cc2007-06-12 06:35:45 -04002898static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2899{
Yan, Zhenga22285a2010-05-16 10:48:46 -04002900 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002901 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002902 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002903 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002904 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002905
Yan, Zhenga22285a2010-05-16 10:48:46 -04002906 trans = __unlink_start_trans(dir, dentry);
2907 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002908 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04002909
Chris Mason39279cc2007-06-12 06:35:45 -04002910 btrfs_set_trans_block_group(trans, dir);
Chris Mason12fcfd22009-03-24 10:24:20 -04002911
2912 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2913
Chris Masone02119d2008-09-05 16:13:11 -04002914 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2915 dentry->d_name.name, dentry->d_name.len);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002916 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04002917
Yan, Zhenga22285a2010-05-16 10:48:46 -04002918 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04002919 ret = btrfs_orphan_add(trans, inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002920 BUG_ON(ret);
2921 }
Josef Bacik7b128762008-07-24 12:17:14 -04002922
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002923 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002924 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002925 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002926 return ret;
2927}
2928
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002929int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2930 struct btrfs_root *root,
2931 struct inode *dir, u64 objectid,
2932 const char *name, int name_len)
2933{
2934 struct btrfs_path *path;
2935 struct extent_buffer *leaf;
2936 struct btrfs_dir_item *di;
2937 struct btrfs_key key;
2938 u64 index;
2939 int ret;
2940
2941 path = btrfs_alloc_path();
2942 if (!path)
2943 return -ENOMEM;
2944
2945 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2946 name, name_len, -1);
2947 BUG_ON(!di || IS_ERR(di));
2948
2949 leaf = path->nodes[0];
2950 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2951 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2952 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2953 BUG_ON(ret);
2954 btrfs_release_path(root, path);
2955
2956 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2957 objectid, root->root_key.objectid,
2958 dir->i_ino, &index, name, name_len);
2959 if (ret < 0) {
2960 BUG_ON(ret != -ENOENT);
2961 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
2962 name, name_len);
2963 BUG_ON(!di || IS_ERR(di));
2964
2965 leaf = path->nodes[0];
2966 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2967 btrfs_release_path(root, path);
2968 index = key.offset;
2969 }
2970
2971 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2972 index, name, name_len, -1);
2973 BUG_ON(!di || IS_ERR(di));
2974
2975 leaf = path->nodes[0];
2976 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2977 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2978 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2979 BUG_ON(ret);
2980 btrfs_release_path(root, path);
2981
2982 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2983 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2984 ret = btrfs_update_inode(trans, root, dir);
2985 BUG_ON(ret);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002986
2987 btrfs_free_path(path);
2988 return 0;
2989}
2990
Chris Mason39279cc2007-06-12 06:35:45 -04002991static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2992{
2993 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002994 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002995 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002996 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05002997 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002998
Chris Mason3394e162008-11-17 20:42:26 -05002999 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003000 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04003001 return -ENOTEMPTY;
3002
Yan, Zhenga22285a2010-05-16 10:48:46 -04003003 trans = __unlink_start_trans(dir, dentry);
3004 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003005 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003006
Chris Mason39279cc2007-06-12 06:35:45 -04003007 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003008
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003009 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
3010 err = btrfs_unlink_subvol(trans, root, dir,
3011 BTRFS_I(inode)->location.objectid,
3012 dentry->d_name.name,
3013 dentry->d_name.len);
3014 goto out;
3015 }
3016
Josef Bacik7b128762008-07-24 12:17:14 -04003017 err = btrfs_orphan_add(trans, inode);
3018 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003019 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003020
Chris Mason39279cc2007-06-12 06:35:45 -04003021 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003022 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3023 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003024 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003025 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003026out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003027 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003028 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003029 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05003030
Chris Mason39279cc2007-06-12 06:35:45 -04003031 return err;
3032}
3033
Chris Masond20f7042008-12-08 16:58:54 -05003034#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04003035/*
Chris Mason323ac952008-10-01 19:05:46 -04003036 * when truncating bytes in a file, it is possible to avoid reading
3037 * the leaves that contain only checksum items. This can be the
3038 * majority of the IO required to delete a large file, but it must
3039 * be done carefully.
3040 *
3041 * The keys in the level just above the leaves are checked to make sure
3042 * the lowest key in a given leaf is a csum key, and starts at an offset
3043 * after the new size.
3044 *
3045 * Then the key for the next leaf is checked to make sure it also has
3046 * a checksum item for the same file. If it does, we know our target leaf
3047 * contains only checksum items, and it can be safely freed without reading
3048 * it.
3049 *
3050 * This is just an optimization targeted at large files. It may do
3051 * nothing. It will return 0 unless things went badly.
3052 */
3053static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
3054 struct btrfs_root *root,
3055 struct btrfs_path *path,
3056 struct inode *inode, u64 new_size)
3057{
3058 struct btrfs_key key;
3059 int ret;
3060 int nritems;
3061 struct btrfs_key found_key;
3062 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003063 struct btrfs_leaf_ref *ref;
3064 u64 leaf_gen;
3065 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04003066
3067 path->lowest_level = 1;
3068 key.objectid = inode->i_ino;
3069 key.type = BTRFS_CSUM_ITEM_KEY;
3070 key.offset = new_size;
3071again:
3072 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3073 if (ret < 0)
3074 goto out;
3075
3076 if (path->nodes[1] == NULL) {
3077 ret = 0;
3078 goto out;
3079 }
3080 ret = 0;
3081 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
3082 nritems = btrfs_header_nritems(path->nodes[1]);
3083
3084 if (!nritems)
3085 goto out;
3086
3087 if (path->slots[1] >= nritems)
3088 goto next_node;
3089
3090 /* did we find a key greater than anything we want to delete? */
3091 if (found_key.objectid > inode->i_ino ||
3092 (found_key.objectid == inode->i_ino && found_key.type > key.type))
3093 goto out;
3094
3095 /* we check the next key in the node to make sure the leave contains
3096 * only checksum items. This comparison doesn't work if our
3097 * leaf is the last one in the node
3098 */
3099 if (path->slots[1] + 1 >= nritems) {
3100next_node:
3101 /* search forward from the last key in the node, this
3102 * will bring us into the next node in the tree
3103 */
3104 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
3105
3106 /* unlikely, but we inc below, so check to be safe */
3107 if (found_key.offset == (u64)-1)
3108 goto out;
3109
3110 /* search_forward needs a path with locks held, do the
3111 * search again for the original key. It is possible
3112 * this will race with a balance and return a path that
3113 * we could modify, but this drop is just an optimization
3114 * and is allowed to miss some leaves.
3115 */
3116 btrfs_release_path(root, path);
3117 found_key.offset++;
3118
3119 /* setup a max key for search_forward */
3120 other_key.offset = (u64)-1;
3121 other_key.type = key.type;
3122 other_key.objectid = key.objectid;
3123
3124 path->keep_locks = 1;
3125 ret = btrfs_search_forward(root, &found_key, &other_key,
3126 path, 0, 0);
3127 path->keep_locks = 0;
3128 if (ret || found_key.objectid != key.objectid ||
3129 found_key.type != key.type) {
3130 ret = 0;
3131 goto out;
3132 }
3133
3134 key.offset = found_key.offset;
3135 btrfs_release_path(root, path);
3136 cond_resched();
3137 goto again;
3138 }
3139
3140 /* we know there's one more slot after us in the tree,
3141 * read that key so we can verify it is also a checksum item
3142 */
3143 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
3144
3145 if (found_key.objectid < inode->i_ino)
3146 goto next_key;
3147
3148 if (found_key.type != key.type || found_key.offset < new_size)
3149 goto next_key;
3150
3151 /*
3152 * if the key for the next leaf isn't a csum key from this objectid,
3153 * we can't be sure there aren't good items inside this leaf.
3154 * Bail out
3155 */
3156 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
3157 goto out;
3158
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003159 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
3160 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04003161 /*
3162 * it is safe to delete this leaf, it contains only
3163 * csum items from this inode at an offset >= new_size
3164 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003165 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04003166 BUG_ON(ret);
3167
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003168 if (root->ref_cows && leaf_gen < trans->transid) {
3169 ref = btrfs_alloc_leaf_ref(root, 0);
3170 if (ref) {
3171 ref->root_gen = root->root_key.offset;
3172 ref->bytenr = leaf_start;
3173 ref->owner = 0;
3174 ref->generation = leaf_gen;
3175 ref->nritems = 0;
3176
Chris Masonbd56b302009-02-04 09:27:02 -05003177 btrfs_sort_leaf_ref(ref);
3178
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003179 ret = btrfs_add_leaf_ref(root, ref, 0);
3180 WARN_ON(ret);
3181 btrfs_free_leaf_ref(root, ref);
3182 } else {
3183 WARN_ON(1);
3184 }
3185 }
Chris Mason323ac952008-10-01 19:05:46 -04003186next_key:
3187 btrfs_release_path(root, path);
3188
3189 if (other_key.objectid == inode->i_ino &&
3190 other_key.type == key.type && other_key.offset > key.offset) {
3191 key.offset = other_key.offset;
3192 cond_resched();
3193 goto again;
3194 }
3195 ret = 0;
3196out:
3197 /* fixup any changes we've made to the path */
3198 path->lowest_level = 0;
3199 path->keep_locks = 0;
3200 btrfs_release_path(root, path);
3201 return ret;
3202}
3203
Chris Masond20f7042008-12-08 16:58:54 -05003204#endif
3205
Chris Mason323ac952008-10-01 19:05:46 -04003206/*
Chris Mason39279cc2007-06-12 06:35:45 -04003207 * this can truncate away extent items, csum items and directory items.
3208 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003209 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003210 *
3211 * csum items that cross the new i_size are truncated to the new size
3212 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003213 *
3214 * min_type is the minimum key type to truncate down to. If set to 0, this
3215 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003216 */
Yan, Zheng80825102009-11-12 09:35:36 +00003217int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3218 struct btrfs_root *root,
3219 struct inode *inode,
3220 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003221{
Chris Mason39279cc2007-06-12 06:35:45 -04003222 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003223 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003224 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003225 struct btrfs_key key;
3226 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003227 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003228 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003229 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003230 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003231 u64 mask = root->sectorsize - 1;
3232 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003233 int found_extent;
3234 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003235 int pending_del_nr = 0;
3236 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003237 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05003238 int encoding;
Yan, Zheng80825102009-11-12 09:35:36 +00003239 int ret;
3240 int err = 0;
3241
3242 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003243
Josef Bacik0af3d002010-06-21 14:48:16 -04003244 if (root->ref_cows || root == root->fs_info->tree_root)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003245 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003246
Chris Mason39279cc2007-06-12 06:35:45 -04003247 path = btrfs_alloc_path();
3248 BUG_ON(!path);
Julia Lawall33c17ad2009-07-22 16:49:01 -04003249 path->reada = -1;
Chris Mason5f39d392007-10-15 16:14:19 -04003250
Chris Mason39279cc2007-06-12 06:35:45 -04003251 key.objectid = inode->i_ino;
3252 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003253 key.type = (u8)-1;
3254
Chris Mason85e21ba2008-01-29 15:11:36 -05003255search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003256 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003257 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003258 if (ret < 0) {
3259 err = ret;
3260 goto out;
3261 }
Chris Masond3977122009-01-05 21:25:51 -05003262
Chris Mason85e21ba2008-01-29 15:11:36 -05003263 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003264 /* there are no items in the tree for us to truncate, we're
3265 * done
3266 */
Yan, Zheng80825102009-11-12 09:35:36 +00003267 if (path->slots[0] == 0)
3268 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003269 path->slots[0]--;
3270 }
3271
Chris Masond3977122009-01-05 21:25:51 -05003272 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003273 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003274 leaf = path->nodes[0];
3275 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3276 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05003277 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003278
Chris Mason5f39d392007-10-15 16:14:19 -04003279 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003280 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003281
Chris Mason85e21ba2008-01-29 15:11:36 -05003282 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003283 break;
3284
Chris Mason5f39d392007-10-15 16:14:19 -04003285 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003286 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003287 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003288 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003289 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003290 encoding = btrfs_file_extent_compression(leaf, fi);
3291 encoding |= btrfs_file_extent_encryption(leaf, fi);
3292 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
3293
Chris Mason179e29e2007-11-01 11:28:41 -04003294 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003295 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003296 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003297 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003298 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003299 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003300 }
Yan008630c2007-11-07 13:31:09 -05003301 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003302 }
Yan, Zheng80825102009-11-12 09:35:36 +00003303 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003304 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003305 } else {
3306 if (item_end < new_size)
3307 break;
3308 if (found_key.offset >= new_size)
3309 del_item = 1;
3310 else
3311 del_item = 0;
3312 }
Chris Mason39279cc2007-06-12 06:35:45 -04003313 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003314 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003315 if (found_type != BTRFS_EXTENT_DATA_KEY)
3316 goto delete;
3317
3318 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003319 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003320 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003321 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04003322 u64 orig_num_bytes =
3323 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003324 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003325 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003326 extent_num_bytes = extent_num_bytes &
3327 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003328 btrfs_set_file_extent_num_bytes(leaf, fi,
3329 extent_num_bytes);
3330 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003331 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003332 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003333 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003334 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003335 } else {
Chris Masondb945352007-10-15 16:15:53 -04003336 extent_num_bytes =
3337 btrfs_file_extent_disk_num_bytes(leaf,
3338 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003339 extent_offset = found_key.offset -
3340 btrfs_file_extent_offset(leaf, fi);
3341
Chris Mason39279cc2007-06-12 06:35:45 -04003342 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003343 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003344 if (extent_start != 0) {
3345 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003346 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003347 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003348 }
3349 }
Chris Mason90692182008-02-08 13:49:28 -05003350 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003351 /*
3352 * we can't truncate inline items that have had
3353 * special encodings
3354 */
3355 if (!del_item &&
3356 btrfs_file_extent_compression(leaf, fi) == 0 &&
3357 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3358 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003359 u32 size = new_size - found_key.offset;
3360
3361 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003362 inode_sub_bytes(inode, item_end + 1 -
3363 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003364 }
3365 size =
3366 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05003367 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04003368 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05003369 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04003370 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003371 inode_sub_bytes(inode, item_end + 1 -
3372 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003373 }
Chris Mason39279cc2007-06-12 06:35:45 -04003374 }
Chris Mason179e29e2007-11-01 11:28:41 -04003375delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003376 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003377 if (!pending_del_nr) {
3378 /* no pending yet, add ourselves */
3379 pending_del_slot = path->slots[0];
3380 pending_del_nr = 1;
3381 } else if (pending_del_nr &&
3382 path->slots[0] + 1 == pending_del_slot) {
3383 /* hop on the pending chunk */
3384 pending_del_nr++;
3385 pending_del_slot = path->slots[0];
3386 } else {
Chris Masond3977122009-01-05 21:25:51 -05003387 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003388 }
Chris Mason39279cc2007-06-12 06:35:45 -04003389 } else {
3390 break;
3391 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003392 if (found_extent && (root->ref_cows ||
3393 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003394 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003395 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003396 extent_num_bytes, 0,
3397 btrfs_header_owner(leaf),
3398 inode->i_ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04003399 BUG_ON(ret);
3400 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003401
Yan, Zheng80825102009-11-12 09:35:36 +00003402 if (found_type == BTRFS_INODE_ITEM_KEY)
3403 break;
3404
3405 if (path->slots[0] == 0 ||
3406 path->slots[0] != pending_del_slot) {
3407 if (root->ref_cows) {
3408 err = -EAGAIN;
3409 goto out;
3410 }
3411 if (pending_del_nr) {
3412 ret = btrfs_del_items(trans, root, path,
3413 pending_del_slot,
3414 pending_del_nr);
3415 BUG_ON(ret);
3416 pending_del_nr = 0;
3417 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003418 btrfs_release_path(root, path);
3419 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003420 } else {
3421 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003422 }
Chris Mason39279cc2007-06-12 06:35:45 -04003423 }
Yan, Zheng80825102009-11-12 09:35:36 +00003424out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003425 if (pending_del_nr) {
3426 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3427 pending_del_nr);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003428 BUG_ON(ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003429 }
Chris Mason39279cc2007-06-12 06:35:45 -04003430 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003431 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003432}
3433
Chris Masona52d9a82007-08-27 16:49:44 -04003434/*
3435 * taken from block_truncate_page, but does cow as it zeros out
3436 * any bytes left in the last page in the file.
3437 */
3438static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3439{
3440 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003441 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003442 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3443 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003444 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003445 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003446 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003447 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3448 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3449 struct page *page;
3450 int ret = 0;
3451 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003452 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003453
3454 if ((offset & (blocksize - 1)) == 0)
3455 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003456 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003457 if (ret)
3458 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003459
3460 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003461again:
Chris Masona52d9a82007-08-27 16:49:44 -04003462 page = grab_cache_page(mapping, index);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003463 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003464 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04003465 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003466 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003467
3468 page_start = page_offset(page);
3469 page_end = page_start + PAGE_CACHE_SIZE - 1;
3470
Chris Masona52d9a82007-08-27 16:49:44 -04003471 if (!PageUptodate(page)) {
3472 ret = btrfs_readpage(NULL, page);
3473 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003474 if (page->mapping != mapping) {
3475 unlock_page(page);
3476 page_cache_release(page);
3477 goto again;
3478 }
Chris Masona52d9a82007-08-27 16:49:44 -04003479 if (!PageUptodate(page)) {
3480 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003481 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003482 }
3483 }
Chris Mason211c17f2008-05-15 09:13:45 -04003484 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003485
Josef Bacik2ac55d42010-02-03 19:33:23 +00003486 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
3487 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003488 set_page_extent_mapped(page);
3489
3490 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3491 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003492 unlock_extent_cached(io_tree, page_start, page_end,
3493 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003494 unlock_page(page);
3495 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003496 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003497 btrfs_put_ordered_extent(ordered);
3498 goto again;
3499 }
3500
Josef Bacik2ac55d42010-02-03 19:33:23 +00003501 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik5d5e1032009-10-13 16:46:49 -04003502 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003503 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003504
Josef Bacik2ac55d42010-02-03 19:33:23 +00003505 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3506 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003507 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003508 unlock_extent_cached(io_tree, page_start, page_end,
3509 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003510 goto out_unlock;
3511 }
3512
Chris Masone6dcd2d2008-07-17 12:53:50 -04003513 ret = 0;
3514 if (offset != PAGE_CACHE_SIZE) {
3515 kaddr = kmap(page);
3516 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3517 flush_dcache_page(page);
3518 kunmap(page);
3519 }
Chris Mason247e7432008-07-17 12:53:51 -04003520 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003521 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003522 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3523 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003524
Chris Mason89642222008-07-24 09:41:53 -04003525out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003526 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003527 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003528 unlock_page(page);
3529 page_cache_release(page);
3530out:
3531 return ret;
3532}
3533
Yan Zheng9036c102008-10-30 14:19:41 -04003534int btrfs_cont_expand(struct inode *inode, loff_t size)
3535{
3536 struct btrfs_trans_handle *trans;
3537 struct btrfs_root *root = BTRFS_I(inode)->root;
3538 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003539 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003540 struct extent_state *cached_state = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003541 u64 mask = root->sectorsize - 1;
3542 u64 hole_start = (inode->i_size + mask) & ~mask;
3543 u64 block_end = (size + mask) & ~mask;
3544 u64 last_byte;
3545 u64 cur_offset;
3546 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003547 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003548
3549 if (size <= hole_start)
3550 return 0;
3551
Yan Zheng9036c102008-10-30 14:19:41 -04003552 while (1) {
3553 struct btrfs_ordered_extent *ordered;
3554 btrfs_wait_ordered_range(inode, hole_start,
3555 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003556 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
3557 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003558 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3559 if (!ordered)
3560 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003561 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3562 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003563 btrfs_put_ordered_extent(ordered);
3564 }
3565
Yan Zheng9036c102008-10-30 14:19:41 -04003566 cur_offset = hole_start;
3567 while (1) {
3568 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3569 block_end - cur_offset, 0);
3570 BUG_ON(IS_ERR(em) || !em);
3571 last_byte = min(extent_map_end(em), block_end);
3572 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003573 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003574 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003575 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003576
Yan, Zhenga22285a2010-05-16 10:48:46 -04003577 trans = btrfs_start_transaction(root, 2);
3578 if (IS_ERR(trans)) {
3579 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003580 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003581 }
Yan, Zheng80825102009-11-12 09:35:36 +00003582 btrfs_set_trans_block_group(trans, inode);
3583
3584 err = btrfs_drop_extents(trans, inode, cur_offset,
3585 cur_offset + hole_size,
3586 &hint_byte, 1);
3587 BUG_ON(err);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003588
Yan Zheng9036c102008-10-30 14:19:41 -04003589 err = btrfs_insert_file_extent(trans, root,
3590 inode->i_ino, cur_offset, 0,
3591 0, hole_size, 0, hole_size,
3592 0, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003593 BUG_ON(err);
3594
Yan Zheng9036c102008-10-30 14:19:41 -04003595 btrfs_drop_extent_cache(inode, hole_start,
3596 last_byte - 1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003597
3598 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003599 }
3600 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003601 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003602 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003603 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003604 break;
3605 }
3606
Yan, Zhenga22285a2010-05-16 10:48:46 -04003607 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003608 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3609 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003610 return err;
3611}
3612
Yan, Zheng80825102009-11-12 09:35:36 +00003613static int btrfs_setattr_size(struct inode *inode, struct iattr *attr)
3614{
3615 struct btrfs_root *root = BTRFS_I(inode)->root;
3616 struct btrfs_trans_handle *trans;
3617 unsigned long nr;
3618 int ret;
3619
3620 if (attr->ia_size == inode->i_size)
3621 return 0;
3622
3623 if (attr->ia_size > inode->i_size) {
3624 unsigned long limit;
3625 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
3626 if (attr->ia_size > inode->i_sb->s_maxbytes)
3627 return -EFBIG;
3628 if (limit != RLIM_INFINITY && attr->ia_size > limit) {
3629 send_sig(SIGXFSZ, current, 0);
3630 return -EFBIG;
3631 }
3632 }
3633
Yan, Zhengd68fc572010-05-16 10:49:58 -04003634 trans = btrfs_start_transaction(root, 5);
3635 if (IS_ERR(trans))
3636 return PTR_ERR(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00003637
Yan, Zheng80825102009-11-12 09:35:36 +00003638 btrfs_set_trans_block_group(trans, inode);
3639
3640 ret = btrfs_orphan_add(trans, inode);
3641 BUG_ON(ret);
3642
3643 nr = trans->blocks_used;
3644 btrfs_end_transaction(trans, root);
Yan, Zheng80825102009-11-12 09:35:36 +00003645 btrfs_btree_balance_dirty(root, nr);
3646
3647 if (attr->ia_size > inode->i_size) {
3648 ret = btrfs_cont_expand(inode, attr->ia_size);
3649 if (ret) {
3650 btrfs_truncate(inode);
3651 return ret;
3652 }
3653
3654 i_size_write(inode, attr->ia_size);
3655 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
3656
Yan, Zhengd68fc572010-05-16 10:49:58 -04003657 trans = btrfs_start_transaction(root, 0);
3658 BUG_ON(IS_ERR(trans));
Yan, Zheng80825102009-11-12 09:35:36 +00003659 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003660 trans->block_rsv = root->orphan_block_rsv;
3661 BUG_ON(!trans->block_rsv);
Yan, Zheng80825102009-11-12 09:35:36 +00003662
3663 ret = btrfs_update_inode(trans, root, inode);
3664 BUG_ON(ret);
3665 if (inode->i_nlink > 0) {
3666 ret = btrfs_orphan_del(trans, inode);
3667 BUG_ON(ret);
3668 }
3669 nr = trans->blocks_used;
3670 btrfs_end_transaction(trans, root);
3671 btrfs_btree_balance_dirty(root, nr);
3672 return 0;
3673 }
3674
3675 /*
3676 * We're truncating a file that used to have good data down to
3677 * zero. Make sure it gets into the ordered flush list so that
3678 * any new writes get down to disk quickly.
3679 */
3680 if (attr->ia_size == 0)
3681 BTRFS_I(inode)->ordered_data_close = 1;
3682
3683 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3684 ret = vmtruncate(inode, attr->ia_size);
3685 BUG_ON(ret);
3686
3687 return 0;
3688}
3689
Chris Mason39279cc2007-06-12 06:35:45 -04003690static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3691{
3692 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003693 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003694 int err;
3695
Li Zefanb83cc962010-12-20 16:04:08 +08003696 if (btrfs_root_readonly(root))
3697 return -EROFS;
3698
Chris Mason39279cc2007-06-12 06:35:45 -04003699 err = inode_change_ok(inode, attr);
3700 if (err)
3701 return err;
3702
Chris Mason5a3f23d2009-03-31 13:27:11 -04003703 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Yan, Zheng80825102009-11-12 09:35:36 +00003704 err = btrfs_setattr_size(inode, attr);
3705 if (err)
3706 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003707 }
Yan Zheng9036c102008-10-30 14:19:41 -04003708
Christoph Hellwig10257742010-06-04 11:30:02 +02003709 if (attr->ia_valid) {
3710 setattr_copy(inode, attr);
3711 mark_inode_dirty(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003712
Christoph Hellwig10257742010-06-04 11:30:02 +02003713 if (attr->ia_valid & ATTR_MODE)
3714 err = btrfs_acl_chmod(inode);
3715 }
3716
Chris Mason39279cc2007-06-12 06:35:45 -04003717 return err;
3718}
Chris Mason61295eb2008-01-14 16:24:38 -05003719
Al Virobd555972010-06-07 11:35:40 -04003720void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003721{
3722 struct btrfs_trans_handle *trans;
3723 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003724 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003725 int ret;
3726
3727 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003728 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
3729 root == root->fs_info->tree_root))
Al Virobd555972010-06-07 11:35:40 -04003730 goto no_delete;
3731
Chris Mason39279cc2007-06-12 06:35:45 -04003732 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003733 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003734 goto no_delete;
3735 }
Al Virobd555972010-06-07 11:35:40 -04003736 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003737 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003738
Yan, Zhengc71bf092009-11-12 09:34:40 +00003739 if (root->fs_info->log_root_recovering) {
3740 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3741 goto no_delete;
3742 }
3743
Yan, Zheng76dda932009-09-21 16:00:26 -04003744 if (inode->i_nlink > 0) {
3745 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3746 goto no_delete;
3747 }
3748
Chris Masondbe674a2008-07-17 12:54:05 -04003749 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003750
Yan, Zheng80825102009-11-12 09:35:36 +00003751 while (1) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003752 trans = btrfs_start_transaction(root, 0);
3753 BUG_ON(IS_ERR(trans));
Yan, Zheng80825102009-11-12 09:35:36 +00003754 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003755 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003756
Yan, Zhengd68fc572010-05-16 10:49:58 -04003757 ret = btrfs_block_rsv_check(trans, root,
3758 root->orphan_block_rsv, 0, 5);
3759 if (ret) {
3760 BUG_ON(ret != -EAGAIN);
3761 ret = btrfs_commit_transaction(trans, root);
3762 BUG_ON(ret);
3763 continue;
3764 }
3765
3766 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003767 if (ret != -EAGAIN)
3768 break;
3769
3770 nr = trans->blocks_used;
3771 btrfs_end_transaction(trans, root);
3772 trans = NULL;
3773 btrfs_btree_balance_dirty(root, nr);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003774
Josef Bacik7b128762008-07-24 12:17:14 -04003775 }
3776
Yan, Zheng80825102009-11-12 09:35:36 +00003777 if (ret == 0) {
3778 ret = btrfs_orphan_del(trans, inode);
3779 BUG_ON(ret);
3780 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003781
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003782 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003783 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003784 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003785no_delete:
Al Virobd555972010-06-07 11:35:40 -04003786 end_writeback(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003787 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003788}
3789
3790/*
3791 * this returns the key found in the dir entry in the location pointer.
3792 * If no dir entries were found, location->objectid is 0.
3793 */
3794static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3795 struct btrfs_key *location)
3796{
3797 const char *name = dentry->d_name.name;
3798 int namelen = dentry->d_name.len;
3799 struct btrfs_dir_item *di;
3800 struct btrfs_path *path;
3801 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003802 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003803
3804 path = btrfs_alloc_path();
3805 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05003806
Chris Mason39279cc2007-06-12 06:35:45 -04003807 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
3808 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003809 if (IS_ERR(di))
3810 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003811
3812 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05003813 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003814
Chris Mason5f39d392007-10-15 16:14:19 -04003815 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003816out:
Chris Mason39279cc2007-06-12 06:35:45 -04003817 btrfs_free_path(path);
3818 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003819out_err:
3820 location->objectid = 0;
3821 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003822}
3823
3824/*
3825 * when we hit a tree root in a directory, the btrfs part of the inode
3826 * needs to be changed to reflect the root directory of the tree root. This
3827 * is kind of like crossing a mount point.
3828 */
3829static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003830 struct inode *dir,
3831 struct dentry *dentry,
3832 struct btrfs_key *location,
3833 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003834{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003835 struct btrfs_path *path;
3836 struct btrfs_root *new_root;
3837 struct btrfs_root_ref *ref;
3838 struct extent_buffer *leaf;
3839 int ret;
3840 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003841
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003842 path = btrfs_alloc_path();
3843 if (!path) {
3844 err = -ENOMEM;
3845 goto out;
3846 }
Chris Mason39279cc2007-06-12 06:35:45 -04003847
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003848 err = -ENOENT;
3849 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3850 BTRFS_I(dir)->root->root_key.objectid,
3851 location->objectid);
3852 if (ret) {
3853 if (ret < 0)
3854 err = ret;
3855 goto out;
3856 }
Chris Mason39279cc2007-06-12 06:35:45 -04003857
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003858 leaf = path->nodes[0];
3859 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3860 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
3861 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3862 goto out;
3863
3864 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3865 (unsigned long)(ref + 1),
3866 dentry->d_name.len);
3867 if (ret)
3868 goto out;
3869
3870 btrfs_release_path(root->fs_info->tree_root, path);
3871
3872 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3873 if (IS_ERR(new_root)) {
3874 err = PTR_ERR(new_root);
3875 goto out;
3876 }
3877
3878 if (btrfs_root_refs(&new_root->root_item) == 0) {
3879 err = -ENOENT;
3880 goto out;
3881 }
3882
3883 *sub_root = new_root;
3884 location->objectid = btrfs_root_dirid(&new_root->root_item);
3885 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003886 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003887 err = 0;
3888out:
3889 btrfs_free_path(path);
3890 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003891}
3892
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003893static void inode_tree_add(struct inode *inode)
3894{
3895 struct btrfs_root *root = BTRFS_I(inode)->root;
3896 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003897 struct rb_node **p;
3898 struct rb_node *parent;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003899again:
3900 p = &root->inode_tree.rb_node;
3901 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003902
Yan, Zheng76dda932009-09-21 16:00:26 -04003903 if (hlist_unhashed(&inode->i_hash))
3904 return;
3905
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003906 spin_lock(&root->inode_lock);
3907 while (*p) {
3908 parent = *p;
3909 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3910
3911 if (inode->i_ino < entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003912 p = &parent->rb_left;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003913 else if (inode->i_ino > entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003914 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003915 else {
3916 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04003917 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003918 rb_erase(parent, &root->inode_tree);
3919 RB_CLEAR_NODE(parent);
3920 spin_unlock(&root->inode_lock);
3921 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003922 }
3923 }
3924 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3925 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3926 spin_unlock(&root->inode_lock);
3927}
3928
3929static void inode_tree_del(struct inode *inode)
3930{
3931 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003932 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003933
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003934 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003935 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003936 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003937 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003938 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003939 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003940 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003941
Josef Bacik0af3d002010-06-21 14:48:16 -04003942 /*
3943 * Free space cache has inodes in the tree root, but the tree root has a
3944 * root_refs of 0, so this could end up dropping the tree root as a
3945 * snapshot, so we need the extra !root->fs_info->tree_root check to
3946 * make sure we don't drop it.
3947 */
3948 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
3949 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04003950 synchronize_srcu(&root->fs_info->subvol_srcu);
3951 spin_lock(&root->inode_lock);
3952 empty = RB_EMPTY_ROOT(&root->inode_tree);
3953 spin_unlock(&root->inode_lock);
3954 if (empty)
3955 btrfs_add_dead_root(root);
3956 }
3957}
3958
3959int btrfs_invalidate_inodes(struct btrfs_root *root)
3960{
3961 struct rb_node *node;
3962 struct rb_node *prev;
3963 struct btrfs_inode *entry;
3964 struct inode *inode;
3965 u64 objectid = 0;
3966
3967 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3968
3969 spin_lock(&root->inode_lock);
3970again:
3971 node = root->inode_tree.rb_node;
3972 prev = NULL;
3973 while (node) {
3974 prev = node;
3975 entry = rb_entry(node, struct btrfs_inode, rb_node);
3976
3977 if (objectid < entry->vfs_inode.i_ino)
3978 node = node->rb_left;
3979 else if (objectid > entry->vfs_inode.i_ino)
3980 node = node->rb_right;
3981 else
3982 break;
3983 }
3984 if (!node) {
3985 while (prev) {
3986 entry = rb_entry(prev, struct btrfs_inode, rb_node);
3987 if (objectid <= entry->vfs_inode.i_ino) {
3988 node = prev;
3989 break;
3990 }
3991 prev = rb_next(prev);
3992 }
3993 }
3994 while (node) {
3995 entry = rb_entry(node, struct btrfs_inode, rb_node);
3996 objectid = entry->vfs_inode.i_ino + 1;
3997 inode = igrab(&entry->vfs_inode);
3998 if (inode) {
3999 spin_unlock(&root->inode_lock);
4000 if (atomic_read(&inode->i_count) > 1)
4001 d_prune_aliases(inode);
4002 /*
Al Viro45321ac2010-06-07 13:43:19 -04004003 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004004 * the inode cache when its usage count
4005 * hits zero.
4006 */
4007 iput(inode);
4008 cond_resched();
4009 spin_lock(&root->inode_lock);
4010 goto again;
4011 }
4012
4013 if (cond_resched_lock(&root->inode_lock))
4014 goto again;
4015
4016 node = rb_next(node);
4017 }
4018 spin_unlock(&root->inode_lock);
4019 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004020}
4021
Chris Masone02119d2008-09-05 16:13:11 -04004022static int btrfs_init_locked_inode(struct inode *inode, void *p)
4023{
4024 struct btrfs_iget_args *args = p;
4025 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004026 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05004027 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004028 return 0;
4029}
4030
4031static int btrfs_find_actor(struct inode *inode, void *opaque)
4032{
4033 struct btrfs_iget_args *args = opaque;
Chris Masond3977122009-01-05 21:25:51 -05004034 return args->ino == inode->i_ino &&
4035 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004036}
4037
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004038static struct inode *btrfs_iget_locked(struct super_block *s,
4039 u64 objectid,
4040 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004041{
4042 struct inode *inode;
4043 struct btrfs_iget_args args;
4044 args.ino = objectid;
4045 args.root = root;
4046
4047 inode = iget5_locked(s, objectid, btrfs_find_actor,
4048 btrfs_init_locked_inode,
4049 (void *)&args);
4050 return inode;
4051}
4052
Balaji Rao1a54ef82008-07-21 02:01:04 +05304053/* Get an inode object given its location and corresponding root.
4054 * Returns in *is_new if the inode was read from disk
4055 */
4056struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004057 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304058{
4059 struct inode *inode;
4060
4061 inode = btrfs_iget_locked(s, location->objectid, root);
4062 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004063 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304064
4065 if (inode->i_state & I_NEW) {
4066 BTRFS_I(inode)->root = root;
4067 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4068 btrfs_read_locked_inode(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004069
4070 inode_tree_add(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304071 unlock_new_inode(inode);
Josef Bacik73f73412009-12-04 17:38:27 +00004072 if (new)
4073 *new = 1;
Balaji Rao1a54ef82008-07-21 02:01:04 +05304074 }
4075
4076 return inode;
4077}
4078
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004079static struct inode *new_simple_dir(struct super_block *s,
4080 struct btrfs_key *key,
4081 struct btrfs_root *root)
4082{
4083 struct inode *inode = new_inode(s);
4084
4085 if (!inode)
4086 return ERR_PTR(-ENOMEM);
4087
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004088 BTRFS_I(inode)->root = root;
4089 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
4090 BTRFS_I(inode)->dummy_inode = 1;
4091
4092 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
4093 inode->i_op = &simple_dir_inode_operations;
4094 inode->i_fop = &simple_dir_operations;
4095 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4096 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4097
4098 return inode;
4099}
4100
Chris Mason3de45862008-11-17 21:02:50 -05004101struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004102{
Chris Masond3977122009-01-05 21:25:51 -05004103 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004104 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004105 struct btrfs_root *sub_root = root;
4106 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004107 int index;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004108 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004109
Yan, Zheng76dda932009-09-21 16:00:26 -04004110 dentry->d_op = &btrfs_dentry_operations;
4111
Chris Mason39279cc2007-06-12 06:35:45 -04004112 if (dentry->d_name.len > BTRFS_NAME_LEN)
4113 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004114
Chris Mason39279cc2007-06-12 06:35:45 -04004115 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04004116
Chris Mason39279cc2007-06-12 06:35:45 -04004117 if (ret < 0)
4118 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004119
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004120 if (location.objectid == 0)
4121 return NULL;
4122
4123 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004124 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004125 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004126 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004127
4128 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4129
Yan, Zheng76dda932009-09-21 16:00:26 -04004130 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004131 ret = fixup_tree_root_location(root, dir, dentry,
4132 &location, &sub_root);
4133 if (ret < 0) {
4134 if (ret != -ENOENT)
4135 inode = ERR_PTR(ret);
4136 else
4137 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4138 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004139 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004140 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004141 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4142
Julia Lawall34d19ba2011-01-24 19:55:19 +00004143 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004144 down_read(&root->fs_info->cleanup_work_sem);
4145 if (!(inode->i_sb->s_flags & MS_RDONLY))
4146 btrfs_orphan_cleanup(sub_root);
4147 up_read(&root->fs_info->cleanup_work_sem);
4148 }
4149
Chris Mason3de45862008-11-17 21:02:50 -05004150 return inode;
4151}
4152
Yan, Zheng76dda932009-09-21 16:00:26 -04004153static int btrfs_dentry_delete(struct dentry *dentry)
4154{
4155 struct btrfs_root *root;
4156
Yan, Zhengefefb142009-10-09 09:25:16 -04004157 if (!dentry->d_inode && !IS_ROOT(dentry))
4158 dentry = dentry->d_parent;
Yan, Zheng76dda932009-09-21 16:00:26 -04004159
Yan, Zhengefefb142009-10-09 09:25:16 -04004160 if (dentry->d_inode) {
4161 root = BTRFS_I(dentry->d_inode)->root;
4162 if (btrfs_root_refs(&root->root_item) == 0)
4163 return 1;
4164 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004165 return 0;
4166}
4167
Chris Mason3de45862008-11-17 21:02:50 -05004168static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4169 struct nameidata *nd)
4170{
4171 struct inode *inode;
4172
Chris Mason3de45862008-11-17 21:02:50 -05004173 inode = btrfs_lookup_dentry(dir, dentry);
4174 if (IS_ERR(inode))
4175 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04004176
Chris Mason39279cc2007-06-12 06:35:45 -04004177 return d_splice_alias(inode, dentry);
4178}
4179
Chris Mason39279cc2007-06-12 06:35:45 -04004180static unsigned char btrfs_filetype_table[] = {
4181 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4182};
4183
David Woodhousecbdf5a22008-08-06 19:42:33 +01004184static int btrfs_real_readdir(struct file *filp, void *dirent,
4185 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004186{
Chris Mason6da6aba2007-12-18 16:15:09 -05004187 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004188 struct btrfs_root *root = BTRFS_I(inode)->root;
4189 struct btrfs_item *item;
4190 struct btrfs_dir_item *di;
4191 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004192 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004193 struct btrfs_path *path;
4194 int ret;
4195 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04004196 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004197 int slot;
4198 int advance;
4199 unsigned char d_type;
4200 int over = 0;
4201 u32 di_cur;
4202 u32 di_total;
4203 u32 di_len;
4204 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004205 char tmp_name[32];
4206 char *name_ptr;
4207 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04004208
4209 /* FIXME, use a real flag for deciding about the key type */
4210 if (root->fs_info->tree_root == root)
4211 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004212
Chris Mason39544012007-12-12 14:38:19 -05004213 /* special case for "." */
4214 if (filp->f_pos == 0) {
4215 over = filldir(dirent, ".", 1,
4216 1, inode->i_ino,
4217 DT_DIR);
4218 if (over)
4219 return 0;
4220 filp->f_pos = 1;
4221 }
Chris Mason39544012007-12-12 14:38:19 -05004222 /* special case for .., just use the back ref */
4223 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004224 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004225 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004226 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004227 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004228 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004229 filp->f_pos = 2;
4230 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004231 path = btrfs_alloc_path();
4232 path->reada = 2;
4233
Chris Mason39279cc2007-06-12 06:35:45 -04004234 btrfs_set_key_type(&key, key_type);
4235 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01004236 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04004237
Chris Mason39279cc2007-06-12 06:35:45 -04004238 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4239 if (ret < 0)
4240 goto err;
4241 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01004242
4243 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004244 leaf = path->nodes[0];
4245 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004246 slot = path->slots[0];
4247 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01004248 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004249 ret = btrfs_next_leaf(root, path);
4250 if (ret)
4251 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004252 leaf = path->nodes[0];
4253 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004254 slot = path->slots[0];
4255 } else {
4256 slot++;
4257 path->slots[0]++;
4258 }
4259 }
Chris Mason3de45862008-11-17 21:02:50 -05004260
Chris Mason39279cc2007-06-12 06:35:45 -04004261 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04004262 item = btrfs_item_nr(leaf, slot);
4263 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4264
4265 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004266 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004267 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004268 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004269 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04004270 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04004271
4272 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01004273
Chris Mason39279cc2007-06-12 06:35:45 -04004274 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4275 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004276 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004277
4278 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004279 struct btrfs_key location;
4280
4281 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004282 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004283 name_ptr = tmp_name;
4284 } else {
4285 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004286 if (!name_ptr) {
4287 ret = -ENOMEM;
4288 goto err;
4289 }
Chris Mason5f39d392007-10-15 16:14:19 -04004290 }
4291 read_extent_buffer(leaf, name_ptr,
4292 (unsigned long)(di + 1), name_len);
4293
4294 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4295 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004296
4297 /* is this a reference to our own snapshot? If so
4298 * skip it
4299 */
4300 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4301 location.objectid == root->root_key.objectid) {
4302 over = 0;
4303 goto skip;
4304 }
Chris Mason5f39d392007-10-15 16:14:19 -04004305 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004306 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004307 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004308
Chris Mason3de45862008-11-17 21:02:50 -05004309skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004310 if (name_ptr != tmp_name)
4311 kfree(name_ptr);
4312
Chris Mason39279cc2007-06-12 06:35:45 -04004313 if (over)
4314 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004315 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004316 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004317 di_cur += di_len;
4318 di = (struct btrfs_dir_item *)((char *)di + di_len);
4319 }
4320 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004321
4322 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004323 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004324 /*
4325 * 32-bit glibc will use getdents64, but then strtol -
4326 * so the last number we can serve is this.
4327 */
4328 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004329 else
4330 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004331nopos:
4332 ret = 0;
4333err:
Chris Mason39279cc2007-06-12 06:35:45 -04004334 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004335 return ret;
4336}
4337
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004338int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004339{
4340 struct btrfs_root *root = BTRFS_I(inode)->root;
4341 struct btrfs_trans_handle *trans;
4342 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004343 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004344
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004345 if (BTRFS_I(inode)->dummy_inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04004346 return 0;
4347
Josef Bacik0af3d002010-06-21 14:48:16 -04004348 smp_mb();
4349 nolock = (root->fs_info->closing && root == root->fs_info->tree_root);
4350
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004351 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004352 if (nolock)
4353 trans = btrfs_join_transaction_nolock(root, 1);
4354 else
4355 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004356 if (IS_ERR(trans))
4357 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04004358 btrfs_set_trans_block_group(trans, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04004359 if (nolock)
4360 ret = btrfs_end_transaction_nolock(trans, root);
4361 else
4362 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004363 }
4364 return ret;
4365}
4366
4367/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004368 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004369 * inode changes. But, it is most likely to find the inode in cache.
4370 * FIXME, needs more benchmarking...there are no reasons other than performance
4371 * to keep or drop this code.
4372 */
4373void btrfs_dirty_inode(struct inode *inode)
4374{
4375 struct btrfs_root *root = BTRFS_I(inode)->root;
4376 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004377 int ret;
4378
4379 if (BTRFS_I(inode)->dummy_inode)
4380 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004381
Chris Masonf9295742008-07-17 12:54:14 -04004382 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004383 BUG_ON(IS_ERR(trans));
Chris Mason39279cc2007-06-12 06:35:45 -04004384 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004385
4386 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004387 if (ret && ret == -ENOSPC) {
4388 /* whoops, lets try again with the full transaction */
4389 btrfs_end_transaction(trans, root);
4390 trans = btrfs_start_transaction(root, 1);
Chris Mason9aeead72010-05-27 10:23:00 -04004391 if (IS_ERR(trans)) {
4392 if (printk_ratelimit()) {
4393 printk(KERN_ERR "btrfs: fail to "
4394 "dirty inode %lu error %ld\n",
4395 inode->i_ino, PTR_ERR(trans));
4396 }
4397 return;
4398 }
Chris Mason94b60442010-05-26 11:02:00 -04004399 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004400
Chris Mason94b60442010-05-26 11:02:00 -04004401 ret = btrfs_update_inode(trans, root, inode);
4402 if (ret) {
Chris Mason9aeead72010-05-27 10:23:00 -04004403 if (printk_ratelimit()) {
4404 printk(KERN_ERR "btrfs: fail to "
4405 "dirty inode %lu error %d\n",
4406 inode->i_ino, ret);
4407 }
Chris Mason94b60442010-05-26 11:02:00 -04004408 }
4409 }
Chris Mason39279cc2007-06-12 06:35:45 -04004410 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004411}
4412
Chris Masond352ac62008-09-29 15:18:18 -04004413/*
4414 * find the highest existing sequence number in a directory
4415 * and then set the in-memory index_cnt variable to reflect
4416 * free sequence numbers
4417 */
Josef Bacikaec74772008-07-24 12:12:38 -04004418static int btrfs_set_inode_index_count(struct inode *inode)
4419{
4420 struct btrfs_root *root = BTRFS_I(inode)->root;
4421 struct btrfs_key key, found_key;
4422 struct btrfs_path *path;
4423 struct extent_buffer *leaf;
4424 int ret;
4425
4426 key.objectid = inode->i_ino;
4427 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4428 key.offset = (u64)-1;
4429
4430 path = btrfs_alloc_path();
4431 if (!path)
4432 return -ENOMEM;
4433
4434 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4435 if (ret < 0)
4436 goto out;
4437 /* FIXME: we should be able to handle this */
4438 if (ret == 0)
4439 goto out;
4440 ret = 0;
4441
4442 /*
4443 * MAGIC NUMBER EXPLANATION:
4444 * since we search a directory based on f_pos we have to start at 2
4445 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4446 * else has to start at 2
4447 */
4448 if (path->slots[0] == 0) {
4449 BTRFS_I(inode)->index_cnt = 2;
4450 goto out;
4451 }
4452
4453 path->slots[0]--;
4454
4455 leaf = path->nodes[0];
4456 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4457
4458 if (found_key.objectid != inode->i_ino ||
4459 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4460 BTRFS_I(inode)->index_cnt = 2;
4461 goto out;
4462 }
4463
4464 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4465out:
4466 btrfs_free_path(path);
4467 return ret;
4468}
4469
Chris Masond352ac62008-09-29 15:18:18 -04004470/*
4471 * helper to find a free sequence number in a given directory. This current
4472 * code is very simple, later versions will do smarter things in the btree
4473 */
Chris Mason3de45862008-11-17 21:02:50 -05004474int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004475{
4476 int ret = 0;
4477
4478 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4479 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05004480 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04004481 return ret;
4482 }
4483
Chris Mason00e4e6b2008-08-05 11:18:09 -04004484 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004485 BTRFS_I(dir)->index_cnt++;
4486
4487 return ret;
4488}
4489
Chris Mason39279cc2007-06-12 06:35:45 -04004490static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4491 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004492 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004493 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05004494 u64 ref_objectid, u64 objectid,
4495 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004496{
4497 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004498 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004499 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004500 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004501 struct btrfs_inode_ref *ref;
4502 struct btrfs_key key[2];
4503 u32 sizes[2];
4504 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004505 int ret;
4506 int owner;
4507
Chris Mason5f39d392007-10-15 16:14:19 -04004508 path = btrfs_alloc_path();
4509 BUG_ON(!path);
4510
Chris Mason39279cc2007-06-12 06:35:45 -04004511 inode = new_inode(root->fs_info->sb);
4512 if (!inode)
4513 return ERR_PTR(-ENOMEM);
4514
Josef Bacikaec74772008-07-24 12:12:38 -04004515 if (dir) {
Chris Mason3de45862008-11-17 21:02:50 -05004516 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004517 if (ret) {
4518 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004519 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004520 }
Josef Bacikaec74772008-07-24 12:12:38 -04004521 }
4522 /*
4523 * index_cnt is ignored for everything but a dir,
4524 * btrfs_get_inode_index_count has an explanation for the magic
4525 * number
4526 */
4527 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004528 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004529 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004530 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik6a632092009-02-20 11:00:09 -05004531 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04004532
Chris Mason39279cc2007-06-12 06:35:45 -04004533 if (mode & S_IFDIR)
4534 owner = 0;
4535 else
4536 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004537 BTRFS_I(inode)->block_group =
4538 btrfs_find_block_group(root, 0, alloc_hint, owner);
Chris Mason9c583092008-01-29 15:15:18 -05004539
4540 key[0].objectid = objectid;
4541 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4542 key[0].offset = 0;
4543
4544 key[1].objectid = objectid;
4545 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4546 key[1].offset = ref_objectid;
4547
4548 sizes[0] = sizeof(struct btrfs_inode_item);
4549 sizes[1] = name_len + sizeof(*ref);
4550
Chris Masonb9473432009-03-13 11:00:37 -04004551 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004552 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4553 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004554 goto fail;
4555
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004556 inode_init_owner(inode, dir, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04004557 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004558 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004559 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004560 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4561 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004562 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004563
4564 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4565 struct btrfs_inode_ref);
4566 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004567 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004568 ptr = (unsigned long)(ref + 1);
4569 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4570
Chris Mason5f39d392007-10-15 16:14:19 -04004571 btrfs_mark_buffer_dirty(path->nodes[0]);
4572 btrfs_free_path(path);
4573
Chris Mason39279cc2007-06-12 06:35:45 -04004574 location = &BTRFS_I(inode)->location;
4575 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004576 location->offset = 0;
4577 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4578
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004579 btrfs_inherit_iflags(inode, dir);
4580
Chris Mason94272162009-07-02 12:26:06 -04004581 if ((mode & S_IFREG)) {
4582 if (btrfs_test_opt(root, NODATASUM))
4583 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
4584 if (btrfs_test_opt(root, NODATACOW))
4585 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4586 }
4587
Chris Mason39279cc2007-06-12 06:35:45 -04004588 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004589 inode_tree_add(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004590 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004591fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004592 if (dir)
4593 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004594 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004595 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004596 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004597}
4598
4599static inline u8 btrfs_inode_type(struct inode *inode)
4600{
4601 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4602}
4603
Chris Masond352ac62008-09-29 15:18:18 -04004604/*
4605 * utility function to add 'inode' into 'parent_inode' with
4606 * a give name and a given sequence number.
4607 * if 'add_backref' is true, also insert a backref from the
4608 * inode to the parent directory.
4609 */
Chris Masone02119d2008-09-05 16:13:11 -04004610int btrfs_add_link(struct btrfs_trans_handle *trans,
4611 struct inode *parent_inode, struct inode *inode,
4612 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004613{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004614 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004615 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004616 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04004617
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004618 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4619 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4620 } else {
4621 key.objectid = inode->i_ino;
4622 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4623 key.offset = 0;
4624 }
Chris Mason39279cc2007-06-12 06:35:45 -04004625
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004626 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4627 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4628 key.objectid, root->root_key.objectid,
4629 parent_inode->i_ino,
4630 index, name, name_len);
4631 } else if (add_backref) {
4632 ret = btrfs_insert_inode_ref(trans, root,
4633 name, name_len, inode->i_ino,
4634 parent_inode->i_ino, index);
4635 }
4636
Chris Mason39279cc2007-06-12 06:35:45 -04004637 if (ret == 0) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004638 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4639 parent_inode->i_ino, &key,
4640 btrfs_inode_type(inode), index);
4641 BUG_ON(ret);
4642
Chris Masondbe674a2008-07-17 12:54:05 -04004643 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04004644 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04004645 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004646 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004647 }
4648 return ret;
4649}
4650
4651static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00004652 struct inode *dir, struct dentry *dentry,
4653 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004654{
Josef Bacika1b075d2010-11-19 20:36:11 +00004655 int err = btrfs_add_link(trans, dir, inode,
4656 dentry->d_name.name, dentry->d_name.len,
4657 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004658 if (!err) {
4659 d_instantiate(dentry, inode);
4660 return 0;
4661 }
4662 if (err > 0)
4663 err = -EEXIST;
4664 return err;
4665}
4666
Josef Bacik618e21d2007-07-11 10:18:17 -04004667static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4668 int mode, dev_t rdev)
4669{
4670 struct btrfs_trans_handle *trans;
4671 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004672 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004673 int err;
4674 int drop_inode = 0;
4675 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004676 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004677 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004678
4679 if (!new_valid_dev(rdev))
4680 return -EINVAL;
4681
Yan, Zhenga22285a2010-05-16 10:48:46 -04004682 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4683 if (err)
4684 return err;
4685
Josef Bacik9ed74f22009-09-11 16:12:44 -04004686 /*
4687 * 2 for inode item and ref
4688 * 2 for dir items
4689 * 1 for xattr if selinux is on
4690 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004691 trans = btrfs_start_transaction(root, 5);
4692 if (IS_ERR(trans))
4693 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004694
Josef Bacik618e21d2007-07-11 10:18:17 -04004695 btrfs_set_trans_block_group(trans, dir);
4696
Josef Bacikaec74772008-07-24 12:12:38 -04004697 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Josef Bacika1b075d2010-11-19 20:36:11 +00004698 dentry->d_name.len, dir->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004699 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004700 err = PTR_ERR(inode);
4701 if (IS_ERR(inode))
4702 goto out_unlock;
4703
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004704 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004705 if (err) {
4706 drop_inode = 1;
4707 goto out_unlock;
4708 }
4709
Josef Bacik618e21d2007-07-11 10:18:17 -04004710 btrfs_set_trans_block_group(trans, inode);
Josef Bacika1b075d2010-11-19 20:36:11 +00004711 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004712 if (err)
4713 drop_inode = 1;
4714 else {
4715 inode->i_op = &btrfs_special_inode_operations;
4716 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004717 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004718 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004719 btrfs_update_inode_block_group(trans, inode);
4720 btrfs_update_inode_block_group(trans, dir);
4721out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004722 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004723 btrfs_end_transaction_throttle(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004724 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004725 if (drop_inode) {
4726 inode_dec_link_count(inode);
4727 iput(inode);
4728 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004729 return err;
4730}
4731
Chris Mason39279cc2007-06-12 06:35:45 -04004732static int btrfs_create(struct inode *dir, struct dentry *dentry,
4733 int mode, struct nameidata *nd)
4734{
4735 struct btrfs_trans_handle *trans;
4736 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004737 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004738 int drop_inode = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004739 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004740 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004741 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004742 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004743
Yan, Zhenga22285a2010-05-16 10:48:46 -04004744 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4745 if (err)
4746 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004747 /*
4748 * 2 for inode item and ref
4749 * 2 for dir items
4750 * 1 for xattr if selinux is on
4751 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004752 trans = btrfs_start_transaction(root, 5);
4753 if (IS_ERR(trans))
4754 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004755
Chris Mason39279cc2007-06-12 06:35:45 -04004756 btrfs_set_trans_block_group(trans, dir);
4757
Josef Bacikaec74772008-07-24 12:12:38 -04004758 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Josef Bacika1b075d2010-11-19 20:36:11 +00004759 dentry->d_name.len, dir->i_ino, objectid,
4760 BTRFS_I(dir)->block_group, mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004761 err = PTR_ERR(inode);
4762 if (IS_ERR(inode))
4763 goto out_unlock;
4764
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004765 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004766 if (err) {
4767 drop_inode = 1;
4768 goto out_unlock;
4769 }
4770
Chris Mason39279cc2007-06-12 06:35:45 -04004771 btrfs_set_trans_block_group(trans, inode);
Josef Bacika1b075d2010-11-19 20:36:11 +00004772 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004773 if (err)
4774 drop_inode = 1;
4775 else {
4776 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004777 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004778 inode->i_fop = &btrfs_file_operations;
4779 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004780 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004781 }
Chris Mason39279cc2007-06-12 06:35:45 -04004782 btrfs_update_inode_block_group(trans, inode);
4783 btrfs_update_inode_block_group(trans, dir);
4784out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004785 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004786 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004787 if (drop_inode) {
4788 inode_dec_link_count(inode);
4789 iput(inode);
4790 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004791 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004792 return err;
4793}
4794
4795static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4796 struct dentry *dentry)
4797{
4798 struct btrfs_trans_handle *trans;
4799 struct btrfs_root *root = BTRFS_I(dir)->root;
4800 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004801 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004802 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004803 int err;
4804 int drop_inode = 0;
4805
4806 if (inode->i_nlink == 0)
4807 return -ENOENT;
4808
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004809 /* do not allow sys_link's with other subvols of the same device */
4810 if (root->objectid != BTRFS_I(inode)->root->objectid)
4811 return -EPERM;
4812
Josef Bacik9ed74f22009-09-11 16:12:44 -04004813 btrfs_inc_nlink(inode);
Josef Bacikbc1cbf12010-11-23 19:50:59 +00004814 inode->i_ctime = CURRENT_TIME;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004815
Chris Mason3de45862008-11-17 21:02:50 -05004816 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004817 if (err)
4818 goto fail;
4819
Yan, Zhenga22285a2010-05-16 10:48:46 -04004820 /*
4821 * 1 item for inode ref
4822 * 2 items for dir items
4823 */
4824 trans = btrfs_start_transaction(root, 3);
4825 if (IS_ERR(trans)) {
4826 err = PTR_ERR(trans);
4827 goto fail;
4828 }
Chris Mason5f39d392007-10-15 16:14:19 -04004829
Chris Mason39279cc2007-06-12 06:35:45 -04004830 btrfs_set_trans_block_group(trans, dir);
4831 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04004832
Josef Bacika1b075d2010-11-19 20:36:11 +00004833 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004834
Yan, Zhenga5719522009-09-24 09:17:31 -04004835 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004836 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004837 } else {
Josef Bacik6a912212010-11-20 09:48:00 +00004838 struct dentry *parent = dget_parent(dentry);
Yan, Zhenga5719522009-09-24 09:17:31 -04004839 btrfs_update_inode_block_group(trans, dir);
4840 err = btrfs_update_inode(trans, root, inode);
4841 BUG_ON(err);
Josef Bacik6a912212010-11-20 09:48:00 +00004842 btrfs_log_new_name(trans, inode, NULL, parent);
4843 dput(parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04004844 }
Chris Mason39279cc2007-06-12 06:35:45 -04004845
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004846 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004847 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004848fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004849 if (drop_inode) {
4850 inode_dec_link_count(inode);
4851 iput(inode);
4852 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004853 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004854 return err;
4855}
4856
Chris Mason39279cc2007-06-12 06:35:45 -04004857static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4858{
Chris Masonb9d86662008-05-02 16:13:49 -04004859 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004860 struct btrfs_trans_handle *trans;
4861 struct btrfs_root *root = BTRFS_I(dir)->root;
4862 int err = 0;
4863 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004864 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004865 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004866 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004867
Yan, Zhenga22285a2010-05-16 10:48:46 -04004868 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4869 if (err)
4870 return err;
4871
Josef Bacik9ed74f22009-09-11 16:12:44 -04004872 /*
4873 * 2 items for inode and ref
4874 * 2 items for dir items
4875 * 1 for xattr if selinux is on
4876 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004877 trans = btrfs_start_transaction(root, 5);
4878 if (IS_ERR(trans))
4879 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004880 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004881
Josef Bacikaec74772008-07-24 12:12:38 -04004882 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Josef Bacika1b075d2010-11-19 20:36:11 +00004883 dentry->d_name.len, dir->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004884 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4885 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004886 if (IS_ERR(inode)) {
4887 err = PTR_ERR(inode);
4888 goto out_fail;
4889 }
Chris Mason5f39d392007-10-15 16:14:19 -04004890
Chris Mason39279cc2007-06-12 06:35:45 -04004891 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04004892
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004893 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004894 if (err)
4895 goto out_fail;
4896
Chris Mason39279cc2007-06-12 06:35:45 -04004897 inode->i_op = &btrfs_dir_inode_operations;
4898 inode->i_fop = &btrfs_dir_file_operations;
4899 btrfs_set_trans_block_group(trans, inode);
4900
Chris Masondbe674a2008-07-17 12:54:05 -04004901 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004902 err = btrfs_update_inode(trans, root, inode);
4903 if (err)
4904 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004905
Josef Bacika1b075d2010-11-19 20:36:11 +00004906 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
4907 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004908 if (err)
4909 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004910
Chris Mason39279cc2007-06-12 06:35:45 -04004911 d_instantiate(dentry, inode);
4912 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004913 btrfs_update_inode_block_group(trans, inode);
4914 btrfs_update_inode_block_group(trans, dir);
4915
4916out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004917 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004918 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004919 if (drop_on_err)
4920 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004921 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004922 return err;
4923}
4924
Chris Masond352ac62008-09-29 15:18:18 -04004925/* helper for btfs_get_extent. Given an existing extent in the tree,
4926 * and an extent that you want to insert, deal with overlap and insert
4927 * the new extent into the tree.
4928 */
Chris Mason3b951512008-04-17 11:29:12 -04004929static int merge_extent_mapping(struct extent_map_tree *em_tree,
4930 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004931 struct extent_map *em,
4932 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04004933{
4934 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04004935
Chris Masone6dcd2d2008-07-17 12:53:50 -04004936 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
4937 start_diff = map_start - em->start;
4938 em->start = map_start;
4939 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04004940 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
4941 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004942 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04004943 em->block_len -= start_diff;
4944 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004945 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004946}
4947
Chris Masonc8b97812008-10-29 14:49:59 -04004948static noinline int uncompress_inline(struct btrfs_path *path,
4949 struct inode *inode, struct page *page,
4950 size_t pg_offset, u64 extent_offset,
4951 struct btrfs_file_extent_item *item)
4952{
4953 int ret;
4954 struct extent_buffer *leaf = path->nodes[0];
4955 char *tmp;
4956 size_t max_size;
4957 unsigned long inline_size;
4958 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08004959 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04004960
4961 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08004962 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04004963 max_size = btrfs_file_extent_ram_bytes(leaf, item);
4964 inline_size = btrfs_file_extent_inline_item_len(leaf,
4965 btrfs_item_nr(leaf, path->slots[0]));
4966 tmp = kmalloc(inline_size, GFP_NOFS);
4967 ptr = btrfs_file_extent_inline_start(item);
4968
4969 read_extent_buffer(leaf, tmp, ptr, inline_size);
4970
Chris Mason5b050f02008-11-11 09:34:41 -05004971 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08004972 ret = btrfs_decompress(compress_type, tmp, page,
4973 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004974 if (ret) {
4975 char *kaddr = kmap_atomic(page, KM_USER0);
4976 unsigned long copy_size = min_t(u64,
4977 PAGE_CACHE_SIZE - pg_offset,
4978 max_size - extent_offset);
4979 memset(kaddr + pg_offset, 0, copy_size);
4980 kunmap_atomic(kaddr, KM_USER0);
4981 }
4982 kfree(tmp);
4983 return 0;
4984}
4985
Chris Masond352ac62008-09-29 15:18:18 -04004986/*
4987 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05004988 * the ugly parts come from merging extents from the disk with the in-ram
4989 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04004990 * where the in-ram extents might be locked pending data=ordered completion.
4991 *
4992 * This also copies inline extents directly into the page.
4993 */
Chris Masond3977122009-01-05 21:25:51 -05004994
Chris Masona52d9a82007-08-27 16:49:44 -04004995struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05004996 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04004997 int create)
4998{
4999 int ret;
5000 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005001 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005002 u64 extent_start = 0;
5003 u64 extent_end = 0;
5004 u64 objectid = inode->i_ino;
5005 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005006 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005007 struct btrfs_root *root = BTRFS_I(inode)->root;
5008 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005009 struct extent_buffer *leaf;
5010 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005011 struct extent_map *em = NULL;
5012 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005013 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005014 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005015 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005016
Chris Masona52d9a82007-08-27 16:49:44 -04005017again:
Chris Mason890871b2009-09-02 16:24:52 -04005018 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005019 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005020 if (em)
5021 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005022 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005023
Chris Masona52d9a82007-08-27 16:49:44 -04005024 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005025 if (em->start > start || em->start + em->len <= start)
5026 free_extent_map(em);
5027 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005028 free_extent_map(em);
5029 else
5030 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005031 }
Chris Masond1310b22008-01-24 16:13:08 -05005032 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005033 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005034 err = -ENOMEM;
5035 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005036 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005037 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005038 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005039 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005040 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005041 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005042
5043 if (!path) {
5044 path = btrfs_alloc_path();
5045 BUG_ON(!path);
5046 }
5047
Chris Mason179e29e2007-11-01 11:28:41 -04005048 ret = btrfs_lookup_file_extent(trans, root, path,
5049 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005050 if (ret < 0) {
5051 err = ret;
5052 goto out;
5053 }
5054
5055 if (ret != 0) {
5056 if (path->slots[0] == 0)
5057 goto not_found;
5058 path->slots[0]--;
5059 }
5060
Chris Mason5f39d392007-10-15 16:14:19 -04005061 leaf = path->nodes[0];
5062 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005063 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005064 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005065 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5066 found_type = btrfs_key_type(&found_key);
5067 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005068 found_type != BTRFS_EXTENT_DATA_KEY) {
5069 goto not_found;
5070 }
5071
Chris Mason5f39d392007-10-15 16:14:19 -04005072 found_type = btrfs_file_extent_type(leaf, item);
5073 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005074 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005075 if (found_type == BTRFS_FILE_EXTENT_REG ||
5076 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005077 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005078 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005079 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5080 size_t size;
5081 size = btrfs_file_extent_inline_len(leaf, item);
5082 extent_end = (extent_start + size + root->sectorsize - 1) &
5083 ~((u64)root->sectorsize - 1);
5084 }
5085
5086 if (start >= extent_end) {
5087 path->slots[0]++;
5088 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5089 ret = btrfs_next_leaf(root, path);
5090 if (ret < 0) {
5091 err = ret;
5092 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005093 }
Yan Zheng9036c102008-10-30 14:19:41 -04005094 if (ret > 0)
5095 goto not_found;
5096 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005097 }
Yan Zheng9036c102008-10-30 14:19:41 -04005098 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5099 if (found_key.objectid != objectid ||
5100 found_key.type != BTRFS_EXTENT_DATA_KEY)
5101 goto not_found;
5102 if (start + len <= found_key.offset)
5103 goto not_found;
5104 em->start = start;
5105 em->len = found_key.offset - start;
5106 goto not_found_em;
5107 }
5108
Yan Zhengd899e052008-10-30 14:25:28 -04005109 if (found_type == BTRFS_FILE_EXTENT_REG ||
5110 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005111 em->start = extent_start;
5112 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005113 em->orig_start = extent_start -
5114 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04005115 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5116 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005117 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005118 goto insert;
5119 }
Li Zefan261507a02010-12-17 14:21:50 +08005120 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005121 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005122 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005123 em->block_start = bytenr;
5124 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5125 item);
5126 } else {
5127 bytenr += btrfs_file_extent_offset(leaf, item);
5128 em->block_start = bytenr;
5129 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005130 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5131 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005132 }
Chris Masona52d9a82007-08-27 16:49:44 -04005133 goto insert;
5134 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005135 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005136 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005137 size_t size;
5138 size_t extent_offset;
5139 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005140
Chris Masona52d9a82007-08-27 16:49:44 -04005141 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005142 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005143 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005144 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005145 goto out;
5146 }
5147
Yan Zheng9036c102008-10-30 14:19:41 -04005148 size = btrfs_file_extent_inline_len(leaf, item);
5149 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005150 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005151 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005152 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005153 em->len = (copy_size + root->sectorsize - 1) &
5154 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005155 em->orig_start = EXTENT_MAP_INLINE;
Li Zefan261507a02010-12-17 14:21:50 +08005156 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005157 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005158 em->compress_type = compress_type;
5159 }
Yan689f9342007-10-29 11:41:07 -04005160 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005161 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005162 if (btrfs_file_extent_compression(leaf, item) !=
5163 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005164 ret = uncompress_inline(path, inode, page,
5165 pg_offset,
5166 extent_offset, item);
5167 BUG_ON(ret);
5168 } else {
5169 map = kmap(page);
5170 read_extent_buffer(leaf, map + pg_offset, ptr,
5171 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005172 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5173 memset(map + pg_offset + copy_size, 0,
5174 PAGE_CACHE_SIZE - pg_offset -
5175 copy_size);
5176 }
Chris Masonc8b97812008-10-29 14:49:59 -04005177 kunmap(page);
5178 }
Chris Mason179e29e2007-11-01 11:28:41 -04005179 flush_dcache_page(page);
5180 } else if (create && PageUptodate(page)) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005181 WARN_ON(1);
Chris Mason179e29e2007-11-01 11:28:41 -04005182 if (!trans) {
5183 kunmap(page);
5184 free_extent_map(em);
5185 em = NULL;
5186 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04005187 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005188 if (IS_ERR(trans))
5189 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005190 goto again;
5191 }
Chris Masonc8b97812008-10-29 14:49:59 -04005192 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005193 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005194 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005195 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005196 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005197 }
Chris Masond1310b22008-01-24 16:13:08 -05005198 set_extent_uptodate(io_tree, em->start,
5199 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005200 goto insert;
5201 } else {
Chris Masond3977122009-01-05 21:25:51 -05005202 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005203 WARN_ON(1);
5204 }
5205not_found:
5206 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005207 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005208not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005209 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005210 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005211insert:
5212 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05005213 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005214 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5215 "[%llu %llu]\n", (unsigned long long)em->start,
5216 (unsigned long long)em->len,
5217 (unsigned long long)start,
5218 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005219 err = -EIO;
5220 goto out;
5221 }
Chris Masond1310b22008-01-24 16:13:08 -05005222
5223 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005224 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005225 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005226 /* it is possible that someone inserted the extent into the tree
5227 * while we had the lock dropped. It is also possible that
5228 * an overlapping map exists in the tree
5229 */
Chris Masona52d9a82007-08-27 16:49:44 -04005230 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005231 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005232
5233 ret = 0;
5234
Chris Mason3b951512008-04-17 11:29:12 -04005235 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005236 if (existing && (existing->start > start ||
5237 existing->start + existing->len <= start)) {
5238 free_extent_map(existing);
5239 existing = NULL;
5240 }
Chris Mason3b951512008-04-17 11:29:12 -04005241 if (!existing) {
5242 existing = lookup_extent_mapping(em_tree, em->start,
5243 em->len);
5244 if (existing) {
5245 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005246 em, start,
5247 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005248 free_extent_map(existing);
5249 if (err) {
5250 free_extent_map(em);
5251 em = NULL;
5252 }
5253 } else {
5254 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005255 free_extent_map(em);
5256 em = NULL;
5257 }
5258 } else {
5259 free_extent_map(em);
5260 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005261 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005262 }
Chris Masona52d9a82007-08-27 16:49:44 -04005263 }
Chris Mason890871b2009-09-02 16:24:52 -04005264 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005265out:
Chris Masonf4219502008-07-22 11:18:09 -04005266 if (path)
5267 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005268 if (trans) {
5269 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005270 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005271 err = ret;
5272 }
Chris Masona52d9a82007-08-27 16:49:44 -04005273 if (err) {
5274 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005275 return ERR_PTR(err);
5276 }
5277 return em;
5278}
5279
Josef Bacik4b46fce2010-05-23 11:00:55 -04005280static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5281 u64 start, u64 len)
5282{
5283 struct btrfs_root *root = BTRFS_I(inode)->root;
5284 struct btrfs_trans_handle *trans;
5285 struct extent_map *em;
5286 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5287 struct btrfs_key ins;
5288 u64 alloc_hint;
5289 int ret;
5290
5291 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5292
5293 trans = btrfs_join_transaction(root, 0);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005294 if (IS_ERR(trans))
5295 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005296
5297 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5298
5299 alloc_hint = get_extent_allocation_hint(inode, start, len);
5300 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
5301 alloc_hint, (u64)-1, &ins, 1);
5302 if (ret) {
5303 em = ERR_PTR(ret);
5304 goto out;
5305 }
5306
5307 em = alloc_extent_map(GFP_NOFS);
5308 if (!em) {
5309 em = ERR_PTR(-ENOMEM);
5310 goto out;
5311 }
5312
5313 em->start = start;
5314 em->orig_start = em->start;
5315 em->len = ins.offset;
5316
5317 em->block_start = ins.objectid;
5318 em->block_len = ins.offset;
5319 em->bdev = root->fs_info->fs_devices->latest_bdev;
5320 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5321
5322 while (1) {
5323 write_lock(&em_tree->lock);
5324 ret = add_extent_mapping(em_tree, em);
5325 write_unlock(&em_tree->lock);
5326 if (ret != -EEXIST)
5327 break;
5328 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5329 }
5330
5331 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5332 ins.offset, ins.offset, 0);
5333 if (ret) {
5334 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5335 em = ERR_PTR(ret);
5336 }
5337out:
5338 btrfs_end_transaction(trans, root);
5339 return em;
5340}
5341
Chris Mason46bfbb52010-05-26 11:04:10 -04005342/*
5343 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5344 * block must be cow'd
5345 */
5346static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5347 struct inode *inode, u64 offset, u64 len)
5348{
5349 struct btrfs_path *path;
5350 int ret;
5351 struct extent_buffer *leaf;
5352 struct btrfs_root *root = BTRFS_I(inode)->root;
5353 struct btrfs_file_extent_item *fi;
5354 struct btrfs_key key;
5355 u64 disk_bytenr;
5356 u64 backref_offset;
5357 u64 extent_end;
5358 u64 num_bytes;
5359 int slot;
5360 int found_type;
5361
5362 path = btrfs_alloc_path();
5363 if (!path)
5364 return -ENOMEM;
5365
5366 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
5367 offset, 0);
5368 if (ret < 0)
5369 goto out;
5370
5371 slot = path->slots[0];
5372 if (ret == 1) {
5373 if (slot == 0) {
5374 /* can't find the item, must cow */
5375 ret = 0;
5376 goto out;
5377 }
5378 slot--;
5379 }
5380 ret = 0;
5381 leaf = path->nodes[0];
5382 btrfs_item_key_to_cpu(leaf, &key, slot);
5383 if (key.objectid != inode->i_ino ||
5384 key.type != BTRFS_EXTENT_DATA_KEY) {
5385 /* not our file or wrong item type, must cow */
5386 goto out;
5387 }
5388
5389 if (key.offset > offset) {
5390 /* Wrong offset, must cow */
5391 goto out;
5392 }
5393
5394 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5395 found_type = btrfs_file_extent_type(leaf, fi);
5396 if (found_type != BTRFS_FILE_EXTENT_REG &&
5397 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5398 /* not a regular extent, must cow */
5399 goto out;
5400 }
5401 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5402 backref_offset = btrfs_file_extent_offset(leaf, fi);
5403
5404 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5405 if (extent_end < offset + len) {
5406 /* extent doesn't include our full range, must cow */
5407 goto out;
5408 }
5409
5410 if (btrfs_extent_readonly(root, disk_bytenr))
5411 goto out;
5412
5413 /*
5414 * look for other files referencing this extent, if we
5415 * find any we must cow
5416 */
5417 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
5418 key.offset - backref_offset, disk_bytenr))
5419 goto out;
5420
5421 /*
5422 * adjust disk_bytenr and num_bytes to cover just the bytes
5423 * in this extent we are about to write. If there
5424 * are any csums in that range we have to cow in order
5425 * to keep the csums correct
5426 */
5427 disk_bytenr += backref_offset;
5428 disk_bytenr += offset - key.offset;
5429 num_bytes = min(offset + len, extent_end) - offset;
5430 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5431 goto out;
5432 /*
5433 * all of the above have passed, it is safe to overwrite this extent
5434 * without cow
5435 */
5436 ret = 1;
5437out:
5438 btrfs_free_path(path);
5439 return ret;
5440}
5441
Josef Bacik4b46fce2010-05-23 11:00:55 -04005442static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5443 struct buffer_head *bh_result, int create)
5444{
5445 struct extent_map *em;
5446 struct btrfs_root *root = BTRFS_I(inode)->root;
5447 u64 start = iblock << inode->i_blkbits;
5448 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04005449 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005450
5451 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
5452 if (IS_ERR(em))
5453 return PTR_ERR(em);
5454
5455 /*
5456 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5457 * io. INLINE is special, and we could probably kludge it in here, but
5458 * it's still buffered so for safety lets just fall back to the generic
5459 * buffered path.
5460 *
5461 * For COMPRESSED we _have_ to read the entire extent in so we can
5462 * decompress it, so there will be buffering required no matter what we
5463 * do, so go ahead and fallback to buffered.
5464 *
5465 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5466 * to buffered IO. Don't blame me, this is the price we pay for using
5467 * the generic code.
5468 */
5469 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5470 em->block_start == EXTENT_MAP_INLINE) {
5471 free_extent_map(em);
5472 return -ENOTBLK;
5473 }
5474
5475 /* Just a good old fashioned hole, return */
5476 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
5477 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5478 free_extent_map(em);
5479 /* DIO will do one hole at a time, so just unlock a sector */
5480 unlock_extent(&BTRFS_I(inode)->io_tree, start,
5481 start + root->sectorsize - 1, GFP_NOFS);
5482 return 0;
5483 }
5484
5485 /*
5486 * We don't allocate a new extent in the following cases
5487 *
5488 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5489 * existing extent.
5490 * 2) The extent is marked as PREALLOC. We're good to go here and can
5491 * just use the extent.
5492 *
5493 */
Chris Mason46bfbb52010-05-26 11:04:10 -04005494 if (!create) {
5495 len = em->len - (start - em->start);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005496 goto map;
Chris Mason46bfbb52010-05-26 11:04:10 -04005497 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005498
5499 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
5500 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
5501 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005502 int type;
5503 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04005504 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005505
5506 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5507 type = BTRFS_ORDERED_PREALLOC;
5508 else
5509 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04005510 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04005511 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04005512
5513 /*
5514 * we're not going to log anything, but we do need
5515 * to make sure the current transaction stays open
5516 * while we look for nocow cross refs
5517 */
5518 trans = btrfs_join_transaction(root, 0);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005519 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04005520 goto must_cow;
5521
5522 if (can_nocow_odirect(trans, inode, start, len) == 1) {
5523 ret = btrfs_add_ordered_extent_dio(inode, start,
5524 block_start, len, len, type);
5525 btrfs_end_transaction(trans, root);
5526 if (ret) {
5527 free_extent_map(em);
5528 return ret;
5529 }
5530 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005531 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005532 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005533 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005534must_cow:
5535 /*
5536 * this will cow the extent, reset the len in case we changed
5537 * it above
5538 */
5539 len = bh_result->b_size;
5540 free_extent_map(em);
5541 em = btrfs_new_extent_direct(inode, start, len);
5542 if (IS_ERR(em))
5543 return PTR_ERR(em);
5544 len = min(len, em->len - (start - em->start));
5545unlock:
Chris Mason4845e442010-05-25 20:56:50 -04005546 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
5547 EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
5548 0, NULL, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005549map:
5550 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
5551 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04005552 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005553 bh_result->b_bdev = em->bdev;
5554 set_buffer_mapped(bh_result);
5555 if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5556 set_buffer_new(bh_result);
5557
5558 free_extent_map(em);
5559
5560 return 0;
5561}
5562
5563struct btrfs_dio_private {
5564 struct inode *inode;
5565 u64 logical_offset;
5566 u64 disk_bytenr;
5567 u64 bytes;
5568 u32 *csums;
5569 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00005570
5571 /* number of bios pending for this dio */
5572 atomic_t pending_bios;
5573
5574 /* IO errors */
5575 int errors;
5576
5577 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005578};
5579
5580static void btrfs_endio_direct_read(struct bio *bio, int err)
5581{
Miao Xiee65e1532010-11-22 03:04:43 +00005582 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005583 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
5584 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005585 struct inode *inode = dip->inode;
5586 struct btrfs_root *root = BTRFS_I(inode)->root;
5587 u64 start;
5588 u32 *private = dip->csums;
5589
5590 start = dip->logical_offset;
5591 do {
5592 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
5593 struct page *page = bvec->bv_page;
5594 char *kaddr;
5595 u32 csum = ~(u32)0;
5596 unsigned long flags;
5597
5598 local_irq_save(flags);
5599 kaddr = kmap_atomic(page, KM_IRQ0);
5600 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
5601 csum, bvec->bv_len);
5602 btrfs_csum_final(csum, (char *)&csum);
5603 kunmap_atomic(kaddr, KM_IRQ0);
5604 local_irq_restore(flags);
5605
5606 flush_dcache_page(bvec->bv_page);
5607 if (csum != *private) {
5608 printk(KERN_ERR "btrfs csum failed ino %lu off"
5609 " %llu csum %u private %u\n",
5610 inode->i_ino, (unsigned long long)start,
5611 csum, *private);
5612 err = -EIO;
5613 }
5614 }
5615
5616 start += bvec->bv_len;
5617 private++;
5618 bvec++;
5619 } while (bvec <= bvec_end);
5620
5621 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
5622 dip->logical_offset + dip->bytes - 1, GFP_NOFS);
5623 bio->bi_private = dip->private;
5624
5625 kfree(dip->csums);
5626 kfree(dip);
5627 dio_end_io(bio, err);
5628}
5629
5630static void btrfs_endio_direct_write(struct bio *bio, int err)
5631{
5632 struct btrfs_dio_private *dip = bio->bi_private;
5633 struct inode *inode = dip->inode;
5634 struct btrfs_root *root = BTRFS_I(inode)->root;
5635 struct btrfs_trans_handle *trans;
5636 struct btrfs_ordered_extent *ordered = NULL;
5637 struct extent_state *cached_state = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05005638 u64 ordered_offset = dip->logical_offset;
5639 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005640 int ret;
5641
5642 if (err)
5643 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05005644again:
5645 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
5646 &ordered_offset,
5647 ordered_bytes);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005648 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05005649 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005650
5651 BUG_ON(!ordered);
5652
5653 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005654 if (IS_ERR(trans)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005655 err = -ENOMEM;
5656 goto out;
5657 }
5658 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5659
5660 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
5661 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5662 if (!ret)
5663 ret = btrfs_update_inode(trans, root, inode);
5664 err = ret;
5665 goto out;
5666 }
5667
5668 lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5669 ordered->file_offset + ordered->len - 1, 0,
5670 &cached_state, GFP_NOFS);
5671
5672 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
5673 ret = btrfs_mark_extent_written(trans, inode,
5674 ordered->file_offset,
5675 ordered->file_offset +
5676 ordered->len);
5677 if (ret) {
5678 err = ret;
5679 goto out_unlock;
5680 }
5681 } else {
5682 ret = insert_reserved_file_extent(trans, inode,
5683 ordered->file_offset,
5684 ordered->start,
5685 ordered->disk_len,
5686 ordered->len,
5687 ordered->len,
5688 0, 0, 0,
5689 BTRFS_FILE_EXTENT_REG);
5690 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
5691 ordered->file_offset, ordered->len);
5692 if (ret) {
5693 err = ret;
5694 WARN_ON(1);
5695 goto out_unlock;
5696 }
5697 }
5698
5699 add_pending_csums(trans, inode, ordered->file_offset, &ordered->list);
5700 btrfs_ordered_update_i_size(inode, 0, ordered);
5701 btrfs_update_inode(trans, root, inode);
5702out_unlock:
5703 unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5704 ordered->file_offset + ordered->len - 1,
5705 &cached_state, GFP_NOFS);
5706out:
5707 btrfs_delalloc_release_metadata(inode, ordered->len);
5708 btrfs_end_transaction(trans, root);
Chris Mason163cf092010-11-28 19:56:33 -05005709 ordered_offset = ordered->file_offset + ordered->len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005710 btrfs_put_ordered_extent(ordered);
5711 btrfs_put_ordered_extent(ordered);
Chris Mason163cf092010-11-28 19:56:33 -05005712
5713out_test:
5714 /*
5715 * our bio might span multiple ordered extents. If we haven't
5716 * completed the accounting for the whole dio, go back and try again
5717 */
5718 if (ordered_offset < dip->logical_offset + dip->bytes) {
5719 ordered_bytes = dip->logical_offset + dip->bytes -
5720 ordered_offset;
5721 goto again;
5722 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005723out_done:
5724 bio->bi_private = dip->private;
5725
5726 kfree(dip->csums);
5727 kfree(dip);
5728 dio_end_io(bio, err);
5729}
5730
Chris Masoneaf25d92010-05-25 09:48:28 -04005731static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
5732 struct bio *bio, int mirror_num,
5733 unsigned long bio_flags, u64 offset)
5734{
5735 int ret;
5736 struct btrfs_root *root = BTRFS_I(inode)->root;
5737 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
5738 BUG_ON(ret);
5739 return 0;
5740}
5741
Miao Xiee65e1532010-11-22 03:04:43 +00005742static void btrfs_end_dio_bio(struct bio *bio, int err)
5743{
5744 struct btrfs_dio_private *dip = bio->bi_private;
5745
5746 if (err) {
5747 printk(KERN_ERR "btrfs direct IO failed ino %lu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00005748 "sector %#Lx len %u err no %d\n",
5749 dip->inode->i_ino, bio->bi_rw,
5750 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00005751 dip->errors = 1;
5752
5753 /*
5754 * before atomic variable goto zero, we must make sure
5755 * dip->errors is perceived to be set.
5756 */
5757 smp_mb__before_atomic_dec();
5758 }
5759
5760 /* if there are more bios still pending for this dio, just exit */
5761 if (!atomic_dec_and_test(&dip->pending_bios))
5762 goto out;
5763
5764 if (dip->errors)
5765 bio_io_error(dip->orig_bio);
5766 else {
5767 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
5768 bio_endio(dip->orig_bio, 0);
5769 }
5770out:
5771 bio_put(bio);
5772}
5773
5774static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
5775 u64 first_sector, gfp_t gfp_flags)
5776{
5777 int nr_vecs = bio_get_nr_vecs(bdev);
5778 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
5779}
5780
5781static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
5782 int rw, u64 file_offset, int skip_sum,
5783 u32 *csums)
5784{
5785 int write = rw & REQ_WRITE;
5786 struct btrfs_root *root = BTRFS_I(inode)->root;
5787 int ret;
5788
5789 bio_get(bio);
5790 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
5791 if (ret)
5792 goto err;
5793
5794 if (write && !skip_sum) {
5795 ret = btrfs_wq_submit_bio(root->fs_info,
5796 inode, rw, bio, 0, 0,
5797 file_offset,
5798 __btrfs_submit_bio_start_direct_io,
5799 __btrfs_submit_bio_done);
5800 goto err;
5801 } else if (!skip_sum)
5802 btrfs_lookup_bio_sums_dio(root, inode, bio,
5803 file_offset, csums);
5804
5805 ret = btrfs_map_bio(root, rw, bio, 0, 1);
5806err:
5807 bio_put(bio);
5808 return ret;
5809}
5810
5811static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
5812 int skip_sum)
5813{
5814 struct inode *inode = dip->inode;
5815 struct btrfs_root *root = BTRFS_I(inode)->root;
5816 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5817 struct bio *bio;
5818 struct bio *orig_bio = dip->orig_bio;
5819 struct bio_vec *bvec = orig_bio->bi_io_vec;
5820 u64 start_sector = orig_bio->bi_sector;
5821 u64 file_offset = dip->logical_offset;
5822 u64 submit_len = 0;
5823 u64 map_length;
5824 int nr_pages = 0;
5825 u32 *csums = dip->csums;
5826 int ret = 0;
5827
5828 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
5829 if (!bio)
5830 return -ENOMEM;
5831 bio->bi_private = dip;
5832 bio->bi_end_io = btrfs_end_dio_bio;
5833 atomic_inc(&dip->pending_bios);
5834
5835 map_length = orig_bio->bi_size;
5836 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
5837 &map_length, NULL, 0);
5838 if (ret) {
5839 bio_put(bio);
5840 return -EIO;
5841 }
5842
5843 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
5844 if (unlikely(map_length < submit_len + bvec->bv_len ||
5845 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5846 bvec->bv_offset) < bvec->bv_len)) {
5847 /*
5848 * inc the count before we submit the bio so
5849 * we know the end IO handler won't happen before
5850 * we inc the count. Otherwise, the dip might get freed
5851 * before we're done setting it up
5852 */
5853 atomic_inc(&dip->pending_bios);
5854 ret = __btrfs_submit_dio_bio(bio, inode, rw,
5855 file_offset, skip_sum,
5856 csums);
5857 if (ret) {
5858 bio_put(bio);
5859 atomic_dec(&dip->pending_bios);
5860 goto out_err;
5861 }
5862
5863 if (!skip_sum)
5864 csums = csums + nr_pages;
5865 start_sector += submit_len >> 9;
5866 file_offset += submit_len;
5867
5868 submit_len = 0;
5869 nr_pages = 0;
5870
5871 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
5872 start_sector, GFP_NOFS);
5873 if (!bio)
5874 goto out_err;
5875 bio->bi_private = dip;
5876 bio->bi_end_io = btrfs_end_dio_bio;
5877
5878 map_length = orig_bio->bi_size;
5879 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
5880 &map_length, NULL, 0);
5881 if (ret) {
5882 bio_put(bio);
5883 goto out_err;
5884 }
5885 } else {
5886 submit_len += bvec->bv_len;
5887 nr_pages ++;
5888 bvec++;
5889 }
5890 }
5891
5892 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
5893 csums);
5894 if (!ret)
5895 return 0;
5896
5897 bio_put(bio);
5898out_err:
5899 dip->errors = 1;
5900 /*
5901 * before atomic variable goto zero, we must
5902 * make sure dip->errors is perceived to be set.
5903 */
5904 smp_mb__before_atomic_dec();
5905 if (atomic_dec_and_test(&dip->pending_bios))
5906 bio_io_error(dip->orig_bio);
5907
5908 /* bio_end_io() will handle error, so we needn't return it */
5909 return 0;
5910}
5911
Josef Bacik4b46fce2010-05-23 11:00:55 -04005912static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
5913 loff_t file_offset)
5914{
5915 struct btrfs_root *root = BTRFS_I(inode)->root;
5916 struct btrfs_dio_private *dip;
5917 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005918 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005919 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005920 int ret = 0;
5921
5922 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
5923
5924 dip = kmalloc(sizeof(*dip), GFP_NOFS);
5925 if (!dip) {
5926 ret = -ENOMEM;
5927 goto free_ordered;
5928 }
5929 dip->csums = NULL;
5930
5931 if (!skip_sum) {
5932 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
5933 if (!dip->csums) {
5934 ret = -ENOMEM;
5935 goto free_ordered;
5936 }
5937 }
5938
5939 dip->private = bio->bi_private;
5940 dip->inode = inode;
5941 dip->logical_offset = file_offset;
5942
Josef Bacik4b46fce2010-05-23 11:00:55 -04005943 dip->bytes = 0;
5944 do {
5945 dip->bytes += bvec->bv_len;
5946 bvec++;
5947 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
5948
Chris Mason46bfbb52010-05-26 11:04:10 -04005949 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005950 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00005951 dip->errors = 0;
5952 dip->orig_bio = bio;
5953 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005954
5955 if (write)
5956 bio->bi_end_io = btrfs_endio_direct_write;
5957 else
5958 bio->bi_end_io = btrfs_endio_direct_read;
5959
Miao Xiee65e1532010-11-22 03:04:43 +00005960 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
5961 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04005962 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005963free_ordered:
5964 /*
5965 * If this is a write, we need to clean up the reserved space and kill
5966 * the ordered extent.
5967 */
5968 if (write) {
5969 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05005970 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005971 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
5972 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
5973 btrfs_free_reserved_extent(root, ordered->start,
5974 ordered->disk_len);
5975 btrfs_put_ordered_extent(ordered);
5976 btrfs_put_ordered_extent(ordered);
5977 }
5978 bio_endio(bio, ret);
5979}
5980
Chris Mason5a5f79b2010-05-26 21:33:37 -04005981static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
5982 const struct iovec *iov, loff_t offset,
5983 unsigned long nr_segs)
5984{
5985 int seg;
5986 size_t size;
5987 unsigned long addr;
5988 unsigned blocksize_mask = root->sectorsize - 1;
5989 ssize_t retval = -EINVAL;
5990 loff_t end = offset;
5991
5992 if (offset & blocksize_mask)
5993 goto out;
5994
5995 /* Check the memory alignment. Blocks cannot straddle pages */
5996 for (seg = 0; seg < nr_segs; seg++) {
5997 addr = (unsigned long)iov[seg].iov_base;
5998 size = iov[seg].iov_len;
5999 end += size;
6000 if ((addr & blocksize_mask) || (size & blocksize_mask))
6001 goto out;
6002 }
6003 retval = 0;
6004out:
6005 return retval;
6006}
Chris Mason16432982008-04-10 10:23:21 -04006007static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6008 const struct iovec *iov, loff_t offset,
6009 unsigned long nr_segs)
6010{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006011 struct file *file = iocb->ki_filp;
6012 struct inode *inode = file->f_mapping->host;
6013 struct btrfs_ordered_extent *ordered;
Chris Mason4845e442010-05-25 20:56:50 -04006014 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006015 u64 lockstart, lockend;
6016 ssize_t ret;
Chris Mason4845e442010-05-25 20:56:50 -04006017 int writing = rw & WRITE;
6018 int write_bits = 0;
Chris Mason3f7c5792010-05-26 10:59:53 -04006019 size_t count = iov_length(iov, nr_segs);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006020
Chris Mason5a5f79b2010-05-26 21:33:37 -04006021 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
6022 offset, nr_segs)) {
6023 return 0;
6024 }
6025
Josef Bacik4b46fce2010-05-23 11:00:55 -04006026 lockstart = offset;
Chris Mason3f7c5792010-05-26 10:59:53 -04006027 lockend = offset + count - 1;
6028
6029 if (writing) {
6030 ret = btrfs_delalloc_reserve_space(inode, count);
6031 if (ret)
6032 goto out;
6033 }
Chris Mason4845e442010-05-25 20:56:50 -04006034
Josef Bacik4b46fce2010-05-23 11:00:55 -04006035 while (1) {
Chris Mason4845e442010-05-25 20:56:50 -04006036 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6037 0, &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006038 /*
6039 * We're concerned with the entire range that we're going to be
6040 * doing DIO to, so we need to make sure theres no ordered
6041 * extents in this range.
6042 */
6043 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6044 lockend - lockstart + 1);
6045 if (!ordered)
6046 break;
Chris Mason4845e442010-05-25 20:56:50 -04006047 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6048 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006049 btrfs_start_ordered_extent(inode, ordered, 1);
6050 btrfs_put_ordered_extent(ordered);
6051 cond_resched();
6052 }
6053
Chris Mason4845e442010-05-25 20:56:50 -04006054 /*
6055 * we don't use btrfs_set_extent_delalloc because we don't want
6056 * the dirty or uptodate bits
6057 */
6058 if (writing) {
6059 write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
6060 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6061 EXTENT_DELALLOC, 0, NULL, &cached_state,
6062 GFP_NOFS);
6063 if (ret) {
6064 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6065 lockend, EXTENT_LOCKED | write_bits,
6066 1, 0, &cached_state, GFP_NOFS);
6067 goto out;
6068 }
6069 }
6070
6071 free_extent_state(cached_state);
6072 cached_state = NULL;
6073
Chris Mason5a5f79b2010-05-26 21:33:37 -04006074 ret = __blockdev_direct_IO(rw, iocb, inode,
6075 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6076 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6077 btrfs_submit_direct, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006078
6079 if (ret < 0 && ret != -EIOCBQUEUED) {
Chris Mason4845e442010-05-25 20:56:50 -04006080 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
6081 offset + iov_length(iov, nr_segs) - 1,
6082 EXTENT_LOCKED | write_bits, 1, 0,
6083 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006084 } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
6085 /*
6086 * We're falling back to buffered, unlock the section we didn't
6087 * do IO on.
6088 */
Chris Mason4845e442010-05-25 20:56:50 -04006089 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
6090 offset + iov_length(iov, nr_segs) - 1,
6091 EXTENT_LOCKED | write_bits, 1, 0,
6092 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006093 }
Chris Mason4845e442010-05-25 20:56:50 -04006094out:
6095 free_extent_state(cached_state);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006096 return ret;
Chris Mason16432982008-04-10 10:23:21 -04006097}
6098
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006099static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6100 __u64 start, __u64 len)
6101{
6102 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
6103}
6104
Chris Mason9ebefb182007-06-15 13:50:00 -04006105int btrfs_readpage(struct file *file, struct page *page)
6106{
Chris Masond1310b22008-01-24 16:13:08 -05006107 struct extent_io_tree *tree;
6108 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006109 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04006110}
Chris Mason1832a6d2007-12-21 16:27:21 -05006111
Chris Mason39279cc2007-06-12 06:35:45 -04006112static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6113{
Chris Masond1310b22008-01-24 16:13:08 -05006114 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04006115
6116
6117 if (current->flags & PF_MEMALLOC) {
6118 redirty_page_for_writepage(wbc, page);
6119 unlock_page(page);
6120 return 0;
6121 }
Chris Masond1310b22008-01-24 16:13:08 -05006122 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006123 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6124}
Chris Mason39279cc2007-06-12 06:35:45 -04006125
Chris Masonf4219502008-07-22 11:18:09 -04006126int btrfs_writepages(struct address_space *mapping,
6127 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04006128{
Chris Masond1310b22008-01-24 16:13:08 -05006129 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006130
Chris Masond1310b22008-01-24 16:13:08 -05006131 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04006132 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6133}
6134
Chris Mason3ab2fb52007-11-08 10:59:22 -05006135static int
6136btrfs_readpages(struct file *file, struct address_space *mapping,
6137 struct list_head *pages, unsigned nr_pages)
6138{
Chris Masond1310b22008-01-24 16:13:08 -05006139 struct extent_io_tree *tree;
6140 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006141 return extent_readpages(tree, mapping, pages, nr_pages,
6142 btrfs_get_extent);
6143}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006144static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006145{
Chris Masond1310b22008-01-24 16:13:08 -05006146 struct extent_io_tree *tree;
6147 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006148 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006149
Chris Masond1310b22008-01-24 16:13:08 -05006150 tree = &BTRFS_I(page->mapping->host)->io_tree;
6151 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006152 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006153 if (ret == 1) {
6154 ClearPagePrivate(page);
6155 set_page_private(page, 0);
6156 page_cache_release(page);
6157 }
6158 return ret;
6159}
Chris Mason39279cc2007-06-12 06:35:45 -04006160
Chris Masone6dcd2d2008-07-17 12:53:50 -04006161static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6162{
Chris Mason98509cf2008-09-11 15:51:43 -04006163 if (PageWriteback(page) || PageDirty(page))
6164 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006165 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006166}
6167
Chris Masona52d9a82007-08-27 16:49:44 -04006168static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6169{
Chris Masond1310b22008-01-24 16:13:08 -05006170 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006171 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006172 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006173 u64 page_start = page_offset(page);
6174 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006175
Chris Mason8b62b722009-09-02 16:53:46 -04006176
6177 /*
6178 * we have the page locked, so new writeback can't start,
6179 * and the dirty bit won't be cleared while we are here.
6180 *
6181 * Wait for IO on this page so that we can safely clear
6182 * the PagePrivate2 bit and do ordered accounting
6183 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006184 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006185
Chris Masond1310b22008-01-24 16:13:08 -05006186 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006187 if (offset) {
6188 btrfs_releasepage(page, GFP_NOFS);
6189 return;
6190 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00006191 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6192 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006193 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
6194 page_offset(page));
6195 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006196 /*
6197 * IO on this page will never be started, so we need
6198 * to account for any ordered extents now
6199 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006200 clear_extent_bit(tree, page_start, page_end,
6201 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04006202 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006203 &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006204 /*
6205 * whoever cleared the private bit is responsible
6206 * for the finish_ordered_io
6207 */
6208 if (TestClearPagePrivate2(page)) {
6209 btrfs_finish_ordered_io(page->mapping->host,
6210 page_start, page_end);
6211 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006212 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006213 cached_state = NULL;
6214 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6215 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006216 }
6217 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006218 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +00006219 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006220 __btrfs_releasepage(page, GFP_NOFS);
6221
Chris Mason4a096752008-07-21 10:29:44 -04006222 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006223 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006224 ClearPagePrivate(page);
6225 set_page_private(page, 0);
6226 page_cache_release(page);
6227 }
Chris Mason39279cc2007-06-12 06:35:45 -04006228}
6229
Chris Mason9ebefb182007-06-15 13:50:00 -04006230/*
6231 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6232 * called from a page fault handler when a page is first dirtied. Hence we must
6233 * be careful to check for EOF conditions here. We set the page up correctly
6234 * for a written page which means we get ENOSPC checking when writing into
6235 * holes and correct delalloc and unwritten extent mapping on filesystems that
6236 * support these features.
6237 *
6238 * We are not allowed to take the i_mutex here so we have to play games to
6239 * protect against truncate races as the page could now be beyond EOF. Because
6240 * vmtruncate() writes the inode size before removing pages, once we have the
6241 * page lock we can determine safely if the page is beyond EOF. If it is not
6242 * beyond EOF, then the page is guaranteed safe against truncation until we
6243 * unlock the page.
6244 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006245int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006246{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006247 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006248 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006249 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006250 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6251 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006252 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006253 char *kaddr;
6254 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006255 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006256 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04006257 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006258 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006259
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006260 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Nick Piggin56a76f82009-03-31 15:23:23 -07006261 if (ret) {
6262 if (ret == -ENOMEM)
6263 ret = VM_FAULT_OOM;
6264 else /* -ENOSPC, -EIO, etc */
6265 ret = VM_FAULT_SIGBUS;
Chris Mason1832a6d2007-12-21 16:27:21 -05006266 goto out;
Nick Piggin56a76f82009-03-31 15:23:23 -07006267 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006268
Nick Piggin56a76f82009-03-31 15:23:23 -07006269 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006270again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006271 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006272 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006273 page_start = page_offset(page);
6274 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006275
Chris Mason9ebefb182007-06-15 13:50:00 -04006276 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006277 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006278 /* page got truncated out from underneath us */
6279 goto out_unlock;
6280 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006281 wait_on_page_writeback(page);
6282
Josef Bacik2ac55d42010-02-03 19:33:23 +00006283 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
6284 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006285 set_page_extent_mapped(page);
6286
Chris Masoneb84ae02008-07-17 13:53:27 -04006287 /*
6288 * we can't set the delalloc bits if there are pending ordered
6289 * extents. Drop our locks and wait for them to finish
6290 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006291 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6292 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006293 unlock_extent_cached(io_tree, page_start, page_end,
6294 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006295 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006296 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006297 btrfs_put_ordered_extent(ordered);
6298 goto again;
6299 }
6300
Josef Bacikfbf19082009-10-01 17:10:23 -04006301 /*
6302 * XXX - page_mkwrite gets called every time the page is dirtied, even
6303 * if it was already dirty, so for space accounting reasons we need to
6304 * clear any delalloc bits for the range we are fixing to save. There
6305 * is probably a better way to do this, but for now keep consistent with
6306 * prepare_pages in the normal write path.
6307 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006308 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006309 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006310 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006311
Josef Bacik2ac55d42010-02-03 19:33:23 +00006312 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6313 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006314 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006315 unlock_extent_cached(io_tree, page_start, page_end,
6316 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006317 ret = VM_FAULT_SIGBUS;
6318 goto out_unlock;
6319 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006320 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006321
6322 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006323 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006324 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006325 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006326 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006327
Chris Masone6dcd2d2008-07-17 12:53:50 -04006328 if (zero_start != PAGE_CACHE_SIZE) {
6329 kaddr = kmap(page);
6330 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6331 flush_dcache_page(page);
6332 kunmap(page);
6333 }
Chris Mason247e7432008-07-17 12:53:51 -04006334 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006335 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006336 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006337
Chris Mason257c62e2009-10-13 13:21:08 -04006338 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6339 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
6340
Josef Bacik2ac55d42010-02-03 19:33:23 +00006341 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006342
6343out_unlock:
Chris Mason50a9b212009-09-11 12:33:12 -04006344 if (!ret)
6345 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04006346 unlock_page(page);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006347 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason1832a6d2007-12-21 16:27:21 -05006348out:
Chris Mason9ebefb182007-06-15 13:50:00 -04006349 return ret;
6350}
6351
Chris Mason39279cc2007-06-12 06:35:45 -04006352static void btrfs_truncate(struct inode *inode)
6353{
6354 struct btrfs_root *root = BTRFS_I(inode)->root;
6355 int ret;
6356 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04006357 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04006358 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04006359
Yan, Zheng80825102009-11-12 09:35:36 +00006360 if (!S_ISREG(inode->i_mode)) {
6361 WARN_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -04006362 return;
Yan, Zheng80825102009-11-12 09:35:36 +00006363 }
Chris Mason39279cc2007-06-12 06:35:45 -04006364
Josef Bacik5d5e1032009-10-13 16:46:49 -04006365 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
6366 if (ret)
6367 return;
Yan, Zheng80825102009-11-12 09:35:36 +00006368
Chris Mason4a096752008-07-21 10:29:44 -04006369 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006370 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006371
Yan, Zhengd68fc572010-05-16 10:49:58 -04006372 trans = btrfs_start_transaction(root, 0);
6373 BUG_ON(IS_ERR(trans));
Yan, Zheng80825102009-11-12 09:35:36 +00006374 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006375 trans->block_rsv = root->orphan_block_rsv;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006376
6377 /*
6378 * setattr is responsible for setting the ordered_data_close flag,
6379 * but that is only tested during the last file release. That
6380 * could happen well after the next commit, leaving a great big
6381 * window where new writes may get lost if someone chooses to write
6382 * to this file after truncating to zero
6383 *
6384 * The inode doesn't have any dirty data here, and so if we commit
6385 * this is a noop. If someone immediately starts writing to the inode
6386 * it is very likely we'll catch some of their writes in this
6387 * transaction, and the commit will find this file on the ordered
6388 * data list with good things to send down.
6389 *
6390 * This is a best effort solution, there is still a window where
6391 * using truncate to replace the contents of the file will
6392 * end up with a zero length file after a crash.
6393 */
6394 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
6395 btrfs_add_ordered_operation(trans, root, inode);
6396
Yan, Zheng80825102009-11-12 09:35:36 +00006397 while (1) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04006398 if (!trans) {
6399 trans = btrfs_start_transaction(root, 0);
6400 BUG_ON(IS_ERR(trans));
6401 btrfs_set_trans_block_group(trans, inode);
6402 trans->block_rsv = root->orphan_block_rsv;
6403 }
6404
6405 ret = btrfs_block_rsv_check(trans, root,
6406 root->orphan_block_rsv, 0, 5);
6407 if (ret) {
6408 BUG_ON(ret != -EAGAIN);
6409 ret = btrfs_commit_transaction(trans, root);
6410 BUG_ON(ret);
6411 trans = NULL;
6412 continue;
6413 }
6414
Yan, Zheng80825102009-11-12 09:35:36 +00006415 ret = btrfs_truncate_inode_items(trans, root, inode,
6416 inode->i_size,
6417 BTRFS_EXTENT_DATA_KEY);
6418 if (ret != -EAGAIN)
6419 break;
Chris Mason39279cc2007-06-12 06:35:45 -04006420
Yan, Zheng80825102009-11-12 09:35:36 +00006421 ret = btrfs_update_inode(trans, root, inode);
6422 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04006423
Yan, Zheng80825102009-11-12 09:35:36 +00006424 nr = trans->blocks_used;
6425 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006426 trans = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00006427 btrfs_btree_balance_dirty(root, nr);
Yan, Zheng80825102009-11-12 09:35:36 +00006428 }
6429
6430 if (ret == 0 && inode->i_nlink > 0) {
6431 ret = btrfs_orphan_del(trans, inode);
6432 BUG_ON(ret);
6433 }
6434
6435 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04006436 BUG_ON(ret);
6437
Josef Bacik7b128762008-07-24 12:17:14 -04006438 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04006439 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006440 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04006441 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04006442}
6443
Sven Wegener3b963622008-06-09 21:57:42 -04006444/*
Chris Masond352ac62008-09-29 15:18:18 -04006445 * create a new subvolume directory/inode (helper for the ioctl).
6446 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05006447int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Yan, Zheng76dda932009-09-21 16:00:26 -04006448 struct btrfs_root *new_root,
Yan Zhengd2fb3432008-12-11 16:30:39 -05006449 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04006450{
Chris Mason39279cc2007-06-12 06:35:45 -04006451 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04006452 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006453 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006454
Josef Bacikaec74772008-07-24 12:12:38 -04006455 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05006456 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04006457 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04006458 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006459 inode->i_op = &btrfs_dir_inode_operations;
6460 inode->i_fop = &btrfs_dir_file_operations;
6461
Chris Mason39279cc2007-06-12 06:35:45 -04006462 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04006463 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04006464
Yan, Zheng76dda932009-09-21 16:00:26 -04006465 err = btrfs_update_inode(trans, new_root, inode);
6466 BUG_ON(err);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006467
Yan, Zheng76dda932009-09-21 16:00:26 -04006468 iput(inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006469 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006470}
6471
Chris Masond352ac62008-09-29 15:18:18 -04006472/* helper function for file defrag and space balancing. This
6473 * forces readahead on a given range of bytes in an inode
6474 */
Chris Masonedbd8d42007-12-21 16:27:24 -05006475unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04006476 struct file_ra_state *ra, struct file *file,
6477 pgoff_t offset, pgoff_t last_index)
6478{
Chris Mason8e7bf942008-04-28 09:02:36 -04006479 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04006480
Chris Mason86479a02007-09-10 19:58:16 -04006481 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
6482 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04006483}
6484
Chris Mason39279cc2007-06-12 06:35:45 -04006485struct inode *btrfs_alloc_inode(struct super_block *sb)
6486{
6487 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006488 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006489
6490 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
6491 if (!ei)
6492 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006493
6494 ei->root = NULL;
6495 ei->space_info = NULL;
6496 ei->generation = 0;
6497 ei->sequence = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04006498 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04006499 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04006500 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006501 ei->delalloc_bytes = 0;
6502 ei->reserved_bytes = 0;
6503 ei->disk_i_size = 0;
6504 ei->flags = 0;
6505 ei->index_cnt = (u64)-1;
6506 ei->last_unlink_trans = 0;
6507
Josef Bacik32c00af2009-10-08 13:34:05 -04006508 spin_lock_init(&ei->accounting_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006509 atomic_set(&ei->outstanding_extents, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006510 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006511
6512 ei->ordered_data_close = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04006513 ei->orphan_meta_reserved = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006514 ei->dummy_inode = 0;
Li Zefan261507a02010-12-17 14:21:50 +08006515 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006516
6517 inode = &ei->vfs_inode;
6518 extent_map_tree_init(&ei->extent_tree, GFP_NOFS);
6519 extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS);
6520 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS);
6521 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006522 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04006523 INIT_LIST_HEAD(&ei->i_orphan);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006524 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006525 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006526 RB_CLEAR_NODE(&ei->rb_node);
6527
6528 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006529}
6530
6531void btrfs_destroy_inode(struct inode *inode)
6532{
Chris Masone6dcd2d2008-07-17 12:53:50 -04006533 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006534 struct btrfs_root *root = BTRFS_I(inode)->root;
6535
Chris Mason39279cc2007-06-12 06:35:45 -04006536 WARN_ON(!list_empty(&inode->i_dentry));
6537 WARN_ON(inode->i_data.nrpages);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006538 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents));
6539 WARN_ON(BTRFS_I(inode)->reserved_extents);
Chris Mason39279cc2007-06-12 06:35:45 -04006540
Chris Mason5a3f23d2009-03-31 13:27:11 -04006541 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05006542 * This can happen where we create an inode, but somebody else also
6543 * created the same inode and we need to destroy the one we already
6544 * created.
6545 */
6546 if (!root)
6547 goto free;
6548
6549 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04006550 * Make sure we're properly removed from the ordered operation
6551 * lists.
6552 */
6553 smp_mb();
6554 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
6555 spin_lock(&root->fs_info->ordered_extent_lock);
6556 list_del_init(&BTRFS_I(inode)->ordered_operations);
6557 spin_unlock(&root->fs_info->ordered_extent_lock);
6558 }
6559
Josef Bacik0af3d002010-06-21 14:48:16 -04006560 if (root == root->fs_info->tree_root) {
6561 struct btrfs_block_group_cache *block_group;
6562
6563 block_group = btrfs_lookup_block_group(root->fs_info,
6564 BTRFS_I(inode)->block_group);
6565 if (block_group && block_group->inode == inode) {
6566 spin_lock(&block_group->lock);
6567 block_group->inode = NULL;
6568 spin_unlock(&block_group->lock);
6569 btrfs_put_block_group(block_group);
6570 } else if (block_group) {
6571 btrfs_put_block_group(block_group);
6572 }
6573 }
6574
Yan, Zhengd68fc572010-05-16 10:49:58 -04006575 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006576 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yan, Zheng80825102009-11-12 09:35:36 +00006577 printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n",
6578 inode->i_ino);
6579 list_del_init(&BTRFS_I(inode)->i_orphan);
Josef Bacik7b128762008-07-24 12:17:14 -04006580 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04006581 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006582
Chris Masond3977122009-01-05 21:25:51 -05006583 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006584 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
6585 if (!ordered)
6586 break;
6587 else {
Chris Masond3977122009-01-05 21:25:51 -05006588 printk(KERN_ERR "btrfs found ordered "
6589 "extent %llu %llu on inode cleanup\n",
6590 (unsigned long long)ordered->file_offset,
6591 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006592 btrfs_remove_ordered_extent(inode, ordered);
6593 btrfs_put_ordered_extent(ordered);
6594 btrfs_put_ordered_extent(ordered);
6595 }
6596 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006597 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006598 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05006599free:
Chris Mason39279cc2007-06-12 06:35:45 -04006600 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
6601}
6602
Al Viro45321ac2010-06-07 13:43:19 -04006603int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04006604{
6605 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04006606
Josef Bacik0af3d002010-06-21 14:48:16 -04006607 if (btrfs_root_refs(&root->root_item) == 0 &&
6608 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04006609 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04006610 else
Al Viro45321ac2010-06-07 13:43:19 -04006611 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04006612}
6613
Sven Wegener0ee0fda2008-07-30 16:54:26 -04006614static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04006615{
6616 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
6617
6618 inode_init_once(&ei->vfs_inode);
6619}
6620
6621void btrfs_destroy_cachep(void)
6622{
6623 if (btrfs_inode_cachep)
6624 kmem_cache_destroy(btrfs_inode_cachep);
6625 if (btrfs_trans_handle_cachep)
6626 kmem_cache_destroy(btrfs_trans_handle_cachep);
6627 if (btrfs_transaction_cachep)
6628 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04006629 if (btrfs_path_cachep)
6630 kmem_cache_destroy(btrfs_path_cachep);
6631}
6632
6633int btrfs_init_cachep(void)
6634{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006635 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
6636 sizeof(struct btrfs_inode), 0,
6637 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04006638 if (!btrfs_inode_cachep)
6639 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006640
6641 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
6642 sizeof(struct btrfs_trans_handle), 0,
6643 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006644 if (!btrfs_trans_handle_cachep)
6645 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006646
6647 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
6648 sizeof(struct btrfs_transaction), 0,
6649 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006650 if (!btrfs_transaction_cachep)
6651 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006652
6653 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
6654 sizeof(struct btrfs_path), 0,
6655 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006656 if (!btrfs_path_cachep)
6657 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006658
Chris Mason39279cc2007-06-12 06:35:45 -04006659 return 0;
6660fail:
6661 btrfs_destroy_cachep();
6662 return -ENOMEM;
6663}
6664
6665static int btrfs_getattr(struct vfsmount *mnt,
6666 struct dentry *dentry, struct kstat *stat)
6667{
6668 struct inode *inode = dentry->d_inode;
6669 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05006670 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05006671 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006672 stat->blocks = (inode_get_bytes(inode) +
6673 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04006674 return 0;
6675}
6676
Chris Masond3977122009-01-05 21:25:51 -05006677static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
6678 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04006679{
6680 struct btrfs_trans_handle *trans;
6681 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006682 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04006683 struct inode *new_inode = new_dentry->d_inode;
6684 struct inode *old_inode = old_dentry->d_inode;
6685 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006686 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006687 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04006688 int ret;
6689
Yan, Zhengf679a842009-09-24 09:17:31 -04006690 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
6691 return -EPERM;
6692
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006693 /* we only allow rename subvolume link between subvolumes */
6694 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05006695 return -EXDEV;
6696
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006697 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
6698 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
6699 return -ENOTEMPTY;
6700
Chris Mason39279cc2007-06-12 06:35:45 -04006701 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006702 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04006703 return -ENOTEMPTY;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006704 /*
6705 * we're using rename to replace one file with another.
6706 * and the replacement file is large. Start IO on it now so
6707 * we don't add too much work to the end of the transaction
6708 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04006709 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04006710 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
6711 filemap_flush(old_inode->i_mapping);
6712
Yan, Zheng76dda932009-09-21 16:00:26 -04006713 /* close the racy window with snapshot create/destroy ioctl */
6714 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
6715 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006716 /*
6717 * We want to reserve the absolute worst case amount of items. So if
6718 * both inodes are subvols and we need to unlink them then that would
6719 * require 4 item modifications, but if they are both normal inodes it
6720 * would require 5 item modifications, so we'll assume their normal
6721 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
6722 * should cover the worst case number of items we'll modify.
6723 */
6724 trans = btrfs_start_transaction(root, 20);
6725 if (IS_ERR(trans))
6726 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04006727
Yan, Zhenga5719522009-09-24 09:17:31 -04006728 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04006729
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006730 if (dest != root)
6731 btrfs_record_root_in_trans(trans, dest);
6732
Yan, Zhenga5719522009-09-24 09:17:31 -04006733 ret = btrfs_set_inode_index(new_dir, &index);
6734 if (ret)
6735 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006736
Yan, Zhenga5719522009-09-24 09:17:31 -04006737 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006738 /* force full log commit if subvolume involved. */
6739 root->fs_info->last_trans_log_full_commit = trans->transid;
6740 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04006741 ret = btrfs_insert_inode_ref(trans, dest,
6742 new_dentry->d_name.name,
6743 new_dentry->d_name.len,
6744 old_inode->i_ino,
6745 new_dir->i_ino, index);
6746 if (ret)
6747 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006748 /*
6749 * this is an ugly little race, but the rename is required
6750 * to make sure that if we crash, the inode is either at the
6751 * old name or the new one. pinning the log transaction lets
6752 * us make sure we don't allow a log commit to come in after
6753 * we unlink the name but before we add the new name back in.
6754 */
6755 btrfs_pin_log_trans(root);
6756 }
Chris Mason12fcfd22009-03-24 10:24:20 -04006757 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04006758 * make sure the inode gets flushed if it is replacing
6759 * something.
6760 */
6761 if (new_inode && new_inode->i_size &&
6762 old_inode && S_ISREG(old_inode->i_mode)) {
6763 btrfs_add_ordered_operation(trans, root, old_inode);
6764 }
6765
Chris Mason39279cc2007-06-12 06:35:45 -04006766 old_dir->i_ctime = old_dir->i_mtime = ctime;
6767 new_dir->i_ctime = new_dir->i_mtime = ctime;
6768 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04006769
Chris Mason12fcfd22009-03-24 10:24:20 -04006770 if (old_dentry->d_parent != new_dentry->d_parent)
6771 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
6772
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006773 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
6774 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
6775 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
6776 old_dentry->d_name.name,
6777 old_dentry->d_name.len);
6778 } else {
6779 btrfs_inc_nlink(old_dentry->d_inode);
6780 ret = btrfs_unlink_inode(trans, root, old_dir,
6781 old_dentry->d_inode,
6782 old_dentry->d_name.name,
6783 old_dentry->d_name.len);
6784 }
6785 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006786
6787 if (new_inode) {
6788 new_inode->i_ctime = CURRENT_TIME;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006789 if (unlikely(new_inode->i_ino ==
6790 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
6791 root_objectid = BTRFS_I(new_inode)->location.objectid;
6792 ret = btrfs_unlink_subvol(trans, dest, new_dir,
6793 root_objectid,
6794 new_dentry->d_name.name,
6795 new_dentry->d_name.len);
6796 BUG_ON(new_inode->i_nlink == 0);
6797 } else {
6798 ret = btrfs_unlink_inode(trans, dest, new_dir,
6799 new_dentry->d_inode,
6800 new_dentry->d_name.name,
6801 new_dentry->d_name.len);
6802 }
6803 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04006804 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04006805 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006806 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04006807 }
Chris Mason39279cc2007-06-12 06:35:45 -04006808 }
Josef Bacikaec74772008-07-24 12:12:38 -04006809
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006810 ret = btrfs_add_link(trans, new_dir, old_inode,
6811 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04006812 new_dentry->d_name.len, 0, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006813 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006814
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006815 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik6a912212010-11-20 09:48:00 +00006816 struct dentry *parent = dget_parent(new_dentry);
6817 btrfs_log_new_name(trans, old_inode, old_dir, parent);
6818 dput(parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006819 btrfs_end_log_trans(root);
6820 }
Chris Mason39279cc2007-06-12 06:35:45 -04006821out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04006822 btrfs_end_transaction_throttle(trans, root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006823
Yan, Zheng76dda932009-09-21 16:00:26 -04006824 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
6825 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006826
Chris Mason39279cc2007-06-12 06:35:45 -04006827 return ret;
6828}
6829
Chris Masond352ac62008-09-29 15:18:18 -04006830/*
6831 * some fairly slow code that needs optimization. This walks the list
6832 * of all the inodes with pending delalloc and forces them to disk.
6833 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00006834int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04006835{
6836 struct list_head *head = &root->fs_info->delalloc_inodes;
6837 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006838 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04006839
Yan Zhengc146afa2008-11-12 14:34:12 -05006840 if (root->fs_info->sb->s_flags & MS_RDONLY)
6841 return -EROFS;
6842
Chris Mason75eff682008-12-15 15:54:40 -05006843 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05006844 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04006845 binode = list_entry(head->next, struct btrfs_inode,
6846 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006847 inode = igrab(&binode->vfs_inode);
6848 if (!inode)
6849 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05006850 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006851 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04006852 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00006853 if (delay_iput)
6854 btrfs_add_delayed_iput(inode);
6855 else
6856 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006857 }
6858 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05006859 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04006860 }
Chris Mason75eff682008-12-15 15:54:40 -05006861 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04006862
6863 /* the filemap_flush will queue IO into the worker threads, but
6864 * we have to make sure the IO is actually started and that
6865 * ordered extents get created before we return
6866 */
6867 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05006868 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05006869 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04006870 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05006871 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
6872 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04006873 }
6874 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04006875 return 0;
6876}
6877
Josef Bacik0019f102010-10-15 15:18:40 -04006878int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput,
6879 int sync)
Yan, Zheng5da9d012010-05-16 10:46:25 -04006880{
6881 struct btrfs_inode *binode;
6882 struct inode *inode = NULL;
6883
6884 spin_lock(&root->fs_info->delalloc_lock);
6885 while (!list_empty(&root->fs_info->delalloc_inodes)) {
6886 binode = list_entry(root->fs_info->delalloc_inodes.next,
6887 struct btrfs_inode, delalloc_inodes);
6888 inode = igrab(&binode->vfs_inode);
6889 if (inode) {
6890 list_move_tail(&binode->delalloc_inodes,
6891 &root->fs_info->delalloc_inodes);
6892 break;
6893 }
6894
6895 list_del_init(&binode->delalloc_inodes);
6896 cond_resched_lock(&root->fs_info->delalloc_lock);
6897 }
6898 spin_unlock(&root->fs_info->delalloc_lock);
6899
6900 if (inode) {
Josef Bacik0019f102010-10-15 15:18:40 -04006901 if (sync) {
6902 filemap_write_and_wait(inode->i_mapping);
6903 /*
6904 * We have to do this because compression doesn't
6905 * actually set PG_writeback until it submits the pages
6906 * for IO, which happens in an async thread, so we could
6907 * race and not actually wait for any writeback pages
6908 * because they've not been submitted yet. Technically
6909 * this could still be the case for the ordered stuff
6910 * since the async thread may not have started to do its
6911 * work yet. If this becomes the case then we need to
6912 * figure out a way to make sure that in writepage we
6913 * wait for any async pages to be submitted before
6914 * returning so that fdatawait does what its supposed to
6915 * do.
6916 */
6917 btrfs_wait_ordered_range(inode, 0, (u64)-1);
6918 } else {
6919 filemap_flush(inode->i_mapping);
6920 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04006921 if (delay_iput)
6922 btrfs_add_delayed_iput(inode);
6923 else
6924 iput(inode);
6925 return 1;
6926 }
6927 return 0;
6928}
6929
Chris Mason39279cc2007-06-12 06:35:45 -04006930static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
6931 const char *symname)
6932{
6933 struct btrfs_trans_handle *trans;
6934 struct btrfs_root *root = BTRFS_I(dir)->root;
6935 struct btrfs_path *path;
6936 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05006937 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006938 int err;
6939 int drop_inode = 0;
6940 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006941 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04006942 int name_len;
6943 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04006944 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006945 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04006946 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05006947 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006948
6949 name_len = strlen(symname) + 1;
6950 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
6951 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05006952
Yan, Zhenga22285a2010-05-16 10:48:46 -04006953 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
6954 if (err)
6955 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006956 /*
6957 * 2 items for inode item and ref
6958 * 2 items for dir items
6959 * 1 item for xattr if selinux is on
6960 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006961 trans = btrfs_start_transaction(root, 5);
6962 if (IS_ERR(trans))
6963 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006964
Chris Mason39279cc2007-06-12 06:35:45 -04006965 btrfs_set_trans_block_group(trans, dir);
6966
Josef Bacikaec74772008-07-24 12:12:38 -04006967 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Josef Bacika1b075d2010-11-19 20:36:11 +00006968 dentry->d_name.len, dir->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04006969 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
6970 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006971 err = PTR_ERR(inode);
6972 if (IS_ERR(inode))
6973 goto out_unlock;
6974
Yan, Zhengf34f57a2009-11-12 09:35:27 +00006975 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04006976 if (err) {
6977 drop_inode = 1;
6978 goto out_unlock;
6979 }
6980
Chris Mason39279cc2007-06-12 06:35:45 -04006981 btrfs_set_trans_block_group(trans, inode);
Josef Bacika1b075d2010-11-19 20:36:11 +00006982 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006983 if (err)
6984 drop_inode = 1;
6985 else {
6986 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04006987 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04006988 inode->i_fop = &btrfs_file_operations;
6989 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05006990 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04006991 }
Chris Mason39279cc2007-06-12 06:35:45 -04006992 btrfs_update_inode_block_group(trans, inode);
6993 btrfs_update_inode_block_group(trans, dir);
6994 if (drop_inode)
6995 goto out_unlock;
6996
6997 path = btrfs_alloc_path();
6998 BUG_ON(!path);
6999 key.objectid = inode->i_ino;
7000 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007001 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7002 datasize = btrfs_file_extent_calc_inline_size(name_len);
7003 err = btrfs_insert_empty_item(trans, root, path, &key,
7004 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007005 if (err) {
7006 drop_inode = 1;
7007 goto out_unlock;
7008 }
Chris Mason5f39d392007-10-15 16:14:19 -04007009 leaf = path->nodes[0];
7010 ei = btrfs_item_ptr(leaf, path->slots[0],
7011 struct btrfs_file_extent_item);
7012 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7013 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007014 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007015 btrfs_set_file_extent_encryption(leaf, ei, 0);
7016 btrfs_set_file_extent_compression(leaf, ei, 0);
7017 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7018 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7019
Chris Mason39279cc2007-06-12 06:35:45 -04007020 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007021 write_extent_buffer(leaf, symname, ptr, name_len);
7022 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007023 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007024
Chris Mason39279cc2007-06-12 06:35:45 -04007025 inode->i_op = &btrfs_symlink_inode_operations;
7026 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007027 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007028 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007029 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007030 err = btrfs_update_inode(trans, root, inode);
7031 if (err)
7032 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007033
7034out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007035 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04007036 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007037 if (drop_inode) {
7038 inode_dec_link_count(inode);
7039 iput(inode);
7040 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04007041 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04007042 return err;
7043}
Chris Mason16432982008-04-10 10:23:21 -04007044
Josef Bacik0af3d002010-06-21 14:48:16 -04007045static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7046 u64 start, u64 num_bytes, u64 min_size,
7047 loff_t actual_len, u64 *alloc_hint,
7048 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007049{
Yan Zhengd899e052008-10-30 14:25:28 -04007050 struct btrfs_root *root = BTRFS_I(inode)->root;
7051 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007052 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007053 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007054 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007055 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007056
Josef Bacik0af3d002010-06-21 14:48:16 -04007057 if (trans)
7058 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007059 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007060 if (own_trans) {
7061 trans = btrfs_start_transaction(root, 3);
7062 if (IS_ERR(trans)) {
7063 ret = PTR_ERR(trans);
7064 break;
7065 }
Yan Zhengd899e052008-10-30 14:25:28 -04007066 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007067
Yan, Zhengefa56462010-05-16 10:49:59 -04007068 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
7069 0, *alloc_hint, (u64)-1, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007070 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007071 if (own_trans)
7072 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007073 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007074 }
7075
Yan Zhengd899e052008-10-30 14:25:28 -04007076 ret = insert_reserved_file_extent(trans, inode,
7077 cur_offset, ins.objectid,
7078 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007079 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007080 BTRFS_FILE_EXTENT_PREALLOC);
7081 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -04007082 btrfs_drop_extent_cache(inode, cur_offset,
7083 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007084
Yan Zhengd899e052008-10-30 14:25:28 -04007085 num_bytes -= ins.offset;
7086 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007087 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007088
Yan Zhengd899e052008-10-30 14:25:28 -04007089 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007090 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007091 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007092 (actual_len > inode->i_size) &&
7093 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007094 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007095 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007096 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007097 i_size = cur_offset;
7098 i_size_write(inode, i_size);
7099 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007100 }
7101
Yan Zhengd899e052008-10-30 14:25:28 -04007102 ret = btrfs_update_inode(trans, root, inode);
7103 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04007104
Josef Bacik0af3d002010-06-21 14:48:16 -04007105 if (own_trans)
7106 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007107 }
Yan Zhengd899e052008-10-30 14:25:28 -04007108 return ret;
7109}
7110
Josef Bacik0af3d002010-06-21 14:48:16 -04007111int btrfs_prealloc_file_range(struct inode *inode, int mode,
7112 u64 start, u64 num_bytes, u64 min_size,
7113 loff_t actual_len, u64 *alloc_hint)
7114{
7115 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7116 min_size, actual_len, alloc_hint,
7117 NULL);
7118}
7119
7120int btrfs_prealloc_file_range_trans(struct inode *inode,
7121 struct btrfs_trans_handle *trans, int mode,
7122 u64 start, u64 num_bytes, u64 min_size,
7123 loff_t actual_len, u64 *alloc_hint)
7124{
7125 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7126 min_size, actual_len, alloc_hint, trans);
7127}
7128
Yan Zhengd899e052008-10-30 14:25:28 -04007129static long btrfs_fallocate(struct inode *inode, int mode,
7130 loff_t offset, loff_t len)
7131{
Josef Bacik2ac55d42010-02-03 19:33:23 +00007132 struct extent_state *cached_state = NULL;
Yan Zhengd899e052008-10-30 14:25:28 -04007133 u64 cur_offset;
7134 u64 last_byte;
7135 u64 alloc_start;
7136 u64 alloc_end;
7137 u64 alloc_hint = 0;
Chris Masone980b502009-04-24 14:39:24 -04007138 u64 locked_end;
Yan Zhengd899e052008-10-30 14:25:28 -04007139 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
7140 struct extent_map *em;
7141 int ret;
7142
7143 alloc_start = offset & ~mask;
7144 alloc_end = (offset + len + mask) & ~mask;
7145
Chris Mason546888d2009-04-21 11:53:38 -04007146 /*
7147 * wait for ordered IO before we have any locks. We'll loop again
7148 * below with the locks held.
7149 */
7150 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
7151
Yan Zhengd899e052008-10-30 14:25:28 -04007152 mutex_lock(&inode->i_mutex);
Josef Bacik0ed42a62010-11-22 18:55:39 +00007153 ret = inode_newsize_ok(inode, alloc_end);
7154 if (ret)
7155 goto out;
7156
Yan Zhengd899e052008-10-30 14:25:28 -04007157 if (alloc_start > inode->i_size) {
7158 ret = btrfs_cont_expand(inode, alloc_start);
7159 if (ret)
7160 goto out;
7161 }
7162
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007163 ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
Josef Bacika970b0a2009-06-27 21:07:34 -04007164 if (ret)
7165 goto out;
7166
Chris Masone980b502009-04-24 14:39:24 -04007167 locked_end = alloc_end - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04007168 while (1) {
7169 struct btrfs_ordered_extent *ordered;
Chris Mason546888d2009-04-21 11:53:38 -04007170
Chris Mason546888d2009-04-21 11:53:38 -04007171 /* the extent lock is ordered inside the running
7172 * transaction
7173 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007174 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
7175 locked_end, 0, &cached_state, GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04007176 ordered = btrfs_lookup_first_ordered_extent(inode,
7177 alloc_end - 1);
7178 if (ordered &&
7179 ordered->file_offset + ordered->len > alloc_start &&
7180 ordered->file_offset < alloc_end) {
7181 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007182 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7183 alloc_start, locked_end,
7184 &cached_state, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04007185 /*
7186 * we can't wait on the range with the transaction
7187 * running or with the extent lock held
7188 */
Yan Zhengd899e052008-10-30 14:25:28 -04007189 btrfs_wait_ordered_range(inode, alloc_start,
7190 alloc_end - alloc_start);
7191 } else {
7192 if (ordered)
7193 btrfs_put_ordered_extent(ordered);
7194 break;
7195 }
7196 }
7197
7198 cur_offset = alloc_start;
7199 while (1) {
7200 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
7201 alloc_end - cur_offset, 0);
7202 BUG_ON(IS_ERR(em) || !em);
7203 last_byte = min(extent_map_end(em), alloc_end);
7204 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007205 if (em->block_start == EXTENT_MAP_HOLE ||
7206 (cur_offset >= inode->i_size &&
7207 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
Josef Bacik83609772010-06-07 18:26:37 +00007208 ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
Yan, Zhengefa56462010-05-16 10:49:59 -04007209 last_byte - cur_offset,
7210 1 << inode->i_blkbits,
7211 offset + len,
7212 &alloc_hint);
Yan Zhengd899e052008-10-30 14:25:28 -04007213 if (ret < 0) {
7214 free_extent_map(em);
7215 break;
7216 }
7217 }
Yan Zhengd899e052008-10-30 14:25:28 -04007218 free_extent_map(em);
7219
7220 cur_offset = last_byte;
7221 if (cur_offset >= alloc_end) {
7222 ret = 0;
7223 break;
7224 }
7225 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00007226 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
7227 &cached_state, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04007228
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007229 btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
Yan Zhengd899e052008-10-30 14:25:28 -04007230out:
7231 mutex_unlock(&inode->i_mutex);
7232 return ret;
7233}
7234
Chris Masone6dcd2d2008-07-17 12:53:50 -04007235static int btrfs_set_page_dirty(struct page *page)
7236{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007237 return __set_page_dirty_nobuffers(page);
7238}
7239
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007240static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05007241{
Li Zefanb83cc962010-12-20 16:04:08 +08007242 struct btrfs_root *root = BTRFS_I(inode)->root;
7243
7244 if (btrfs_root_readonly(root) && (mask & MAY_WRITE))
7245 return -EROFS;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007246 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
Yanfdebe2b2008-01-14 13:26:08 -05007247 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04007248 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05007249}
Chris Mason39279cc2007-06-12 06:35:45 -04007250
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007251static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007252 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007253 .lookup = btrfs_lookup,
7254 .create = btrfs_create,
7255 .unlink = btrfs_unlink,
7256 .link = btrfs_link,
7257 .mkdir = btrfs_mkdir,
7258 .rmdir = btrfs_rmdir,
7259 .rename = btrfs_rename,
7260 .symlink = btrfs_symlink,
7261 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007262 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007263 .setxattr = btrfs_setxattr,
7264 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007265 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007266 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007267 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04007268};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007269static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007270 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007271 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04007272};
Yan, Zheng76dda932009-09-21 16:00:26 -04007273
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007274static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007275 .llseek = generic_file_llseek,
7276 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007277 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007278 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007279#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007280 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007281#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04007282 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04007283 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04007284};
7285
Chris Masond1310b22008-01-24 16:13:08 -05007286static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04007287 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05007288 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04007289 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007290 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007291 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04007292 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04007293 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05007294 .set_bit_hook = btrfs_set_bit_hook,
7295 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007296 .merge_extent_hook = btrfs_merge_extent_hook,
7297 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007298};
7299
Chris Mason35054392009-01-21 13:11:13 -05007300/*
7301 * btrfs doesn't support the bmap operation because swapfiles
7302 * use bmap to make a mapping of extents in the file. They assume
7303 * these extents won't change over the life of the file and they
7304 * use the bmap result to do IO directly to the drive.
7305 *
7306 * the btrfs bmap call would return logical addresses that aren't
7307 * suitable for IO and they also will change frequently as COW
7308 * operations happen. So, swapfile + btrfs == corruption.
7309 *
7310 * For now we're avoiding this by dropping bmap.
7311 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007312static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007313 .readpage = btrfs_readpage,
7314 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04007315 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05007316 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04007317 .sync_page = block_sync_page,
Chris Mason16432982008-04-10 10:23:21 -04007318 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04007319 .invalidatepage = btrfs_invalidatepage,
7320 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007321 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02007322 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04007323};
7324
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007325static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007326 .readpage = btrfs_readpage,
7327 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04007328 .invalidatepage = btrfs_invalidatepage,
7329 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04007330};
7331
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007332static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007333 .truncate = btrfs_truncate,
7334 .getattr = btrfs_getattr,
7335 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007336 .setxattr = btrfs_setxattr,
7337 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007338 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007339 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007340 .permission = btrfs_permission,
Yan Zhengd899e052008-10-30 14:25:28 -04007341 .fallocate = btrfs_fallocate,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007342 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04007343};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007344static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04007345 .getattr = btrfs_getattr,
7346 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007347 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007348 .setxattr = btrfs_setxattr,
7349 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04007350 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007351 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007352};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007353static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007354 .readlink = generic_readlink,
7355 .follow_link = page_follow_link_light,
7356 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00007357 .getattr = btrfs_getattr,
Yanfdebe2b2008-01-14 13:26:08 -05007358 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05007359 .setxattr = btrfs_setxattr,
7360 .getxattr = btrfs_getxattr,
7361 .listxattr = btrfs_listxattr,
7362 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007363};
Yan, Zheng76dda932009-09-21 16:00:26 -04007364
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04007365const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04007366 .d_delete = btrfs_dentry_delete,
7367};