blob: f9c40b839452b00c6a96bb34cfc7db5998d70fdd [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 Masonb888db2b2007-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 Masonb888db2b2007-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
701/*
702 * when extent_io.c finds a delayed allocation range in the file,
703 * the call backs end up in this code. The basic idea is to
704 * allocate extents on disk for the range, and create ordered data structs
705 * in ram to track those extents.
706 *
707 * locked_page is the page that writepage had locked already. We use
708 * it to make sure we don't do extra locks or unlocks.
709 *
710 * *page_started is set to one if we unlock locked_page and do everything
711 * required to start IO on it. It may be clean and already done with
712 * IO when we return.
713 */
714static noinline int cow_file_range(struct inode *inode,
715 struct page *locked_page,
716 u64 start, u64 end, int *page_started,
717 unsigned long *nr_written,
718 int unlock)
719{
720 struct btrfs_root *root = BTRFS_I(inode)->root;
721 struct btrfs_trans_handle *trans;
722 u64 alloc_hint = 0;
723 u64 num_bytes;
724 unsigned long ram_size;
725 u64 disk_num_bytes;
726 u64 cur_alloc_size;
727 u64 blocksize = root->sectorsize;
728 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500729 u64 isize = i_size_read(inode);
Chris Mason771ed682008-11-06 22:02:51 -0500730 struct btrfs_key ins;
731 struct extent_map *em;
732 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
733 int ret = 0;
734
735 trans = btrfs_join_transaction(root, 1);
736 BUG_ON(!trans);
737 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400738 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Chris Mason771ed682008-11-06 22:02:51 -0500739
Chris Mason42dc7ba2008-12-15 11:44:56 -0500740 actual_end = min_t(u64, isize, end + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500741
742 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
743 num_bytes = max(blocksize, num_bytes);
744 disk_num_bytes = num_bytes;
745 ret = 0;
746
747 if (start == 0) {
748 /* lets try to make an inline extent */
749 ret = cow_file_range_inline(trans, root, inode,
750 start, end, 0, NULL);
751 if (ret == 0) {
752 extent_clear_unlock_delalloc(inode,
Chris Masona791e352009-10-08 11:27:10 -0400753 &BTRFS_I(inode)->io_tree,
754 start, end, NULL,
755 EXTENT_CLEAR_UNLOCK_PAGE |
756 EXTENT_CLEAR_UNLOCK |
757 EXTENT_CLEAR_DELALLOC |
758 EXTENT_CLEAR_DIRTY |
759 EXTENT_SET_WRITEBACK |
760 EXTENT_END_WRITEBACK);
Yan, Zhengc2167752009-11-12 09:34:21 +0000761
Chris Mason771ed682008-11-06 22:02:51 -0500762 *nr_written = *nr_written +
763 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
764 *page_started = 1;
765 ret = 0;
766 goto out;
767 }
768 }
Chris Masonc8b97812008-10-29 14:49:59 -0400769
770 BUG_ON(disk_num_bytes >
771 btrfs_super_total_bytes(&root->fs_info->super_copy));
772
Chris Masonb917b7c2009-09-18 16:07:03 -0400773
774 read_lock(&BTRFS_I(inode)->extent_tree.lock);
775 em = search_extent_mapping(&BTRFS_I(inode)->extent_tree,
776 start, num_bytes);
777 if (em) {
Josef Bacik6346c932009-11-10 21:23:47 -0500778 /*
779 * if block start isn't an actual block number then find the
780 * first block in this inode and use that as a hint. If that
781 * block is also bogus then just don't worry about it.
782 */
783 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
784 free_extent_map(em);
785 em = search_extent_mapping(em_tree, 0, 0);
786 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
787 alloc_hint = em->block_start;
788 if (em)
789 free_extent_map(em);
790 } else {
791 alloc_hint = em->block_start;
792 free_extent_map(em);
793 }
Chris Masonb917b7c2009-09-18 16:07:03 -0400794 }
795 read_unlock(&BTRFS_I(inode)->extent_tree.lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400796 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400797
Chris Masond3977122009-01-05 21:25:51 -0500798 while (disk_num_bytes > 0) {
Chris Masona791e352009-10-08 11:27:10 -0400799 unsigned long op;
800
Josef Bacik287a0ab2010-03-19 18:07:23 +0000801 cur_alloc_size = disk_num_bytes;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400802 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500803 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400804 (u64)-1, &ins, 1);
Chris Masond3977122009-01-05 21:25:51 -0500805 BUG_ON(ret);
806
Chris Masone6dcd2d2008-07-17 12:53:50 -0400807 em = alloc_extent_map(GFP_NOFS);
808 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500809 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500810 ram_size = ins.offset;
811 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400812
Chris Masone6dcd2d2008-07-17 12:53:50 -0400813 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400814 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400815 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400816 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400817
Chris Masond3977122009-01-05 21:25:51 -0500818 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -0400819 write_lock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400820 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -0400821 write_unlock(&em_tree->lock);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400822 if (ret != -EEXIST) {
823 free_extent_map(em);
824 break;
825 }
826 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400827 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400828 }
829
Chris Mason98d20f62008-04-14 09:46:10 -0400830 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400831 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500832 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400833 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400834
Yan Zheng17d217f2008-12-12 10:03:38 -0500835 if (root->root_key.objectid ==
836 BTRFS_DATA_RELOC_TREE_OBJECTID) {
837 ret = btrfs_reloc_clone_csums(inode, start,
838 cur_alloc_size);
839 BUG_ON(ret);
840 }
841
Chris Masond3977122009-01-05 21:25:51 -0500842 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400843 break;
Chris Masond3977122009-01-05 21:25:51 -0500844
Chris Masonc8b97812008-10-29 14:49:59 -0400845 /* we're not doing compressed IO, don't unlock the first
846 * page (which the caller expects to stay locked), don't
847 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -0400848 *
849 * Do set the Private2 bit so we know this page was properly
850 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -0400851 */
Chris Masona791e352009-10-08 11:27:10 -0400852 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
853 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
854 EXTENT_SET_PRIVATE2;
855
Chris Masonc8b97812008-10-29 14:49:59 -0400856 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
857 start, start + ram_size - 1,
Chris Masona791e352009-10-08 11:27:10 -0400858 locked_page, op);
Chris Masonc8b97812008-10-29 14:49:59 -0400859 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500860 num_bytes -= cur_alloc_size;
861 alloc_hint = ins.objectid + ins.offset;
862 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -0400863 }
Chris Masonb888db2b2007-08-27 16:49:44 -0400864out:
Chris Mason771ed682008-11-06 22:02:51 -0500865 ret = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -0400866 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400867
Chris Masonbe20aa92007-12-17 20:14:01 -0500868 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500869}
Chris Masonc8b97812008-10-29 14:49:59 -0400870
Chris Mason771ed682008-11-06 22:02:51 -0500871/*
872 * work queue call back to started compression on a file and pages
873 */
874static noinline void async_cow_start(struct btrfs_work *work)
875{
876 struct async_cow *async_cow;
877 int num_added = 0;
878 async_cow = container_of(work, struct async_cow, work);
879
880 compress_file_range(async_cow->inode, async_cow->locked_page,
881 async_cow->start, async_cow->end, async_cow,
882 &num_added);
883 if (num_added == 0)
884 async_cow->inode = NULL;
885}
886
887/*
888 * work queue call back to submit previously compressed pages
889 */
890static noinline void async_cow_submit(struct btrfs_work *work)
891{
892 struct async_cow *async_cow;
893 struct btrfs_root *root;
894 unsigned long nr_pages;
895
896 async_cow = container_of(work, struct async_cow, work);
897
898 root = async_cow->root;
899 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
900 PAGE_CACHE_SHIFT;
901
902 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
903
904 if (atomic_read(&root->fs_info->async_delalloc_pages) <
905 5 * 1042 * 1024 &&
906 waitqueue_active(&root->fs_info->async_submit_wait))
907 wake_up(&root->fs_info->async_submit_wait);
908
Chris Masond3977122009-01-05 21:25:51 -0500909 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -0500910 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500911}
Chris Masonc8b97812008-10-29 14:49:59 -0400912
Chris Mason771ed682008-11-06 22:02:51 -0500913static noinline void async_cow_free(struct btrfs_work *work)
914{
915 struct async_cow *async_cow;
916 async_cow = container_of(work, struct async_cow, work);
917 kfree(async_cow);
918}
919
920static int cow_file_range_async(struct inode *inode, struct page *locked_page,
921 u64 start, u64 end, int *page_started,
922 unsigned long *nr_written)
923{
924 struct async_cow *async_cow;
925 struct btrfs_root *root = BTRFS_I(inode)->root;
926 unsigned long nr_pages;
927 u64 cur_end;
928 int limit = 10 * 1024 * 1042;
929
Chris Masona3429ab2009-10-08 12:30:20 -0400930 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
931 1, 0, NULL, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500932 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500933 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
934 async_cow->inode = inode;
935 async_cow->root = root;
936 async_cow->locked_page = locked_page;
937 async_cow->start = start;
938
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200939 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
Chris Mason771ed682008-11-06 22:02:51 -0500940 cur_end = end;
941 else
942 cur_end = min(end, start + 512 * 1024 - 1);
943
944 async_cow->end = cur_end;
945 INIT_LIST_HEAD(&async_cow->extents);
946
947 async_cow->work.func = async_cow_start;
948 async_cow->work.ordered_func = async_cow_submit;
949 async_cow->work.ordered_free = async_cow_free;
950 async_cow->work.flags = 0;
951
Chris Mason771ed682008-11-06 22:02:51 -0500952 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
953 PAGE_CACHE_SHIFT;
954 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
955
956 btrfs_queue_worker(&root->fs_info->delalloc_workers,
957 &async_cow->work);
958
959 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
960 wait_event(root->fs_info->async_submit_wait,
961 (atomic_read(&root->fs_info->async_delalloc_pages) <
962 limit));
963 }
964
Chris Masond3977122009-01-05 21:25:51 -0500965 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -0500966 atomic_read(&root->fs_info->async_delalloc_pages)) {
967 wait_event(root->fs_info->async_submit_wait,
968 (atomic_read(&root->fs_info->async_delalloc_pages) ==
969 0));
970 }
971
972 *nr_written += nr_pages;
973 start = cur_end + 1;
974 }
975 *page_started = 1;
976 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500977}
978
Chris Masond3977122009-01-05 21:25:51 -0500979static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -0500980 u64 bytenr, u64 num_bytes)
981{
982 int ret;
983 struct btrfs_ordered_sum *sums;
984 LIST_HEAD(list);
985
Yan Zheng07d400a2009-01-06 11:42:00 -0500986 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
987 bytenr + num_bytes - 1, &list);
Yan Zheng17d217f2008-12-12 10:03:38 -0500988 if (ret == 0 && list_empty(&list))
989 return 0;
990
991 while (!list_empty(&list)) {
992 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
993 list_del(&sums->list);
994 kfree(sums);
995 }
996 return 1;
997}
998
Chris Masond352ac62008-09-29 15:18:18 -0400999/*
1000 * when nowcow writeback call back. This checks for snapshots or COW copies
1001 * of the extents that exist in the file, and COWs the file as required.
1002 *
1003 * If no cow copies or snapshots exist, we write directly to the existing
1004 * blocks on disk
1005 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001006static noinline int run_delalloc_nocow(struct inode *inode,
1007 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001008 u64 start, u64 end, int *page_started, int force,
1009 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001010{
Chris Masonbe20aa92007-12-17 20:14:01 -05001011 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001012 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -05001013 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -05001014 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -04001015 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -05001016 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001017 u64 cow_start;
1018 u64 cur_offset;
1019 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001020 u64 extent_offset;
Yan Zheng80ff3852008-10-30 14:20:02 -04001021 u64 disk_bytenr;
1022 u64 num_bytes;
1023 int extent_type;
1024 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -04001025 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -04001026 int nocow;
1027 int check_prev = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -05001028
1029 path = btrfs_alloc_path();
1030 BUG_ON(!path);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001031 trans = btrfs_join_transaction(root, 1);
1032 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -05001033
Yan Zheng80ff3852008-10-30 14:20:02 -04001034 cow_start = (u64)-1;
1035 cur_offset = start;
1036 while (1) {
1037 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1038 cur_offset, 0);
1039 BUG_ON(ret < 0);
1040 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1041 leaf = path->nodes[0];
1042 btrfs_item_key_to_cpu(leaf, &found_key,
1043 path->slots[0] - 1);
1044 if (found_key.objectid == inode->i_ino &&
1045 found_key.type == BTRFS_EXTENT_DATA_KEY)
1046 path->slots[0]--;
1047 }
1048 check_prev = 0;
1049next_slot:
1050 leaf = path->nodes[0];
1051 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1052 ret = btrfs_next_leaf(root, path);
1053 if (ret < 0)
1054 BUG_ON(1);
1055 if (ret > 0)
1056 break;
1057 leaf = path->nodes[0];
1058 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001059
Yan Zheng80ff3852008-10-30 14:20:02 -04001060 nocow = 0;
1061 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001062 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001063 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001064
Yan Zheng80ff3852008-10-30 14:20:02 -04001065 if (found_key.objectid > inode->i_ino ||
1066 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1067 found_key.offset > end)
1068 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001069
Yan Zheng80ff3852008-10-30 14:20:02 -04001070 if (found_key.offset > cur_offset) {
1071 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001072 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001073 goto out_check;
1074 }
Chris Masonc31f8832008-01-08 15:46:31 -05001075
Yan Zheng80ff3852008-10-30 14:20:02 -04001076 fi = btrfs_item_ptr(leaf, path->slots[0],
1077 struct btrfs_file_extent_item);
1078 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001079
Yan Zhengd899e052008-10-30 14:25:28 -04001080 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1081 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001082 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001083 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001084 extent_end = found_key.offset +
1085 btrfs_file_extent_num_bytes(leaf, fi);
1086 if (extent_end <= start) {
1087 path->slots[0]++;
1088 goto next_slot;
1089 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001090 if (disk_bytenr == 0)
1091 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001092 if (btrfs_file_extent_compression(leaf, fi) ||
1093 btrfs_file_extent_encryption(leaf, fi) ||
1094 btrfs_file_extent_other_encoding(leaf, fi))
1095 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001096 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1097 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001098 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001099 goto out_check;
Yan Zheng17d217f2008-12-12 10:03:38 -05001100 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001101 found_key.offset -
1102 extent_offset, disk_bytenr))
Yan Zheng17d217f2008-12-12 10:03:38 -05001103 goto out_check;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001104 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001105 disk_bytenr += cur_offset - found_key.offset;
1106 num_bytes = min(end + 1, extent_end) - cur_offset;
1107 /*
1108 * force cow if csum exists in the range.
1109 * this ensure that csum for a given extent are
1110 * either valid or do not exist.
1111 */
1112 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1113 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001114 nocow = 1;
1115 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1116 extent_end = found_key.offset +
1117 btrfs_file_extent_inline_len(leaf, fi);
1118 extent_end = ALIGN(extent_end, root->sectorsize);
1119 } else {
1120 BUG_ON(1);
1121 }
1122out_check:
1123 if (extent_end <= start) {
1124 path->slots[0]++;
1125 goto next_slot;
1126 }
1127 if (!nocow) {
1128 if (cow_start == (u64)-1)
1129 cow_start = cur_offset;
1130 cur_offset = extent_end;
1131 if (cur_offset > end)
1132 break;
1133 path->slots[0]++;
1134 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001135 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001136
Yan Zheng7ea394f2008-08-05 13:05:02 -04001137 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001138 if (cow_start != (u64)-1) {
1139 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001140 found_key.offset - 1, page_started,
1141 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001142 BUG_ON(ret);
1143 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001144 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001145
Yan Zhengd899e052008-10-30 14:25:28 -04001146 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1147 struct extent_map *em;
1148 struct extent_map_tree *em_tree;
1149 em_tree = &BTRFS_I(inode)->extent_tree;
1150 em = alloc_extent_map(GFP_NOFS);
1151 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001152 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001153 em->len = num_bytes;
1154 em->block_len = num_bytes;
1155 em->block_start = disk_bytenr;
1156 em->bdev = root->fs_info->fs_devices->latest_bdev;
1157 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1158 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04001159 write_lock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001160 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04001161 write_unlock(&em_tree->lock);
Yan Zhengd899e052008-10-30 14:25:28 -04001162 if (ret != -EEXIST) {
1163 free_extent_map(em);
1164 break;
1165 }
1166 btrfs_drop_extent_cache(inode, em->start,
1167 em->start + em->len - 1, 0);
1168 }
1169 type = BTRFS_ORDERED_PREALLOC;
1170 } else {
1171 type = BTRFS_ORDERED_NOCOW;
1172 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001173
1174 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001175 num_bytes, num_bytes, type);
1176 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001177
Yan Zhengd899e052008-10-30 14:25:28 -04001178 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
Chris Masona791e352009-10-08 11:27:10 -04001179 cur_offset, cur_offset + num_bytes - 1,
1180 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1181 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1182 EXTENT_SET_PRIVATE2);
Yan Zheng80ff3852008-10-30 14:20:02 -04001183 cur_offset = extent_end;
1184 if (cur_offset > end)
1185 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001186 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001187 btrfs_release_path(root, path);
1188
1189 if (cur_offset <= end && cow_start == (u64)-1)
1190 cow_start = cur_offset;
1191 if (cow_start != (u64)-1) {
1192 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001193 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001194 BUG_ON(ret);
1195 }
1196
1197 ret = btrfs_end_transaction(trans, root);
1198 BUG_ON(ret);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001199 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001200 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001201}
1202
Chris Masond352ac62008-09-29 15:18:18 -04001203/*
1204 * extent_io.c call back to do delayed allocation processing
1205 */
Chris Masonc8b97812008-10-29 14:49:59 -04001206static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001207 u64 start, u64 end, int *page_started,
1208 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001209{
Chris Masonbe20aa92007-12-17 20:14:01 -05001210 int ret;
Chris Mason7f366cf2009-03-12 20:12:45 -04001211 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona2135012008-06-25 16:01:30 -04001212
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001213 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
Chris Masonc8b97812008-10-29 14:49:59 -04001214 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001215 page_started, 1, nr_written);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001216 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
Yan Zhengd899e052008-10-30 14:25:28 -04001217 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001218 page_started, 0, nr_written);
Chris Mason1e701a32010-03-11 09:42:04 -05001219 else if (!btrfs_test_opt(root, COMPRESS) &&
1220 !(BTRFS_I(inode)->force_compress))
Chris Mason7f366cf2009-03-12 20:12:45 -04001221 ret = cow_file_range(inode, locked_page, start, end,
1222 page_started, nr_written, 1);
Chris Masonbe20aa92007-12-17 20:14:01 -05001223 else
Chris Mason771ed682008-11-06 22:02:51 -05001224 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001225 page_started, nr_written);
Chris Masonb888db2b2007-08-27 16:49:44 -04001226 return ret;
1227}
1228
Josef Bacik9ed74f22009-09-11 16:12:44 -04001229static int btrfs_split_extent_hook(struct inode *inode,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001230 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001231{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001232 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001233 if (!(orig->state & EXTENT_DELALLOC))
1234 return 0;
1235
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001236 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001237 return 0;
1238}
1239
1240/*
1241 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1242 * extents so we can keep track of new extents that are just merged onto old
1243 * extents, such as when we are doing sequential writes, so we can properly
1244 * account for the metadata space we'll need.
1245 */
1246static int btrfs_merge_extent_hook(struct inode *inode,
1247 struct extent_state *new,
1248 struct extent_state *other)
1249{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001250 /* not delalloc, ignore it */
1251 if (!(other->state & EXTENT_DELALLOC))
1252 return 0;
1253
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001254 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001255 return 0;
1256}
1257
Chris Masond352ac62008-09-29 15:18:18 -04001258/*
1259 * extent_io.c set_bit_hook, used to track delayed allocation
1260 * bytes in this file, and to maintain the list of inodes that
1261 * have pending delalloc work to be done.
1262 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001263static int btrfs_set_bit_hook(struct inode *inode,
1264 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001265{
Josef Bacik9ed74f22009-09-11 16:12:44 -04001266
Chris Mason75eff682008-12-15 15:54:40 -05001267 /*
1268 * set_bit and clear bit hooks normally require _irqsave/restore
1269 * but in this case, we are only testeing for the DELALLOC
1270 * bit, which is only set or cleared with irqs on
1271 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001272 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001273 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001274 u64 len = state->end + 1 - state->start;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001275
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001276 if (*bits & EXTENT_FIRST_DELALLOC)
1277 *bits &= ~EXTENT_FIRST_DELALLOC;
1278 else
1279 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001280
Chris Mason75eff682008-12-15 15:54:40 -05001281 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001282 BTRFS_I(inode)->delalloc_bytes += len;
1283 root->fs_info->delalloc_bytes += len;
Chris Masonea8c2812008-08-04 23:17:27 -04001284 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1285 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1286 &root->fs_info->delalloc_inodes);
1287 }
Chris Mason75eff682008-12-15 15:54:40 -05001288 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001289 }
1290 return 0;
1291}
1292
Chris Masond352ac62008-09-29 15:18:18 -04001293/*
1294 * extent_io.c clear_bit_hook, see set_bit_hook for why
1295 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001296static int btrfs_clear_bit_hook(struct inode *inode,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001297 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001298{
Chris Mason75eff682008-12-15 15:54:40 -05001299 /*
1300 * set_bit and clear bit hooks normally require _irqsave/restore
1301 * but in this case, we are only testeing for the DELALLOC
1302 * bit, which is only set or cleared with irqs on
1303 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001304 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001305 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001306 u64 len = state->end + 1 - state->start;
Chris Masonbcbfce82008-04-22 13:26:47 -04001307
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001308 if (*bits & EXTENT_FIRST_DELALLOC)
1309 *bits &= ~EXTENT_FIRST_DELALLOC;
1310 else if (!(*bits & EXTENT_DO_ACCOUNTING))
1311 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
1312
1313 if (*bits & EXTENT_DO_ACCOUNTING)
1314 btrfs_delalloc_release_metadata(inode, len);
1315
1316 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID)
1317 btrfs_free_reserved_data_space(inode, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001318
Chris Mason75eff682008-12-15 15:54:40 -05001319 spin_lock(&root->fs_info->delalloc_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001320 root->fs_info->delalloc_bytes -= len;
1321 BTRFS_I(inode)->delalloc_bytes -= len;
1322
Chris Masonea8c2812008-08-04 23:17:27 -04001323 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1324 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1325 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1326 }
Chris Mason75eff682008-12-15 15:54:40 -05001327 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001328 }
1329 return 0;
1330}
1331
Chris Masond352ac62008-09-29 15:18:18 -04001332/*
1333 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1334 * we don't create bios that span stripes or chunks
1335 */
Chris Mason239b14b2008-03-24 15:02:07 -04001336int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001337 size_t size, struct bio *bio,
1338 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001339{
1340 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1341 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001342 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001343 u64 length = 0;
1344 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001345 int ret;
1346
Chris Mason771ed682008-11-06 22:02:51 -05001347 if (bio_flags & EXTENT_BIO_COMPRESSED)
1348 return 0;
1349
Chris Masonf2d8d742008-04-21 10:03:05 -04001350 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001351 map_tree = &root->fs_info->mapping_tree;
1352 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001353 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001354 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001355
Chris Masond3977122009-01-05 21:25:51 -05001356 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001357 return 1;
Chris Mason239b14b2008-03-24 15:02:07 -04001358 return 0;
1359}
1360
Chris Masond352ac62008-09-29 15:18:18 -04001361/*
1362 * in order to insert checksums into the metadata in large chunks,
1363 * we wait until bio submission time. All the pages in the bio are
1364 * checksummed and sums are attached onto the ordered extent record.
1365 *
1366 * At IO completion time the cums attached on the ordered extent record
1367 * are inserted into the btree
1368 */
Chris Masond3977122009-01-05 21:25:51 -05001369static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1370 struct bio *bio, int mirror_num,
1371 unsigned long bio_flags)
Chris Mason065631f2008-02-20 12:07:25 -05001372{
Chris Mason065631f2008-02-20 12:07:25 -05001373 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001374 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001375
Chris Masond20f7042008-12-08 16:58:54 -05001376 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001377 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001378 return 0;
1379}
Chris Masone0156402008-04-16 11:15:20 -04001380
Chris Mason4a69a412008-11-06 22:03:00 -05001381/*
1382 * in order to insert checksums into the metadata in large chunks,
1383 * we wait until bio submission time. All the pages in the bio are
1384 * checksummed and sums are attached onto the ordered extent record.
1385 *
1386 * At IO completion time the cums attached on the ordered extent record
1387 * are inserted into the btree
1388 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001389static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Mason4a69a412008-11-06 22:03:00 -05001390 int mirror_num, unsigned long bio_flags)
1391{
1392 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001393 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001394}
1395
Chris Masond352ac62008-09-29 15:18:18 -04001396/*
Chris Masoncad321a2008-12-17 14:51:42 -05001397 * extent_io.c submission hook. This does the right thing for csum calculation
1398 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001399 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001400static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001401 int mirror_num, unsigned long bio_flags)
Chris Mason44b8bd72008-04-16 11:14:51 -04001402{
1403 struct btrfs_root *root = BTRFS_I(inode)->root;
1404 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001405 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001406
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001407 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05001408
Chris Masone6dcd2d2008-07-17 12:53:50 -04001409 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1410 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -05001411
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001412 if (!(rw & (1 << BIO_RW))) {
Chris Masond20f7042008-12-08 16:58:54 -05001413 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001414 return btrfs_submit_compressed_read(inode, bio,
1415 mirror_num, bio_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001416 } else if (!skip_sum)
1417 btrfs_lookup_bio_sums(root, inode, bio, NULL);
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001418 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001419 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001420 /* csum items have already been cloned */
1421 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1422 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001423 /* we're doing a write, do the async checksumming */
1424 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001425 inode, rw, bio, mirror_num,
Chris Mason4a69a412008-11-06 22:03:00 -05001426 bio_flags, __btrfs_submit_bio_start,
1427 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001428 }
1429
Chris Mason0b86a832008-03-24 15:01:56 -04001430mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001431 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001432}
Chris Mason6885f302008-02-20 16:11:05 -05001433
Chris Masond352ac62008-09-29 15:18:18 -04001434/*
1435 * given a list of ordered sums record them in the inode. This happens
1436 * at IO completion time based on sums calculated at bio submission time.
1437 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001438static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001439 struct inode *inode, u64 file_offset,
1440 struct list_head *list)
1441{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001442 struct btrfs_ordered_sum *sum;
1443
1444 btrfs_set_trans_block_group(trans, inode);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001445
1446 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001447 btrfs_csum_file_blocks(trans,
1448 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001449 }
1450 return 0;
1451}
1452
Josef Bacik2ac55d42010-02-03 19:33:23 +00001453int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1454 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04001455{
Chris Masond3977122009-01-05 21:25:51 -05001456 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001457 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001458 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001459 cached_state, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001460}
1461
Chris Masond352ac62008-09-29 15:18:18 -04001462/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001463struct btrfs_writepage_fixup {
1464 struct page *page;
1465 struct btrfs_work work;
1466};
1467
Christoph Hellwigb2950862008-12-02 09:54:17 -05001468static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001469{
1470 struct btrfs_writepage_fixup *fixup;
1471 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001472 struct extent_state *cached_state = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04001473 struct page *page;
1474 struct inode *inode;
1475 u64 page_start;
1476 u64 page_end;
1477
1478 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1479 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001480again:
Chris Mason247e7432008-07-17 12:53:51 -04001481 lock_page(page);
1482 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1483 ClearPageChecked(page);
1484 goto out_page;
1485 }
1486
1487 inode = page->mapping->host;
1488 page_start = page_offset(page);
1489 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1490
Josef Bacik2ac55d42010-02-03 19:33:23 +00001491 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
1492 &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001493
1494 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04001495 if (PagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001496 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001497
1498 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1499 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001500 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1501 page_end, &cached_state, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001502 unlock_page(page);
1503 btrfs_start_ordered_extent(inode, ordered, 1);
1504 goto again;
1505 }
Chris Mason247e7432008-07-17 12:53:51 -04001506
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001507 BUG();
Josef Bacik2ac55d42010-02-03 19:33:23 +00001508 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04001509 ClearPageChecked(page);
1510out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00001511 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1512 &cached_state, GFP_NOFS);
Chris Mason247e7432008-07-17 12:53:51 -04001513out_page:
1514 unlock_page(page);
1515 page_cache_release(page);
1516}
1517
1518/*
1519 * There are a few paths in the higher layers of the kernel that directly
1520 * set the page dirty bit without asking the filesystem if it is a
1521 * good idea. This causes problems because we want to make sure COW
1522 * properly happens and the data=ordered rules are followed.
1523 *
Chris Masonc8b97812008-10-29 14:49:59 -04001524 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001525 * hasn't been properly setup for IO. We kick off an async process
1526 * to fix it up. The async helper will wait for ordered extents, set
1527 * the delalloc bit and make it safe to write the page.
1528 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001529static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001530{
1531 struct inode *inode = page->mapping->host;
1532 struct btrfs_writepage_fixup *fixup;
1533 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason247e7432008-07-17 12:53:51 -04001534
Chris Mason8b62b722009-09-02 16:53:46 -04001535 /* this page is properly in the ordered list */
1536 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04001537 return 0;
1538
1539 if (PageChecked(page))
1540 return -EAGAIN;
1541
1542 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1543 if (!fixup)
1544 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001545
Chris Mason247e7432008-07-17 12:53:51 -04001546 SetPageChecked(page);
1547 page_cache_get(page);
1548 fixup->work.func = btrfs_writepage_fixup_worker;
1549 fixup->page = page;
1550 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1551 return -EAGAIN;
1552}
1553
Yan Zhengd899e052008-10-30 14:25:28 -04001554static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1555 struct inode *inode, u64 file_pos,
1556 u64 disk_bytenr, u64 disk_num_bytes,
1557 u64 num_bytes, u64 ram_bytes,
1558 u8 compression, u8 encryption,
1559 u16 other_encoding, int extent_type)
1560{
1561 struct btrfs_root *root = BTRFS_I(inode)->root;
1562 struct btrfs_file_extent_item *fi;
1563 struct btrfs_path *path;
1564 struct extent_buffer *leaf;
1565 struct btrfs_key ins;
1566 u64 hint;
1567 int ret;
1568
1569 path = btrfs_alloc_path();
1570 BUG_ON(!path);
1571
Chris Masonb9473432009-03-13 11:00:37 -04001572 path->leave_spinning = 1;
Chris Masona1ed8352009-09-11 12:27:37 -04001573
1574 /*
1575 * we may be replacing one extent in the tree with another.
1576 * The new extent is pinned in the extent map, and we don't want
1577 * to drop it from the cache until it is completely in the btree.
1578 *
1579 * So, tell btrfs_drop_extents to leave this extent in the cache.
1580 * the caller is expected to unpin it and allow it to be merged
1581 * with the others.
1582 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001583 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1584 &hint, 0);
Yan Zhengd899e052008-10-30 14:25:28 -04001585 BUG_ON(ret);
1586
1587 ins.objectid = inode->i_ino;
1588 ins.offset = file_pos;
1589 ins.type = BTRFS_EXTENT_DATA_KEY;
1590 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1591 BUG_ON(ret);
1592 leaf = path->nodes[0];
1593 fi = btrfs_item_ptr(leaf, path->slots[0],
1594 struct btrfs_file_extent_item);
1595 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1596 btrfs_set_file_extent_type(leaf, fi, extent_type);
1597 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1598 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1599 btrfs_set_file_extent_offset(leaf, fi, 0);
1600 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1601 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1602 btrfs_set_file_extent_compression(leaf, fi, compression);
1603 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1604 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04001605
1606 btrfs_unlock_up_safe(path, 1);
1607 btrfs_set_lock_blocking(leaf);
1608
Yan Zhengd899e052008-10-30 14:25:28 -04001609 btrfs_mark_buffer_dirty(leaf);
1610
1611 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04001612
1613 ins.objectid = disk_bytenr;
1614 ins.offset = disk_num_bytes;
1615 ins.type = BTRFS_EXTENT_ITEM_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001616 ret = btrfs_alloc_reserved_file_extent(trans, root,
1617 root->root_key.objectid,
1618 inode->i_ino, file_pos, &ins);
Yan Zhengd899e052008-10-30 14:25:28 -04001619 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04001620 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04001621
Yan Zhengd899e052008-10-30 14:25:28 -04001622 return 0;
1623}
1624
Chris Mason5d13a982009-03-13 11:41:46 -04001625/*
1626 * helper function for btrfs_finish_ordered_io, this
1627 * just reads in some of the csum leaves to prime them into ram
1628 * before we start the transaction. It limits the amount of btree
1629 * reads required while inside the transaction.
1630 */
Chris Masond352ac62008-09-29 15:18:18 -04001631/* as ordered data IO finishes, this gets called so we can finish
1632 * an ordered extent if the range of bytes in the file it covers are
1633 * fully written.
1634 */
Chris Mason211f90e2008-07-18 11:56:15 -04001635static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001636{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001637 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001638 struct btrfs_trans_handle *trans = NULL;
Chris Mason5d13a982009-03-13 11:41:46 -04001639 struct btrfs_ordered_extent *ordered_extent = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001640 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001641 struct extent_state *cached_state = NULL;
Yan Zhengd899e052008-10-30 14:25:28 -04001642 int compressed = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001643 int ret;
1644
Josef Bacik5a1a3df2010-02-02 20:51:14 +00001645 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1646 end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001647 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001648 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001649 BUG_ON(!ordered_extent);
Josef Bacikefd049f2010-02-02 20:50:10 +00001650
Yan, Zhengc2167752009-11-12 09:34:21 +00001651 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1652 BUG_ON(!list_empty(&ordered_extent->list));
1653 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1654 if (!ret) {
1655 trans = btrfs_join_transaction(root, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001656 btrfs_set_trans_block_group(trans, inode);
1657 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001658 ret = btrfs_update_inode(trans, root, inode);
1659 BUG_ON(ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001660 }
1661 goto out;
1662 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001663
Josef Bacik2ac55d42010-02-03 19:33:23 +00001664 lock_extent_bits(io_tree, ordered_extent->file_offset,
1665 ordered_extent->file_offset + ordered_extent->len - 1,
1666 0, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001667
Yan, Zhengc2167752009-11-12 09:34:21 +00001668 trans = btrfs_join_transaction(root, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001669 btrfs_set_trans_block_group(trans, inode);
1670 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00001671
Chris Masonc8b97812008-10-29 14:49:59 -04001672 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Yan Zhengd899e052008-10-30 14:25:28 -04001673 compressed = 1;
1674 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1675 BUG_ON(compressed);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001676 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04001677 ordered_extent->file_offset,
1678 ordered_extent->file_offset +
1679 ordered_extent->len);
1680 BUG_ON(ret);
1681 } else {
1682 ret = insert_reserved_file_extent(trans, inode,
1683 ordered_extent->file_offset,
1684 ordered_extent->start,
1685 ordered_extent->disk_len,
1686 ordered_extent->len,
1687 ordered_extent->len,
1688 compressed, 0, 0,
1689 BTRFS_FILE_EXTENT_REG);
Chris Masona1ed8352009-09-11 12:27:37 -04001690 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1691 ordered_extent->file_offset,
1692 ordered_extent->len);
Yan Zhengd899e052008-10-30 14:25:28 -04001693 BUG_ON(ret);
1694 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00001695 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1696 ordered_extent->file_offset +
1697 ordered_extent->len - 1, &cached_state, GFP_NOFS);
1698
Chris Masone6dcd2d2008-07-17 12:53:50 -04001699 add_pending_csums(trans, inode, ordered_extent->file_offset,
1700 &ordered_extent->list);
1701
Yan, Zhengc2167752009-11-12 09:34:21 +00001702 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1703 ret = btrfs_update_inode(trans, root, inode);
1704 BUG_ON(ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00001705out:
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001706 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
1707 if (trans)
1708 btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001709 /* once for us */
1710 btrfs_put_ordered_extent(ordered_extent);
1711 /* once for the tree */
1712 btrfs_put_ordered_extent(ordered_extent);
1713
Chris Masone6dcd2d2008-07-17 12:53:50 -04001714 return 0;
1715}
1716
Christoph Hellwigb2950862008-12-02 09:54:17 -05001717static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001718 struct extent_state *state, int uptodate)
1719{
Chris Mason8b62b722009-09-02 16:53:46 -04001720 ClearPagePrivate2(page);
Chris Mason211f90e2008-07-18 11:56:15 -04001721 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1722}
1723
Chris Masond352ac62008-09-29 15:18:18 -04001724/*
1725 * When IO fails, either with EIO or csum verification fails, we
1726 * try other mirrors that might have a good copy of the data. This
1727 * io_failure_record is used to record state as we go through all the
1728 * mirrors. If another mirror has good data, the page is set up to date
1729 * and things continue. If a good mirror can't be found, the original
1730 * bio end_io callback is called to indicate things have failed.
1731 */
Chris Mason7e383262008-04-09 16:28:12 -04001732struct io_failure_record {
1733 struct page *page;
1734 u64 start;
1735 u64 len;
1736 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001737 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001738 int last_mirror;
1739};
1740
Christoph Hellwigb2950862008-12-02 09:54:17 -05001741static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001742 struct page *page, u64 start, u64 end,
1743 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001744{
1745 struct io_failure_record *failrec = NULL;
1746 u64 private;
1747 struct extent_map *em;
1748 struct inode *inode = page->mapping->host;
1749 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001750 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001751 struct bio *bio;
1752 int num_copies;
1753 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001754 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001755 u64 logical;
1756
1757 ret = get_state_private(failure_tree, start, &private);
1758 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001759 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1760 if (!failrec)
1761 return -ENOMEM;
1762 failrec->start = start;
1763 failrec->len = end - start + 1;
1764 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001765 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001766
Chris Mason890871b2009-09-02 16:24:52 -04001767 read_lock(&em_tree->lock);
Chris Mason3b951512008-04-17 11:29:12 -04001768 em = lookup_extent_mapping(em_tree, start, failrec->len);
1769 if (em->start > start || em->start + em->len < start) {
1770 free_extent_map(em);
1771 em = NULL;
1772 }
Chris Mason890871b2009-09-02 16:24:52 -04001773 read_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001774
1775 if (!em || IS_ERR(em)) {
1776 kfree(failrec);
1777 return -EIO;
1778 }
1779 logical = start - em->start;
1780 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001781 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1782 logical = em->block_start;
1783 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1784 }
Chris Mason7e383262008-04-09 16:28:12 -04001785 failrec->logical = logical;
1786 free_extent_map(em);
1787 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1788 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001789 set_state_private(failure_tree, start,
1790 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001791 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001792 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001793 }
1794 num_copies = btrfs_num_copies(
1795 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1796 failrec->logical, failrec->len);
1797 failrec->last_mirror++;
1798 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001799 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001800 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1801 failrec->start,
1802 EXTENT_LOCKED);
1803 if (state && state->start != failrec->start)
1804 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001805 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001806 }
1807 if (!state || failrec->last_mirror > num_copies) {
1808 set_state_private(failure_tree, failrec->start, 0);
1809 clear_extent_bits(failure_tree, failrec->start,
1810 failrec->start + failrec->len - 1,
1811 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1812 kfree(failrec);
1813 return -EIO;
1814 }
1815 bio = bio_alloc(GFP_NOFS, 1);
1816 bio->bi_private = state;
1817 bio->bi_end_io = failed_bio->bi_end_io;
1818 bio->bi_sector = failrec->logical >> 9;
1819 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001820 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001821
Chris Mason7e383262008-04-09 16:28:12 -04001822 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -04001823 if (failed_bio->bi_rw & (1 << BIO_RW))
1824 rw = WRITE;
1825 else
1826 rw = READ;
1827
1828 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001829 failrec->last_mirror,
Chris Masond20f7042008-12-08 16:58:54 -05001830 failrec->bio_flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001831 return 0;
1832}
1833
Chris Masond352ac62008-09-29 15:18:18 -04001834/*
1835 * each time an IO finishes, we do a fast check in the IO failure tree
1836 * to see if we need to process or clean up an io_failure_record
1837 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001838static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001839{
1840 u64 private;
1841 u64 private_failure;
1842 struct io_failure_record *failure;
1843 int ret;
1844
1845 private = 0;
1846 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1847 (u64)-1, 1, EXTENT_DIRTY)) {
1848 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1849 start, &private_failure);
1850 if (ret == 0) {
1851 failure = (struct io_failure_record *)(unsigned long)
1852 private_failure;
1853 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1854 failure->start, 0);
1855 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1856 failure->start,
1857 failure->start + failure->len - 1,
1858 EXTENT_DIRTY | EXTENT_LOCKED,
1859 GFP_NOFS);
1860 kfree(failure);
1861 }
1862 }
Chris Mason7e383262008-04-09 16:28:12 -04001863 return 0;
1864}
1865
Chris Masond352ac62008-09-29 15:18:18 -04001866/*
1867 * when reads are done, we need to check csums to verify the data is correct
1868 * if there's a match, we allow the bio to finish. If not, we go through
1869 * the io_failure_record routines to find good copies
1870 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001871static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001872 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001873{
Chris Mason35ebb932007-10-30 16:56:53 -04001874 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001875 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001876 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001877 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001878 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001879 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001880 struct btrfs_root *root = BTRFS_I(inode)->root;
1881 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001882
Chris Masond20f7042008-12-08 16:58:54 -05001883 if (PageChecked(page)) {
1884 ClearPageChecked(page);
1885 goto good;
1886 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001887
1888 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Chris Masonb6cda9b2007-12-14 15:30:32 -05001889 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001890
Yan Zheng17d217f2008-12-12 10:03:38 -05001891 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04001892 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001893 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1894 GFP_NOFS);
1895 return 0;
1896 }
1897
Yanc2e639f2008-02-04 08:57:25 -05001898 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001899 private = state->private;
1900 ret = 0;
1901 } else {
1902 ret = get_state_private(io_tree, start, &private);
1903 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001904 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05001905 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001906 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001907
Chris Masonff79f812007-10-15 16:22:25 -04001908 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1909 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001910 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04001911 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001912
Chris Mason9ab86c82009-01-07 09:48:51 -05001913 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05001914good:
Chris Mason7e383262008-04-09 16:28:12 -04001915 /* if the io failure tree for this inode is non-empty,
1916 * check to see if we've recovered from a failed IO
1917 */
Chris Mason1259ab72008-05-12 13:39:03 -04001918 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04001919 return 0;
1920
1921zeroit:
Chris Mason193f2842009-04-27 07:29:05 -04001922 if (printk_ratelimit()) {
1923 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1924 "private %llu\n", page->mapping->host->i_ino,
1925 (unsigned long long)start, csum,
1926 (unsigned long long)private);
1927 }
Chris Masondb945352007-10-15 16:15:53 -04001928 memset(kaddr + offset, 1, end - start + 1);
1929 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05001930 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04001931 if (private == 0)
1932 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04001933 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04001934}
Chris Masonb888db2b2007-08-27 16:49:44 -04001935
Yan, Zheng24bbcf02009-11-12 09:36:34 +00001936struct delayed_iput {
1937 struct list_head list;
1938 struct inode *inode;
1939};
1940
1941void btrfs_add_delayed_iput(struct inode *inode)
1942{
1943 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
1944 struct delayed_iput *delayed;
1945
1946 if (atomic_add_unless(&inode->i_count, -1, 1))
1947 return;
1948
1949 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
1950 delayed->inode = inode;
1951
1952 spin_lock(&fs_info->delayed_iput_lock);
1953 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
1954 spin_unlock(&fs_info->delayed_iput_lock);
1955}
1956
1957void btrfs_run_delayed_iputs(struct btrfs_root *root)
1958{
1959 LIST_HEAD(list);
1960 struct btrfs_fs_info *fs_info = root->fs_info;
1961 struct delayed_iput *delayed;
1962 int empty;
1963
1964 spin_lock(&fs_info->delayed_iput_lock);
1965 empty = list_empty(&fs_info->delayed_iputs);
1966 spin_unlock(&fs_info->delayed_iput_lock);
1967 if (empty)
1968 return;
1969
1970 down_read(&root->fs_info->cleanup_work_sem);
1971 spin_lock(&fs_info->delayed_iput_lock);
1972 list_splice_init(&fs_info->delayed_iputs, &list);
1973 spin_unlock(&fs_info->delayed_iput_lock);
1974
1975 while (!list_empty(&list)) {
1976 delayed = list_entry(list.next, struct delayed_iput, list);
1977 list_del(&delayed->list);
1978 iput(delayed->inode);
1979 kfree(delayed);
1980 }
1981 up_read(&root->fs_info->cleanup_work_sem);
1982}
1983
Josef Bacik7b128762008-07-24 12:17:14 -04001984/*
1985 * This creates an orphan entry for the given inode in case something goes
1986 * wrong in the middle of an unlink/truncate.
1987 */
1988int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
1989{
1990 struct btrfs_root *root = BTRFS_I(inode)->root;
1991 int ret = 0;
1992
Yanbcc63ab2008-07-30 16:29:20 -04001993 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001994
1995 /* already on the orphan list, we're good */
1996 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04001997 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001998 return 0;
1999 }
2000
2001 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2002
Yanbcc63ab2008-07-30 16:29:20 -04002003 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002004
2005 /*
2006 * insert an orphan item to track this unlinked/truncated file
2007 */
2008 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
2009
2010 return ret;
2011}
2012
2013/*
2014 * We have done the truncate/delete so we can go ahead and remove the orphan
2015 * item for this particular inode.
2016 */
2017int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2018{
2019 struct btrfs_root *root = BTRFS_I(inode)->root;
2020 int ret = 0;
2021
Yanbcc63ab2008-07-30 16:29:20 -04002022 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002023
2024 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04002025 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002026 return 0;
2027 }
2028
2029 list_del_init(&BTRFS_I(inode)->i_orphan);
2030 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -04002031 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002032 return 0;
2033 }
2034
Yanbcc63ab2008-07-30 16:29:20 -04002035 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002036
2037 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
2038
2039 return ret;
2040}
2041
2042/*
2043 * this cleans up any orphans that may be left on the list from the last use
2044 * of this root.
2045 */
2046void btrfs_orphan_cleanup(struct btrfs_root *root)
2047{
2048 struct btrfs_path *path;
2049 struct extent_buffer *leaf;
2050 struct btrfs_item *item;
2051 struct btrfs_key key, found_key;
2052 struct btrfs_trans_handle *trans;
2053 struct inode *inode;
2054 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2055
Yan, Zhengc71bf092009-11-12 09:34:40 +00002056 if (!xchg(&root->clean_orphans, 0))
Josef Bacik7b128762008-07-24 12:17:14 -04002057 return;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002058
2059 path = btrfs_alloc_path();
2060 BUG_ON(!path);
Josef Bacik7b128762008-07-24 12:17:14 -04002061 path->reada = -1;
2062
2063 key.objectid = BTRFS_ORPHAN_OBJECTID;
2064 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2065 key.offset = (u64)-1;
2066
Josef Bacik7b128762008-07-24 12:17:14 -04002067 while (1) {
2068 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2069 if (ret < 0) {
2070 printk(KERN_ERR "Error searching slot for orphan: %d"
2071 "\n", ret);
2072 break;
2073 }
2074
2075 /*
2076 * if ret == 0 means we found what we were searching for, which
2077 * is weird, but possible, so only screw with path if we didnt
2078 * find the key and see if we have stuff that matches
2079 */
2080 if (ret > 0) {
2081 if (path->slots[0] == 0)
2082 break;
2083 path->slots[0]--;
2084 }
2085
2086 /* pull out the item */
2087 leaf = path->nodes[0];
2088 item = btrfs_item_nr(leaf, path->slots[0]);
2089 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2090
2091 /* make sure the item matches what we want */
2092 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2093 break;
2094 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2095 break;
2096
2097 /* release the path since we're done with it */
2098 btrfs_release_path(root, path);
2099
2100 /*
2101 * this is where we are basically btrfs_lookup, without the
2102 * crossing root thing. we store the inode number in the
2103 * offset of the orphan item.
2104 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002105 found_key.objectid = found_key.offset;
2106 found_key.type = BTRFS_INODE_ITEM_KEY;
2107 found_key.offset = 0;
Josef Bacik73f73412009-12-04 17:38:27 +00002108 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002109 if (IS_ERR(inode))
Josef Bacik7b128762008-07-24 12:17:14 -04002110 break;
2111
Josef Bacik7b128762008-07-24 12:17:14 -04002112 /*
2113 * add this inode to the orphan list so btrfs_orphan_del does
2114 * the proper thing when we hit it
2115 */
Yanbcc63ab2008-07-30 16:29:20 -04002116 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002117 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -04002118 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04002119
2120 /*
2121 * if this is a bad inode, means we actually succeeded in
2122 * removing the inode, but not the orphan record, which means
2123 * we need to manually delete the orphan since iput will just
2124 * do a destroy_inode
2125 */
2126 if (is_bad_inode(inode)) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04002127 trans = btrfs_start_transaction(root, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04002128 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002129 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04002130 iput(inode);
2131 continue;
2132 }
2133
2134 /* if we have links, this was a truncate, lets do that */
2135 if (inode->i_nlink) {
2136 nr_truncate++;
2137 btrfs_truncate(inode);
2138 } else {
2139 nr_unlink++;
2140 }
2141
2142 /* this will do delete_inode and everything for us */
2143 iput(inode);
2144 }
2145
2146 if (nr_unlink)
2147 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2148 if (nr_truncate)
2149 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
2150
2151 btrfs_free_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002152}
2153
Chris Masond352ac62008-09-29 15:18:18 -04002154/*
Chris Mason46a53cc2009-04-27 11:47:50 -04002155 * very simple check to peek ahead in the leaf looking for xattrs. If we
2156 * don't find any xattrs, we know there can't be any acls.
2157 *
2158 * slot is the slot the inode is in, objectid is the objectid of the inode
2159 */
2160static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2161 int slot, u64 objectid)
2162{
2163 u32 nritems = btrfs_header_nritems(leaf);
2164 struct btrfs_key found_key;
2165 int scanned = 0;
2166
2167 slot++;
2168 while (slot < nritems) {
2169 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2170
2171 /* we found a different objectid, there must not be acls */
2172 if (found_key.objectid != objectid)
2173 return 0;
2174
2175 /* we found an xattr, assume we've got an acl */
2176 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2177 return 1;
2178
2179 /*
2180 * we found a key greater than an xattr key, there can't
2181 * be any acls later on
2182 */
2183 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2184 return 0;
2185
2186 slot++;
2187 scanned++;
2188
2189 /*
2190 * it goes inode, inode backrefs, xattrs, extents,
2191 * so if there are a ton of hard links to an inode there can
2192 * be a lot of backrefs. Don't waste time searching too hard,
2193 * this is just an optimization
2194 */
2195 if (scanned >= 8)
2196 break;
2197 }
2198 /* we hit the end of the leaf before we found an xattr or
2199 * something larger than an xattr. We have to assume the inode
2200 * has acls
2201 */
2202 return 1;
2203}
2204
2205/*
Chris Masond352ac62008-09-29 15:18:18 -04002206 * read an inode from the btree into the in-memory inode
2207 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002208static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002209{
2210 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002211 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002212 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04002213 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04002214 struct btrfs_root *root = BTRFS_I(inode)->root;
2215 struct btrfs_key location;
Chris Mason46a53cc2009-04-27 11:47:50 -04002216 int maybe_acls;
Chris Mason39279cc2007-06-12 06:35:45 -04002217 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04002218 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04002219 int ret;
2220
2221 path = btrfs_alloc_path();
2222 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002223 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002224
Chris Mason39279cc2007-06-12 06:35:45 -04002225 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002226 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002227 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002228
Chris Mason5f39d392007-10-15 16:14:19 -04002229 leaf = path->nodes[0];
2230 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2231 struct btrfs_inode_item);
2232
2233 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2234 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2235 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2236 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002237 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002238
2239 tspec = btrfs_inode_atime(inode_item);
2240 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2241 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2242
2243 tspec = btrfs_inode_mtime(inode_item);
2244 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2245 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2246
2247 tspec = btrfs_inode_ctime(inode_item);
2248 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2249 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2250
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002251 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002252 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002253 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002254 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002255 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002256 rdev = btrfs_inode_rdev(leaf, inode_item);
2257
Josef Bacikaec74772008-07-24 12:12:38 -04002258 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002259 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002260
Chris Mason5f39d392007-10-15 16:14:19 -04002261 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002262
Chris Mason46a53cc2009-04-27 11:47:50 -04002263 /*
2264 * try to precache a NULL acl entry for files that don't have
2265 * any xattrs or acls
2266 */
2267 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
Al Viro72c04902009-06-24 16:58:48 -04002268 if (!maybe_acls)
2269 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04002270
Yan Zhengd2fb3432008-12-11 16:30:39 -05002271 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2272 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002273 btrfs_free_path(path);
2274 inode_item = NULL;
2275
Chris Mason39279cc2007-06-12 06:35:45 -04002276 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002277 case S_IFREG:
2278 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002279 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002280 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002281 inode->i_fop = &btrfs_file_operations;
2282 inode->i_op = &btrfs_file_inode_operations;
2283 break;
2284 case S_IFDIR:
2285 inode->i_fop = &btrfs_dir_file_operations;
2286 if (root == root->fs_info->tree_root)
2287 inode->i_op = &btrfs_dir_ro_inode_operations;
2288 else
2289 inode->i_op = &btrfs_dir_inode_operations;
2290 break;
2291 case S_IFLNK:
2292 inode->i_op = &btrfs_symlink_inode_operations;
2293 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002294 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002295 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002296 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002297 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002298 init_special_inode(inode, inode->i_mode, rdev);
2299 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002300 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002301
2302 btrfs_update_iflags(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002303 return;
2304
2305make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002306 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002307 make_bad_inode(inode);
2308}
2309
Chris Masond352ac62008-09-29 15:18:18 -04002310/*
2311 * given a leaf and an inode, copy the inode fields into the leaf
2312 */
Chris Masone02119d2008-09-05 16:13:11 -04002313static void fill_inode_item(struct btrfs_trans_handle *trans,
2314 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002315 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002316 struct inode *inode)
2317{
Chris Mason5f39d392007-10-15 16:14:19 -04002318 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2319 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002320 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002321 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2322 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2323
2324 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2325 inode->i_atime.tv_sec);
2326 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2327 inode->i_atime.tv_nsec);
2328
2329 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2330 inode->i_mtime.tv_sec);
2331 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2332 inode->i_mtime.tv_nsec);
2333
2334 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2335 inode->i_ctime.tv_sec);
2336 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2337 inode->i_ctime.tv_nsec);
2338
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002339 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002340 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002341 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002342 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002343 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002344 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002345 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Chris Mason39279cc2007-06-12 06:35:45 -04002346}
2347
Chris Masond352ac62008-09-29 15:18:18 -04002348/*
2349 * copy everything in the in-memory inode into the btree.
2350 */
Chris Masond3977122009-01-05 21:25:51 -05002351noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2352 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002353{
2354 struct btrfs_inode_item *inode_item;
2355 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002356 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002357 int ret;
2358
2359 path = btrfs_alloc_path();
2360 BUG_ON(!path);
Chris Masonb9473432009-03-13 11:00:37 -04002361 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002362 ret = btrfs_lookup_inode(trans, root, path,
2363 &BTRFS_I(inode)->location, 1);
2364 if (ret) {
2365 if (ret > 0)
2366 ret = -ENOENT;
2367 goto failed;
2368 }
2369
Chris Masonb4ce94d2009-02-04 09:25:08 -05002370 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002371 leaf = path->nodes[0];
2372 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002373 struct btrfs_inode_item);
2374
Chris Masone02119d2008-09-05 16:13:11 -04002375 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002376 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002377 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002378 ret = 0;
2379failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002380 btrfs_free_path(path);
2381 return ret;
2382}
2383
2384
Chris Masond352ac62008-09-29 15:18:18 -04002385/*
2386 * unlink helper that gets used here in inode.c and in the tree logging
2387 * recovery code. It remove a link in a directory with a given name, and
2388 * also drops the back refs in the inode to the directory
2389 */
Chris Masone02119d2008-09-05 16:13:11 -04002390int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2391 struct btrfs_root *root,
2392 struct inode *dir, struct inode *inode,
2393 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002394{
2395 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002396 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002397 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002398 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002399 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002400 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04002401
2402 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002403 if (!path) {
2404 ret = -ENOMEM;
2405 goto err;
2406 }
2407
Chris Masonb9473432009-03-13 11:00:37 -04002408 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002409 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2410 name, name_len, -1);
2411 if (IS_ERR(di)) {
2412 ret = PTR_ERR(di);
2413 goto err;
2414 }
2415 if (!di) {
2416 ret = -ENOENT;
2417 goto err;
2418 }
Chris Mason5f39d392007-10-15 16:14:19 -04002419 leaf = path->nodes[0];
2420 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002421 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002422 if (ret)
2423 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002424 btrfs_release_path(root, path);
2425
Josef Bacikaec74772008-07-24 12:12:38 -04002426 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04002427 inode->i_ino,
2428 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002429 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002430 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Josef Bacikaec74772008-07-24 12:12:38 -04002431 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04002432 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002433 goto err;
2434 }
2435
Chris Mason39279cc2007-06-12 06:35:45 -04002436 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002437 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002438 if (IS_ERR(di)) {
2439 ret = PTR_ERR(di);
2440 goto err;
2441 }
2442 if (!di) {
2443 ret = -ENOENT;
2444 goto err;
2445 }
2446 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002447 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002448
Chris Masone02119d2008-09-05 16:13:11 -04002449 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2450 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002451 BUG_ON(ret != 0 && ret != -ENOENT);
Chris Masone02119d2008-09-05 16:13:11 -04002452
2453 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2454 dir, index);
2455 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002456err:
2457 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002458 if (ret)
2459 goto out;
2460
2461 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2462 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2463 btrfs_update_inode(trans, root, dir);
2464 btrfs_drop_nlink(inode);
2465 ret = btrfs_update_inode(trans, root, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002466out:
Chris Mason39279cc2007-06-12 06:35:45 -04002467 return ret;
2468}
2469
Yan, Zhenga22285a2010-05-16 10:48:46 -04002470/* helper to check if there is any shared block in the path */
2471static int check_path_shared(struct btrfs_root *root,
2472 struct btrfs_path *path)
2473{
2474 struct extent_buffer *eb;
2475 int level;
2476 int ret;
2477 u64 refs;
2478
2479 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2480 if (!path->nodes[level])
2481 break;
2482 eb = path->nodes[level];
2483 if (!btrfs_block_can_be_shared(root, eb))
2484 continue;
2485 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2486 &refs, NULL);
2487 if (refs > 1)
2488 return 1;
2489 }
2490 return 0;
2491}
2492
2493/*
2494 * helper to start transaction for unlink and rmdir.
2495 *
2496 * unlink and rmdir are special in btrfs, they do not always free space.
2497 * so in enospc case, we should make sure they will free space before
2498 * allowing them to use the global metadata reservation.
2499 */
2500static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2501 struct dentry *dentry)
2502{
2503 struct btrfs_trans_handle *trans;
2504 struct btrfs_root *root = BTRFS_I(dir)->root;
2505 struct btrfs_path *path;
2506 struct btrfs_inode_ref *ref;
2507 struct btrfs_dir_item *di;
2508 struct inode *inode = dentry->d_inode;
2509 u64 index;
2510 int check_link = 1;
2511 int err = -ENOSPC;
2512 int ret;
2513
2514 trans = btrfs_start_transaction(root, 10);
2515 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2516 return trans;
2517
2518 if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
2519 return ERR_PTR(-ENOSPC);
2520
2521 /* check if there is someone else holds reference */
2522 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2523 return ERR_PTR(-ENOSPC);
2524
2525 if (atomic_read(&inode->i_count) > 2)
2526 return ERR_PTR(-ENOSPC);
2527
2528 if (xchg(&root->fs_info->enospc_unlink, 1))
2529 return ERR_PTR(-ENOSPC);
2530
2531 path = btrfs_alloc_path();
2532 if (!path) {
2533 root->fs_info->enospc_unlink = 0;
2534 return ERR_PTR(-ENOMEM);
2535 }
2536
2537 trans = btrfs_start_transaction(root, 0);
2538 if (IS_ERR(trans)) {
2539 btrfs_free_path(path);
2540 root->fs_info->enospc_unlink = 0;
2541 return trans;
2542 }
2543
2544 path->skip_locking = 1;
2545 path->search_commit_root = 1;
2546
2547 ret = btrfs_lookup_inode(trans, root, path,
2548 &BTRFS_I(dir)->location, 0);
2549 if (ret < 0) {
2550 err = ret;
2551 goto out;
2552 }
2553 if (ret == 0) {
2554 if (check_path_shared(root, path))
2555 goto out;
2556 } else {
2557 check_link = 0;
2558 }
2559 btrfs_release_path(root, path);
2560
2561 ret = btrfs_lookup_inode(trans, root, path,
2562 &BTRFS_I(inode)->location, 0);
2563 if (ret < 0) {
2564 err = ret;
2565 goto out;
2566 }
2567 if (ret == 0) {
2568 if (check_path_shared(root, path))
2569 goto out;
2570 } else {
2571 check_link = 0;
2572 }
2573 btrfs_release_path(root, path);
2574
2575 if (ret == 0 && S_ISREG(inode->i_mode)) {
2576 ret = btrfs_lookup_file_extent(trans, root, path,
2577 inode->i_ino, (u64)-1, 0);
2578 if (ret < 0) {
2579 err = ret;
2580 goto out;
2581 }
2582 BUG_ON(ret == 0);
2583 if (check_path_shared(root, path))
2584 goto out;
2585 btrfs_release_path(root, path);
2586 }
2587
2588 if (!check_link) {
2589 err = 0;
2590 goto out;
2591 }
2592
2593 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2594 dentry->d_name.name, dentry->d_name.len, 0);
2595 if (IS_ERR(di)) {
2596 err = PTR_ERR(di);
2597 goto out;
2598 }
2599 if (di) {
2600 if (check_path_shared(root, path))
2601 goto out;
2602 } else {
2603 err = 0;
2604 goto out;
2605 }
2606 btrfs_release_path(root, path);
2607
2608 ref = btrfs_lookup_inode_ref(trans, root, path,
2609 dentry->d_name.name, dentry->d_name.len,
2610 inode->i_ino, dir->i_ino, 0);
2611 if (IS_ERR(ref)) {
2612 err = PTR_ERR(ref);
2613 goto out;
2614 }
2615 BUG_ON(!ref);
2616 if (check_path_shared(root, path))
2617 goto out;
2618 index = btrfs_inode_ref_index(path->nodes[0], ref);
2619 btrfs_release_path(root, path);
2620
2621 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index,
2622 dentry->d_name.name, dentry->d_name.len, 0);
2623 if (IS_ERR(di)) {
2624 err = PTR_ERR(di);
2625 goto out;
2626 }
2627 BUG_ON(ret == -ENOENT);
2628 if (check_path_shared(root, path))
2629 goto out;
2630
2631 err = 0;
2632out:
2633 btrfs_free_path(path);
2634 if (err) {
2635 btrfs_end_transaction(trans, root);
2636 root->fs_info->enospc_unlink = 0;
2637 return ERR_PTR(err);
2638 }
2639
2640 trans->block_rsv = &root->fs_info->global_block_rsv;
2641 return trans;
2642}
2643
2644static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2645 struct btrfs_root *root)
2646{
2647 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
2648 BUG_ON(!root->fs_info->enospc_unlink);
2649 root->fs_info->enospc_unlink = 0;
2650 }
2651 btrfs_end_transaction_throttle(trans, root);
2652}
2653
Chris Mason39279cc2007-06-12 06:35:45 -04002654static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2655{
Yan, Zhenga22285a2010-05-16 10:48:46 -04002656 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002657 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002658 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002659 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002660 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002661
Yan, Zhenga22285a2010-05-16 10:48:46 -04002662 trans = __unlink_start_trans(dir, dentry);
2663 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002664 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04002665
Chris Mason39279cc2007-06-12 06:35:45 -04002666 btrfs_set_trans_block_group(trans, dir);
Chris Mason12fcfd22009-03-24 10:24:20 -04002667
2668 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2669
Chris Masone02119d2008-09-05 16:13:11 -04002670 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2671 dentry->d_name.name, dentry->d_name.len);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002672 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04002673
Yan, Zhenga22285a2010-05-16 10:48:46 -04002674 if (inode->i_nlink == 0) {
Josef Bacik7b128762008-07-24 12:17:14 -04002675 ret = btrfs_orphan_add(trans, inode);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002676 BUG_ON(ret);
2677 }
Josef Bacik7b128762008-07-24 12:17:14 -04002678
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002679 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002680 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002681 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002682 return ret;
2683}
2684
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002685int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2686 struct btrfs_root *root,
2687 struct inode *dir, u64 objectid,
2688 const char *name, int name_len)
2689{
2690 struct btrfs_path *path;
2691 struct extent_buffer *leaf;
2692 struct btrfs_dir_item *di;
2693 struct btrfs_key key;
2694 u64 index;
2695 int ret;
2696
2697 path = btrfs_alloc_path();
2698 if (!path)
2699 return -ENOMEM;
2700
2701 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2702 name, name_len, -1);
2703 BUG_ON(!di || IS_ERR(di));
2704
2705 leaf = path->nodes[0];
2706 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2707 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2708 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2709 BUG_ON(ret);
2710 btrfs_release_path(root, path);
2711
2712 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2713 objectid, root->root_key.objectid,
2714 dir->i_ino, &index, name, name_len);
2715 if (ret < 0) {
2716 BUG_ON(ret != -ENOENT);
2717 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
2718 name, name_len);
2719 BUG_ON(!di || IS_ERR(di));
2720
2721 leaf = path->nodes[0];
2722 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2723 btrfs_release_path(root, path);
2724 index = key.offset;
2725 }
2726
2727 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2728 index, name, name_len, -1);
2729 BUG_ON(!di || IS_ERR(di));
2730
2731 leaf = path->nodes[0];
2732 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2733 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2734 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2735 BUG_ON(ret);
2736 btrfs_release_path(root, path);
2737
2738 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2739 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2740 ret = btrfs_update_inode(trans, root, dir);
2741 BUG_ON(ret);
2742 dir->i_sb->s_dirt = 1;
2743
2744 btrfs_free_path(path);
2745 return 0;
2746}
2747
Chris Mason39279cc2007-06-12 06:35:45 -04002748static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2749{
2750 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002751 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002752 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002753 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05002754 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002755
Chris Mason3394e162008-11-17 20:42:26 -05002756 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002757 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
Yan134d4512007-10-25 15:49:25 -04002758 return -ENOTEMPTY;
2759
Yan, Zhenga22285a2010-05-16 10:48:46 -04002760 trans = __unlink_start_trans(dir, dentry);
2761 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002762 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05002763
Chris Mason39279cc2007-06-12 06:35:45 -04002764 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002765
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002766 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
2767 err = btrfs_unlink_subvol(trans, root, dir,
2768 BTRFS_I(inode)->location.objectid,
2769 dentry->d_name.name,
2770 dentry->d_name.len);
2771 goto out;
2772 }
2773
Josef Bacik7b128762008-07-24 12:17:14 -04002774 err = btrfs_orphan_add(trans, inode);
2775 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002776 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002777
Chris Mason39279cc2007-06-12 06:35:45 -04002778 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04002779 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2780 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05002781 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04002782 btrfs_i_size_write(inode, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04002783out:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002784 nr = trans->blocks_used;
Yan, Zhenga22285a2010-05-16 10:48:46 -04002785 __unlink_end_trans(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002786 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05002787
Chris Mason39279cc2007-06-12 06:35:45 -04002788 return err;
2789}
2790
Chris Masond20f7042008-12-08 16:58:54 -05002791#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04002792/*
Chris Mason323ac952008-10-01 19:05:46 -04002793 * when truncating bytes in a file, it is possible to avoid reading
2794 * the leaves that contain only checksum items. This can be the
2795 * majority of the IO required to delete a large file, but it must
2796 * be done carefully.
2797 *
2798 * The keys in the level just above the leaves are checked to make sure
2799 * the lowest key in a given leaf is a csum key, and starts at an offset
2800 * after the new size.
2801 *
2802 * Then the key for the next leaf is checked to make sure it also has
2803 * a checksum item for the same file. If it does, we know our target leaf
2804 * contains only checksum items, and it can be safely freed without reading
2805 * it.
2806 *
2807 * This is just an optimization targeted at large files. It may do
2808 * nothing. It will return 0 unless things went badly.
2809 */
2810static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
2811 struct btrfs_root *root,
2812 struct btrfs_path *path,
2813 struct inode *inode, u64 new_size)
2814{
2815 struct btrfs_key key;
2816 int ret;
2817 int nritems;
2818 struct btrfs_key found_key;
2819 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002820 struct btrfs_leaf_ref *ref;
2821 u64 leaf_gen;
2822 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04002823
2824 path->lowest_level = 1;
2825 key.objectid = inode->i_ino;
2826 key.type = BTRFS_CSUM_ITEM_KEY;
2827 key.offset = new_size;
2828again:
2829 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2830 if (ret < 0)
2831 goto out;
2832
2833 if (path->nodes[1] == NULL) {
2834 ret = 0;
2835 goto out;
2836 }
2837 ret = 0;
2838 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
2839 nritems = btrfs_header_nritems(path->nodes[1]);
2840
2841 if (!nritems)
2842 goto out;
2843
2844 if (path->slots[1] >= nritems)
2845 goto next_node;
2846
2847 /* did we find a key greater than anything we want to delete? */
2848 if (found_key.objectid > inode->i_ino ||
2849 (found_key.objectid == inode->i_ino && found_key.type > key.type))
2850 goto out;
2851
2852 /* we check the next key in the node to make sure the leave contains
2853 * only checksum items. This comparison doesn't work if our
2854 * leaf is the last one in the node
2855 */
2856 if (path->slots[1] + 1 >= nritems) {
2857next_node:
2858 /* search forward from the last key in the node, this
2859 * will bring us into the next node in the tree
2860 */
2861 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
2862
2863 /* unlikely, but we inc below, so check to be safe */
2864 if (found_key.offset == (u64)-1)
2865 goto out;
2866
2867 /* search_forward needs a path with locks held, do the
2868 * search again for the original key. It is possible
2869 * this will race with a balance and return a path that
2870 * we could modify, but this drop is just an optimization
2871 * and is allowed to miss some leaves.
2872 */
2873 btrfs_release_path(root, path);
2874 found_key.offset++;
2875
2876 /* setup a max key for search_forward */
2877 other_key.offset = (u64)-1;
2878 other_key.type = key.type;
2879 other_key.objectid = key.objectid;
2880
2881 path->keep_locks = 1;
2882 ret = btrfs_search_forward(root, &found_key, &other_key,
2883 path, 0, 0);
2884 path->keep_locks = 0;
2885 if (ret || found_key.objectid != key.objectid ||
2886 found_key.type != key.type) {
2887 ret = 0;
2888 goto out;
2889 }
2890
2891 key.offset = found_key.offset;
2892 btrfs_release_path(root, path);
2893 cond_resched();
2894 goto again;
2895 }
2896
2897 /* we know there's one more slot after us in the tree,
2898 * read that key so we can verify it is also a checksum item
2899 */
2900 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
2901
2902 if (found_key.objectid < inode->i_ino)
2903 goto next_key;
2904
2905 if (found_key.type != key.type || found_key.offset < new_size)
2906 goto next_key;
2907
2908 /*
2909 * if the key for the next leaf isn't a csum key from this objectid,
2910 * we can't be sure there aren't good items inside this leaf.
2911 * Bail out
2912 */
2913 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
2914 goto out;
2915
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002916 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
2917 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04002918 /*
2919 * it is safe to delete this leaf, it contains only
2920 * csum items from this inode at an offset >= new_size
2921 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002922 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04002923 BUG_ON(ret);
2924
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002925 if (root->ref_cows && leaf_gen < trans->transid) {
2926 ref = btrfs_alloc_leaf_ref(root, 0);
2927 if (ref) {
2928 ref->root_gen = root->root_key.offset;
2929 ref->bytenr = leaf_start;
2930 ref->owner = 0;
2931 ref->generation = leaf_gen;
2932 ref->nritems = 0;
2933
Chris Masonbd56b302009-02-04 09:27:02 -05002934 btrfs_sort_leaf_ref(ref);
2935
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002936 ret = btrfs_add_leaf_ref(root, ref, 0);
2937 WARN_ON(ret);
2938 btrfs_free_leaf_ref(root, ref);
2939 } else {
2940 WARN_ON(1);
2941 }
2942 }
Chris Mason323ac952008-10-01 19:05:46 -04002943next_key:
2944 btrfs_release_path(root, path);
2945
2946 if (other_key.objectid == inode->i_ino &&
2947 other_key.type == key.type && other_key.offset > key.offset) {
2948 key.offset = other_key.offset;
2949 cond_resched();
2950 goto again;
2951 }
2952 ret = 0;
2953out:
2954 /* fixup any changes we've made to the path */
2955 path->lowest_level = 0;
2956 path->keep_locks = 0;
2957 btrfs_release_path(root, path);
2958 return ret;
2959}
2960
Chris Masond20f7042008-12-08 16:58:54 -05002961#endif
2962
Chris Mason323ac952008-10-01 19:05:46 -04002963/*
Chris Mason39279cc2007-06-12 06:35:45 -04002964 * this can truncate away extent items, csum items and directory items.
2965 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04002966 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04002967 *
2968 * csum items that cross the new i_size are truncated to the new size
2969 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04002970 *
2971 * min_type is the minimum key type to truncate down to. If set to 0, this
2972 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04002973 */
Yan, Zheng80825102009-11-12 09:35:36 +00002974int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2975 struct btrfs_root *root,
2976 struct inode *inode,
2977 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002978{
Chris Mason39279cc2007-06-12 06:35:45 -04002979 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002980 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002981 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00002982 struct btrfs_key key;
2983 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04002984 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04002985 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002986 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002987 u64 item_end = 0;
Yan, Zheng80825102009-11-12 09:35:36 +00002988 u64 mask = root->sectorsize - 1;
2989 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04002990 int found_extent;
2991 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002992 int pending_del_nr = 0;
2993 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04002994 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05002995 int encoding;
Yan, Zheng80825102009-11-12 09:35:36 +00002996 int ret;
2997 int err = 0;
2998
2999 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003000
Chris Masone02119d2008-09-05 16:13:11 -04003001 if (root->ref_cows)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003002 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003003
Chris Mason39279cc2007-06-12 06:35:45 -04003004 path = btrfs_alloc_path();
3005 BUG_ON(!path);
Julia Lawall33c17ad2009-07-22 16:49:01 -04003006 path->reada = -1;
Chris Mason5f39d392007-10-15 16:14:19 -04003007
Chris Mason39279cc2007-06-12 06:35:45 -04003008 key.objectid = inode->i_ino;
3009 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04003010 key.type = (u8)-1;
3011
Chris Mason85e21ba2008-01-29 15:11:36 -05003012search_again:
Chris Masonb9473432009-03-13 11:00:37 -04003013 path->leave_spinning = 1;
Chris Mason85e21ba2008-01-29 15:11:36 -05003014 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00003015 if (ret < 0) {
3016 err = ret;
3017 goto out;
3018 }
Chris Masond3977122009-01-05 21:25:51 -05003019
Chris Mason85e21ba2008-01-29 15:11:36 -05003020 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003021 /* there are no items in the tree for us to truncate, we're
3022 * done
3023 */
Yan, Zheng80825102009-11-12 09:35:36 +00003024 if (path->slots[0] == 0)
3025 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05003026 path->slots[0]--;
3027 }
3028
Chris Masond3977122009-01-05 21:25:51 -05003029 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003030 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04003031 leaf = path->nodes[0];
3032 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3033 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05003034 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003035
Chris Mason5f39d392007-10-15 16:14:19 -04003036 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04003037 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003038
Chris Mason85e21ba2008-01-29 15:11:36 -05003039 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003040 break;
3041
Chris Mason5f39d392007-10-15 16:14:19 -04003042 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04003043 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04003044 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04003045 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04003046 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003047 encoding = btrfs_file_extent_compression(leaf, fi);
3048 encoding |= btrfs_file_extent_encryption(leaf, fi);
3049 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
3050
Chris Mason179e29e2007-11-01 11:28:41 -04003051 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04003052 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04003053 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04003054 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04003055 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04003056 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003057 }
Yan008630c2007-11-07 13:31:09 -05003058 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04003059 }
Yan, Zheng80825102009-11-12 09:35:36 +00003060 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04003061 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00003062 } else {
3063 if (item_end < new_size)
3064 break;
3065 if (found_key.offset >= new_size)
3066 del_item = 1;
3067 else
3068 del_item = 0;
3069 }
Chris Mason39279cc2007-06-12 06:35:45 -04003070 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003071 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04003072 if (found_type != BTRFS_EXTENT_DATA_KEY)
3073 goto delete;
3074
3075 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04003076 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04003077 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05003078 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04003079 u64 orig_num_bytes =
3080 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04003081 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04003082 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05003083 extent_num_bytes = extent_num_bytes &
3084 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04003085 btrfs_set_file_extent_num_bytes(leaf, fi,
3086 extent_num_bytes);
3087 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05003088 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04003089 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003090 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04003091 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003092 } else {
Chris Masondb945352007-10-15 16:15:53 -04003093 extent_num_bytes =
3094 btrfs_file_extent_disk_num_bytes(leaf,
3095 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003096 extent_offset = found_key.offset -
3097 btrfs_file_extent_offset(leaf, fi);
3098
Chris Mason39279cc2007-06-12 06:35:45 -04003099 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05003100 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04003101 if (extent_start != 0) {
3102 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04003103 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003104 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04003105 }
3106 }
Chris Mason90692182008-02-08 13:49:28 -05003107 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003108 /*
3109 * we can't truncate inline items that have had
3110 * special encodings
3111 */
3112 if (!del_item &&
3113 btrfs_file_extent_compression(leaf, fi) == 0 &&
3114 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3115 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003116 u32 size = new_size - found_key.offset;
3117
3118 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003119 inode_sub_bytes(inode, item_end + 1 -
3120 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04003121 }
3122 size =
3123 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05003124 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04003125 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05003126 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04003127 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003128 inode_sub_bytes(inode, item_end + 1 -
3129 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05003130 }
Chris Mason39279cc2007-06-12 06:35:45 -04003131 }
Chris Mason179e29e2007-11-01 11:28:41 -04003132delete:
Chris Mason39279cc2007-06-12 06:35:45 -04003133 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05003134 if (!pending_del_nr) {
3135 /* no pending yet, add ourselves */
3136 pending_del_slot = path->slots[0];
3137 pending_del_nr = 1;
3138 } else if (pending_del_nr &&
3139 path->slots[0] + 1 == pending_del_slot) {
3140 /* hop on the pending chunk */
3141 pending_del_nr++;
3142 pending_del_slot = path->slots[0];
3143 } else {
Chris Masond3977122009-01-05 21:25:51 -05003144 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05003145 }
Chris Mason39279cc2007-06-12 06:35:45 -04003146 } else {
3147 break;
3148 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003149 if (found_extent && root->ref_cows) {
Chris Masonb9473432009-03-13 11:00:37 -04003150 btrfs_set_path_blocking(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003151 ret = btrfs_free_extent(trans, root, extent_start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003152 extent_num_bytes, 0,
3153 btrfs_header_owner(leaf),
3154 inode->i_ino, extent_offset);
Chris Mason39279cc2007-06-12 06:35:45 -04003155 BUG_ON(ret);
3156 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003157
Yan, Zheng80825102009-11-12 09:35:36 +00003158 if (found_type == BTRFS_INODE_ITEM_KEY)
3159 break;
3160
3161 if (path->slots[0] == 0 ||
3162 path->slots[0] != pending_del_slot) {
3163 if (root->ref_cows) {
3164 err = -EAGAIN;
3165 goto out;
3166 }
3167 if (pending_del_nr) {
3168 ret = btrfs_del_items(trans, root, path,
3169 pending_del_slot,
3170 pending_del_nr);
3171 BUG_ON(ret);
3172 pending_del_nr = 0;
3173 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003174 btrfs_release_path(root, path);
3175 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00003176 } else {
3177 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05003178 }
Chris Mason39279cc2007-06-12 06:35:45 -04003179 }
Yan, Zheng80825102009-11-12 09:35:36 +00003180out:
Chris Mason85e21ba2008-01-29 15:11:36 -05003181 if (pending_del_nr) {
3182 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3183 pending_del_nr);
3184 }
Chris Mason39279cc2007-06-12 06:35:45 -04003185 btrfs_free_path(path);
Yan, Zheng80825102009-11-12 09:35:36 +00003186 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003187}
3188
Chris Masona52d9a82007-08-27 16:49:44 -04003189/*
3190 * taken from block_truncate_page, but does cow as it zeros out
3191 * any bytes left in the last page in the file.
3192 */
3193static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3194{
3195 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04003196 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003197 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3198 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003199 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003200 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04003201 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04003202 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3203 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3204 struct page *page;
3205 int ret = 0;
3206 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003207 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04003208
3209 if ((offset & (blocksize - 1)) == 0)
3210 goto out;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003211 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003212 if (ret)
3213 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003214
3215 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04003216again:
Chris Masona52d9a82007-08-27 16:49:44 -04003217 page = grab_cache_page(mapping, index);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003218 if (!page) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003219 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04003220 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04003221 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003222
3223 page_start = page_offset(page);
3224 page_end = page_start + PAGE_CACHE_SIZE - 1;
3225
Chris Masona52d9a82007-08-27 16:49:44 -04003226 if (!PageUptodate(page)) {
3227 ret = btrfs_readpage(NULL, page);
3228 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04003229 if (page->mapping != mapping) {
3230 unlock_page(page);
3231 page_cache_release(page);
3232 goto again;
3233 }
Chris Masona52d9a82007-08-27 16:49:44 -04003234 if (!PageUptodate(page)) {
3235 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04003236 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04003237 }
3238 }
Chris Mason211c17f2008-05-15 09:13:45 -04003239 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003240
Josef Bacik2ac55d42010-02-03 19:33:23 +00003241 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
3242 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003243 set_page_extent_mapped(page);
3244
3245 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3246 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003247 unlock_extent_cached(io_tree, page_start, page_end,
3248 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003249 unlock_page(page);
3250 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003251 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003252 btrfs_put_ordered_extent(ordered);
3253 goto again;
3254 }
3255
Josef Bacik2ac55d42010-02-03 19:33:23 +00003256 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik5d5e1032009-10-13 16:46:49 -04003257 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003258 0, 0, &cached_state, GFP_NOFS);
Josef Bacik5d5e1032009-10-13 16:46:49 -04003259
Josef Bacik2ac55d42010-02-03 19:33:23 +00003260 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3261 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003262 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00003263 unlock_extent_cached(io_tree, page_start, page_end,
3264 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003265 goto out_unlock;
3266 }
3267
Chris Masone6dcd2d2008-07-17 12:53:50 -04003268 ret = 0;
3269 if (offset != PAGE_CACHE_SIZE) {
3270 kaddr = kmap(page);
3271 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3272 flush_dcache_page(page);
3273 kunmap(page);
3274 }
Chris Mason247e7432008-07-17 12:53:51 -04003275 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003276 set_page_dirty(page);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003277 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3278 GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04003279
Chris Mason89642222008-07-24 09:41:53 -04003280out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04003281 if (ret)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003282 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason39279cc2007-06-12 06:35:45 -04003283 unlock_page(page);
3284 page_cache_release(page);
3285out:
3286 return ret;
3287}
3288
Yan Zheng9036c102008-10-30 14:19:41 -04003289int btrfs_cont_expand(struct inode *inode, loff_t size)
3290{
3291 struct btrfs_trans_handle *trans;
3292 struct btrfs_root *root = BTRFS_I(inode)->root;
3293 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003294 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003295 struct extent_state *cached_state = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003296 u64 mask = root->sectorsize - 1;
3297 u64 hole_start = (inode->i_size + mask) & ~mask;
3298 u64 block_end = (size + mask) & ~mask;
3299 u64 last_byte;
3300 u64 cur_offset;
3301 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003302 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003303
3304 if (size <= hole_start)
3305 return 0;
3306
Yan Zheng9036c102008-10-30 14:19:41 -04003307 while (1) {
3308 struct btrfs_ordered_extent *ordered;
3309 btrfs_wait_ordered_range(inode, hole_start,
3310 block_end - hole_start);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003311 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
3312 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003313 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3314 if (!ordered)
3315 break;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003316 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3317 &cached_state, GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003318 btrfs_put_ordered_extent(ordered);
3319 }
3320
Yan Zheng9036c102008-10-30 14:19:41 -04003321 cur_offset = hole_start;
3322 while (1) {
3323 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3324 block_end - cur_offset, 0);
3325 BUG_ON(IS_ERR(em) || !em);
3326 last_byte = min(extent_map_end(em), block_end);
3327 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng80825102009-11-12 09:35:36 +00003328 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05003329 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04003330 hole_size = last_byte - cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00003331
Yan, Zhenga22285a2010-05-16 10:48:46 -04003332 trans = btrfs_start_transaction(root, 2);
3333 if (IS_ERR(trans)) {
3334 err = PTR_ERR(trans);
Chris Mason771ed682008-11-06 22:02:51 -05003335 break;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003336 }
Yan, Zheng80825102009-11-12 09:35:36 +00003337 btrfs_set_trans_block_group(trans, inode);
3338
3339 err = btrfs_drop_extents(trans, inode, cur_offset,
3340 cur_offset + hole_size,
3341 &hint_byte, 1);
3342 BUG_ON(err);
Josef Bacik9ed74f22009-09-11 16:12:44 -04003343
Yan Zheng9036c102008-10-30 14:19:41 -04003344 err = btrfs_insert_file_extent(trans, root,
3345 inode->i_ino, cur_offset, 0,
3346 0, hole_size, 0, hole_size,
3347 0, 0, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003348 BUG_ON(err);
3349
Yan Zheng9036c102008-10-30 14:19:41 -04003350 btrfs_drop_extent_cache(inode, hole_start,
3351 last_byte - 1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00003352
3353 btrfs_end_transaction(trans, root);
Yan Zheng9036c102008-10-30 14:19:41 -04003354 }
3355 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003356 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04003357 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00003358 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04003359 break;
3360 }
3361
Yan, Zhenga22285a2010-05-16 10:48:46 -04003362 free_extent_map(em);
Josef Bacik2ac55d42010-02-03 19:33:23 +00003363 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3364 GFP_NOFS);
Yan Zheng9036c102008-10-30 14:19:41 -04003365 return err;
3366}
3367
Yan, Zheng80825102009-11-12 09:35:36 +00003368static int btrfs_setattr_size(struct inode *inode, struct iattr *attr)
3369{
3370 struct btrfs_root *root = BTRFS_I(inode)->root;
3371 struct btrfs_trans_handle *trans;
3372 unsigned long nr;
3373 int ret;
3374
3375 if (attr->ia_size == inode->i_size)
3376 return 0;
3377
3378 if (attr->ia_size > inode->i_size) {
3379 unsigned long limit;
3380 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
3381 if (attr->ia_size > inode->i_sb->s_maxbytes)
3382 return -EFBIG;
3383 if (limit != RLIM_INFINITY && attr->ia_size > limit) {
3384 send_sig(SIGXFSZ, current, 0);
3385 return -EFBIG;
3386 }
3387 }
3388
Yan, Zheng80825102009-11-12 09:35:36 +00003389 trans = btrfs_start_transaction(root, 1);
3390 btrfs_set_trans_block_group(trans, inode);
3391
3392 ret = btrfs_orphan_add(trans, inode);
3393 BUG_ON(ret);
3394
3395 nr = trans->blocks_used;
3396 btrfs_end_transaction(trans, root);
Yan, Zheng80825102009-11-12 09:35:36 +00003397 btrfs_btree_balance_dirty(root, nr);
3398
3399 if (attr->ia_size > inode->i_size) {
3400 ret = btrfs_cont_expand(inode, attr->ia_size);
3401 if (ret) {
3402 btrfs_truncate(inode);
3403 return ret;
3404 }
3405
3406 i_size_write(inode, attr->ia_size);
3407 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
3408
3409 trans = btrfs_start_transaction(root, 1);
3410 btrfs_set_trans_block_group(trans, inode);
3411
3412 ret = btrfs_update_inode(trans, root, inode);
3413 BUG_ON(ret);
3414 if (inode->i_nlink > 0) {
3415 ret = btrfs_orphan_del(trans, inode);
3416 BUG_ON(ret);
3417 }
3418 nr = trans->blocks_used;
3419 btrfs_end_transaction(trans, root);
3420 btrfs_btree_balance_dirty(root, nr);
3421 return 0;
3422 }
3423
3424 /*
3425 * We're truncating a file that used to have good data down to
3426 * zero. Make sure it gets into the ordered flush list so that
3427 * any new writes get down to disk quickly.
3428 */
3429 if (attr->ia_size == 0)
3430 BTRFS_I(inode)->ordered_data_close = 1;
3431
3432 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3433 ret = vmtruncate(inode, attr->ia_size);
3434 BUG_ON(ret);
3435
3436 return 0;
3437}
3438
Chris Mason39279cc2007-06-12 06:35:45 -04003439static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3440{
3441 struct inode *inode = dentry->d_inode;
3442 int err;
3443
3444 err = inode_change_ok(inode, attr);
3445 if (err)
3446 return err;
3447
Chris Mason5a3f23d2009-03-31 13:27:11 -04003448 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Yan, Zheng80825102009-11-12 09:35:36 +00003449 err = btrfs_setattr_size(inode, attr);
3450 if (err)
3451 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003452 }
Yan, Zheng80825102009-11-12 09:35:36 +00003453 attr->ia_valid &= ~ATTR_SIZE;
Yan Zheng9036c102008-10-30 14:19:41 -04003454
Yan, Zheng80825102009-11-12 09:35:36 +00003455 if (attr->ia_valid)
3456 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04003457
3458 if (!err && ((attr->ia_valid & ATTR_MODE)))
3459 err = btrfs_acl_chmod(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003460 return err;
3461}
Chris Mason61295eb2008-01-14 16:24:38 -05003462
Chris Mason39279cc2007-06-12 06:35:45 -04003463void btrfs_delete_inode(struct inode *inode)
3464{
3465 struct btrfs_trans_handle *trans;
3466 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003467 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04003468 int ret;
3469
3470 truncate_inode_pages(&inode->i_data, 0);
3471 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04003472 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003473 goto no_delete;
3474 }
Chris Mason4a096752008-07-21 10:29:44 -04003475 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04003476
Yan, Zhengc71bf092009-11-12 09:34:40 +00003477 if (root->fs_info->log_root_recovering) {
3478 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3479 goto no_delete;
3480 }
3481
Yan, Zheng76dda932009-09-21 16:00:26 -04003482 if (inode->i_nlink > 0) {
3483 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3484 goto no_delete;
3485 }
3486
Chris Masondbe674a2008-07-17 12:54:05 -04003487 btrfs_i_size_write(inode, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003488
Yan, Zheng80825102009-11-12 09:35:36 +00003489 while (1) {
3490 trans = btrfs_start_transaction(root, 1);
3491 btrfs_set_trans_block_group(trans, inode);
3492 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
3493
3494 if (ret != -EAGAIN)
3495 break;
3496
3497 nr = trans->blocks_used;
3498 btrfs_end_transaction(trans, root);
3499 trans = NULL;
3500 btrfs_btree_balance_dirty(root, nr);
Josef Bacik7b128762008-07-24 12:17:14 -04003501 }
3502
Yan, Zheng80825102009-11-12 09:35:36 +00003503 if (ret == 0) {
3504 ret = btrfs_orphan_del(trans, inode);
3505 BUG_ON(ret);
3506 }
Chris Mason85e21ba2008-01-29 15:11:36 -05003507
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003508 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04003509 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003510 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003511no_delete:
3512 clear_inode(inode);
Yan, Zheng80825102009-11-12 09:35:36 +00003513 return;
Chris Mason39279cc2007-06-12 06:35:45 -04003514}
3515
3516/*
3517 * this returns the key found in the dir entry in the location pointer.
3518 * If no dir entries were found, location->objectid is 0.
3519 */
3520static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3521 struct btrfs_key *location)
3522{
3523 const char *name = dentry->d_name.name;
3524 int namelen = dentry->d_name.len;
3525 struct btrfs_dir_item *di;
3526 struct btrfs_path *path;
3527 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04003528 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003529
3530 path = btrfs_alloc_path();
3531 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05003532
Chris Mason39279cc2007-06-12 06:35:45 -04003533 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
3534 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04003535 if (IS_ERR(di))
3536 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05003537
3538 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05003539 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05003540
Chris Mason5f39d392007-10-15 16:14:19 -04003541 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04003542out:
Chris Mason39279cc2007-06-12 06:35:45 -04003543 btrfs_free_path(path);
3544 return ret;
Chris Mason39544012007-12-12 14:38:19 -05003545out_err:
3546 location->objectid = 0;
3547 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003548}
3549
3550/*
3551 * when we hit a tree root in a directory, the btrfs part of the inode
3552 * needs to be changed to reflect the root directory of the tree root. This
3553 * is kind of like crossing a mount point.
3554 */
3555static int fixup_tree_root_location(struct btrfs_root *root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003556 struct inode *dir,
3557 struct dentry *dentry,
3558 struct btrfs_key *location,
3559 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04003560{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003561 struct btrfs_path *path;
3562 struct btrfs_root *new_root;
3563 struct btrfs_root_ref *ref;
3564 struct extent_buffer *leaf;
3565 int ret;
3566 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003567
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003568 path = btrfs_alloc_path();
3569 if (!path) {
3570 err = -ENOMEM;
3571 goto out;
3572 }
Chris Mason39279cc2007-06-12 06:35:45 -04003573
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003574 err = -ENOENT;
3575 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3576 BTRFS_I(dir)->root->root_key.objectid,
3577 location->objectid);
3578 if (ret) {
3579 if (ret < 0)
3580 err = ret;
3581 goto out;
3582 }
Chris Mason39279cc2007-06-12 06:35:45 -04003583
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003584 leaf = path->nodes[0];
3585 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3586 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
3587 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3588 goto out;
3589
3590 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3591 (unsigned long)(ref + 1),
3592 dentry->d_name.len);
3593 if (ret)
3594 goto out;
3595
3596 btrfs_release_path(root->fs_info->tree_root, path);
3597
3598 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3599 if (IS_ERR(new_root)) {
3600 err = PTR_ERR(new_root);
3601 goto out;
3602 }
3603
3604 if (btrfs_root_refs(&new_root->root_item) == 0) {
3605 err = -ENOENT;
3606 goto out;
3607 }
3608
3609 *sub_root = new_root;
3610 location->objectid = btrfs_root_dirid(&new_root->root_item);
3611 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04003612 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003613 err = 0;
3614out:
3615 btrfs_free_path(path);
3616 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04003617}
3618
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003619static void inode_tree_add(struct inode *inode)
3620{
3621 struct btrfs_root *root = BTRFS_I(inode)->root;
3622 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003623 struct rb_node **p;
3624 struct rb_node *parent;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003625again:
3626 p = &root->inode_tree.rb_node;
3627 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003628
Yan, Zheng76dda932009-09-21 16:00:26 -04003629 if (hlist_unhashed(&inode->i_hash))
3630 return;
3631
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003632 spin_lock(&root->inode_lock);
3633 while (*p) {
3634 parent = *p;
3635 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3636
3637 if (inode->i_ino < entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003638 p = &parent->rb_left;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003639 else if (inode->i_ino > entry->vfs_inode.i_ino)
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003640 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003641 else {
3642 WARN_ON(!(entry->vfs_inode.i_state &
3643 (I_WILL_FREE | I_FREEING | I_CLEAR)));
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003644 rb_erase(parent, &root->inode_tree);
3645 RB_CLEAR_NODE(parent);
3646 spin_unlock(&root->inode_lock);
3647 goto again;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003648 }
3649 }
3650 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3651 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3652 spin_unlock(&root->inode_lock);
3653}
3654
3655static void inode_tree_del(struct inode *inode)
3656{
3657 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04003658 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003659
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003660 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003661 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003662 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003663 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04003664 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003665 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02003666 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04003667
3668 if (empty && btrfs_root_refs(&root->root_item) == 0) {
3669 synchronize_srcu(&root->fs_info->subvol_srcu);
3670 spin_lock(&root->inode_lock);
3671 empty = RB_EMPTY_ROOT(&root->inode_tree);
3672 spin_unlock(&root->inode_lock);
3673 if (empty)
3674 btrfs_add_dead_root(root);
3675 }
3676}
3677
3678int btrfs_invalidate_inodes(struct btrfs_root *root)
3679{
3680 struct rb_node *node;
3681 struct rb_node *prev;
3682 struct btrfs_inode *entry;
3683 struct inode *inode;
3684 u64 objectid = 0;
3685
3686 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3687
3688 spin_lock(&root->inode_lock);
3689again:
3690 node = root->inode_tree.rb_node;
3691 prev = NULL;
3692 while (node) {
3693 prev = node;
3694 entry = rb_entry(node, struct btrfs_inode, rb_node);
3695
3696 if (objectid < entry->vfs_inode.i_ino)
3697 node = node->rb_left;
3698 else if (objectid > entry->vfs_inode.i_ino)
3699 node = node->rb_right;
3700 else
3701 break;
3702 }
3703 if (!node) {
3704 while (prev) {
3705 entry = rb_entry(prev, struct btrfs_inode, rb_node);
3706 if (objectid <= entry->vfs_inode.i_ino) {
3707 node = prev;
3708 break;
3709 }
3710 prev = rb_next(prev);
3711 }
3712 }
3713 while (node) {
3714 entry = rb_entry(node, struct btrfs_inode, rb_node);
3715 objectid = entry->vfs_inode.i_ino + 1;
3716 inode = igrab(&entry->vfs_inode);
3717 if (inode) {
3718 spin_unlock(&root->inode_lock);
3719 if (atomic_read(&inode->i_count) > 1)
3720 d_prune_aliases(inode);
3721 /*
3722 * btrfs_drop_inode will remove it from
3723 * the inode cache when its usage count
3724 * hits zero.
3725 */
3726 iput(inode);
3727 cond_resched();
3728 spin_lock(&root->inode_lock);
3729 goto again;
3730 }
3731
3732 if (cond_resched_lock(&root->inode_lock))
3733 goto again;
3734
3735 node = rb_next(node);
3736 }
3737 spin_unlock(&root->inode_lock);
3738 return 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003739}
3740
Chris Masone02119d2008-09-05 16:13:11 -04003741static int btrfs_init_locked_inode(struct inode *inode, void *p)
3742{
3743 struct btrfs_iget_args *args = p;
3744 inode->i_ino = args->ino;
Chris Masone02119d2008-09-05 16:13:11 -04003745 BTRFS_I(inode)->root = args->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003746 btrfs_set_inode_space_info(args->root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003747 return 0;
3748}
3749
3750static int btrfs_find_actor(struct inode *inode, void *opaque)
3751{
3752 struct btrfs_iget_args *args = opaque;
Chris Masond3977122009-01-05 21:25:51 -05003753 return args->ino == inode->i_ino &&
3754 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003755}
3756
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003757static struct inode *btrfs_iget_locked(struct super_block *s,
3758 u64 objectid,
3759 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04003760{
3761 struct inode *inode;
3762 struct btrfs_iget_args args;
3763 args.ino = objectid;
3764 args.root = root;
3765
3766 inode = iget5_locked(s, objectid, btrfs_find_actor,
3767 btrfs_init_locked_inode,
3768 (void *)&args);
3769 return inode;
3770}
3771
Balaji Rao1a54ef82008-07-21 02:01:04 +05303772/* Get an inode object given its location and corresponding root.
3773 * Returns in *is_new if the inode was read from disk
3774 */
3775struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
Josef Bacik73f73412009-12-04 17:38:27 +00003776 struct btrfs_root *root, int *new)
Balaji Rao1a54ef82008-07-21 02:01:04 +05303777{
3778 struct inode *inode;
3779
3780 inode = btrfs_iget_locked(s, location->objectid, root);
3781 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003782 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303783
3784 if (inode->i_state & I_NEW) {
3785 BTRFS_I(inode)->root = root;
3786 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
3787 btrfs_read_locked_inode(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003788
3789 inode_tree_add(inode);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303790 unlock_new_inode(inode);
Josef Bacik73f73412009-12-04 17:38:27 +00003791 if (new)
3792 *new = 1;
Balaji Rao1a54ef82008-07-21 02:01:04 +05303793 }
3794
3795 return inode;
3796}
3797
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003798static struct inode *new_simple_dir(struct super_block *s,
3799 struct btrfs_key *key,
3800 struct btrfs_root *root)
3801{
3802 struct inode *inode = new_inode(s);
3803
3804 if (!inode)
3805 return ERR_PTR(-ENOMEM);
3806
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003807 BTRFS_I(inode)->root = root;
3808 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
3809 BTRFS_I(inode)->dummy_inode = 1;
3810
3811 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
3812 inode->i_op = &simple_dir_inode_operations;
3813 inode->i_fop = &simple_dir_operations;
3814 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
3815 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
3816
3817 return inode;
3818}
3819
Chris Mason3de45862008-11-17 21:02:50 -05003820struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04003821{
Chris Masond3977122009-01-05 21:25:51 -05003822 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003823 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003824 struct btrfs_root *sub_root = root;
3825 struct btrfs_key location;
Yan, Zheng76dda932009-09-21 16:00:26 -04003826 int index;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003827 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003828
Yan, Zheng76dda932009-09-21 16:00:26 -04003829 dentry->d_op = &btrfs_dentry_operations;
3830
Chris Mason39279cc2007-06-12 06:35:45 -04003831 if (dentry->d_name.len > BTRFS_NAME_LEN)
3832 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04003833
Chris Mason39279cc2007-06-12 06:35:45 -04003834 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04003835
Chris Mason39279cc2007-06-12 06:35:45 -04003836 if (ret < 0)
3837 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003838
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003839 if (location.objectid == 0)
3840 return NULL;
3841
3842 if (location.type == BTRFS_INODE_ITEM_KEY) {
Josef Bacik73f73412009-12-04 17:38:27 +00003843 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003844 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003845 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003846
3847 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
3848
Yan, Zheng76dda932009-09-21 16:00:26 -04003849 index = srcu_read_lock(&root->fs_info->subvol_srcu);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003850 ret = fixup_tree_root_location(root, dir, dentry,
3851 &location, &sub_root);
3852 if (ret < 0) {
3853 if (ret != -ENOENT)
3854 inode = ERR_PTR(ret);
3855 else
3856 inode = new_simple_dir(dir->i_sb, &location, sub_root);
3857 } else {
Josef Bacik73f73412009-12-04 17:38:27 +00003858 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003859 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003860 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
3861
Yan, Zhengc71bf092009-11-12 09:34:40 +00003862 if (root != sub_root) {
3863 down_read(&root->fs_info->cleanup_work_sem);
3864 if (!(inode->i_sb->s_flags & MS_RDONLY))
3865 btrfs_orphan_cleanup(sub_root);
3866 up_read(&root->fs_info->cleanup_work_sem);
3867 }
3868
Chris Mason3de45862008-11-17 21:02:50 -05003869 return inode;
3870}
3871
Yan, Zheng76dda932009-09-21 16:00:26 -04003872static int btrfs_dentry_delete(struct dentry *dentry)
3873{
3874 struct btrfs_root *root;
3875
Yan, Zhengefefb142009-10-09 09:25:16 -04003876 if (!dentry->d_inode && !IS_ROOT(dentry))
3877 dentry = dentry->d_parent;
Yan, Zheng76dda932009-09-21 16:00:26 -04003878
Yan, Zhengefefb142009-10-09 09:25:16 -04003879 if (dentry->d_inode) {
3880 root = BTRFS_I(dentry->d_inode)->root;
3881 if (btrfs_root_refs(&root->root_item) == 0)
3882 return 1;
3883 }
Yan, Zheng76dda932009-09-21 16:00:26 -04003884 return 0;
3885}
3886
Chris Mason3de45862008-11-17 21:02:50 -05003887static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
3888 struct nameidata *nd)
3889{
3890 struct inode *inode;
3891
Chris Mason3de45862008-11-17 21:02:50 -05003892 inode = btrfs_lookup_dentry(dir, dentry);
3893 if (IS_ERR(inode))
3894 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003895
Chris Mason39279cc2007-06-12 06:35:45 -04003896 return d_splice_alias(inode, dentry);
3897}
3898
Chris Mason39279cc2007-06-12 06:35:45 -04003899static unsigned char btrfs_filetype_table[] = {
3900 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
3901};
3902
David Woodhousecbdf5a22008-08-06 19:42:33 +01003903static int btrfs_real_readdir(struct file *filp, void *dirent,
3904 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04003905{
Chris Mason6da6aba2007-12-18 16:15:09 -05003906 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003907 struct btrfs_root *root = BTRFS_I(inode)->root;
3908 struct btrfs_item *item;
3909 struct btrfs_dir_item *di;
3910 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003911 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003912 struct btrfs_path *path;
3913 int ret;
3914 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003915 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003916 int slot;
3917 int advance;
3918 unsigned char d_type;
3919 int over = 0;
3920 u32 di_cur;
3921 u32 di_total;
3922 u32 di_len;
3923 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003924 char tmp_name[32];
3925 char *name_ptr;
3926 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04003927
3928 /* FIXME, use a real flag for deciding about the key type */
3929 if (root->fs_info->tree_root == root)
3930 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003931
Chris Mason39544012007-12-12 14:38:19 -05003932 /* special case for "." */
3933 if (filp->f_pos == 0) {
3934 over = filldir(dirent, ".", 1,
3935 1, inode->i_ino,
3936 DT_DIR);
3937 if (over)
3938 return 0;
3939 filp->f_pos = 1;
3940 }
Chris Mason39544012007-12-12 14:38:19 -05003941 /* special case for .., just use the back ref */
3942 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003943 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05003944 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003945 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05003946 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01003947 return 0;
Chris Mason39544012007-12-12 14:38:19 -05003948 filp->f_pos = 2;
3949 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003950 path = btrfs_alloc_path();
3951 path->reada = 2;
3952
Chris Mason39279cc2007-06-12 06:35:45 -04003953 btrfs_set_key_type(&key, key_type);
3954 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01003955 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04003956
Chris Mason39279cc2007-06-12 06:35:45 -04003957 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3958 if (ret < 0)
3959 goto err;
3960 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01003961
3962 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003963 leaf = path->nodes[0];
3964 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003965 slot = path->slots[0];
3966 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01003967 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003968 ret = btrfs_next_leaf(root, path);
3969 if (ret)
3970 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003971 leaf = path->nodes[0];
3972 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003973 slot = path->slots[0];
3974 } else {
3975 slot++;
3976 path->slots[0]++;
3977 }
3978 }
Chris Mason3de45862008-11-17 21:02:50 -05003979
Chris Mason39279cc2007-06-12 06:35:45 -04003980 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003981 item = btrfs_item_nr(leaf, slot);
3982 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3983
3984 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04003985 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003986 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003987 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003988 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04003989 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003990
3991 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01003992
Chris Mason39279cc2007-06-12 06:35:45 -04003993 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
3994 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003995 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01003996
3997 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04003998 struct btrfs_key location;
3999
4000 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01004001 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04004002 name_ptr = tmp_name;
4003 } else {
4004 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01004005 if (!name_ptr) {
4006 ret = -ENOMEM;
4007 goto err;
4008 }
Chris Mason5f39d392007-10-15 16:14:19 -04004009 }
4010 read_extent_buffer(leaf, name_ptr,
4011 (unsigned long)(di + 1), name_len);
4012
4013 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4014 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05004015
4016 /* is this a reference to our own snapshot? If so
4017 * skip it
4018 */
4019 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4020 location.objectid == root->root_key.objectid) {
4021 over = 0;
4022 goto skip;
4023 }
Chris Mason5f39d392007-10-15 16:14:19 -04004024 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01004025 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04004026 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04004027
Chris Mason3de45862008-11-17 21:02:50 -05004028skip:
Chris Mason5f39d392007-10-15 16:14:19 -04004029 if (name_ptr != tmp_name)
4030 kfree(name_ptr);
4031
Chris Mason39279cc2007-06-12 06:35:45 -04004032 if (over)
4033 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05004034 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01004035 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04004036 di_cur += di_len;
4037 di = (struct btrfs_dir_item *)((char *)di + di_len);
4038 }
4039 }
David Woodhouse49593bf2008-08-17 17:08:36 +01004040
4041 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05004042 if (key_type == BTRFS_DIR_INDEX_KEY)
Jan Engelhardt406266a2009-12-09 22:00:38 +00004043 /*
4044 * 32-bit glibc will use getdents64, but then strtol -
4045 * so the last number we can serve is this.
4046 */
4047 filp->f_pos = 0x7fffffff;
Yan Zheng5e591a02008-02-19 11:41:02 -05004048 else
4049 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04004050nopos:
4051 ret = 0;
4052err:
Chris Mason39279cc2007-06-12 06:35:45 -04004053 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04004054 return ret;
4055}
4056
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004057int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
Chris Mason39279cc2007-06-12 06:35:45 -04004058{
4059 struct btrfs_root *root = BTRFS_I(inode)->root;
4060 struct btrfs_trans_handle *trans;
4061 int ret = 0;
4062
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004063 if (BTRFS_I(inode)->dummy_inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04004064 return 0;
4065
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004066 if (wbc->sync_mode == WB_SYNC_ALL) {
Chris Masonf9295742008-07-17 12:54:14 -04004067 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004068 btrfs_set_trans_block_group(trans, inode);
4069 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004070 }
4071 return ret;
4072}
4073
4074/*
Chris Mason54aa1f42007-06-22 14:16:25 -04004075 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04004076 * inode changes. But, it is most likely to find the inode in cache.
4077 * FIXME, needs more benchmarking...there are no reasons other than performance
4078 * to keep or drop this code.
4079 */
4080void btrfs_dirty_inode(struct inode *inode)
4081{
4082 struct btrfs_root *root = BTRFS_I(inode)->root;
4083 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004084 int ret;
4085
4086 if (BTRFS_I(inode)->dummy_inode)
4087 return;
Chris Mason39279cc2007-06-12 06:35:45 -04004088
Chris Masonf9295742008-07-17 12:54:14 -04004089 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04004090 btrfs_set_trans_block_group(trans, inode);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004091
4092 ret = btrfs_update_inode(trans, root, inode);
4093 if (ret)
4094 printk(KERN_ERR"btrfs: fail to dirty inode %lu error %d\n",
4095 inode->i_ino, ret);
4096
Chris Mason39279cc2007-06-12 06:35:45 -04004097 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004098}
4099
Chris Masond352ac62008-09-29 15:18:18 -04004100/*
4101 * find the highest existing sequence number in a directory
4102 * and then set the in-memory index_cnt variable to reflect
4103 * free sequence numbers
4104 */
Josef Bacikaec74772008-07-24 12:12:38 -04004105static int btrfs_set_inode_index_count(struct inode *inode)
4106{
4107 struct btrfs_root *root = BTRFS_I(inode)->root;
4108 struct btrfs_key key, found_key;
4109 struct btrfs_path *path;
4110 struct extent_buffer *leaf;
4111 int ret;
4112
4113 key.objectid = inode->i_ino;
4114 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4115 key.offset = (u64)-1;
4116
4117 path = btrfs_alloc_path();
4118 if (!path)
4119 return -ENOMEM;
4120
4121 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4122 if (ret < 0)
4123 goto out;
4124 /* FIXME: we should be able to handle this */
4125 if (ret == 0)
4126 goto out;
4127 ret = 0;
4128
4129 /*
4130 * MAGIC NUMBER EXPLANATION:
4131 * since we search a directory based on f_pos we have to start at 2
4132 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4133 * else has to start at 2
4134 */
4135 if (path->slots[0] == 0) {
4136 BTRFS_I(inode)->index_cnt = 2;
4137 goto out;
4138 }
4139
4140 path->slots[0]--;
4141
4142 leaf = path->nodes[0];
4143 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4144
4145 if (found_key.objectid != inode->i_ino ||
4146 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4147 BTRFS_I(inode)->index_cnt = 2;
4148 goto out;
4149 }
4150
4151 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4152out:
4153 btrfs_free_path(path);
4154 return ret;
4155}
4156
Chris Masond352ac62008-09-29 15:18:18 -04004157/*
4158 * helper to find a free sequence number in a given directory. This current
4159 * code is very simple, later versions will do smarter things in the btree
4160 */
Chris Mason3de45862008-11-17 21:02:50 -05004161int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04004162{
4163 int ret = 0;
4164
4165 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4166 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05004167 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04004168 return ret;
4169 }
4170
Chris Mason00e4e6b2008-08-05 11:18:09 -04004171 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04004172 BTRFS_I(dir)->index_cnt++;
4173
4174 return ret;
4175}
4176
Chris Mason39279cc2007-06-12 06:35:45 -04004177static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4178 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04004179 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05004180 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05004181 u64 ref_objectid, u64 objectid,
4182 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04004183{
4184 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004185 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04004186 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04004187 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05004188 struct btrfs_inode_ref *ref;
4189 struct btrfs_key key[2];
4190 u32 sizes[2];
4191 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004192 int ret;
4193 int owner;
4194
Chris Mason5f39d392007-10-15 16:14:19 -04004195 path = btrfs_alloc_path();
4196 BUG_ON(!path);
4197
Chris Mason39279cc2007-06-12 06:35:45 -04004198 inode = new_inode(root->fs_info->sb);
4199 if (!inode)
4200 return ERR_PTR(-ENOMEM);
4201
Josef Bacikaec74772008-07-24 12:12:38 -04004202 if (dir) {
Chris Mason3de45862008-11-17 21:02:50 -05004203 ret = btrfs_set_inode_index(dir, index);
Shen Feng09771432009-04-02 16:46:06 -04004204 if (ret) {
4205 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04004206 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04004207 }
Josef Bacikaec74772008-07-24 12:12:38 -04004208 }
4209 /*
4210 * index_cnt is ignored for everything but a dir,
4211 * btrfs_get_inode_index_count has an explanation for the magic
4212 * number
4213 */
4214 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04004215 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04004216 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik6a632092009-02-20 11:00:09 -05004217 btrfs_set_inode_space_info(root, inode);
Chris Masonb888db2b2007-08-27 16:49:44 -04004218
Chris Mason39279cc2007-06-12 06:35:45 -04004219 if (mode & S_IFDIR)
4220 owner = 0;
4221 else
4222 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05004223 BTRFS_I(inode)->block_group =
4224 btrfs_find_block_group(root, 0, alloc_hint, owner);
Chris Mason9c583092008-01-29 15:15:18 -05004225
4226 key[0].objectid = objectid;
4227 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4228 key[0].offset = 0;
4229
4230 key[1].objectid = objectid;
4231 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4232 key[1].offset = ref_objectid;
4233
4234 sizes[0] = sizeof(struct btrfs_inode_item);
4235 sizes[1] = name_len + sizeof(*ref);
4236
Chris Masonb9473432009-03-13 11:00:37 -04004237 path->leave_spinning = 1;
Chris Mason9c583092008-01-29 15:15:18 -05004238 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4239 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04004240 goto fail;
4241
Chris Mason79683f22008-11-19 22:00:53 -05004242 inode->i_uid = current_fsuid();
Chris Ball8c087b52009-02-04 09:29:54 -05004243
Chris Mason42f15d72009-02-06 11:35:57 -05004244 if (dir && (dir->i_mode & S_ISGID)) {
Chris Ball8c087b52009-02-04 09:29:54 -05004245 inode->i_gid = dir->i_gid;
4246 if (S_ISDIR(mode))
4247 mode |= S_ISGID;
4248 } else
4249 inode->i_gid = current_fsgid();
4250
Chris Mason39279cc2007-06-12 06:35:45 -04004251 inode->i_mode = mode;
4252 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004253 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004254 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04004255 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4256 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04004257 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05004258
4259 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4260 struct btrfs_inode_ref);
4261 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004262 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05004263 ptr = (unsigned long)(ref + 1);
4264 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4265
Chris Mason5f39d392007-10-15 16:14:19 -04004266 btrfs_mark_buffer_dirty(path->nodes[0]);
4267 btrfs_free_path(path);
4268
Chris Mason39279cc2007-06-12 06:35:45 -04004269 location = &BTRFS_I(inode)->location;
4270 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04004271 location->offset = 0;
4272 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4273
Christoph Hellwig6cbff002009-04-17 10:37:41 +02004274 btrfs_inherit_iflags(inode, dir);
4275
Chris Mason94272162009-07-02 12:26:06 -04004276 if ((mode & S_IFREG)) {
4277 if (btrfs_test_opt(root, NODATASUM))
4278 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
4279 if (btrfs_test_opt(root, NODATACOW))
4280 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4281 }
4282
Chris Mason39279cc2007-06-12 06:35:45 -04004283 insert_inode_hash(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004284 inode_tree_add(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004285 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04004286fail:
Josef Bacikaec74772008-07-24 12:12:38 -04004287 if (dir)
4288 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04004289 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04004290 iput(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004291 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04004292}
4293
4294static inline u8 btrfs_inode_type(struct inode *inode)
4295{
4296 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4297}
4298
Chris Masond352ac62008-09-29 15:18:18 -04004299/*
4300 * utility function to add 'inode' into 'parent_inode' with
4301 * a give name and a given sequence number.
4302 * if 'add_backref' is true, also insert a backref from the
4303 * inode to the parent directory.
4304 */
Chris Masone02119d2008-09-05 16:13:11 -04004305int btrfs_add_link(struct btrfs_trans_handle *trans,
4306 struct inode *parent_inode, struct inode *inode,
4307 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004308{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004309 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004310 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04004311 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04004312
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004313 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4314 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4315 } else {
4316 key.objectid = inode->i_ino;
4317 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4318 key.offset = 0;
4319 }
Chris Mason39279cc2007-06-12 06:35:45 -04004320
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004321 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4322 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4323 key.objectid, root->root_key.objectid,
4324 parent_inode->i_ino,
4325 index, name, name_len);
4326 } else if (add_backref) {
4327 ret = btrfs_insert_inode_ref(trans, root,
4328 name, name_len, inode->i_ino,
4329 parent_inode->i_ino, index);
4330 }
4331
Chris Mason39279cc2007-06-12 06:35:45 -04004332 if (ret == 0) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004333 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4334 parent_inode->i_ino, &key,
4335 btrfs_inode_type(inode), index);
4336 BUG_ON(ret);
4337
Chris Masondbe674a2008-07-17 12:54:05 -04004338 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04004339 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04004340 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004341 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004342 }
4343 return ret;
4344}
4345
4346static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05004347 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004348 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04004349{
Chris Masone02119d2008-09-05 16:13:11 -04004350 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4351 inode, dentry->d_name.name,
4352 dentry->d_name.len, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004353 if (!err) {
4354 d_instantiate(dentry, inode);
4355 return 0;
4356 }
4357 if (err > 0)
4358 err = -EEXIST;
4359 return err;
4360}
4361
Josef Bacik618e21d2007-07-11 10:18:17 -04004362static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4363 int mode, dev_t rdev)
4364{
4365 struct btrfs_trans_handle *trans;
4366 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004367 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04004368 int err;
4369 int drop_inode = 0;
4370 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05004371 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004372 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04004373
4374 if (!new_valid_dev(rdev))
4375 return -EINVAL;
4376
Yan, Zhenga22285a2010-05-16 10:48:46 -04004377 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4378 if (err)
4379 return err;
4380
Josef Bacik9ed74f22009-09-11 16:12:44 -04004381 /*
4382 * 2 for inode item and ref
4383 * 2 for dir items
4384 * 1 for xattr if selinux is on
4385 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004386 trans = btrfs_start_transaction(root, 5);
4387 if (IS_ERR(trans))
4388 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05004389
Josef Bacik618e21d2007-07-11 10:18:17 -04004390 btrfs_set_trans_block_group(trans, dir);
4391
Josef Bacikaec74772008-07-24 12:12:38 -04004392 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004393 dentry->d_name.len,
4394 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004395 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004396 err = PTR_ERR(inode);
4397 if (IS_ERR(inode))
4398 goto out_unlock;
4399
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004400 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004401 if (err) {
4402 drop_inode = 1;
4403 goto out_unlock;
4404 }
4405
Josef Bacik618e21d2007-07-11 10:18:17 -04004406 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004407 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04004408 if (err)
4409 drop_inode = 1;
4410 else {
4411 inode->i_op = &btrfs_special_inode_operations;
4412 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04004413 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04004414 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004415 btrfs_update_inode_block_group(trans, inode);
4416 btrfs_update_inode_block_group(trans, dir);
4417out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004418 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004419 btrfs_end_transaction_throttle(trans, root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004420 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04004421 if (drop_inode) {
4422 inode_dec_link_count(inode);
4423 iput(inode);
4424 }
Josef Bacik618e21d2007-07-11 10:18:17 -04004425 return err;
4426}
4427
Chris Mason39279cc2007-06-12 06:35:45 -04004428static int btrfs_create(struct inode *dir, struct dentry *dentry,
4429 int mode, struct nameidata *nd)
4430{
4431 struct btrfs_trans_handle *trans;
4432 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05004433 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004434 int drop_inode = 0;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004435 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05004436 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004437 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004438 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004439
Yan, Zhenga22285a2010-05-16 10:48:46 -04004440 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4441 if (err)
4442 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004443 /*
4444 * 2 for inode item and ref
4445 * 2 for dir items
4446 * 1 for xattr if selinux is on
4447 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004448 trans = btrfs_start_transaction(root, 5);
4449 if (IS_ERR(trans))
4450 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004451
Chris Mason39279cc2007-06-12 06:35:45 -04004452 btrfs_set_trans_block_group(trans, dir);
4453
Josef Bacikaec74772008-07-24 12:12:38 -04004454 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004455 dentry->d_name.len,
4456 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004457 objectid, BTRFS_I(dir)->block_group, mode,
4458 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004459 err = PTR_ERR(inode);
4460 if (IS_ERR(inode))
4461 goto out_unlock;
4462
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004463 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004464 if (err) {
4465 drop_inode = 1;
4466 goto out_unlock;
4467 }
4468
Chris Mason39279cc2007-06-12 06:35:45 -04004469 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004470 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004471 if (err)
4472 drop_inode = 1;
4473 else {
4474 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004475 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004476 inode->i_fop = &btrfs_file_operations;
4477 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004478 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004479 }
Chris Mason39279cc2007-06-12 06:35:45 -04004480 btrfs_update_inode_block_group(trans, inode);
4481 btrfs_update_inode_block_group(trans, dir);
4482out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004483 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004484 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004485 if (drop_inode) {
4486 inode_dec_link_count(inode);
4487 iput(inode);
4488 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004489 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004490 return err;
4491}
4492
4493static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4494 struct dentry *dentry)
4495{
4496 struct btrfs_trans_handle *trans;
4497 struct btrfs_root *root = BTRFS_I(dir)->root;
4498 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004499 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05004500 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004501 int err;
4502 int drop_inode = 0;
4503
4504 if (inode->i_nlink == 0)
4505 return -ENOENT;
4506
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00004507 /* do not allow sys_link's with other subvols of the same device */
4508 if (root->objectid != BTRFS_I(inode)->root->objectid)
4509 return -EPERM;
4510
Josef Bacik9ed74f22009-09-11 16:12:44 -04004511 btrfs_inc_nlink(inode);
4512
Chris Mason3de45862008-11-17 21:02:50 -05004513 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004514 if (err)
4515 goto fail;
4516
Yan, Zhenga22285a2010-05-16 10:48:46 -04004517 /*
4518 * 1 item for inode ref
4519 * 2 items for dir items
4520 */
4521 trans = btrfs_start_transaction(root, 3);
4522 if (IS_ERR(trans)) {
4523 err = PTR_ERR(trans);
4524 goto fail;
4525 }
Chris Mason5f39d392007-10-15 16:14:19 -04004526
Chris Mason39279cc2007-06-12 06:35:45 -04004527 btrfs_set_trans_block_group(trans, dir);
4528 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04004529
Chris Mason00e4e6b2008-08-05 11:18:09 -04004530 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04004531
Yan, Zhenga5719522009-09-24 09:17:31 -04004532 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04004533 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04004534 } else {
4535 btrfs_update_inode_block_group(trans, dir);
4536 err = btrfs_update_inode(trans, root, inode);
4537 BUG_ON(err);
4538 btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
4539 }
Chris Mason39279cc2007-06-12 06:35:45 -04004540
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004541 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004542 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004543fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004544 if (drop_inode) {
4545 inode_dec_link_count(inode);
4546 iput(inode);
4547 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004548 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004549 return err;
4550}
4551
Chris Mason39279cc2007-06-12 06:35:45 -04004552static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4553{
Chris Masonb9d86662008-05-02 16:13:49 -04004554 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004555 struct btrfs_trans_handle *trans;
4556 struct btrfs_root *root = BTRFS_I(dir)->root;
4557 int err = 0;
4558 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04004559 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004560 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004561 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004562
Yan, Zhenga22285a2010-05-16 10:48:46 -04004563 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4564 if (err)
4565 return err;
4566
Josef Bacik9ed74f22009-09-11 16:12:44 -04004567 /*
4568 * 2 items for inode and ref
4569 * 2 items for dir items
4570 * 1 for xattr if selinux is on
4571 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004572 trans = btrfs_start_transaction(root, 5);
4573 if (IS_ERR(trans))
4574 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004575 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004576
Josef Bacikaec74772008-07-24 12:12:38 -04004577 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004578 dentry->d_name.len,
4579 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004580 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4581 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004582 if (IS_ERR(inode)) {
4583 err = PTR_ERR(inode);
4584 goto out_fail;
4585 }
Chris Mason5f39d392007-10-15 16:14:19 -04004586
Chris Mason39279cc2007-06-12 06:35:45 -04004587 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04004588
Yan, Zhengf34f57a2009-11-12 09:35:27 +00004589 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004590 if (err)
4591 goto out_fail;
4592
Chris Mason39279cc2007-06-12 06:35:45 -04004593 inode->i_op = &btrfs_dir_inode_operations;
4594 inode->i_fop = &btrfs_dir_file_operations;
4595 btrfs_set_trans_block_group(trans, inode);
4596
Chris Masondbe674a2008-07-17 12:54:05 -04004597 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004598 err = btrfs_update_inode(trans, root, inode);
4599 if (err)
4600 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004601
Chris Masone02119d2008-09-05 16:13:11 -04004602 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4603 inode, dentry->d_name.name,
4604 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004605 if (err)
4606 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04004607
Chris Mason39279cc2007-06-12 06:35:45 -04004608 d_instantiate(dentry, inode);
4609 drop_on_err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004610 btrfs_update_inode_block_group(trans, inode);
4611 btrfs_update_inode_block_group(trans, dir);
4612
4613out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004614 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004615 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004616 if (drop_on_err)
4617 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004618 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004619 return err;
4620}
4621
Chris Masond352ac62008-09-29 15:18:18 -04004622/* helper for btfs_get_extent. Given an existing extent in the tree,
4623 * and an extent that you want to insert, deal with overlap and insert
4624 * the new extent into the tree.
4625 */
Chris Mason3b951512008-04-17 11:29:12 -04004626static int merge_extent_mapping(struct extent_map_tree *em_tree,
4627 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004628 struct extent_map *em,
4629 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04004630{
4631 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04004632
Chris Masone6dcd2d2008-07-17 12:53:50 -04004633 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
4634 start_diff = map_start - em->start;
4635 em->start = map_start;
4636 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04004637 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
4638 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004639 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04004640 em->block_len -= start_diff;
4641 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004642 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004643}
4644
Chris Masonc8b97812008-10-29 14:49:59 -04004645static noinline int uncompress_inline(struct btrfs_path *path,
4646 struct inode *inode, struct page *page,
4647 size_t pg_offset, u64 extent_offset,
4648 struct btrfs_file_extent_item *item)
4649{
4650 int ret;
4651 struct extent_buffer *leaf = path->nodes[0];
4652 char *tmp;
4653 size_t max_size;
4654 unsigned long inline_size;
4655 unsigned long ptr;
4656
4657 WARN_ON(pg_offset != 0);
4658 max_size = btrfs_file_extent_ram_bytes(leaf, item);
4659 inline_size = btrfs_file_extent_inline_item_len(leaf,
4660 btrfs_item_nr(leaf, path->slots[0]));
4661 tmp = kmalloc(inline_size, GFP_NOFS);
4662 ptr = btrfs_file_extent_inline_start(item);
4663
4664 read_extent_buffer(leaf, tmp, ptr, inline_size);
4665
Chris Mason5b050f02008-11-11 09:34:41 -05004666 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004667 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
4668 inline_size, max_size);
4669 if (ret) {
4670 char *kaddr = kmap_atomic(page, KM_USER0);
4671 unsigned long copy_size = min_t(u64,
4672 PAGE_CACHE_SIZE - pg_offset,
4673 max_size - extent_offset);
4674 memset(kaddr + pg_offset, 0, copy_size);
4675 kunmap_atomic(kaddr, KM_USER0);
4676 }
4677 kfree(tmp);
4678 return 0;
4679}
4680
Chris Masond352ac62008-09-29 15:18:18 -04004681/*
4682 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05004683 * the ugly parts come from merging extents from the disk with the in-ram
4684 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04004685 * where the in-ram extents might be locked pending data=ordered completion.
4686 *
4687 * This also copies inline extents directly into the page.
4688 */
Chris Masond3977122009-01-05 21:25:51 -05004689
Chris Masona52d9a82007-08-27 16:49:44 -04004690struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05004691 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04004692 int create)
4693{
4694 int ret;
4695 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04004696 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04004697 u64 extent_start = 0;
4698 u64 extent_end = 0;
4699 u64 objectid = inode->i_ino;
4700 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04004701 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04004702 struct btrfs_root *root = BTRFS_I(inode)->root;
4703 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04004704 struct extent_buffer *leaf;
4705 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04004706 struct extent_map *em = NULL;
4707 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05004708 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004709 struct btrfs_trans_handle *trans = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004710 int compressed;
Chris Masona52d9a82007-08-27 16:49:44 -04004711
Chris Masona52d9a82007-08-27 16:49:44 -04004712again:
Chris Mason890871b2009-09-02 16:24:52 -04004713 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004714 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04004715 if (em)
4716 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason890871b2009-09-02 16:24:52 -04004717 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05004718
Chris Masona52d9a82007-08-27 16:49:44 -04004719 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04004720 if (em->start > start || em->start + em->len <= start)
4721 free_extent_map(em);
4722 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05004723 free_extent_map(em);
4724 else
4725 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004726 }
Chris Masond1310b22008-01-24 16:13:08 -05004727 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004728 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05004729 err = -ENOMEM;
4730 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004731 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004732 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05004733 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05004734 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05004735 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04004736 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04004737
4738 if (!path) {
4739 path = btrfs_alloc_path();
4740 BUG_ON(!path);
4741 }
4742
Chris Mason179e29e2007-11-01 11:28:41 -04004743 ret = btrfs_lookup_file_extent(trans, root, path,
4744 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04004745 if (ret < 0) {
4746 err = ret;
4747 goto out;
4748 }
4749
4750 if (ret != 0) {
4751 if (path->slots[0] == 0)
4752 goto not_found;
4753 path->slots[0]--;
4754 }
4755
Chris Mason5f39d392007-10-15 16:14:19 -04004756 leaf = path->nodes[0];
4757 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04004758 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04004759 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04004760 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4761 found_type = btrfs_key_type(&found_key);
4762 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04004763 found_type != BTRFS_EXTENT_DATA_KEY) {
4764 goto not_found;
4765 }
4766
Chris Mason5f39d392007-10-15 16:14:19 -04004767 found_type = btrfs_file_extent_type(leaf, item);
4768 extent_start = found_key.offset;
Chris Masonc8b97812008-10-29 14:49:59 -04004769 compressed = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04004770 if (found_type == BTRFS_FILE_EXTENT_REG ||
4771 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04004772 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04004773 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04004774 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
4775 size_t size;
4776 size = btrfs_file_extent_inline_len(leaf, item);
4777 extent_end = (extent_start + size + root->sectorsize - 1) &
4778 ~((u64)root->sectorsize - 1);
4779 }
4780
4781 if (start >= extent_end) {
4782 path->slots[0]++;
4783 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
4784 ret = btrfs_next_leaf(root, path);
4785 if (ret < 0) {
4786 err = ret;
4787 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004788 }
Yan Zheng9036c102008-10-30 14:19:41 -04004789 if (ret > 0)
4790 goto not_found;
4791 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04004792 }
Yan Zheng9036c102008-10-30 14:19:41 -04004793 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4794 if (found_key.objectid != objectid ||
4795 found_key.type != BTRFS_EXTENT_DATA_KEY)
4796 goto not_found;
4797 if (start + len <= found_key.offset)
4798 goto not_found;
4799 em->start = start;
4800 em->len = found_key.offset - start;
4801 goto not_found_em;
4802 }
4803
Yan Zhengd899e052008-10-30 14:25:28 -04004804 if (found_type == BTRFS_FILE_EXTENT_REG ||
4805 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04004806 em->start = extent_start;
4807 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004808 em->orig_start = extent_start -
4809 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04004810 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
4811 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004812 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04004813 goto insert;
4814 }
Chris Masonc8b97812008-10-29 14:49:59 -04004815 if (compressed) {
4816 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
4817 em->block_start = bytenr;
4818 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
4819 item);
4820 } else {
4821 bytenr += btrfs_file_extent_offset(leaf, item);
4822 em->block_start = bytenr;
4823 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04004824 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
4825 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04004826 }
Chris Masona52d9a82007-08-27 16:49:44 -04004827 goto insert;
4828 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004829 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04004830 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04004831 size_t size;
4832 size_t extent_offset;
4833 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04004834
Chris Masona52d9a82007-08-27 16:49:44 -04004835 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004836 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04004837 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04004838 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04004839 goto out;
4840 }
4841
Yan Zheng9036c102008-10-30 14:19:41 -04004842 size = btrfs_file_extent_inline_len(leaf, item);
4843 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05004844 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04004845 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04004846 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05004847 em->len = (copy_size + root->sectorsize - 1) &
4848 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004849 em->orig_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004850 if (compressed)
4851 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Yan689f9342007-10-29 11:41:07 -04004852 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04004853 if (create == 0 && !PageUptodate(page)) {
Chris Masonc8b97812008-10-29 14:49:59 -04004854 if (btrfs_file_extent_compression(leaf, item) ==
4855 BTRFS_COMPRESS_ZLIB) {
4856 ret = uncompress_inline(path, inode, page,
4857 pg_offset,
4858 extent_offset, item);
4859 BUG_ON(ret);
4860 } else {
4861 map = kmap(page);
4862 read_extent_buffer(leaf, map + pg_offset, ptr,
4863 copy_size);
Chris Mason93c82d52009-09-11 12:36:29 -04004864 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
4865 memset(map + pg_offset + copy_size, 0,
4866 PAGE_CACHE_SIZE - pg_offset -
4867 copy_size);
4868 }
Chris Masonc8b97812008-10-29 14:49:59 -04004869 kunmap(page);
4870 }
Chris Mason179e29e2007-11-01 11:28:41 -04004871 flush_dcache_page(page);
4872 } else if (create && PageUptodate(page)) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004873 WARN_ON(1);
Chris Mason179e29e2007-11-01 11:28:41 -04004874 if (!trans) {
4875 kunmap(page);
4876 free_extent_map(em);
4877 em = NULL;
4878 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04004879 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004880 goto again;
4881 }
Chris Masonc8b97812008-10-29 14:49:59 -04004882 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05004883 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04004884 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004885 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04004886 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04004887 }
Chris Masond1310b22008-01-24 16:13:08 -05004888 set_extent_uptodate(io_tree, em->start,
4889 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004890 goto insert;
4891 } else {
Chris Masond3977122009-01-05 21:25:51 -05004892 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04004893 WARN_ON(1);
4894 }
4895not_found:
4896 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05004897 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04004898not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04004899 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04004900 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004901insert:
4902 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05004903 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05004904 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
4905 "[%llu %llu]\n", (unsigned long long)em->start,
4906 (unsigned long long)em->len,
4907 (unsigned long long)start,
4908 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04004909 err = -EIO;
4910 goto out;
4911 }
Chris Masond1310b22008-01-24 16:13:08 -05004912
4913 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04004914 write_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004915 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004916 /* it is possible that someone inserted the extent into the tree
4917 * while we had the lock dropped. It is also possible that
4918 * an overlapping map exists in the tree
4919 */
Chris Masona52d9a82007-08-27 16:49:44 -04004920 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04004921 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004922
4923 ret = 0;
4924
Chris Mason3b951512008-04-17 11:29:12 -04004925 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04004926 if (existing && (existing->start > start ||
4927 existing->start + existing->len <= start)) {
4928 free_extent_map(existing);
4929 existing = NULL;
4930 }
Chris Mason3b951512008-04-17 11:29:12 -04004931 if (!existing) {
4932 existing = lookup_extent_mapping(em_tree, em->start,
4933 em->len);
4934 if (existing) {
4935 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004936 em, start,
4937 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04004938 free_extent_map(existing);
4939 if (err) {
4940 free_extent_map(em);
4941 em = NULL;
4942 }
4943 } else {
4944 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04004945 free_extent_map(em);
4946 em = NULL;
4947 }
4948 } else {
4949 free_extent_map(em);
4950 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004951 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04004952 }
Chris Masona52d9a82007-08-27 16:49:44 -04004953 }
Chris Mason890871b2009-09-02 16:24:52 -04004954 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004955out:
Chris Masonf4219502008-07-22 11:18:09 -04004956 if (path)
4957 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04004958 if (trans) {
4959 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05004960 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04004961 err = ret;
4962 }
Chris Masona52d9a82007-08-27 16:49:44 -04004963 if (err) {
4964 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04004965 return ERR_PTR(err);
4966 }
4967 return em;
4968}
4969
Chris Mason16432982008-04-10 10:23:21 -04004970static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
4971 const struct iovec *iov, loff_t offset,
4972 unsigned long nr_segs)
4973{
Chris Masone1c4b742008-04-22 13:26:46 -04004974 return -EINVAL;
Chris Mason16432982008-04-10 10:23:21 -04004975}
4976
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004977static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4978 __u64 start, __u64 len)
4979{
4980 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
4981}
4982
Chris Mason9ebefb182007-06-15 13:50:00 -04004983int btrfs_readpage(struct file *file, struct page *page)
4984{
Chris Masond1310b22008-01-24 16:13:08 -05004985 struct extent_io_tree *tree;
4986 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004987 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04004988}
Chris Mason1832a6d2007-12-21 16:27:21 -05004989
Chris Mason39279cc2007-06-12 06:35:45 -04004990static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
4991{
Chris Masond1310b22008-01-24 16:13:08 -05004992 struct extent_io_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04004993
4994
4995 if (current->flags & PF_MEMALLOC) {
4996 redirty_page_for_writepage(wbc, page);
4997 unlock_page(page);
4998 return 0;
4999 }
Chris Masond1310b22008-01-24 16:13:08 -05005000 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04005001 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
5002}
Chris Mason39279cc2007-06-12 06:35:45 -04005003
Chris Masonf4219502008-07-22 11:18:09 -04005004int btrfs_writepages(struct address_space *mapping,
5005 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04005006{
Chris Masond1310b22008-01-24 16:13:08 -05005007 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05005008
Chris Masond1310b22008-01-24 16:13:08 -05005009 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04005010 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
5011}
5012
Chris Mason3ab2fb52007-11-08 10:59:22 -05005013static int
5014btrfs_readpages(struct file *file, struct address_space *mapping,
5015 struct list_head *pages, unsigned nr_pages)
5016{
Chris Masond1310b22008-01-24 16:13:08 -05005017 struct extent_io_tree *tree;
5018 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05005019 return extent_readpages(tree, mapping, pages, nr_pages,
5020 btrfs_get_extent);
5021}
Chris Masone6dcd2d2008-07-17 12:53:50 -04005022static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04005023{
Chris Masond1310b22008-01-24 16:13:08 -05005024 struct extent_io_tree *tree;
5025 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04005026 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005027
Chris Masond1310b22008-01-24 16:13:08 -05005028 tree = &BTRFS_I(page->mapping->host)->io_tree;
5029 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05005030 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04005031 if (ret == 1) {
5032 ClearPagePrivate(page);
5033 set_page_private(page, 0);
5034 page_cache_release(page);
5035 }
5036 return ret;
5037}
Chris Mason39279cc2007-06-12 06:35:45 -04005038
Chris Masone6dcd2d2008-07-17 12:53:50 -04005039static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
5040{
Chris Mason98509cf2008-09-11 15:51:43 -04005041 if (PageWriteback(page) || PageDirty(page))
5042 return 0;
Yan Zhengb335b002009-02-12 10:06:04 -05005043 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005044}
5045
Chris Masona52d9a82007-08-27 16:49:44 -04005046static void btrfs_invalidatepage(struct page *page, unsigned long offset)
5047{
Chris Masond1310b22008-01-24 16:13:08 -05005048 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005049 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005050 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005051 u64 page_start = page_offset(page);
5052 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04005053
Chris Mason8b62b722009-09-02 16:53:46 -04005054
5055 /*
5056 * we have the page locked, so new writeback can't start,
5057 * and the dirty bit won't be cleared while we are here.
5058 *
5059 * Wait for IO on this page so that we can safely clear
5060 * the PagePrivate2 bit and do ordered accounting
5061 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005062 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04005063
Chris Masond1310b22008-01-24 16:13:08 -05005064 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005065 if (offset) {
5066 btrfs_releasepage(page, GFP_NOFS);
5067 return;
5068 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00005069 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
5070 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005071 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
5072 page_offset(page));
5073 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04005074 /*
5075 * IO on this page will never be started, so we need
5076 * to account for any ordered extents now
5077 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005078 clear_extent_bit(tree, page_start, page_end,
5079 EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik32c00af2009-10-08 13:34:05 -04005080 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00005081 &cached_state, GFP_NOFS);
Chris Mason8b62b722009-09-02 16:53:46 -04005082 /*
5083 * whoever cleared the private bit is responsible
5084 * for the finish_ordered_io
5085 */
5086 if (TestClearPagePrivate2(page)) {
5087 btrfs_finish_ordered_io(page->mapping->host,
5088 page_start, page_end);
5089 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005090 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00005091 cached_state = NULL;
5092 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
5093 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005094 }
5095 clear_extent_bit(tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04005096 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +00005097 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005098 __btrfs_releasepage(page, GFP_NOFS);
5099
Chris Mason4a096752008-07-21 10:29:44 -04005100 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04005101 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04005102 ClearPagePrivate(page);
5103 set_page_private(page, 0);
5104 page_cache_release(page);
5105 }
Chris Mason39279cc2007-06-12 06:35:45 -04005106}
5107
Chris Mason9ebefb182007-06-15 13:50:00 -04005108/*
5109 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
5110 * called from a page fault handler when a page is first dirtied. Hence we must
5111 * be careful to check for EOF conditions here. We set the page up correctly
5112 * for a written page which means we get ENOSPC checking when writing into
5113 * holes and correct delalloc and unwritten extent mapping on filesystems that
5114 * support these features.
5115 *
5116 * We are not allowed to take the i_mutex here so we have to play games to
5117 * protect against truncate races as the page could now be beyond EOF. Because
5118 * vmtruncate() writes the inode size before removing pages, once we have the
5119 * page lock we can determine safely if the page is beyond EOF. If it is not
5120 * beyond EOF, then the page is guaranteed safe against truncation until we
5121 * unlock the page.
5122 */
Nick Pigginc2ec1752009-03-31 15:23:21 -07005123int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04005124{
Nick Pigginc2ec1752009-03-31 15:23:21 -07005125 struct page *page = vmf->page;
Chris Mason6da6aba2007-12-18 16:15:09 -05005126 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05005127 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005128 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5129 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00005130 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005131 char *kaddr;
5132 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04005133 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05005134 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04005135 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04005136 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04005137
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005138 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
Nick Piggin56a76f82009-03-31 15:23:23 -07005139 if (ret) {
5140 if (ret == -ENOMEM)
5141 ret = VM_FAULT_OOM;
5142 else /* -ENOSPC, -EIO, etc */
5143 ret = VM_FAULT_SIGBUS;
Chris Mason1832a6d2007-12-21 16:27:21 -05005144 goto out;
Nick Piggin56a76f82009-03-31 15:23:23 -07005145 }
Chris Mason1832a6d2007-12-21 16:27:21 -05005146
Nick Piggin56a76f82009-03-31 15:23:23 -07005147 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005148again:
Chris Mason9ebefb182007-06-15 13:50:00 -04005149 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04005150 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005151 page_start = page_offset(page);
5152 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04005153
Chris Mason9ebefb182007-06-15 13:50:00 -04005154 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04005155 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04005156 /* page got truncated out from underneath us */
5157 goto out_unlock;
5158 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005159 wait_on_page_writeback(page);
5160
Josef Bacik2ac55d42010-02-03 19:33:23 +00005161 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
5162 GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005163 set_page_extent_mapped(page);
5164
Chris Masoneb84ae02008-07-17 13:53:27 -04005165 /*
5166 * we can't set the delalloc bits if there are pending ordered
5167 * extents. Drop our locks and wait for them to finish
5168 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04005169 ordered = btrfs_lookup_ordered_extent(inode, page_start);
5170 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00005171 unlock_extent_cached(io_tree, page_start, page_end,
5172 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005173 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04005174 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005175 btrfs_put_ordered_extent(ordered);
5176 goto again;
5177 }
5178
Josef Bacikfbf19082009-10-01 17:10:23 -04005179 /*
5180 * XXX - page_mkwrite gets called every time the page is dirtied, even
5181 * if it was already dirty, so for space accounting reasons we need to
5182 * clear any delalloc bits for the range we are fixing to save. There
5183 * is probably a better way to do this, but for now keep consistent with
5184 * prepare_pages in the normal write path.
5185 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00005186 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
Josef Bacik32c00af2009-10-08 13:34:05 -04005187 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00005188 0, 0, &cached_state, GFP_NOFS);
Josef Bacikfbf19082009-10-01 17:10:23 -04005189
Josef Bacik2ac55d42010-02-03 19:33:23 +00005190 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
5191 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005192 if (ret) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00005193 unlock_extent_cached(io_tree, page_start, page_end,
5194 &cached_state, GFP_NOFS);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005195 ret = VM_FAULT_SIGBUS;
5196 goto out_unlock;
5197 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04005198 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04005199
5200 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04005201 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04005202 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04005203 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04005204 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04005205
Chris Masone6dcd2d2008-07-17 12:53:50 -04005206 if (zero_start != PAGE_CACHE_SIZE) {
5207 kaddr = kmap(page);
5208 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
5209 flush_dcache_page(page);
5210 kunmap(page);
5211 }
Chris Mason247e7432008-07-17 12:53:51 -04005212 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005213 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04005214 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005215
Chris Mason257c62e2009-10-13 13:21:08 -04005216 BTRFS_I(inode)->last_trans = root->fs_info->generation;
5217 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
5218
Josef Bacik2ac55d42010-02-03 19:33:23 +00005219 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04005220
5221out_unlock:
Chris Mason50a9b212009-09-11 12:33:12 -04005222 if (!ret)
5223 return VM_FAULT_LOCKED;
Chris Mason9ebefb182007-06-15 13:50:00 -04005224 unlock_page(page);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005225 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
Chris Mason1832a6d2007-12-21 16:27:21 -05005226out:
Chris Mason9ebefb182007-06-15 13:50:00 -04005227 return ret;
5228}
5229
Chris Mason39279cc2007-06-12 06:35:45 -04005230static void btrfs_truncate(struct inode *inode)
5231{
5232 struct btrfs_root *root = BTRFS_I(inode)->root;
5233 int ret;
5234 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005235 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04005236 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005237
Yan, Zheng80825102009-11-12 09:35:36 +00005238 if (!S_ISREG(inode->i_mode)) {
5239 WARN_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -04005240 return;
Yan, Zheng80825102009-11-12 09:35:36 +00005241 }
Chris Mason39279cc2007-06-12 06:35:45 -04005242
Josef Bacik5d5e1032009-10-13 16:46:49 -04005243 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
5244 if (ret)
5245 return;
Yan, Zheng80825102009-11-12 09:35:36 +00005246
Chris Mason4a096752008-07-21 10:29:44 -04005247 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Yan, Zheng80825102009-11-12 09:35:36 +00005248 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005249
Chris Mason39279cc2007-06-12 06:35:45 -04005250 trans = btrfs_start_transaction(root, 1);
Yan, Zheng80825102009-11-12 09:35:36 +00005251 btrfs_set_trans_block_group(trans, inode);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005252
5253 /*
5254 * setattr is responsible for setting the ordered_data_close flag,
5255 * but that is only tested during the last file release. That
5256 * could happen well after the next commit, leaving a great big
5257 * window where new writes may get lost if someone chooses to write
5258 * to this file after truncating to zero
5259 *
5260 * The inode doesn't have any dirty data here, and so if we commit
5261 * this is a noop. If someone immediately starts writing to the inode
5262 * it is very likely we'll catch some of their writes in this
5263 * transaction, and the commit will find this file on the ordered
5264 * data list with good things to send down.
5265 *
5266 * This is a best effort solution, there is still a window where
5267 * using truncate to replace the contents of the file will
5268 * end up with a zero length file after a crash.
5269 */
5270 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
5271 btrfs_add_ordered_operation(trans, root, inode);
5272
Yan, Zheng80825102009-11-12 09:35:36 +00005273 while (1) {
5274 ret = btrfs_truncate_inode_items(trans, root, inode,
5275 inode->i_size,
5276 BTRFS_EXTENT_DATA_KEY);
5277 if (ret != -EAGAIN)
5278 break;
Chris Mason39279cc2007-06-12 06:35:45 -04005279
Yan, Zheng80825102009-11-12 09:35:36 +00005280 ret = btrfs_update_inode(trans, root, inode);
5281 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005282
Yan, Zheng80825102009-11-12 09:35:36 +00005283 nr = trans->blocks_used;
5284 btrfs_end_transaction(trans, root);
5285 btrfs_btree_balance_dirty(root, nr);
5286
5287 trans = btrfs_start_transaction(root, 1);
5288 btrfs_set_trans_block_group(trans, inode);
5289 }
5290
5291 if (ret == 0 && inode->i_nlink > 0) {
5292 ret = btrfs_orphan_del(trans, inode);
5293 BUG_ON(ret);
5294 }
5295
5296 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik7b128762008-07-24 12:17:14 -04005297 BUG_ON(ret);
5298
Josef Bacik7b128762008-07-24 12:17:14 -04005299 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04005300 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005301 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005302 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005303}
5304
Sven Wegener3b963622008-06-09 21:57:42 -04005305/*
Chris Masond352ac62008-09-29 15:18:18 -04005306 * create a new subvolume directory/inode (helper for the ioctl).
5307 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05005308int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Yan, Zheng76dda932009-09-21 16:00:26 -04005309 struct btrfs_root *new_root,
Yan Zhengd2fb3432008-12-11 16:30:39 -05005310 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04005311{
Chris Mason39279cc2007-06-12 06:35:45 -04005312 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04005313 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005314 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005315
Josef Bacikaec74772008-07-24 12:12:38 -04005316 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05005317 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04005318 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04005319 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005320 inode->i_op = &btrfs_dir_inode_operations;
5321 inode->i_fop = &btrfs_dir_file_operations;
5322
Chris Mason39279cc2007-06-12 06:35:45 -04005323 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04005324 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04005325
Yan, Zheng76dda932009-09-21 16:00:26 -04005326 err = btrfs_update_inode(trans, new_root, inode);
5327 BUG_ON(err);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04005328
Yan, Zheng76dda932009-09-21 16:00:26 -04005329 iput(inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04005330 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005331}
5332
Chris Masond352ac62008-09-29 15:18:18 -04005333/* helper function for file defrag and space balancing. This
5334 * forces readahead on a given range of bytes in an inode
5335 */
Chris Masonedbd8d42007-12-21 16:27:24 -05005336unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04005337 struct file_ra_state *ra, struct file *file,
5338 pgoff_t offset, pgoff_t last_index)
5339{
Chris Mason8e7bf942008-04-28 09:02:36 -04005340 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04005341
Chris Mason86479a02007-09-10 19:58:16 -04005342 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
5343 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04005344}
5345
Chris Mason39279cc2007-06-12 06:35:45 -04005346struct inode *btrfs_alloc_inode(struct super_block *sb)
5347{
5348 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04005349 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005350
5351 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
5352 if (!ei)
5353 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04005354
5355 ei->root = NULL;
5356 ei->space_info = NULL;
5357 ei->generation = 0;
5358 ei->sequence = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04005359 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04005360 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04005361 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04005362 ei->delalloc_bytes = 0;
5363 ei->reserved_bytes = 0;
5364 ei->disk_i_size = 0;
5365 ei->flags = 0;
5366 ei->index_cnt = (u64)-1;
5367 ei->last_unlink_trans = 0;
5368
5369 spin_lock_init(&ei->accounting_lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005370 atomic_set(&ei->outstanding_extents, 0);
Josef Bacik32c00af2009-10-08 13:34:05 -04005371 ei->reserved_extents = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04005372
5373 ei->ordered_data_close = 0;
5374 ei->dummy_inode = 0;
5375 ei->force_compress = 0;
5376
5377 inode = &ei->vfs_inode;
5378 extent_map_tree_init(&ei->extent_tree, GFP_NOFS);
5379 extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS);
5380 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS);
5381 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005382 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik7b128762008-07-24 12:17:14 -04005383 INIT_LIST_HEAD(&ei->i_orphan);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04005384 INIT_LIST_HEAD(&ei->delalloc_inodes);
Chris Mason5a3f23d2009-03-31 13:27:11 -04005385 INIT_LIST_HEAD(&ei->ordered_operations);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04005386 RB_CLEAR_NODE(&ei->rb_node);
5387
5388 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005389}
5390
5391void btrfs_destroy_inode(struct inode *inode)
5392{
Chris Masone6dcd2d2008-07-17 12:53:50 -04005393 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005394 struct btrfs_root *root = BTRFS_I(inode)->root;
5395
Chris Mason39279cc2007-06-12 06:35:45 -04005396 WARN_ON(!list_empty(&inode->i_dentry));
5397 WARN_ON(inode->i_data.nrpages);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005398 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents));
5399 WARN_ON(BTRFS_I(inode)->reserved_extents);
Chris Mason39279cc2007-06-12 06:35:45 -04005400
Chris Mason5a3f23d2009-03-31 13:27:11 -04005401 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05005402 * This can happen where we create an inode, but somebody else also
5403 * created the same inode and we need to destroy the one we already
5404 * created.
5405 */
5406 if (!root)
5407 goto free;
5408
5409 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04005410 * Make sure we're properly removed from the ordered operation
5411 * lists.
5412 */
5413 smp_mb();
5414 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
5415 spin_lock(&root->fs_info->ordered_extent_lock);
5416 list_del_init(&BTRFS_I(inode)->ordered_operations);
5417 spin_unlock(&root->fs_info->ordered_extent_lock);
5418 }
5419
5420 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04005421 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yan, Zheng80825102009-11-12 09:35:36 +00005422 printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n",
5423 inode->i_ino);
5424 list_del_init(&BTRFS_I(inode)->i_orphan);
Josef Bacik7b128762008-07-24 12:17:14 -04005425 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04005426 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04005427
Chris Masond3977122009-01-05 21:25:51 -05005428 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04005429 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
5430 if (!ordered)
5431 break;
5432 else {
Chris Masond3977122009-01-05 21:25:51 -05005433 printk(KERN_ERR "btrfs found ordered "
5434 "extent %llu %llu on inode cleanup\n",
5435 (unsigned long long)ordered->file_offset,
5436 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04005437 btrfs_remove_ordered_extent(inode, ordered);
5438 btrfs_put_ordered_extent(ordered);
5439 btrfs_put_ordered_extent(ordered);
5440 }
5441 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005442 inode_tree_del(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005443 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Josef Bacika6dbd422009-11-11 15:53:34 -05005444free:
Chris Mason39279cc2007-06-12 06:35:45 -04005445 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5446}
5447
Yan, Zheng76dda932009-09-21 16:00:26 -04005448void btrfs_drop_inode(struct inode *inode)
5449{
5450 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005451 if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0)
5452 generic_delete_inode(inode);
5453 else
5454 generic_drop_inode(inode);
5455}
5456
Sven Wegener0ee0fda2008-07-30 16:54:26 -04005457static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04005458{
5459 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
5460
5461 inode_init_once(&ei->vfs_inode);
5462}
5463
5464void btrfs_destroy_cachep(void)
5465{
5466 if (btrfs_inode_cachep)
5467 kmem_cache_destroy(btrfs_inode_cachep);
5468 if (btrfs_trans_handle_cachep)
5469 kmem_cache_destroy(btrfs_trans_handle_cachep);
5470 if (btrfs_transaction_cachep)
5471 kmem_cache_destroy(btrfs_transaction_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04005472 if (btrfs_path_cachep)
5473 kmem_cache_destroy(btrfs_path_cachep);
5474}
5475
5476int btrfs_init_cachep(void)
5477{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005478 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
5479 sizeof(struct btrfs_inode), 0,
5480 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04005481 if (!btrfs_inode_cachep)
5482 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005483
5484 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
5485 sizeof(struct btrfs_trans_handle), 0,
5486 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005487 if (!btrfs_trans_handle_cachep)
5488 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005489
5490 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
5491 sizeof(struct btrfs_transaction), 0,
5492 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005493 if (!btrfs_transaction_cachep)
5494 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005495
5496 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
5497 sizeof(struct btrfs_path), 0,
5498 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04005499 if (!btrfs_path_cachep)
5500 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02005501
Chris Mason39279cc2007-06-12 06:35:45 -04005502 return 0;
5503fail:
5504 btrfs_destroy_cachep();
5505 return -ENOMEM;
5506}
5507
5508static int btrfs_getattr(struct vfsmount *mnt,
5509 struct dentry *dentry, struct kstat *stat)
5510{
5511 struct inode *inode = dentry->d_inode;
5512 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05005513 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05005514 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005515 stat->blocks = (inode_get_bytes(inode) +
5516 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04005517 return 0;
5518}
5519
Chris Masond3977122009-01-05 21:25:51 -05005520static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
5521 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005522{
5523 struct btrfs_trans_handle *trans;
5524 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005525 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005526 struct inode *new_inode = new_dentry->d_inode;
5527 struct inode *old_inode = old_dentry->d_inode;
5528 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005529 u64 index = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005530 u64 root_objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04005531 int ret;
5532
Yan, Zhengf679a842009-09-24 09:17:31 -04005533 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5534 return -EPERM;
5535
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005536 /* we only allow rename subvolume link between subvolumes */
5537 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05005538 return -EXDEV;
5539
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005540 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
5541 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
5542 return -ENOTEMPTY;
5543
Chris Mason39279cc2007-06-12 06:35:45 -04005544 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005545 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04005546 return -ENOTEMPTY;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005547 /*
5548 * we're using rename to replace one file with another.
5549 * and the replacement file is large. Start IO on it now so
5550 * we don't add too much work to the end of the transaction
5551 */
Bartlomiej Zolnierkiewicz4baf8c92009-08-07 13:47:08 -04005552 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
Chris Mason5a3f23d2009-03-31 13:27:11 -04005553 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
5554 filemap_flush(old_inode->i_mapping);
5555
Yan, Zheng76dda932009-09-21 16:00:26 -04005556 /* close the racy window with snapshot create/destroy ioctl */
5557 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5558 down_read(&root->fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04005559 /*
5560 * We want to reserve the absolute worst case amount of items. So if
5561 * both inodes are subvols and we need to unlink them then that would
5562 * require 4 item modifications, but if they are both normal inodes it
5563 * would require 5 item modifications, so we'll assume their normal
5564 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
5565 * should cover the worst case number of items we'll modify.
5566 */
5567 trans = btrfs_start_transaction(root, 20);
5568 if (IS_ERR(trans))
5569 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04005570
Yan, Zhenga5719522009-09-24 09:17:31 -04005571 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04005572
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005573 if (dest != root)
5574 btrfs_record_root_in_trans(trans, dest);
5575
Yan, Zhenga5719522009-09-24 09:17:31 -04005576 ret = btrfs_set_inode_index(new_dir, &index);
5577 if (ret)
5578 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04005579
Yan, Zhenga5719522009-09-24 09:17:31 -04005580 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005581 /* force full log commit if subvolume involved. */
5582 root->fs_info->last_trans_log_full_commit = trans->transid;
5583 } else {
Yan, Zhenga5719522009-09-24 09:17:31 -04005584 ret = btrfs_insert_inode_ref(trans, dest,
5585 new_dentry->d_name.name,
5586 new_dentry->d_name.len,
5587 old_inode->i_ino,
5588 new_dir->i_ino, index);
5589 if (ret)
5590 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005591 /*
5592 * this is an ugly little race, but the rename is required
5593 * to make sure that if we crash, the inode is either at the
5594 * old name or the new one. pinning the log transaction lets
5595 * us make sure we don't allow a log commit to come in after
5596 * we unlink the name but before we add the new name back in.
5597 */
5598 btrfs_pin_log_trans(root);
5599 }
Chris Mason12fcfd22009-03-24 10:24:20 -04005600 /*
Chris Mason5a3f23d2009-03-31 13:27:11 -04005601 * make sure the inode gets flushed if it is replacing
5602 * something.
5603 */
5604 if (new_inode && new_inode->i_size &&
5605 old_inode && S_ISREG(old_inode->i_mode)) {
5606 btrfs_add_ordered_operation(trans, root, old_inode);
5607 }
5608
Chris Mason39279cc2007-06-12 06:35:45 -04005609 old_dir->i_ctime = old_dir->i_mtime = ctime;
5610 new_dir->i_ctime = new_dir->i_mtime = ctime;
5611 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04005612
Chris Mason12fcfd22009-03-24 10:24:20 -04005613 if (old_dentry->d_parent != new_dentry->d_parent)
5614 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
5615
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005616 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
5617 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
5618 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
5619 old_dentry->d_name.name,
5620 old_dentry->d_name.len);
5621 } else {
5622 btrfs_inc_nlink(old_dentry->d_inode);
5623 ret = btrfs_unlink_inode(trans, root, old_dir,
5624 old_dentry->d_inode,
5625 old_dentry->d_name.name,
5626 old_dentry->d_name.len);
5627 }
5628 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005629
5630 if (new_inode) {
5631 new_inode->i_ctime = CURRENT_TIME;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005632 if (unlikely(new_inode->i_ino ==
5633 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
5634 root_objectid = BTRFS_I(new_inode)->location.objectid;
5635 ret = btrfs_unlink_subvol(trans, dest, new_dir,
5636 root_objectid,
5637 new_dentry->d_name.name,
5638 new_dentry->d_name.len);
5639 BUG_ON(new_inode->i_nlink == 0);
5640 } else {
5641 ret = btrfs_unlink_inode(trans, dest, new_dir,
5642 new_dentry->d_inode,
5643 new_dentry->d_name.name,
5644 new_dentry->d_name.len);
5645 }
5646 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04005647 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04005648 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005649 BUG_ON(ret);
Josef Bacik7b128762008-07-24 12:17:14 -04005650 }
Chris Mason39279cc2007-06-12 06:35:45 -04005651 }
Josef Bacikaec74772008-07-24 12:12:38 -04005652
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005653 ret = btrfs_add_link(trans, new_dir, old_inode,
5654 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04005655 new_dentry->d_name.len, 0, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005656 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005657
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005658 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
5659 btrfs_log_new_name(trans, old_inode, old_dir,
5660 new_dentry->d_parent);
5661 btrfs_end_log_trans(root);
5662 }
Chris Mason39279cc2007-06-12 06:35:45 -04005663out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04005664 btrfs_end_transaction_throttle(trans, root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005665
Yan, Zheng76dda932009-09-21 16:00:26 -04005666 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5667 up_read(&root->fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04005668
Chris Mason39279cc2007-06-12 06:35:45 -04005669 return ret;
5670}
5671
Chris Masond352ac62008-09-29 15:18:18 -04005672/*
5673 * some fairly slow code that needs optimization. This walks the list
5674 * of all the inodes with pending delalloc and forces them to disk.
5675 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00005676int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
Chris Masonea8c2812008-08-04 23:17:27 -04005677{
5678 struct list_head *head = &root->fs_info->delalloc_inodes;
5679 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005680 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04005681
Yan Zhengc146afa2008-11-12 14:34:12 -05005682 if (root->fs_info->sb->s_flags & MS_RDONLY)
5683 return -EROFS;
5684
Chris Mason75eff682008-12-15 15:54:40 -05005685 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05005686 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04005687 binode = list_entry(head->next, struct btrfs_inode,
5688 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005689 inode = igrab(&binode->vfs_inode);
5690 if (!inode)
5691 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05005692 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005693 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04005694 filemap_flush(inode->i_mapping);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00005695 if (delay_iput)
5696 btrfs_add_delayed_iput(inode);
5697 else
5698 iput(inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04005699 }
5700 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05005701 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04005702 }
Chris Mason75eff682008-12-15 15:54:40 -05005703 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04005704
5705 /* the filemap_flush will queue IO into the worker threads, but
5706 * we have to make sure the IO is actually started and that
5707 * ordered extents get created before we return
5708 */
5709 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05005710 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05005711 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04005712 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05005713 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
5714 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04005715 }
5716 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04005717 return 0;
5718}
5719
Yan, Zheng5da9d012010-05-16 10:46:25 -04005720int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput)
5721{
5722 struct btrfs_inode *binode;
5723 struct inode *inode = NULL;
5724
5725 spin_lock(&root->fs_info->delalloc_lock);
5726 while (!list_empty(&root->fs_info->delalloc_inodes)) {
5727 binode = list_entry(root->fs_info->delalloc_inodes.next,
5728 struct btrfs_inode, delalloc_inodes);
5729 inode = igrab(&binode->vfs_inode);
5730 if (inode) {
5731 list_move_tail(&binode->delalloc_inodes,
5732 &root->fs_info->delalloc_inodes);
5733 break;
5734 }
5735
5736 list_del_init(&binode->delalloc_inodes);
5737 cond_resched_lock(&root->fs_info->delalloc_lock);
5738 }
5739 spin_unlock(&root->fs_info->delalloc_lock);
5740
5741 if (inode) {
5742 write_inode_now(inode, 0);
5743 if (delay_iput)
5744 btrfs_add_delayed_iput(inode);
5745 else
5746 iput(inode);
5747 return 1;
5748 }
5749 return 0;
5750}
5751
Chris Mason39279cc2007-06-12 06:35:45 -04005752static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
5753 const char *symname)
5754{
5755 struct btrfs_trans_handle *trans;
5756 struct btrfs_root *root = BTRFS_I(dir)->root;
5757 struct btrfs_path *path;
5758 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05005759 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04005760 int err;
5761 int drop_inode = 0;
5762 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04005763 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04005764 int name_len;
5765 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04005766 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04005767 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04005768 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05005769 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005770
5771 name_len = strlen(symname) + 1;
5772 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
5773 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05005774
Yan, Zhenga22285a2010-05-16 10:48:46 -04005775 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
5776 if (err)
5777 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -04005778 /*
5779 * 2 items for inode item and ref
5780 * 2 items for dir items
5781 * 1 item for xattr if selinux is on
5782 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04005783 trans = btrfs_start_transaction(root, 5);
5784 if (IS_ERR(trans))
5785 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05005786
Chris Mason39279cc2007-06-12 06:35:45 -04005787 btrfs_set_trans_block_group(trans, dir);
5788
Josef Bacikaec74772008-07-24 12:12:38 -04005789 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05005790 dentry->d_name.len,
5791 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04005792 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
5793 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04005794 err = PTR_ERR(inode);
5795 if (IS_ERR(inode))
5796 goto out_unlock;
5797
Yan, Zhengf34f57a2009-11-12 09:35:27 +00005798 err = btrfs_init_inode_security(trans, inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04005799 if (err) {
5800 drop_inode = 1;
5801 goto out_unlock;
5802 }
5803
Chris Mason39279cc2007-06-12 06:35:45 -04005804 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04005805 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04005806 if (err)
5807 drop_inode = 1;
5808 else {
5809 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04005810 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04005811 inode->i_fop = &btrfs_file_operations;
5812 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05005813 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04005814 }
Chris Mason39279cc2007-06-12 06:35:45 -04005815 btrfs_update_inode_block_group(trans, inode);
5816 btrfs_update_inode_block_group(trans, dir);
5817 if (drop_inode)
5818 goto out_unlock;
5819
5820 path = btrfs_alloc_path();
5821 BUG_ON(!path);
5822 key.objectid = inode->i_ino;
5823 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005824 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
5825 datasize = btrfs_file_extent_calc_inline_size(name_len);
5826 err = btrfs_insert_empty_item(trans, root, path, &key,
5827 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04005828 if (err) {
5829 drop_inode = 1;
5830 goto out_unlock;
5831 }
Chris Mason5f39d392007-10-15 16:14:19 -04005832 leaf = path->nodes[0];
5833 ei = btrfs_item_ptr(leaf, path->slots[0],
5834 struct btrfs_file_extent_item);
5835 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
5836 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04005837 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04005838 btrfs_set_file_extent_encryption(leaf, ei, 0);
5839 btrfs_set_file_extent_compression(leaf, ei, 0);
5840 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
5841 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
5842
Chris Mason39279cc2007-06-12 06:35:45 -04005843 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04005844 write_extent_buffer(leaf, symname, ptr, name_len);
5845 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04005846 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04005847
Chris Mason39279cc2007-06-12 06:35:45 -04005848 inode->i_op = &btrfs_symlink_inode_operations;
5849 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04005850 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04005851 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04005852 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04005853 err = btrfs_update_inode(trans, root, inode);
5854 if (err)
5855 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04005856
5857out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005858 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04005859 btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04005860 if (drop_inode) {
5861 inode_dec_link_count(inode);
5862 iput(inode);
5863 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04005864 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04005865 return err;
5866}
Chris Mason16432982008-04-10 10:23:21 -04005867
Yan, Zheng5a303d52009-11-12 09:34:52 +00005868static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00005869 u64 alloc_hint, int mode, loff_t actual_len)
Yan Zhengd899e052008-10-30 14:25:28 -04005870{
Yan, Zheng5a303d52009-11-12 09:34:52 +00005871 struct btrfs_trans_handle *trans;
Yan Zhengd899e052008-10-30 14:25:28 -04005872 struct btrfs_root *root = BTRFS_I(inode)->root;
5873 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04005874 u64 cur_offset = start;
5875 u64 num_bytes = end - start;
5876 int ret = 0;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00005877 u64 i_size;
Yan Zhengd899e052008-10-30 14:25:28 -04005878
Yan Zhengd899e052008-10-30 14:25:28 -04005879 while (num_bytes > 0) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04005880 trans = btrfs_start_transaction(root, 3);
5881 if (IS_ERR(trans)) {
5882 ret = PTR_ERR(trans);
5883 break;
5884 }
Chris Mason3a1abec2009-12-17 15:47:17 -05005885
Josef Bacik287a0ab2010-03-19 18:07:23 +00005886 ret = btrfs_reserve_extent(trans, root, num_bytes,
Yan Zhengd899e052008-10-30 14:25:28 -04005887 root->sectorsize, 0, alloc_hint,
5888 (u64)-1, &ins, 1);
5889 if (ret) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04005890 btrfs_end_transaction(trans, root);
5891 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00005892 }
5893
Yan Zhengd899e052008-10-30 14:25:28 -04005894 ret = insert_reserved_file_extent(trans, inode,
5895 cur_offset, ins.objectid,
5896 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00005897 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04005898 BTRFS_FILE_EXTENT_PREALLOC);
5899 BUG_ON(ret);
Chris Masona1ed8352009-09-11 12:27:37 -04005900 btrfs_drop_extent_cache(inode, cur_offset,
5901 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00005902
Yan Zhengd899e052008-10-30 14:25:28 -04005903 num_bytes -= ins.offset;
5904 cur_offset += ins.offset;
5905 alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00005906
Yan Zhengd899e052008-10-30 14:25:28 -04005907 inode->i_ctime = CURRENT_TIME;
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005908 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04005909 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Aneesh Kumar K.V23b5c502010-02-04 11:33:03 -05005910 (actual_len > inode->i_size) &&
5911 (cur_offset > inode->i_size)) {
5912
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00005913 if (cur_offset > actual_len)
5914 i_size = actual_len;
5915 else
5916 i_size = cur_offset;
5917 i_size_write(inode, i_size);
5918 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +00005919 }
5920
Yan Zhengd899e052008-10-30 14:25:28 -04005921 ret = btrfs_update_inode(trans, root, inode);
5922 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -04005923
Yan, Zheng5a303d52009-11-12 09:34:52 +00005924 btrfs_end_transaction(trans, root);
Yan, Zheng5a303d52009-11-12 09:34:52 +00005925 }
Yan Zhengd899e052008-10-30 14:25:28 -04005926 return ret;
5927}
5928
5929static long btrfs_fallocate(struct inode *inode, int mode,
5930 loff_t offset, loff_t len)
5931{
Josef Bacik2ac55d42010-02-03 19:33:23 +00005932 struct extent_state *cached_state = NULL;
Yan Zhengd899e052008-10-30 14:25:28 -04005933 u64 cur_offset;
5934 u64 last_byte;
5935 u64 alloc_start;
5936 u64 alloc_end;
5937 u64 alloc_hint = 0;
Chris Masone980b502009-04-24 14:39:24 -04005938 u64 locked_end;
Yan Zhengd899e052008-10-30 14:25:28 -04005939 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
5940 struct extent_map *em;
5941 int ret;
5942
5943 alloc_start = offset & ~mask;
5944 alloc_end = (offset + len + mask) & ~mask;
5945
Chris Mason546888d2009-04-21 11:53:38 -04005946 /*
5947 * wait for ordered IO before we have any locks. We'll loop again
5948 * below with the locks held.
5949 */
5950 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
5951
Yan Zhengd899e052008-10-30 14:25:28 -04005952 mutex_lock(&inode->i_mutex);
5953 if (alloc_start > inode->i_size) {
5954 ret = btrfs_cont_expand(inode, alloc_start);
5955 if (ret)
5956 goto out;
5957 }
5958
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005959 ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
Josef Bacika970b0a2009-06-27 21:07:34 -04005960 if (ret)
5961 goto out;
5962
Chris Masone980b502009-04-24 14:39:24 -04005963 locked_end = alloc_end - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04005964 while (1) {
5965 struct btrfs_ordered_extent *ordered;
Chris Mason546888d2009-04-21 11:53:38 -04005966
Chris Mason546888d2009-04-21 11:53:38 -04005967 /* the extent lock is ordered inside the running
5968 * transaction
5969 */
Josef Bacik2ac55d42010-02-03 19:33:23 +00005970 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
5971 locked_end, 0, &cached_state, GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04005972 ordered = btrfs_lookup_first_ordered_extent(inode,
5973 alloc_end - 1);
5974 if (ordered &&
5975 ordered->file_offset + ordered->len > alloc_start &&
5976 ordered->file_offset < alloc_end) {
5977 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00005978 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
5979 alloc_start, locked_end,
5980 &cached_state, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04005981 /*
5982 * we can't wait on the range with the transaction
5983 * running or with the extent lock held
5984 */
Yan Zhengd899e052008-10-30 14:25:28 -04005985 btrfs_wait_ordered_range(inode, alloc_start,
5986 alloc_end - alloc_start);
5987 } else {
5988 if (ordered)
5989 btrfs_put_ordered_extent(ordered);
5990 break;
5991 }
5992 }
5993
5994 cur_offset = alloc_start;
5995 while (1) {
5996 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
5997 alloc_end - cur_offset, 0);
5998 BUG_ON(IS_ERR(em) || !em);
5999 last_byte = min(extent_map_end(em), alloc_end);
6000 last_byte = (last_byte + mask) & ~mask;
Yan, Zheng5a303d52009-11-12 09:34:52 +00006001 if (em->block_start == EXTENT_MAP_HOLE ||
6002 (cur_offset >= inode->i_size &&
6003 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6004 ret = prealloc_file_range(inode,
6005 cur_offset, last_byte,
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00006006 alloc_hint, mode, offset+len);
Yan Zhengd899e052008-10-30 14:25:28 -04006007 if (ret < 0) {
6008 free_extent_map(em);
6009 break;
6010 }
6011 }
6012 if (em->block_start <= EXTENT_MAP_LAST_BYTE)
6013 alloc_hint = em->block_start;
6014 free_extent_map(em);
6015
6016 cur_offset = last_byte;
6017 if (cur_offset >= alloc_end) {
6018 ret = 0;
6019 break;
6020 }
6021 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00006022 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
6023 &cached_state, GFP_NOFS);
Chris Mason546888d2009-04-21 11:53:38 -04006024
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04006025 btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
Yan Zhengd899e052008-10-30 14:25:28 -04006026out:
6027 mutex_unlock(&inode->i_mutex);
6028 return ret;
6029}
6030
Chris Masone6dcd2d2008-07-17 12:53:50 -04006031static int btrfs_set_page_dirty(struct page *page)
6032{
Chris Masone6dcd2d2008-07-17 12:53:50 -04006033 return __set_page_dirty_nobuffers(page);
6034}
6035
Sven Wegener0ee0fda2008-07-30 16:54:26 -04006036static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05006037{
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006038 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
Yanfdebe2b2008-01-14 13:26:08 -05006039 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04006040 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05006041}
Chris Mason39279cc2007-06-12 06:35:45 -04006042
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006043static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05006044 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04006045 .lookup = btrfs_lookup,
6046 .create = btrfs_create,
6047 .unlink = btrfs_unlink,
6048 .link = btrfs_link,
6049 .mkdir = btrfs_mkdir,
6050 .rmdir = btrfs_rmdir,
6051 .rename = btrfs_rename,
6052 .symlink = btrfs_symlink,
6053 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04006054 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006055 .setxattr = btrfs_setxattr,
6056 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05006057 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006058 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05006059 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04006060};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006061static const struct inode_operations btrfs_dir_ro_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04006062 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05006063 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04006064};
Yan, Zheng76dda932009-09-21 16:00:26 -04006065
Alexey Dobriyan828c0952009-10-01 15:43:56 -07006066static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04006067 .llseek = generic_file_llseek,
6068 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01006069 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04006070 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04006071#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04006072 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04006073#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04006074 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04006075 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04006076};
6077
Chris Masond1310b22008-01-24 16:13:08 -05006078static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04006079 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05006080 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04006081 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04006082 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006083 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04006084 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04006085 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05006086 .set_bit_hook = btrfs_set_bit_hook,
6087 .clear_bit_hook = btrfs_clear_bit_hook,
Josef Bacik9ed74f22009-09-11 16:12:44 -04006088 .merge_extent_hook = btrfs_merge_extent_hook,
6089 .split_extent_hook = btrfs_split_extent_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04006090};
6091
Chris Mason35054392009-01-21 13:11:13 -05006092/*
6093 * btrfs doesn't support the bmap operation because swapfiles
6094 * use bmap to make a mapping of extents in the file. They assume
6095 * these extents won't change over the life of the file and they
6096 * use the bmap result to do IO directly to the drive.
6097 *
6098 * the btrfs bmap call would return logical addresses that aren't
6099 * suitable for IO and they also will change frequently as COW
6100 * operations happen. So, swapfile + btrfs == corruption.
6101 *
6102 * For now we're avoiding this by dropping bmap.
6103 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -07006104static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04006105 .readpage = btrfs_readpage,
6106 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04006107 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05006108 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04006109 .sync_page = block_sync_page,
Chris Mason16432982008-04-10 10:23:21 -04006110 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04006111 .invalidatepage = btrfs_invalidatepage,
6112 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04006113 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +02006114 .error_remove_page = generic_error_remove_page,
Chris Mason39279cc2007-06-12 06:35:45 -04006115};
6116
Alexey Dobriyan7f094102009-09-21 17:01:10 -07006117static const struct address_space_operations btrfs_symlink_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -04006118 .readpage = btrfs_readpage,
6119 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04006120 .invalidatepage = btrfs_invalidatepage,
6121 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04006122};
6123
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006124static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04006125 .truncate = btrfs_truncate,
6126 .getattr = btrfs_getattr,
6127 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006128 .setxattr = btrfs_setxattr,
6129 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05006130 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006131 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05006132 .permission = btrfs_permission,
Yan Zhengd899e052008-10-30 14:25:28 -04006133 .fallocate = btrfs_fallocate,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05006134 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04006135};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006136static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -04006137 .getattr = btrfs_getattr,
6138 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05006139 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006140 .setxattr = btrfs_setxattr,
6141 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04006142 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04006143 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04006144};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07006145static const struct inode_operations btrfs_symlink_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04006146 .readlink = generic_readlink,
6147 .follow_link = page_follow_link_light,
6148 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05006149 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05006150 .setxattr = btrfs_setxattr,
6151 .getxattr = btrfs_getxattr,
6152 .listxattr = btrfs_listxattr,
6153 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04006154};
Yan, Zheng76dda932009-09-21 16:00:26 -04006155
Alexey Dobriyan82d339d2009-10-09 09:54:36 -04006156const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -04006157 .d_delete = btrfs_dentry_delete,
6158};