blob: 36bc3f49ebf92113fc00427b20bdf01fe4f60778 [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 Masonf03d9302009-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 Masonf03d9302009-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);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002360 BUG_ON(IS_ERR(trans));
Josef Bacik7b128762008-07-24 12:17:14 -04002361 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002362 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04002363 iput(inode);
2364 continue;
2365 }
2366
2367 /* if we have links, this was a truncate, lets do that */
2368 if (inode->i_nlink) {
2369 nr_truncate++;
2370 btrfs_truncate(inode);
2371 } else {
2372 nr_unlink++;
2373 }
2374
2375 /* this will do delete_inode and everything for us */
2376 iput(inode);
2377 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002378 btrfs_free_path(path);
2379
2380 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2381
2382 if (root->orphan_block_rsv)
2383 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2384 (u64)-1);
2385
2386 if (root->orphan_block_rsv || root->orphan_item_inserted) {
2387 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00002388 BUG_ON(IS_ERR(trans));
Yan, Zhengd68fc572010-05-16 10:49:58 -04002389 btrfs_end_transaction(trans, root);
2390 }
Josef Bacik7b128762008-07-24 12:17:14 -04002391
2392 if (nr_unlink)
2393 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2394 if (nr_truncate)
2395 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik7b128762008-07-24 12:17:14 -04002396}
2397
Chris Masond352ac62008-09-29 15:18:18 -04002398/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002399 * very simple check to peek ahead in the leaf looking for xattrs. If we
2400 * don't find any xattrs, we know there can't be any acls.
2401 *
2402 * slot is the slot the inode is in, objectid is the objectid of the inode
2403 */
2404static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2405 int slot, u64 objectid)
2406{
2407 u32 nritems = btrfs_header_nritems(leaf);
2408 struct btrfs_key found_key;
2409 int scanned = 0;
2410
2411 slot++;
2412 while (slot < nritems) {
2413 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2414
2415 /* we found a different objectid, there must not be acls */
2416 if (found_key.objectid != objectid)
2417 return 0;
2418
2419 /* we found an xattr, assume we've got an acl */
2420 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2421 return 1;
2422
2423 /*
2424 * we found a key greater than an xattr key, there can't
2425 * be any acls later on
2426 */
2427 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2428 return 0;
2429
2430 slot++;
2431 scanned++;
2432
2433 /*
2434 * it goes inode, inode backrefs, xattrs, extents,
2435 * so if there are a ton of hard links to an inode there can
2436 * be a lot of backrefs. Don't waste time searching too hard,
2437 * this is just an optimization
2438 */
2439 if (scanned >= 8)
2440 break;
2441 }
2442 /* we hit the end of the leaf before we found an xattr or
2443 * something larger than an xattr. We have to assume the inode
2444 * has acls
2445 */
2446 return 1;
2447}
2448
2449/*
Chris Masond352ac62008-09-29 15:18:18 -04002450 * read an inode from the btree into the in-memory inode
2451 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002452static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002453{
2454 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002455 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002456 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002457 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002458 struct btrfs_root *root = BTRFS_I(inode)->root;
2459 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002460 int maybe_acls;
Chris Mason39279cc2007-06-12 06:35:45 -04002461 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04002462 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002463 int ret;
2464
2465 path = btrfs_alloc_path();
2466 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002467 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002468
Chris Mason39279cc2007-06-12 06:35:45 -04002469 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002470 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002471 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002472
Chris Mason5f39d392007-10-15 16:14:19 -04002473 leaf = path->nodes[0];
2474 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2475 struct btrfs_inode_item);
2476
2477 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2478 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2479 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2480 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002481 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002482
2483 tspec = btrfs_inode_atime(inode_item);
2484 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2485 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2486
2487 tspec = btrfs_inode_mtime(inode_item);
2488 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2489 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2490
2491 tspec = btrfs_inode_ctime(inode_item);
2492 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2493 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2494
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002495 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002496 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002497 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002498 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002499 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002500 rdev = btrfs_inode_rdev(leaf, inode_item);
2501
Josef Bacikaec74772008-07-24 12:12:38 -04002502 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002503 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002504
Chris Mason5f39d392007-10-15 16:14:19 -04002505 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002506
Chris Mason46a53cc2009-04-27 11:47:50 -04002507 /*
2508 * try to precache a NULL acl entry for files that don't have
2509 * any xattrs or acls
2510 */
2511 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
Al Viro72c04902009-06-24 16:58:48 -04002512 if (!maybe_acls)
2513 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002514
Yan Zhengd2fb3432008-12-11 16:30:39 -05002515 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2516 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002517 btrfs_free_path(path);
2518 inode_item = NULL;
2519
Chris Mason39279cc2007-06-12 06:35:45 -04002520 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002521 case S_IFREG:
2522 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002523 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002524 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002525 inode->i_fop = &btrfs_file_operations;
2526 inode->i_op = &btrfs_file_inode_operations;
2527 break;
2528 case S_IFDIR:
2529 inode->i_fop = &btrfs_dir_file_operations;
2530 if (root == root->fs_info->tree_root)
2531 inode->i_op = &btrfs_dir_ro_inode_operations;
2532 else
2533 inode->i_op = &btrfs_dir_inode_operations;
2534 break;
2535 case S_IFLNK:
2536 inode->i_op = &btrfs_symlink_inode_operations;
2537 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002538 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002539 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002540 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002541 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002542 init_special_inode(inode, inode->i_mode, rdev);
2543 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002544 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002545
2546 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002547 return;
2548
2549make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002550 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002551 make_bad_inode(inode);
2552}
2553
Chris Masond352ac62008-09-29 15:18:18 -04002554/*
2555 * given a leaf and an inode, copy the inode fields into the leaf
2556 */
Chris Masone02119d2008-09-05 16:13:11 -04002557static void fill_inode_item(struct btrfs_trans_handle *trans,
2558 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002559 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002560 struct inode *inode)
2561{
Chris Mason5f39d392007-10-15 16:14:19 -04002562 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2563 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002564 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002565 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2566 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2567
2568 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2569 inode->i_atime.tv_sec);
2570 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2571 inode->i_atime.tv_nsec);
2572
2573 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2574 inode->i_mtime.tv_sec);
2575 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2576 inode->i_mtime.tv_nsec);
2577
2578 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2579 inode->i_ctime.tv_sec);
2580 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2581 inode->i_ctime.tv_nsec);
2582
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002583 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002584 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002585 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002586 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002587 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002588 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002589 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Chris Mason39279cc2007-06-12 06:35:45 -04002590}
2591
Chris Masond352ac62008-09-29 15:18:18 -04002592/*
2593 * copy everything in the in-memory inode into the btree.
2594 */
Chris Masond3977122009-01-05 21:25:51 -05002595noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2596 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002597{
2598 struct btrfs_inode_item *inode_item;
2599 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002600 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002601 int ret;
2602
2603 path = btrfs_alloc_path();
2604 BUG_ON(!path);
Chris Masonb9473432009-03-13 11:00:37 -04002605 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002606 ret = btrfs_lookup_inode(trans, root, path,
2607 &BTRFS_I(inode)->location, 1);
2608 if (ret) {
2609 if (ret > 0)
2610 ret = -ENOENT;
2611 goto failed;
2612 }
2613
Chris Masonb4ce94d2009-02-04 09:25:08 -05002614 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002615 leaf = path->nodes[0];
2616 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002617 struct btrfs_inode_item);
2618
Chris Masone02119d2008-09-05 16:13:11 -04002619 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002620 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002621 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002622 ret = 0;
2623failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002624 btrfs_free_path(path);
2625 return ret;
2626}
2627
2628
Chris Masond352ac62008-09-29 15:18:18 -04002629/*
2630 * unlink helper that gets used here in inode.c and in the tree logging
2631 * recovery code. It remove a link in a directory with a given name, and
2632 * also drops the back refs in the inode to the directory
2633 */
Chris Masone02119d2008-09-05 16:13:11 -04002634int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2635 struct btrfs_root *root,
2636 struct inode *dir, struct inode *inode,
2637 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002638{
2639 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002640 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002641 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002642 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002643 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002644 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04002645
2646 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002647 if (!path) {
2648 ret = -ENOMEM;
2649 goto err;
2650 }
2651
Chris Masonb9473432009-03-13 11:00:37 -04002652 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002653 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2654 name, name_len, -1);
2655 if (IS_ERR(di)) {
2656 ret = PTR_ERR(di);
2657 goto err;
2658 }
2659 if (!di) {
2660 ret = -ENOENT;
2661 goto err;
2662 }
Chris Mason5f39d392007-10-15 16:14:19 -04002663 leaf = path->nodes[0];
2664 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002665 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002666 if (ret)
2667 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002668 btrfs_release_path(root, path);
2669
Josef Bacikaec74772008-07-24 12:12:38 -04002670 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04002671 inode->i_ino,
2672 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002673 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002674 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Josef Bacikaec74772008-07-24 12:12:38 -04002675 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04002676 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002677 goto err;
2678 }
2679
Chris Mason39279cc2007-06-12 06:35:45 -04002680 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002681 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002682 if (IS_ERR(di)) {
2683 ret = PTR_ERR(di);
2684 goto err;
2685 }
2686 if (!di) {
2687 ret = -ENOENT;
2688 goto err;
2689 }
2690 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002691 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002692
Chris Masone02119d2008-09-05 16:13:11 -04002693 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2694 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002695 BUG_ON(ret != 0 && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04002696
2697 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2698 dir, index);
Chris Mason6418c962010-10-30 07:34:24 -04002699 if (ret == -ENOENT)
2700 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002701err:
2702 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002703 if (ret)
2704 goto out;
2705
2706 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2707 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2708 btrfs_update_inode(trans, root, dir);
2709 btrfs_drop_nlink(inode);
2710 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002711out:
Chris Mason39279cc2007-06-12 06:35:45 -04002712 return ret;
2713}
2714
Yan, Zhenga22285a2010-05-16 10:48:46 -04002715/* helper to check if there is any shared block in the path */
2716static int check_path_shared(struct btrfs_root *root,
2717 struct btrfs_path *path)
2718{
2719 struct extent_buffer *eb;
2720 int level;
Dan Carpenter0e4dcbe2010-06-01 08:23:11 +00002721 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002722
2723 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
Josef Bacikdedefd72011-01-24 21:43:18 +00002724 int ret;
2725
Yan, Zhenga22285a2010-05-16 10:48:46 -04002726 if (!path->nodes[level])
2727 break;
2728 eb = path->nodes[level];
2729 if (!btrfs_block_can_be_shared(root, eb))
2730 continue;
2731 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2732 &refs, NULL);
2733 if (refs > 1)
2734 return 1;
2735 }
Josef Bacikdedefd72011-01-24 21:43:18 +00002736 return 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002737}
2738
2739/*
2740 * helper to start transaction for unlink and rmdir.
2741 *
2742 * unlink and rmdir are special in btrfs, they do not always free space.
2743 * so in enospc case, we should make sure they will free space before
2744 * allowing them to use the global metadata reservation.
2745 */
2746static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2747 struct dentry *dentry)
2748{
2749 struct btrfs_trans_handle *trans;
2750 struct btrfs_root *root = BTRFS_I(dir)->root;
2751 struct btrfs_path *path;
2752 struct btrfs_inode_ref *ref;
2753 struct btrfs_dir_item *di;
2754 struct inode *inode = dentry->d_inode;
2755 u64 index;
2756 int check_link = 1;
2757 int err = -ENOSPC;
2758 int ret;
2759
2760 trans = btrfs_start_transaction(root, 10);
2761 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2762 return trans;
2763
2764 if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
2765 return ERR_PTR(-ENOSPC);
2766
2767 /* check if there is someone else holds reference */
2768 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2769 return ERR_PTR(-ENOSPC);
2770
2771 if (atomic_read(&inode->i_count) > 2)
2772 return ERR_PTR(-ENOSPC);
2773
2774 if (xchg(&root->fs_info->enospc_unlink, 1))
2775 return ERR_PTR(-ENOSPC);
2776
2777 path = btrfs_alloc_path();
2778 if (!path) {
2779 root->fs_info->enospc_unlink = 0;
2780 return ERR_PTR(-ENOMEM);
2781 }
2782
2783 trans = btrfs_start_transaction(root, 0);
2784 if (IS_ERR(trans)) {
2785 btrfs_free_path(path);
2786 root->fs_info->enospc_unlink = 0;
2787 return trans;
2788 }
2789
2790 path->skip_locking = 1;
2791 path->search_commit_root = 1;
2792
2793 ret = btrfs_lookup_inode(trans, root, path,
2794 &BTRFS_I(dir)->location, 0);
2795 if (ret < 0) {
2796 err = ret;
2797 goto out;
2798 }
2799 if (ret == 0) {
2800 if (check_path_shared(root, path))
2801 goto out;
2802 } else {
2803 check_link = 0;
2804 }
2805 btrfs_release_path(root, path);
2806
2807 ret = btrfs_lookup_inode(trans, root, path,
2808 &BTRFS_I(inode)->location, 0);
2809 if (ret < 0) {
2810 err = ret;
2811 goto out;
2812 }
2813 if (ret == 0) {
2814 if (check_path_shared(root, path))
2815 goto out;
2816 } else {
2817 check_link = 0;
2818 }
2819 btrfs_release_path(root, path);
2820
2821 if (ret == 0 && S_ISREG(inode->i_mode)) {
2822 ret = btrfs_lookup_file_extent(trans, root, path,
2823 inode->i_ino, (u64)-1, 0);
2824 if (ret < 0) {
2825 err = ret;
2826 goto out;
2827 }
2828 BUG_ON(ret == 0);
2829 if (check_path_shared(root, path))
2830 goto out;
2831 btrfs_release_path(root, path);
2832 }
2833
2834 if (!check_link) {
2835 err = 0;
2836 goto out;
2837 }
2838
2839 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2840 dentry->d_name.name, dentry->d_name.len, 0);
2841 if (IS_ERR(di)) {
2842 err = PTR_ERR(di);
2843 goto out;
2844 }
2845 if (di) {
2846 if (check_path_shared(root, path))
2847 goto out;
2848 } else {
2849 err = 0;
2850 goto out;
2851 }
2852 btrfs_release_path(root, path);
2853
2854 ref = btrfs_lookup_inode_ref(trans, root, path,
2855 dentry->d_name.name, dentry->d_name.len,
2856 inode->i_ino, dir->i_ino, 0);
2857 if (IS_ERR(ref)) {
2858 err = PTR_ERR(ref);
2859 goto out;
2860 }
2861 BUG_ON(!ref);
2862 if (check_path_shared(root, path))
2863 goto out;
2864 index = btrfs_inode_ref_index(path->nodes[0], ref);
2865 btrfs_release_path(root, path);
2866
2867 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index,
2868 dentry->d_name.name, dentry->d_name.len, 0);
2869 if (IS_ERR(di)) {
2870 err = PTR_ERR(di);
2871 goto out;
2872 }
2873 BUG_ON(ret == -ENOENT);
2874 if (check_path_shared(root, path))
2875 goto out;
2876
2877 err = 0;
2878out:
2879 btrfs_free_path(path);
2880 if (err) {
2881 btrfs_end_transaction(trans, root);
2882 root->fs_info->enospc_unlink = 0;
2883 return ERR_PTR(err);
2884 }
2885
2886 trans->block_rsv = &root->fs_info->global_block_rsv;
2887 return trans;
2888}
2889
2890static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2891 struct btrfs_root *root)
2892{
2893 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
2894 BUG_ON(!root->fs_info->enospc_unlink);
2895 root->fs_info->enospc_unlink = 0;
2896 }
2897 btrfs_end_transaction_throttle(trans, root);
2898}
2899
Chris Mason39279cc2007-06-12 06:35:45 -04002900static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2901{
Yan, Zhenga22285a2010-05-16 10:48:46 -04002902 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002903 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002904 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002905 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002906 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002907
Yan, Zhenga22285a2010-05-16 10:48:46 -04002908 trans = __unlink_start_trans(dir, dentry);
2909 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002910 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04002911
Chris Mason39279cc2007-06-12 06:35:45 -04002912 btrfs_set_trans_block_group(trans, dir);
Chris Mason12fcfd22009-03-24 10:24:20 -04002913
2914 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2915
Chris Masone02119d2008-09-05 16:13:11 -04002916 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2917 dentry->d_name.name, dentry->d_name.len);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002918 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04002919
Yan, Zhenga22285a2010-05-16 10:48:46 -04002920 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04002921 ret = btrfs_orphan_add(trans, inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002922 BUG_ON(ret);
2923 }
Josef Bacik7b128762008-07-24 12:17:14 -04002924
Chris Masond3c2fdc2007-09-17 10:58:06 -04002925 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002926 __unlink_end_trans(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002927 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002928 return ret;
2929}
2930
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002931int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2932 struct btrfs_root *root,
2933 struct inode *dir, u64 objectid,
2934 const char *name, int name_len)
2935{
2936 struct btrfs_path *path;
2937 struct extent_buffer *leaf;
2938 struct btrfs_dir_item *di;
2939 struct btrfs_key key;
2940 u64 index;
2941 int ret;
2942
2943 path = btrfs_alloc_path();
2944 if (!path)
2945 return -ENOMEM;
2946
2947 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2948 name, name_len, -1);
2949 BUG_ON(!di || IS_ERR(di));
2950
2951 leaf = path->nodes[0];
2952 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2953 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2954 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2955 BUG_ON(ret);
2956 btrfs_release_path(root, path);
2957
2958 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2959 objectid, root->root_key.objectid,
2960 dir->i_ino, &index, name, name_len);
2961 if (ret < 0) {
2962 BUG_ON(ret != -ENOENT);
2963 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
2964 name, name_len);
2965 BUG_ON(!di || IS_ERR(di));
2966
2967 leaf = path->nodes[0];
2968 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2969 btrfs_release_path(root, path);
2970 index = key.offset;
2971 }
2972
2973 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2974 index, name, name_len, -1);
2975 BUG_ON(!di || IS_ERR(di));
2976
2977 leaf = path->nodes[0];
2978 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2979 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2980 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2981 BUG_ON(ret);
2982 btrfs_release_path(root, path);
2983
2984 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2985 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2986 ret = btrfs_update_inode(trans, root, dir);
2987 BUG_ON(ret);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002988
2989 btrfs_free_path(path);
2990 return 0;
2991}
2992
Chris Mason39279cc2007-06-12 06:35:45 -04002993static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2994{
2995 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002996 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002997 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002998 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05002999 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003000
Chris Mason3394e162008-11-17 20:42:26 -05003001 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003002 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04003003 return -ENOTEMPTY;
3004
Yan, Zhenga22285a2010-05-16 10:48:46 -04003005 trans = __unlink_start_trans(dir, dentry);
3006 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003007 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003008
Chris Mason39279cc2007-06-12 06:35:45 -04003009 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003010
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003011 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
3012 err = btrfs_unlink_subvol(trans, root, dir,
3013 BTRFS_I(inode)->location.objectid,
3014 dentry->d_name.name,
3015 dentry->d_name.len);
3016 goto out;
3017 }
3018
Josef Bacik7b128762008-07-24 12:17:14 -04003019 err = btrfs_orphan_add(trans, inode);
3020 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003021 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003022
Chris Mason39279cc2007-06-12 06:35:45 -04003023 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003024 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3025 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003026 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003027 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003028out:
Chris Masond3c2fdc2007-09-17 10:58:06 -04003029 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003030 __unlink_end_trans(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003031 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05003032
Chris Mason39279cc2007-06-12 06:35:45 -04003033 return err;
3034}
3035
Chris Masond20f7042008-12-08 16:58:54 -05003036#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04003037/*
Chris Mason323ac952008-10-01 19:05:46 -04003038 * when truncating bytes in a file, it is possible to avoid reading
3039 * the leaves that contain only checksum items. This can be the
3040 * majority of the IO required to delete a large file, but it must
3041 * be done carefully.
3042 *
3043 * The keys in the level just above the leaves are checked to make sure
3044 * the lowest key in a given leaf is a csum key, and starts at an offset
3045 * after the new size.
3046 *
3047 * Then the key for the next leaf is checked to make sure it also has
3048 * a checksum item for the same file. If it does, we know our target leaf
3049 * contains only checksum items, and it can be safely freed without reading
3050 * it.
3051 *
3052 * This is just an optimization targeted at large files. It may do
3053 * nothing. It will return 0 unless things went badly.
3054 */
3055static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
3056 struct btrfs_root *root,
3057 struct btrfs_path *path,
3058 struct inode *inode, u64 new_size)
3059{
3060 struct btrfs_key key;
3061 int ret;
3062 int nritems;
3063 struct btrfs_key found_key;
3064 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003065 struct btrfs_leaf_ref *ref;
3066 u64 leaf_gen;
3067 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04003068
3069 path->lowest_level = 1;
3070 key.objectid = inode->i_ino;
3071 key.type = BTRFS_CSUM_ITEM_KEY;
3072 key.offset = new_size;
3073again:
3074 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3075 if (ret < 0)
3076 goto out;
3077
3078 if (path->nodes[1] == NULL) {
3079 ret = 0;
3080 goto out;
3081 }
3082 ret = 0;
3083 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
3084 nritems = btrfs_header_nritems(path->nodes[1]);
3085
3086 if (!nritems)
3087 goto out;
3088
3089 if (path->slots[1] >= nritems)
3090 goto next_node;
3091
3092 /* did we find a key greater than anything we want to delete? */
3093 if (found_key.objectid > inode->i_ino ||
3094 (found_key.objectid == inode->i_ino && found_key.type > key.type))
3095 goto out;
3096
3097 /* we check the next key in the node to make sure the leave contains
3098 * only checksum items. This comparison doesn't work if our
3099 * leaf is the last one in the node
3100 */
3101 if (path->slots[1] + 1 >= nritems) {
3102next_node:
3103 /* search forward from the last key in the node, this
3104 * will bring us into the next node in the tree
3105 */
3106 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
3107
3108 /* unlikely, but we inc below, so check to be safe */
3109 if (found_key.offset == (u64)-1)
3110 goto out;
3111
3112 /* search_forward needs a path with locks held, do the
3113 * search again for the original key. It is possible
3114 * this will race with a balance and return a path that
3115 * we could modify, but this drop is just an optimization
3116 * and is allowed to miss some leaves.
3117 */
3118 btrfs_release_path(root, path);
3119 found_key.offset++;
3120
3121 /* setup a max key for search_forward */
3122 other_key.offset = (u64)-1;
3123 other_key.type = key.type;
3124 other_key.objectid = key.objectid;
3125
3126 path->keep_locks = 1;
3127 ret = btrfs_search_forward(root, &found_key, &other_key,
3128 path, 0, 0);
3129 path->keep_locks = 0;
3130 if (ret || found_key.objectid != key.objectid ||
3131 found_key.type != key.type) {
3132 ret = 0;
3133 goto out;
3134 }
3135
3136 key.offset = found_key.offset;
3137 btrfs_release_path(root, path);
3138 cond_resched();
3139 goto again;
3140 }
3141
3142 /* we know there's one more slot after us in the tree,
3143 * read that key so we can verify it is also a checksum item
3144 */
3145 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
3146
3147 if (found_key.objectid < inode->i_ino)
3148 goto next_key;
3149
3150 if (found_key.type != key.type || found_key.offset < new_size)
3151 goto next_key;
3152
3153 /*
3154 * if the key for the next leaf isn't a csum key from this objectid,
3155 * we can't be sure there aren't good items inside this leaf.
3156 * Bail out
3157 */
3158 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
3159 goto out;
3160
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003161 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
3162 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04003163 /*
3164 * it is safe to delete this leaf, it contains only
3165 * csum items from this inode at an offset >= new_size
3166 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003167 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04003168 BUG_ON(ret);
3169
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003170 if (root->ref_cows && leaf_gen < trans->transid) {
3171 ref = btrfs_alloc_leaf_ref(root, 0);
3172 if (ref) {
3173 ref->root_gen = root->root_key.offset;
3174 ref->bytenr = leaf_start;
3175 ref->owner = 0;
3176 ref->generation = leaf_gen;
3177 ref->nritems = 0;
3178
Chris Masonbd56b302009-02-04 09:27:02 -05003179 btrfs_sort_leaf_ref(ref);
3180
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003181 ret = btrfs_add_leaf_ref(root, ref, 0);
3182 WARN_ON(ret);
3183 btrfs_free_leaf_ref(root, ref);
3184 } else {
3185 WARN_ON(1);
3186 }
3187 }
Chris Mason323ac952008-10-01 19:05:46 -04003188next_key:
3189 btrfs_release_path(root, path);
3190
3191 if (other_key.objectid == inode->i_ino &&
3192 other_key.type == key.type && other_key.offset > key.offset) {
3193 key.offset = other_key.offset;
3194 cond_resched();
3195 goto again;
3196 }
3197 ret = 0;
3198out:
3199 /* fixup any changes we've made to the path */
3200 path->lowest_level = 0;
3201 path->keep_locks = 0;
3202 btrfs_release_path(root, path);
3203 return ret;
3204}
3205
Chris Masond20f7042008-12-08 16:58:54 -05003206#endif
3207
Chris Mason323ac952008-10-01 19:05:46 -04003208/*
Chris Mason39279cc2007-06-12 06:35:45 -04003209 * this can truncate away extent items, csum items and directory items.
3210 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003211 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003212 *
3213 * csum items that cross the new i_size are truncated to the new size
3214 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003215 *
3216 * min_type is the minimum key type to truncate down to. If set to 0, this
3217 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003218 */
Yan, Zheng80825102009-11-12 09:35:36 +00003219int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3220 struct btrfs_root *root,
3221 struct inode *inode,
3222 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003223{
Chris Mason39279cc2007-06-12 06:35:45 -04003224 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003225 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003226 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003227 struct btrfs_key key;
3228 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003229 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003230 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003231 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003232 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003233 u64 mask = root->sectorsize - 1;
3234 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003235 int found_extent;
3236 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003237 int pending_del_nr = 0;
3238 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003239 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05003240 int encoding;
Yan, Zheng80825102009-11-12 09:35:36 +00003241 int ret;
3242 int err = 0;
3243
3244 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003245
Josef Bacik0af3d002010-06-21 14:48:16 -04003246 if (root->ref_cows || root == root->fs_info->tree_root)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003247 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003248
Chris Mason39279cc2007-06-12 06:35:45 -04003249 path = btrfs_alloc_path();
3250 BUG_ON(!path);
Julia Lawall33c17ad2009-07-22 16:49:01 -04003251 path->reada = -1;
Chris Mason5f39d392007-10-15 16:14:19 -04003252
Chris Mason39279cc2007-06-12 06:35:45 -04003253 key.objectid = inode->i_ino;
3254 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003255 key.type = (u8)-1;
3256
Chris Mason85e21ba2008-01-29 15:11:36 -05003257search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003258 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003259 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003260 if (ret < 0) {
3261 err = ret;
3262 goto out;
3263 }
Chris Masond3977122009-01-05 21:25:51 -05003264
Chris Mason85e21ba2008-01-29 15:11:36 -05003265 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003266 /* there are no items in the tree for us to truncate, we're
3267 * done
3268 */
Yan, Zheng80825102009-11-12 09:35:36 +00003269 if (path->slots[0] == 0)
3270 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003271 path->slots[0]--;
3272 }
3273
Chris Masond3977122009-01-05 21:25:51 -05003274 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003275 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003276 leaf = path->nodes[0];
3277 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3278 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05003279 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003280
Chris Mason5f39d392007-10-15 16:14:19 -04003281 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003282 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003283
Chris Mason85e21ba2008-01-29 15:11:36 -05003284 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003285 break;
3286
Chris Mason5f39d392007-10-15 16:14:19 -04003287 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003288 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003289 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003290 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003291 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003292 encoding = btrfs_file_extent_compression(leaf, fi);
3293 encoding |= btrfs_file_extent_encryption(leaf, fi);
3294 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
3295
Chris Mason179e29e2007-11-01 11:28:41 -04003296 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003297 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003298 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003299 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003300 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003301 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003302 }
Yan008630c2007-11-07 13:31:09 -05003303 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003304 }
Yan, Zheng80825102009-11-12 09:35:36 +00003305 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003306 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003307 } else {
3308 if (item_end < new_size)
3309 break;
3310 if (found_key.offset >= new_size)
3311 del_item = 1;
3312 else
3313 del_item = 0;
3314 }
Chris Mason39279cc2007-06-12 06:35:45 -04003315 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003316 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003317 if (found_type != BTRFS_EXTENT_DATA_KEY)
3318 goto delete;
3319
3320 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003321 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003322 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003323 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04003324 u64 orig_num_bytes =
3325 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003326 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003327 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003328 extent_num_bytes = extent_num_bytes &
3329 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003330 btrfs_set_file_extent_num_bytes(leaf, fi,
3331 extent_num_bytes);
3332 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003333 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003334 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003335 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003336 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003337 } else {
Chris Masondb945352007-10-15 16:15:53 -04003338 extent_num_bytes =
3339 btrfs_file_extent_disk_num_bytes(leaf,
3340 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003341 extent_offset = found_key.offset -
3342 btrfs_file_extent_offset(leaf, fi);
3343
Chris Mason39279cc2007-06-12 06:35:45 -04003344 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003345 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003346 if (extent_start != 0) {
3347 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003348 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003349 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003350 }
3351 }
Chris Mason90692182008-02-08 13:49:28 -05003352 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003353 /*
3354 * we can't truncate inline items that have had
3355 * special encodings
3356 */
3357 if (!del_item &&
3358 btrfs_file_extent_compression(leaf, fi) == 0 &&
3359 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3360 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003361 u32 size = new_size - found_key.offset;
3362
3363 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003364 inode_sub_bytes(inode, item_end + 1 -
3365 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003366 }
3367 size =
3368 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05003369 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04003370 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05003371 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04003372 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003373 inode_sub_bytes(inode, item_end + 1 -
3374 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003375 }
Chris Mason39279cc2007-06-12 06:35:45 -04003376 }
Chris Mason179e29e2007-11-01 11:28:41 -04003377delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003378 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003379 if (!pending_del_nr) {
3380 /* no pending yet, add ourselves */
3381 pending_del_slot = path->slots[0];
3382 pending_del_nr = 1;
3383 } else if (pending_del_nr &&
3384 path->slots[0] + 1 == pending_del_slot) {
3385 /* hop on the pending chunk */
3386 pending_del_nr++;
3387 pending_del_slot = path->slots[0];
3388 } else {
Chris Masond3977122009-01-05 21:25:51 -05003389 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003390 }
Chris Mason39279cc2007-06-12 06:35:45 -04003391 } else {
3392 break;
3393 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003394 if (found_extent && (root->ref_cows ||
3395 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003396 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003397 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003398 extent_num_bytes, 0,
3399 btrfs_header_owner(leaf),
3400 inode->i_ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04003401 BUG_ON(ret);
3402 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003403
Yan, Zheng80825102009-11-12 09:35:36 +00003404 if (found_type == BTRFS_INODE_ITEM_KEY)
3405 break;
3406
3407 if (path->slots[0] == 0 ||
3408 path->slots[0] != pending_del_slot) {
3409 if (root->ref_cows) {
3410 err = -EAGAIN;
3411 goto out;
3412 }
3413 if (pending_del_nr) {
3414 ret = btrfs_del_items(trans, root, path,
3415 pending_del_slot,
3416 pending_del_nr);
3417 BUG_ON(ret);
3418 pending_del_nr = 0;
3419 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003420 btrfs_release_path(root, path);
3421 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003422 } else {
3423 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003424 }
Chris Mason39279cc2007-06-12 06:35:45 -04003425 }
Yan, Zheng80825102009-11-12 09:35:36 +00003426out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003427 if (pending_del_nr) {
3428 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3429 pending_del_nr);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003430 BUG_ON(ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003431 }
Chris Mason39279cc2007-06-12 06:35:45 -04003432 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003433 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003434}
3435
Chris Masona52d9a82007-08-27 16:49:44 -04003436/*
3437 * taken from block_truncate_page, but does cow as it zeros out
3438 * any bytes left in the last page in the file.
3439 */
3440static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3441{
3442 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003443 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003444 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3445 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003446 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003447 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003448 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003449 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3450 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3451 struct page *page;
3452 int ret = 0;
3453 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003454 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003455
3456 if ((offset & (blocksize - 1)) == 0)
3457 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003458 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003459 if (ret)
3460 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003461
3462 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003463again:
Chris Masona52d9a82007-08-27 16:49:44 -04003464 page = grab_cache_page(mapping, index);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003465 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003466 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04003467 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003468 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003469
3470 page_start = page_offset(page);
3471 page_end = page_start + PAGE_CACHE_SIZE - 1;
3472
Chris Masona52d9a82007-08-27 16:49:44 -04003473 if (!PageUptodate(page)) {
3474 ret = btrfs_readpage(NULL, page);
3475 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003476 if (page->mapping != mapping) {
3477 unlock_page(page);
3478 page_cache_release(page);
3479 goto again;
3480 }
Chris Masona52d9a82007-08-27 16:49:44 -04003481 if (!PageUptodate(page)) {
3482 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003483 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003484 }
3485 }
Chris Mason211c17f2008-05-15 09:13:45 -04003486 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003487
Josef Bacik2ac55d42010-02-03 19:33:23 +00003488 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
3489 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003490 set_page_extent_mapped(page);
3491
3492 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3493 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003494 unlock_extent_cached(io_tree, page_start, page_end,
3495 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003496 unlock_page(page);
3497 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003498 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003499 btrfs_put_ordered_extent(ordered);
3500 goto again;
3501 }
3502
Josef Bacik2ac55d42010-02-03 19:33:23 +00003503 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik5d5e1032009-10-13 16:46:49 -04003504 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003505 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003506
Josef Bacik2ac55d42010-02-03 19:33:23 +00003507 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3508 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003509 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003510 unlock_extent_cached(io_tree, page_start, page_end,
3511 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003512 goto out_unlock;
3513 }
3514
Chris Masone6dcd2d2008-07-17 12:53:50 -04003515 ret = 0;
3516 if (offset != PAGE_CACHE_SIZE) {
3517 kaddr = kmap(page);
3518 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3519 flush_dcache_page(page);
3520 kunmap(page);
3521 }
Chris Mason247e7432008-07-17 12:53:51 -04003522 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003523 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003524 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3525 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003526
Chris Mason89642222008-07-24 09:41:53 -04003527out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003528 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003529 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003530 unlock_page(page);
3531 page_cache_release(page);
3532out:
3533 return ret;
3534}
3535
Yan Zheng9036c102008-10-30 14:19:41 -04003536int btrfs_cont_expand(struct inode *inode, loff_t size)
3537{
3538 struct btrfs_trans_handle *trans;
3539 struct btrfs_root *root = BTRFS_I(inode)->root;
3540 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003541 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003542 struct extent_state *cached_state = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003543 u64 mask = root->sectorsize - 1;
3544 u64 hole_start = (inode->i_size + mask) & ~mask;
3545 u64 block_end = (size + mask) & ~mask;
3546 u64 last_byte;
3547 u64 cur_offset;
3548 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003549 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003550
3551 if (size <= hole_start)
3552 return 0;
3553
Yan Zheng9036c102008-10-30 14:19:41 -04003554 while (1) {
3555 struct btrfs_ordered_extent *ordered;
3556 btrfs_wait_ordered_range(inode, hole_start,
3557 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003558 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
3559 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003560 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3561 if (!ordered)
3562 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003563 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3564 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003565 btrfs_put_ordered_extent(ordered);
3566 }
3567
Yan Zheng9036c102008-10-30 14:19:41 -04003568 cur_offset = hole_start;
3569 while (1) {
3570 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3571 block_end - cur_offset, 0);
3572 BUG_ON(IS_ERR(em) || !em);
3573 last_byte = min(extent_map_end(em), block_end);
3574 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003575 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003576 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003577 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003578
Yan, Zhenga22285a2010-05-16 10:48:46 -04003579 trans = btrfs_start_transaction(root, 2);
3580 if (IS_ERR(trans)) {
3581 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003582 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003583 }
Yan, Zheng80825102009-11-12 09:35:36 +00003584 btrfs_set_trans_block_group(trans, inode);
3585
3586 err = btrfs_drop_extents(trans, inode, cur_offset,
3587 cur_offset + hole_size,
3588 &hint_byte, 1);
3589 BUG_ON(err);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003590
Yan Zheng9036c102008-10-30 14:19:41 -04003591 err = btrfs_insert_file_extent(trans, root,
3592 inode->i_ino, cur_offset, 0,
3593 0, hole_size, 0, hole_size,
3594 0, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003595 BUG_ON(err);
3596
Yan Zheng9036c102008-10-30 14:19:41 -04003597 btrfs_drop_extent_cache(inode, hole_start,
3598 last_byte - 1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003599
3600 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003601 }
3602 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003603 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003604 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003605 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003606 break;
3607 }
3608
Yan, Zhenga22285a2010-05-16 10:48:46 -04003609 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003610 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3611 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003612 return err;
3613}
3614
Yan, Zheng80825102009-11-12 09:35:36 +00003615static int btrfs_setattr_size(struct inode *inode, struct iattr *attr)
3616{
3617 struct btrfs_root *root = BTRFS_I(inode)->root;
3618 struct btrfs_trans_handle *trans;
3619 unsigned long nr;
3620 int ret;
3621
3622 if (attr->ia_size == inode->i_size)
3623 return 0;
3624
3625 if (attr->ia_size > inode->i_size) {
3626 unsigned long limit;
3627 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
3628 if (attr->ia_size > inode->i_sb->s_maxbytes)
3629 return -EFBIG;
3630 if (limit != RLIM_INFINITY && attr->ia_size > limit) {
3631 send_sig(SIGXFSZ, current, 0);
3632 return -EFBIG;
3633 }
3634 }
3635
Yan, Zhengd68fc572010-05-16 10:49:58 -04003636 trans = btrfs_start_transaction(root, 5);
3637 if (IS_ERR(trans))
3638 return PTR_ERR(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00003639
Yan, Zheng80825102009-11-12 09:35:36 +00003640 btrfs_set_trans_block_group(trans, inode);
3641
3642 ret = btrfs_orphan_add(trans, inode);
3643 BUG_ON(ret);
3644
3645 nr = trans->blocks_used;
3646 btrfs_end_transaction(trans, root);
Yan, Zheng80825102009-11-12 09:35:36 +00003647 btrfs_btree_balance_dirty(root, nr);
3648
3649 if (attr->ia_size > inode->i_size) {
3650 ret = btrfs_cont_expand(inode, attr->ia_size);
3651 if (ret) {
3652 btrfs_truncate(inode);
3653 return ret;
3654 }
3655
3656 i_size_write(inode, attr->ia_size);
3657 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
3658
Yan, Zhengd68fc572010-05-16 10:49:58 -04003659 trans = btrfs_start_transaction(root, 0);
3660 BUG_ON(IS_ERR(trans));
Yan, Zheng80825102009-11-12 09:35:36 +00003661 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003662 trans->block_rsv = root->orphan_block_rsv;
3663 BUG_ON(!trans->block_rsv);
Yan, Zheng80825102009-11-12 09:35:36 +00003664
3665 ret = btrfs_update_inode(trans, root, inode);
3666 BUG_ON(ret);
3667 if (inode->i_nlink > 0) {
3668 ret = btrfs_orphan_del(trans, inode);
3669 BUG_ON(ret);
3670 }
3671 nr = trans->blocks_used;
3672 btrfs_end_transaction(trans, root);
3673 btrfs_btree_balance_dirty(root, nr);
3674 return 0;
3675 }
3676
3677 /*
3678 * We're truncating a file that used to have good data down to
3679 * zero. Make sure it gets into the ordered flush list so that
3680 * any new writes get down to disk quickly.
3681 */
3682 if (attr->ia_size == 0)
3683 BTRFS_I(inode)->ordered_data_close = 1;
3684
3685 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3686 ret = vmtruncate(inode, attr->ia_size);
3687 BUG_ON(ret);
3688
3689 return 0;
3690}
3691
Chris Mason39279cc2007-06-12 06:35:45 -04003692static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3693{
3694 struct inode *inode = dentry->d_inode;
Li Zefanb83cc962010-12-20 16:04:08 +08003695 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003696 int err;
3697
Li Zefanb83cc962010-12-20 16:04:08 +08003698 if (btrfs_root_readonly(root))
3699 return -EROFS;
3700
Chris Mason39279cc2007-06-12 06:35:45 -04003701 err = inode_change_ok(inode, attr);
3702 if (err)
3703 return err;
3704
Chris Mason5a3f23d2009-03-31 13:27:11 -04003705 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Yan, Zheng80825102009-11-12 09:35:36 +00003706 err = btrfs_setattr_size(inode, attr);
3707 if (err)
3708 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003709 }
Yan Zheng9036c102008-10-30 14:19:41 -04003710
Christoph Hellwig10257742010-06-04 11:30:02 +02003711 if (attr->ia_valid) {
3712 setattr_copy(inode, attr);
3713 mark_inode_dirty(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003714
Christoph Hellwig10257742010-06-04 11:30:02 +02003715 if (attr->ia_valid & ATTR_MODE)
3716 err = btrfs_acl_chmod(inode);
3717 }
3718
Chris Mason39279cc2007-06-12 06:35:45 -04003719 return err;
3720}
Chris Mason61295eb2008-01-14 16:24:38 -05003721
Al Virobd555972010-06-07 11:35:40 -04003722void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003723{
3724 struct btrfs_trans_handle *trans;
3725 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdc2007-09-17 10:58:06 -04003726 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003727 int ret;
3728
3729 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003730 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
3731 root == root->fs_info->tree_root))
Al Virobd555972010-06-07 11:35:40 -04003732 goto no_delete;
3733
Chris Mason39279cc2007-06-12 06:35:45 -04003734 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003735 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003736 goto no_delete;
3737 }
Al Virobd555972010-06-07 11:35:40 -04003738 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003739 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003740
Yan, Zhengc71bf092009-11-12 09:34:40 +00003741 if (root->fs_info->log_root_recovering) {
3742 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3743 goto no_delete;
3744 }
3745
Yan, Zheng76dda932009-09-21 16:00:26 -04003746 if (inode->i_nlink > 0) {
3747 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3748 goto no_delete;
3749 }
3750
Chris Masondbe674a2008-07-17 12:54:05 -04003751 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003752
Yan, Zheng80825102009-11-12 09:35:36 +00003753 while (1) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003754 trans = btrfs_start_transaction(root, 0);
3755 BUG_ON(IS_ERR(trans));
Yan, Zheng80825102009-11-12 09:35:36 +00003756 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003757 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003758
Yan, Zhengd68fc572010-05-16 10:49:58 -04003759 ret = btrfs_block_rsv_check(trans, root,
3760 root->orphan_block_rsv, 0, 5);
3761 if (ret) {
3762 BUG_ON(ret != -EAGAIN);
3763 ret = btrfs_commit_transaction(trans, root);
3764 BUG_ON(ret);
3765 continue;
3766 }
3767
3768 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003769 if (ret != -EAGAIN)
3770 break;
3771
3772 nr = trans->blocks_used;
3773 btrfs_end_transaction(trans, root);
3774 trans = NULL;
3775 btrfs_btree_balance_dirty(root, nr);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003776
Josef Bacik7b128762008-07-24 12:17:14 -04003777 }
3778
Yan, Zheng80825102009-11-12 09:35:36 +00003779 if (ret == 0) {
3780 ret = btrfs_orphan_del(trans, inode);
3781 BUG_ON(ret);
3782 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003783
Chris Masond3c2fdc2007-09-17 10:58:06 -04003784 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003785 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003786 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003787no_delete:
Al Virobd555972010-06-07 11:35:40 -04003788 end_writeback(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003789 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003790}
3791
3792/*
3793 * this returns the key found in the dir entry in the location pointer.
3794 * If no dir entries were found, location->objectid is 0.
3795 */
3796static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3797 struct btrfs_key *location)
3798{
3799 const char *name = dentry->d_name.name;
3800 int namelen = dentry->d_name.len;
3801 struct btrfs_dir_item *di;
3802 struct btrfs_path *path;
3803 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003804 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003805
3806 path = btrfs_alloc_path();
3807 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05003808
Chris Mason39279cc2007-06-12 06:35:45 -04003809 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
3810 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003811 if (IS_ERR(di))
3812 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003813
3814 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05003815 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003816
Chris Mason5f39d392007-10-15 16:14:19 -04003817 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003818out:
Chris Mason39279cc2007-06-12 06:35:45 -04003819 btrfs_free_path(path);
3820 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003821out_err:
3822 location->objectid = 0;
3823 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003824}
3825
3826/*
3827 * when we hit a tree root in a directory, the btrfs part of the inode
3828 * needs to be changed to reflect the root directory of the tree root. This
3829 * is kind of like crossing a mount point.
3830 */
3831static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003832 struct inode *dir,
3833 struct dentry *dentry,
3834 struct btrfs_key *location,
3835 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003836{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003837 struct btrfs_path *path;
3838 struct btrfs_root *new_root;
3839 struct btrfs_root_ref *ref;
3840 struct extent_buffer *leaf;
3841 int ret;
3842 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003843
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003844 path = btrfs_alloc_path();
3845 if (!path) {
3846 err = -ENOMEM;
3847 goto out;
3848 }
Chris Mason39279cc2007-06-12 06:35:45 -04003849
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003850 err = -ENOENT;
3851 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3852 BTRFS_I(dir)->root->root_key.objectid,
3853 location->objectid);
3854 if (ret) {
3855 if (ret < 0)
3856 err = ret;
3857 goto out;
3858 }
Chris Mason39279cc2007-06-12 06:35:45 -04003859
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003860 leaf = path->nodes[0];
3861 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3862 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
3863 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3864 goto out;
3865
3866 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3867 (unsigned long)(ref + 1),
3868 dentry->d_name.len);
3869 if (ret)
3870 goto out;
3871
3872 btrfs_release_path(root->fs_info->tree_root, path);
3873
3874 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3875 if (IS_ERR(new_root)) {
3876 err = PTR_ERR(new_root);
3877 goto out;
3878 }
3879
3880 if (btrfs_root_refs(&new_root->root_item) == 0) {
3881 err = -ENOENT;
3882 goto out;
3883 }
3884
3885 *sub_root = new_root;
3886 location->objectid = btrfs_root_dirid(&new_root->root_item);
3887 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003888 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003889 err = 0;
3890out:
3891 btrfs_free_path(path);
3892 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003893}
3894
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003895static void inode_tree_add(struct inode *inode)
3896{
3897 struct btrfs_root *root = BTRFS_I(inode)->root;
3898 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003899 struct rb_node **p;
3900 struct rb_node *parent;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003901again:
3902 p = &root->inode_tree.rb_node;
3903 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003904
Yan, Zheng76dda932009-09-21 16:00:26 -04003905 if (hlist_unhashed(&inode->i_hash))
3906 return;
3907
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003908 spin_lock(&root->inode_lock);
3909 while (*p) {
3910 parent = *p;
3911 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3912
3913 if (inode->i_ino < entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003914 p = &parent->rb_left;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003915 else if (inode->i_ino > entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003916 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003917 else {
3918 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04003919 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003920 rb_erase(parent, &root->inode_tree);
3921 RB_CLEAR_NODE(parent);
3922 spin_unlock(&root->inode_lock);
3923 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003924 }
3925 }
3926 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3927 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3928 spin_unlock(&root->inode_lock);
3929}
3930
3931static void inode_tree_del(struct inode *inode)
3932{
3933 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003934 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003935
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003936 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003937 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003938 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003939 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003940 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003941 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003942 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003943
Josef Bacik0af3d002010-06-21 14:48:16 -04003944 /*
3945 * Free space cache has inodes in the tree root, but the tree root has a
3946 * root_refs of 0, so this could end up dropping the tree root as a
3947 * snapshot, so we need the extra !root->fs_info->tree_root check to
3948 * make sure we don't drop it.
3949 */
3950 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
3951 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04003952 synchronize_srcu(&root->fs_info->subvol_srcu);
3953 spin_lock(&root->inode_lock);
3954 empty = RB_EMPTY_ROOT(&root->inode_tree);
3955 spin_unlock(&root->inode_lock);
3956 if (empty)
3957 btrfs_add_dead_root(root);
3958 }
3959}
3960
3961int btrfs_invalidate_inodes(struct btrfs_root *root)
3962{
3963 struct rb_node *node;
3964 struct rb_node *prev;
3965 struct btrfs_inode *entry;
3966 struct inode *inode;
3967 u64 objectid = 0;
3968
3969 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3970
3971 spin_lock(&root->inode_lock);
3972again:
3973 node = root->inode_tree.rb_node;
3974 prev = NULL;
3975 while (node) {
3976 prev = node;
3977 entry = rb_entry(node, struct btrfs_inode, rb_node);
3978
3979 if (objectid < entry->vfs_inode.i_ino)
3980 node = node->rb_left;
3981 else if (objectid > entry->vfs_inode.i_ino)
3982 node = node->rb_right;
3983 else
3984 break;
3985 }
3986 if (!node) {
3987 while (prev) {
3988 entry = rb_entry(prev, struct btrfs_inode, rb_node);
3989 if (objectid <= entry->vfs_inode.i_ino) {
3990 node = prev;
3991 break;
3992 }
3993 prev = rb_next(prev);
3994 }
3995 }
3996 while (node) {
3997 entry = rb_entry(node, struct btrfs_inode, rb_node);
3998 objectid = entry->vfs_inode.i_ino + 1;
3999 inode = igrab(&entry->vfs_inode);
4000 if (inode) {
4001 spin_unlock(&root->inode_lock);
4002 if (atomic_read(&inode->i_count) > 1)
4003 d_prune_aliases(inode);
4004 /*
Al Viro45321ac2010-06-07 13:43:19 -04004005 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04004006 * the inode cache when its usage count
4007 * hits zero.
4008 */
4009 iput(inode);
4010 cond_resched();
4011 spin_lock(&root->inode_lock);
4012 goto again;
4013 }
4014
4015 if (cond_resched_lock(&root->inode_lock))
4016 goto again;
4017
4018 node = rb_next(node);
4019 }
4020 spin_unlock(&root->inode_lock);
4021 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004022}
4023
Chris Masone02119d2008-09-05 16:13:11 -04004024static int btrfs_init_locked_inode(struct inode *inode, void *p)
4025{
4026 struct btrfs_iget_args *args = p;
4027 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004028 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05004029 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004030 return 0;
4031}
4032
4033static int btrfs_find_actor(struct inode *inode, void *opaque)
4034{
4035 struct btrfs_iget_args *args = opaque;
Chris Masond3977122009-01-05 21:25:51 -05004036 return args->ino == inode->i_ino &&
4037 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004038}
4039
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004040static struct inode *btrfs_iget_locked(struct super_block *s,
4041 u64 objectid,
4042 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004043{
4044 struct inode *inode;
4045 struct btrfs_iget_args args;
4046 args.ino = objectid;
4047 args.root = root;
4048
4049 inode = iget5_locked(s, objectid, btrfs_find_actor,
4050 btrfs_init_locked_inode,
4051 (void *)&args);
4052 return inode;
4053}
4054
Balaji Rao1a54ef82008-07-21 02:01:04 +05304055/* Get an inode object given its location and corresponding root.
4056 * Returns in *is_new if the inode was read from disk
4057 */
4058struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004059 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304060{
4061 struct inode *inode;
4062
4063 inode = btrfs_iget_locked(s, location->objectid, root);
4064 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004065 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304066
4067 if (inode->i_state & I_NEW) {
4068 BTRFS_I(inode)->root = root;
4069 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4070 btrfs_read_locked_inode(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004071
4072 inode_tree_add(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304073 unlock_new_inode(inode);
Josef Bacik73f73412009-12-04 17:38:27 +00004074 if (new)
4075 *new = 1;
Balaji Rao1a54ef82008-07-21 02:01:04 +05304076 }
4077
4078 return inode;
4079}
4080
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004081static struct inode *new_simple_dir(struct super_block *s,
4082 struct btrfs_key *key,
4083 struct btrfs_root *root)
4084{
4085 struct inode *inode = new_inode(s);
4086
4087 if (!inode)
4088 return ERR_PTR(-ENOMEM);
4089
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004090 BTRFS_I(inode)->root = root;
4091 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
4092 BTRFS_I(inode)->dummy_inode = 1;
4093
4094 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
4095 inode->i_op = &simple_dir_inode_operations;
4096 inode->i_fop = &simple_dir_operations;
4097 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4098 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4099
4100 return inode;
4101}
4102
Chris Mason3de45862008-11-17 21:02:50 -05004103struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004104{
Chris Masond3977122009-01-05 21:25:51 -05004105 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004106 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004107 struct btrfs_root *sub_root = root;
4108 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004109 int index;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004110 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004111
Yan, Zheng76dda932009-09-21 16:00:26 -04004112 dentry->d_op = &btrfs_dentry_operations;
4113
Chris Mason39279cc2007-06-12 06:35:45 -04004114 if (dentry->d_name.len > BTRFS_NAME_LEN)
4115 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004116
Chris Mason39279cc2007-06-12 06:35:45 -04004117 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04004118
Chris Mason39279cc2007-06-12 06:35:45 -04004119 if (ret < 0)
4120 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004121
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004122 if (location.objectid == 0)
4123 return NULL;
4124
4125 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004126 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004127 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004128 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004129
4130 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4131
Yan, Zheng76dda932009-09-21 16:00:26 -04004132 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004133 ret = fixup_tree_root_location(root, dir, dentry,
4134 &location, &sub_root);
4135 if (ret < 0) {
4136 if (ret != -ENOENT)
4137 inode = ERR_PTR(ret);
4138 else
4139 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4140 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004141 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004142 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004143 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4144
Julia Lawall34d19ba2011-01-24 19:55:19 +00004145 if (!IS_ERR(inode) && root != sub_root) {
Yan, Zhengc71bf092009-11-12 09:34:40 +00004146 down_read(&root->fs_info->cleanup_work_sem);
4147 if (!(inode->i_sb->s_flags & MS_RDONLY))
4148 btrfs_orphan_cleanup(sub_root);
4149 up_read(&root->fs_info->cleanup_work_sem);
4150 }
4151
Chris Mason3de45862008-11-17 21:02:50 -05004152 return inode;
4153}
4154
Yan, Zheng76dda932009-09-21 16:00:26 -04004155static int btrfs_dentry_delete(struct dentry *dentry)
4156{
4157 struct btrfs_root *root;
4158
Yan, Zhengefefb142009-10-09 09:25:16 -04004159 if (!dentry->d_inode && !IS_ROOT(dentry))
4160 dentry = dentry->d_parent;
Yan, Zheng76dda932009-09-21 16:00:26 -04004161
Yan, Zhengefefb142009-10-09 09:25:16 -04004162 if (dentry->d_inode) {
4163 root = BTRFS_I(dentry->d_inode)->root;
4164 if (btrfs_root_refs(&root->root_item) == 0)
4165 return 1;
4166 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004167 return 0;
4168}
4169
Chris Mason3de45862008-11-17 21:02:50 -05004170static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4171 struct nameidata *nd)
4172{
4173 struct inode *inode;
4174
Chris Mason3de45862008-11-17 21:02:50 -05004175 inode = btrfs_lookup_dentry(dir, dentry);
4176 if (IS_ERR(inode))
4177 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04004178
Chris Mason39279cc2007-06-12 06:35:45 -04004179 return d_splice_alias(inode, dentry);
4180}
4181
Chris Mason39279cc2007-06-12 06:35:45 -04004182static unsigned char btrfs_filetype_table[] = {
4183 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4184};
4185
David Woodhousecbdf5a22008-08-06 19:42:33 +01004186static int btrfs_real_readdir(struct file *filp, void *dirent,
4187 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004188{
Chris Mason6da6aba2007-12-18 16:15:09 -05004189 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004190 struct btrfs_root *root = BTRFS_I(inode)->root;
4191 struct btrfs_item *item;
4192 struct btrfs_dir_item *di;
4193 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004194 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004195 struct btrfs_path *path;
4196 int ret;
4197 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04004198 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004199 int slot;
4200 int advance;
4201 unsigned char d_type;
4202 int over = 0;
4203 u32 di_cur;
4204 u32 di_total;
4205 u32 di_len;
4206 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004207 char tmp_name[32];
4208 char *name_ptr;
4209 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04004210
4211 /* FIXME, use a real flag for deciding about the key type */
4212 if (root->fs_info->tree_root == root)
4213 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004214
Chris Mason39544012007-12-12 14:38:19 -05004215 /* special case for "." */
4216 if (filp->f_pos == 0) {
4217 over = filldir(dirent, ".", 1,
4218 1, inode->i_ino,
4219 DT_DIR);
4220 if (over)
4221 return 0;
4222 filp->f_pos = 1;
4223 }
Chris Mason39544012007-12-12 14:38:19 -05004224 /* special case for .., just use the back ref */
4225 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004226 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004227 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004228 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004229 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004230 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004231 filp->f_pos = 2;
4232 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004233 path = btrfs_alloc_path();
4234 path->reada = 2;
4235
Chris Mason39279cc2007-06-12 06:35:45 -04004236 btrfs_set_key_type(&key, key_type);
4237 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01004238 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04004239
Chris Mason39279cc2007-06-12 06:35:45 -04004240 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4241 if (ret < 0)
4242 goto err;
4243 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01004244
4245 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004246 leaf = path->nodes[0];
4247 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004248 slot = path->slots[0];
4249 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01004250 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004251 ret = btrfs_next_leaf(root, path);
4252 if (ret)
4253 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004254 leaf = path->nodes[0];
4255 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004256 slot = path->slots[0];
4257 } else {
4258 slot++;
4259 path->slots[0]++;
4260 }
4261 }
Chris Mason3de45862008-11-17 21:02:50 -05004262
Chris Mason39279cc2007-06-12 06:35:45 -04004263 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04004264 item = btrfs_item_nr(leaf, slot);
4265 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4266
4267 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004268 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004269 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004270 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004271 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04004272 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04004273
4274 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01004275
Chris Mason39279cc2007-06-12 06:35:45 -04004276 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4277 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004278 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004279
4280 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004281 struct btrfs_key location;
4282
4283 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004284 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004285 name_ptr = tmp_name;
4286 } else {
4287 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004288 if (!name_ptr) {
4289 ret = -ENOMEM;
4290 goto err;
4291 }
Chris Mason5f39d392007-10-15 16:14:19 -04004292 }
4293 read_extent_buffer(leaf, name_ptr,
4294 (unsigned long)(di + 1), name_len);
4295
4296 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4297 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004298
4299 /* is this a reference to our own snapshot? If so
4300 * skip it
4301 */
4302 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4303 location.objectid == root->root_key.objectid) {
4304 over = 0;
4305 goto skip;
4306 }
Chris Mason5f39d392007-10-15 16:14:19 -04004307 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004308 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004309 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004310
Chris Mason3de45862008-11-17 21:02:50 -05004311skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004312 if (name_ptr != tmp_name)
4313 kfree(name_ptr);
4314
Chris Mason39279cc2007-06-12 06:35:45 -04004315 if (over)
4316 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004317 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004318 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004319 di_cur += di_len;
4320 di = (struct btrfs_dir_item *)((char *)di + di_len);
4321 }
4322 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004323
4324 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004325 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004326 /*
4327 * 32-bit glibc will use getdents64, but then strtol -
4328 * so the last number we can serve is this.
4329 */
4330 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004331 else
4332 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004333nopos:
4334 ret = 0;
4335err:
Chris Mason39279cc2007-06-12 06:35:45 -04004336 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004337 return ret;
4338}
4339
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004340int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004341{
4342 struct btrfs_root *root = BTRFS_I(inode)->root;
4343 struct btrfs_trans_handle *trans;
4344 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004345 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004346
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004347 if (BTRFS_I(inode)->dummy_inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04004348 return 0;
4349
Josef Bacik0af3d002010-06-21 14:48:16 -04004350 smp_mb();
4351 nolock = (root->fs_info->closing && root == root->fs_info->tree_root);
4352
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004353 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004354 if (nolock)
4355 trans = btrfs_join_transaction_nolock(root, 1);
4356 else
4357 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004358 if (IS_ERR(trans))
4359 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04004360 btrfs_set_trans_block_group(trans, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04004361 if (nolock)
4362 ret = btrfs_end_transaction_nolock(trans, root);
4363 else
4364 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004365 }
4366 return ret;
4367}
4368
4369/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004370 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004371 * inode changes. But, it is most likely to find the inode in cache.
4372 * FIXME, needs more benchmarking...there are no reasons other than performance
4373 * to keep or drop this code.
4374 */
4375void btrfs_dirty_inode(struct inode *inode)
4376{
4377 struct btrfs_root *root = BTRFS_I(inode)->root;
4378 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004379 int ret;
4380
4381 if (BTRFS_I(inode)->dummy_inode)
4382 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004383
Chris Masonf9295742008-07-17 12:54:14 -04004384 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00004385 BUG_ON(IS_ERR(trans));
Chris Mason39279cc2007-06-12 06:35:45 -04004386 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004387
4388 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004389 if (ret && ret == -ENOSPC) {
4390 /* whoops, lets try again with the full transaction */
4391 btrfs_end_transaction(trans, root);
4392 trans = btrfs_start_transaction(root, 1);
Chris Mason9aeead72010-05-27 10:23:00 -04004393 if (IS_ERR(trans)) {
4394 if (printk_ratelimit()) {
4395 printk(KERN_ERR "btrfs: fail to "
4396 "dirty inode %lu error %ld\n",
4397 inode->i_ino, PTR_ERR(trans));
4398 }
4399 return;
4400 }
Chris Mason94b60442010-05-26 11:02:00 -04004401 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004402
Chris Mason94b60442010-05-26 11:02:00 -04004403 ret = btrfs_update_inode(trans, root, inode);
4404 if (ret) {
Chris Mason9aeead72010-05-27 10:23:00 -04004405 if (printk_ratelimit()) {
4406 printk(KERN_ERR "btrfs: fail to "
4407 "dirty inode %lu error %d\n",
4408 inode->i_ino, ret);
4409 }
Chris Mason94b60442010-05-26 11:02:00 -04004410 }
4411 }
Chris Mason39279cc2007-06-12 06:35:45 -04004412 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004413}
4414
Chris Masond352ac62008-09-29 15:18:18 -04004415/*
4416 * find the highest existing sequence number in a directory
4417 * and then set the in-memory index_cnt variable to reflect
4418 * free sequence numbers
4419 */
Josef Bacikaec74772008-07-24 12:12:38 -04004420static int btrfs_set_inode_index_count(struct inode *inode)
4421{
4422 struct btrfs_root *root = BTRFS_I(inode)->root;
4423 struct btrfs_key key, found_key;
4424 struct btrfs_path *path;
4425 struct extent_buffer *leaf;
4426 int ret;
4427
4428 key.objectid = inode->i_ino;
4429 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4430 key.offset = (u64)-1;
4431
4432 path = btrfs_alloc_path();
4433 if (!path)
4434 return -ENOMEM;
4435
4436 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4437 if (ret < 0)
4438 goto out;
4439 /* FIXME: we should be able to handle this */
4440 if (ret == 0)
4441 goto out;
4442 ret = 0;
4443
4444 /*
4445 * MAGIC NUMBER EXPLANATION:
4446 * since we search a directory based on f_pos we have to start at 2
4447 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4448 * else has to start at 2
4449 */
4450 if (path->slots[0] == 0) {
4451 BTRFS_I(inode)->index_cnt = 2;
4452 goto out;
4453 }
4454
4455 path->slots[0]--;
4456
4457 leaf = path->nodes[0];
4458 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4459
4460 if (found_key.objectid != inode->i_ino ||
4461 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4462 BTRFS_I(inode)->index_cnt = 2;
4463 goto out;
4464 }
4465
4466 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4467out:
4468 btrfs_free_path(path);
4469 return ret;
4470}
4471
Chris Masond352ac62008-09-29 15:18:18 -04004472/*
4473 * helper to find a free sequence number in a given directory. This current
4474 * code is very simple, later versions will do smarter things in the btree
4475 */
Chris Mason3de45862008-11-17 21:02:50 -05004476int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004477{
4478 int ret = 0;
4479
4480 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4481 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05004482 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04004483 return ret;
4484 }
4485
Chris Mason00e4e6b2008-08-05 11:18:09 -04004486 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004487 BTRFS_I(dir)->index_cnt++;
4488
4489 return ret;
4490}
4491
Chris Mason39279cc2007-06-12 06:35:45 -04004492static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4493 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004494 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004495 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05004496 u64 ref_objectid, u64 objectid,
4497 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004498{
4499 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004500 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004501 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004502 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004503 struct btrfs_inode_ref *ref;
4504 struct btrfs_key key[2];
4505 u32 sizes[2];
4506 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004507 int ret;
4508 int owner;
4509
Chris Mason5f39d392007-10-15 16:14:19 -04004510 path = btrfs_alloc_path();
4511 BUG_ON(!path);
4512
Chris Mason39279cc2007-06-12 06:35:45 -04004513 inode = new_inode(root->fs_info->sb);
4514 if (!inode)
4515 return ERR_PTR(-ENOMEM);
4516
Josef Bacikaec74772008-07-24 12:12:38 -04004517 if (dir) {
Chris Mason3de45862008-11-17 21:02:50 -05004518 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004519 if (ret) {
4520 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004521 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004522 }
Josef Bacikaec74772008-07-24 12:12:38 -04004523 }
4524 /*
4525 * index_cnt is ignored for everything but a dir,
4526 * btrfs_get_inode_index_count has an explanation for the magic
4527 * number
4528 */
4529 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004530 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004531 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00004532 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik6a632092009-02-20 11:00:09 -05004533 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04004534
Chris Mason39279cc2007-06-12 06:35:45 -04004535 if (mode & S_IFDIR)
4536 owner = 0;
4537 else
4538 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004539 BTRFS_I(inode)->block_group =
4540 btrfs_find_block_group(root, 0, alloc_hint, owner);
Chris Mason9c583092008-01-29 15:15:18 -05004541
4542 key[0].objectid = objectid;
4543 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4544 key[0].offset = 0;
4545
4546 key[1].objectid = objectid;
4547 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4548 key[1].offset = ref_objectid;
4549
4550 sizes[0] = sizeof(struct btrfs_inode_item);
4551 sizes[1] = name_len + sizeof(*ref);
4552
Chris Masonb9473432009-03-13 11:00:37 -04004553 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004554 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4555 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004556 goto fail;
4557
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004558 inode_init_owner(inode, dir, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04004559 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004560 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004561 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004562 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4563 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004564 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004565
4566 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4567 struct btrfs_inode_ref);
4568 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004569 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004570 ptr = (unsigned long)(ref + 1);
4571 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4572
Chris Mason5f39d392007-10-15 16:14:19 -04004573 btrfs_mark_buffer_dirty(path->nodes[0]);
4574 btrfs_free_path(path);
4575
Chris Mason39279cc2007-06-12 06:35:45 -04004576 location = &BTRFS_I(inode)->location;
4577 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004578 location->offset = 0;
4579 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4580
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004581 btrfs_inherit_iflags(inode, dir);
4582
Chris Mason94272162009-07-02 12:26:06 -04004583 if ((mode & S_IFREG)) {
4584 if (btrfs_test_opt(root, NODATASUM))
4585 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
4586 if (btrfs_test_opt(root, NODATACOW))
4587 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4588 }
4589
Chris Mason39279cc2007-06-12 06:35:45 -04004590 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004591 inode_tree_add(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004592 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004593fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004594 if (dir)
4595 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004596 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004597 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004598 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004599}
4600
4601static inline u8 btrfs_inode_type(struct inode *inode)
4602{
4603 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4604}
4605
Chris Masond352ac62008-09-29 15:18:18 -04004606/*
4607 * utility function to add 'inode' into 'parent_inode' with
4608 * a give name and a given sequence number.
4609 * if 'add_backref' is true, also insert a backref from the
4610 * inode to the parent directory.
4611 */
Chris Masone02119d2008-09-05 16:13:11 -04004612int btrfs_add_link(struct btrfs_trans_handle *trans,
4613 struct inode *parent_inode, struct inode *inode,
4614 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004615{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004616 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004617 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004618 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04004619
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004620 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4621 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4622 } else {
4623 key.objectid = inode->i_ino;
4624 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4625 key.offset = 0;
4626 }
Chris Mason39279cc2007-06-12 06:35:45 -04004627
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004628 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4629 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4630 key.objectid, root->root_key.objectid,
4631 parent_inode->i_ino,
4632 index, name, name_len);
4633 } else if (add_backref) {
4634 ret = btrfs_insert_inode_ref(trans, root,
4635 name, name_len, inode->i_ino,
4636 parent_inode->i_ino, index);
4637 }
4638
Chris Mason39279cc2007-06-12 06:35:45 -04004639 if (ret == 0) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004640 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4641 parent_inode->i_ino, &key,
4642 btrfs_inode_type(inode), index);
4643 BUG_ON(ret);
4644
Chris Masondbe674a2008-07-17 12:54:05 -04004645 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04004646 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04004647 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004648 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004649 }
4650 return ret;
4651}
4652
4653static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Josef Bacika1b075d2010-11-19 20:36:11 +00004654 struct inode *dir, struct dentry *dentry,
4655 struct inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004656{
Josef Bacika1b075d2010-11-19 20:36:11 +00004657 int err = btrfs_add_link(trans, dir, inode,
4658 dentry->d_name.name, dentry->d_name.len,
4659 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004660 if (!err) {
4661 d_instantiate(dentry, inode);
4662 return 0;
4663 }
4664 if (err > 0)
4665 err = -EEXIST;
4666 return err;
4667}
4668
Josef Bacik618e21d2007-07-11 10:18:17 -04004669static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4670 int mode, dev_t rdev)
4671{
4672 struct btrfs_trans_handle *trans;
4673 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004674 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004675 int err;
4676 int drop_inode = 0;
4677 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004678 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004679 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004680
4681 if (!new_valid_dev(rdev))
4682 return -EINVAL;
4683
Yan, Zhenga22285a2010-05-16 10:48:46 -04004684 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4685 if (err)
4686 return err;
4687
Josef Bacik9ed74f22009-09-11 16:12:44 -04004688 /*
4689 * 2 for inode item and ref
4690 * 2 for dir items
4691 * 1 for xattr if selinux is on
4692 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004693 trans = btrfs_start_transaction(root, 5);
4694 if (IS_ERR(trans))
4695 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004696
Josef Bacik618e21d2007-07-11 10:18:17 -04004697 btrfs_set_trans_block_group(trans, dir);
4698
Josef Bacikaec74772008-07-24 12:12:38 -04004699 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Josef Bacika1b075d2010-11-19 20:36:11 +00004700 dentry->d_name.len, dir->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004701 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004702 err = PTR_ERR(inode);
4703 if (IS_ERR(inode))
4704 goto out_unlock;
4705
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004706 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004707 if (err) {
4708 drop_inode = 1;
4709 goto out_unlock;
4710 }
4711
Josef Bacik618e21d2007-07-11 10:18:17 -04004712 btrfs_set_trans_block_group(trans, inode);
Josef Bacika1b075d2010-11-19 20:36:11 +00004713 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004714 if (err)
4715 drop_inode = 1;
4716 else {
4717 inode->i_op = &btrfs_special_inode_operations;
4718 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004719 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004720 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004721 btrfs_update_inode_block_group(trans, inode);
4722 btrfs_update_inode_block_group(trans, dir);
4723out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04004724 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004725 btrfs_end_transaction_throttle(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004726 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004727 if (drop_inode) {
4728 inode_dec_link_count(inode);
4729 iput(inode);
4730 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004731 return err;
4732}
4733
Chris Mason39279cc2007-06-12 06:35:45 -04004734static int btrfs_create(struct inode *dir, struct dentry *dentry,
4735 int mode, struct nameidata *nd)
4736{
4737 struct btrfs_trans_handle *trans;
4738 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004739 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004740 int drop_inode = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004741 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004742 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004743 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004744 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004745
Yan, Zhenga22285a2010-05-16 10:48:46 -04004746 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4747 if (err)
4748 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004749 /*
4750 * 2 for inode item and ref
4751 * 2 for dir items
4752 * 1 for xattr if selinux is on
4753 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004754 trans = btrfs_start_transaction(root, 5);
4755 if (IS_ERR(trans))
4756 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004757
Chris Mason39279cc2007-06-12 06:35:45 -04004758 btrfs_set_trans_block_group(trans, dir);
4759
Josef Bacikaec74772008-07-24 12:12:38 -04004760 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Josef Bacika1b075d2010-11-19 20:36:11 +00004761 dentry->d_name.len, dir->i_ino, objectid,
4762 BTRFS_I(dir)->block_group, mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004763 err = PTR_ERR(inode);
4764 if (IS_ERR(inode))
4765 goto out_unlock;
4766
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004767 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004768 if (err) {
4769 drop_inode = 1;
4770 goto out_unlock;
4771 }
4772
Chris Mason39279cc2007-06-12 06:35:45 -04004773 btrfs_set_trans_block_group(trans, inode);
Josef Bacika1b075d2010-11-19 20:36:11 +00004774 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004775 if (err)
4776 drop_inode = 1;
4777 else {
4778 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004779 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004780 inode->i_fop = &btrfs_file_operations;
4781 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004782 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004783 }
Chris Mason39279cc2007-06-12 06:35:45 -04004784 btrfs_update_inode_block_group(trans, inode);
4785 btrfs_update_inode_block_group(trans, dir);
4786out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04004787 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004788 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004789 if (drop_inode) {
4790 inode_dec_link_count(inode);
4791 iput(inode);
4792 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04004793 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004794 return err;
4795}
4796
4797static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4798 struct dentry *dentry)
4799{
4800 struct btrfs_trans_handle *trans;
4801 struct btrfs_root *root = BTRFS_I(dir)->root;
4802 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004803 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004804 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004805 int err;
4806 int drop_inode = 0;
4807
4808 if (inode->i_nlink == 0)
4809 return -ENOENT;
4810
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004811 /* do not allow sys_link's with other subvols of the same device */
4812 if (root->objectid != BTRFS_I(inode)->root->objectid)
4813 return -EPERM;
4814
Josef Bacik9ed74f22009-09-11 16:12:44 -04004815 btrfs_inc_nlink(inode);
Josef Bacikbc1cbf12010-11-23 19:50:59 +00004816 inode->i_ctime = CURRENT_TIME;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004817
Chris Mason3de45862008-11-17 21:02:50 -05004818 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004819 if (err)
4820 goto fail;
4821
Yan, Zhenga22285a2010-05-16 10:48:46 -04004822 /*
4823 * 1 item for inode ref
4824 * 2 items for dir items
4825 */
4826 trans = btrfs_start_transaction(root, 3);
4827 if (IS_ERR(trans)) {
4828 err = PTR_ERR(trans);
4829 goto fail;
4830 }
Chris Mason5f39d392007-10-15 16:14:19 -04004831
Chris Mason39279cc2007-06-12 06:35:45 -04004832 btrfs_set_trans_block_group(trans, dir);
4833 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04004834
Josef Bacika1b075d2010-11-19 20:36:11 +00004835 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004836
Yan, Zhenga5719522009-09-24 09:17:31 -04004837 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004838 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004839 } else {
Josef Bacik6a912212010-11-20 09:48:00 +00004840 struct dentry *parent = dget_parent(dentry);
Yan, Zhenga5719522009-09-24 09:17:31 -04004841 btrfs_update_inode_block_group(trans, dir);
4842 err = btrfs_update_inode(trans, root, inode);
4843 BUG_ON(err);
Josef Bacik6a912212010-11-20 09:48:00 +00004844 btrfs_log_new_name(trans, inode, NULL, parent);
4845 dput(parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04004846 }
Chris Mason39279cc2007-06-12 06:35:45 -04004847
Chris Masond3c2fdc2007-09-17 10:58:06 -04004848 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004849 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004850fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004851 if (drop_inode) {
4852 inode_dec_link_count(inode);
4853 iput(inode);
4854 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04004855 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004856 return err;
4857}
4858
Chris Mason39279cc2007-06-12 06:35:45 -04004859static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4860{
Chris Masonb9d86662008-05-02 16:13:49 -04004861 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004862 struct btrfs_trans_handle *trans;
4863 struct btrfs_root *root = BTRFS_I(dir)->root;
4864 int err = 0;
4865 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004866 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004867 u64 index = 0;
Chris Masond3c2fdc2007-09-17 10:58:06 -04004868 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004869
Yan, Zhenga22285a2010-05-16 10:48:46 -04004870 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4871 if (err)
4872 return err;
4873
Josef Bacik9ed74f22009-09-11 16:12:44 -04004874 /*
4875 * 2 items for inode and ref
4876 * 2 items for dir items
4877 * 1 for xattr if selinux is on
4878 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004879 trans = btrfs_start_transaction(root, 5);
4880 if (IS_ERR(trans))
4881 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004882 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004883
Josef Bacikaec74772008-07-24 12:12:38 -04004884 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Josef Bacika1b075d2010-11-19 20:36:11 +00004885 dentry->d_name.len, dir->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004886 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4887 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004888 if (IS_ERR(inode)) {
4889 err = PTR_ERR(inode);
4890 goto out_fail;
4891 }
Chris Mason5f39d392007-10-15 16:14:19 -04004892
Chris Mason39279cc2007-06-12 06:35:45 -04004893 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04004894
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004895 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004896 if (err)
4897 goto out_fail;
4898
Chris Mason39279cc2007-06-12 06:35:45 -04004899 inode->i_op = &btrfs_dir_inode_operations;
4900 inode->i_fop = &btrfs_dir_file_operations;
4901 btrfs_set_trans_block_group(trans, inode);
4902
Chris Masondbe674a2008-07-17 12:54:05 -04004903 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004904 err = btrfs_update_inode(trans, root, inode);
4905 if (err)
4906 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004907
Josef Bacika1b075d2010-11-19 20:36:11 +00004908 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
4909 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004910 if (err)
4911 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004912
Chris Mason39279cc2007-06-12 06:35:45 -04004913 d_instantiate(dentry, inode);
4914 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004915 btrfs_update_inode_block_group(trans, inode);
4916 btrfs_update_inode_block_group(trans, dir);
4917
4918out_fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04004919 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004920 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004921 if (drop_on_err)
4922 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04004923 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004924 return err;
4925}
4926
Chris Masond352ac62008-09-29 15:18:18 -04004927/* helper for btfs_get_extent. Given an existing extent in the tree,
4928 * and an extent that you want to insert, deal with overlap and insert
4929 * the new extent into the tree.
4930 */
Chris Mason3b951512008-04-17 11:29:12 -04004931static int merge_extent_mapping(struct extent_map_tree *em_tree,
4932 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004933 struct extent_map *em,
4934 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04004935{
4936 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04004937
Chris Masone6dcd2d2008-07-17 12:53:50 -04004938 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
4939 start_diff = map_start - em->start;
4940 em->start = map_start;
4941 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04004942 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
4943 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004944 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04004945 em->block_len -= start_diff;
4946 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004947 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004948}
4949
Chris Masonc8b97812008-10-29 14:49:59 -04004950static noinline int uncompress_inline(struct btrfs_path *path,
4951 struct inode *inode, struct page *page,
4952 size_t pg_offset, u64 extent_offset,
4953 struct btrfs_file_extent_item *item)
4954{
4955 int ret;
4956 struct extent_buffer *leaf = path->nodes[0];
4957 char *tmp;
4958 size_t max_size;
4959 unsigned long inline_size;
4960 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08004961 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04004962
4963 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08004964 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04004965 max_size = btrfs_file_extent_ram_bytes(leaf, item);
4966 inline_size = btrfs_file_extent_inline_item_len(leaf,
4967 btrfs_item_nr(leaf, path->slots[0]));
4968 tmp = kmalloc(inline_size, GFP_NOFS);
4969 ptr = btrfs_file_extent_inline_start(item);
4970
4971 read_extent_buffer(leaf, tmp, ptr, inline_size);
4972
Chris Mason5b050f02008-11-11 09:34:41 -05004973 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08004974 ret = btrfs_decompress(compress_type, tmp, page,
4975 extent_offset, inline_size, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004976 if (ret) {
4977 char *kaddr = kmap_atomic(page, KM_USER0);
4978 unsigned long copy_size = min_t(u64,
4979 PAGE_CACHE_SIZE - pg_offset,
4980 max_size - extent_offset);
4981 memset(kaddr + pg_offset, 0, copy_size);
4982 kunmap_atomic(kaddr, KM_USER0);
4983 }
4984 kfree(tmp);
4985 return 0;
4986}
4987
Chris Masond352ac62008-09-29 15:18:18 -04004988/*
4989 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05004990 * the ugly parts come from merging extents from the disk with the in-ram
4991 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04004992 * where the in-ram extents might be locked pending data=ordered completion.
4993 *
4994 * This also copies inline extents directly into the page.
4995 */
Chris Masond3977122009-01-05 21:25:51 -05004996
Chris Masona52d9a82007-08-27 16:49:44 -04004997struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05004998 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04004999 int create)
5000{
5001 int ret;
5002 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04005003 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04005004 u64 extent_start = 0;
5005 u64 extent_end = 0;
5006 u64 objectid = inode->i_ino;
5007 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04005008 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04005009 struct btrfs_root *root = BTRFS_I(inode)->root;
5010 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04005011 struct extent_buffer *leaf;
5012 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04005013 struct extent_map *em = NULL;
5014 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05005015 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005016 struct btrfs_trans_handle *trans = NULL;
Li Zefan261507a02010-12-17 14:21:50 +08005017 int compress_type;
Chris Masona52d9a82007-08-27 16:49:44 -04005018
Chris Masona52d9a82007-08-27 16:49:44 -04005019again:
Chris Mason890871b2009-09-02 16:24:52 -04005020 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005021 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005022 if (em)
5023 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005024 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005025
Chris Masona52d9a82007-08-27 16:49:44 -04005026 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005027 if (em->start > start || em->start + em->len <= start)
5028 free_extent_map(em);
5029 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005030 free_extent_map(em);
5031 else
5032 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005033 }
Chris Masond1310b22008-01-24 16:13:08 -05005034 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005035 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005036 err = -ENOMEM;
5037 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005038 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005039 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005040 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005041 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005042 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005043 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005044
5045 if (!path) {
5046 path = btrfs_alloc_path();
5047 BUG_ON(!path);
5048 }
5049
Chris Mason179e29e2007-11-01 11:28:41 -04005050 ret = btrfs_lookup_file_extent(trans, root, path,
5051 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005052 if (ret < 0) {
5053 err = ret;
5054 goto out;
5055 }
5056
5057 if (ret != 0) {
5058 if (path->slots[0] == 0)
5059 goto not_found;
5060 path->slots[0]--;
5061 }
5062
Chris Mason5f39d392007-10-15 16:14:19 -04005063 leaf = path->nodes[0];
5064 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005065 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005066 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005067 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5068 found_type = btrfs_key_type(&found_key);
5069 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005070 found_type != BTRFS_EXTENT_DATA_KEY) {
5071 goto not_found;
5072 }
5073
Chris Mason5f39d392007-10-15 16:14:19 -04005074 found_type = btrfs_file_extent_type(leaf, item);
5075 extent_start = found_key.offset;
Li Zefan261507a02010-12-17 14:21:50 +08005076 compress_type = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005077 if (found_type == BTRFS_FILE_EXTENT_REG ||
5078 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005079 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005080 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005081 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5082 size_t size;
5083 size = btrfs_file_extent_inline_len(leaf, item);
5084 extent_end = (extent_start + size + root->sectorsize - 1) &
5085 ~((u64)root->sectorsize - 1);
5086 }
5087
5088 if (start >= extent_end) {
5089 path->slots[0]++;
5090 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5091 ret = btrfs_next_leaf(root, path);
5092 if (ret < 0) {
5093 err = ret;
5094 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005095 }
Yan Zheng9036c102008-10-30 14:19:41 -04005096 if (ret > 0)
5097 goto not_found;
5098 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005099 }
Yan Zheng9036c102008-10-30 14:19:41 -04005100 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5101 if (found_key.objectid != objectid ||
5102 found_key.type != BTRFS_EXTENT_DATA_KEY)
5103 goto not_found;
5104 if (start + len <= found_key.offset)
5105 goto not_found;
5106 em->start = start;
5107 em->len = found_key.offset - start;
5108 goto not_found_em;
5109 }
5110
Yan Zhengd899e052008-10-30 14:25:28 -04005111 if (found_type == BTRFS_FILE_EXTENT_REG ||
5112 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005113 em->start = extent_start;
5114 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005115 em->orig_start = extent_start -
5116 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04005117 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5118 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005119 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005120 goto insert;
5121 }
Li Zefan261507a02010-12-17 14:21:50 +08005122 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005123 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005124 em->compress_type = compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04005125 em->block_start = bytenr;
5126 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5127 item);
5128 } else {
5129 bytenr += btrfs_file_extent_offset(leaf, item);
5130 em->block_start = bytenr;
5131 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005132 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5133 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005134 }
Chris Masona52d9a82007-08-27 16:49:44 -04005135 goto insert;
5136 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005137 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005138 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005139 size_t size;
5140 size_t extent_offset;
5141 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005142
Chris Masona52d9a82007-08-27 16:49:44 -04005143 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005144 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005145 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005146 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005147 goto out;
5148 }
5149
Yan Zheng9036c102008-10-30 14:19:41 -04005150 size = btrfs_file_extent_inline_len(leaf, item);
5151 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005152 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005153 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005154 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005155 em->len = (copy_size + root->sectorsize - 1) &
5156 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005157 em->orig_start = EXTENT_MAP_INLINE;
Li Zefan261507a02010-12-17 14:21:50 +08005158 if (compress_type) {
Chris Masonc8b97812008-10-29 14:49:59 -04005159 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Li Zefan261507a02010-12-17 14:21:50 +08005160 em->compress_type = compress_type;
5161 }
Yan689f9342007-10-29 11:41:07 -04005162 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005163 if (create == 0 && !PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08005164 if (btrfs_file_extent_compression(leaf, item) !=
5165 BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -04005166 ret = uncompress_inline(path, inode, page,
5167 pg_offset,
5168 extent_offset, item);
5169 BUG_ON(ret);
5170 } else {
5171 map = kmap(page);
5172 read_extent_buffer(leaf, map + pg_offset, ptr,
5173 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005174 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5175 memset(map + pg_offset + copy_size, 0,
5176 PAGE_CACHE_SIZE - pg_offset -
5177 copy_size);
5178 }
Chris Masonc8b97812008-10-29 14:49:59 -04005179 kunmap(page);
5180 }
Chris Mason179e29e2007-11-01 11:28:41 -04005181 flush_dcache_page(page);
5182 } else if (create && PageUptodate(page)) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005183 WARN_ON(1);
Chris Mason179e29e2007-11-01 11:28:41 -04005184 if (!trans) {
5185 kunmap(page);
5186 free_extent_map(em);
5187 em = NULL;
5188 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04005189 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005190 if (IS_ERR(trans))
5191 return ERR_CAST(trans);
Chris Mason179e29e2007-11-01 11:28:41 -04005192 goto again;
5193 }
Chris Masonc8b97812008-10-29 14:49:59 -04005194 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005195 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005196 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005197 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005198 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005199 }
Chris Masond1310b22008-01-24 16:13:08 -05005200 set_extent_uptodate(io_tree, em->start,
5201 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005202 goto insert;
5203 } else {
Chris Masond3977122009-01-05 21:25:51 -05005204 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005205 WARN_ON(1);
5206 }
5207not_found:
5208 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005209 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005210not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005211 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005212 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005213insert:
5214 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05005215 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005216 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5217 "[%llu %llu]\n", (unsigned long long)em->start,
5218 (unsigned long long)em->len,
5219 (unsigned long long)start,
5220 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005221 err = -EIO;
5222 goto out;
5223 }
Chris Masond1310b22008-01-24 16:13:08 -05005224
5225 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005226 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005227 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005228 /* it is possible that someone inserted the extent into the tree
5229 * while we had the lock dropped. It is also possible that
5230 * an overlapping map exists in the tree
5231 */
Chris Masona52d9a82007-08-27 16:49:44 -04005232 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005233 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005234
5235 ret = 0;
5236
Chris Mason3b951512008-04-17 11:29:12 -04005237 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005238 if (existing && (existing->start > start ||
5239 existing->start + existing->len <= start)) {
5240 free_extent_map(existing);
5241 existing = NULL;
5242 }
Chris Mason3b951512008-04-17 11:29:12 -04005243 if (!existing) {
5244 existing = lookup_extent_mapping(em_tree, em->start,
5245 em->len);
5246 if (existing) {
5247 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005248 em, start,
5249 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005250 free_extent_map(existing);
5251 if (err) {
5252 free_extent_map(em);
5253 em = NULL;
5254 }
5255 } else {
5256 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005257 free_extent_map(em);
5258 em = NULL;
5259 }
5260 } else {
5261 free_extent_map(em);
5262 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005263 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005264 }
Chris Masona52d9a82007-08-27 16:49:44 -04005265 }
Chris Mason890871b2009-09-02 16:24:52 -04005266 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005267out:
Chris Masonf4219502008-07-22 11:18:09 -04005268 if (path)
5269 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005270 if (trans) {
5271 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005272 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005273 err = ret;
5274 }
Chris Masona52d9a82007-08-27 16:49:44 -04005275 if (err) {
5276 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005277 return ERR_PTR(err);
5278 }
5279 return em;
5280}
5281
Josef Bacik4b46fce2010-05-23 11:00:55 -04005282static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5283 u64 start, u64 len)
5284{
5285 struct btrfs_root *root = BTRFS_I(inode)->root;
5286 struct btrfs_trans_handle *trans;
5287 struct extent_map *em;
5288 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5289 struct btrfs_key ins;
5290 u64 alloc_hint;
5291 int ret;
5292
5293 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5294
5295 trans = btrfs_join_transaction(root, 0);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005296 if (IS_ERR(trans))
5297 return ERR_CAST(trans);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005298
5299 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5300
5301 alloc_hint = get_extent_allocation_hint(inode, start, len);
5302 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
5303 alloc_hint, (u64)-1, &ins, 1);
5304 if (ret) {
5305 em = ERR_PTR(ret);
5306 goto out;
5307 }
5308
5309 em = alloc_extent_map(GFP_NOFS);
5310 if (!em) {
5311 em = ERR_PTR(-ENOMEM);
5312 goto out;
5313 }
5314
5315 em->start = start;
5316 em->orig_start = em->start;
5317 em->len = ins.offset;
5318
5319 em->block_start = ins.objectid;
5320 em->block_len = ins.offset;
5321 em->bdev = root->fs_info->fs_devices->latest_bdev;
5322 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5323
5324 while (1) {
5325 write_lock(&em_tree->lock);
5326 ret = add_extent_mapping(em_tree, em);
5327 write_unlock(&em_tree->lock);
5328 if (ret != -EEXIST)
5329 break;
5330 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5331 }
5332
5333 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5334 ins.offset, ins.offset, 0);
5335 if (ret) {
5336 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5337 em = ERR_PTR(ret);
5338 }
5339out:
5340 btrfs_end_transaction(trans, root);
5341 return em;
5342}
5343
Chris Mason46bfbb52010-05-26 11:04:10 -04005344/*
5345 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5346 * block must be cow'd
5347 */
5348static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5349 struct inode *inode, u64 offset, u64 len)
5350{
5351 struct btrfs_path *path;
5352 int ret;
5353 struct extent_buffer *leaf;
5354 struct btrfs_root *root = BTRFS_I(inode)->root;
5355 struct btrfs_file_extent_item *fi;
5356 struct btrfs_key key;
5357 u64 disk_bytenr;
5358 u64 backref_offset;
5359 u64 extent_end;
5360 u64 num_bytes;
5361 int slot;
5362 int found_type;
5363
5364 path = btrfs_alloc_path();
5365 if (!path)
5366 return -ENOMEM;
5367
5368 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
5369 offset, 0);
5370 if (ret < 0)
5371 goto out;
5372
5373 slot = path->slots[0];
5374 if (ret == 1) {
5375 if (slot == 0) {
5376 /* can't find the item, must cow */
5377 ret = 0;
5378 goto out;
5379 }
5380 slot--;
5381 }
5382 ret = 0;
5383 leaf = path->nodes[0];
5384 btrfs_item_key_to_cpu(leaf, &key, slot);
5385 if (key.objectid != inode->i_ino ||
5386 key.type != BTRFS_EXTENT_DATA_KEY) {
5387 /* not our file or wrong item type, must cow */
5388 goto out;
5389 }
5390
5391 if (key.offset > offset) {
5392 /* Wrong offset, must cow */
5393 goto out;
5394 }
5395
5396 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5397 found_type = btrfs_file_extent_type(leaf, fi);
5398 if (found_type != BTRFS_FILE_EXTENT_REG &&
5399 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5400 /* not a regular extent, must cow */
5401 goto out;
5402 }
5403 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5404 backref_offset = btrfs_file_extent_offset(leaf, fi);
5405
5406 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5407 if (extent_end < offset + len) {
5408 /* extent doesn't include our full range, must cow */
5409 goto out;
5410 }
5411
5412 if (btrfs_extent_readonly(root, disk_bytenr))
5413 goto out;
5414
5415 /*
5416 * look for other files referencing this extent, if we
5417 * find any we must cow
5418 */
5419 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
5420 key.offset - backref_offset, disk_bytenr))
5421 goto out;
5422
5423 /*
5424 * adjust disk_bytenr and num_bytes to cover just the bytes
5425 * in this extent we are about to write. If there
5426 * are any csums in that range we have to cow in order
5427 * to keep the csums correct
5428 */
5429 disk_bytenr += backref_offset;
5430 disk_bytenr += offset - key.offset;
5431 num_bytes = min(offset + len, extent_end) - offset;
5432 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5433 goto out;
5434 /*
5435 * all of the above have passed, it is safe to overwrite this extent
5436 * without cow
5437 */
5438 ret = 1;
5439out:
5440 btrfs_free_path(path);
5441 return ret;
5442}
5443
Josef Bacik4b46fce2010-05-23 11:00:55 -04005444static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5445 struct buffer_head *bh_result, int create)
5446{
5447 struct extent_map *em;
5448 struct btrfs_root *root = BTRFS_I(inode)->root;
5449 u64 start = iblock << inode->i_blkbits;
5450 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04005451 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005452
5453 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
5454 if (IS_ERR(em))
5455 return PTR_ERR(em);
5456
5457 /*
5458 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5459 * io. INLINE is special, and we could probably kludge it in here, but
5460 * it's still buffered so for safety lets just fall back to the generic
5461 * buffered path.
5462 *
5463 * For COMPRESSED we _have_ to read the entire extent in so we can
5464 * decompress it, so there will be buffering required no matter what we
5465 * do, so go ahead and fallback to buffered.
5466 *
5467 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5468 * to buffered IO. Don't blame me, this is the price we pay for using
5469 * the generic code.
5470 */
5471 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5472 em->block_start == EXTENT_MAP_INLINE) {
5473 free_extent_map(em);
5474 return -ENOTBLK;
5475 }
5476
5477 /* Just a good old fashioned hole, return */
5478 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
5479 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5480 free_extent_map(em);
5481 /* DIO will do one hole at a time, so just unlock a sector */
5482 unlock_extent(&BTRFS_I(inode)->io_tree, start,
5483 start + root->sectorsize - 1, GFP_NOFS);
5484 return 0;
5485 }
5486
5487 /*
5488 * We don't allocate a new extent in the following cases
5489 *
5490 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5491 * existing extent.
5492 * 2) The extent is marked as PREALLOC. We're good to go here and can
5493 * just use the extent.
5494 *
5495 */
Chris Mason46bfbb52010-05-26 11:04:10 -04005496 if (!create) {
5497 len = em->len - (start - em->start);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005498 goto map;
Chris Mason46bfbb52010-05-26 11:04:10 -04005499 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005500
5501 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
5502 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
5503 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005504 int type;
5505 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04005506 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005507
5508 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5509 type = BTRFS_ORDERED_PREALLOC;
5510 else
5511 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04005512 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04005513 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04005514
5515 /*
5516 * we're not going to log anything, but we do need
5517 * to make sure the current transaction stays open
5518 * while we look for nocow cross refs
5519 */
5520 trans = btrfs_join_transaction(root, 0);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005521 if (IS_ERR(trans))
Chris Mason46bfbb52010-05-26 11:04:10 -04005522 goto must_cow;
5523
5524 if (can_nocow_odirect(trans, inode, start, len) == 1) {
5525 ret = btrfs_add_ordered_extent_dio(inode, start,
5526 block_start, len, len, type);
5527 btrfs_end_transaction(trans, root);
5528 if (ret) {
5529 free_extent_map(em);
5530 return ret;
5531 }
5532 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005533 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005534 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005535 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005536must_cow:
5537 /*
5538 * this will cow the extent, reset the len in case we changed
5539 * it above
5540 */
5541 len = bh_result->b_size;
5542 free_extent_map(em);
5543 em = btrfs_new_extent_direct(inode, start, len);
5544 if (IS_ERR(em))
5545 return PTR_ERR(em);
5546 len = min(len, em->len - (start - em->start));
5547unlock:
Chris Mason4845e442010-05-25 20:56:50 -04005548 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
5549 EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
5550 0, NULL, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005551map:
5552 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
5553 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04005554 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005555 bh_result->b_bdev = em->bdev;
5556 set_buffer_mapped(bh_result);
5557 if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5558 set_buffer_new(bh_result);
5559
5560 free_extent_map(em);
5561
5562 return 0;
5563}
5564
5565struct btrfs_dio_private {
5566 struct inode *inode;
5567 u64 logical_offset;
5568 u64 disk_bytenr;
5569 u64 bytes;
5570 u32 *csums;
5571 void *private;
Miao Xiee65e1532010-11-22 03:04:43 +00005572
5573 /* number of bios pending for this dio */
5574 atomic_t pending_bios;
5575
5576 /* IO errors */
5577 int errors;
5578
5579 struct bio *orig_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005580};
5581
5582static void btrfs_endio_direct_read(struct bio *bio, int err)
5583{
Miao Xiee65e1532010-11-22 03:04:43 +00005584 struct btrfs_dio_private *dip = bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005585 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
5586 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005587 struct inode *inode = dip->inode;
5588 struct btrfs_root *root = BTRFS_I(inode)->root;
5589 u64 start;
5590 u32 *private = dip->csums;
5591
5592 start = dip->logical_offset;
5593 do {
5594 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
5595 struct page *page = bvec->bv_page;
5596 char *kaddr;
5597 u32 csum = ~(u32)0;
5598 unsigned long flags;
5599
5600 local_irq_save(flags);
5601 kaddr = kmap_atomic(page, KM_IRQ0);
5602 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
5603 csum, bvec->bv_len);
5604 btrfs_csum_final(csum, (char *)&csum);
5605 kunmap_atomic(kaddr, KM_IRQ0);
5606 local_irq_restore(flags);
5607
5608 flush_dcache_page(bvec->bv_page);
5609 if (csum != *private) {
5610 printk(KERN_ERR "btrfs csum failed ino %lu off"
5611 " %llu csum %u private %u\n",
5612 inode->i_ino, (unsigned long long)start,
5613 csum, *private);
5614 err = -EIO;
5615 }
5616 }
5617
5618 start += bvec->bv_len;
5619 private++;
5620 bvec++;
5621 } while (bvec <= bvec_end);
5622
5623 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
5624 dip->logical_offset + dip->bytes - 1, GFP_NOFS);
5625 bio->bi_private = dip->private;
5626
5627 kfree(dip->csums);
5628 kfree(dip);
5629 dio_end_io(bio, err);
5630}
5631
5632static void btrfs_endio_direct_write(struct bio *bio, int err)
5633{
5634 struct btrfs_dio_private *dip = bio->bi_private;
5635 struct inode *inode = dip->inode;
5636 struct btrfs_root *root = BTRFS_I(inode)->root;
5637 struct btrfs_trans_handle *trans;
5638 struct btrfs_ordered_extent *ordered = NULL;
5639 struct extent_state *cached_state = NULL;
Chris Mason163cf092010-11-28 19:56:33 -05005640 u64 ordered_offset = dip->logical_offset;
5641 u64 ordered_bytes = dip->bytes;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005642 int ret;
5643
5644 if (err)
5645 goto out_done;
Chris Mason163cf092010-11-28 19:56:33 -05005646again:
5647 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
5648 &ordered_offset,
5649 ordered_bytes);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005650 if (!ret)
Chris Mason163cf092010-11-28 19:56:33 -05005651 goto out_test;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005652
5653 BUG_ON(!ordered);
5654
5655 trans = btrfs_join_transaction(root, 1);
Tsutomu Itoh3612b492011-01-25 02:51:38 +00005656 if (IS_ERR(trans)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005657 err = -ENOMEM;
5658 goto out;
5659 }
5660 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5661
5662 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
5663 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5664 if (!ret)
5665 ret = btrfs_update_inode(trans, root, inode);
5666 err = ret;
5667 goto out;
5668 }
5669
5670 lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5671 ordered->file_offset + ordered->len - 1, 0,
5672 &cached_state, GFP_NOFS);
5673
5674 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
5675 ret = btrfs_mark_extent_written(trans, inode,
5676 ordered->file_offset,
5677 ordered->file_offset +
5678 ordered->len);
5679 if (ret) {
5680 err = ret;
5681 goto out_unlock;
5682 }
5683 } else {
5684 ret = insert_reserved_file_extent(trans, inode,
5685 ordered->file_offset,
5686 ordered->start,
5687 ordered->disk_len,
5688 ordered->len,
5689 ordered->len,
5690 0, 0, 0,
5691 BTRFS_FILE_EXTENT_REG);
5692 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
5693 ordered->file_offset, ordered->len);
5694 if (ret) {
5695 err = ret;
5696 WARN_ON(1);
5697 goto out_unlock;
5698 }
5699 }
5700
5701 add_pending_csums(trans, inode, ordered->file_offset, &ordered->list);
5702 btrfs_ordered_update_i_size(inode, 0, ordered);
5703 btrfs_update_inode(trans, root, inode);
5704out_unlock:
5705 unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5706 ordered->file_offset + ordered->len - 1,
5707 &cached_state, GFP_NOFS);
5708out:
5709 btrfs_delalloc_release_metadata(inode, ordered->len);
5710 btrfs_end_transaction(trans, root);
Chris Mason163cf092010-11-28 19:56:33 -05005711 ordered_offset = ordered->file_offset + ordered->len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005712 btrfs_put_ordered_extent(ordered);
5713 btrfs_put_ordered_extent(ordered);
Chris Mason163cf092010-11-28 19:56:33 -05005714
5715out_test:
5716 /*
5717 * our bio might span multiple ordered extents. If we haven't
5718 * completed the accounting for the whole dio, go back and try again
5719 */
5720 if (ordered_offset < dip->logical_offset + dip->bytes) {
5721 ordered_bytes = dip->logical_offset + dip->bytes -
5722 ordered_offset;
5723 goto again;
5724 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005725out_done:
5726 bio->bi_private = dip->private;
5727
5728 kfree(dip->csums);
5729 kfree(dip);
5730 dio_end_io(bio, err);
5731}
5732
Chris Masoneaf25d92010-05-25 09:48:28 -04005733static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
5734 struct bio *bio, int mirror_num,
5735 unsigned long bio_flags, u64 offset)
5736{
5737 int ret;
5738 struct btrfs_root *root = BTRFS_I(inode)->root;
5739 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
5740 BUG_ON(ret);
5741 return 0;
5742}
5743
Miao Xiee65e1532010-11-22 03:04:43 +00005744static void btrfs_end_dio_bio(struct bio *bio, int err)
5745{
5746 struct btrfs_dio_private *dip = bio->bi_private;
5747
5748 if (err) {
5749 printk(KERN_ERR "btrfs direct IO failed ino %lu rw %lu "
Jan Beulich3dd14622010-12-07 14:54:09 +00005750 "sector %#Lx len %u err no %d\n",
5751 dip->inode->i_ino, bio->bi_rw,
5752 (unsigned long long)bio->bi_sector, bio->bi_size, err);
Miao Xiee65e1532010-11-22 03:04:43 +00005753 dip->errors = 1;
5754
5755 /*
5756 * before atomic variable goto zero, we must make sure
5757 * dip->errors is perceived to be set.
5758 */
5759 smp_mb__before_atomic_dec();
5760 }
5761
5762 /* if there are more bios still pending for this dio, just exit */
5763 if (!atomic_dec_and_test(&dip->pending_bios))
5764 goto out;
5765
5766 if (dip->errors)
5767 bio_io_error(dip->orig_bio);
5768 else {
5769 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
5770 bio_endio(dip->orig_bio, 0);
5771 }
5772out:
5773 bio_put(bio);
5774}
5775
5776static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
5777 u64 first_sector, gfp_t gfp_flags)
5778{
5779 int nr_vecs = bio_get_nr_vecs(bdev);
5780 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
5781}
5782
5783static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
5784 int rw, u64 file_offset, int skip_sum,
5785 u32 *csums)
5786{
5787 int write = rw & REQ_WRITE;
5788 struct btrfs_root *root = BTRFS_I(inode)->root;
5789 int ret;
5790
5791 bio_get(bio);
5792 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
5793 if (ret)
5794 goto err;
5795
5796 if (write && !skip_sum) {
5797 ret = btrfs_wq_submit_bio(root->fs_info,
5798 inode, rw, bio, 0, 0,
5799 file_offset,
5800 __btrfs_submit_bio_start_direct_io,
5801 __btrfs_submit_bio_done);
5802 goto err;
5803 } else if (!skip_sum)
5804 btrfs_lookup_bio_sums_dio(root, inode, bio,
5805 file_offset, csums);
5806
5807 ret = btrfs_map_bio(root, rw, bio, 0, 1);
5808err:
5809 bio_put(bio);
5810 return ret;
5811}
5812
5813static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
5814 int skip_sum)
5815{
5816 struct inode *inode = dip->inode;
5817 struct btrfs_root *root = BTRFS_I(inode)->root;
5818 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5819 struct bio *bio;
5820 struct bio *orig_bio = dip->orig_bio;
5821 struct bio_vec *bvec = orig_bio->bi_io_vec;
5822 u64 start_sector = orig_bio->bi_sector;
5823 u64 file_offset = dip->logical_offset;
5824 u64 submit_len = 0;
5825 u64 map_length;
5826 int nr_pages = 0;
5827 u32 *csums = dip->csums;
5828 int ret = 0;
5829
5830 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
5831 if (!bio)
5832 return -ENOMEM;
5833 bio->bi_private = dip;
5834 bio->bi_end_io = btrfs_end_dio_bio;
5835 atomic_inc(&dip->pending_bios);
5836
5837 map_length = orig_bio->bi_size;
5838 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
5839 &map_length, NULL, 0);
5840 if (ret) {
5841 bio_put(bio);
5842 return -EIO;
5843 }
5844
5845 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
5846 if (unlikely(map_length < submit_len + bvec->bv_len ||
5847 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5848 bvec->bv_offset) < bvec->bv_len)) {
5849 /*
5850 * inc the count before we submit the bio so
5851 * we know the end IO handler won't happen before
5852 * we inc the count. Otherwise, the dip might get freed
5853 * before we're done setting it up
5854 */
5855 atomic_inc(&dip->pending_bios);
5856 ret = __btrfs_submit_dio_bio(bio, inode, rw,
5857 file_offset, skip_sum,
5858 csums);
5859 if (ret) {
5860 bio_put(bio);
5861 atomic_dec(&dip->pending_bios);
5862 goto out_err;
5863 }
5864
5865 if (!skip_sum)
5866 csums = csums + nr_pages;
5867 start_sector += submit_len >> 9;
5868 file_offset += submit_len;
5869
5870 submit_len = 0;
5871 nr_pages = 0;
5872
5873 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
5874 start_sector, GFP_NOFS);
5875 if (!bio)
5876 goto out_err;
5877 bio->bi_private = dip;
5878 bio->bi_end_io = btrfs_end_dio_bio;
5879
5880 map_length = orig_bio->bi_size;
5881 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
5882 &map_length, NULL, 0);
5883 if (ret) {
5884 bio_put(bio);
5885 goto out_err;
5886 }
5887 } else {
5888 submit_len += bvec->bv_len;
5889 nr_pages ++;
5890 bvec++;
5891 }
5892 }
5893
5894 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
5895 csums);
5896 if (!ret)
5897 return 0;
5898
5899 bio_put(bio);
5900out_err:
5901 dip->errors = 1;
5902 /*
5903 * before atomic variable goto zero, we must
5904 * make sure dip->errors is perceived to be set.
5905 */
5906 smp_mb__before_atomic_dec();
5907 if (atomic_dec_and_test(&dip->pending_bios))
5908 bio_io_error(dip->orig_bio);
5909
5910 /* bio_end_io() will handle error, so we needn't return it */
5911 return 0;
5912}
5913
Josef Bacik4b46fce2010-05-23 11:00:55 -04005914static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
5915 loff_t file_offset)
5916{
5917 struct btrfs_root *root = BTRFS_I(inode)->root;
5918 struct btrfs_dio_private *dip;
5919 struct bio_vec *bvec = bio->bi_io_vec;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005920 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005921 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005922 int ret = 0;
5923
5924 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
5925
5926 dip = kmalloc(sizeof(*dip), GFP_NOFS);
5927 if (!dip) {
5928 ret = -ENOMEM;
5929 goto free_ordered;
5930 }
5931 dip->csums = NULL;
5932
5933 if (!skip_sum) {
5934 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
5935 if (!dip->csums) {
5936 ret = -ENOMEM;
5937 goto free_ordered;
5938 }
5939 }
5940
5941 dip->private = bio->bi_private;
5942 dip->inode = inode;
5943 dip->logical_offset = file_offset;
5944
Josef Bacik4b46fce2010-05-23 11:00:55 -04005945 dip->bytes = 0;
5946 do {
5947 dip->bytes += bvec->bv_len;
5948 bvec++;
5949 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
5950
Chris Mason46bfbb52010-05-26 11:04:10 -04005951 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005952 bio->bi_private = dip;
Miao Xiee65e1532010-11-22 03:04:43 +00005953 dip->errors = 0;
5954 dip->orig_bio = bio;
5955 atomic_set(&dip->pending_bios, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005956
5957 if (write)
5958 bio->bi_end_io = btrfs_endio_direct_write;
5959 else
5960 bio->bi_end_io = btrfs_endio_direct_read;
5961
Miao Xiee65e1532010-11-22 03:04:43 +00005962 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
5963 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04005964 return;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005965free_ordered:
5966 /*
5967 * If this is a write, we need to clean up the reserved space and kill
5968 * the ordered extent.
5969 */
5970 if (write) {
5971 struct btrfs_ordered_extent *ordered;
Josef Bacik955256f2010-11-19 09:41:10 -05005972 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005973 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
5974 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
5975 btrfs_free_reserved_extent(root, ordered->start,
5976 ordered->disk_len);
5977 btrfs_put_ordered_extent(ordered);
5978 btrfs_put_ordered_extent(ordered);
5979 }
5980 bio_endio(bio, ret);
5981}
5982
Chris Mason5a5f79b2010-05-26 21:33:37 -04005983static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
5984 const struct iovec *iov, loff_t offset,
5985 unsigned long nr_segs)
5986{
5987 int seg;
5988 size_t size;
5989 unsigned long addr;
5990 unsigned blocksize_mask = root->sectorsize - 1;
5991 ssize_t retval = -EINVAL;
5992 loff_t end = offset;
5993
5994 if (offset & blocksize_mask)
5995 goto out;
5996
5997 /* Check the memory alignment. Blocks cannot straddle pages */
5998 for (seg = 0; seg < nr_segs; seg++) {
5999 addr = (unsigned long)iov[seg].iov_base;
6000 size = iov[seg].iov_len;
6001 end += size;
6002 if ((addr & blocksize_mask) || (size & blocksize_mask))
6003 goto out;
6004 }
6005 retval = 0;
6006out:
6007 return retval;
6008}
Chris Mason16432982008-04-10 10:23:21 -04006009static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6010 const struct iovec *iov, loff_t offset,
6011 unsigned long nr_segs)
6012{
Josef Bacik4b46fce2010-05-23 11:00:55 -04006013 struct file *file = iocb->ki_filp;
6014 struct inode *inode = file->f_mapping->host;
6015 struct btrfs_ordered_extent *ordered;
Chris Mason4845e442010-05-25 20:56:50 -04006016 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006017 u64 lockstart, lockend;
6018 ssize_t ret;
Chris Mason4845e442010-05-25 20:56:50 -04006019 int writing = rw & WRITE;
6020 int write_bits = 0;
Chris Mason3f7c5792010-05-26 10:59:53 -04006021 size_t count = iov_length(iov, nr_segs);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006022
Chris Mason5a5f79b2010-05-26 21:33:37 -04006023 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
6024 offset, nr_segs)) {
6025 return 0;
6026 }
6027
Josef Bacik4b46fce2010-05-23 11:00:55 -04006028 lockstart = offset;
Chris Mason3f7c5792010-05-26 10:59:53 -04006029 lockend = offset + count - 1;
6030
6031 if (writing) {
6032 ret = btrfs_delalloc_reserve_space(inode, count);
6033 if (ret)
6034 goto out;
6035 }
Chris Mason4845e442010-05-25 20:56:50 -04006036
Josef Bacik4b46fce2010-05-23 11:00:55 -04006037 while (1) {
Chris Mason4845e442010-05-25 20:56:50 -04006038 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6039 0, &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006040 /*
6041 * We're concerned with the entire range that we're going to be
6042 * doing DIO to, so we need to make sure theres no ordered
6043 * extents in this range.
6044 */
6045 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6046 lockend - lockstart + 1);
6047 if (!ordered)
6048 break;
Chris Mason4845e442010-05-25 20:56:50 -04006049 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6050 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006051 btrfs_start_ordered_extent(inode, ordered, 1);
6052 btrfs_put_ordered_extent(ordered);
6053 cond_resched();
6054 }
6055
Chris Mason4845e442010-05-25 20:56:50 -04006056 /*
6057 * we don't use btrfs_set_extent_delalloc because we don't want
6058 * the dirty or uptodate bits
6059 */
6060 if (writing) {
6061 write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
6062 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6063 EXTENT_DELALLOC, 0, NULL, &cached_state,
6064 GFP_NOFS);
6065 if (ret) {
6066 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6067 lockend, EXTENT_LOCKED | write_bits,
6068 1, 0, &cached_state, GFP_NOFS);
6069 goto out;
6070 }
6071 }
6072
6073 free_extent_state(cached_state);
6074 cached_state = NULL;
6075
Chris Mason5a5f79b2010-05-26 21:33:37 -04006076 ret = __blockdev_direct_IO(rw, iocb, inode,
6077 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6078 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6079 btrfs_submit_direct, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006080
6081 if (ret < 0 && ret != -EIOCBQUEUED) {
Chris Mason4845e442010-05-25 20:56:50 -04006082 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
6083 offset + iov_length(iov, nr_segs) - 1,
6084 EXTENT_LOCKED | write_bits, 1, 0,
6085 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006086 } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
6087 /*
6088 * We're falling back to buffered, unlock the section we didn't
6089 * do IO on.
6090 */
Chris Mason4845e442010-05-25 20:56:50 -04006091 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
6092 offset + iov_length(iov, nr_segs) - 1,
6093 EXTENT_LOCKED | write_bits, 1, 0,
6094 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006095 }
Chris Mason4845e442010-05-25 20:56:50 -04006096out:
6097 free_extent_state(cached_state);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006098 return ret;
Chris Mason16432982008-04-10 10:23:21 -04006099}
6100
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006101static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6102 __u64 start, __u64 len)
6103{
6104 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
6105}
6106
Chris Mason9ebefb182007-06-15 13:50:00 -04006107int btrfs_readpage(struct file *file, struct page *page)
6108{
Chris Masond1310b22008-01-24 16:13:08 -05006109 struct extent_io_tree *tree;
6110 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006111 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04006112}
Chris Mason1832a6d2007-12-21 16:27:21 -05006113
Chris Mason39279cc2007-06-12 06:35:45 -04006114static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6115{
Chris Masond1310b22008-01-24 16:13:08 -05006116 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04006117
6118
6119 if (current->flags & PF_MEMALLOC) {
6120 redirty_page_for_writepage(wbc, page);
6121 unlock_page(page);
6122 return 0;
6123 }
Chris Masond1310b22008-01-24 16:13:08 -05006124 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006125 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6126}
Chris Mason39279cc2007-06-12 06:35:45 -04006127
Chris Masonf4219502008-07-22 11:18:09 -04006128int btrfs_writepages(struct address_space *mapping,
6129 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04006130{
Chris Masond1310b22008-01-24 16:13:08 -05006131 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05006132
Chris Masond1310b22008-01-24 16:13:08 -05006133 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04006134 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6135}
6136
Chris Mason3ab2fb52007-11-08 10:59:22 -05006137static int
6138btrfs_readpages(struct file *file, struct address_space *mapping,
6139 struct list_head *pages, unsigned nr_pages)
6140{
Chris Masond1310b22008-01-24 16:13:08 -05006141 struct extent_io_tree *tree;
6142 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05006143 return extent_readpages(tree, mapping, pages, nr_pages,
6144 btrfs_get_extent);
6145}
Chris Masone6dcd2d2008-07-17 12:53:50 -04006146static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04006147{
Chris Masond1310b22008-01-24 16:13:08 -05006148 struct extent_io_tree *tree;
6149 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04006150 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006151
Chris Masond1310b22008-01-24 16:13:08 -05006152 tree = &BTRFS_I(page->mapping->host)->io_tree;
6153 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05006154 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04006155 if (ret == 1) {
6156 ClearPagePrivate(page);
6157 set_page_private(page, 0);
6158 page_cache_release(page);
6159 }
6160 return ret;
6161}
Chris Mason39279cc2007-06-12 06:35:45 -04006162
Chris Masone6dcd2d2008-07-17 12:53:50 -04006163static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6164{
Chris Mason98509cf2008-09-11 15:51:43 -04006165 if (PageWriteback(page) || PageDirty(page))
6166 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05006167 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006168}
6169
Chris Masona52d9a82007-08-27 16:49:44 -04006170static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6171{
Chris Masond1310b22008-01-24 16:13:08 -05006172 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006173 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006174 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006175 u64 page_start = page_offset(page);
6176 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006177
Chris Mason8b62b722009-09-02 16:53:46 -04006178
6179 /*
6180 * we have the page locked, so new writeback can't start,
6181 * and the dirty bit won't be cleared while we are here.
6182 *
6183 * Wait for IO on this page so that we can safely clear
6184 * the PagePrivate2 bit and do ordered accounting
6185 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006186 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04006187
Chris Masond1310b22008-01-24 16:13:08 -05006188 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006189 if (offset) {
6190 btrfs_releasepage(page, GFP_NOFS);
6191 return;
6192 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00006193 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6194 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006195 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
6196 page_offset(page));
6197 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006198 /*
6199 * IO on this page will never be started, so we need
6200 * to account for any ordered extents now
6201 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006202 clear_extent_bit(tree, page_start, page_end,
6203 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04006204 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006205 &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006206 /*
6207 * whoever cleared the private bit is responsible
6208 * for the finish_ordered_io
6209 */
6210 if (TestClearPagePrivate2(page)) {
6211 btrfs_finish_ordered_io(page->mapping->host,
6212 page_start, page_end);
6213 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006214 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006215 cached_state = NULL;
6216 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6217 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006218 }
6219 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006220 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +00006221 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006222 __btrfs_releasepage(page, GFP_NOFS);
6223
Chris Mason4a096752008-07-21 10:29:44 -04006224 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006225 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04006226 ClearPagePrivate(page);
6227 set_page_private(page, 0);
6228 page_cache_release(page);
6229 }
Chris Mason39279cc2007-06-12 06:35:45 -04006230}
6231
Chris Mason9ebefb182007-06-15 13:50:00 -04006232/*
6233 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6234 * called from a page fault handler when a page is first dirtied. Hence we must
6235 * be careful to check for EOF conditions here. We set the page up correctly
6236 * for a written page which means we get ENOSPC checking when writing into
6237 * holes and correct delalloc and unwritten extent mapping on filesystems that
6238 * support these features.
6239 *
6240 * We are not allowed to take the i_mutex here so we have to play games to
6241 * protect against truncate races as the page could now be beyond EOF. Because
6242 * vmtruncate() writes the inode size before removing pages, once we have the
6243 * page lock we can determine safely if the page is beyond EOF. If it is not
6244 * beyond EOF, then the page is guaranteed safe against truncation until we
6245 * unlock the page.
6246 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006247int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006248{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006249 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006250 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006251 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006252 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6253 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006254 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006255 char *kaddr;
6256 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006257 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006258 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04006259 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006260 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006261
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006262 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Nick Piggin56a76f82009-03-31 15:23:23 -07006263 if (ret) {
6264 if (ret == -ENOMEM)
6265 ret = VM_FAULT_OOM;
6266 else /* -ENOSPC, -EIO, etc */
6267 ret = VM_FAULT_SIGBUS;
Chris Mason1832a6d2007-12-21 16:27:21 -05006268 goto out;
Nick Piggin56a76f82009-03-31 15:23:23 -07006269 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006270
Nick Piggin56a76f82009-03-31 15:23:23 -07006271 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006272again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006273 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006274 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006275 page_start = page_offset(page);
6276 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006277
Chris Mason9ebefb182007-06-15 13:50:00 -04006278 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006279 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006280 /* page got truncated out from underneath us */
6281 goto out_unlock;
6282 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006283 wait_on_page_writeback(page);
6284
Josef Bacik2ac55d42010-02-03 19:33:23 +00006285 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
6286 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006287 set_page_extent_mapped(page);
6288
Chris Masoneb84ae02008-07-17 13:53:27 -04006289 /*
6290 * we can't set the delalloc bits if there are pending ordered
6291 * extents. Drop our locks and wait for them to finish
6292 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006293 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6294 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006295 unlock_extent_cached(io_tree, page_start, page_end,
6296 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006297 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006298 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006299 btrfs_put_ordered_extent(ordered);
6300 goto again;
6301 }
6302
Josef Bacikfbf19082009-10-01 17:10:23 -04006303 /*
6304 * XXX - page_mkwrite gets called every time the page is dirtied, even
6305 * if it was already dirty, so for space accounting reasons we need to
6306 * clear any delalloc bits for the range we are fixing to save. There
6307 * is probably a better way to do this, but for now keep consistent with
6308 * prepare_pages in the normal write path.
6309 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006310 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006311 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006312 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006313
Josef Bacik2ac55d42010-02-03 19:33:23 +00006314 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6315 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006316 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006317 unlock_extent_cached(io_tree, page_start, page_end,
6318 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006319 ret = VM_FAULT_SIGBUS;
6320 goto out_unlock;
6321 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006322 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006323
6324 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006325 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006326 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006327 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006328 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006329
Chris Masone6dcd2d2008-07-17 12:53:50 -04006330 if (zero_start != PAGE_CACHE_SIZE) {
6331 kaddr = kmap(page);
6332 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6333 flush_dcache_page(page);
6334 kunmap(page);
6335 }
Chris Mason247e7432008-07-17 12:53:51 -04006336 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006337 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006338 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006339
Chris Mason257c62e2009-10-13 13:21:08 -04006340 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6341 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
6342
Josef Bacik2ac55d42010-02-03 19:33:23 +00006343 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006344
6345out_unlock:
Chris Mason50a9b212009-09-11 12:33:12 -04006346 if (!ret)
6347 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04006348 unlock_page(page);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006349 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason1832a6d2007-12-21 16:27:21 -05006350out:
Chris Mason9ebefb182007-06-15 13:50:00 -04006351 return ret;
6352}
6353
Chris Mason39279cc2007-06-12 06:35:45 -04006354static void btrfs_truncate(struct inode *inode)
6355{
6356 struct btrfs_root *root = BTRFS_I(inode)->root;
6357 int ret;
6358 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04006359 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04006360 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04006361
Yan, Zheng80825102009-11-12 09:35:36 +00006362 if (!S_ISREG(inode->i_mode)) {
6363 WARN_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -04006364 return;
Yan, Zheng80825102009-11-12 09:35:36 +00006365 }
Chris Mason39279cc2007-06-12 06:35:45 -04006366
Josef Bacik5d5e1032009-10-13 16:46:49 -04006367 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
6368 if (ret)
6369 return;
Yan, Zheng80825102009-11-12 09:35:36 +00006370
Chris Mason4a096752008-07-21 10:29:44 -04006371 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006372 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006373
Yan, Zhengd68fc572010-05-16 10:49:58 -04006374 trans = btrfs_start_transaction(root, 0);
6375 BUG_ON(IS_ERR(trans));
Yan, Zheng80825102009-11-12 09:35:36 +00006376 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006377 trans->block_rsv = root->orphan_block_rsv;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006378
6379 /*
6380 * setattr is responsible for setting the ordered_data_close flag,
6381 * but that is only tested during the last file release. That
6382 * could happen well after the next commit, leaving a great big
6383 * window where new writes may get lost if someone chooses to write
6384 * to this file after truncating to zero
6385 *
6386 * The inode doesn't have any dirty data here, and so if we commit
6387 * this is a noop. If someone immediately starts writing to the inode
6388 * it is very likely we'll catch some of their writes in this
6389 * transaction, and the commit will find this file on the ordered
6390 * data list with good things to send down.
6391 *
6392 * This is a best effort solution, there is still a window where
6393 * using truncate to replace the contents of the file will
6394 * end up with a zero length file after a crash.
6395 */
6396 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
6397 btrfs_add_ordered_operation(trans, root, inode);
6398
Yan, Zheng80825102009-11-12 09:35:36 +00006399 while (1) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04006400 if (!trans) {
6401 trans = btrfs_start_transaction(root, 0);
6402 BUG_ON(IS_ERR(trans));
6403 btrfs_set_trans_block_group(trans, inode);
6404 trans->block_rsv = root->orphan_block_rsv;
6405 }
6406
6407 ret = btrfs_block_rsv_check(trans, root,
6408 root->orphan_block_rsv, 0, 5);
6409 if (ret) {
6410 BUG_ON(ret != -EAGAIN);
6411 ret = btrfs_commit_transaction(trans, root);
6412 BUG_ON(ret);
6413 trans = NULL;
6414 continue;
6415 }
6416
Yan, Zheng80825102009-11-12 09:35:36 +00006417 ret = btrfs_truncate_inode_items(trans, root, inode,
6418 inode->i_size,
6419 BTRFS_EXTENT_DATA_KEY);
6420 if (ret != -EAGAIN)
6421 break;
Chris Mason39279cc2007-06-12 06:35:45 -04006422
Yan, Zheng80825102009-11-12 09:35:36 +00006423 ret = btrfs_update_inode(trans, root, inode);
6424 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04006425
Yan, Zheng80825102009-11-12 09:35:36 +00006426 nr = trans->blocks_used;
6427 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006428 trans = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00006429 btrfs_btree_balance_dirty(root, nr);
Yan, Zheng80825102009-11-12 09:35:36 +00006430 }
6431
6432 if (ret == 0 && inode->i_nlink > 0) {
6433 ret = btrfs_orphan_del(trans, inode);
6434 BUG_ON(ret);
6435 }
6436
6437 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04006438 BUG_ON(ret);
6439
Josef Bacik7b128762008-07-24 12:17:14 -04006440 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04006441 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006442 BUG_ON(ret);
Chris Masond3c2fdc2007-09-17 10:58:06 -04006443 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04006444}
6445
Sven Wegener3b963622008-06-09 21:57:42 -04006446/*
Chris Masond352ac62008-09-29 15:18:18 -04006447 * create a new subvolume directory/inode (helper for the ioctl).
6448 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05006449int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Yan, Zheng76dda932009-09-21 16:00:26 -04006450 struct btrfs_root *new_root,
Yan Zhengd2fb3432008-12-11 16:30:39 -05006451 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04006452{
Chris Mason39279cc2007-06-12 06:35:45 -04006453 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04006454 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006455 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006456
Josef Bacikaec74772008-07-24 12:12:38 -04006457 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05006458 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04006459 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04006460 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006461 inode->i_op = &btrfs_dir_inode_operations;
6462 inode->i_fop = &btrfs_dir_file_operations;
6463
Chris Mason39279cc2007-06-12 06:35:45 -04006464 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04006465 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04006466
Yan, Zheng76dda932009-09-21 16:00:26 -04006467 err = btrfs_update_inode(trans, new_root, inode);
6468 BUG_ON(err);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006469
Yan, Zheng76dda932009-09-21 16:00:26 -04006470 iput(inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006471 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006472}
6473
Chris Masond352ac62008-09-29 15:18:18 -04006474/* helper function for file defrag and space balancing. This
6475 * forces readahead on a given range of bytes in an inode
6476 */
Chris Masonedbd8d42007-12-21 16:27:24 -05006477unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04006478 struct file_ra_state *ra, struct file *file,
6479 pgoff_t offset, pgoff_t last_index)
6480{
Chris Mason8e7bf942008-04-28 09:02:36 -04006481 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04006482
Chris Mason86479a02007-09-10 19:58:16 -04006483 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
6484 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04006485}
6486
Chris Mason39279cc2007-06-12 06:35:45 -04006487struct inode *btrfs_alloc_inode(struct super_block *sb)
6488{
6489 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006490 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006491
6492 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
6493 if (!ei)
6494 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006495
6496 ei->root = NULL;
6497 ei->space_info = NULL;
6498 ei->generation = 0;
6499 ei->sequence = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04006500 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04006501 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04006502 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006503 ei->delalloc_bytes = 0;
6504 ei->reserved_bytes = 0;
6505 ei->disk_i_size = 0;
6506 ei->flags = 0;
6507 ei->index_cnt = (u64)-1;
6508 ei->last_unlink_trans = 0;
6509
Josef Bacik32c00af2009-10-08 13:34:05 -04006510 spin_lock_init(&ei->accounting_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006511 atomic_set(&ei->outstanding_extents, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006512 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006513
6514 ei->ordered_data_close = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04006515 ei->orphan_meta_reserved = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006516 ei->dummy_inode = 0;
Li Zefan261507a02010-12-17 14:21:50 +08006517 ei->force_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006518
6519 inode = &ei->vfs_inode;
6520 extent_map_tree_init(&ei->extent_tree, GFP_NOFS);
6521 extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS);
6522 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS);
6523 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006524 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04006525 INIT_LIST_HEAD(&ei->i_orphan);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006526 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006527 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006528 RB_CLEAR_NODE(&ei->rb_node);
6529
6530 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006531}
6532
6533void btrfs_destroy_inode(struct inode *inode)
6534{
Chris Masone6dcd2d2008-07-17 12:53:50 -04006535 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006536 struct btrfs_root *root = BTRFS_I(inode)->root;
6537
Chris Mason39279cc2007-06-12 06:35:45 -04006538 WARN_ON(!list_empty(&inode->i_dentry));
6539 WARN_ON(inode->i_data.nrpages);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006540 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents));
6541 WARN_ON(BTRFS_I(inode)->reserved_extents);
Chris Mason39279cc2007-06-12 06:35:45 -04006542
Chris Mason5a3f23d2009-03-31 13:27:11 -04006543 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05006544 * This can happen where we create an inode, but somebody else also
6545 * created the same inode and we need to destroy the one we already
6546 * created.
6547 */
6548 if (!root)
6549 goto free;
6550
6551 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04006552 * Make sure we're properly removed from the ordered operation
6553 * lists.
6554 */
6555 smp_mb();
6556 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
6557 spin_lock(&root->fs_info->ordered_extent_lock);
6558 list_del_init(&BTRFS_I(inode)->ordered_operations);
6559 spin_unlock(&root->fs_info->ordered_extent_lock);
6560 }
6561
Josef Bacik0af3d002010-06-21 14:48:16 -04006562 if (root == root->fs_info->tree_root) {
6563 struct btrfs_block_group_cache *block_group;
6564
6565 block_group = btrfs_lookup_block_group(root->fs_info,
6566 BTRFS_I(inode)->block_group);
6567 if (block_group && block_group->inode == inode) {
6568 spin_lock(&block_group->lock);
6569 block_group->inode = NULL;
6570 spin_unlock(&block_group->lock);
6571 btrfs_put_block_group(block_group);
6572 } else if (block_group) {
6573 btrfs_put_block_group(block_group);
6574 }
6575 }
6576
Yan, Zhengd68fc572010-05-16 10:49:58 -04006577 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006578 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yan, Zheng80825102009-11-12 09:35:36 +00006579 printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n",
6580 inode->i_ino);
6581 list_del_init(&BTRFS_I(inode)->i_orphan);
Josef Bacik7b128762008-07-24 12:17:14 -04006582 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04006583 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006584
Chris Masond3977122009-01-05 21:25:51 -05006585 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006586 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
6587 if (!ordered)
6588 break;
6589 else {
Chris Masond3977122009-01-05 21:25:51 -05006590 printk(KERN_ERR "btrfs found ordered "
6591 "extent %llu %llu on inode cleanup\n",
6592 (unsigned long long)ordered->file_offset,
6593 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006594 btrfs_remove_ordered_extent(inode, ordered);
6595 btrfs_put_ordered_extent(ordered);
6596 btrfs_put_ordered_extent(ordered);
6597 }
6598 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006599 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006600 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05006601free:
Chris Mason39279cc2007-06-12 06:35:45 -04006602 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
6603}
6604
Al Viro45321ac2010-06-07 13:43:19 -04006605int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04006606{
6607 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04006608
Josef Bacik0af3d002010-06-21 14:48:16 -04006609 if (btrfs_root_refs(&root->root_item) == 0 &&
6610 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04006611 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04006612 else
Al Viro45321ac2010-06-07 13:43:19 -04006613 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04006614}
6615
Sven Wegener0ee0fda2008-07-30 16:54:26 -04006616static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04006617{
6618 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
6619
6620 inode_init_once(&ei->vfs_inode);
6621}
6622
6623void btrfs_destroy_cachep(void)
6624{
6625 if (btrfs_inode_cachep)
6626 kmem_cache_destroy(btrfs_inode_cachep);
6627 if (btrfs_trans_handle_cachep)
6628 kmem_cache_destroy(btrfs_trans_handle_cachep);
6629 if (btrfs_transaction_cachep)
6630 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04006631 if (btrfs_path_cachep)
6632 kmem_cache_destroy(btrfs_path_cachep);
6633}
6634
6635int btrfs_init_cachep(void)
6636{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006637 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
6638 sizeof(struct btrfs_inode), 0,
6639 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04006640 if (!btrfs_inode_cachep)
6641 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006642
6643 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
6644 sizeof(struct btrfs_trans_handle), 0,
6645 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006646 if (!btrfs_trans_handle_cachep)
6647 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006648
6649 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
6650 sizeof(struct btrfs_transaction), 0,
6651 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006652 if (!btrfs_transaction_cachep)
6653 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006654
6655 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
6656 sizeof(struct btrfs_path), 0,
6657 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006658 if (!btrfs_path_cachep)
6659 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006660
Chris Mason39279cc2007-06-12 06:35:45 -04006661 return 0;
6662fail:
6663 btrfs_destroy_cachep();
6664 return -ENOMEM;
6665}
6666
6667static int btrfs_getattr(struct vfsmount *mnt,
6668 struct dentry *dentry, struct kstat *stat)
6669{
6670 struct inode *inode = dentry->d_inode;
6671 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05006672 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05006673 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006674 stat->blocks = (inode_get_bytes(inode) +
6675 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04006676 return 0;
6677}
6678
Chris Masond3977122009-01-05 21:25:51 -05006679static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
6680 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04006681{
6682 struct btrfs_trans_handle *trans;
6683 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006684 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04006685 struct inode *new_inode = new_dentry->d_inode;
6686 struct inode *old_inode = old_dentry->d_inode;
6687 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006688 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006689 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04006690 int ret;
6691
Yan, Zhengf679a842009-09-24 09:17:31 -04006692 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
6693 return -EPERM;
6694
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006695 /* we only allow rename subvolume link between subvolumes */
6696 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05006697 return -EXDEV;
6698
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006699 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
6700 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
6701 return -ENOTEMPTY;
6702
Chris Mason39279cc2007-06-12 06:35:45 -04006703 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006704 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04006705 return -ENOTEMPTY;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006706 /*
6707 * we're using rename to replace one file with another.
6708 * and the replacement file is large. Start IO on it now so
6709 * we don't add too much work to the end of the transaction
6710 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04006711 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04006712 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
6713 filemap_flush(old_inode->i_mapping);
6714
Yan, Zheng76dda932009-09-21 16:00:26 -04006715 /* close the racy window with snapshot create/destroy ioctl */
6716 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
6717 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006718 /*
6719 * We want to reserve the absolute worst case amount of items. So if
6720 * both inodes are subvols and we need to unlink them then that would
6721 * require 4 item modifications, but if they are both normal inodes it
6722 * would require 5 item modifications, so we'll assume their normal
6723 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
6724 * should cover the worst case number of items we'll modify.
6725 */
6726 trans = btrfs_start_transaction(root, 20);
6727 if (IS_ERR(trans))
6728 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04006729
Yan, Zhenga5719522009-09-24 09:17:31 -04006730 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04006731
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006732 if (dest != root)
6733 btrfs_record_root_in_trans(trans, dest);
6734
Yan, Zhenga5719522009-09-24 09:17:31 -04006735 ret = btrfs_set_inode_index(new_dir, &index);
6736 if (ret)
6737 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006738
Yan, Zhenga5719522009-09-24 09:17:31 -04006739 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006740 /* force full log commit if subvolume involved. */
6741 root->fs_info->last_trans_log_full_commit = trans->transid;
6742 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04006743 ret = btrfs_insert_inode_ref(trans, dest,
6744 new_dentry->d_name.name,
6745 new_dentry->d_name.len,
6746 old_inode->i_ino,
6747 new_dir->i_ino, index);
6748 if (ret)
6749 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006750 /*
6751 * this is an ugly little race, but the rename is required
6752 * to make sure that if we crash, the inode is either at the
6753 * old name or the new one. pinning the log transaction lets
6754 * us make sure we don't allow a log commit to come in after
6755 * we unlink the name but before we add the new name back in.
6756 */
6757 btrfs_pin_log_trans(root);
6758 }
Chris Mason12fcfd22009-03-24 10:24:20 -04006759 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04006760 * make sure the inode gets flushed if it is replacing
6761 * something.
6762 */
6763 if (new_inode && new_inode->i_size &&
6764 old_inode && S_ISREG(old_inode->i_mode)) {
6765 btrfs_add_ordered_operation(trans, root, old_inode);
6766 }
6767
Chris Mason39279cc2007-06-12 06:35:45 -04006768 old_dir->i_ctime = old_dir->i_mtime = ctime;
6769 new_dir->i_ctime = new_dir->i_mtime = ctime;
6770 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04006771
Chris Mason12fcfd22009-03-24 10:24:20 -04006772 if (old_dentry->d_parent != new_dentry->d_parent)
6773 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
6774
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006775 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
6776 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
6777 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
6778 old_dentry->d_name.name,
6779 old_dentry->d_name.len);
6780 } else {
6781 btrfs_inc_nlink(old_dentry->d_inode);
6782 ret = btrfs_unlink_inode(trans, root, old_dir,
6783 old_dentry->d_inode,
6784 old_dentry->d_name.name,
6785 old_dentry->d_name.len);
6786 }
6787 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006788
6789 if (new_inode) {
6790 new_inode->i_ctime = CURRENT_TIME;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006791 if (unlikely(new_inode->i_ino ==
6792 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
6793 root_objectid = BTRFS_I(new_inode)->location.objectid;
6794 ret = btrfs_unlink_subvol(trans, dest, new_dir,
6795 root_objectid,
6796 new_dentry->d_name.name,
6797 new_dentry->d_name.len);
6798 BUG_ON(new_inode->i_nlink == 0);
6799 } else {
6800 ret = btrfs_unlink_inode(trans, dest, new_dir,
6801 new_dentry->d_inode,
6802 new_dentry->d_name.name,
6803 new_dentry->d_name.len);
6804 }
6805 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04006806 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04006807 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006808 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04006809 }
Chris Mason39279cc2007-06-12 06:35:45 -04006810 }
Josef Bacikaec74772008-07-24 12:12:38 -04006811
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006812 ret = btrfs_add_link(trans, new_dir, old_inode,
6813 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04006814 new_dentry->d_name.len, 0, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006815 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006816
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006817 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik6a912212010-11-20 09:48:00 +00006818 struct dentry *parent = dget_parent(new_dentry);
6819 btrfs_log_new_name(trans, old_inode, old_dir, parent);
6820 dput(parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006821 btrfs_end_log_trans(root);
6822 }
Chris Mason39279cc2007-06-12 06:35:45 -04006823out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04006824 btrfs_end_transaction_throttle(trans, root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006825
Yan, Zheng76dda932009-09-21 16:00:26 -04006826 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
6827 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006828
Chris Mason39279cc2007-06-12 06:35:45 -04006829 return ret;
6830}
6831
Chris Masond352ac62008-09-29 15:18:18 -04006832/*
6833 * some fairly slow code that needs optimization. This walks the list
6834 * of all the inodes with pending delalloc and forces them to disk.
6835 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00006836int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04006837{
6838 struct list_head *head = &root->fs_info->delalloc_inodes;
6839 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006840 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04006841
Yan Zhengc146afa2008-11-12 14:34:12 -05006842 if (root->fs_info->sb->s_flags & MS_RDONLY)
6843 return -EROFS;
6844
Chris Mason75eff682008-12-15 15:54:40 -05006845 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05006846 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04006847 binode = list_entry(head->next, struct btrfs_inode,
6848 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006849 inode = igrab(&binode->vfs_inode);
6850 if (!inode)
6851 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05006852 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006853 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04006854 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00006855 if (delay_iput)
6856 btrfs_add_delayed_iput(inode);
6857 else
6858 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006859 }
6860 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05006861 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04006862 }
Chris Mason75eff682008-12-15 15:54:40 -05006863 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04006864
6865 /* the filemap_flush will queue IO into the worker threads, but
6866 * we have to make sure the IO is actually started and that
6867 * ordered extents get created before we return
6868 */
6869 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05006870 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05006871 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04006872 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05006873 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
6874 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04006875 }
6876 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04006877 return 0;
6878}
6879
Josef Bacik0019f102010-10-15 15:18:40 -04006880int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput,
6881 int sync)
Yan, Zheng5da9d012010-05-16 10:46:25 -04006882{
6883 struct btrfs_inode *binode;
6884 struct inode *inode = NULL;
6885
6886 spin_lock(&root->fs_info->delalloc_lock);
6887 while (!list_empty(&root->fs_info->delalloc_inodes)) {
6888 binode = list_entry(root->fs_info->delalloc_inodes.next,
6889 struct btrfs_inode, delalloc_inodes);
6890 inode = igrab(&binode->vfs_inode);
6891 if (inode) {
6892 list_move_tail(&binode->delalloc_inodes,
6893 &root->fs_info->delalloc_inodes);
6894 break;
6895 }
6896
6897 list_del_init(&binode->delalloc_inodes);
6898 cond_resched_lock(&root->fs_info->delalloc_lock);
6899 }
6900 spin_unlock(&root->fs_info->delalloc_lock);
6901
6902 if (inode) {
Josef Bacik0019f102010-10-15 15:18:40 -04006903 if (sync) {
6904 filemap_write_and_wait(inode->i_mapping);
6905 /*
6906 * We have to do this because compression doesn't
6907 * actually set PG_writeback until it submits the pages
6908 * for IO, which happens in an async thread, so we could
6909 * race and not actually wait for any writeback pages
6910 * because they've not been submitted yet. Technically
6911 * this could still be the case for the ordered stuff
6912 * since the async thread may not have started to do its
6913 * work yet. If this becomes the case then we need to
6914 * figure out a way to make sure that in writepage we
6915 * wait for any async pages to be submitted before
6916 * returning so that fdatawait does what its supposed to
6917 * do.
6918 */
6919 btrfs_wait_ordered_range(inode, 0, (u64)-1);
6920 } else {
6921 filemap_flush(inode->i_mapping);
6922 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04006923 if (delay_iput)
6924 btrfs_add_delayed_iput(inode);
6925 else
6926 iput(inode);
6927 return 1;
6928 }
6929 return 0;
6930}
6931
Chris Mason39279cc2007-06-12 06:35:45 -04006932static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
6933 const char *symname)
6934{
6935 struct btrfs_trans_handle *trans;
6936 struct btrfs_root *root = BTRFS_I(dir)->root;
6937 struct btrfs_path *path;
6938 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05006939 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006940 int err;
6941 int drop_inode = 0;
6942 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006943 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04006944 int name_len;
6945 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04006946 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006947 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04006948 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05006949 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006950
6951 name_len = strlen(symname) + 1;
6952 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
6953 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05006954
Yan, Zhenga22285a2010-05-16 10:48:46 -04006955 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
6956 if (err)
6957 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006958 /*
6959 * 2 items for inode item and ref
6960 * 2 items for dir items
6961 * 1 item for xattr if selinux is on
6962 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006963 trans = btrfs_start_transaction(root, 5);
6964 if (IS_ERR(trans))
6965 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006966
Chris Mason39279cc2007-06-12 06:35:45 -04006967 btrfs_set_trans_block_group(trans, dir);
6968
Josef Bacikaec74772008-07-24 12:12:38 -04006969 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Josef Bacika1b075d2010-11-19 20:36:11 +00006970 dentry->d_name.len, dir->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04006971 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
6972 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006973 err = PTR_ERR(inode);
6974 if (IS_ERR(inode))
6975 goto out_unlock;
6976
Yan, Zhengf34f57a2009-11-12 09:35:27 +00006977 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04006978 if (err) {
6979 drop_inode = 1;
6980 goto out_unlock;
6981 }
6982
Chris Mason39279cc2007-06-12 06:35:45 -04006983 btrfs_set_trans_block_group(trans, inode);
Josef Bacika1b075d2010-11-19 20:36:11 +00006984 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006985 if (err)
6986 drop_inode = 1;
6987 else {
6988 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04006989 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04006990 inode->i_fop = &btrfs_file_operations;
6991 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05006992 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04006993 }
Chris Mason39279cc2007-06-12 06:35:45 -04006994 btrfs_update_inode_block_group(trans, inode);
6995 btrfs_update_inode_block_group(trans, dir);
6996 if (drop_inode)
6997 goto out_unlock;
6998
6999 path = btrfs_alloc_path();
7000 BUG_ON(!path);
7001 key.objectid = inode->i_ino;
7002 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04007003 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7004 datasize = btrfs_file_extent_calc_inline_size(name_len);
7005 err = btrfs_insert_empty_item(trans, root, path, &key,
7006 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007007 if (err) {
7008 drop_inode = 1;
7009 goto out_unlock;
7010 }
Chris Mason5f39d392007-10-15 16:14:19 -04007011 leaf = path->nodes[0];
7012 ei = btrfs_item_ptr(leaf, path->slots[0],
7013 struct btrfs_file_extent_item);
7014 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7015 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04007016 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04007017 btrfs_set_file_extent_encryption(leaf, ei, 0);
7018 btrfs_set_file_extent_compression(leaf, ei, 0);
7019 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7020 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7021
Chris Mason39279cc2007-06-12 06:35:45 -04007022 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04007023 write_extent_buffer(leaf, symname, ptr, name_len);
7024 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04007025 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04007026
Chris Mason39279cc2007-06-12 06:35:45 -04007027 inode->i_op = &btrfs_symlink_inode_operations;
7028 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04007029 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04007030 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04007031 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04007032 err = btrfs_update_inode(trans, root, inode);
7033 if (err)
7034 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04007035
7036out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04007037 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04007038 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04007039 if (drop_inode) {
7040 inode_dec_link_count(inode);
7041 iput(inode);
7042 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04007043 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04007044 return err;
7045}
Chris Mason16432982008-04-10 10:23:21 -04007046
Josef Bacik0af3d002010-06-21 14:48:16 -04007047static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7048 u64 start, u64 num_bytes, u64 min_size,
7049 loff_t actual_len, u64 *alloc_hint,
7050 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04007051{
Yan Zhengd899e052008-10-30 14:25:28 -04007052 struct btrfs_root *root = BTRFS_I(inode)->root;
7053 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04007054 u64 cur_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00007055 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04007056 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04007057 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04007058
Josef Bacik0af3d002010-06-21 14:48:16 -04007059 if (trans)
7060 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04007061 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007062 if (own_trans) {
7063 trans = btrfs_start_transaction(root, 3);
7064 if (IS_ERR(trans)) {
7065 ret = PTR_ERR(trans);
7066 break;
7067 }
Yan Zhengd899e052008-10-30 14:25:28 -04007068 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00007069
Yan, Zhengefa56462010-05-16 10:49:59 -04007070 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
7071 0, *alloc_hint, (u64)-1, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007072 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04007073 if (own_trans)
7074 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04007075 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007076 }
7077
Yan Zhengd899e052008-10-30 14:25:28 -04007078 ret = insert_reserved_file_extent(trans, inode,
7079 cur_offset, ins.objectid,
7080 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00007081 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04007082 BTRFS_FILE_EXTENT_PREALLOC);
7083 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -04007084 btrfs_drop_extent_cache(inode, cur_offset,
7085 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007086
Yan Zhengd899e052008-10-30 14:25:28 -04007087 num_bytes -= ins.offset;
7088 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04007089 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007090
Yan Zhengd899e052008-10-30 14:25:28 -04007091 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007092 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04007093 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04007094 (actual_len > inode->i_size) &&
7095 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007096 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00007097 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00007098 else
Josef Bacik55a61d12010-11-22 18:50:32 +00007099 i_size = cur_offset;
7100 i_size_write(inode, i_size);
7101 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007102 }
7103
Yan Zhengd899e052008-10-30 14:25:28 -04007104 ret = btrfs_update_inode(trans, root, inode);
7105 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04007106
Josef Bacik0af3d002010-06-21 14:48:16 -04007107 if (own_trans)
7108 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00007109 }
Yan Zhengd899e052008-10-30 14:25:28 -04007110 return ret;
7111}
7112
Josef Bacik0af3d002010-06-21 14:48:16 -04007113int btrfs_prealloc_file_range(struct inode *inode, int mode,
7114 u64 start, u64 num_bytes, u64 min_size,
7115 loff_t actual_len, u64 *alloc_hint)
7116{
7117 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7118 min_size, actual_len, alloc_hint,
7119 NULL);
7120}
7121
7122int btrfs_prealloc_file_range_trans(struct inode *inode,
7123 struct btrfs_trans_handle *trans, int mode,
7124 u64 start, u64 num_bytes, u64 min_size,
7125 loff_t actual_len, u64 *alloc_hint)
7126{
7127 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7128 min_size, actual_len, alloc_hint, trans);
7129}
7130
Yan Zhengd899e052008-10-30 14:25:28 -04007131static long btrfs_fallocate(struct inode *inode, int mode,
7132 loff_t offset, loff_t len)
7133{
Josef Bacik2ac55d42010-02-03 19:33:23 +00007134 struct extent_state *cached_state = NULL;
Yan Zhengd899e052008-10-30 14:25:28 -04007135 u64 cur_offset;
7136 u64 last_byte;
7137 u64 alloc_start;
7138 u64 alloc_end;
7139 u64 alloc_hint = 0;
Chris Masone980b502009-04-24 14:39:24 -04007140 u64 locked_end;
Yan Zhengd899e052008-10-30 14:25:28 -04007141 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
7142 struct extent_map *em;
7143 int ret;
7144
7145 alloc_start = offset & ~mask;
7146 alloc_end = (offset + len + mask) & ~mask;
7147
Chris Mason546888d2009-04-21 11:53:38 -04007148 /*
7149 * wait for ordered IO before we have any locks. We'll loop again
7150 * below with the locks held.
7151 */
7152 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
7153
Yan Zhengd899e052008-10-30 14:25:28 -04007154 mutex_lock(&inode->i_mutex);
Josef Bacik0ed42a62010-11-22 18:55:39 +00007155 ret = inode_newsize_ok(inode, alloc_end);
7156 if (ret)
7157 goto out;
7158
Yan Zhengd899e052008-10-30 14:25:28 -04007159 if (alloc_start > inode->i_size) {
7160 ret = btrfs_cont_expand(inode, alloc_start);
7161 if (ret)
7162 goto out;
7163 }
7164
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007165 ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
Josef Bacika970b0a2009-06-27 21:07:34 -04007166 if (ret)
7167 goto out;
7168
Chris Masone980b502009-04-24 14:39:24 -04007169 locked_end = alloc_end - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04007170 while (1) {
7171 struct btrfs_ordered_extent *ordered;
Chris Mason546888d2009-04-21 11:53:38 -04007172
Chris Mason546888d2009-04-21 11:53:38 -04007173 /* the extent lock is ordered inside the running
7174 * transaction
7175 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00007176 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
7177 locked_end, 0, &cached_state, GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04007178 ordered = btrfs_lookup_first_ordered_extent(inode,
7179 alloc_end - 1);
7180 if (ordered &&
7181 ordered->file_offset + ordered->len > alloc_start &&
7182 ordered->file_offset < alloc_end) {
7183 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00007184 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7185 alloc_start, locked_end,
7186 &cached_state, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04007187 /*
7188 * we can't wait on the range with the transaction
7189 * running or with the extent lock held
7190 */
Yan Zhengd899e052008-10-30 14:25:28 -04007191 btrfs_wait_ordered_range(inode, alloc_start,
7192 alloc_end - alloc_start);
7193 } else {
7194 if (ordered)
7195 btrfs_put_ordered_extent(ordered);
7196 break;
7197 }
7198 }
7199
7200 cur_offset = alloc_start;
7201 while (1) {
7202 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
7203 alloc_end - cur_offset, 0);
7204 BUG_ON(IS_ERR(em) || !em);
7205 last_byte = min(extent_map_end(em), alloc_end);
7206 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng5a303d52009-11-12 09:34:52 +00007207 if (em->block_start == EXTENT_MAP_HOLE ||
7208 (cur_offset >= inode->i_size &&
7209 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
Josef Bacik83609772010-06-07 18:26:37 +00007210 ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
Yan, Zhengefa56462010-05-16 10:49:59 -04007211 last_byte - cur_offset,
7212 1 << inode->i_blkbits,
7213 offset + len,
7214 &alloc_hint);
Yan Zhengd899e052008-10-30 14:25:28 -04007215 if (ret < 0) {
7216 free_extent_map(em);
7217 break;
7218 }
7219 }
Yan Zhengd899e052008-10-30 14:25:28 -04007220 free_extent_map(em);
7221
7222 cur_offset = last_byte;
7223 if (cur_offset >= alloc_end) {
7224 ret = 0;
7225 break;
7226 }
7227 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00007228 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
7229 &cached_state, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04007230
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007231 btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
Yan Zhengd899e052008-10-30 14:25:28 -04007232out:
7233 mutex_unlock(&inode->i_mutex);
7234 return ret;
7235}
7236
Chris Masone6dcd2d2008-07-17 12:53:50 -04007237static int btrfs_set_page_dirty(struct page *page)
7238{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007239 return __set_page_dirty_nobuffers(page);
7240}
7241
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007242static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05007243{
Li Zefanb83cc962010-12-20 16:04:08 +08007244 struct btrfs_root *root = BTRFS_I(inode)->root;
7245
7246 if (btrfs_root_readonly(root) && (mask & MAY_WRITE))
7247 return -EROFS;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007248 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
Yanfdebe2b2008-01-14 13:26:08 -05007249 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04007250 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05007251}
Chris Mason39279cc2007-06-12 06:35:45 -04007252
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007253static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007254 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007255 .lookup = btrfs_lookup,
7256 .create = btrfs_create,
7257 .unlink = btrfs_unlink,
7258 .link = btrfs_link,
7259 .mkdir = btrfs_mkdir,
7260 .rmdir = btrfs_rmdir,
7261 .rename = btrfs_rename,
7262 .symlink = btrfs_symlink,
7263 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007264 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007265 .setxattr = btrfs_setxattr,
7266 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007267 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007268 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007269 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04007270};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007271static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007272 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007273 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04007274};
Yan, Zheng76dda932009-09-21 16:00:26 -04007275
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007276static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007277 .llseek = generic_file_llseek,
7278 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007279 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007280 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007281#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007282 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007283#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04007284 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04007285 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04007286};
7287
Chris Masond1310b22008-01-24 16:13:08 -05007288static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04007289 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05007290 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04007291 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007292 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007293 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04007294 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04007295 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05007296 .set_bit_hook = btrfs_set_bit_hook,
7297 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007298 .merge_extent_hook = btrfs_merge_extent_hook,
7299 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007300};
7301
Chris Mason35054392009-01-21 13:11:13 -05007302/*
7303 * btrfs doesn't support the bmap operation because swapfiles
7304 * use bmap to make a mapping of extents in the file. They assume
7305 * these extents won't change over the life of the file and they
7306 * use the bmap result to do IO directly to the drive.
7307 *
7308 * the btrfs bmap call would return logical addresses that aren't
7309 * suitable for IO and they also will change frequently as COW
7310 * operations happen. So, swapfile + btrfs == corruption.
7311 *
7312 * For now we're avoiding this by dropping bmap.
7313 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007314static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007315 .readpage = btrfs_readpage,
7316 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04007317 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05007318 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04007319 .sync_page = block_sync_page,
Chris Mason16432982008-04-10 10:23:21 -04007320 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04007321 .invalidatepage = btrfs_invalidatepage,
7322 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007323 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02007324 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04007325};
7326
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007327static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007328 .readpage = btrfs_readpage,
7329 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04007330 .invalidatepage = btrfs_invalidatepage,
7331 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04007332};
7333
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007334static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007335 .truncate = btrfs_truncate,
7336 .getattr = btrfs_getattr,
7337 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007338 .setxattr = btrfs_setxattr,
7339 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007340 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007341 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007342 .permission = btrfs_permission,
Yan Zhengd899e052008-10-30 14:25:28 -04007343 .fallocate = btrfs_fallocate,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007344 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04007345};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007346static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04007347 .getattr = btrfs_getattr,
7348 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007349 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007350 .setxattr = btrfs_setxattr,
7351 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04007352 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007353 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007354};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007355static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007356 .readlink = generic_readlink,
7357 .follow_link = page_follow_link_light,
7358 .put_link = page_put_link,
Li Zefanf2095612010-11-19 02:05:24 +00007359 .getattr = btrfs_getattr,
Yanfdebe2b2008-01-14 13:26:08 -05007360 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05007361 .setxattr = btrfs_setxattr,
7362 .getxattr = btrfs_getxattr,
7363 .listxattr = btrfs_listxattr,
7364 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007365};
Yan, Zheng76dda932009-09-21 16:00:26 -04007366
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04007367const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04007368 .d_delete = btrfs_dentry_delete,
7369};