blob: f2fb974ed8f035385f6b7b953338607f7bbf1db2 [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;
125 int use_compress = 0;
126
127 if (compressed_size && compressed_pages) {
128 use_compress = 1;
129 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
162 if (use_compress) {
163 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,
179 BTRFS_COMPRESS_ZLIB);
180 } 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;
266 struct list_head list;
267};
268
269struct async_cow {
270 struct inode *inode;
271 struct btrfs_root *root;
272 struct page *locked_page;
273 u64 start;
274 u64 end;
275 struct list_head extents;
276 struct btrfs_work work;
277};
278
279static noinline int add_async_extent(struct async_cow *cow,
280 u64 start, u64 ram_size,
281 u64 compressed_size,
282 struct page **pages,
283 unsigned long nr_pages)
284{
285 struct async_extent *async_extent;
286
287 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
288 async_extent->start = start;
289 async_extent->ram_size = ram_size;
290 async_extent->compressed_size = compressed_size;
291 async_extent->pages = pages;
292 async_extent->nr_pages = nr_pages;
293 list_add_tail(&async_extent->list, &cow->extents);
294 return 0;
295}
296
Chris Masonc8b97812008-10-29 14:49:59 -0400297/*
Chris Mason771ed682008-11-06 22:02:51 -0500298 * we create compressed extents in two phases. The first
299 * phase compresses a range of pages that have already been
300 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400301 *
Chris Mason771ed682008-11-06 22:02:51 -0500302 * This is done inside an ordered work queue, and the compression
303 * is spread across many cpus. The actual IO submission is step
304 * two, and the ordered work queue takes care of making sure that
305 * happens in the same order things were put onto the queue by
306 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400307 *
Chris Mason771ed682008-11-06 22:02:51 -0500308 * If this code finds it can't get good compression, it puts an
309 * entry onto the work queue to write the uncompressed bytes. This
310 * makes sure that both compressed inodes and uncompressed inodes
311 * are written in the same order that pdflush sent them down.
Chris Masond352ac62008-09-29 15:18:18 -0400312 */
Chris Mason771ed682008-11-06 22:02:51 -0500313static noinline int compress_file_range(struct inode *inode,
314 struct page *locked_page,
315 u64 start, u64 end,
316 struct async_cow *async_cow,
317 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400318{
319 struct btrfs_root *root = BTRFS_I(inode)->root;
320 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400321 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -0400322 u64 orig_start;
323 u64 disk_num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400324 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400325 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500326 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400327 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400328 struct page **pages = NULL;
329 unsigned long nr_pages;
330 unsigned long nr_pages_ret = 0;
331 unsigned long total_compressed = 0;
332 unsigned long total_in = 0;
333 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500334 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400335 int i;
336 int will_compress;
Chris Masonb888db22007-08-27 16:49:44 -0400337
Chris Masonc8b97812008-10-29 14:49:59 -0400338 orig_start = start;
Chris Masonbe20aa92007-12-17 20:14:01 -0500339
Chris Mason42dc7ba2008-12-15 11:44:56 -0500340 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400341again:
342 will_compress = 0;
343 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
344 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
345
Chris Masonf03d9301f2009-02-04 09:31:06 -0500346 /*
347 * we don't want to send crud past the end of i_size through
348 * compression, that's just a waste of CPU time. So, if the
349 * end of the file is before the start of our current
350 * requested range of bytes, we bail out to the uncompressed
351 * cleanup code that can deal with all of this.
352 *
353 * It isn't really the fastest way to fix things, but this is a
354 * very uncommon corner.
355 */
356 if (actual_end <= start)
357 goto cleanup_and_bail_uncompressed;
358
Chris Masonc8b97812008-10-29 14:49:59 -0400359 total_compressed = actual_end - start;
360
361 /* we want to make sure that amount of ram required to uncompress
362 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500363 * of a compressed extent to 128k. This is a crucial number
364 * because it also controls how easily we can spread reads across
365 * cpus for decompression.
366 *
367 * We also want to make sure the amount of IO required to do
368 * a random read is reasonably small, so we limit the size of
369 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400370 */
371 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400372 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500373 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400374 disk_num_bytes = num_bytes;
375 total_in = 0;
376 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400377
Chris Mason771ed682008-11-06 22:02:51 -0500378 /*
379 * we do compression for mount -o compress and when the
380 * inode has not been flagged as nocompress. This flag can
381 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400382 */
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200383 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
Chris Mason1e701a32010-03-11 09:42:04 -0500384 (btrfs_test_opt(root, COMPRESS) ||
385 (BTRFS_I(inode)->force_compress))) {
Chris Masonc8b97812008-10-29 14:49:59 -0400386 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400387 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Chris Mason179e29e2007-11-01 11:28:41 -0400388
Chris Masonc8b97812008-10-29 14:49:59 -0400389 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
390 total_compressed, pages,
391 nr_pages, &nr_pages_ret,
392 &total_in,
393 &total_compressed,
394 max_compressed);
395
396 if (!ret) {
397 unsigned long offset = total_compressed &
398 (PAGE_CACHE_SIZE - 1);
399 struct page *page = pages[nr_pages_ret - 1];
400 char *kaddr;
401
402 /* zero the tail end of the last page, we might be
403 * sending it down to disk
404 */
405 if (offset) {
406 kaddr = kmap_atomic(page, KM_USER0);
407 memset(kaddr + offset, 0,
408 PAGE_CACHE_SIZE - offset);
409 kunmap_atomic(kaddr, KM_USER0);
410 }
411 will_compress = 1;
412 }
413 }
414 if (start == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500415 trans = btrfs_join_transaction(root, 1);
416 BUG_ON(!trans);
417 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400418 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500419
Chris Masonc8b97812008-10-29 14:49:59 -0400420 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500421 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400422 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500423 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400424 */
425 ret = cow_file_range_inline(trans, root, inode,
426 start, end, 0, NULL);
427 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500428 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400429 ret = cow_file_range_inline(trans, root, inode,
430 start, end,
431 total_compressed, pages);
432 }
433 if (ret == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500434 /*
435 * inline extent creation worked, we don't need
436 * to create any more async work items. Unlock
437 * and free up our temp pages.
438 */
Chris Masonc8b97812008-10-29 14:49:59 -0400439 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400440 &BTRFS_I(inode)->io_tree,
441 start, end, NULL,
442 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
Chris Masona3429ab2009-10-08 12:30:20 -0400443 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400444 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000445
446 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400447 goto free_pages_out;
448 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000449 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400450 }
451
452 if (will_compress) {
453 /*
454 * we aren't doing an inline extent round the compressed size
455 * up to a block size boundary so the allocator does sane
456 * things
457 */
458 total_compressed = (total_compressed + blocksize - 1) &
459 ~(blocksize - 1);
460
461 /*
462 * one last check to make sure the compression is really a
463 * win, compare the page count read with the blocks on disk
464 */
465 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
466 ~(PAGE_CACHE_SIZE - 1);
467 if (total_compressed >= total_in) {
468 will_compress = 0;
469 } else {
470 disk_num_bytes = total_compressed;
471 num_bytes = total_in;
472 }
473 }
474 if (!will_compress && pages) {
475 /*
476 * the compression code ran but failed to make things smaller,
477 * free any pages it allocated and our page pointer array
478 */
479 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400480 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400481 page_cache_release(pages[i]);
482 }
483 kfree(pages);
484 pages = NULL;
485 total_compressed = 0;
486 nr_pages_ret = 0;
487
488 /* flag the file so we don't compress in the future */
Chris Mason1e701a32010-03-11 09:42:04 -0500489 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
490 !(BTRFS_I(inode)->force_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500491 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500492 }
Chris Masonc8b97812008-10-29 14:49:59 -0400493 }
Chris Mason771ed682008-11-06 22:02:51 -0500494 if (will_compress) {
495 *num_added += 1;
496
497 /* the async work queues will take care of doing actual
498 * allocation on disk for these compressed pages,
499 * and will submit them to the elevator.
500 */
501 add_async_extent(async_cow, start, num_bytes,
502 total_compressed, pages, nr_pages_ret);
503
Chris Mason42dc7ba2008-12-15 11:44:56 -0500504 if (start + num_bytes < end && start + num_bytes < actual_end) {
Chris Mason771ed682008-11-06 22:02:51 -0500505 start += num_bytes;
506 pages = NULL;
507 cond_resched();
508 goto again;
509 }
510 } else {
Chris Masonf03d9301f2009-02-04 09:31:06 -0500511cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500512 /*
513 * No compression, but we still need to write the pages in
514 * the file we've been given so far. redirty the locked
515 * page if it corresponds to our extent and set things up
516 * for the async work queue to run cow_file_range to do
517 * the normal delalloc dance
518 */
519 if (page_offset(locked_page) >= start &&
520 page_offset(locked_page) <= end) {
521 __set_page_dirty_nobuffers(locked_page);
522 /* unlocked later on in the async handlers */
523 }
524 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
525 *num_added += 1;
526 }
527
528out:
529 return 0;
530
531free_pages_out:
532 for (i = 0; i < nr_pages_ret; i++) {
533 WARN_ON(pages[i]->mapping);
534 page_cache_release(pages[i]);
535 }
Chris Masond3977122009-01-05 21:25:51 -0500536 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500537
538 goto out;
539}
540
541/*
542 * phase two of compressed writeback. This is the ordered portion
543 * of the code, which only gets called in the order the work was
544 * queued. We walk all the async extents created by compress_file_range
545 * and send them down to the disk.
546 */
547static noinline int submit_compressed_extents(struct inode *inode,
548 struct async_cow *async_cow)
549{
550 struct async_extent *async_extent;
551 u64 alloc_hint = 0;
552 struct btrfs_trans_handle *trans;
553 struct btrfs_key ins;
554 struct extent_map *em;
555 struct btrfs_root *root = BTRFS_I(inode)->root;
556 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
557 struct extent_io_tree *io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500558 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500559
560 if (list_empty(&async_cow->extents))
561 return 0;
562
Chris Mason771ed682008-11-06 22:02:51 -0500563
Chris Masond3977122009-01-05 21:25:51 -0500564 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500565 async_extent = list_entry(async_cow->extents.next,
566 struct async_extent, list);
567 list_del(&async_extent->list);
568
569 io_tree = &BTRFS_I(inode)->io_tree;
570
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500571retry:
Chris Mason771ed682008-11-06 22:02:51 -0500572 /* did the compression code fall back to uncompressed IO? */
573 if (!async_extent->pages) {
574 int page_started = 0;
575 unsigned long nr_written = 0;
576
577 lock_extent(io_tree, async_extent->start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000578 async_extent->start +
579 async_extent->ram_size - 1, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500580
581 /* allocate blocks */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500582 ret = cow_file_range(inode, async_cow->locked_page,
583 async_extent->start,
584 async_extent->start +
585 async_extent->ram_size - 1,
586 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500587
588 /*
589 * if page_started, cow_file_range inserted an
590 * inline extent and took care of all the unlocking
591 * and IO for us. Otherwise, we need to submit
592 * all those pages down to the drive.
593 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500594 if (!page_started && !ret)
Chris Mason771ed682008-11-06 22:02:51 -0500595 extent_write_locked_range(io_tree,
596 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500597 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500598 async_extent->ram_size - 1,
599 btrfs_get_extent,
600 WB_SYNC_ALL);
601 kfree(async_extent);
602 cond_resched();
603 continue;
604 }
605
606 lock_extent(io_tree, async_extent->start,
607 async_extent->start + async_extent->ram_size - 1,
608 GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500609
Yan, Zhengc2167752009-11-12 09:34:21 +0000610 trans = btrfs_join_transaction(root, 1);
Chris Mason771ed682008-11-06 22:02:51 -0500611 ret = btrfs_reserve_extent(trans, root,
612 async_extent->compressed_size,
613 async_extent->compressed_size,
614 0, alloc_hint,
615 (u64)-1, &ins, 1);
Yan, Zhengc2167752009-11-12 09:34:21 +0000616 btrfs_end_transaction(trans, root);
617
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500618 if (ret) {
619 int i;
620 for (i = 0; i < async_extent->nr_pages; i++) {
621 WARN_ON(async_extent->pages[i]->mapping);
622 page_cache_release(async_extent->pages[i]);
623 }
624 kfree(async_extent->pages);
625 async_extent->nr_pages = 0;
626 async_extent->pages = NULL;
627 unlock_extent(io_tree, async_extent->start,
628 async_extent->start +
629 async_extent->ram_size - 1, GFP_NOFS);
630 goto retry;
631 }
632
Yan, Zhengc2167752009-11-12 09:34:21 +0000633 /*
634 * here we're doing allocation and writeback of the
635 * compressed pages
636 */
637 btrfs_drop_extent_cache(inode, async_extent->start,
638 async_extent->start +
639 async_extent->ram_size - 1, 0);
640
Chris Mason771ed682008-11-06 22:02:51 -0500641 em = alloc_extent_map(GFP_NOFS);
642 em->start = async_extent->start;
643 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500644 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500645
646 em->block_start = ins.objectid;
647 em->block_len = ins.offset;
648 em->bdev = root->fs_info->fs_devices->latest_bdev;
649 set_bit(EXTENT_FLAG_PINNED, &em->flags);
650 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
651
Chris Masond3977122009-01-05 21:25:51 -0500652 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400653 write_lock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500654 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400655 write_unlock(&em_tree->lock);
Chris Mason771ed682008-11-06 22:02:51 -0500656 if (ret != -EEXIST) {
657 free_extent_map(em);
658 break;
659 }
660 btrfs_drop_extent_cache(inode, async_extent->start,
661 async_extent->start +
662 async_extent->ram_size - 1, 0);
663 }
664
665 ret = btrfs_add_ordered_extent(inode, async_extent->start,
666 ins.objectid,
667 async_extent->ram_size,
668 ins.offset,
669 BTRFS_ORDERED_COMPRESSED);
670 BUG_ON(ret);
671
Chris Mason771ed682008-11-06 22:02:51 -0500672 /*
673 * clear dirty, set writeback and unlock the pages.
674 */
675 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400676 &BTRFS_I(inode)->io_tree,
677 async_extent->start,
678 async_extent->start +
679 async_extent->ram_size - 1,
680 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
681 EXTENT_CLEAR_UNLOCK |
Chris Masona3429ab2009-10-08 12:30:20 -0400682 EXTENT_CLEAR_DELALLOC |
Chris Masona791e352009-10-08 11:27:10 -0400683 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -0500684
685 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500686 async_extent->start,
687 async_extent->ram_size,
688 ins.objectid,
689 ins.offset, async_extent->pages,
690 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500691
692 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -0500693 alloc_hint = ins.objectid + ins.offset;
694 kfree(async_extent);
695 cond_resched();
696 }
697
Chris Mason771ed682008-11-06 22:02:51 -0500698 return 0;
699}
700
Josef Bacik4b46fce2010-05-23 11:00:55 -0400701static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
702 u64 num_bytes)
703{
704 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
705 struct extent_map *em;
706 u64 alloc_hint = 0;
707
708 read_lock(&em_tree->lock);
709 em = search_extent_mapping(em_tree, start, num_bytes);
710 if (em) {
711 /*
712 * if block start isn't an actual block number then find the
713 * first block in this inode and use that as a hint. If that
714 * block is also bogus then just don't worry about it.
715 */
716 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
717 free_extent_map(em);
718 em = search_extent_mapping(em_tree, 0, 0);
719 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
720 alloc_hint = em->block_start;
721 if (em)
722 free_extent_map(em);
723 } else {
724 alloc_hint = em->block_start;
725 free_extent_map(em);
726 }
727 }
728 read_unlock(&em_tree->lock);
729
730 return alloc_hint;
731}
732
Chris Mason771ed682008-11-06 22:02:51 -0500733/*
734 * when extent_io.c finds a delayed allocation range in the file,
735 * the call backs end up in this code. The basic idea is to
736 * allocate extents on disk for the range, and create ordered data structs
737 * in ram to track those extents.
738 *
739 * locked_page is the page that writepage had locked already. We use
740 * it to make sure we don't do extra locks or unlocks.
741 *
742 * *page_started is set to one if we unlock locked_page and do everything
743 * required to start IO on it. It may be clean and already done with
744 * IO when we return.
745 */
746static noinline int cow_file_range(struct inode *inode,
747 struct page *locked_page,
748 u64 start, u64 end, int *page_started,
749 unsigned long *nr_written,
750 int unlock)
751{
752 struct btrfs_root *root = BTRFS_I(inode)->root;
753 struct btrfs_trans_handle *trans;
754 u64 alloc_hint = 0;
755 u64 num_bytes;
756 unsigned long ram_size;
757 u64 disk_num_bytes;
758 u64 cur_alloc_size;
759 u64 blocksize = root->sectorsize;
760 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500761 u64 isize = i_size_read(inode);
Chris Mason771ed682008-11-06 22:02:51 -0500762 struct btrfs_key ins;
763 struct extent_map *em;
764 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
765 int ret = 0;
766
Josef Bacik0cb59c92010-07-02 12:14:14 -0400767 BUG_ON(root == root->fs_info->tree_root);
Chris Mason771ed682008-11-06 22:02:51 -0500768 trans = btrfs_join_transaction(root, 1);
769 BUG_ON(!trans);
770 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400771 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500772
Chris Mason42dc7ba2008-12-15 11:44:56 -0500773 actual_end = min_t(u64, isize, end + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500774
775 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
776 num_bytes = max(blocksize, num_bytes);
777 disk_num_bytes = num_bytes;
778 ret = 0;
779
780 if (start == 0) {
781 /* lets try to make an inline extent */
782 ret = cow_file_range_inline(trans, root, inode,
783 start, end, 0, NULL);
784 if (ret == 0) {
785 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400786 &BTRFS_I(inode)->io_tree,
787 start, end, NULL,
788 EXTENT_CLEAR_UNLOCK_PAGE |
789 EXTENT_CLEAR_UNLOCK |
790 EXTENT_CLEAR_DELALLOC |
791 EXTENT_CLEAR_DIRTY |
792 EXTENT_SET_WRITEBACK |
793 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000794
Chris Mason771ed682008-11-06 22:02:51 -0500795 *nr_written = *nr_written +
796 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
797 *page_started = 1;
798 ret = 0;
799 goto out;
800 }
801 }
Chris Masonc8b97812008-10-29 14:49:59 -0400802
803 BUG_ON(disk_num_bytes >
804 btrfs_super_total_bytes(&root->fs_info->super_copy));
805
Josef Bacik4b46fce2010-05-23 11:00:55 -0400806 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400807 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400808
Chris Masond3977122009-01-05 21:25:51 -0500809 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400810 unsigned long op;
811
Josef Bacik287a0ab2010-03-19 18:07:23 +0000812 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400813 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500814 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400815 (u64)-1, &ins, 1);
Chris Masond3977122009-01-05 21:25:51 -0500816 BUG_ON(ret);
817
Chris Masone6dcd2d2008-07-17 12:53:50 -0400818 em = alloc_extent_map(GFP_NOFS);
819 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500820 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500821 ram_size = ins.offset;
822 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400823
Chris Masone6dcd2d2008-07-17 12:53:50 -0400824 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400825 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400826 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400827 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400828
Chris Masond3977122009-01-05 21:25:51 -0500829 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400830 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400831 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400832 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400833 if (ret != -EEXIST) {
834 free_extent_map(em);
835 break;
836 }
837 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400838 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400839 }
840
Chris Mason98d20f62008-04-14 09:46:10 -0400841 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400842 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500843 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400844 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400845
Yan Zheng17d217f2008-12-12 10:03:38 -0500846 if (root->root_key.objectid ==
847 BTRFS_DATA_RELOC_TREE_OBJECTID) {
848 ret = btrfs_reloc_clone_csums(inode, start,
849 cur_alloc_size);
850 BUG_ON(ret);
851 }
852
Chris Masond3977122009-01-05 21:25:51 -0500853 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400854 break;
Chris Masond3977122009-01-05 21:25:51 -0500855
Chris Masonc8b97812008-10-29 14:49:59 -0400856 /* we're not doing compressed IO, don't unlock the first
857 * page (which the caller expects to stay locked), don't
858 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400859 *
860 * Do set the Private2 bit so we know this page was properly
861 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400862 */
Chris Masona791e352009-10-08 11:27:10 -0400863 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
864 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
865 EXTENT_SET_PRIVATE2;
866
Chris Masonc8b97812008-10-29 14:49:59 -0400867 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
868 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400869 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400870 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500871 num_bytes -= cur_alloc_size;
872 alloc_hint = ins.objectid + ins.offset;
873 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400874 }
Chris Masonb888db22007-08-27 16:49:44 -0400875out:
Chris Mason771ed682008-11-06 22:02:51 -0500876 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400877 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400878
Chris Masonbe20aa92007-12-17 20:14:01 -0500879 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500880}
Chris Masonc8b97812008-10-29 14:49:59 -0400881
Chris Mason771ed682008-11-06 22:02:51 -0500882/*
883 * work queue call back to started compression on a file and pages
884 */
885static noinline void async_cow_start(struct btrfs_work *work)
886{
887 struct async_cow *async_cow;
888 int num_added = 0;
889 async_cow = container_of(work, struct async_cow, work);
890
891 compress_file_range(async_cow->inode, async_cow->locked_page,
892 async_cow->start, async_cow->end, async_cow,
893 &num_added);
894 if (num_added == 0)
895 async_cow->inode = NULL;
896}
897
898/*
899 * work queue call back to submit previously compressed pages
900 */
901static noinline void async_cow_submit(struct btrfs_work *work)
902{
903 struct async_cow *async_cow;
904 struct btrfs_root *root;
905 unsigned long nr_pages;
906
907 async_cow = container_of(work, struct async_cow, work);
908
909 root = async_cow->root;
910 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
911 PAGE_CACHE_SHIFT;
912
913 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
914
915 if (atomic_read(&root->fs_info->async_delalloc_pages) <
916 5 * 1042 * 1024 &&
917 waitqueue_active(&root->fs_info->async_submit_wait))
918 wake_up(&root->fs_info->async_submit_wait);
919
Chris Masond3977122009-01-05 21:25:51 -0500920 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -0500921 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500922}
Chris Masonc8b97812008-10-29 14:49:59 -0400923
Chris Mason771ed682008-11-06 22:02:51 -0500924static noinline void async_cow_free(struct btrfs_work *work)
925{
926 struct async_cow *async_cow;
927 async_cow = container_of(work, struct async_cow, work);
928 kfree(async_cow);
929}
930
931static int cow_file_range_async(struct inode *inode, struct page *locked_page,
932 u64 start, u64 end, int *page_started,
933 unsigned long *nr_written)
934{
935 struct async_cow *async_cow;
936 struct btrfs_root *root = BTRFS_I(inode)->root;
937 unsigned long nr_pages;
938 u64 cur_end;
939 int limit = 10 * 1024 * 1042;
940
Chris Masona3429ab2009-10-08 12:30:20 -0400941 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
942 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500943 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500944 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
945 async_cow->inode = inode;
946 async_cow->root = root;
947 async_cow->locked_page = locked_page;
948 async_cow->start = start;
949
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200950 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -0500951 cur_end = end;
952 else
953 cur_end = min(end, start + 512 * 1024 - 1);
954
955 async_cow->end = cur_end;
956 INIT_LIST_HEAD(&async_cow->extents);
957
958 async_cow->work.func = async_cow_start;
959 async_cow->work.ordered_func = async_cow_submit;
960 async_cow->work.ordered_free = async_cow_free;
961 async_cow->work.flags = 0;
962
Chris Mason771ed682008-11-06 22:02:51 -0500963 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
964 PAGE_CACHE_SHIFT;
965 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
966
967 btrfs_queue_worker(&root->fs_info->delalloc_workers,
968 &async_cow->work);
969
970 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
971 wait_event(root->fs_info->async_submit_wait,
972 (atomic_read(&root->fs_info->async_delalloc_pages) <
973 limit));
974 }
975
Chris Masond3977122009-01-05 21:25:51 -0500976 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -0500977 atomic_read(&root->fs_info->async_delalloc_pages)) {
978 wait_event(root->fs_info->async_submit_wait,
979 (atomic_read(&root->fs_info->async_delalloc_pages) ==
980 0));
981 }
982
983 *nr_written += nr_pages;
984 start = cur_end + 1;
985 }
986 *page_started = 1;
987 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500988}
989
Chris Masond3977122009-01-05 21:25:51 -0500990static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -0500991 u64 bytenr, u64 num_bytes)
992{
993 int ret;
994 struct btrfs_ordered_sum *sums;
995 LIST_HEAD(list);
996
Yan Zheng07d400a2009-01-06 11:42:00 -0500997 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
998 bytenr + num_bytes - 1, &list);
Yan Zheng17d217f2008-12-12 10:03:38 -0500999 if (ret == 0 && list_empty(&list))
1000 return 0;
1001
1002 while (!list_empty(&list)) {
1003 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1004 list_del(&sums->list);
1005 kfree(sums);
1006 }
1007 return 1;
1008}
1009
Chris Masond352ac62008-09-29 15:18:18 -04001010/*
1011 * when nowcow writeback call back. This checks for snapshots or COW copies
1012 * of the extents that exist in the file, and COWs the file as required.
1013 *
1014 * If no cow copies or snapshots exist, we write directly to the existing
1015 * blocks on disk
1016 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001017static noinline int run_delalloc_nocow(struct inode *inode,
1018 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001019 u64 start, u64 end, int *page_started, int force,
1020 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001021{
Chris Masonbe20aa92007-12-17 20:14:01 -05001022 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001023 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001024 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001025 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001026 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001027 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001028 u64 cow_start;
1029 u64 cur_offset;
1030 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001031 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001032 u64 disk_bytenr;
1033 u64 num_bytes;
1034 int extent_type;
1035 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001036 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001037 int nocow;
1038 int check_prev = 1;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001039 bool nolock = false;
Chris Masonbe20aa92007-12-17 20:14:01 -05001040
1041 path = btrfs_alloc_path();
1042 BUG_ON(!path);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001043 if (root == root->fs_info->tree_root) {
1044 nolock = true;
1045 trans = btrfs_join_transaction_nolock(root, 1);
1046 } else {
1047 trans = btrfs_join_transaction(root, 1);
1048 }
Yan Zheng7ea394f2008-08-05 13:05:02 -04001049 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -05001050
Yan Zheng80ff3852008-10-30 14:20:02 -04001051 cow_start = (u64)-1;
1052 cur_offset = start;
1053 while (1) {
1054 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1055 cur_offset, 0);
1056 BUG_ON(ret < 0);
1057 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1058 leaf = path->nodes[0];
1059 btrfs_item_key_to_cpu(leaf, &found_key,
1060 path->slots[0] - 1);
1061 if (found_key.objectid == inode->i_ino &&
1062 found_key.type == BTRFS_EXTENT_DATA_KEY)
1063 path->slots[0]--;
1064 }
1065 check_prev = 0;
1066next_slot:
1067 leaf = path->nodes[0];
1068 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1069 ret = btrfs_next_leaf(root, path);
1070 if (ret < 0)
1071 BUG_ON(1);
1072 if (ret > 0)
1073 break;
1074 leaf = path->nodes[0];
1075 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001076
Yan Zheng80ff3852008-10-30 14:20:02 -04001077 nocow = 0;
1078 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001079 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001080 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001081
Yan Zheng80ff3852008-10-30 14:20:02 -04001082 if (found_key.objectid > inode->i_ino ||
1083 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1084 found_key.offset > end)
1085 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001086
Yan Zheng80ff3852008-10-30 14:20:02 -04001087 if (found_key.offset > cur_offset) {
1088 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001089 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001090 goto out_check;
1091 }
Chris Masonc31f8832008-01-08 15:46:31 -05001092
Yan Zheng80ff3852008-10-30 14:20:02 -04001093 fi = btrfs_item_ptr(leaf, path->slots[0],
1094 struct btrfs_file_extent_item);
1095 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001096
Yan Zhengd899e052008-10-30 14:25:28 -04001097 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1098 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001099 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001100 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001101 extent_end = found_key.offset +
1102 btrfs_file_extent_num_bytes(leaf, fi);
1103 if (extent_end <= start) {
1104 path->slots[0]++;
1105 goto next_slot;
1106 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001107 if (disk_bytenr == 0)
1108 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001109 if (btrfs_file_extent_compression(leaf, fi) ||
1110 btrfs_file_extent_encryption(leaf, fi) ||
1111 btrfs_file_extent_other_encoding(leaf, fi))
1112 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001113 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1114 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001115 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001116 goto out_check;
Yan Zheng17d217f2008-12-12 10:03:38 -05001117 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001118 found_key.offset -
1119 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001120 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001121 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001122 disk_bytenr += cur_offset - found_key.offset;
1123 num_bytes = min(end + 1, extent_end) - cur_offset;
1124 /*
1125 * force cow if csum exists in the range.
1126 * this ensure that csum for a given extent are
1127 * either valid or do not exist.
1128 */
1129 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1130 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001131 nocow = 1;
1132 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1133 extent_end = found_key.offset +
1134 btrfs_file_extent_inline_len(leaf, fi);
1135 extent_end = ALIGN(extent_end, root->sectorsize);
1136 } else {
1137 BUG_ON(1);
1138 }
1139out_check:
1140 if (extent_end <= start) {
1141 path->slots[0]++;
1142 goto next_slot;
1143 }
1144 if (!nocow) {
1145 if (cow_start == (u64)-1)
1146 cow_start = cur_offset;
1147 cur_offset = extent_end;
1148 if (cur_offset > end)
1149 break;
1150 path->slots[0]++;
1151 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001152 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001153
Yan Zheng7ea394f2008-08-05 13:05:02 -04001154 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001155 if (cow_start != (u64)-1) {
1156 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001157 found_key.offset - 1, page_started,
1158 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001159 BUG_ON(ret);
1160 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001161 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001162
Yan Zhengd899e052008-10-30 14:25:28 -04001163 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1164 struct extent_map *em;
1165 struct extent_map_tree *em_tree;
1166 em_tree = &BTRFS_I(inode)->extent_tree;
1167 em = alloc_extent_map(GFP_NOFS);
1168 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001169 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001170 em->len = num_bytes;
1171 em->block_len = num_bytes;
1172 em->block_start = disk_bytenr;
1173 em->bdev = root->fs_info->fs_devices->latest_bdev;
1174 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1175 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001176 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001177 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001178 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001179 if (ret != -EEXIST) {
1180 free_extent_map(em);
1181 break;
1182 }
1183 btrfs_drop_extent_cache(inode, em->start,
1184 em->start + em->len - 1, 0);
1185 }
1186 type = BTRFS_ORDERED_PREALLOC;
1187 } else {
1188 type = BTRFS_ORDERED_NOCOW;
1189 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001190
1191 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001192 num_bytes, num_bytes, type);
1193 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001194
Yan, Zhengefa56462010-05-16 10:49:59 -04001195 if (root->root_key.objectid ==
1196 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1197 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1198 num_bytes);
1199 BUG_ON(ret);
1200 }
1201
Yan Zhengd899e052008-10-30 14:25:28 -04001202 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001203 cur_offset, cur_offset + num_bytes - 1,
1204 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1205 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1206 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001207 cur_offset = extent_end;
1208 if (cur_offset > end)
1209 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001210 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001211 btrfs_release_path(root, path);
1212
1213 if (cur_offset <= end && cow_start == (u64)-1)
1214 cow_start = cur_offset;
1215 if (cow_start != (u64)-1) {
1216 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001217 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001218 BUG_ON(ret);
1219 }
1220
Josef Bacik0cb59c92010-07-02 12:14:14 -04001221 if (nolock) {
1222 ret = btrfs_end_transaction_nolock(trans, root);
1223 BUG_ON(ret);
1224 } else {
1225 ret = btrfs_end_transaction(trans, root);
1226 BUG_ON(ret);
1227 }
Yan Zheng7ea394f2008-08-05 13:05:02 -04001228 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001229 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001230}
1231
Chris Masond352ac62008-09-29 15:18:18 -04001232/*
1233 * extent_io.c call back to do delayed allocation processing
1234 */
Chris Masonc8b97812008-10-29 14:49:59 -04001235static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001236 u64 start, u64 end, int *page_started,
1237 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001238{
Chris Masonbe20aa92007-12-17 20:14:01 -05001239 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001240 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001241
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001242 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
Chris Masonc8b97812008-10-29 14:49:59 -04001243 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001244 page_started, 1, nr_written);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001245 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
Yan Zhengd899e052008-10-30 14:25:28 -04001246 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001247 page_started, 0, nr_written);
Chris Mason1e701a32010-03-11 09:42:04 -05001248 else if (!btrfs_test_opt(root, COMPRESS) &&
1249 !(BTRFS_I(inode)->force_compress))
Chris Mason7f366cf2009-03-12 20:12:45 -04001250 ret = cow_file_range(inode, locked_page, start, end,
1251 page_started, nr_written, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -05001252 else
Chris Mason771ed682008-11-06 22:02:51 -05001253 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001254 page_started, nr_written);
Chris Masonb888db22007-08-27 16:49:44 -04001255 return ret;
1256}
1257
Josef Bacik9ed74f22009-09-11 16:12:44 -04001258static int btrfs_split_extent_hook(struct inode *inode,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001259 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001260{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001261 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001262 if (!(orig->state & EXTENT_DELALLOC))
1263 return 0;
1264
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001265 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001266 return 0;
1267}
1268
1269/*
1270 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1271 * extents so we can keep track of new extents that are just merged onto old
1272 * extents, such as when we are doing sequential writes, so we can properly
1273 * account for the metadata space we'll need.
1274 */
1275static int btrfs_merge_extent_hook(struct inode *inode,
1276 struct extent_state *new,
1277 struct extent_state *other)
1278{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001279 /* not delalloc, ignore it */
1280 if (!(other->state & EXTENT_DELALLOC))
1281 return 0;
1282
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001283 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001284 return 0;
1285}
1286
Chris Masond352ac62008-09-29 15:18:18 -04001287/*
1288 * extent_io.c set_bit_hook, used to track delayed allocation
1289 * bytes in this file, and to maintain the list of inodes that
1290 * have pending delalloc work to be done.
1291 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001292static int btrfs_set_bit_hook(struct inode *inode,
1293 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001294{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001295
Chris Mason75eff682008-12-15 15:54:40 -05001296 /*
1297 * set_bit and clear bit hooks normally require _irqsave/restore
1298 * but in this case, we are only testeing for the DELALLOC
1299 * bit, which is only set or cleared with irqs on
1300 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001301 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001302 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001303 u64 len = state->end + 1 - state->start;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001304 int do_list = (root->root_key.objectid !=
1305 BTRFS_ROOT_TREE_OBJECTID);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001306
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001307 if (*bits & EXTENT_FIRST_DELALLOC)
1308 *bits &= ~EXTENT_FIRST_DELALLOC;
1309 else
1310 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001311
Chris Mason75eff682008-12-15 15:54:40 -05001312 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001313 BTRFS_I(inode)->delalloc_bytes += len;
1314 root->fs_info->delalloc_bytes += len;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001315 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
Chris Masonea8c2812008-08-04 23:17:27 -04001316 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1317 &root->fs_info->delalloc_inodes);
1318 }
Chris Mason75eff682008-12-15 15:54:40 -05001319 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001320 }
1321 return 0;
1322}
1323
Chris Masond352ac62008-09-29 15:18:18 -04001324/*
1325 * extent_io.c clear_bit_hook, see set_bit_hook for why
1326 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001327static int btrfs_clear_bit_hook(struct inode *inode,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001328 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001329{
Chris Mason75eff682008-12-15 15:54:40 -05001330 /*
1331 * set_bit and clear bit hooks normally require _irqsave/restore
1332 * but in this case, we are only testeing for the DELALLOC
1333 * bit, which is only set or cleared with irqs on
1334 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001335 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001336 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001337 u64 len = state->end + 1 - state->start;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001338 int do_list = (root->root_key.objectid !=
1339 BTRFS_ROOT_TREE_OBJECTID);
Chris Masonbcbfce82008-04-22 13:26:47 -04001340
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001341 if (*bits & EXTENT_FIRST_DELALLOC)
1342 *bits &= ~EXTENT_FIRST_DELALLOC;
1343 else if (!(*bits & EXTENT_DO_ACCOUNTING))
1344 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
1345
1346 if (*bits & EXTENT_DO_ACCOUNTING)
1347 btrfs_delalloc_release_metadata(inode, len);
1348
Josef Bacik0cb59c92010-07-02 12:14:14 -04001349 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1350 && do_list)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001351 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001352
Chris Mason75eff682008-12-15 15:54:40 -05001353 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001354 root->fs_info->delalloc_bytes -= len;
1355 BTRFS_I(inode)->delalloc_bytes -= len;
1356
Josef Bacik0cb59c92010-07-02 12:14:14 -04001357 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
Chris Masonea8c2812008-08-04 23:17:27 -04001358 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1359 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1360 }
Chris Mason75eff682008-12-15 15:54:40 -05001361 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001362 }
1363 return 0;
1364}
1365
Chris Masond352ac62008-09-29 15:18:18 -04001366/*
1367 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1368 * we don't create bios that span stripes or chunks
1369 */
Chris Mason239b14b2008-03-24 15:02:07 -04001370int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001371 size_t size, struct bio *bio,
1372 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001373{
1374 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1375 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001376 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001377 u64 length = 0;
1378 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001379 int ret;
1380
Chris Mason771ed682008-11-06 22:02:51 -05001381 if (bio_flags & EXTENT_BIO_COMPRESSED)
1382 return 0;
1383
Chris Masonf2d8d742008-04-21 10:03:05 -04001384 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001385 map_tree = &root->fs_info->mapping_tree;
1386 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001387 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001388 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001389
Chris Masond3977122009-01-05 21:25:51 -05001390 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001391 return 1;
Chris Mason239b14b2008-03-24 15:02:07 -04001392 return 0;
1393}
1394
Chris Masond352ac62008-09-29 15:18:18 -04001395/*
1396 * in order to insert checksums into the metadata in large chunks,
1397 * we wait until bio submission time. All the pages in the bio are
1398 * checksummed and sums are attached onto the ordered extent record.
1399 *
1400 * At IO completion time the cums attached on the ordered extent record
1401 * are inserted into the btree
1402 */
Chris Masond3977122009-01-05 21:25:51 -05001403static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1404 struct bio *bio, int mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001405 unsigned long bio_flags,
1406 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05001407{
Chris Mason065631f2008-02-20 12:07:25 -05001408 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001409 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001410
Chris Masond20f7042008-12-08 16:58:54 -05001411 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001412 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001413 return 0;
1414}
Chris Masone0156402008-04-16 11:15:20 -04001415
Chris Mason4a69a412008-11-06 22:03:00 -05001416/*
1417 * in order to insert checksums into the metadata in large chunks,
1418 * we wait until bio submission time. All the pages in the bio are
1419 * checksummed and sums are attached onto the ordered extent record.
1420 *
1421 * At IO completion time the cums attached on the ordered extent record
1422 * are inserted into the btree
1423 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001424static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001425 int mirror_num, unsigned long bio_flags,
1426 u64 bio_offset)
Chris Mason4a69a412008-11-06 22:03:00 -05001427{
1428 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001429 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001430}
1431
Chris Masond352ac62008-09-29 15:18:18 -04001432/*
Chris Masoncad321a2008-12-17 14:51:42 -05001433 * extent_io.c submission hook. This does the right thing for csum calculation
1434 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001435 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001436static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001437 int mirror_num, unsigned long bio_flags,
1438 u64 bio_offset)
Chris Mason44b8bd72008-04-16 11:14:51 -04001439{
1440 struct btrfs_root *root = BTRFS_I(inode)->root;
1441 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001442 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001443
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001444 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001445
Josef Bacik0cb59c92010-07-02 12:14:14 -04001446 if (root == root->fs_info->tree_root)
1447 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2);
1448 else
1449 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001450 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -05001451
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001452 if (!(rw & REQ_WRITE)) {
Chris Masond20f7042008-12-08 16:58:54 -05001453 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001454 return btrfs_submit_compressed_read(inode, bio,
1455 mirror_num, bio_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001456 } else if (!skip_sum)
1457 btrfs_lookup_bio_sums(root, inode, bio, NULL);
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001458 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001459 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001460 /* csum items have already been cloned */
1461 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1462 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001463 /* we're doing a write, do the async checksumming */
1464 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001465 inode, rw, bio, mirror_num,
Chris Masoneaf25d92010-05-25 09:48:28 -04001466 bio_flags, bio_offset,
1467 __btrfs_submit_bio_start,
Chris Mason4a69a412008-11-06 22:03:00 -05001468 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001469 }
1470
Chris Mason0b86a832008-03-24 15:01:56 -04001471mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001472 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001473}
Chris Mason6885f302008-02-20 16:11:05 -05001474
Chris Masond352ac62008-09-29 15:18:18 -04001475/*
1476 * given a list of ordered sums record them in the inode. This happens
1477 * at IO completion time based on sums calculated at bio submission time.
1478 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001479static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001480 struct inode *inode, u64 file_offset,
1481 struct list_head *list)
1482{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001483 struct btrfs_ordered_sum *sum;
1484
1485 btrfs_set_trans_block_group(trans, inode);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001486
1487 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001488 btrfs_csum_file_blocks(trans,
1489 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001490 }
1491 return 0;
1492}
1493
Josef Bacik2ac55d42010-02-03 19:33:23 +00001494int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1495 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001496{
Chris Masond3977122009-01-05 21:25:51 -05001497 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001498 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001499 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001500 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001501}
1502
Chris Masond352ac62008-09-29 15:18:18 -04001503/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001504struct btrfs_writepage_fixup {
1505 struct page *page;
1506 struct btrfs_work work;
1507};
1508
Christoph Hellwigb2950862008-12-02 09:54:17 -05001509static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001510{
1511 struct btrfs_writepage_fixup *fixup;
1512 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001513 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001514 struct page *page;
1515 struct inode *inode;
1516 u64 page_start;
1517 u64 page_end;
1518
1519 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1520 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001521again:
Chris Mason247e7432008-07-17 12:53:51 -04001522 lock_page(page);
1523 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1524 ClearPageChecked(page);
1525 goto out_page;
1526 }
1527
1528 inode = page->mapping->host;
1529 page_start = page_offset(page);
1530 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1531
Josef Bacik2ac55d42010-02-03 19:33:23 +00001532 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
1533 &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001534
1535 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001536 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001537 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001538
1539 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1540 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001541 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1542 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001543 unlock_page(page);
1544 btrfs_start_ordered_extent(inode, ordered, 1);
1545 goto again;
1546 }
Chris Mason247e7432008-07-17 12:53:51 -04001547
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001548 BUG();
Josef Bacik2ac55d42010-02-03 19:33:23 +00001549 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001550 ClearPageChecked(page);
1551out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001552 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1553 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001554out_page:
1555 unlock_page(page);
1556 page_cache_release(page);
1557}
1558
1559/*
1560 * There are a few paths in the higher layers of the kernel that directly
1561 * set the page dirty bit without asking the filesystem if it is a
1562 * good idea. This causes problems because we want to make sure COW
1563 * properly happens and the data=ordered rules are followed.
1564 *
Chris Masonc8b97812008-10-29 14:49:59 -04001565 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001566 * hasn't been properly setup for IO. We kick off an async process
1567 * to fix it up. The async helper will wait for ordered extents, set
1568 * the delalloc bit and make it safe to write the page.
1569 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001570static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001571{
1572 struct inode *inode = page->mapping->host;
1573 struct btrfs_writepage_fixup *fixup;
1574 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001575
Chris Mason8b62b722009-09-02 16:53:46 -04001576 /* this page is properly in the ordered list */
1577 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001578 return 0;
1579
1580 if (PageChecked(page))
1581 return -EAGAIN;
1582
1583 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1584 if (!fixup)
1585 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001586
Chris Mason247e7432008-07-17 12:53:51 -04001587 SetPageChecked(page);
1588 page_cache_get(page);
1589 fixup->work.func = btrfs_writepage_fixup_worker;
1590 fixup->page = page;
1591 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1592 return -EAGAIN;
1593}
1594
Yan Zhengd899e052008-10-30 14:25:28 -04001595static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1596 struct inode *inode, u64 file_pos,
1597 u64 disk_bytenr, u64 disk_num_bytes,
1598 u64 num_bytes, u64 ram_bytes,
1599 u8 compression, u8 encryption,
1600 u16 other_encoding, int extent_type)
1601{
1602 struct btrfs_root *root = BTRFS_I(inode)->root;
1603 struct btrfs_file_extent_item *fi;
1604 struct btrfs_path *path;
1605 struct extent_buffer *leaf;
1606 struct btrfs_key ins;
1607 u64 hint;
1608 int ret;
1609
1610 path = btrfs_alloc_path();
1611 BUG_ON(!path);
1612
Chris Masonb9473432009-03-13 11:00:37 -04001613 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001614
1615 /*
1616 * we may be replacing one extent in the tree with another.
1617 * The new extent is pinned in the extent map, and we don't want
1618 * to drop it from the cache until it is completely in the btree.
1619 *
1620 * So, tell btrfs_drop_extents to leave this extent in the cache.
1621 * the caller is expected to unpin it and allow it to be merged
1622 * with the others.
1623 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001624 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1625 &hint, 0);
Yan Zhengd899e052008-10-30 14:25:28 -04001626 BUG_ON(ret);
1627
1628 ins.objectid = inode->i_ino;
1629 ins.offset = file_pos;
1630 ins.type = BTRFS_EXTENT_DATA_KEY;
1631 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1632 BUG_ON(ret);
1633 leaf = path->nodes[0];
1634 fi = btrfs_item_ptr(leaf, path->slots[0],
1635 struct btrfs_file_extent_item);
1636 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1637 btrfs_set_file_extent_type(leaf, fi, extent_type);
1638 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1639 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1640 btrfs_set_file_extent_offset(leaf, fi, 0);
1641 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1642 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1643 btrfs_set_file_extent_compression(leaf, fi, compression);
1644 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1645 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001646
1647 btrfs_unlock_up_safe(path, 1);
1648 btrfs_set_lock_blocking(leaf);
1649
Yan Zhengd899e052008-10-30 14:25:28 -04001650 btrfs_mark_buffer_dirty(leaf);
1651
1652 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001653
1654 ins.objectid = disk_bytenr;
1655 ins.offset = disk_num_bytes;
1656 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001657 ret = btrfs_alloc_reserved_file_extent(trans, root,
1658 root->root_key.objectid,
1659 inode->i_ino, file_pos, &ins);
Yan Zhengd899e052008-10-30 14:25:28 -04001660 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04001661 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001662
Yan Zhengd899e052008-10-30 14:25:28 -04001663 return 0;
1664}
1665
Chris Mason5d13a982009-03-13 11:41:46 -04001666/*
1667 * helper function for btrfs_finish_ordered_io, this
1668 * just reads in some of the csum leaves to prime them into ram
1669 * before we start the transaction. It limits the amount of btree
1670 * reads required while inside the transaction.
1671 */
Chris Masond352ac62008-09-29 15:18:18 -04001672/* as ordered data IO finishes, this gets called so we can finish
1673 * an ordered extent if the range of bytes in the file it covers are
1674 * fully written.
1675 */
Chris Mason211f90e2008-07-18 11:56:15 -04001676static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001677{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001678 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001679 struct btrfs_trans_handle *trans = NULL;
Chris Mason5d13a982009-03-13 11:41:46 -04001680 struct btrfs_ordered_extent *ordered_extent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001681 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001682 struct extent_state *cached_state = NULL;
Yan Zhengd899e052008-10-30 14:25:28 -04001683 int compressed = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001684 int ret;
Josef Bacik0cb59c92010-07-02 12:14:14 -04001685 bool nolock = false;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001686
Josef Bacik5a1a3df2010-02-02 20:51:14 +00001687 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1688 end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001689 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001690 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001691 BUG_ON(!ordered_extent);
Josef Bacikefd049f2010-02-02 20:50:10 +00001692
Josef Bacik0cb59c92010-07-02 12:14:14 -04001693 nolock = (root == root->fs_info->tree_root);
1694
Yan, Zhengc2167752009-11-12 09:34:21 +00001695 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1696 BUG_ON(!list_empty(&ordered_extent->list));
1697 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1698 if (!ret) {
Josef Bacik0cb59c92010-07-02 12:14:14 -04001699 if (nolock)
1700 trans = btrfs_join_transaction_nolock(root, 1);
1701 else
1702 trans = btrfs_join_transaction(root, 1);
1703 BUG_ON(!trans);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001704 btrfs_set_trans_block_group(trans, inode);
1705 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001706 ret = btrfs_update_inode(trans, root, inode);
1707 BUG_ON(ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001708 }
1709 goto out;
1710 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001711
Josef Bacik2ac55d42010-02-03 19:33:23 +00001712 lock_extent_bits(io_tree, ordered_extent->file_offset,
1713 ordered_extent->file_offset + ordered_extent->len - 1,
1714 0, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001715
Josef Bacik0cb59c92010-07-02 12:14:14 -04001716 if (nolock)
1717 trans = btrfs_join_transaction_nolock(root, 1);
1718 else
1719 trans = btrfs_join_transaction(root, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001720 btrfs_set_trans_block_group(trans, inode);
1721 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001722
Chris Masonc8b97812008-10-29 14:49:59 -04001723 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Yan Zhengd899e052008-10-30 14:25:28 -04001724 compressed = 1;
1725 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1726 BUG_ON(compressed);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001727 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001728 ordered_extent->file_offset,
1729 ordered_extent->file_offset +
1730 ordered_extent->len);
1731 BUG_ON(ret);
1732 } else {
Josef Bacik0af3d002010-06-21 14:48:16 -04001733 BUG_ON(root == root->fs_info->tree_root);
Yan Zhengd899e052008-10-30 14:25:28 -04001734 ret = insert_reserved_file_extent(trans, inode,
1735 ordered_extent->file_offset,
1736 ordered_extent->start,
1737 ordered_extent->disk_len,
1738 ordered_extent->len,
1739 ordered_extent->len,
1740 compressed, 0, 0,
1741 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001742 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1743 ordered_extent->file_offset,
1744 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001745 BUG_ON(ret);
1746 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001747 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1748 ordered_extent->file_offset +
1749 ordered_extent->len - 1, &cached_state, GFP_NOFS);
1750
Chris Masone6dcd2d2008-07-17 12:53:50 -04001751 add_pending_csums(trans, inode, ordered_extent->file_offset,
1752 &ordered_extent->list);
1753
Yan, Zhengc2167752009-11-12 09:34:21 +00001754 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1755 ret = btrfs_update_inode(trans, root, inode);
1756 BUG_ON(ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001757out:
Josef Bacik0cb59c92010-07-02 12:14:14 -04001758 if (nolock) {
1759 if (trans)
1760 btrfs_end_transaction_nolock(trans, root);
1761 } else {
1762 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
1763 if (trans)
1764 btrfs_end_transaction(trans, root);
1765 }
1766
Chris Masone6dcd2d2008-07-17 12:53:50 -04001767 /* once for us */
1768 btrfs_put_ordered_extent(ordered_extent);
1769 /* once for the tree */
1770 btrfs_put_ordered_extent(ordered_extent);
1771
Chris Masone6dcd2d2008-07-17 12:53:50 -04001772 return 0;
1773}
1774
Christoph Hellwigb2950862008-12-02 09:54:17 -05001775static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001776 struct extent_state *state, int uptodate)
1777{
Chris Mason8b62b722009-09-02 16:53:46 -04001778 ClearPagePrivate2(page);
Chris Mason211f90e2008-07-18 11:56:15 -04001779 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1780}
1781
Chris Masond352ac62008-09-29 15:18:18 -04001782/*
1783 * When IO fails, either with EIO or csum verification fails, we
1784 * try other mirrors that might have a good copy of the data. This
1785 * io_failure_record is used to record state as we go through all the
1786 * mirrors. If another mirror has good data, the page is set up to date
1787 * and things continue. If a good mirror can't be found, the original
1788 * bio end_io callback is called to indicate things have failed.
1789 */
Chris Mason7e383262008-04-09 16:28:12 -04001790struct io_failure_record {
1791 struct page *page;
1792 u64 start;
1793 u64 len;
1794 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001795 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001796 int last_mirror;
1797};
1798
Christoph Hellwigb2950862008-12-02 09:54:17 -05001799static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001800 struct page *page, u64 start, u64 end,
1801 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001802{
1803 struct io_failure_record *failrec = NULL;
1804 u64 private;
1805 struct extent_map *em;
1806 struct inode *inode = page->mapping->host;
1807 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001808 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001809 struct bio *bio;
1810 int num_copies;
1811 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001812 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001813 u64 logical;
1814
1815 ret = get_state_private(failure_tree, start, &private);
1816 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001817 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1818 if (!failrec)
1819 return -ENOMEM;
1820 failrec->start = start;
1821 failrec->len = end - start + 1;
1822 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001823 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001824
Chris Mason890871b2009-09-02 16:24:52 -04001825 read_lock(&em_tree->lock);
Chris Mason3b951512008-04-17 11:29:12 -04001826 em = lookup_extent_mapping(em_tree, start, failrec->len);
1827 if (em->start > start || em->start + em->len < start) {
1828 free_extent_map(em);
1829 em = NULL;
1830 }
Chris Mason890871b2009-09-02 16:24:52 -04001831 read_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001832
1833 if (!em || IS_ERR(em)) {
1834 kfree(failrec);
1835 return -EIO;
1836 }
1837 logical = start - em->start;
1838 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001839 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1840 logical = em->block_start;
1841 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1842 }
Chris Mason7e383262008-04-09 16:28:12 -04001843 failrec->logical = logical;
1844 free_extent_map(em);
1845 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1846 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001847 set_state_private(failure_tree, start,
1848 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001849 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001850 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001851 }
1852 num_copies = btrfs_num_copies(
1853 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1854 failrec->logical, failrec->len);
1855 failrec->last_mirror++;
1856 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001857 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001858 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1859 failrec->start,
1860 EXTENT_LOCKED);
1861 if (state && state->start != failrec->start)
1862 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001863 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001864 }
1865 if (!state || failrec->last_mirror > num_copies) {
1866 set_state_private(failure_tree, failrec->start, 0);
1867 clear_extent_bits(failure_tree, failrec->start,
1868 failrec->start + failrec->len - 1,
1869 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1870 kfree(failrec);
1871 return -EIO;
1872 }
1873 bio = bio_alloc(GFP_NOFS, 1);
1874 bio->bi_private = state;
1875 bio->bi_end_io = failed_bio->bi_end_io;
1876 bio->bi_sector = failrec->logical >> 9;
1877 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001878 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001879
Chris Mason7e383262008-04-09 16:28:12 -04001880 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001881 if (failed_bio->bi_rw & REQ_WRITE)
Chris Mason1259ab72008-05-12 13:39:03 -04001882 rw = WRITE;
1883 else
1884 rw = READ;
1885
1886 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001887 failrec->last_mirror,
Chris Masoneaf25d92010-05-25 09:48:28 -04001888 failrec->bio_flags, 0);
Chris Mason1259ab72008-05-12 13:39:03 -04001889 return 0;
1890}
1891
Chris Masond352ac62008-09-29 15:18:18 -04001892/*
1893 * each time an IO finishes, we do a fast check in the IO failure tree
1894 * to see if we need to process or clean up an io_failure_record
1895 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001896static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001897{
1898 u64 private;
1899 u64 private_failure;
1900 struct io_failure_record *failure;
1901 int ret;
1902
1903 private = 0;
1904 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1905 (u64)-1, 1, EXTENT_DIRTY)) {
1906 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1907 start, &private_failure);
1908 if (ret == 0) {
1909 failure = (struct io_failure_record *)(unsigned long)
1910 private_failure;
1911 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1912 failure->start, 0);
1913 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1914 failure->start,
1915 failure->start + failure->len - 1,
1916 EXTENT_DIRTY | EXTENT_LOCKED,
1917 GFP_NOFS);
1918 kfree(failure);
1919 }
1920 }
Chris Mason7e383262008-04-09 16:28:12 -04001921 return 0;
1922}
1923
Chris Masond352ac62008-09-29 15:18:18 -04001924/*
1925 * when reads are done, we need to check csums to verify the data is correct
1926 * if there's a match, we allow the bio to finish. If not, we go through
1927 * the io_failure_record routines to find good copies
1928 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001929static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001930 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001931{
Chris Mason35ebb932007-10-30 16:56:53 -04001932 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001933 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001934 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001935 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001936 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001937 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001938 struct btrfs_root *root = BTRFS_I(inode)->root;
1939 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001940
Chris Masond20f7042008-12-08 16:58:54 -05001941 if (PageChecked(page)) {
1942 ClearPageChecked(page);
1943 goto good;
1944 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001945
1946 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Chris Masonb6cda9b2007-12-14 15:30:32 -05001947 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001948
Yan Zheng17d217f2008-12-12 10:03:38 -05001949 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04001950 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001951 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1952 GFP_NOFS);
1953 return 0;
1954 }
1955
Yanc2e639f2008-02-04 08:57:25 -05001956 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001957 private = state->private;
1958 ret = 0;
1959 } else {
1960 ret = get_state_private(io_tree, start, &private);
1961 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001962 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05001963 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001964 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001965
Chris Masonff79f812007-10-15 16:22:25 -04001966 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1967 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001968 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04001969 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001970
Chris Mason9ab86c82009-01-07 09:48:51 -05001971 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05001972good:
Chris Mason7e383262008-04-09 16:28:12 -04001973 /* if the io failure tree for this inode is non-empty,
1974 * check to see if we've recovered from a failed IO
1975 */
Chris Mason1259ab72008-05-12 13:39:03 -04001976 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04001977 return 0;
1978
1979zeroit:
Chris Mason193f2842009-04-27 07:29:05 -04001980 if (printk_ratelimit()) {
1981 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1982 "private %llu\n", page->mapping->host->i_ino,
1983 (unsigned long long)start, csum,
1984 (unsigned long long)private);
1985 }
Chris Masondb945352007-10-15 16:15:53 -04001986 memset(kaddr + offset, 1, end - start + 1);
1987 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05001988 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04001989 if (private == 0)
1990 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04001991 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04001992}
Chris Masonb888db22007-08-27 16:49:44 -04001993
Yan, Zheng24bbcf02009-11-12 09:36:34 +00001994struct delayed_iput {
1995 struct list_head list;
1996 struct inode *inode;
1997};
1998
1999void btrfs_add_delayed_iput(struct inode *inode)
2000{
2001 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2002 struct delayed_iput *delayed;
2003
2004 if (atomic_add_unless(&inode->i_count, -1, 1))
2005 return;
2006
2007 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2008 delayed->inode = inode;
2009
2010 spin_lock(&fs_info->delayed_iput_lock);
2011 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2012 spin_unlock(&fs_info->delayed_iput_lock);
2013}
2014
2015void btrfs_run_delayed_iputs(struct btrfs_root *root)
2016{
2017 LIST_HEAD(list);
2018 struct btrfs_fs_info *fs_info = root->fs_info;
2019 struct delayed_iput *delayed;
2020 int empty;
2021
2022 spin_lock(&fs_info->delayed_iput_lock);
2023 empty = list_empty(&fs_info->delayed_iputs);
2024 spin_unlock(&fs_info->delayed_iput_lock);
2025 if (empty)
2026 return;
2027
2028 down_read(&root->fs_info->cleanup_work_sem);
2029 spin_lock(&fs_info->delayed_iput_lock);
2030 list_splice_init(&fs_info->delayed_iputs, &list);
2031 spin_unlock(&fs_info->delayed_iput_lock);
2032
2033 while (!list_empty(&list)) {
2034 delayed = list_entry(list.next, struct delayed_iput, list);
2035 list_del(&delayed->list);
2036 iput(delayed->inode);
2037 kfree(delayed);
2038 }
2039 up_read(&root->fs_info->cleanup_work_sem);
2040}
2041
Josef Bacik7b128762008-07-24 12:17:14 -04002042/*
Yan, Zhengd68fc572010-05-16 10:49:58 -04002043 * calculate extra metadata reservation when snapshotting a subvolume
2044 * contains orphan files.
2045 */
2046void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle *trans,
2047 struct btrfs_pending_snapshot *pending,
2048 u64 *bytes_to_reserve)
2049{
2050 struct btrfs_root *root;
2051 struct btrfs_block_rsv *block_rsv;
2052 u64 num_bytes;
2053 int index;
2054
2055 root = pending->root;
2056 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2057 return;
2058
2059 block_rsv = root->orphan_block_rsv;
2060
2061 /* orphan block reservation for the snapshot */
2062 num_bytes = block_rsv->size;
2063
2064 /*
2065 * after the snapshot is created, COWing tree blocks may use more
2066 * space than it frees. So we should make sure there is enough
2067 * reserved space.
2068 */
2069 index = trans->transid & 0x1;
2070 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2071 num_bytes += block_rsv->size -
2072 (block_rsv->reserved + block_rsv->freed[index]);
2073 }
2074
2075 *bytes_to_reserve += num_bytes;
2076}
2077
2078void btrfs_orphan_post_snapshot(struct btrfs_trans_handle *trans,
2079 struct btrfs_pending_snapshot *pending)
2080{
2081 struct btrfs_root *root = pending->root;
2082 struct btrfs_root *snap = pending->snap;
2083 struct btrfs_block_rsv *block_rsv;
2084 u64 num_bytes;
2085 int index;
2086 int ret;
2087
2088 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2089 return;
2090
2091 /* refill source subvolume's orphan block reservation */
2092 block_rsv = root->orphan_block_rsv;
2093 index = trans->transid & 0x1;
2094 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2095 num_bytes = block_rsv->size -
2096 (block_rsv->reserved + block_rsv->freed[index]);
2097 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2098 root->orphan_block_rsv,
2099 num_bytes);
2100 BUG_ON(ret);
2101 }
2102
2103 /* setup orphan block reservation for the snapshot */
2104 block_rsv = btrfs_alloc_block_rsv(snap);
2105 BUG_ON(!block_rsv);
2106
2107 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2108 snap->orphan_block_rsv = block_rsv;
2109
2110 num_bytes = root->orphan_block_rsv->size;
2111 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2112 block_rsv, num_bytes);
2113 BUG_ON(ret);
2114
2115#if 0
2116 /* insert orphan item for the snapshot */
2117 WARN_ON(!root->orphan_item_inserted);
2118 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2119 snap->root_key.objectid);
2120 BUG_ON(ret);
2121 snap->orphan_item_inserted = 1;
2122#endif
2123}
2124
2125enum btrfs_orphan_cleanup_state {
2126 ORPHAN_CLEANUP_STARTED = 1,
2127 ORPHAN_CLEANUP_DONE = 2,
2128};
2129
2130/*
2131 * This is called in transaction commmit time. If there are no orphan
2132 * files in the subvolume, it removes orphan item and frees block_rsv
2133 * structure.
2134 */
2135void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2136 struct btrfs_root *root)
2137{
2138 int ret;
2139
2140 if (!list_empty(&root->orphan_list) ||
2141 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2142 return;
2143
2144 if (root->orphan_item_inserted &&
2145 btrfs_root_refs(&root->root_item) > 0) {
2146 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2147 root->root_key.objectid);
2148 BUG_ON(ret);
2149 root->orphan_item_inserted = 0;
2150 }
2151
2152 if (root->orphan_block_rsv) {
2153 WARN_ON(root->orphan_block_rsv->size > 0);
2154 btrfs_free_block_rsv(root, root->orphan_block_rsv);
2155 root->orphan_block_rsv = NULL;
2156 }
2157}
2158
2159/*
Josef Bacik7b128762008-07-24 12:17:14 -04002160 * This creates an orphan entry for the given inode in case something goes
2161 * wrong in the middle of an unlink/truncate.
Yan, Zhengd68fc572010-05-16 10:49:58 -04002162 *
2163 * NOTE: caller of this function should reserve 5 units of metadata for
2164 * this function.
Josef Bacik7b128762008-07-24 12:17:14 -04002165 */
2166int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2167{
2168 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002169 struct btrfs_block_rsv *block_rsv = NULL;
2170 int reserve = 0;
2171 int insert = 0;
2172 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002173
Yan, Zhengd68fc572010-05-16 10:49:58 -04002174 if (!root->orphan_block_rsv) {
2175 block_rsv = btrfs_alloc_block_rsv(root);
2176 BUG_ON(!block_rsv);
Josef Bacik7b128762008-07-24 12:17:14 -04002177 }
2178
Yan, Zhengd68fc572010-05-16 10:49:58 -04002179 spin_lock(&root->orphan_lock);
2180 if (!root->orphan_block_rsv) {
2181 root->orphan_block_rsv = block_rsv;
2182 } else if (block_rsv) {
2183 btrfs_free_block_rsv(root, block_rsv);
2184 block_rsv = NULL;
2185 }
Josef Bacik7b128762008-07-24 12:17:14 -04002186
Yan, Zhengd68fc572010-05-16 10:49:58 -04002187 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
2188 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2189#if 0
2190 /*
2191 * For proper ENOSPC handling, we should do orphan
2192 * cleanup when mounting. But this introduces backward
2193 * compatibility issue.
2194 */
2195 if (!xchg(&root->orphan_item_inserted, 1))
2196 insert = 2;
2197 else
2198 insert = 1;
2199#endif
2200 insert = 1;
2201 } else {
2202 WARN_ON(!BTRFS_I(inode)->orphan_meta_reserved);
2203 }
Josef Bacik7b128762008-07-24 12:17:14 -04002204
Yan, Zhengd68fc572010-05-16 10:49:58 -04002205 if (!BTRFS_I(inode)->orphan_meta_reserved) {
2206 BTRFS_I(inode)->orphan_meta_reserved = 1;
2207 reserve = 1;
2208 }
2209 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002210
Yan, Zhengd68fc572010-05-16 10:49:58 -04002211 if (block_rsv)
2212 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2213
2214 /* grab metadata reservation from transaction handle */
2215 if (reserve) {
2216 ret = btrfs_orphan_reserve_metadata(trans, inode);
2217 BUG_ON(ret);
2218 }
2219
2220 /* insert an orphan item to track this unlinked/truncated file */
2221 if (insert >= 1) {
2222 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
2223 BUG_ON(ret);
2224 }
2225
2226 /* insert an orphan item to track subvolume contains orphan files */
2227 if (insert >= 2) {
2228 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2229 root->root_key.objectid);
2230 BUG_ON(ret);
2231 }
2232 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002233}
2234
2235/*
2236 * We have done the truncate/delete so we can go ahead and remove the orphan
2237 * item for this particular inode.
2238 */
2239int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2240{
2241 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002242 int delete_item = 0;
2243 int release_rsv = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002244 int ret = 0;
2245
Yan, Zhengd68fc572010-05-16 10:49:58 -04002246 spin_lock(&root->orphan_lock);
2247 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
2248 list_del_init(&BTRFS_I(inode)->i_orphan);
2249 delete_item = 1;
Josef Bacik7b128762008-07-24 12:17:14 -04002250 }
2251
Yan, Zhengd68fc572010-05-16 10:49:58 -04002252 if (BTRFS_I(inode)->orphan_meta_reserved) {
2253 BTRFS_I(inode)->orphan_meta_reserved = 0;
2254 release_rsv = 1;
2255 }
2256 spin_unlock(&root->orphan_lock);
2257
2258 if (trans && delete_item) {
2259 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
2260 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04002261 }
2262
Yan, Zhengd68fc572010-05-16 10:49:58 -04002263 if (release_rsv)
2264 btrfs_orphan_release_metadata(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002265
Yan, Zhengd68fc572010-05-16 10:49:58 -04002266 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002267}
2268
2269/*
2270 * this cleans up any orphans that may be left on the list from the last use
2271 * of this root.
2272 */
2273void btrfs_orphan_cleanup(struct btrfs_root *root)
2274{
2275 struct btrfs_path *path;
2276 struct extent_buffer *leaf;
2277 struct btrfs_item *item;
2278 struct btrfs_key key, found_key;
2279 struct btrfs_trans_handle *trans;
2280 struct inode *inode;
2281 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2282
Yan, Zhengd68fc572010-05-16 10:49:58 -04002283 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik7b128762008-07-24 12:17:14 -04002284 return;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002285
2286 path = btrfs_alloc_path();
2287 BUG_ON(!path);
Josef Bacik7b128762008-07-24 12:17:14 -04002288 path->reada = -1;
2289
2290 key.objectid = BTRFS_ORPHAN_OBJECTID;
2291 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2292 key.offset = (u64)-1;
2293
Josef Bacik7b128762008-07-24 12:17:14 -04002294 while (1) {
2295 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2296 if (ret < 0) {
2297 printk(KERN_ERR "Error searching slot for orphan: %d"
2298 "\n", ret);
2299 break;
2300 }
2301
2302 /*
2303 * if ret == 0 means we found what we were searching for, which
2304 * is weird, but possible, so only screw with path if we didnt
2305 * find the key and see if we have stuff that matches
2306 */
2307 if (ret > 0) {
2308 if (path->slots[0] == 0)
2309 break;
2310 path->slots[0]--;
2311 }
2312
2313 /* pull out the item */
2314 leaf = path->nodes[0];
2315 item = btrfs_item_nr(leaf, path->slots[0]);
2316 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2317
2318 /* make sure the item matches what we want */
2319 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2320 break;
2321 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2322 break;
2323
2324 /* release the path since we're done with it */
2325 btrfs_release_path(root, path);
2326
2327 /*
2328 * this is where we are basically btrfs_lookup, without the
2329 * crossing root thing. we store the inode number in the
2330 * offset of the orphan item.
2331 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002332 found_key.objectid = found_key.offset;
2333 found_key.type = BTRFS_INODE_ITEM_KEY;
2334 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002335 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002336 BUG_ON(IS_ERR(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04002337
Josef Bacik7b128762008-07-24 12:17:14 -04002338 /*
2339 * add this inode to the orphan list so btrfs_orphan_del does
2340 * the proper thing when we hit it
2341 */
Yan, Zhengd68fc572010-05-16 10:49:58 -04002342 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002343 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yan, Zhengd68fc572010-05-16 10:49:58 -04002344 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002345
2346 /*
2347 * if this is a bad inode, means we actually succeeded in
2348 * removing the inode, but not the orphan record, which means
2349 * we need to manually delete the orphan since iput will just
2350 * do a destroy_inode
2351 */
2352 if (is_bad_inode(inode)) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04002353 trans = btrfs_start_transaction(root, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04002354 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002355 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04002356 iput(inode);
2357 continue;
2358 }
2359
2360 /* if we have links, this was a truncate, lets do that */
2361 if (inode->i_nlink) {
2362 nr_truncate++;
2363 btrfs_truncate(inode);
2364 } else {
2365 nr_unlink++;
2366 }
2367
2368 /* this will do delete_inode and everything for us */
2369 iput(inode);
2370 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04002371 btrfs_free_path(path);
2372
2373 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2374
2375 if (root->orphan_block_rsv)
2376 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2377 (u64)-1);
2378
2379 if (root->orphan_block_rsv || root->orphan_item_inserted) {
2380 trans = btrfs_join_transaction(root, 1);
2381 btrfs_end_transaction(trans, root);
2382 }
Josef Bacik7b128762008-07-24 12:17:14 -04002383
2384 if (nr_unlink)
2385 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2386 if (nr_truncate)
2387 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
Josef Bacik7b128762008-07-24 12:17:14 -04002388}
2389
Chris Masond352ac62008-09-29 15:18:18 -04002390/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002391 * very simple check to peek ahead in the leaf looking for xattrs. If we
2392 * don't find any xattrs, we know there can't be any acls.
2393 *
2394 * slot is the slot the inode is in, objectid is the objectid of the inode
2395 */
2396static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2397 int slot, u64 objectid)
2398{
2399 u32 nritems = btrfs_header_nritems(leaf);
2400 struct btrfs_key found_key;
2401 int scanned = 0;
2402
2403 slot++;
2404 while (slot < nritems) {
2405 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2406
2407 /* we found a different objectid, there must not be acls */
2408 if (found_key.objectid != objectid)
2409 return 0;
2410
2411 /* we found an xattr, assume we've got an acl */
2412 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2413 return 1;
2414
2415 /*
2416 * we found a key greater than an xattr key, there can't
2417 * be any acls later on
2418 */
2419 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2420 return 0;
2421
2422 slot++;
2423 scanned++;
2424
2425 /*
2426 * it goes inode, inode backrefs, xattrs, extents,
2427 * so if there are a ton of hard links to an inode there can
2428 * be a lot of backrefs. Don't waste time searching too hard,
2429 * this is just an optimization
2430 */
2431 if (scanned >= 8)
2432 break;
2433 }
2434 /* we hit the end of the leaf before we found an xattr or
2435 * something larger than an xattr. We have to assume the inode
2436 * has acls
2437 */
2438 return 1;
2439}
2440
2441/*
Chris Masond352ac62008-09-29 15:18:18 -04002442 * read an inode from the btree into the in-memory inode
2443 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002444static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002445{
2446 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002447 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002448 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002449 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002450 struct btrfs_root *root = BTRFS_I(inode)->root;
2451 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002452 int maybe_acls;
Chris Mason39279cc2007-06-12 06:35:45 -04002453 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04002454 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002455 int ret;
2456
2457 path = btrfs_alloc_path();
2458 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002459 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002460
Chris Mason39279cc2007-06-12 06:35:45 -04002461 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002462 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002463 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002464
Chris Mason5f39d392007-10-15 16:14:19 -04002465 leaf = path->nodes[0];
2466 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2467 struct btrfs_inode_item);
2468
2469 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2470 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2471 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2472 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002473 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002474
2475 tspec = btrfs_inode_atime(inode_item);
2476 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2477 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2478
2479 tspec = btrfs_inode_mtime(inode_item);
2480 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2481 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2482
2483 tspec = btrfs_inode_ctime(inode_item);
2484 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2485 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2486
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002487 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002488 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002489 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002490 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002491 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002492 rdev = btrfs_inode_rdev(leaf, inode_item);
2493
Josef Bacikaec74772008-07-24 12:12:38 -04002494 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002495 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002496
Chris Mason5f39d392007-10-15 16:14:19 -04002497 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002498
Chris Mason46a53cc2009-04-27 11:47:50 -04002499 /*
2500 * try to precache a NULL acl entry for files that don't have
2501 * any xattrs or acls
2502 */
2503 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
Al Viro72c04902009-06-24 16:58:48 -04002504 if (!maybe_acls)
2505 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002506
Yan Zhengd2fb3432008-12-11 16:30:39 -05002507 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2508 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002509 btrfs_free_path(path);
2510 inode_item = NULL;
2511
Chris Mason39279cc2007-06-12 06:35:45 -04002512 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002513 case S_IFREG:
2514 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002515 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002516 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002517 inode->i_fop = &btrfs_file_operations;
2518 inode->i_op = &btrfs_file_inode_operations;
2519 break;
2520 case S_IFDIR:
2521 inode->i_fop = &btrfs_dir_file_operations;
2522 if (root == root->fs_info->tree_root)
2523 inode->i_op = &btrfs_dir_ro_inode_operations;
2524 else
2525 inode->i_op = &btrfs_dir_inode_operations;
2526 break;
2527 case S_IFLNK:
2528 inode->i_op = &btrfs_symlink_inode_operations;
2529 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002530 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002531 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002532 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002533 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002534 init_special_inode(inode, inode->i_mode, rdev);
2535 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002536 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002537
2538 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002539 return;
2540
2541make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002542 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002543 make_bad_inode(inode);
2544}
2545
Chris Masond352ac62008-09-29 15:18:18 -04002546/*
2547 * given a leaf and an inode, copy the inode fields into the leaf
2548 */
Chris Masone02119d2008-09-05 16:13:11 -04002549static void fill_inode_item(struct btrfs_trans_handle *trans,
2550 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002551 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002552 struct inode *inode)
2553{
Chris Mason5f39d392007-10-15 16:14:19 -04002554 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2555 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002556 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002557 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2558 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2559
2560 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2561 inode->i_atime.tv_sec);
2562 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2563 inode->i_atime.tv_nsec);
2564
2565 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2566 inode->i_mtime.tv_sec);
2567 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2568 inode->i_mtime.tv_nsec);
2569
2570 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2571 inode->i_ctime.tv_sec);
2572 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2573 inode->i_ctime.tv_nsec);
2574
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002575 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002576 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002577 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002578 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002579 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002580 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002581 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Chris Mason39279cc2007-06-12 06:35:45 -04002582}
2583
Chris Masond352ac62008-09-29 15:18:18 -04002584/*
2585 * copy everything in the in-memory inode into the btree.
2586 */
Chris Masond3977122009-01-05 21:25:51 -05002587noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2588 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002589{
2590 struct btrfs_inode_item *inode_item;
2591 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002592 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002593 int ret;
2594
2595 path = btrfs_alloc_path();
2596 BUG_ON(!path);
Chris Masonb9473432009-03-13 11:00:37 -04002597 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002598 ret = btrfs_lookup_inode(trans, root, path,
2599 &BTRFS_I(inode)->location, 1);
2600 if (ret) {
2601 if (ret > 0)
2602 ret = -ENOENT;
2603 goto failed;
2604 }
2605
Chris Masonb4ce94d2009-02-04 09:25:08 -05002606 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002607 leaf = path->nodes[0];
2608 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002609 struct btrfs_inode_item);
2610
Chris Masone02119d2008-09-05 16:13:11 -04002611 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002612 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002613 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002614 ret = 0;
2615failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002616 btrfs_free_path(path);
2617 return ret;
2618}
2619
2620
Chris Masond352ac62008-09-29 15:18:18 -04002621/*
2622 * unlink helper that gets used here in inode.c and in the tree logging
2623 * recovery code. It remove a link in a directory with a given name, and
2624 * also drops the back refs in the inode to the directory
2625 */
Chris Masone02119d2008-09-05 16:13:11 -04002626int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2627 struct btrfs_root *root,
2628 struct inode *dir, struct inode *inode,
2629 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002630{
2631 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002632 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002633 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002634 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002635 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002636 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04002637
2638 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002639 if (!path) {
2640 ret = -ENOMEM;
2641 goto err;
2642 }
2643
Chris Masonb9473432009-03-13 11:00:37 -04002644 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002645 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2646 name, name_len, -1);
2647 if (IS_ERR(di)) {
2648 ret = PTR_ERR(di);
2649 goto err;
2650 }
2651 if (!di) {
2652 ret = -ENOENT;
2653 goto err;
2654 }
Chris Mason5f39d392007-10-15 16:14:19 -04002655 leaf = path->nodes[0];
2656 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002657 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002658 if (ret)
2659 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002660 btrfs_release_path(root, path);
2661
Josef Bacikaec74772008-07-24 12:12:38 -04002662 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04002663 inode->i_ino,
2664 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002665 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002666 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Josef Bacikaec74772008-07-24 12:12:38 -04002667 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04002668 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002669 goto err;
2670 }
2671
Chris Mason39279cc2007-06-12 06:35:45 -04002672 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002673 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002674 if (IS_ERR(di)) {
2675 ret = PTR_ERR(di);
2676 goto err;
2677 }
2678 if (!di) {
2679 ret = -ENOENT;
2680 goto err;
2681 }
2682 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002683 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002684
Chris Masone02119d2008-09-05 16:13:11 -04002685 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2686 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002687 BUG_ON(ret != 0 && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04002688
2689 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2690 dir, index);
2691 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002692err:
2693 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002694 if (ret)
2695 goto out;
2696
2697 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2698 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2699 btrfs_update_inode(trans, root, dir);
2700 btrfs_drop_nlink(inode);
2701 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002702out:
Chris Mason39279cc2007-06-12 06:35:45 -04002703 return ret;
2704}
2705
Yan, Zhenga22285a2010-05-16 10:48:46 -04002706/* helper to check if there is any shared block in the path */
2707static int check_path_shared(struct btrfs_root *root,
2708 struct btrfs_path *path)
2709{
2710 struct extent_buffer *eb;
2711 int level;
2712 int ret;
Dan Carpenter0e4dcbef2010-06-01 08:23:11 +00002713 u64 refs = 1;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002714
2715 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2716 if (!path->nodes[level])
2717 break;
2718 eb = path->nodes[level];
2719 if (!btrfs_block_can_be_shared(root, eb))
2720 continue;
2721 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2722 &refs, NULL);
2723 if (refs > 1)
2724 return 1;
2725 }
2726 return 0;
2727}
2728
2729/*
2730 * helper to start transaction for unlink and rmdir.
2731 *
2732 * unlink and rmdir are special in btrfs, they do not always free space.
2733 * so in enospc case, we should make sure they will free space before
2734 * allowing them to use the global metadata reservation.
2735 */
2736static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2737 struct dentry *dentry)
2738{
2739 struct btrfs_trans_handle *trans;
2740 struct btrfs_root *root = BTRFS_I(dir)->root;
2741 struct btrfs_path *path;
2742 struct btrfs_inode_ref *ref;
2743 struct btrfs_dir_item *di;
2744 struct inode *inode = dentry->d_inode;
2745 u64 index;
2746 int check_link = 1;
2747 int err = -ENOSPC;
2748 int ret;
2749
2750 trans = btrfs_start_transaction(root, 10);
2751 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2752 return trans;
2753
2754 if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
2755 return ERR_PTR(-ENOSPC);
2756
2757 /* check if there is someone else holds reference */
2758 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2759 return ERR_PTR(-ENOSPC);
2760
2761 if (atomic_read(&inode->i_count) > 2)
2762 return ERR_PTR(-ENOSPC);
2763
2764 if (xchg(&root->fs_info->enospc_unlink, 1))
2765 return ERR_PTR(-ENOSPC);
2766
2767 path = btrfs_alloc_path();
2768 if (!path) {
2769 root->fs_info->enospc_unlink = 0;
2770 return ERR_PTR(-ENOMEM);
2771 }
2772
2773 trans = btrfs_start_transaction(root, 0);
2774 if (IS_ERR(trans)) {
2775 btrfs_free_path(path);
2776 root->fs_info->enospc_unlink = 0;
2777 return trans;
2778 }
2779
2780 path->skip_locking = 1;
2781 path->search_commit_root = 1;
2782
2783 ret = btrfs_lookup_inode(trans, root, path,
2784 &BTRFS_I(dir)->location, 0);
2785 if (ret < 0) {
2786 err = ret;
2787 goto out;
2788 }
2789 if (ret == 0) {
2790 if (check_path_shared(root, path))
2791 goto out;
2792 } else {
2793 check_link = 0;
2794 }
2795 btrfs_release_path(root, path);
2796
2797 ret = btrfs_lookup_inode(trans, root, path,
2798 &BTRFS_I(inode)->location, 0);
2799 if (ret < 0) {
2800 err = ret;
2801 goto out;
2802 }
2803 if (ret == 0) {
2804 if (check_path_shared(root, path))
2805 goto out;
2806 } else {
2807 check_link = 0;
2808 }
2809 btrfs_release_path(root, path);
2810
2811 if (ret == 0 && S_ISREG(inode->i_mode)) {
2812 ret = btrfs_lookup_file_extent(trans, root, path,
2813 inode->i_ino, (u64)-1, 0);
2814 if (ret < 0) {
2815 err = ret;
2816 goto out;
2817 }
2818 BUG_ON(ret == 0);
2819 if (check_path_shared(root, path))
2820 goto out;
2821 btrfs_release_path(root, path);
2822 }
2823
2824 if (!check_link) {
2825 err = 0;
2826 goto out;
2827 }
2828
2829 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2830 dentry->d_name.name, dentry->d_name.len, 0);
2831 if (IS_ERR(di)) {
2832 err = PTR_ERR(di);
2833 goto out;
2834 }
2835 if (di) {
2836 if (check_path_shared(root, path))
2837 goto out;
2838 } else {
2839 err = 0;
2840 goto out;
2841 }
2842 btrfs_release_path(root, path);
2843
2844 ref = btrfs_lookup_inode_ref(trans, root, path,
2845 dentry->d_name.name, dentry->d_name.len,
2846 inode->i_ino, dir->i_ino, 0);
2847 if (IS_ERR(ref)) {
2848 err = PTR_ERR(ref);
2849 goto out;
2850 }
2851 BUG_ON(!ref);
2852 if (check_path_shared(root, path))
2853 goto out;
2854 index = btrfs_inode_ref_index(path->nodes[0], ref);
2855 btrfs_release_path(root, path);
2856
2857 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index,
2858 dentry->d_name.name, dentry->d_name.len, 0);
2859 if (IS_ERR(di)) {
2860 err = PTR_ERR(di);
2861 goto out;
2862 }
2863 BUG_ON(ret == -ENOENT);
2864 if (check_path_shared(root, path))
2865 goto out;
2866
2867 err = 0;
2868out:
2869 btrfs_free_path(path);
2870 if (err) {
2871 btrfs_end_transaction(trans, root);
2872 root->fs_info->enospc_unlink = 0;
2873 return ERR_PTR(err);
2874 }
2875
2876 trans->block_rsv = &root->fs_info->global_block_rsv;
2877 return trans;
2878}
2879
2880static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2881 struct btrfs_root *root)
2882{
2883 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
2884 BUG_ON(!root->fs_info->enospc_unlink);
2885 root->fs_info->enospc_unlink = 0;
2886 }
2887 btrfs_end_transaction_throttle(trans, root);
2888}
2889
Chris Mason39279cc2007-06-12 06:35:45 -04002890static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2891{
Yan, Zhenga22285a2010-05-16 10:48:46 -04002892 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002893 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002894 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002895 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002896 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002897
Yan, Zhenga22285a2010-05-16 10:48:46 -04002898 trans = __unlink_start_trans(dir, dentry);
2899 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002900 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04002901
Chris Mason39279cc2007-06-12 06:35:45 -04002902 btrfs_set_trans_block_group(trans, dir);
Chris Mason12fcfd22009-03-24 10:24:20 -04002903
2904 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2905
Chris Masone02119d2008-09-05 16:13:11 -04002906 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2907 dentry->d_name.name, dentry->d_name.len);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002908 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04002909
Yan, Zhenga22285a2010-05-16 10:48:46 -04002910 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04002911 ret = btrfs_orphan_add(trans, inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002912 BUG_ON(ret);
2913 }
Josef Bacik7b128762008-07-24 12:17:14 -04002914
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002915 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002916 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002917 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002918 return ret;
2919}
2920
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002921int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2922 struct btrfs_root *root,
2923 struct inode *dir, u64 objectid,
2924 const char *name, int name_len)
2925{
2926 struct btrfs_path *path;
2927 struct extent_buffer *leaf;
2928 struct btrfs_dir_item *di;
2929 struct btrfs_key key;
2930 u64 index;
2931 int ret;
2932
2933 path = btrfs_alloc_path();
2934 if (!path)
2935 return -ENOMEM;
2936
2937 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2938 name, name_len, -1);
2939 BUG_ON(!di || IS_ERR(di));
2940
2941 leaf = path->nodes[0];
2942 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2943 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2944 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2945 BUG_ON(ret);
2946 btrfs_release_path(root, path);
2947
2948 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2949 objectid, root->root_key.objectid,
2950 dir->i_ino, &index, name, name_len);
2951 if (ret < 0) {
2952 BUG_ON(ret != -ENOENT);
2953 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
2954 name, name_len);
2955 BUG_ON(!di || IS_ERR(di));
2956
2957 leaf = path->nodes[0];
2958 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2959 btrfs_release_path(root, path);
2960 index = key.offset;
2961 }
2962
2963 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2964 index, name, name_len, -1);
2965 BUG_ON(!di || IS_ERR(di));
2966
2967 leaf = path->nodes[0];
2968 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2969 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2970 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2971 BUG_ON(ret);
2972 btrfs_release_path(root, path);
2973
2974 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2975 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2976 ret = btrfs_update_inode(trans, root, dir);
2977 BUG_ON(ret);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002978
2979 btrfs_free_path(path);
2980 return 0;
2981}
2982
Chris Mason39279cc2007-06-12 06:35:45 -04002983static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2984{
2985 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002986 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002987 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002988 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05002989 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002990
Chris Mason3394e162008-11-17 20:42:26 -05002991 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002992 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04002993 return -ENOTEMPTY;
2994
Yan, Zhenga22285a2010-05-16 10:48:46 -04002995 trans = __unlink_start_trans(dir, dentry);
2996 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002997 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002998
Chris Mason39279cc2007-06-12 06:35:45 -04002999 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003000
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003001 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
3002 err = btrfs_unlink_subvol(trans, root, dir,
3003 BTRFS_I(inode)->location.objectid,
3004 dentry->d_name.name,
3005 dentry->d_name.len);
3006 goto out;
3007 }
3008
Josef Bacik7b128762008-07-24 12:17:14 -04003009 err = btrfs_orphan_add(trans, inode);
3010 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003011 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003012
Chris Mason39279cc2007-06-12 06:35:45 -04003013 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04003014 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3015 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05003016 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04003017 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003018out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003019 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003020 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003021 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05003022
Chris Mason39279cc2007-06-12 06:35:45 -04003023 return err;
3024}
3025
Chris Masond20f7042008-12-08 16:58:54 -05003026#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04003027/*
Chris Mason323ac952008-10-01 19:05:46 -04003028 * when truncating bytes in a file, it is possible to avoid reading
3029 * the leaves that contain only checksum items. This can be the
3030 * majority of the IO required to delete a large file, but it must
3031 * be done carefully.
3032 *
3033 * The keys in the level just above the leaves are checked to make sure
3034 * the lowest key in a given leaf is a csum key, and starts at an offset
3035 * after the new size.
3036 *
3037 * Then the key for the next leaf is checked to make sure it also has
3038 * a checksum item for the same file. If it does, we know our target leaf
3039 * contains only checksum items, and it can be safely freed without reading
3040 * it.
3041 *
3042 * This is just an optimization targeted at large files. It may do
3043 * nothing. It will return 0 unless things went badly.
3044 */
3045static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
3046 struct btrfs_root *root,
3047 struct btrfs_path *path,
3048 struct inode *inode, u64 new_size)
3049{
3050 struct btrfs_key key;
3051 int ret;
3052 int nritems;
3053 struct btrfs_key found_key;
3054 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003055 struct btrfs_leaf_ref *ref;
3056 u64 leaf_gen;
3057 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04003058
3059 path->lowest_level = 1;
3060 key.objectid = inode->i_ino;
3061 key.type = BTRFS_CSUM_ITEM_KEY;
3062 key.offset = new_size;
3063again:
3064 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3065 if (ret < 0)
3066 goto out;
3067
3068 if (path->nodes[1] == NULL) {
3069 ret = 0;
3070 goto out;
3071 }
3072 ret = 0;
3073 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
3074 nritems = btrfs_header_nritems(path->nodes[1]);
3075
3076 if (!nritems)
3077 goto out;
3078
3079 if (path->slots[1] >= nritems)
3080 goto next_node;
3081
3082 /* did we find a key greater than anything we want to delete? */
3083 if (found_key.objectid > inode->i_ino ||
3084 (found_key.objectid == inode->i_ino && found_key.type > key.type))
3085 goto out;
3086
3087 /* we check the next key in the node to make sure the leave contains
3088 * only checksum items. This comparison doesn't work if our
3089 * leaf is the last one in the node
3090 */
3091 if (path->slots[1] + 1 >= nritems) {
3092next_node:
3093 /* search forward from the last key in the node, this
3094 * will bring us into the next node in the tree
3095 */
3096 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
3097
3098 /* unlikely, but we inc below, so check to be safe */
3099 if (found_key.offset == (u64)-1)
3100 goto out;
3101
3102 /* search_forward needs a path with locks held, do the
3103 * search again for the original key. It is possible
3104 * this will race with a balance and return a path that
3105 * we could modify, but this drop is just an optimization
3106 * and is allowed to miss some leaves.
3107 */
3108 btrfs_release_path(root, path);
3109 found_key.offset++;
3110
3111 /* setup a max key for search_forward */
3112 other_key.offset = (u64)-1;
3113 other_key.type = key.type;
3114 other_key.objectid = key.objectid;
3115
3116 path->keep_locks = 1;
3117 ret = btrfs_search_forward(root, &found_key, &other_key,
3118 path, 0, 0);
3119 path->keep_locks = 0;
3120 if (ret || found_key.objectid != key.objectid ||
3121 found_key.type != key.type) {
3122 ret = 0;
3123 goto out;
3124 }
3125
3126 key.offset = found_key.offset;
3127 btrfs_release_path(root, path);
3128 cond_resched();
3129 goto again;
3130 }
3131
3132 /* we know there's one more slot after us in the tree,
3133 * read that key so we can verify it is also a checksum item
3134 */
3135 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
3136
3137 if (found_key.objectid < inode->i_ino)
3138 goto next_key;
3139
3140 if (found_key.type != key.type || found_key.offset < new_size)
3141 goto next_key;
3142
3143 /*
3144 * if the key for the next leaf isn't a csum key from this objectid,
3145 * we can't be sure there aren't good items inside this leaf.
3146 * Bail out
3147 */
3148 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
3149 goto out;
3150
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003151 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
3152 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04003153 /*
3154 * it is safe to delete this leaf, it contains only
3155 * csum items from this inode at an offset >= new_size
3156 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003157 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04003158 BUG_ON(ret);
3159
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003160 if (root->ref_cows && leaf_gen < trans->transid) {
3161 ref = btrfs_alloc_leaf_ref(root, 0);
3162 if (ref) {
3163 ref->root_gen = root->root_key.offset;
3164 ref->bytenr = leaf_start;
3165 ref->owner = 0;
3166 ref->generation = leaf_gen;
3167 ref->nritems = 0;
3168
Chris Masonbd56b302009-02-04 09:27:02 -05003169 btrfs_sort_leaf_ref(ref);
3170
Yan Zheng5b84e8d2008-10-09 11:46:19 -04003171 ret = btrfs_add_leaf_ref(root, ref, 0);
3172 WARN_ON(ret);
3173 btrfs_free_leaf_ref(root, ref);
3174 } else {
3175 WARN_ON(1);
3176 }
3177 }
Chris Mason323ac952008-10-01 19:05:46 -04003178next_key:
3179 btrfs_release_path(root, path);
3180
3181 if (other_key.objectid == inode->i_ino &&
3182 other_key.type == key.type && other_key.offset > key.offset) {
3183 key.offset = other_key.offset;
3184 cond_resched();
3185 goto again;
3186 }
3187 ret = 0;
3188out:
3189 /* fixup any changes we've made to the path */
3190 path->lowest_level = 0;
3191 path->keep_locks = 0;
3192 btrfs_release_path(root, path);
3193 return ret;
3194}
3195
Chris Masond20f7042008-12-08 16:58:54 -05003196#endif
3197
Chris Mason323ac952008-10-01 19:05:46 -04003198/*
Chris Mason39279cc2007-06-12 06:35:45 -04003199 * this can truncate away extent items, csum items and directory items.
3200 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04003201 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04003202 *
3203 * csum items that cross the new i_size are truncated to the new size
3204 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04003205 *
3206 * min_type is the minimum key type to truncate down to. If set to 0, this
3207 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04003208 */
Yan, Zheng80825102009-11-12 09:35:36 +00003209int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3210 struct btrfs_root *root,
3211 struct inode *inode,
3212 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003213{
Chris Mason39279cc2007-06-12 06:35:45 -04003214 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003215 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003216 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00003217 struct btrfs_key key;
3218 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003219 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04003220 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003221 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003222 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00003223 u64 mask = root->sectorsize - 1;
3224 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04003225 int found_extent;
3226 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05003227 int pending_del_nr = 0;
3228 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04003229 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05003230 int encoding;
Yan, Zheng80825102009-11-12 09:35:36 +00003231 int ret;
3232 int err = 0;
3233
3234 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003235
Josef Bacik0af3d002010-06-21 14:48:16 -04003236 if (root->ref_cows || root == root->fs_info->tree_root)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003237 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003238
Chris Mason39279cc2007-06-12 06:35:45 -04003239 path = btrfs_alloc_path();
3240 BUG_ON(!path);
Julia Lawall33c17ad2009-07-22 16:49:01 -04003241 path->reada = -1;
Chris Mason5f39d392007-10-15 16:14:19 -04003242
Chris Mason39279cc2007-06-12 06:35:45 -04003243 key.objectid = inode->i_ino;
3244 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003245 key.type = (u8)-1;
3246
Chris Mason85e21ba2008-01-29 15:11:36 -05003247search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003248 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003249 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003250 if (ret < 0) {
3251 err = ret;
3252 goto out;
3253 }
Chris Masond3977122009-01-05 21:25:51 -05003254
Chris Mason85e21ba2008-01-29 15:11:36 -05003255 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003256 /* there are no items in the tree for us to truncate, we're
3257 * done
3258 */
Yan, Zheng80825102009-11-12 09:35:36 +00003259 if (path->slots[0] == 0)
3260 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003261 path->slots[0]--;
3262 }
3263
Chris Masond3977122009-01-05 21:25:51 -05003264 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003265 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003266 leaf = path->nodes[0];
3267 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3268 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05003269 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003270
Chris Mason5f39d392007-10-15 16:14:19 -04003271 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003272 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003273
Chris Mason85e21ba2008-01-29 15:11:36 -05003274 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003275 break;
3276
Chris Mason5f39d392007-10-15 16:14:19 -04003277 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003278 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003279 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003280 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003281 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003282 encoding = btrfs_file_extent_compression(leaf, fi);
3283 encoding |= btrfs_file_extent_encryption(leaf, fi);
3284 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
3285
Chris Mason179e29e2007-11-01 11:28:41 -04003286 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003287 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003288 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003289 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003290 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003291 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003292 }
Yan008630c2007-11-07 13:31:09 -05003293 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003294 }
Yan, Zheng80825102009-11-12 09:35:36 +00003295 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003296 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003297 } else {
3298 if (item_end < new_size)
3299 break;
3300 if (found_key.offset >= new_size)
3301 del_item = 1;
3302 else
3303 del_item = 0;
3304 }
Chris Mason39279cc2007-06-12 06:35:45 -04003305 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003306 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003307 if (found_type != BTRFS_EXTENT_DATA_KEY)
3308 goto delete;
3309
3310 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003311 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003312 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003313 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04003314 u64 orig_num_bytes =
3315 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003316 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003317 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003318 extent_num_bytes = extent_num_bytes &
3319 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003320 btrfs_set_file_extent_num_bytes(leaf, fi,
3321 extent_num_bytes);
3322 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003323 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003324 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003325 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003326 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003327 } else {
Chris Masondb945352007-10-15 16:15:53 -04003328 extent_num_bytes =
3329 btrfs_file_extent_disk_num_bytes(leaf,
3330 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003331 extent_offset = found_key.offset -
3332 btrfs_file_extent_offset(leaf, fi);
3333
Chris Mason39279cc2007-06-12 06:35:45 -04003334 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003335 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003336 if (extent_start != 0) {
3337 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003338 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003339 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003340 }
3341 }
Chris Mason90692182008-02-08 13:49:28 -05003342 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003343 /*
3344 * we can't truncate inline items that have had
3345 * special encodings
3346 */
3347 if (!del_item &&
3348 btrfs_file_extent_compression(leaf, fi) == 0 &&
3349 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3350 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003351 u32 size = new_size - found_key.offset;
3352
3353 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003354 inode_sub_bytes(inode, item_end + 1 -
3355 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003356 }
3357 size =
3358 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05003359 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04003360 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05003361 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04003362 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003363 inode_sub_bytes(inode, item_end + 1 -
3364 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003365 }
Chris Mason39279cc2007-06-12 06:35:45 -04003366 }
Chris Mason179e29e2007-11-01 11:28:41 -04003367delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003368 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003369 if (!pending_del_nr) {
3370 /* no pending yet, add ourselves */
3371 pending_del_slot = path->slots[0];
3372 pending_del_nr = 1;
3373 } else if (pending_del_nr &&
3374 path->slots[0] + 1 == pending_del_slot) {
3375 /* hop on the pending chunk */
3376 pending_del_nr++;
3377 pending_del_slot = path->slots[0];
3378 } else {
Chris Masond3977122009-01-05 21:25:51 -05003379 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003380 }
Chris Mason39279cc2007-06-12 06:35:45 -04003381 } else {
3382 break;
3383 }
Josef Bacik0af3d002010-06-21 14:48:16 -04003384 if (found_extent && (root->ref_cows ||
3385 root == root->fs_info->tree_root)) {
Chris Masonb9473432009-03-13 11:00:37 -04003386 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003387 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003388 extent_num_bytes, 0,
3389 btrfs_header_owner(leaf),
3390 inode->i_ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04003391 BUG_ON(ret);
3392 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003393
Yan, Zheng80825102009-11-12 09:35:36 +00003394 if (found_type == BTRFS_INODE_ITEM_KEY)
3395 break;
3396
3397 if (path->slots[0] == 0 ||
3398 path->slots[0] != pending_del_slot) {
3399 if (root->ref_cows) {
3400 err = -EAGAIN;
3401 goto out;
3402 }
3403 if (pending_del_nr) {
3404 ret = btrfs_del_items(trans, root, path,
3405 pending_del_slot,
3406 pending_del_nr);
3407 BUG_ON(ret);
3408 pending_del_nr = 0;
3409 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003410 btrfs_release_path(root, path);
3411 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003412 } else {
3413 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003414 }
Chris Mason39279cc2007-06-12 06:35:45 -04003415 }
Yan, Zheng80825102009-11-12 09:35:36 +00003416out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003417 if (pending_del_nr) {
3418 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3419 pending_del_nr);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003420 BUG_ON(ret);
Chris Mason85e21ba2008-01-29 15:11:36 -05003421 }
Chris Mason39279cc2007-06-12 06:35:45 -04003422 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003423 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003424}
3425
Chris Masona52d9a82007-08-27 16:49:44 -04003426/*
3427 * taken from block_truncate_page, but does cow as it zeros out
3428 * any bytes left in the last page in the file.
3429 */
3430static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3431{
3432 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003433 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003434 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3435 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003436 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003437 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003438 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003439 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3440 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3441 struct page *page;
3442 int ret = 0;
3443 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003444 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003445
3446 if ((offset & (blocksize - 1)) == 0)
3447 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003448 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003449 if (ret)
3450 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003451
3452 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003453again:
Chris Masona52d9a82007-08-27 16:49:44 -04003454 page = grab_cache_page(mapping, index);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003455 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003456 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04003457 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003458 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003459
3460 page_start = page_offset(page);
3461 page_end = page_start + PAGE_CACHE_SIZE - 1;
3462
Chris Masona52d9a82007-08-27 16:49:44 -04003463 if (!PageUptodate(page)) {
3464 ret = btrfs_readpage(NULL, page);
3465 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003466 if (page->mapping != mapping) {
3467 unlock_page(page);
3468 page_cache_release(page);
3469 goto again;
3470 }
Chris Masona52d9a82007-08-27 16:49:44 -04003471 if (!PageUptodate(page)) {
3472 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003473 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003474 }
3475 }
Chris Mason211c17f2008-05-15 09:13:45 -04003476 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003477
Josef Bacik2ac55d42010-02-03 19:33:23 +00003478 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
3479 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003480 set_page_extent_mapped(page);
3481
3482 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3483 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003484 unlock_extent_cached(io_tree, page_start, page_end,
3485 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003486 unlock_page(page);
3487 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003488 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003489 btrfs_put_ordered_extent(ordered);
3490 goto again;
3491 }
3492
Josef Bacik2ac55d42010-02-03 19:33:23 +00003493 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik5d5e1032009-10-13 16:46:49 -04003494 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003495 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003496
Josef Bacik2ac55d42010-02-03 19:33:23 +00003497 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3498 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003499 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003500 unlock_extent_cached(io_tree, page_start, page_end,
3501 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003502 goto out_unlock;
3503 }
3504
Chris Masone6dcd2d2008-07-17 12:53:50 -04003505 ret = 0;
3506 if (offset != PAGE_CACHE_SIZE) {
3507 kaddr = kmap(page);
3508 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3509 flush_dcache_page(page);
3510 kunmap(page);
3511 }
Chris Mason247e7432008-07-17 12:53:51 -04003512 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003513 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003514 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3515 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003516
Chris Mason89642222008-07-24 09:41:53 -04003517out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003518 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003519 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003520 unlock_page(page);
3521 page_cache_release(page);
3522out:
3523 return ret;
3524}
3525
Yan Zheng9036c102008-10-30 14:19:41 -04003526int btrfs_cont_expand(struct inode *inode, loff_t size)
3527{
3528 struct btrfs_trans_handle *trans;
3529 struct btrfs_root *root = BTRFS_I(inode)->root;
3530 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003531 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003532 struct extent_state *cached_state = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003533 u64 mask = root->sectorsize - 1;
3534 u64 hole_start = (inode->i_size + mask) & ~mask;
3535 u64 block_end = (size + mask) & ~mask;
3536 u64 last_byte;
3537 u64 cur_offset;
3538 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003539 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003540
3541 if (size <= hole_start)
3542 return 0;
3543
Yan Zheng9036c102008-10-30 14:19:41 -04003544 while (1) {
3545 struct btrfs_ordered_extent *ordered;
3546 btrfs_wait_ordered_range(inode, hole_start,
3547 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003548 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
3549 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003550 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3551 if (!ordered)
3552 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003553 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3554 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003555 btrfs_put_ordered_extent(ordered);
3556 }
3557
Yan Zheng9036c102008-10-30 14:19:41 -04003558 cur_offset = hole_start;
3559 while (1) {
3560 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3561 block_end - cur_offset, 0);
3562 BUG_ON(IS_ERR(em) || !em);
3563 last_byte = min(extent_map_end(em), block_end);
3564 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003565 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003566 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003567 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003568
Yan, Zhenga22285a2010-05-16 10:48:46 -04003569 trans = btrfs_start_transaction(root, 2);
3570 if (IS_ERR(trans)) {
3571 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003572 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003573 }
Yan, Zheng80825102009-11-12 09:35:36 +00003574 btrfs_set_trans_block_group(trans, inode);
3575
3576 err = btrfs_drop_extents(trans, inode, cur_offset,
3577 cur_offset + hole_size,
3578 &hint_byte, 1);
3579 BUG_ON(err);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003580
Yan Zheng9036c102008-10-30 14:19:41 -04003581 err = btrfs_insert_file_extent(trans, root,
3582 inode->i_ino, cur_offset, 0,
3583 0, hole_size, 0, hole_size,
3584 0, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003585 BUG_ON(err);
3586
Yan Zheng9036c102008-10-30 14:19:41 -04003587 btrfs_drop_extent_cache(inode, hole_start,
3588 last_byte - 1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003589
3590 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003591 }
3592 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003593 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003594 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003595 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003596 break;
3597 }
3598
Yan, Zhenga22285a2010-05-16 10:48:46 -04003599 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003600 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3601 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003602 return err;
3603}
3604
Yan, Zheng80825102009-11-12 09:35:36 +00003605static int btrfs_setattr_size(struct inode *inode, struct iattr *attr)
3606{
3607 struct btrfs_root *root = BTRFS_I(inode)->root;
3608 struct btrfs_trans_handle *trans;
3609 unsigned long nr;
3610 int ret;
3611
3612 if (attr->ia_size == inode->i_size)
3613 return 0;
3614
3615 if (attr->ia_size > inode->i_size) {
3616 unsigned long limit;
3617 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
3618 if (attr->ia_size > inode->i_sb->s_maxbytes)
3619 return -EFBIG;
3620 if (limit != RLIM_INFINITY && attr->ia_size > limit) {
3621 send_sig(SIGXFSZ, current, 0);
3622 return -EFBIG;
3623 }
3624 }
3625
Yan, Zhengd68fc572010-05-16 10:49:58 -04003626 trans = btrfs_start_transaction(root, 5);
3627 if (IS_ERR(trans))
3628 return PTR_ERR(trans);
Yan, Zheng80825102009-11-12 09:35:36 +00003629
Yan, Zheng80825102009-11-12 09:35:36 +00003630 btrfs_set_trans_block_group(trans, inode);
3631
3632 ret = btrfs_orphan_add(trans, inode);
3633 BUG_ON(ret);
3634
3635 nr = trans->blocks_used;
3636 btrfs_end_transaction(trans, root);
Yan, Zheng80825102009-11-12 09:35:36 +00003637 btrfs_btree_balance_dirty(root, nr);
3638
3639 if (attr->ia_size > inode->i_size) {
3640 ret = btrfs_cont_expand(inode, attr->ia_size);
3641 if (ret) {
3642 btrfs_truncate(inode);
3643 return ret;
3644 }
3645
3646 i_size_write(inode, attr->ia_size);
3647 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
3648
Yan, Zhengd68fc572010-05-16 10:49:58 -04003649 trans = btrfs_start_transaction(root, 0);
3650 BUG_ON(IS_ERR(trans));
Yan, Zheng80825102009-11-12 09:35:36 +00003651 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003652 trans->block_rsv = root->orphan_block_rsv;
3653 BUG_ON(!trans->block_rsv);
Yan, Zheng80825102009-11-12 09:35:36 +00003654
3655 ret = btrfs_update_inode(trans, root, inode);
3656 BUG_ON(ret);
3657 if (inode->i_nlink > 0) {
3658 ret = btrfs_orphan_del(trans, inode);
3659 BUG_ON(ret);
3660 }
3661 nr = trans->blocks_used;
3662 btrfs_end_transaction(trans, root);
3663 btrfs_btree_balance_dirty(root, nr);
3664 return 0;
3665 }
3666
3667 /*
3668 * We're truncating a file that used to have good data down to
3669 * zero. Make sure it gets into the ordered flush list so that
3670 * any new writes get down to disk quickly.
3671 */
3672 if (attr->ia_size == 0)
3673 BTRFS_I(inode)->ordered_data_close = 1;
3674
3675 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3676 ret = vmtruncate(inode, attr->ia_size);
3677 BUG_ON(ret);
3678
3679 return 0;
3680}
3681
Chris Mason39279cc2007-06-12 06:35:45 -04003682static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3683{
3684 struct inode *inode = dentry->d_inode;
3685 int err;
3686
3687 err = inode_change_ok(inode, attr);
3688 if (err)
3689 return err;
3690
Chris Mason5a3f23d2009-03-31 13:27:11 -04003691 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Yan, Zheng80825102009-11-12 09:35:36 +00003692 err = btrfs_setattr_size(inode, attr);
3693 if (err)
3694 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003695 }
Yan Zheng9036c102008-10-30 14:19:41 -04003696
Christoph Hellwig10257742010-06-04 11:30:02 +02003697 if (attr->ia_valid) {
3698 setattr_copy(inode, attr);
3699 mark_inode_dirty(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04003700
Christoph Hellwig10257742010-06-04 11:30:02 +02003701 if (attr->ia_valid & ATTR_MODE)
3702 err = btrfs_acl_chmod(inode);
3703 }
3704
Chris Mason39279cc2007-06-12 06:35:45 -04003705 return err;
3706}
Chris Mason61295eb2008-01-14 16:24:38 -05003707
Al Virobd555972010-06-07 11:35:40 -04003708void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003709{
3710 struct btrfs_trans_handle *trans;
3711 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003712 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003713 int ret;
3714
3715 truncate_inode_pages(&inode->i_data, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -04003716 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
3717 root == root->fs_info->tree_root))
Al Virobd555972010-06-07 11:35:40 -04003718 goto no_delete;
3719
Chris Mason39279cc2007-06-12 06:35:45 -04003720 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003721 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003722 goto no_delete;
3723 }
Al Virobd555972010-06-07 11:35:40 -04003724 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
Chris Mason4a096752008-07-21 10:29:44 -04003725 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003726
Yan, Zhengc71bf092009-11-12 09:34:40 +00003727 if (root->fs_info->log_root_recovering) {
3728 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3729 goto no_delete;
3730 }
3731
Yan, Zheng76dda932009-09-21 16:00:26 -04003732 if (inode->i_nlink > 0) {
3733 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3734 goto no_delete;
3735 }
3736
Chris Masondbe674a2008-07-17 12:54:05 -04003737 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003738
Yan, Zheng80825102009-11-12 09:35:36 +00003739 while (1) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04003740 trans = btrfs_start_transaction(root, 0);
3741 BUG_ON(IS_ERR(trans));
Yan, Zheng80825102009-11-12 09:35:36 +00003742 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003743 trans->block_rsv = root->orphan_block_rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00003744
Yan, Zhengd68fc572010-05-16 10:49:58 -04003745 ret = btrfs_block_rsv_check(trans, root,
3746 root->orphan_block_rsv, 0, 5);
3747 if (ret) {
3748 BUG_ON(ret != -EAGAIN);
3749 ret = btrfs_commit_transaction(trans, root);
3750 BUG_ON(ret);
3751 continue;
3752 }
3753
3754 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003755 if (ret != -EAGAIN)
3756 break;
3757
3758 nr = trans->blocks_used;
3759 btrfs_end_transaction(trans, root);
3760 trans = NULL;
3761 btrfs_btree_balance_dirty(root, nr);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003762
Josef Bacik7b128762008-07-24 12:17:14 -04003763 }
3764
Yan, Zheng80825102009-11-12 09:35:36 +00003765 if (ret == 0) {
3766 ret = btrfs_orphan_del(trans, inode);
3767 BUG_ON(ret);
3768 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003769
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003770 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003771 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003772 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003773no_delete:
Al Virobd555972010-06-07 11:35:40 -04003774 end_writeback(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003775 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003776}
3777
3778/*
3779 * this returns the key found in the dir entry in the location pointer.
3780 * If no dir entries were found, location->objectid is 0.
3781 */
3782static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3783 struct btrfs_key *location)
3784{
3785 const char *name = dentry->d_name.name;
3786 int namelen = dentry->d_name.len;
3787 struct btrfs_dir_item *di;
3788 struct btrfs_path *path;
3789 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003790 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003791
3792 path = btrfs_alloc_path();
3793 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05003794
Chris Mason39279cc2007-06-12 06:35:45 -04003795 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
3796 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003797 if (IS_ERR(di))
3798 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003799
3800 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05003801 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003802
Chris Mason5f39d392007-10-15 16:14:19 -04003803 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003804out:
Chris Mason39279cc2007-06-12 06:35:45 -04003805 btrfs_free_path(path);
3806 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003807out_err:
3808 location->objectid = 0;
3809 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003810}
3811
3812/*
3813 * when we hit a tree root in a directory, the btrfs part of the inode
3814 * needs to be changed to reflect the root directory of the tree root. This
3815 * is kind of like crossing a mount point.
3816 */
3817static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003818 struct inode *dir,
3819 struct dentry *dentry,
3820 struct btrfs_key *location,
3821 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003822{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003823 struct btrfs_path *path;
3824 struct btrfs_root *new_root;
3825 struct btrfs_root_ref *ref;
3826 struct extent_buffer *leaf;
3827 int ret;
3828 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003829
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003830 path = btrfs_alloc_path();
3831 if (!path) {
3832 err = -ENOMEM;
3833 goto out;
3834 }
Chris Mason39279cc2007-06-12 06:35:45 -04003835
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003836 err = -ENOENT;
3837 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3838 BTRFS_I(dir)->root->root_key.objectid,
3839 location->objectid);
3840 if (ret) {
3841 if (ret < 0)
3842 err = ret;
3843 goto out;
3844 }
Chris Mason39279cc2007-06-12 06:35:45 -04003845
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003846 leaf = path->nodes[0];
3847 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3848 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
3849 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3850 goto out;
3851
3852 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3853 (unsigned long)(ref + 1),
3854 dentry->d_name.len);
3855 if (ret)
3856 goto out;
3857
3858 btrfs_release_path(root->fs_info->tree_root, path);
3859
3860 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3861 if (IS_ERR(new_root)) {
3862 err = PTR_ERR(new_root);
3863 goto out;
3864 }
3865
3866 if (btrfs_root_refs(&new_root->root_item) == 0) {
3867 err = -ENOENT;
3868 goto out;
3869 }
3870
3871 *sub_root = new_root;
3872 location->objectid = btrfs_root_dirid(&new_root->root_item);
3873 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003874 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003875 err = 0;
3876out:
3877 btrfs_free_path(path);
3878 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003879}
3880
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003881static void inode_tree_add(struct inode *inode)
3882{
3883 struct btrfs_root *root = BTRFS_I(inode)->root;
3884 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003885 struct rb_node **p;
3886 struct rb_node *parent;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003887again:
3888 p = &root->inode_tree.rb_node;
3889 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003890
Yan, Zheng76dda932009-09-21 16:00:26 -04003891 if (hlist_unhashed(&inode->i_hash))
3892 return;
3893
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003894 spin_lock(&root->inode_lock);
3895 while (*p) {
3896 parent = *p;
3897 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3898
3899 if (inode->i_ino < entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003900 p = &parent->rb_left;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003901 else if (inode->i_ino > entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003902 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003903 else {
3904 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04003905 (I_WILL_FREE | I_FREEING)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003906 rb_erase(parent, &root->inode_tree);
3907 RB_CLEAR_NODE(parent);
3908 spin_unlock(&root->inode_lock);
3909 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003910 }
3911 }
3912 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3913 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3914 spin_unlock(&root->inode_lock);
3915}
3916
3917static void inode_tree_del(struct inode *inode)
3918{
3919 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003920 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003921
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003922 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003923 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003924 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003925 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003926 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003927 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003928 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003929
Josef Bacik0af3d002010-06-21 14:48:16 -04003930 /*
3931 * Free space cache has inodes in the tree root, but the tree root has a
3932 * root_refs of 0, so this could end up dropping the tree root as a
3933 * snapshot, so we need the extra !root->fs_info->tree_root check to
3934 * make sure we don't drop it.
3935 */
3936 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
3937 root != root->fs_info->tree_root) {
Yan, Zheng76dda932009-09-21 16:00:26 -04003938 synchronize_srcu(&root->fs_info->subvol_srcu);
3939 spin_lock(&root->inode_lock);
3940 empty = RB_EMPTY_ROOT(&root->inode_tree);
3941 spin_unlock(&root->inode_lock);
3942 if (empty)
3943 btrfs_add_dead_root(root);
3944 }
3945}
3946
3947int btrfs_invalidate_inodes(struct btrfs_root *root)
3948{
3949 struct rb_node *node;
3950 struct rb_node *prev;
3951 struct btrfs_inode *entry;
3952 struct inode *inode;
3953 u64 objectid = 0;
3954
3955 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3956
3957 spin_lock(&root->inode_lock);
3958again:
3959 node = root->inode_tree.rb_node;
3960 prev = NULL;
3961 while (node) {
3962 prev = node;
3963 entry = rb_entry(node, struct btrfs_inode, rb_node);
3964
3965 if (objectid < entry->vfs_inode.i_ino)
3966 node = node->rb_left;
3967 else if (objectid > entry->vfs_inode.i_ino)
3968 node = node->rb_right;
3969 else
3970 break;
3971 }
3972 if (!node) {
3973 while (prev) {
3974 entry = rb_entry(prev, struct btrfs_inode, rb_node);
3975 if (objectid <= entry->vfs_inode.i_ino) {
3976 node = prev;
3977 break;
3978 }
3979 prev = rb_next(prev);
3980 }
3981 }
3982 while (node) {
3983 entry = rb_entry(node, struct btrfs_inode, rb_node);
3984 objectid = entry->vfs_inode.i_ino + 1;
3985 inode = igrab(&entry->vfs_inode);
3986 if (inode) {
3987 spin_unlock(&root->inode_lock);
3988 if (atomic_read(&inode->i_count) > 1)
3989 d_prune_aliases(inode);
3990 /*
Al Viro45321ac2010-06-07 13:43:19 -04003991 * btrfs_drop_inode will have it removed from
Yan, Zheng76dda932009-09-21 16:00:26 -04003992 * the inode cache when its usage count
3993 * hits zero.
3994 */
3995 iput(inode);
3996 cond_resched();
3997 spin_lock(&root->inode_lock);
3998 goto again;
3999 }
4000
4001 if (cond_resched_lock(&root->inode_lock))
4002 goto again;
4003
4004 node = rb_next(node);
4005 }
4006 spin_unlock(&root->inode_lock);
4007 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004008}
4009
Chris Masone02119d2008-09-05 16:13:11 -04004010static int btrfs_init_locked_inode(struct inode *inode, void *p)
4011{
4012 struct btrfs_iget_args *args = p;
4013 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04004014 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05004015 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004016 return 0;
4017}
4018
4019static int btrfs_find_actor(struct inode *inode, void *opaque)
4020{
4021 struct btrfs_iget_args *args = opaque;
Chris Masond3977122009-01-05 21:25:51 -05004022 return args->ino == inode->i_ino &&
4023 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004024}
4025
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004026static struct inode *btrfs_iget_locked(struct super_block *s,
4027 u64 objectid,
4028 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04004029{
4030 struct inode *inode;
4031 struct btrfs_iget_args args;
4032 args.ino = objectid;
4033 args.root = root;
4034
4035 inode = iget5_locked(s, objectid, btrfs_find_actor,
4036 btrfs_init_locked_inode,
4037 (void *)&args);
4038 return inode;
4039}
4040
Balaji Rao1a54ef82008-07-21 02:01:04 +05304041/* Get an inode object given its location and corresponding root.
4042 * Returns in *is_new if the inode was read from disk
4043 */
4044struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00004045 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05304046{
4047 struct inode *inode;
4048
4049 inode = btrfs_iget_locked(s, location->objectid, root);
4050 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004051 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304052
4053 if (inode->i_state & I_NEW) {
4054 BTRFS_I(inode)->root = root;
4055 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4056 btrfs_read_locked_inode(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004057
4058 inode_tree_add(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05304059 unlock_new_inode(inode);
Josef Bacik73f73412009-12-04 17:38:27 +00004060 if (new)
4061 *new = 1;
Balaji Rao1a54ef82008-07-21 02:01:04 +05304062 }
4063
4064 return inode;
4065}
4066
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004067static struct inode *new_simple_dir(struct super_block *s,
4068 struct btrfs_key *key,
4069 struct btrfs_root *root)
4070{
4071 struct inode *inode = new_inode(s);
4072
4073 if (!inode)
4074 return ERR_PTR(-ENOMEM);
4075
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004076 BTRFS_I(inode)->root = root;
4077 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
4078 BTRFS_I(inode)->dummy_inode = 1;
4079
4080 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
4081 inode->i_op = &simple_dir_inode_operations;
4082 inode->i_fop = &simple_dir_operations;
4083 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4084 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4085
4086 return inode;
4087}
4088
Chris Mason3de45862008-11-17 21:02:50 -05004089struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004090{
Chris Masond3977122009-01-05 21:25:51 -05004091 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004092 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004093 struct btrfs_root *sub_root = root;
4094 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04004095 int index;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004096 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004097
Yan, Zheng76dda932009-09-21 16:00:26 -04004098 dentry->d_op = &btrfs_dentry_operations;
4099
Chris Mason39279cc2007-06-12 06:35:45 -04004100 if (dentry->d_name.len > BTRFS_NAME_LEN)
4101 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04004102
Chris Mason39279cc2007-06-12 06:35:45 -04004103 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04004104
Chris Mason39279cc2007-06-12 06:35:45 -04004105 if (ret < 0)
4106 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04004107
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004108 if (location.objectid == 0)
4109 return NULL;
4110
4111 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00004112 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004113 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004114 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004115
4116 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4117
Yan, Zheng76dda932009-09-21 16:00:26 -04004118 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004119 ret = fixup_tree_root_location(root, dir, dentry,
4120 &location, &sub_root);
4121 if (ret < 0) {
4122 if (ret != -ENOENT)
4123 inode = ERR_PTR(ret);
4124 else
4125 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4126 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00004127 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004128 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004129 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4130
Yan, Zhengc71bf092009-11-12 09:34:40 +00004131 if (root != sub_root) {
4132 down_read(&root->fs_info->cleanup_work_sem);
4133 if (!(inode->i_sb->s_flags & MS_RDONLY))
4134 btrfs_orphan_cleanup(sub_root);
4135 up_read(&root->fs_info->cleanup_work_sem);
4136 }
4137
Chris Mason3de45862008-11-17 21:02:50 -05004138 return inode;
4139}
4140
Yan, Zheng76dda932009-09-21 16:00:26 -04004141static int btrfs_dentry_delete(struct dentry *dentry)
4142{
4143 struct btrfs_root *root;
4144
Yan, Zhengefefb142009-10-09 09:25:16 -04004145 if (!dentry->d_inode && !IS_ROOT(dentry))
4146 dentry = dentry->d_parent;
Yan, Zheng76dda932009-09-21 16:00:26 -04004147
Yan, Zhengefefb142009-10-09 09:25:16 -04004148 if (dentry->d_inode) {
4149 root = BTRFS_I(dentry->d_inode)->root;
4150 if (btrfs_root_refs(&root->root_item) == 0)
4151 return 1;
4152 }
Yan, Zheng76dda932009-09-21 16:00:26 -04004153 return 0;
4154}
4155
Chris Mason3de45862008-11-17 21:02:50 -05004156static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4157 struct nameidata *nd)
4158{
4159 struct inode *inode;
4160
Chris Mason3de45862008-11-17 21:02:50 -05004161 inode = btrfs_lookup_dentry(dir, dentry);
4162 if (IS_ERR(inode))
4163 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04004164
Chris Mason39279cc2007-06-12 06:35:45 -04004165 return d_splice_alias(inode, dentry);
4166}
4167
Chris Mason39279cc2007-06-12 06:35:45 -04004168static unsigned char btrfs_filetype_table[] = {
4169 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4170};
4171
David Woodhousecbdf5a22008-08-06 19:42:33 +01004172static int btrfs_real_readdir(struct file *filp, void *dirent,
4173 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04004174{
Chris Mason6da6aba2007-12-18 16:15:09 -05004175 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04004176 struct btrfs_root *root = BTRFS_I(inode)->root;
4177 struct btrfs_item *item;
4178 struct btrfs_dir_item *di;
4179 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004180 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004181 struct btrfs_path *path;
4182 int ret;
4183 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04004184 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004185 int slot;
4186 int advance;
4187 unsigned char d_type;
4188 int over = 0;
4189 u32 di_cur;
4190 u32 di_total;
4191 u32 di_len;
4192 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004193 char tmp_name[32];
4194 char *name_ptr;
4195 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04004196
4197 /* FIXME, use a real flag for deciding about the key type */
4198 if (root->fs_info->tree_root == root)
4199 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04004200
Chris Mason39544012007-12-12 14:38:19 -05004201 /* special case for "." */
4202 if (filp->f_pos == 0) {
4203 over = filldir(dirent, ".", 1,
4204 1, inode->i_ino,
4205 DT_DIR);
4206 if (over)
4207 return 0;
4208 filp->f_pos = 1;
4209 }
Chris Mason39544012007-12-12 14:38:19 -05004210 /* special case for .., just use the back ref */
4211 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004212 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05004213 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01004214 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05004215 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01004216 return 0;
Chris Mason39544012007-12-12 14:38:19 -05004217 filp->f_pos = 2;
4218 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004219 path = btrfs_alloc_path();
4220 path->reada = 2;
4221
Chris Mason39279cc2007-06-12 06:35:45 -04004222 btrfs_set_key_type(&key, key_type);
4223 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01004224 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04004225
Chris Mason39279cc2007-06-12 06:35:45 -04004226 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4227 if (ret < 0)
4228 goto err;
4229 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01004230
4231 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004232 leaf = path->nodes[0];
4233 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004234 slot = path->slots[0];
4235 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01004236 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04004237 ret = btrfs_next_leaf(root, path);
4238 if (ret)
4239 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004240 leaf = path->nodes[0];
4241 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004242 slot = path->slots[0];
4243 } else {
4244 slot++;
4245 path->slots[0]++;
4246 }
4247 }
Chris Mason3de45862008-11-17 21:02:50 -05004248
Chris Mason39279cc2007-06-12 06:35:45 -04004249 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04004250 item = btrfs_item_nr(leaf, slot);
4251 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4252
4253 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04004254 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004255 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004256 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004257 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04004258 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04004259
4260 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01004261
Chris Mason39279cc2007-06-12 06:35:45 -04004262 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4263 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04004264 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01004265
4266 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04004267 struct btrfs_key location;
4268
4269 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004270 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004271 name_ptr = tmp_name;
4272 } else {
4273 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004274 if (!name_ptr) {
4275 ret = -ENOMEM;
4276 goto err;
4277 }
Chris Mason5f39d392007-10-15 16:14:19 -04004278 }
4279 read_extent_buffer(leaf, name_ptr,
4280 (unsigned long)(di + 1), name_len);
4281
4282 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4283 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004284
4285 /* is this a reference to our own snapshot? If so
4286 * skip it
4287 */
4288 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4289 location.objectid == root->root_key.objectid) {
4290 over = 0;
4291 goto skip;
4292 }
Chris Mason5f39d392007-10-15 16:14:19 -04004293 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004294 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004295 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004296
Chris Mason3de45862008-11-17 21:02:50 -05004297skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004298 if (name_ptr != tmp_name)
4299 kfree(name_ptr);
4300
Chris Mason39279cc2007-06-12 06:35:45 -04004301 if (over)
4302 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004303 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004304 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004305 di_cur += di_len;
4306 di = (struct btrfs_dir_item *)((char *)di + di_len);
4307 }
4308 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004309
4310 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004311 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004312 /*
4313 * 32-bit glibc will use getdents64, but then strtol -
4314 * so the last number we can serve is this.
4315 */
4316 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004317 else
4318 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004319nopos:
4320 ret = 0;
4321err:
Chris Mason39279cc2007-06-12 06:35:45 -04004322 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004323 return ret;
4324}
4325
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004326int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004327{
4328 struct btrfs_root *root = BTRFS_I(inode)->root;
4329 struct btrfs_trans_handle *trans;
4330 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04004331 bool nolock = false;
Chris Mason39279cc2007-06-12 06:35:45 -04004332
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004333 if (BTRFS_I(inode)->dummy_inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04004334 return 0;
4335
Josef Bacik0af3d002010-06-21 14:48:16 -04004336 smp_mb();
4337 nolock = (root->fs_info->closing && root == root->fs_info->tree_root);
4338
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004339 if (wbc->sync_mode == WB_SYNC_ALL) {
Josef Bacik0af3d002010-06-21 14:48:16 -04004340 if (nolock)
4341 trans = btrfs_join_transaction_nolock(root, 1);
4342 else
4343 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004344 btrfs_set_trans_block_group(trans, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04004345 if (nolock)
4346 ret = btrfs_end_transaction_nolock(trans, root);
4347 else
4348 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004349 }
4350 return ret;
4351}
4352
4353/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004354 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004355 * inode changes. But, it is most likely to find the inode in cache.
4356 * FIXME, needs more benchmarking...there are no reasons other than performance
4357 * to keep or drop this code.
4358 */
4359void btrfs_dirty_inode(struct inode *inode)
4360{
4361 struct btrfs_root *root = BTRFS_I(inode)->root;
4362 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004363 int ret;
4364
4365 if (BTRFS_I(inode)->dummy_inode)
4366 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004367
Chris Masonf9295742008-07-17 12:54:14 -04004368 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004369 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004370
4371 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04004372 if (ret && ret == -ENOSPC) {
4373 /* whoops, lets try again with the full transaction */
4374 btrfs_end_transaction(trans, root);
4375 trans = btrfs_start_transaction(root, 1);
Chris Mason9aeead72010-05-27 10:23:00 -04004376 if (IS_ERR(trans)) {
4377 if (printk_ratelimit()) {
4378 printk(KERN_ERR "btrfs: fail to "
4379 "dirty inode %lu error %ld\n",
4380 inode->i_ino, PTR_ERR(trans));
4381 }
4382 return;
4383 }
Chris Mason94b60442010-05-26 11:02:00 -04004384 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004385
Chris Mason94b60442010-05-26 11:02:00 -04004386 ret = btrfs_update_inode(trans, root, inode);
4387 if (ret) {
Chris Mason9aeead72010-05-27 10:23:00 -04004388 if (printk_ratelimit()) {
4389 printk(KERN_ERR "btrfs: fail to "
4390 "dirty inode %lu error %d\n",
4391 inode->i_ino, ret);
4392 }
Chris Mason94b60442010-05-26 11:02:00 -04004393 }
4394 }
Chris Mason39279cc2007-06-12 06:35:45 -04004395 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004396}
4397
Chris Masond352ac62008-09-29 15:18:18 -04004398/*
4399 * find the highest existing sequence number in a directory
4400 * and then set the in-memory index_cnt variable to reflect
4401 * free sequence numbers
4402 */
Josef Bacikaec74772008-07-24 12:12:38 -04004403static int btrfs_set_inode_index_count(struct inode *inode)
4404{
4405 struct btrfs_root *root = BTRFS_I(inode)->root;
4406 struct btrfs_key key, found_key;
4407 struct btrfs_path *path;
4408 struct extent_buffer *leaf;
4409 int ret;
4410
4411 key.objectid = inode->i_ino;
4412 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4413 key.offset = (u64)-1;
4414
4415 path = btrfs_alloc_path();
4416 if (!path)
4417 return -ENOMEM;
4418
4419 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4420 if (ret < 0)
4421 goto out;
4422 /* FIXME: we should be able to handle this */
4423 if (ret == 0)
4424 goto out;
4425 ret = 0;
4426
4427 /*
4428 * MAGIC NUMBER EXPLANATION:
4429 * since we search a directory based on f_pos we have to start at 2
4430 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4431 * else has to start at 2
4432 */
4433 if (path->slots[0] == 0) {
4434 BTRFS_I(inode)->index_cnt = 2;
4435 goto out;
4436 }
4437
4438 path->slots[0]--;
4439
4440 leaf = path->nodes[0];
4441 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4442
4443 if (found_key.objectid != inode->i_ino ||
4444 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4445 BTRFS_I(inode)->index_cnt = 2;
4446 goto out;
4447 }
4448
4449 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4450out:
4451 btrfs_free_path(path);
4452 return ret;
4453}
4454
Chris Masond352ac62008-09-29 15:18:18 -04004455/*
4456 * helper to find a free sequence number in a given directory. This current
4457 * code is very simple, later versions will do smarter things in the btree
4458 */
Chris Mason3de45862008-11-17 21:02:50 -05004459int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004460{
4461 int ret = 0;
4462
4463 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4464 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05004465 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04004466 return ret;
4467 }
4468
Chris Mason00e4e6b2008-08-05 11:18:09 -04004469 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004470 BTRFS_I(dir)->index_cnt++;
4471
4472 return ret;
4473}
4474
Chris Mason39279cc2007-06-12 06:35:45 -04004475static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4476 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004477 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004478 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05004479 u64 ref_objectid, u64 objectid,
4480 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004481{
4482 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004483 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004484 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004485 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004486 struct btrfs_inode_ref *ref;
4487 struct btrfs_key key[2];
4488 u32 sizes[2];
4489 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004490 int ret;
4491 int owner;
4492
Chris Mason5f39d392007-10-15 16:14:19 -04004493 path = btrfs_alloc_path();
4494 BUG_ON(!path);
4495
Chris Mason39279cc2007-06-12 06:35:45 -04004496 inode = new_inode(root->fs_info->sb);
4497 if (!inode)
4498 return ERR_PTR(-ENOMEM);
4499
Josef Bacikaec74772008-07-24 12:12:38 -04004500 if (dir) {
Chris Mason3de45862008-11-17 21:02:50 -05004501 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004502 if (ret) {
4503 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004504 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004505 }
Josef Bacikaec74772008-07-24 12:12:38 -04004506 }
4507 /*
4508 * index_cnt is ignored for everything but a dir,
4509 * btrfs_get_inode_index_count has an explanation for the magic
4510 * number
4511 */
4512 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004513 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004514 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik6a632092009-02-20 11:00:09 -05004515 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db22007-08-27 16:49:44 -04004516
Chris Mason39279cc2007-06-12 06:35:45 -04004517 if (mode & S_IFDIR)
4518 owner = 0;
4519 else
4520 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004521 BTRFS_I(inode)->block_group =
4522 btrfs_find_block_group(root, 0, alloc_hint, owner);
Chris Mason9c583092008-01-29 15:15:18 -05004523
4524 key[0].objectid = objectid;
4525 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4526 key[0].offset = 0;
4527
4528 key[1].objectid = objectid;
4529 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4530 key[1].offset = ref_objectid;
4531
4532 sizes[0] = sizeof(struct btrfs_inode_item);
4533 sizes[1] = name_len + sizeof(*ref);
4534
Chris Masonb9473432009-03-13 11:00:37 -04004535 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004536 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4537 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004538 goto fail;
4539
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03004540 inode_init_owner(inode, dir, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04004541 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004542 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004543 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004544 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4545 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004546 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004547
4548 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4549 struct btrfs_inode_ref);
4550 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004551 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004552 ptr = (unsigned long)(ref + 1);
4553 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4554
Chris Mason5f39d392007-10-15 16:14:19 -04004555 btrfs_mark_buffer_dirty(path->nodes[0]);
4556 btrfs_free_path(path);
4557
Chris Mason39279cc2007-06-12 06:35:45 -04004558 location = &BTRFS_I(inode)->location;
4559 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004560 location->offset = 0;
4561 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4562
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004563 btrfs_inherit_iflags(inode, dir);
4564
Chris Mason94272162009-07-02 12:26:06 -04004565 if ((mode & S_IFREG)) {
4566 if (btrfs_test_opt(root, NODATASUM))
4567 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
4568 if (btrfs_test_opt(root, NODATACOW))
4569 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4570 }
4571
Chris Mason39279cc2007-06-12 06:35:45 -04004572 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004573 inode_tree_add(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004574 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004575fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004576 if (dir)
4577 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004578 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004579 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004580 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004581}
4582
4583static inline u8 btrfs_inode_type(struct inode *inode)
4584{
4585 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4586}
4587
Chris Masond352ac62008-09-29 15:18:18 -04004588/*
4589 * utility function to add 'inode' into 'parent_inode' with
4590 * a give name and a given sequence number.
4591 * if 'add_backref' is true, also insert a backref from the
4592 * inode to the parent directory.
4593 */
Chris Masone02119d2008-09-05 16:13:11 -04004594int btrfs_add_link(struct btrfs_trans_handle *trans,
4595 struct inode *parent_inode, struct inode *inode,
4596 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004597{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004598 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004599 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004600 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04004601
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004602 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4603 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4604 } else {
4605 key.objectid = inode->i_ino;
4606 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4607 key.offset = 0;
4608 }
Chris Mason39279cc2007-06-12 06:35:45 -04004609
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004610 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4611 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4612 key.objectid, root->root_key.objectid,
4613 parent_inode->i_ino,
4614 index, name, name_len);
4615 } else if (add_backref) {
4616 ret = btrfs_insert_inode_ref(trans, root,
4617 name, name_len, inode->i_ino,
4618 parent_inode->i_ino, index);
4619 }
4620
Chris Mason39279cc2007-06-12 06:35:45 -04004621 if (ret == 0) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004622 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4623 parent_inode->i_ino, &key,
4624 btrfs_inode_type(inode), index);
4625 BUG_ON(ret);
4626
Chris Masondbe674a2008-07-17 12:54:05 -04004627 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04004628 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04004629 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004630 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004631 }
4632 return ret;
4633}
4634
4635static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05004636 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004637 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004638{
Chris Masone02119d2008-09-05 16:13:11 -04004639 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4640 inode, dentry->d_name.name,
4641 dentry->d_name.len, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004642 if (!err) {
4643 d_instantiate(dentry, inode);
4644 return 0;
4645 }
4646 if (err > 0)
4647 err = -EEXIST;
4648 return err;
4649}
4650
Josef Bacik618e21d2007-07-11 10:18:17 -04004651static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4652 int mode, dev_t rdev)
4653{
4654 struct btrfs_trans_handle *trans;
4655 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004656 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004657 int err;
4658 int drop_inode = 0;
4659 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004660 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004661 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004662
4663 if (!new_valid_dev(rdev))
4664 return -EINVAL;
4665
Yan, Zhenga22285a2010-05-16 10:48:46 -04004666 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4667 if (err)
4668 return err;
4669
Josef Bacik9ed74f22009-09-11 16:12:44 -04004670 /*
4671 * 2 for inode item and ref
4672 * 2 for dir items
4673 * 1 for xattr if selinux is on
4674 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004675 trans = btrfs_start_transaction(root, 5);
4676 if (IS_ERR(trans))
4677 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004678
Josef Bacik618e21d2007-07-11 10:18:17 -04004679 btrfs_set_trans_block_group(trans, dir);
4680
Josef Bacikaec74772008-07-24 12:12:38 -04004681 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004682 dentry->d_name.len,
4683 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004684 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004685 err = PTR_ERR(inode);
4686 if (IS_ERR(inode))
4687 goto out_unlock;
4688
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004689 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004690 if (err) {
4691 drop_inode = 1;
4692 goto out_unlock;
4693 }
4694
Josef Bacik618e21d2007-07-11 10:18:17 -04004695 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004696 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004697 if (err)
4698 drop_inode = 1;
4699 else {
4700 inode->i_op = &btrfs_special_inode_operations;
4701 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004702 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004703 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004704 btrfs_update_inode_block_group(trans, inode);
4705 btrfs_update_inode_block_group(trans, dir);
4706out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004707 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004708 btrfs_end_transaction_throttle(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004709 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004710 if (drop_inode) {
4711 inode_dec_link_count(inode);
4712 iput(inode);
4713 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004714 return err;
4715}
4716
Chris Mason39279cc2007-06-12 06:35:45 -04004717static int btrfs_create(struct inode *dir, struct dentry *dentry,
4718 int mode, struct nameidata *nd)
4719{
4720 struct btrfs_trans_handle *trans;
4721 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004722 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004723 int drop_inode = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004724 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004725 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004726 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004727 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004728
Yan, Zhenga22285a2010-05-16 10:48:46 -04004729 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4730 if (err)
4731 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004732 /*
4733 * 2 for inode item and ref
4734 * 2 for dir items
4735 * 1 for xattr if selinux is on
4736 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004737 trans = btrfs_start_transaction(root, 5);
4738 if (IS_ERR(trans))
4739 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004740
Chris Mason39279cc2007-06-12 06:35:45 -04004741 btrfs_set_trans_block_group(trans, dir);
4742
Josef Bacikaec74772008-07-24 12:12:38 -04004743 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004744 dentry->d_name.len,
4745 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004746 objectid, BTRFS_I(dir)->block_group, mode,
4747 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004748 err = PTR_ERR(inode);
4749 if (IS_ERR(inode))
4750 goto out_unlock;
4751
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004752 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004753 if (err) {
4754 drop_inode = 1;
4755 goto out_unlock;
4756 }
4757
Chris Mason39279cc2007-06-12 06:35:45 -04004758 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004759 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004760 if (err)
4761 drop_inode = 1;
4762 else {
4763 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004764 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004765 inode->i_fop = &btrfs_file_operations;
4766 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004767 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004768 }
Chris Mason39279cc2007-06-12 06:35:45 -04004769 btrfs_update_inode_block_group(trans, inode);
4770 btrfs_update_inode_block_group(trans, dir);
4771out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004772 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004773 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004774 if (drop_inode) {
4775 inode_dec_link_count(inode);
4776 iput(inode);
4777 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004778 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004779 return err;
4780}
4781
4782static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4783 struct dentry *dentry)
4784{
4785 struct btrfs_trans_handle *trans;
4786 struct btrfs_root *root = BTRFS_I(dir)->root;
4787 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004788 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004789 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004790 int err;
4791 int drop_inode = 0;
4792
4793 if (inode->i_nlink == 0)
4794 return -ENOENT;
4795
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004796 /* do not allow sys_link's with other subvols of the same device */
4797 if (root->objectid != BTRFS_I(inode)->root->objectid)
4798 return -EPERM;
4799
Josef Bacik9ed74f22009-09-11 16:12:44 -04004800 btrfs_inc_nlink(inode);
4801
Chris Mason3de45862008-11-17 21:02:50 -05004802 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004803 if (err)
4804 goto fail;
4805
Yan, Zhenga22285a2010-05-16 10:48:46 -04004806 /*
4807 * 1 item for inode ref
4808 * 2 items for dir items
4809 */
4810 trans = btrfs_start_transaction(root, 3);
4811 if (IS_ERR(trans)) {
4812 err = PTR_ERR(trans);
4813 goto fail;
4814 }
Chris Mason5f39d392007-10-15 16:14:19 -04004815
Chris Mason39279cc2007-06-12 06:35:45 -04004816 btrfs_set_trans_block_group(trans, dir);
4817 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04004818
Chris Mason00e4e6b2008-08-05 11:18:09 -04004819 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004820
Yan, Zhenga5719522009-09-24 09:17:31 -04004821 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004822 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004823 } else {
4824 btrfs_update_inode_block_group(trans, dir);
4825 err = btrfs_update_inode(trans, root, inode);
4826 BUG_ON(err);
4827 btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
4828 }
Chris Mason39279cc2007-06-12 06:35:45 -04004829
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004830 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004831 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004832fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004833 if (drop_inode) {
4834 inode_dec_link_count(inode);
4835 iput(inode);
4836 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004837 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004838 return err;
4839}
4840
Chris Mason39279cc2007-06-12 06:35:45 -04004841static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4842{
Chris Masonb9d86662008-05-02 16:13:49 -04004843 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004844 struct btrfs_trans_handle *trans;
4845 struct btrfs_root *root = BTRFS_I(dir)->root;
4846 int err = 0;
4847 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004848 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004849 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004850 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004851
Yan, Zhenga22285a2010-05-16 10:48:46 -04004852 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4853 if (err)
4854 return err;
4855
Josef Bacik9ed74f22009-09-11 16:12:44 -04004856 /*
4857 * 2 items for inode and ref
4858 * 2 items for dir items
4859 * 1 for xattr if selinux is on
4860 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004861 trans = btrfs_start_transaction(root, 5);
4862 if (IS_ERR(trans))
4863 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004864 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004865
Josef Bacikaec74772008-07-24 12:12:38 -04004866 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004867 dentry->d_name.len,
4868 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004869 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4870 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004871 if (IS_ERR(inode)) {
4872 err = PTR_ERR(inode);
4873 goto out_fail;
4874 }
Chris Mason5f39d392007-10-15 16:14:19 -04004875
Chris Mason39279cc2007-06-12 06:35:45 -04004876 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04004877
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004878 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004879 if (err)
4880 goto out_fail;
4881
Chris Mason39279cc2007-06-12 06:35:45 -04004882 inode->i_op = &btrfs_dir_inode_operations;
4883 inode->i_fop = &btrfs_dir_file_operations;
4884 btrfs_set_trans_block_group(trans, inode);
4885
Chris Masondbe674a2008-07-17 12:54:05 -04004886 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004887 err = btrfs_update_inode(trans, root, inode);
4888 if (err)
4889 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004890
Chris Masone02119d2008-09-05 16:13:11 -04004891 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4892 inode, dentry->d_name.name,
4893 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004894 if (err)
4895 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004896
Chris Mason39279cc2007-06-12 06:35:45 -04004897 d_instantiate(dentry, inode);
4898 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004899 btrfs_update_inode_block_group(trans, inode);
4900 btrfs_update_inode_block_group(trans, dir);
4901
4902out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004903 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004904 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004905 if (drop_on_err)
4906 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004907 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004908 return err;
4909}
4910
Chris Masond352ac62008-09-29 15:18:18 -04004911/* helper for btfs_get_extent. Given an existing extent in the tree,
4912 * and an extent that you want to insert, deal with overlap and insert
4913 * the new extent into the tree.
4914 */
Chris Mason3b951512008-04-17 11:29:12 -04004915static int merge_extent_mapping(struct extent_map_tree *em_tree,
4916 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004917 struct extent_map *em,
4918 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04004919{
4920 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04004921
Chris Masone6dcd2d2008-07-17 12:53:50 -04004922 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
4923 start_diff = map_start - em->start;
4924 em->start = map_start;
4925 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04004926 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
4927 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004928 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04004929 em->block_len -= start_diff;
4930 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004931 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004932}
4933
Chris Masonc8b97812008-10-29 14:49:59 -04004934static noinline int uncompress_inline(struct btrfs_path *path,
4935 struct inode *inode, struct page *page,
4936 size_t pg_offset, u64 extent_offset,
4937 struct btrfs_file_extent_item *item)
4938{
4939 int ret;
4940 struct extent_buffer *leaf = path->nodes[0];
4941 char *tmp;
4942 size_t max_size;
4943 unsigned long inline_size;
4944 unsigned long ptr;
4945
4946 WARN_ON(pg_offset != 0);
4947 max_size = btrfs_file_extent_ram_bytes(leaf, item);
4948 inline_size = btrfs_file_extent_inline_item_len(leaf,
4949 btrfs_item_nr(leaf, path->slots[0]));
4950 tmp = kmalloc(inline_size, GFP_NOFS);
4951 ptr = btrfs_file_extent_inline_start(item);
4952
4953 read_extent_buffer(leaf, tmp, ptr, inline_size);
4954
Chris Mason5b050f02008-11-11 09:34:41 -05004955 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004956 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
4957 inline_size, max_size);
4958 if (ret) {
4959 char *kaddr = kmap_atomic(page, KM_USER0);
4960 unsigned long copy_size = min_t(u64,
4961 PAGE_CACHE_SIZE - pg_offset,
4962 max_size - extent_offset);
4963 memset(kaddr + pg_offset, 0, copy_size);
4964 kunmap_atomic(kaddr, KM_USER0);
4965 }
4966 kfree(tmp);
4967 return 0;
4968}
4969
Chris Masond352ac62008-09-29 15:18:18 -04004970/*
4971 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05004972 * the ugly parts come from merging extents from the disk with the in-ram
4973 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04004974 * where the in-ram extents might be locked pending data=ordered completion.
4975 *
4976 * This also copies inline extents directly into the page.
4977 */
Chris Masond3977122009-01-05 21:25:51 -05004978
Chris Masona52d9a82007-08-27 16:49:44 -04004979struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05004980 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04004981 int create)
4982{
4983 int ret;
4984 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04004985 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04004986 u64 extent_start = 0;
4987 u64 extent_end = 0;
4988 u64 objectid = inode->i_ino;
4989 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04004990 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04004991 struct btrfs_root *root = BTRFS_I(inode)->root;
4992 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04004993 struct extent_buffer *leaf;
4994 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04004995 struct extent_map *em = NULL;
4996 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05004997 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004998 struct btrfs_trans_handle *trans = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004999 int compressed;
Chris Masona52d9a82007-08-27 16:49:44 -04005000
Chris Masona52d9a82007-08-27 16:49:44 -04005001again:
Chris Mason890871b2009-09-02 16:24:52 -04005002 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005003 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04005004 if (em)
5005 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04005006 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05005007
Chris Masona52d9a82007-08-27 16:49:44 -04005008 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04005009 if (em->start > start || em->start + em->len <= start)
5010 free_extent_map(em);
5011 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05005012 free_extent_map(em);
5013 else
5014 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005015 }
Chris Masond1310b22008-01-24 16:13:08 -05005016 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005017 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05005018 err = -ENOMEM;
5019 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005020 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005021 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05005022 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05005023 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05005024 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04005025 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04005026
5027 if (!path) {
5028 path = btrfs_alloc_path();
5029 BUG_ON(!path);
5030 }
5031
Chris Mason179e29e2007-11-01 11:28:41 -04005032 ret = btrfs_lookup_file_extent(trans, root, path,
5033 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04005034 if (ret < 0) {
5035 err = ret;
5036 goto out;
5037 }
5038
5039 if (ret != 0) {
5040 if (path->slots[0] == 0)
5041 goto not_found;
5042 path->slots[0]--;
5043 }
5044
Chris Mason5f39d392007-10-15 16:14:19 -04005045 leaf = path->nodes[0];
5046 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04005047 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04005048 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04005049 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5050 found_type = btrfs_key_type(&found_key);
5051 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04005052 found_type != BTRFS_EXTENT_DATA_KEY) {
5053 goto not_found;
5054 }
5055
Chris Mason5f39d392007-10-15 16:14:19 -04005056 found_type = btrfs_file_extent_type(leaf, item);
5057 extent_start = found_key.offset;
Chris Masonc8b97812008-10-29 14:49:59 -04005058 compressed = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04005059 if (found_type == BTRFS_FILE_EXTENT_REG ||
5060 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04005061 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04005062 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04005063 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5064 size_t size;
5065 size = btrfs_file_extent_inline_len(leaf, item);
5066 extent_end = (extent_start + size + root->sectorsize - 1) &
5067 ~((u64)root->sectorsize - 1);
5068 }
5069
5070 if (start >= extent_end) {
5071 path->slots[0]++;
5072 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5073 ret = btrfs_next_leaf(root, path);
5074 if (ret < 0) {
5075 err = ret;
5076 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04005077 }
Yan Zheng9036c102008-10-30 14:19:41 -04005078 if (ret > 0)
5079 goto not_found;
5080 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04005081 }
Yan Zheng9036c102008-10-30 14:19:41 -04005082 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5083 if (found_key.objectid != objectid ||
5084 found_key.type != BTRFS_EXTENT_DATA_KEY)
5085 goto not_found;
5086 if (start + len <= found_key.offset)
5087 goto not_found;
5088 em->start = start;
5089 em->len = found_key.offset - start;
5090 goto not_found_em;
5091 }
5092
Yan Zhengd899e052008-10-30 14:25:28 -04005093 if (found_type == BTRFS_FILE_EXTENT_REG ||
5094 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04005095 em->start = extent_start;
5096 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005097 em->orig_start = extent_start -
5098 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04005099 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5100 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005101 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04005102 goto insert;
5103 }
Chris Masonc8b97812008-10-29 14:49:59 -04005104 if (compressed) {
5105 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
5106 em->block_start = bytenr;
5107 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5108 item);
5109 } else {
5110 bytenr += btrfs_file_extent_offset(leaf, item);
5111 em->block_start = bytenr;
5112 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04005113 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5114 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04005115 }
Chris Masona52d9a82007-08-27 16:49:44 -04005116 goto insert;
5117 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04005118 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04005119 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04005120 size_t size;
5121 size_t extent_offset;
5122 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04005123
Chris Masona52d9a82007-08-27 16:49:44 -04005124 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005125 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04005126 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04005127 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04005128 goto out;
5129 }
5130
Yan Zheng9036c102008-10-30 14:19:41 -04005131 size = btrfs_file_extent_inline_len(leaf, item);
5132 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05005133 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04005134 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04005135 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05005136 em->len = (copy_size + root->sectorsize - 1) &
5137 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05005138 em->orig_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04005139 if (compressed)
5140 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Yan689f9342007-10-29 11:41:07 -04005141 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04005142 if (create == 0 && !PageUptodate(page)) {
Chris Masonc8b97812008-10-29 14:49:59 -04005143 if (btrfs_file_extent_compression(leaf, item) ==
5144 BTRFS_COMPRESS_ZLIB) {
5145 ret = uncompress_inline(path, inode, page,
5146 pg_offset,
5147 extent_offset, item);
5148 BUG_ON(ret);
5149 } else {
5150 map = kmap(page);
5151 read_extent_buffer(leaf, map + pg_offset, ptr,
5152 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04005153 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5154 memset(map + pg_offset + copy_size, 0,
5155 PAGE_CACHE_SIZE - pg_offset -
5156 copy_size);
5157 }
Chris Masonc8b97812008-10-29 14:49:59 -04005158 kunmap(page);
5159 }
Chris Mason179e29e2007-11-01 11:28:41 -04005160 flush_dcache_page(page);
5161 } else if (create && PageUptodate(page)) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005162 WARN_ON(1);
Chris Mason179e29e2007-11-01 11:28:41 -04005163 if (!trans) {
5164 kunmap(page);
5165 free_extent_map(em);
5166 em = NULL;
5167 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04005168 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04005169 goto again;
5170 }
Chris Masonc8b97812008-10-29 14:49:59 -04005171 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05005172 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04005173 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04005174 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04005175 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04005176 }
Chris Masond1310b22008-01-24 16:13:08 -05005177 set_extent_uptodate(io_tree, em->start,
5178 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04005179 goto insert;
5180 } else {
Chris Masond3977122009-01-05 21:25:51 -05005181 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04005182 WARN_ON(1);
5183 }
5184not_found:
5185 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05005186 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04005187not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04005188 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04005189 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005190insert:
5191 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05005192 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05005193 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5194 "[%llu %llu]\n", (unsigned long long)em->start,
5195 (unsigned long long)em->len,
5196 (unsigned long long)start,
5197 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04005198 err = -EIO;
5199 goto out;
5200 }
Chris Masond1310b22008-01-24 16:13:08 -05005201
5202 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04005203 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005204 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04005205 /* it is possible that someone inserted the extent into the tree
5206 * while we had the lock dropped. It is also possible that
5207 * an overlapping map exists in the tree
5208 */
Chris Masona52d9a82007-08-27 16:49:44 -04005209 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04005210 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005211
5212 ret = 0;
5213
Chris Mason3b951512008-04-17 11:29:12 -04005214 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04005215 if (existing && (existing->start > start ||
5216 existing->start + existing->len <= start)) {
5217 free_extent_map(existing);
5218 existing = NULL;
5219 }
Chris Mason3b951512008-04-17 11:29:12 -04005220 if (!existing) {
5221 existing = lookup_extent_mapping(em_tree, em->start,
5222 em->len);
5223 if (existing) {
5224 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005225 em, start,
5226 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04005227 free_extent_map(existing);
5228 if (err) {
5229 free_extent_map(em);
5230 em = NULL;
5231 }
5232 } else {
5233 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04005234 free_extent_map(em);
5235 em = NULL;
5236 }
5237 } else {
5238 free_extent_map(em);
5239 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005240 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04005241 }
Chris Masona52d9a82007-08-27 16:49:44 -04005242 }
Chris Mason890871b2009-09-02 16:24:52 -04005243 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04005244out:
Chris Masonf4219502008-07-22 11:18:09 -04005245 if (path)
5246 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04005247 if (trans) {
5248 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05005249 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04005250 err = ret;
5251 }
Chris Masona52d9a82007-08-27 16:49:44 -04005252 if (err) {
5253 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04005254 return ERR_PTR(err);
5255 }
5256 return em;
5257}
5258
Josef Bacik4b46fce2010-05-23 11:00:55 -04005259static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5260 u64 start, u64 len)
5261{
5262 struct btrfs_root *root = BTRFS_I(inode)->root;
5263 struct btrfs_trans_handle *trans;
5264 struct extent_map *em;
5265 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5266 struct btrfs_key ins;
5267 u64 alloc_hint;
5268 int ret;
5269
5270 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5271
5272 trans = btrfs_join_transaction(root, 0);
5273 if (!trans)
5274 return ERR_PTR(-ENOMEM);
5275
5276 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5277
5278 alloc_hint = get_extent_allocation_hint(inode, start, len);
5279 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
5280 alloc_hint, (u64)-1, &ins, 1);
5281 if (ret) {
5282 em = ERR_PTR(ret);
5283 goto out;
5284 }
5285
5286 em = alloc_extent_map(GFP_NOFS);
5287 if (!em) {
5288 em = ERR_PTR(-ENOMEM);
5289 goto out;
5290 }
5291
5292 em->start = start;
5293 em->orig_start = em->start;
5294 em->len = ins.offset;
5295
5296 em->block_start = ins.objectid;
5297 em->block_len = ins.offset;
5298 em->bdev = root->fs_info->fs_devices->latest_bdev;
5299 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5300
5301 while (1) {
5302 write_lock(&em_tree->lock);
5303 ret = add_extent_mapping(em_tree, em);
5304 write_unlock(&em_tree->lock);
5305 if (ret != -EEXIST)
5306 break;
5307 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5308 }
5309
5310 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5311 ins.offset, ins.offset, 0);
5312 if (ret) {
5313 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5314 em = ERR_PTR(ret);
5315 }
5316out:
5317 btrfs_end_transaction(trans, root);
5318 return em;
5319}
5320
Chris Mason46bfbb52010-05-26 11:04:10 -04005321/*
5322 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5323 * block must be cow'd
5324 */
5325static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5326 struct inode *inode, u64 offset, u64 len)
5327{
5328 struct btrfs_path *path;
5329 int ret;
5330 struct extent_buffer *leaf;
5331 struct btrfs_root *root = BTRFS_I(inode)->root;
5332 struct btrfs_file_extent_item *fi;
5333 struct btrfs_key key;
5334 u64 disk_bytenr;
5335 u64 backref_offset;
5336 u64 extent_end;
5337 u64 num_bytes;
5338 int slot;
5339 int found_type;
5340
5341 path = btrfs_alloc_path();
5342 if (!path)
5343 return -ENOMEM;
5344
5345 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
5346 offset, 0);
5347 if (ret < 0)
5348 goto out;
5349
5350 slot = path->slots[0];
5351 if (ret == 1) {
5352 if (slot == 0) {
5353 /* can't find the item, must cow */
5354 ret = 0;
5355 goto out;
5356 }
5357 slot--;
5358 }
5359 ret = 0;
5360 leaf = path->nodes[0];
5361 btrfs_item_key_to_cpu(leaf, &key, slot);
5362 if (key.objectid != inode->i_ino ||
5363 key.type != BTRFS_EXTENT_DATA_KEY) {
5364 /* not our file or wrong item type, must cow */
5365 goto out;
5366 }
5367
5368 if (key.offset > offset) {
5369 /* Wrong offset, must cow */
5370 goto out;
5371 }
5372
5373 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5374 found_type = btrfs_file_extent_type(leaf, fi);
5375 if (found_type != BTRFS_FILE_EXTENT_REG &&
5376 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5377 /* not a regular extent, must cow */
5378 goto out;
5379 }
5380 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5381 backref_offset = btrfs_file_extent_offset(leaf, fi);
5382
5383 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5384 if (extent_end < offset + len) {
5385 /* extent doesn't include our full range, must cow */
5386 goto out;
5387 }
5388
5389 if (btrfs_extent_readonly(root, disk_bytenr))
5390 goto out;
5391
5392 /*
5393 * look for other files referencing this extent, if we
5394 * find any we must cow
5395 */
5396 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
5397 key.offset - backref_offset, disk_bytenr))
5398 goto out;
5399
5400 /*
5401 * adjust disk_bytenr and num_bytes to cover just the bytes
5402 * in this extent we are about to write. If there
5403 * are any csums in that range we have to cow in order
5404 * to keep the csums correct
5405 */
5406 disk_bytenr += backref_offset;
5407 disk_bytenr += offset - key.offset;
5408 num_bytes = min(offset + len, extent_end) - offset;
5409 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5410 goto out;
5411 /*
5412 * all of the above have passed, it is safe to overwrite this extent
5413 * without cow
5414 */
5415 ret = 1;
5416out:
5417 btrfs_free_path(path);
5418 return ret;
5419}
5420
Josef Bacik4b46fce2010-05-23 11:00:55 -04005421static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5422 struct buffer_head *bh_result, int create)
5423{
5424 struct extent_map *em;
5425 struct btrfs_root *root = BTRFS_I(inode)->root;
5426 u64 start = iblock << inode->i_blkbits;
5427 u64 len = bh_result->b_size;
Chris Mason46bfbb52010-05-26 11:04:10 -04005428 struct btrfs_trans_handle *trans;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005429
5430 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
5431 if (IS_ERR(em))
5432 return PTR_ERR(em);
5433
5434 /*
5435 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5436 * io. INLINE is special, and we could probably kludge it in here, but
5437 * it's still buffered so for safety lets just fall back to the generic
5438 * buffered path.
5439 *
5440 * For COMPRESSED we _have_ to read the entire extent in so we can
5441 * decompress it, so there will be buffering required no matter what we
5442 * do, so go ahead and fallback to buffered.
5443 *
5444 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5445 * to buffered IO. Don't blame me, this is the price we pay for using
5446 * the generic code.
5447 */
5448 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5449 em->block_start == EXTENT_MAP_INLINE) {
5450 free_extent_map(em);
5451 return -ENOTBLK;
5452 }
5453
5454 /* Just a good old fashioned hole, return */
5455 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
5456 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5457 free_extent_map(em);
5458 /* DIO will do one hole at a time, so just unlock a sector */
5459 unlock_extent(&BTRFS_I(inode)->io_tree, start,
5460 start + root->sectorsize - 1, GFP_NOFS);
5461 return 0;
5462 }
5463
5464 /*
5465 * We don't allocate a new extent in the following cases
5466 *
5467 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5468 * existing extent.
5469 * 2) The extent is marked as PREALLOC. We're good to go here and can
5470 * just use the extent.
5471 *
5472 */
Chris Mason46bfbb52010-05-26 11:04:10 -04005473 if (!create) {
5474 len = em->len - (start - em->start);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005475 goto map;
Chris Mason46bfbb52010-05-26 11:04:10 -04005476 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04005477
5478 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
5479 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
5480 em->block_start != EXTENT_MAP_HOLE)) {
Josef Bacik4b46fce2010-05-23 11:00:55 -04005481 int type;
5482 int ret;
Chris Mason46bfbb52010-05-26 11:04:10 -04005483 u64 block_start;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005484
5485 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5486 type = BTRFS_ORDERED_PREALLOC;
5487 else
5488 type = BTRFS_ORDERED_NOCOW;
Chris Mason46bfbb52010-05-26 11:04:10 -04005489 len = min(len, em->len - (start - em->start));
Josef Bacik4b46fce2010-05-23 11:00:55 -04005490 block_start = em->block_start + (start - em->start);
Chris Mason46bfbb52010-05-26 11:04:10 -04005491
5492 /*
5493 * we're not going to log anything, but we do need
5494 * to make sure the current transaction stays open
5495 * while we look for nocow cross refs
5496 */
5497 trans = btrfs_join_transaction(root, 0);
5498 if (!trans)
5499 goto must_cow;
5500
5501 if (can_nocow_odirect(trans, inode, start, len) == 1) {
5502 ret = btrfs_add_ordered_extent_dio(inode, start,
5503 block_start, len, len, type);
5504 btrfs_end_transaction(trans, root);
5505 if (ret) {
5506 free_extent_map(em);
5507 return ret;
5508 }
5509 goto unlock;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005510 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005511 btrfs_end_transaction(trans, root);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005512 }
Chris Mason46bfbb52010-05-26 11:04:10 -04005513must_cow:
5514 /*
5515 * this will cow the extent, reset the len in case we changed
5516 * it above
5517 */
5518 len = bh_result->b_size;
5519 free_extent_map(em);
5520 em = btrfs_new_extent_direct(inode, start, len);
5521 if (IS_ERR(em))
5522 return PTR_ERR(em);
5523 len = min(len, em->len - (start - em->start));
5524unlock:
Chris Mason4845e442010-05-25 20:56:50 -04005525 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
5526 EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
5527 0, NULL, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005528map:
5529 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
5530 inode->i_blkbits;
Chris Mason46bfbb52010-05-26 11:04:10 -04005531 bh_result->b_size = len;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005532 bh_result->b_bdev = em->bdev;
5533 set_buffer_mapped(bh_result);
5534 if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5535 set_buffer_new(bh_result);
5536
5537 free_extent_map(em);
5538
5539 return 0;
5540}
5541
5542struct btrfs_dio_private {
5543 struct inode *inode;
5544 u64 logical_offset;
5545 u64 disk_bytenr;
5546 u64 bytes;
5547 u32 *csums;
5548 void *private;
5549};
5550
5551static void btrfs_endio_direct_read(struct bio *bio, int err)
5552{
5553 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
5554 struct bio_vec *bvec = bio->bi_io_vec;
5555 struct btrfs_dio_private *dip = bio->bi_private;
5556 struct inode *inode = dip->inode;
5557 struct btrfs_root *root = BTRFS_I(inode)->root;
5558 u64 start;
5559 u32 *private = dip->csums;
5560
5561 start = dip->logical_offset;
5562 do {
5563 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
5564 struct page *page = bvec->bv_page;
5565 char *kaddr;
5566 u32 csum = ~(u32)0;
5567 unsigned long flags;
5568
5569 local_irq_save(flags);
5570 kaddr = kmap_atomic(page, KM_IRQ0);
5571 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
5572 csum, bvec->bv_len);
5573 btrfs_csum_final(csum, (char *)&csum);
5574 kunmap_atomic(kaddr, KM_IRQ0);
5575 local_irq_restore(flags);
5576
5577 flush_dcache_page(bvec->bv_page);
5578 if (csum != *private) {
5579 printk(KERN_ERR "btrfs csum failed ino %lu off"
5580 " %llu csum %u private %u\n",
5581 inode->i_ino, (unsigned long long)start,
5582 csum, *private);
5583 err = -EIO;
5584 }
5585 }
5586
5587 start += bvec->bv_len;
5588 private++;
5589 bvec++;
5590 } while (bvec <= bvec_end);
5591
5592 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
5593 dip->logical_offset + dip->bytes - 1, GFP_NOFS);
5594 bio->bi_private = dip->private;
5595
5596 kfree(dip->csums);
5597 kfree(dip);
5598 dio_end_io(bio, err);
5599}
5600
5601static void btrfs_endio_direct_write(struct bio *bio, int err)
5602{
5603 struct btrfs_dio_private *dip = bio->bi_private;
5604 struct inode *inode = dip->inode;
5605 struct btrfs_root *root = BTRFS_I(inode)->root;
5606 struct btrfs_trans_handle *trans;
5607 struct btrfs_ordered_extent *ordered = NULL;
5608 struct extent_state *cached_state = NULL;
5609 int ret;
5610
5611 if (err)
5612 goto out_done;
5613
5614 ret = btrfs_dec_test_ordered_pending(inode, &ordered,
5615 dip->logical_offset, dip->bytes);
5616 if (!ret)
5617 goto out_done;
5618
5619 BUG_ON(!ordered);
5620
5621 trans = btrfs_join_transaction(root, 1);
5622 if (!trans) {
5623 err = -ENOMEM;
5624 goto out;
5625 }
5626 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5627
5628 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
5629 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5630 if (!ret)
5631 ret = btrfs_update_inode(trans, root, inode);
5632 err = ret;
5633 goto out;
5634 }
5635
5636 lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5637 ordered->file_offset + ordered->len - 1, 0,
5638 &cached_state, GFP_NOFS);
5639
5640 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
5641 ret = btrfs_mark_extent_written(trans, inode,
5642 ordered->file_offset,
5643 ordered->file_offset +
5644 ordered->len);
5645 if (ret) {
5646 err = ret;
5647 goto out_unlock;
5648 }
5649 } else {
5650 ret = insert_reserved_file_extent(trans, inode,
5651 ordered->file_offset,
5652 ordered->start,
5653 ordered->disk_len,
5654 ordered->len,
5655 ordered->len,
5656 0, 0, 0,
5657 BTRFS_FILE_EXTENT_REG);
5658 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
5659 ordered->file_offset, ordered->len);
5660 if (ret) {
5661 err = ret;
5662 WARN_ON(1);
5663 goto out_unlock;
5664 }
5665 }
5666
5667 add_pending_csums(trans, inode, ordered->file_offset, &ordered->list);
5668 btrfs_ordered_update_i_size(inode, 0, ordered);
5669 btrfs_update_inode(trans, root, inode);
5670out_unlock:
5671 unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5672 ordered->file_offset + ordered->len - 1,
5673 &cached_state, GFP_NOFS);
5674out:
5675 btrfs_delalloc_release_metadata(inode, ordered->len);
5676 btrfs_end_transaction(trans, root);
5677 btrfs_put_ordered_extent(ordered);
5678 btrfs_put_ordered_extent(ordered);
5679out_done:
5680 bio->bi_private = dip->private;
5681
5682 kfree(dip->csums);
5683 kfree(dip);
5684 dio_end_io(bio, err);
5685}
5686
Chris Masoneaf25d92010-05-25 09:48:28 -04005687static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
5688 struct bio *bio, int mirror_num,
5689 unsigned long bio_flags, u64 offset)
5690{
5691 int ret;
5692 struct btrfs_root *root = BTRFS_I(inode)->root;
5693 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
5694 BUG_ON(ret);
5695 return 0;
5696}
5697
Josef Bacik4b46fce2010-05-23 11:00:55 -04005698static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
5699 loff_t file_offset)
5700{
5701 struct btrfs_root *root = BTRFS_I(inode)->root;
5702 struct btrfs_dio_private *dip;
5703 struct bio_vec *bvec = bio->bi_io_vec;
5704 u64 start;
5705 int skip_sum;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005706 int write = rw & REQ_WRITE;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005707 int ret = 0;
5708
5709 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
5710
5711 dip = kmalloc(sizeof(*dip), GFP_NOFS);
5712 if (!dip) {
5713 ret = -ENOMEM;
5714 goto free_ordered;
5715 }
5716 dip->csums = NULL;
5717
5718 if (!skip_sum) {
5719 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
5720 if (!dip->csums) {
5721 ret = -ENOMEM;
5722 goto free_ordered;
5723 }
5724 }
5725
5726 dip->private = bio->bi_private;
5727 dip->inode = inode;
5728 dip->logical_offset = file_offset;
5729
5730 start = dip->logical_offset;
5731 dip->bytes = 0;
5732 do {
5733 dip->bytes += bvec->bv_len;
5734 bvec++;
5735 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
5736
Chris Mason46bfbb52010-05-26 11:04:10 -04005737 dip->disk_bytenr = (u64)bio->bi_sector << 9;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005738 bio->bi_private = dip;
5739
5740 if (write)
5741 bio->bi_end_io = btrfs_endio_direct_write;
5742 else
5743 bio->bi_end_io = btrfs_endio_direct_read;
5744
5745 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
5746 if (ret)
5747 goto out_err;
5748
Chris Masoneaf25d92010-05-25 09:48:28 -04005749 if (write && !skip_sum) {
5750 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
5751 inode, rw, bio, 0, 0,
5752 dip->logical_offset,
5753 __btrfs_submit_bio_start_direct_io,
5754 __btrfs_submit_bio_done);
5755 if (ret)
5756 goto out_err;
5757 return;
5758 } else if (!skip_sum)
Josef Bacik4b46fce2010-05-23 11:00:55 -04005759 btrfs_lookup_bio_sums_dio(root, inode, bio,
5760 dip->logical_offset, dip->csums);
5761
Chris Masoneaf25d92010-05-25 09:48:28 -04005762 ret = btrfs_map_bio(root, rw, bio, 0, 1);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005763 if (ret)
5764 goto out_err;
5765 return;
5766out_err:
5767 kfree(dip->csums);
5768 kfree(dip);
5769free_ordered:
5770 /*
5771 * If this is a write, we need to clean up the reserved space and kill
5772 * the ordered extent.
5773 */
5774 if (write) {
5775 struct btrfs_ordered_extent *ordered;
5776 ordered = btrfs_lookup_ordered_extent(inode,
5777 dip->logical_offset);
5778 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
5779 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
5780 btrfs_free_reserved_extent(root, ordered->start,
5781 ordered->disk_len);
5782 btrfs_put_ordered_extent(ordered);
5783 btrfs_put_ordered_extent(ordered);
5784 }
5785 bio_endio(bio, ret);
5786}
5787
Chris Mason5a5f79b2010-05-26 21:33:37 -04005788static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
5789 const struct iovec *iov, loff_t offset,
5790 unsigned long nr_segs)
5791{
5792 int seg;
5793 size_t size;
5794 unsigned long addr;
5795 unsigned blocksize_mask = root->sectorsize - 1;
5796 ssize_t retval = -EINVAL;
5797 loff_t end = offset;
5798
5799 if (offset & blocksize_mask)
5800 goto out;
5801
5802 /* Check the memory alignment. Blocks cannot straddle pages */
5803 for (seg = 0; seg < nr_segs; seg++) {
5804 addr = (unsigned long)iov[seg].iov_base;
5805 size = iov[seg].iov_len;
5806 end += size;
5807 if ((addr & blocksize_mask) || (size & blocksize_mask))
5808 goto out;
5809 }
5810 retval = 0;
5811out:
5812 return retval;
5813}
Chris Mason16432982008-04-10 10:23:21 -04005814static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
5815 const struct iovec *iov, loff_t offset,
5816 unsigned long nr_segs)
5817{
Josef Bacik4b46fce2010-05-23 11:00:55 -04005818 struct file *file = iocb->ki_filp;
5819 struct inode *inode = file->f_mapping->host;
5820 struct btrfs_ordered_extent *ordered;
Chris Mason4845e442010-05-25 20:56:50 -04005821 struct extent_state *cached_state = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04005822 u64 lockstart, lockend;
5823 ssize_t ret;
Chris Mason4845e442010-05-25 20:56:50 -04005824 int writing = rw & WRITE;
5825 int write_bits = 0;
Chris Mason3f7c5792010-05-26 10:59:53 -04005826 size_t count = iov_length(iov, nr_segs);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005827
Chris Mason5a5f79b2010-05-26 21:33:37 -04005828 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
5829 offset, nr_segs)) {
5830 return 0;
5831 }
5832
Josef Bacik4b46fce2010-05-23 11:00:55 -04005833 lockstart = offset;
Chris Mason3f7c5792010-05-26 10:59:53 -04005834 lockend = offset + count - 1;
5835
5836 if (writing) {
5837 ret = btrfs_delalloc_reserve_space(inode, count);
5838 if (ret)
5839 goto out;
5840 }
Chris Mason4845e442010-05-25 20:56:50 -04005841
Josef Bacik4b46fce2010-05-23 11:00:55 -04005842 while (1) {
Chris Mason4845e442010-05-25 20:56:50 -04005843 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5844 0, &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005845 /*
5846 * We're concerned with the entire range that we're going to be
5847 * doing DIO to, so we need to make sure theres no ordered
5848 * extents in this range.
5849 */
5850 ordered = btrfs_lookup_ordered_range(inode, lockstart,
5851 lockend - lockstart + 1);
5852 if (!ordered)
5853 break;
Chris Mason4845e442010-05-25 20:56:50 -04005854 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5855 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005856 btrfs_start_ordered_extent(inode, ordered, 1);
5857 btrfs_put_ordered_extent(ordered);
5858 cond_resched();
5859 }
5860
Chris Mason4845e442010-05-25 20:56:50 -04005861 /*
5862 * we don't use btrfs_set_extent_delalloc because we don't want
5863 * the dirty or uptodate bits
5864 */
5865 if (writing) {
5866 write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
5867 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5868 EXTENT_DELALLOC, 0, NULL, &cached_state,
5869 GFP_NOFS);
5870 if (ret) {
5871 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
5872 lockend, EXTENT_LOCKED | write_bits,
5873 1, 0, &cached_state, GFP_NOFS);
5874 goto out;
5875 }
5876 }
5877
5878 free_extent_state(cached_state);
5879 cached_state = NULL;
5880
Chris Mason5a5f79b2010-05-26 21:33:37 -04005881 ret = __blockdev_direct_IO(rw, iocb, inode,
5882 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
5883 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
5884 btrfs_submit_direct, 0);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005885
5886 if (ret < 0 && ret != -EIOCBQUEUED) {
Chris Mason4845e442010-05-25 20:56:50 -04005887 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
5888 offset + iov_length(iov, nr_segs) - 1,
5889 EXTENT_LOCKED | write_bits, 1, 0,
5890 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005891 } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
5892 /*
5893 * We're falling back to buffered, unlock the section we didn't
5894 * do IO on.
5895 */
Chris Mason4845e442010-05-25 20:56:50 -04005896 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
5897 offset + iov_length(iov, nr_segs) - 1,
5898 EXTENT_LOCKED | write_bits, 1, 0,
5899 &cached_state, GFP_NOFS);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005900 }
Chris Mason4845e442010-05-25 20:56:50 -04005901out:
5902 free_extent_state(cached_state);
Josef Bacik4b46fce2010-05-23 11:00:55 -04005903 return ret;
Chris Mason16432982008-04-10 10:23:21 -04005904}
5905
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05005906static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
5907 __u64 start, __u64 len)
5908{
5909 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
5910}
5911
Chris Mason9ebefb182007-06-15 13:50:00 -04005912int btrfs_readpage(struct file *file, struct page *page)
5913{
Chris Masond1310b22008-01-24 16:13:08 -05005914 struct extent_io_tree *tree;
5915 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005916 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04005917}
Chris Mason1832a6d2007-12-21 16:27:21 -05005918
Chris Mason39279cc2007-06-12 06:35:45 -04005919static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
5920{
Chris Masond1310b22008-01-24 16:13:08 -05005921 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04005922
5923
5924 if (current->flags & PF_MEMALLOC) {
5925 redirty_page_for_writepage(wbc, page);
5926 unlock_page(page);
5927 return 0;
5928 }
Chris Masond1310b22008-01-24 16:13:08 -05005929 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005930 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
5931}
Chris Mason39279cc2007-06-12 06:35:45 -04005932
Chris Masonf4219502008-07-22 11:18:09 -04005933int btrfs_writepages(struct address_space *mapping,
5934 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04005935{
Chris Masond1310b22008-01-24 16:13:08 -05005936 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05005937
Chris Masond1310b22008-01-24 16:13:08 -05005938 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04005939 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
5940}
5941
Chris Mason3ab2fb52007-11-08 10:59:22 -05005942static int
5943btrfs_readpages(struct file *file, struct address_space *mapping,
5944 struct list_head *pages, unsigned nr_pages)
5945{
Chris Masond1310b22008-01-24 16:13:08 -05005946 struct extent_io_tree *tree;
5947 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05005948 return extent_readpages(tree, mapping, pages, nr_pages,
5949 btrfs_get_extent);
5950}
Chris Masone6dcd2d2008-07-17 12:53:50 -04005951static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04005952{
Chris Masond1310b22008-01-24 16:13:08 -05005953 struct extent_io_tree *tree;
5954 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04005955 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005956
Chris Masond1310b22008-01-24 16:13:08 -05005957 tree = &BTRFS_I(page->mapping->host)->io_tree;
5958 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05005959 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005960 if (ret == 1) {
5961 ClearPagePrivate(page);
5962 set_page_private(page, 0);
5963 page_cache_release(page);
5964 }
5965 return ret;
5966}
Chris Mason39279cc2007-06-12 06:35:45 -04005967
Chris Masone6dcd2d2008-07-17 12:53:50 -04005968static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
5969{
Chris Mason98509cf2008-09-11 15:51:43 -04005970 if (PageWriteback(page) || PageDirty(page))
5971 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05005972 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005973}
5974
Chris Masona52d9a82007-08-27 16:49:44 -04005975static void btrfs_invalidatepage(struct page *page, unsigned long offset)
5976{
Chris Masond1310b22008-01-24 16:13:08 -05005977 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005978 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005979 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005980 u64 page_start = page_offset(page);
5981 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04005982
Chris Mason8b62b722009-09-02 16:53:46 -04005983
5984 /*
5985 * we have the page locked, so new writeback can't start,
5986 * and the dirty bit won't be cleared while we are here.
5987 *
5988 * Wait for IO on this page so that we can safely clear
5989 * the PagePrivate2 bit and do ordered accounting
5990 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005991 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04005992
Chris Masond1310b22008-01-24 16:13:08 -05005993 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005994 if (offset) {
5995 btrfs_releasepage(page, GFP_NOFS);
5996 return;
5997 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00005998 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
5999 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006000 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
6001 page_offset(page));
6002 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04006003 /*
6004 * IO on this page will never be started, so we need
6005 * to account for any ordered extents now
6006 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006007 clear_extent_bit(tree, page_start, page_end,
6008 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04006009 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006010 &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04006011 /*
6012 * whoever cleared the private bit is responsible
6013 * for the finish_ordered_io
6014 */
6015 if (TestClearPagePrivate2(page)) {
6016 btrfs_finish_ordered_io(page->mapping->host,
6017 page_start, page_end);
6018 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006019 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006020 cached_state = NULL;
6021 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6022 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006023 }
6024 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006025 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +00006026 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006027 __btrfs_releasepage(page, GFP_NOFS);
6028
Chris Mason4a096752008-07-21 10:29:44 -04006029 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04006030 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04006031 ClearPagePrivate(page);
6032 set_page_private(page, 0);
6033 page_cache_release(page);
6034 }
Chris Mason39279cc2007-06-12 06:35:45 -04006035}
6036
Chris Mason9ebefb182007-06-15 13:50:00 -04006037/*
6038 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6039 * called from a page fault handler when a page is first dirtied. Hence we must
6040 * be careful to check for EOF conditions here. We set the page up correctly
6041 * for a written page which means we get ENOSPC checking when writing into
6042 * holes and correct delalloc and unwritten extent mapping on filesystems that
6043 * support these features.
6044 *
6045 * We are not allowed to take the i_mutex here so we have to play games to
6046 * protect against truncate races as the page could now be beyond EOF. Because
6047 * vmtruncate() writes the inode size before removing pages, once we have the
6048 * page lock we can determine safely if the page is beyond EOF. If it is not
6049 * beyond EOF, then the page is guaranteed safe against truncation until we
6050 * unlock the page.
6051 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07006052int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04006053{
Nick Pigginc2ec1752009-03-31 15:23:21 -07006054 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05006055 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05006056 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006057 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6058 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00006059 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006060 char *kaddr;
6061 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04006062 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05006063 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04006064 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006065 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04006066
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006067 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Nick Piggin56a76f82009-03-31 15:23:23 -07006068 if (ret) {
6069 if (ret == -ENOMEM)
6070 ret = VM_FAULT_OOM;
6071 else /* -ENOSPC, -EIO, etc */
6072 ret = VM_FAULT_SIGBUS;
Chris Mason1832a6d2007-12-21 16:27:21 -05006073 goto out;
Nick Piggin56a76f82009-03-31 15:23:23 -07006074 }
Chris Mason1832a6d2007-12-21 16:27:21 -05006075
Nick Piggin56a76f82009-03-31 15:23:23 -07006076 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006077again:
Chris Mason9ebefb182007-06-15 13:50:00 -04006078 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04006079 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006080 page_start = page_offset(page);
6081 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04006082
Chris Mason9ebefb182007-06-15 13:50:00 -04006083 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04006084 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04006085 /* page got truncated out from underneath us */
6086 goto out_unlock;
6087 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006088 wait_on_page_writeback(page);
6089
Josef Bacik2ac55d42010-02-03 19:33:23 +00006090 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
6091 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006092 set_page_extent_mapped(page);
6093
Chris Masoneb84ae02008-07-17 13:53:27 -04006094 /*
6095 * we can't set the delalloc bits if there are pending ordered
6096 * extents. Drop our locks and wait for them to finish
6097 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04006098 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6099 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006100 unlock_extent_cached(io_tree, page_start, page_end,
6101 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006102 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04006103 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006104 btrfs_put_ordered_extent(ordered);
6105 goto again;
6106 }
6107
Josef Bacikfbf19082009-10-01 17:10:23 -04006108 /*
6109 * XXX - page_mkwrite gets called every time the page is dirtied, even
6110 * if it was already dirty, so for space accounting reasons we need to
6111 * clear any delalloc bits for the range we are fixing to save. There
6112 * is probably a better way to do this, but for now keep consistent with
6113 * prepare_pages in the normal write path.
6114 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006115 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04006116 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00006117 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04006118
Josef Bacik2ac55d42010-02-03 19:33:23 +00006119 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6120 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006121 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00006122 unlock_extent_cached(io_tree, page_start, page_end,
6123 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006124 ret = VM_FAULT_SIGBUS;
6125 goto out_unlock;
6126 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006127 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04006128
6129 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04006130 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006131 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04006132 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04006133 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04006134
Chris Masone6dcd2d2008-07-17 12:53:50 -04006135 if (zero_start != PAGE_CACHE_SIZE) {
6136 kaddr = kmap(page);
6137 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6138 flush_dcache_page(page);
6139 kunmap(page);
6140 }
Chris Mason247e7432008-07-17 12:53:51 -04006141 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006142 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04006143 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006144
Chris Mason257c62e2009-10-13 13:21:08 -04006145 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6146 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
6147
Josef Bacik2ac55d42010-02-03 19:33:23 +00006148 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04006149
6150out_unlock:
Chris Mason50a9b212009-09-11 12:33:12 -04006151 if (!ret)
6152 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04006153 unlock_page(page);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006154 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason1832a6d2007-12-21 16:27:21 -05006155out:
Chris Mason9ebefb182007-06-15 13:50:00 -04006156 return ret;
6157}
6158
Chris Mason39279cc2007-06-12 06:35:45 -04006159static void btrfs_truncate(struct inode *inode)
6160{
6161 struct btrfs_root *root = BTRFS_I(inode)->root;
6162 int ret;
6163 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04006164 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04006165 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04006166
Yan, Zheng80825102009-11-12 09:35:36 +00006167 if (!S_ISREG(inode->i_mode)) {
6168 WARN_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -04006169 return;
Yan, Zheng80825102009-11-12 09:35:36 +00006170 }
Chris Mason39279cc2007-06-12 06:35:45 -04006171
Josef Bacik5d5e1032009-10-13 16:46:49 -04006172 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
6173 if (ret)
6174 return;
Yan, Zheng80825102009-11-12 09:35:36 +00006175
Chris Mason4a096752008-07-21 10:29:44 -04006176 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00006177 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006178
Yan, Zhengd68fc572010-05-16 10:49:58 -04006179 trans = btrfs_start_transaction(root, 0);
6180 BUG_ON(IS_ERR(trans));
Yan, Zheng80825102009-11-12 09:35:36 +00006181 btrfs_set_trans_block_group(trans, inode);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006182 trans->block_rsv = root->orphan_block_rsv;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006183
6184 /*
6185 * setattr is responsible for setting the ordered_data_close flag,
6186 * but that is only tested during the last file release. That
6187 * could happen well after the next commit, leaving a great big
6188 * window where new writes may get lost if someone chooses to write
6189 * to this file after truncating to zero
6190 *
6191 * The inode doesn't have any dirty data here, and so if we commit
6192 * this is a noop. If someone immediately starts writing to the inode
6193 * it is very likely we'll catch some of their writes in this
6194 * transaction, and the commit will find this file on the ordered
6195 * data list with good things to send down.
6196 *
6197 * This is a best effort solution, there is still a window where
6198 * using truncate to replace the contents of the file will
6199 * end up with a zero length file after a crash.
6200 */
6201 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
6202 btrfs_add_ordered_operation(trans, root, inode);
6203
Yan, Zheng80825102009-11-12 09:35:36 +00006204 while (1) {
Yan, Zhengd68fc572010-05-16 10:49:58 -04006205 if (!trans) {
6206 trans = btrfs_start_transaction(root, 0);
6207 BUG_ON(IS_ERR(trans));
6208 btrfs_set_trans_block_group(trans, inode);
6209 trans->block_rsv = root->orphan_block_rsv;
6210 }
6211
6212 ret = btrfs_block_rsv_check(trans, root,
6213 root->orphan_block_rsv, 0, 5);
6214 if (ret) {
6215 BUG_ON(ret != -EAGAIN);
6216 ret = btrfs_commit_transaction(trans, root);
6217 BUG_ON(ret);
6218 trans = NULL;
6219 continue;
6220 }
6221
Yan, Zheng80825102009-11-12 09:35:36 +00006222 ret = btrfs_truncate_inode_items(trans, root, inode,
6223 inode->i_size,
6224 BTRFS_EXTENT_DATA_KEY);
6225 if (ret != -EAGAIN)
6226 break;
Chris Mason39279cc2007-06-12 06:35:45 -04006227
Yan, Zheng80825102009-11-12 09:35:36 +00006228 ret = btrfs_update_inode(trans, root, inode);
6229 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04006230
Yan, Zheng80825102009-11-12 09:35:36 +00006231 nr = trans->blocks_used;
6232 btrfs_end_transaction(trans, root);
Yan, Zhengd68fc572010-05-16 10:49:58 -04006233 trans = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00006234 btrfs_btree_balance_dirty(root, nr);
Yan, Zheng80825102009-11-12 09:35:36 +00006235 }
6236
6237 if (ret == 0 && inode->i_nlink > 0) {
6238 ret = btrfs_orphan_del(trans, inode);
6239 BUG_ON(ret);
6240 }
6241
6242 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04006243 BUG_ON(ret);
6244
Josef Bacik7b128762008-07-24 12:17:14 -04006245 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04006246 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006247 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04006248 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04006249}
6250
Sven Wegener3b963622008-06-09 21:57:42 -04006251/*
Chris Masond352ac62008-09-29 15:18:18 -04006252 * create a new subvolume directory/inode (helper for the ioctl).
6253 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05006254int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Yan, Zheng76dda932009-09-21 16:00:26 -04006255 struct btrfs_root *new_root,
Yan Zhengd2fb3432008-12-11 16:30:39 -05006256 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04006257{
Chris Mason39279cc2007-06-12 06:35:45 -04006258 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04006259 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006260 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006261
Josef Bacikaec74772008-07-24 12:12:38 -04006262 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05006263 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04006264 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04006265 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006266 inode->i_op = &btrfs_dir_inode_operations;
6267 inode->i_fop = &btrfs_dir_file_operations;
6268
Chris Mason39279cc2007-06-12 06:35:45 -04006269 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04006270 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04006271
Yan, Zheng76dda932009-09-21 16:00:26 -04006272 err = btrfs_update_inode(trans, new_root, inode);
6273 BUG_ON(err);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006274
Yan, Zheng76dda932009-09-21 16:00:26 -04006275 iput(inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04006276 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006277}
6278
Chris Masond352ac62008-09-29 15:18:18 -04006279/* helper function for file defrag and space balancing. This
6280 * forces readahead on a given range of bytes in an inode
6281 */
Chris Masonedbd8d42007-12-21 16:27:24 -05006282unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04006283 struct file_ra_state *ra, struct file *file,
6284 pgoff_t offset, pgoff_t last_index)
6285{
Chris Mason8e7bf942008-04-28 09:02:36 -04006286 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04006287
Chris Mason86479a02007-09-10 19:58:16 -04006288 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
6289 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04006290}
6291
Chris Mason39279cc2007-06-12 06:35:45 -04006292struct inode *btrfs_alloc_inode(struct super_block *sb)
6293{
6294 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006295 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006296
6297 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
6298 if (!ei)
6299 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006300
6301 ei->root = NULL;
6302 ei->space_info = NULL;
6303 ei->generation = 0;
6304 ei->sequence = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04006305 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04006306 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04006307 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006308 ei->delalloc_bytes = 0;
6309 ei->reserved_bytes = 0;
6310 ei->disk_i_size = 0;
6311 ei->flags = 0;
6312 ei->index_cnt = (u64)-1;
6313 ei->last_unlink_trans = 0;
6314
Josef Bacik32c00af2009-10-08 13:34:05 -04006315 spin_lock_init(&ei->accounting_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006316 atomic_set(&ei->outstanding_extents, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006317 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006318
6319 ei->ordered_data_close = 0;
Yan, Zhengd68fc572010-05-16 10:49:58 -04006320 ei->orphan_meta_reserved = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006321 ei->dummy_inode = 0;
6322 ei->force_compress = 0;
6323
6324 inode = &ei->vfs_inode;
6325 extent_map_tree_init(&ei->extent_tree, GFP_NOFS);
6326 extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS);
6327 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS);
6328 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006329 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04006330 INIT_LIST_HEAD(&ei->i_orphan);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006331 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04006332 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04006333 RB_CLEAR_NODE(&ei->rb_node);
6334
6335 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04006336}
6337
6338void btrfs_destroy_inode(struct inode *inode)
6339{
Chris Masone6dcd2d2008-07-17 12:53:50 -04006340 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006341 struct btrfs_root *root = BTRFS_I(inode)->root;
6342
Chris Mason39279cc2007-06-12 06:35:45 -04006343 WARN_ON(!list_empty(&inode->i_dentry));
6344 WARN_ON(inode->i_data.nrpages);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006345 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents));
6346 WARN_ON(BTRFS_I(inode)->reserved_extents);
Chris Mason39279cc2007-06-12 06:35:45 -04006347
Chris Mason5a3f23d2009-03-31 13:27:11 -04006348 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05006349 * This can happen where we create an inode, but somebody else also
6350 * created the same inode and we need to destroy the one we already
6351 * created.
6352 */
6353 if (!root)
6354 goto free;
6355
6356 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04006357 * Make sure we're properly removed from the ordered operation
6358 * lists.
6359 */
6360 smp_mb();
6361 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
6362 spin_lock(&root->fs_info->ordered_extent_lock);
6363 list_del_init(&BTRFS_I(inode)->ordered_operations);
6364 spin_unlock(&root->fs_info->ordered_extent_lock);
6365 }
6366
Josef Bacik0af3d002010-06-21 14:48:16 -04006367 if (root == root->fs_info->tree_root) {
6368 struct btrfs_block_group_cache *block_group;
6369
6370 block_group = btrfs_lookup_block_group(root->fs_info,
6371 BTRFS_I(inode)->block_group);
6372 if (block_group && block_group->inode == inode) {
6373 spin_lock(&block_group->lock);
6374 block_group->inode = NULL;
6375 spin_unlock(&block_group->lock);
6376 btrfs_put_block_group(block_group);
6377 } else if (block_group) {
6378 btrfs_put_block_group(block_group);
6379 }
6380 }
6381
Yan, Zhengd68fc572010-05-16 10:49:58 -04006382 spin_lock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006383 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yan, Zheng80825102009-11-12 09:35:36 +00006384 printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n",
6385 inode->i_ino);
6386 list_del_init(&BTRFS_I(inode)->i_orphan);
Josef Bacik7b128762008-07-24 12:17:14 -04006387 }
Yan, Zhengd68fc572010-05-16 10:49:58 -04006388 spin_unlock(&root->orphan_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04006389
Chris Masond3977122009-01-05 21:25:51 -05006390 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04006391 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
6392 if (!ordered)
6393 break;
6394 else {
Chris Masond3977122009-01-05 21:25:51 -05006395 printk(KERN_ERR "btrfs found ordered "
6396 "extent %llu %llu on inode cleanup\n",
6397 (unsigned long long)ordered->file_offset,
6398 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006399 btrfs_remove_ordered_extent(inode, ordered);
6400 btrfs_put_ordered_extent(ordered);
6401 btrfs_put_ordered_extent(ordered);
6402 }
6403 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006404 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006405 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05006406free:
Chris Mason39279cc2007-06-12 06:35:45 -04006407 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
6408}
6409
Al Viro45321ac2010-06-07 13:43:19 -04006410int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04006411{
6412 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04006413
Josef Bacik0af3d002010-06-21 14:48:16 -04006414 if (btrfs_root_refs(&root->root_item) == 0 &&
6415 root != root->fs_info->tree_root)
Al Viro45321ac2010-06-07 13:43:19 -04006416 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04006417 else
Al Viro45321ac2010-06-07 13:43:19 -04006418 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04006419}
6420
Sven Wegener0ee0fda2008-07-30 16:54:26 -04006421static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04006422{
6423 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
6424
6425 inode_init_once(&ei->vfs_inode);
6426}
6427
6428void btrfs_destroy_cachep(void)
6429{
6430 if (btrfs_inode_cachep)
6431 kmem_cache_destroy(btrfs_inode_cachep);
6432 if (btrfs_trans_handle_cachep)
6433 kmem_cache_destroy(btrfs_trans_handle_cachep);
6434 if (btrfs_transaction_cachep)
6435 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04006436 if (btrfs_path_cachep)
6437 kmem_cache_destroy(btrfs_path_cachep);
6438}
6439
6440int btrfs_init_cachep(void)
6441{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006442 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
6443 sizeof(struct btrfs_inode), 0,
6444 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04006445 if (!btrfs_inode_cachep)
6446 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006447
6448 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
6449 sizeof(struct btrfs_trans_handle), 0,
6450 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006451 if (!btrfs_trans_handle_cachep)
6452 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006453
6454 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
6455 sizeof(struct btrfs_transaction), 0,
6456 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006457 if (!btrfs_transaction_cachep)
6458 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006459
6460 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
6461 sizeof(struct btrfs_path), 0,
6462 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04006463 if (!btrfs_path_cachep)
6464 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02006465
Chris Mason39279cc2007-06-12 06:35:45 -04006466 return 0;
6467fail:
6468 btrfs_destroy_cachep();
6469 return -ENOMEM;
6470}
6471
6472static int btrfs_getattr(struct vfsmount *mnt,
6473 struct dentry *dentry, struct kstat *stat)
6474{
6475 struct inode *inode = dentry->d_inode;
6476 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05006477 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05006478 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006479 stat->blocks = (inode_get_bytes(inode) +
6480 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04006481 return 0;
6482}
6483
Chris Masond3977122009-01-05 21:25:51 -05006484static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
6485 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04006486{
6487 struct btrfs_trans_handle *trans;
6488 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006489 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04006490 struct inode *new_inode = new_dentry->d_inode;
6491 struct inode *old_inode = old_dentry->d_inode;
6492 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006493 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006494 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04006495 int ret;
6496
Yan, Zhengf679a842009-09-24 09:17:31 -04006497 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
6498 return -EPERM;
6499
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006500 /* we only allow rename subvolume link between subvolumes */
6501 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05006502 return -EXDEV;
6503
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006504 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
6505 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
6506 return -ENOTEMPTY;
6507
Chris Mason39279cc2007-06-12 06:35:45 -04006508 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006509 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04006510 return -ENOTEMPTY;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006511 /*
6512 * we're using rename to replace one file with another.
6513 * and the replacement file is large. Start IO on it now so
6514 * we don't add too much work to the end of the transaction
6515 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04006516 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04006517 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
6518 filemap_flush(old_inode->i_mapping);
6519
Yan, Zheng76dda932009-09-21 16:00:26 -04006520 /* close the racy window with snapshot create/destroy ioctl */
6521 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
6522 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006523 /*
6524 * We want to reserve the absolute worst case amount of items. So if
6525 * both inodes are subvols and we need to unlink them then that would
6526 * require 4 item modifications, but if they are both normal inodes it
6527 * would require 5 item modifications, so we'll assume their normal
6528 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
6529 * should cover the worst case number of items we'll modify.
6530 */
6531 trans = btrfs_start_transaction(root, 20);
6532 if (IS_ERR(trans))
6533 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04006534
Yan, Zhenga5719522009-09-24 09:17:31 -04006535 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04006536
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006537 if (dest != root)
6538 btrfs_record_root_in_trans(trans, dest);
6539
Yan, Zhenga5719522009-09-24 09:17:31 -04006540 ret = btrfs_set_inode_index(new_dir, &index);
6541 if (ret)
6542 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04006543
Yan, Zhenga5719522009-09-24 09:17:31 -04006544 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006545 /* force full log commit if subvolume involved. */
6546 root->fs_info->last_trans_log_full_commit = trans->transid;
6547 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04006548 ret = btrfs_insert_inode_ref(trans, dest,
6549 new_dentry->d_name.name,
6550 new_dentry->d_name.len,
6551 old_inode->i_ino,
6552 new_dir->i_ino, index);
6553 if (ret)
6554 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006555 /*
6556 * this is an ugly little race, but the rename is required
6557 * to make sure that if we crash, the inode is either at the
6558 * old name or the new one. pinning the log transaction lets
6559 * us make sure we don't allow a log commit to come in after
6560 * we unlink the name but before we add the new name back in.
6561 */
6562 btrfs_pin_log_trans(root);
6563 }
Chris Mason12fcfd22009-03-24 10:24:20 -04006564 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04006565 * make sure the inode gets flushed if it is replacing
6566 * something.
6567 */
6568 if (new_inode && new_inode->i_size &&
6569 old_inode && S_ISREG(old_inode->i_mode)) {
6570 btrfs_add_ordered_operation(trans, root, old_inode);
6571 }
6572
Chris Mason39279cc2007-06-12 06:35:45 -04006573 old_dir->i_ctime = old_dir->i_mtime = ctime;
6574 new_dir->i_ctime = new_dir->i_mtime = ctime;
6575 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04006576
Chris Mason12fcfd22009-03-24 10:24:20 -04006577 if (old_dentry->d_parent != new_dentry->d_parent)
6578 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
6579
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006580 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
6581 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
6582 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
6583 old_dentry->d_name.name,
6584 old_dentry->d_name.len);
6585 } else {
6586 btrfs_inc_nlink(old_dentry->d_inode);
6587 ret = btrfs_unlink_inode(trans, root, old_dir,
6588 old_dentry->d_inode,
6589 old_dentry->d_name.name,
6590 old_dentry->d_name.len);
6591 }
6592 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006593
6594 if (new_inode) {
6595 new_inode->i_ctime = CURRENT_TIME;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006596 if (unlikely(new_inode->i_ino ==
6597 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
6598 root_objectid = BTRFS_I(new_inode)->location.objectid;
6599 ret = btrfs_unlink_subvol(trans, dest, new_dir,
6600 root_objectid,
6601 new_dentry->d_name.name,
6602 new_dentry->d_name.len);
6603 BUG_ON(new_inode->i_nlink == 0);
6604 } else {
6605 ret = btrfs_unlink_inode(trans, dest, new_dir,
6606 new_dentry->d_inode,
6607 new_dentry->d_name.name,
6608 new_dentry->d_name.len);
6609 }
6610 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04006611 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04006612 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006613 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04006614 }
Chris Mason39279cc2007-06-12 06:35:45 -04006615 }
Josef Bacikaec74772008-07-24 12:12:38 -04006616
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006617 ret = btrfs_add_link(trans, new_dir, old_inode,
6618 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04006619 new_dentry->d_name.len, 0, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006620 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006621
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006622 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
6623 btrfs_log_new_name(trans, old_inode, old_dir,
6624 new_dentry->d_parent);
6625 btrfs_end_log_trans(root);
6626 }
Chris Mason39279cc2007-06-12 06:35:45 -04006627out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04006628 btrfs_end_transaction_throttle(trans, root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006629
Yan, Zheng76dda932009-09-21 16:00:26 -04006630 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
6631 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006632
Chris Mason39279cc2007-06-12 06:35:45 -04006633 return ret;
6634}
6635
Chris Masond352ac62008-09-29 15:18:18 -04006636/*
6637 * some fairly slow code that needs optimization. This walks the list
6638 * of all the inodes with pending delalloc and forces them to disk.
6639 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00006640int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04006641{
6642 struct list_head *head = &root->fs_info->delalloc_inodes;
6643 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006644 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04006645
Yan Zhengc146afa2008-11-12 14:34:12 -05006646 if (root->fs_info->sb->s_flags & MS_RDONLY)
6647 return -EROFS;
6648
Chris Mason75eff682008-12-15 15:54:40 -05006649 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05006650 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04006651 binode = list_entry(head->next, struct btrfs_inode,
6652 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006653 inode = igrab(&binode->vfs_inode);
6654 if (!inode)
6655 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05006656 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006657 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04006658 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00006659 if (delay_iput)
6660 btrfs_add_delayed_iput(inode);
6661 else
6662 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04006663 }
6664 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05006665 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04006666 }
Chris Mason75eff682008-12-15 15:54:40 -05006667 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04006668
6669 /* the filemap_flush will queue IO into the worker threads, but
6670 * we have to make sure the IO is actually started and that
6671 * ordered extents get created before we return
6672 */
6673 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05006674 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05006675 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04006676 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05006677 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
6678 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04006679 }
6680 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04006681 return 0;
6682}
6683
Yan, Zheng5da9d012010-05-16 10:46:25 -04006684int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput)
6685{
6686 struct btrfs_inode *binode;
6687 struct inode *inode = NULL;
6688
6689 spin_lock(&root->fs_info->delalloc_lock);
6690 while (!list_empty(&root->fs_info->delalloc_inodes)) {
6691 binode = list_entry(root->fs_info->delalloc_inodes.next,
6692 struct btrfs_inode, delalloc_inodes);
6693 inode = igrab(&binode->vfs_inode);
6694 if (inode) {
6695 list_move_tail(&binode->delalloc_inodes,
6696 &root->fs_info->delalloc_inodes);
6697 break;
6698 }
6699
6700 list_del_init(&binode->delalloc_inodes);
6701 cond_resched_lock(&root->fs_info->delalloc_lock);
6702 }
6703 spin_unlock(&root->fs_info->delalloc_lock);
6704
6705 if (inode) {
6706 write_inode_now(inode, 0);
6707 if (delay_iput)
6708 btrfs_add_delayed_iput(inode);
6709 else
6710 iput(inode);
6711 return 1;
6712 }
6713 return 0;
6714}
6715
Chris Mason39279cc2007-06-12 06:35:45 -04006716static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
6717 const char *symname)
6718{
6719 struct btrfs_trans_handle *trans;
6720 struct btrfs_root *root = BTRFS_I(dir)->root;
6721 struct btrfs_path *path;
6722 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05006723 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006724 int err;
6725 int drop_inode = 0;
6726 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006727 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04006728 int name_len;
6729 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04006730 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04006731 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04006732 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05006733 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006734
6735 name_len = strlen(symname) + 1;
6736 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
6737 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05006738
Yan, Zhenga22285a2010-05-16 10:48:46 -04006739 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
6740 if (err)
6741 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006742 /*
6743 * 2 items for inode item and ref
6744 * 2 items for dir items
6745 * 1 item for xattr if selinux is on
6746 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006747 trans = btrfs_start_transaction(root, 5);
6748 if (IS_ERR(trans))
6749 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006750
Chris Mason39279cc2007-06-12 06:35:45 -04006751 btrfs_set_trans_block_group(trans, dir);
6752
Josef Bacikaec74772008-07-24 12:12:38 -04006753 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05006754 dentry->d_name.len,
6755 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04006756 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
6757 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006758 err = PTR_ERR(inode);
6759 if (IS_ERR(inode))
6760 goto out_unlock;
6761
Yan, Zhengf34f57a2009-11-12 09:35:27 +00006762 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04006763 if (err) {
6764 drop_inode = 1;
6765 goto out_unlock;
6766 }
6767
Chris Mason39279cc2007-06-12 06:35:45 -04006768 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006769 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006770 if (err)
6771 drop_inode = 1;
6772 else {
6773 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04006774 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04006775 inode->i_fop = &btrfs_file_operations;
6776 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05006777 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04006778 }
Chris Mason39279cc2007-06-12 06:35:45 -04006779 btrfs_update_inode_block_group(trans, inode);
6780 btrfs_update_inode_block_group(trans, dir);
6781 if (drop_inode)
6782 goto out_unlock;
6783
6784 path = btrfs_alloc_path();
6785 BUG_ON(!path);
6786 key.objectid = inode->i_ino;
6787 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006788 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
6789 datasize = btrfs_file_extent_calc_inline_size(name_len);
6790 err = btrfs_insert_empty_item(trans, root, path, &key,
6791 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04006792 if (err) {
6793 drop_inode = 1;
6794 goto out_unlock;
6795 }
Chris Mason5f39d392007-10-15 16:14:19 -04006796 leaf = path->nodes[0];
6797 ei = btrfs_item_ptr(leaf, path->slots[0],
6798 struct btrfs_file_extent_item);
6799 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
6800 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04006801 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04006802 btrfs_set_file_extent_encryption(leaf, ei, 0);
6803 btrfs_set_file_extent_compression(leaf, ei, 0);
6804 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
6805 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
6806
Chris Mason39279cc2007-06-12 06:35:45 -04006807 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04006808 write_extent_buffer(leaf, symname, ptr, name_len);
6809 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04006810 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04006811
Chris Mason39279cc2007-06-12 06:35:45 -04006812 inode->i_op = &btrfs_symlink_inode_operations;
6813 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04006814 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04006815 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04006816 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04006817 err = btrfs_update_inode(trans, root, inode);
6818 if (err)
6819 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04006820
6821out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04006822 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04006823 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04006824 if (drop_inode) {
6825 inode_dec_link_count(inode);
6826 iput(inode);
6827 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04006828 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04006829 return err;
6830}
Chris Mason16432982008-04-10 10:23:21 -04006831
Josef Bacik0af3d002010-06-21 14:48:16 -04006832static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
6833 u64 start, u64 num_bytes, u64 min_size,
6834 loff_t actual_len, u64 *alloc_hint,
6835 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04006836{
Yan Zhengd899e052008-10-30 14:25:28 -04006837 struct btrfs_root *root = BTRFS_I(inode)->root;
6838 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04006839 u64 cur_offset = start;
Yan Zhengd899e052008-10-30 14:25:28 -04006840 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04006841 bool own_trans = true;
Yan Zhengd899e052008-10-30 14:25:28 -04006842
Josef Bacik0af3d002010-06-21 14:48:16 -04006843 if (trans)
6844 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04006845 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006846 if (own_trans) {
6847 trans = btrfs_start_transaction(root, 3);
6848 if (IS_ERR(trans)) {
6849 ret = PTR_ERR(trans);
6850 break;
6851 }
Yan Zhengd899e052008-10-30 14:25:28 -04006852 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00006853
Yan, Zhengefa56462010-05-16 10:49:59 -04006854 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
6855 0, *alloc_hint, (u64)-1, &ins, 1);
Yan, Zheng5a303d52009-11-12 09:34:52 +00006856 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04006857 if (own_trans)
6858 btrfs_end_transaction(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006859 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00006860 }
6861
Yan Zhengd899e052008-10-30 14:25:28 -04006862 ret = insert_reserved_file_extent(trans, inode,
6863 cur_offset, ins.objectid,
6864 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00006865 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04006866 BTRFS_FILE_EXTENT_PREALLOC);
6867 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -04006868 btrfs_drop_extent_cache(inode, cur_offset,
6869 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00006870
Yan Zhengd899e052008-10-30 14:25:28 -04006871 num_bytes -= ins.offset;
6872 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04006873 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00006874
Yan Zhengd899e052008-10-30 14:25:28 -04006875 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006876 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04006877 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04006878 (actual_len > inode->i_size) &&
6879 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00006880 if (cur_offset > actual_len)
Yan, Zhengefa56462010-05-16 10:49:59 -04006881 i_size_write(inode, actual_len);
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00006882 else
Yan, Zhengefa56462010-05-16 10:49:59 -04006883 i_size_write(inode, cur_offset);
6884 i_size_write(inode, cur_offset);
6885 btrfs_ordered_update_i_size(inode, cur_offset, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00006886 }
6887
Yan Zhengd899e052008-10-30 14:25:28 -04006888 ret = btrfs_update_inode(trans, root, inode);
6889 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04006890
Josef Bacik0af3d002010-06-21 14:48:16 -04006891 if (own_trans)
6892 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00006893 }
Yan Zhengd899e052008-10-30 14:25:28 -04006894 return ret;
6895}
6896
Josef Bacik0af3d002010-06-21 14:48:16 -04006897int btrfs_prealloc_file_range(struct inode *inode, int mode,
6898 u64 start, u64 num_bytes, u64 min_size,
6899 loff_t actual_len, u64 *alloc_hint)
6900{
6901 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
6902 min_size, actual_len, alloc_hint,
6903 NULL);
6904}
6905
6906int btrfs_prealloc_file_range_trans(struct inode *inode,
6907 struct btrfs_trans_handle *trans, int mode,
6908 u64 start, u64 num_bytes, u64 min_size,
6909 loff_t actual_len, u64 *alloc_hint)
6910{
6911 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
6912 min_size, actual_len, alloc_hint, trans);
6913}
6914
Yan Zhengd899e052008-10-30 14:25:28 -04006915static long btrfs_fallocate(struct inode *inode, int mode,
6916 loff_t offset, loff_t len)
6917{
Josef Bacik2ac55d42010-02-03 19:33:23 +00006918 struct extent_state *cached_state = NULL;
Yan Zhengd899e052008-10-30 14:25:28 -04006919 u64 cur_offset;
6920 u64 last_byte;
6921 u64 alloc_start;
6922 u64 alloc_end;
6923 u64 alloc_hint = 0;
Chris Masone980b502009-04-24 14:39:24 -04006924 u64 locked_end;
Yan Zhengd899e052008-10-30 14:25:28 -04006925 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
6926 struct extent_map *em;
6927 int ret;
6928
6929 alloc_start = offset & ~mask;
6930 alloc_end = (offset + len + mask) & ~mask;
6931
Chris Mason546888d2009-04-21 11:53:38 -04006932 /*
6933 * wait for ordered IO before we have any locks. We'll loop again
6934 * below with the locks held.
6935 */
6936 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
6937
Yan Zhengd899e052008-10-30 14:25:28 -04006938 mutex_lock(&inode->i_mutex);
6939 if (alloc_start > inode->i_size) {
6940 ret = btrfs_cont_expand(inode, alloc_start);
6941 if (ret)
6942 goto out;
6943 }
6944
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006945 ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
Josef Bacika970b0a2009-06-27 21:07:34 -04006946 if (ret)
6947 goto out;
6948
Chris Masone980b502009-04-24 14:39:24 -04006949 locked_end = alloc_end - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04006950 while (1) {
6951 struct btrfs_ordered_extent *ordered;
Chris Mason546888d2009-04-21 11:53:38 -04006952
Chris Mason546888d2009-04-21 11:53:38 -04006953 /* the extent lock is ordered inside the running
6954 * transaction
6955 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00006956 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
6957 locked_end, 0, &cached_state, GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04006958 ordered = btrfs_lookup_first_ordered_extent(inode,
6959 alloc_end - 1);
6960 if (ordered &&
6961 ordered->file_offset + ordered->len > alloc_start &&
6962 ordered->file_offset < alloc_end) {
6963 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00006964 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
6965 alloc_start, locked_end,
6966 &cached_state, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04006967 /*
6968 * we can't wait on the range with the transaction
6969 * running or with the extent lock held
6970 */
Yan Zhengd899e052008-10-30 14:25:28 -04006971 btrfs_wait_ordered_range(inode, alloc_start,
6972 alloc_end - alloc_start);
6973 } else {
6974 if (ordered)
6975 btrfs_put_ordered_extent(ordered);
6976 break;
6977 }
6978 }
6979
6980 cur_offset = alloc_start;
6981 while (1) {
6982 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
6983 alloc_end - cur_offset, 0);
6984 BUG_ON(IS_ERR(em) || !em);
6985 last_byte = min(extent_map_end(em), alloc_end);
6986 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng5a303d52009-11-12 09:34:52 +00006987 if (em->block_start == EXTENT_MAP_HOLE ||
6988 (cur_offset >= inode->i_size &&
6989 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
Josef Bacik83609772010-06-07 18:26:37 +00006990 ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
Yan, Zhengefa56462010-05-16 10:49:59 -04006991 last_byte - cur_offset,
6992 1 << inode->i_blkbits,
6993 offset + len,
6994 &alloc_hint);
Yan Zhengd899e052008-10-30 14:25:28 -04006995 if (ret < 0) {
6996 free_extent_map(em);
6997 break;
6998 }
6999 }
Yan Zhengd899e052008-10-30 14:25:28 -04007000 free_extent_map(em);
7001
7002 cur_offset = last_byte;
7003 if (cur_offset >= alloc_end) {
7004 ret = 0;
7005 break;
7006 }
7007 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00007008 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
7009 &cached_state, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04007010
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04007011 btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
Yan Zhengd899e052008-10-30 14:25:28 -04007012out:
7013 mutex_unlock(&inode->i_mutex);
7014 return ret;
7015}
7016
Chris Masone6dcd2d2008-07-17 12:53:50 -04007017static int btrfs_set_page_dirty(struct page *page)
7018{
Chris Masone6dcd2d2008-07-17 12:53:50 -04007019 return __set_page_dirty_nobuffers(page);
7020}
7021
Sven Wegener0ee0fda2008-07-30 16:54:26 -04007022static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05007023{
Christoph Hellwig6cbff002009-04-17 10:37:41 +02007024 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
Yanfdebe2b2008-01-14 13:26:08 -05007025 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04007026 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05007027}
Chris Mason39279cc2007-06-12 06:35:45 -04007028
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007029static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05007030 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007031 .lookup = btrfs_lookup,
7032 .create = btrfs_create,
7033 .unlink = btrfs_unlink,
7034 .link = btrfs_link,
7035 .mkdir = btrfs_mkdir,
7036 .rmdir = btrfs_rmdir,
7037 .rename = btrfs_rename,
7038 .symlink = btrfs_symlink,
7039 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007040 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007041 .setxattr = btrfs_setxattr,
7042 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007043 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007044 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007045 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04007046};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007047static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007048 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05007049 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04007050};
Yan, Zheng76dda932009-09-21 16:00:26 -04007051
Alexey Dobriyan828c0952009-10-01 15:43:56 -07007052static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007053 .llseek = generic_file_llseek,
7054 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01007055 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007056 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007057#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04007058 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04007059#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04007060 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04007061 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04007062};
7063
Chris Masond1310b22008-01-24 16:13:08 -05007064static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04007065 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05007066 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04007067 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007068 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007069 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04007070 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04007071 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05007072 .set_bit_hook = btrfs_set_bit_hook,
7073 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04007074 .merge_extent_hook = btrfs_merge_extent_hook,
7075 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04007076};
7077
Chris Mason35054392009-01-21 13:11:13 -05007078/*
7079 * btrfs doesn't support the bmap operation because swapfiles
7080 * use bmap to make a mapping of extents in the file. They assume
7081 * these extents won't change over the life of the file and they
7082 * use the bmap result to do IO directly to the drive.
7083 *
7084 * the btrfs bmap call would return logical addresses that aren't
7085 * suitable for IO and they also will change frequently as COW
7086 * operations happen. So, swapfile + btrfs == corruption.
7087 *
7088 * For now we're avoiding this by dropping bmap.
7089 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007090static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007091 .readpage = btrfs_readpage,
7092 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04007093 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05007094 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04007095 .sync_page = block_sync_page,
Chris Mason16432982008-04-10 10:23:21 -04007096 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04007097 .invalidatepage = btrfs_invalidatepage,
7098 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04007099 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02007100 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04007101};
7102
Alexey Dobriyan7f094102009-09-21 17:01:10 -07007103static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04007104 .readpage = btrfs_readpage,
7105 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04007106 .invalidatepage = btrfs_invalidatepage,
7107 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04007108};
7109
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007110static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007111 .truncate = btrfs_truncate,
7112 .getattr = btrfs_getattr,
7113 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007114 .setxattr = btrfs_setxattr,
7115 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05007116 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007117 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05007118 .permission = btrfs_permission,
Yan Zhengd899e052008-10-30 14:25:28 -04007119 .fallocate = btrfs_fallocate,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007120 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04007121};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007122static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04007123 .getattr = btrfs_getattr,
7124 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05007125 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007126 .setxattr = btrfs_setxattr,
7127 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04007128 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04007129 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04007130};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07007131static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04007132 .readlink = generic_readlink,
7133 .follow_link = page_follow_link_light,
7134 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05007135 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05007136 .setxattr = btrfs_setxattr,
7137 .getxattr = btrfs_getxattr,
7138 .listxattr = btrfs_listxattr,
7139 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04007140};
Yan, Zheng76dda932009-09-21 16:00:26 -04007141
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04007142const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04007143 .d_delete = btrfs_dentry_delete,
7144};