blob: 8f0706210a478cc8866b3dd280b2860e75089757 [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>
29#include <linux/smp_lock.h>
30#include <linux/backing-dev.h>
31#include <linux/mpage.h>
32#include <linux/swap.h>
33#include <linux/writeback.h>
34#include <linux/statfs.h>
35#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040036#include <linux/bit_spinlock.h>
Josef Bacik5103e942007-11-16 11:45:54 -050037#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040038#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040039#include <linux/falloc.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"
Yan Zheng5b84e8d2008-10-09 11:46:19 -040051#include "ref-cache.h"
Chris Masonc8b97812008-10-29 14:49:59 -040052#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050053#include "locking.h"
Chris Mason39279cc2007-06-12 06:35:45 -040054
55struct btrfs_iget_args {
56 u64 ino;
57 struct btrfs_root *root;
58};
59
60static struct inode_operations btrfs_dir_inode_operations;
61static struct inode_operations btrfs_symlink_inode_operations;
62static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040063static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040064static struct inode_operations btrfs_file_inode_operations;
65static struct address_space_operations btrfs_aops;
66static struct address_space_operations btrfs_symlink_aops;
67static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050068static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040069
70static struct kmem_cache *btrfs_inode_cachep;
71struct kmem_cache *btrfs_trans_handle_cachep;
72struct kmem_cache *btrfs_transaction_cachep;
73struct kmem_cache *btrfs_bit_radix_cachep;
74struct kmem_cache *btrfs_path_cachep;
75
76#define S_SHIFT 12
77static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
78 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
79 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
80 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
81 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
82 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
83 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
84 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
85};
86
Josef Bacik7b128762008-07-24 12:17:14 -040087static void btrfs_truncate(struct inode *inode);
Chris Masonc8b97812008-10-29 14:49:59 -040088static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
Chris Mason771ed682008-11-06 22:02:51 -050089static noinline int cow_file_range(struct inode *inode,
90 struct page *locked_page,
91 u64 start, u64 end, int *page_started,
92 unsigned long *nr_written, int unlock);
Josef Bacik7b128762008-07-24 12:17:14 -040093
Jim Owens0279b4c2009-02-04 09:29:13 -050094static int btrfs_init_inode_security(struct inode *inode, struct inode *dir)
95{
96 int err;
97
98 err = btrfs_init_acl(inode, dir);
99 if (!err)
100 err = btrfs_xattr_security_init(inode, dir);
101 return err;
102}
103
Chris Masond352ac62008-09-29 15:18:18 -0400104/*
105 * a very lame attempt at stopping writes when the FS is 85% full. There
106 * are countless ways this is incorrect, but it is better than nothing.
107 */
Chris Mason1832a6d2007-12-21 16:27:21 -0500108int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
109 int for_del)
110{
Chris Masona2135012008-06-25 16:01:30 -0400111 u64 total;
112 u64 used;
Chris Mason1832a6d2007-12-21 16:27:21 -0500113 u64 thresh;
114 int ret = 0;
115
Chris Mason75eff682008-12-15 15:54:40 -0500116 spin_lock(&root->fs_info->delalloc_lock);
Chris Masona2135012008-06-25 16:01:30 -0400117 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
118 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
Chris Mason1832a6d2007-12-21 16:27:21 -0500119 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -0500120 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -0500121 else
Chris Masonf9ef6602008-01-03 09:22:38 -0500122 thresh = total * 85;
123
124 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -0500125
Chris Mason1832a6d2007-12-21 16:27:21 -0500126 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
127 ret = -ENOSPC;
Chris Mason75eff682008-12-15 15:54:40 -0500128 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason1832a6d2007-12-21 16:27:21 -0500129 return ret;
130}
131
Chris Masond352ac62008-09-29 15:18:18 -0400132/*
Chris Masonc8b97812008-10-29 14:49:59 -0400133 * this does all the hard work for inserting an inline extent into
134 * the btree. The caller should have done a btrfs_drop_extents so that
135 * no overlapping inline items exist in the btree
136 */
Chris Masond3977122009-01-05 21:25:51 -0500137static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masonc8b97812008-10-29 14:49:59 -0400138 struct btrfs_root *root, struct inode *inode,
139 u64 start, size_t size, size_t compressed_size,
140 struct page **compressed_pages)
141{
142 struct btrfs_key key;
143 struct btrfs_path *path;
144 struct extent_buffer *leaf;
145 struct page *page = NULL;
146 char *kaddr;
147 unsigned long ptr;
148 struct btrfs_file_extent_item *ei;
149 int err = 0;
150 int ret;
151 size_t cur_size = size;
152 size_t datasize;
153 unsigned long offset;
154 int use_compress = 0;
155
156 if (compressed_size && compressed_pages) {
157 use_compress = 1;
158 cur_size = compressed_size;
159 }
160
Chris Masond3977122009-01-05 21:25:51 -0500161 path = btrfs_alloc_path();
162 if (!path)
Chris Masonc8b97812008-10-29 14:49:59 -0400163 return -ENOMEM;
164
165 btrfs_set_trans_block_group(trans, inode);
166
167 key.objectid = inode->i_ino;
168 key.offset = start;
169 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Masonc8b97812008-10-29 14:49:59 -0400170 datasize = btrfs_file_extent_calc_inline_size(cur_size);
171
172 inode_add_bytes(inode, size);
173 ret = btrfs_insert_empty_item(trans, root, path, &key,
174 datasize);
175 BUG_ON(ret);
176 if (ret) {
177 err = ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400178 goto fail;
179 }
180 leaf = path->nodes[0];
181 ei = btrfs_item_ptr(leaf, path->slots[0],
182 struct btrfs_file_extent_item);
183 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
184 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
185 btrfs_set_file_extent_encryption(leaf, ei, 0);
186 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
187 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
188 ptr = btrfs_file_extent_inline_start(ei);
189
190 if (use_compress) {
191 struct page *cpage;
192 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500193 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400194 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500195 cur_size = min_t(unsigned long, compressed_size,
Chris Masonc8b97812008-10-29 14:49:59 -0400196 PAGE_CACHE_SIZE);
197
198 kaddr = kmap(cpage);
199 write_extent_buffer(leaf, kaddr, ptr, cur_size);
200 kunmap(cpage);
201
202 i++;
203 ptr += cur_size;
204 compressed_size -= cur_size;
205 }
206 btrfs_set_file_extent_compression(leaf, ei,
207 BTRFS_COMPRESS_ZLIB);
208 } else {
209 page = find_get_page(inode->i_mapping,
210 start >> PAGE_CACHE_SHIFT);
211 btrfs_set_file_extent_compression(leaf, ei, 0);
212 kaddr = kmap_atomic(page, KM_USER0);
213 offset = start & (PAGE_CACHE_SIZE - 1);
214 write_extent_buffer(leaf, kaddr + offset, ptr, size);
215 kunmap_atomic(kaddr, KM_USER0);
216 page_cache_release(page);
217 }
218 btrfs_mark_buffer_dirty(leaf);
219 btrfs_free_path(path);
220
221 BTRFS_I(inode)->disk_i_size = inode->i_size;
222 btrfs_update_inode(trans, root, inode);
223 return 0;
224fail:
225 btrfs_free_path(path);
226 return err;
227}
228
229
230/*
231 * conditionally insert an inline extent into the file. This
232 * does the checks required to make sure the data is small enough
233 * to fit as an inline extent.
234 */
235static int cow_file_range_inline(struct btrfs_trans_handle *trans,
236 struct btrfs_root *root,
237 struct inode *inode, u64 start, u64 end,
238 size_t compressed_size,
239 struct page **compressed_pages)
240{
241 u64 isize = i_size_read(inode);
242 u64 actual_end = min(end + 1, isize);
243 u64 inline_len = actual_end - start;
244 u64 aligned_end = (end + root->sectorsize - 1) &
245 ~((u64)root->sectorsize - 1);
246 u64 hint_byte;
247 u64 data_len = inline_len;
248 int ret;
249
250 if (compressed_size)
251 data_len = compressed_size;
252
253 if (start > 0 ||
Chris Mason70b99e62008-10-31 12:46:39 -0400254 actual_end >= PAGE_CACHE_SIZE ||
Chris Masonc8b97812008-10-29 14:49:59 -0400255 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
256 (!compressed_size &&
257 (actual_end & (root->sectorsize - 1)) == 0) ||
258 end + 1 < isize ||
259 data_len > root->fs_info->max_inline) {
260 return 1;
261 }
262
Chris Masonc8b97812008-10-29 14:49:59 -0400263 ret = btrfs_drop_extents(trans, root, inode, start,
Chris Mason70b99e62008-10-31 12:46:39 -0400264 aligned_end, start, &hint_byte);
Chris Masonc8b97812008-10-29 14:49:59 -0400265 BUG_ON(ret);
266
267 if (isize > actual_end)
268 inline_len = min_t(u64, isize, actual_end);
269 ret = insert_inline_extent(trans, root, inode, start,
270 inline_len, compressed_size,
271 compressed_pages);
272 BUG_ON(ret);
273 btrfs_drop_extent_cache(inode, start, aligned_end, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400274 return 0;
275}
276
Chris Mason771ed682008-11-06 22:02:51 -0500277struct async_extent {
278 u64 start;
279 u64 ram_size;
280 u64 compressed_size;
281 struct page **pages;
282 unsigned long nr_pages;
283 struct list_head list;
284};
285
286struct async_cow {
287 struct inode *inode;
288 struct btrfs_root *root;
289 struct page *locked_page;
290 u64 start;
291 u64 end;
292 struct list_head extents;
293 struct btrfs_work work;
294};
295
296static noinline int add_async_extent(struct async_cow *cow,
297 u64 start, u64 ram_size,
298 u64 compressed_size,
299 struct page **pages,
300 unsigned long nr_pages)
301{
302 struct async_extent *async_extent;
303
304 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
305 async_extent->start = start;
306 async_extent->ram_size = ram_size;
307 async_extent->compressed_size = compressed_size;
308 async_extent->pages = pages;
309 async_extent->nr_pages = nr_pages;
310 list_add_tail(&async_extent->list, &cow->extents);
311 return 0;
312}
313
Chris Masonc8b97812008-10-29 14:49:59 -0400314/*
Chris Mason771ed682008-11-06 22:02:51 -0500315 * we create compressed extents in two phases. The first
316 * phase compresses a range of pages that have already been
317 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400318 *
Chris Mason771ed682008-11-06 22:02:51 -0500319 * This is done inside an ordered work queue, and the compression
320 * is spread across many cpus. The actual IO submission is step
321 * two, and the ordered work queue takes care of making sure that
322 * happens in the same order things were put onto the queue by
323 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400324 *
Chris Mason771ed682008-11-06 22:02:51 -0500325 * If this code finds it can't get good compression, it puts an
326 * entry onto the work queue to write the uncompressed bytes. This
327 * makes sure that both compressed inodes and uncompressed inodes
328 * are written in the same order that pdflush sent them down.
Chris Masond352ac62008-09-29 15:18:18 -0400329 */
Chris Mason771ed682008-11-06 22:02:51 -0500330static noinline int compress_file_range(struct inode *inode,
331 struct page *locked_page,
332 u64 start, u64 end,
333 struct async_cow *async_cow,
334 int *num_added)
Chris Masonb888db22007-08-27 16:49:44 -0400335{
336 struct btrfs_root *root = BTRFS_I(inode)->root;
337 struct btrfs_trans_handle *trans;
Chris Masondb945352007-10-15 16:15:53 -0400338 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -0400339 u64 orig_start;
340 u64 disk_num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400341 u64 blocksize = root->sectorsize;
Chris Masonc8b97812008-10-29 14:49:59 -0400342 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500343 u64 isize = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400344 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400345 struct page **pages = NULL;
346 unsigned long nr_pages;
347 unsigned long nr_pages_ret = 0;
348 unsigned long total_compressed = 0;
349 unsigned long total_in = 0;
350 unsigned long max_compressed = 128 * 1024;
Chris Mason771ed682008-11-06 22:02:51 -0500351 unsigned long max_uncompressed = 128 * 1024;
Chris Masonc8b97812008-10-29 14:49:59 -0400352 int i;
353 int will_compress;
Chris Masonb888db22007-08-27 16:49:44 -0400354
Chris Masonc8b97812008-10-29 14:49:59 -0400355 orig_start = start;
Chris Masonbe20aa92007-12-17 20:14:01 -0500356
Chris Mason42dc7ba2008-12-15 11:44:56 -0500357 actual_end = min_t(u64, isize, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400358again:
359 will_compress = 0;
360 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
361 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
362
Chris Masonf03d9302009-02-04 09:31:06 -0500363 /*
364 * we don't want to send crud past the end of i_size through
365 * compression, that's just a waste of CPU time. So, if the
366 * end of the file is before the start of our current
367 * requested range of bytes, we bail out to the uncompressed
368 * cleanup code that can deal with all of this.
369 *
370 * It isn't really the fastest way to fix things, but this is a
371 * very uncommon corner.
372 */
373 if (actual_end <= start)
374 goto cleanup_and_bail_uncompressed;
375
Chris Masonc8b97812008-10-29 14:49:59 -0400376 total_compressed = actual_end - start;
377
378 /* we want to make sure that amount of ram required to uncompress
379 * an extent is reasonable, so we limit the total size in ram
Chris Mason771ed682008-11-06 22:02:51 -0500380 * of a compressed extent to 128k. This is a crucial number
381 * because it also controls how easily we can spread reads across
382 * cpus for decompression.
383 *
384 * We also want to make sure the amount of IO required to do
385 * a random read is reasonably small, so we limit the size of
386 * a compressed extent to 128k.
Chris Masonc8b97812008-10-29 14:49:59 -0400387 */
388 total_compressed = min(total_compressed, max_uncompressed);
Chris Masondb945352007-10-15 16:15:53 -0400389 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500390 num_bytes = max(blocksize, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -0400391 disk_num_bytes = num_bytes;
392 total_in = 0;
393 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400394
Chris Mason771ed682008-11-06 22:02:51 -0500395 /*
396 * we do compression for mount -o compress and when the
397 * inode has not been flagged as nocompress. This flag can
398 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400399 */
400 if (!btrfs_test_flag(inode, NOCOMPRESS) &&
401 btrfs_test_opt(root, COMPRESS)) {
402 WARN_ON(pages);
Chris Masoncfbc2462008-10-30 13:22:14 -0400403 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
Chris Mason179e29e2007-11-01 11:28:41 -0400404
Chris Masonc8b97812008-10-29 14:49:59 -0400405 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
406 total_compressed, pages,
407 nr_pages, &nr_pages_ret,
408 &total_in,
409 &total_compressed,
410 max_compressed);
411
412 if (!ret) {
413 unsigned long offset = total_compressed &
414 (PAGE_CACHE_SIZE - 1);
415 struct page *page = pages[nr_pages_ret - 1];
416 char *kaddr;
417
418 /* zero the tail end of the last page, we might be
419 * sending it down to disk
420 */
421 if (offset) {
422 kaddr = kmap_atomic(page, KM_USER0);
423 memset(kaddr + offset, 0,
424 PAGE_CACHE_SIZE - offset);
425 kunmap_atomic(kaddr, KM_USER0);
426 }
427 will_compress = 1;
428 }
429 }
430 if (start == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500431 trans = btrfs_join_transaction(root, 1);
432 BUG_ON(!trans);
433 btrfs_set_trans_block_group(trans, inode);
434
Chris Masonc8b97812008-10-29 14:49:59 -0400435 /* lets try to make an inline extent */
Chris Mason771ed682008-11-06 22:02:51 -0500436 if (ret || total_in < (actual_end - start)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400437 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500438 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400439 */
440 ret = cow_file_range_inline(trans, root, inode,
441 start, end, 0, NULL);
442 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500443 /* try making a compressed inline extent */
Chris Masonc8b97812008-10-29 14:49:59 -0400444 ret = cow_file_range_inline(trans, root, inode,
445 start, end,
446 total_compressed, pages);
447 }
Chris Mason771ed682008-11-06 22:02:51 -0500448 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400449 if (ret == 0) {
Chris Mason771ed682008-11-06 22:02:51 -0500450 /*
451 * inline extent creation worked, we don't need
452 * to create any more async work items. Unlock
453 * and free up our temp pages.
454 */
Chris Masonc8b97812008-10-29 14:49:59 -0400455 extent_clear_unlock_delalloc(inode,
456 &BTRFS_I(inode)->io_tree,
Chris Mason771ed682008-11-06 22:02:51 -0500457 start, end, NULL, 1, 0,
458 0, 1, 1, 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400459 ret = 0;
460 goto free_pages_out;
461 }
462 }
463
464 if (will_compress) {
465 /*
466 * we aren't doing an inline extent round the compressed size
467 * up to a block size boundary so the allocator does sane
468 * things
469 */
470 total_compressed = (total_compressed + blocksize - 1) &
471 ~(blocksize - 1);
472
473 /*
474 * one last check to make sure the compression is really a
475 * win, compare the page count read with the blocks on disk
476 */
477 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
478 ~(PAGE_CACHE_SIZE - 1);
479 if (total_compressed >= total_in) {
480 will_compress = 0;
481 } else {
482 disk_num_bytes = total_compressed;
483 num_bytes = total_in;
484 }
485 }
486 if (!will_compress && pages) {
487 /*
488 * the compression code ran but failed to make things smaller,
489 * free any pages it allocated and our page pointer array
490 */
491 for (i = 0; i < nr_pages_ret; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400492 WARN_ON(pages[i]->mapping);
Chris Masonc8b97812008-10-29 14:49:59 -0400493 page_cache_release(pages[i]);
494 }
495 kfree(pages);
496 pages = NULL;
497 total_compressed = 0;
498 nr_pages_ret = 0;
499
500 /* flag the file so we don't compress in the future */
501 btrfs_set_flag(inode, NOCOMPRESS);
502 }
Chris Mason771ed682008-11-06 22:02:51 -0500503 if (will_compress) {
504 *num_added += 1;
505
506 /* the async work queues will take care of doing actual
507 * allocation on disk for these compressed pages,
508 * and will submit them to the elevator.
509 */
510 add_async_extent(async_cow, start, num_bytes,
511 total_compressed, pages, nr_pages_ret);
512
Chris Mason42dc7ba2008-12-15 11:44:56 -0500513 if (start + num_bytes < end && start + num_bytes < actual_end) {
Chris Mason771ed682008-11-06 22:02:51 -0500514 start += num_bytes;
515 pages = NULL;
516 cond_resched();
517 goto again;
518 }
519 } else {
Chris Masonf03d9302009-02-04 09:31:06 -0500520cleanup_and_bail_uncompressed:
Chris Mason771ed682008-11-06 22:02:51 -0500521 /*
522 * No compression, but we still need to write the pages in
523 * the file we've been given so far. redirty the locked
524 * page if it corresponds to our extent and set things up
525 * for the async work queue to run cow_file_range to do
526 * the normal delalloc dance
527 */
528 if (page_offset(locked_page) >= start &&
529 page_offset(locked_page) <= end) {
530 __set_page_dirty_nobuffers(locked_page);
531 /* unlocked later on in the async handlers */
532 }
533 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
534 *num_added += 1;
535 }
536
537out:
538 return 0;
539
540free_pages_out:
541 for (i = 0; i < nr_pages_ret; i++) {
542 WARN_ON(pages[i]->mapping);
543 page_cache_release(pages[i]);
544 }
Chris Masond3977122009-01-05 21:25:51 -0500545 kfree(pages);
Chris Mason771ed682008-11-06 22:02:51 -0500546
547 goto out;
548}
549
550/*
551 * phase two of compressed writeback. This is the ordered portion
552 * of the code, which only gets called in the order the work was
553 * queued. We walk all the async extents created by compress_file_range
554 * and send them down to the disk.
555 */
556static noinline int submit_compressed_extents(struct inode *inode,
557 struct async_cow *async_cow)
558{
559 struct async_extent *async_extent;
560 u64 alloc_hint = 0;
561 struct btrfs_trans_handle *trans;
562 struct btrfs_key ins;
563 struct extent_map *em;
564 struct btrfs_root *root = BTRFS_I(inode)->root;
565 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
566 struct extent_io_tree *io_tree;
567 int ret;
568
569 if (list_empty(&async_cow->extents))
570 return 0;
571
572 trans = btrfs_join_transaction(root, 1);
573
Chris Masond3977122009-01-05 21:25:51 -0500574 while (!list_empty(&async_cow->extents)) {
Chris Mason771ed682008-11-06 22:02:51 -0500575 async_extent = list_entry(async_cow->extents.next,
576 struct async_extent, list);
577 list_del(&async_extent->list);
578
579 io_tree = &BTRFS_I(inode)->io_tree;
580
581 /* did the compression code fall back to uncompressed IO? */
582 if (!async_extent->pages) {
583 int page_started = 0;
584 unsigned long nr_written = 0;
585
586 lock_extent(io_tree, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500587 async_extent->start +
588 async_extent->ram_size - 1, GFP_NOFS);
Chris Mason771ed682008-11-06 22:02:51 -0500589
590 /* allocate blocks */
591 cow_file_range(inode, async_cow->locked_page,
592 async_extent->start,
593 async_extent->start +
594 async_extent->ram_size - 1,
595 &page_started, &nr_written, 0);
596
597 /*
598 * if page_started, cow_file_range inserted an
599 * inline extent and took care of all the unlocking
600 * and IO for us. Otherwise, we need to submit
601 * all those pages down to the drive.
602 */
603 if (!page_started)
604 extent_write_locked_range(io_tree,
605 inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500606 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500607 async_extent->ram_size - 1,
608 btrfs_get_extent,
609 WB_SYNC_ALL);
610 kfree(async_extent);
611 cond_resched();
612 continue;
613 }
614
615 lock_extent(io_tree, async_extent->start,
616 async_extent->start + async_extent->ram_size - 1,
617 GFP_NOFS);
618 /*
619 * here we're doing allocation and writeback of the
620 * compressed pages
621 */
622 btrfs_drop_extent_cache(inode, async_extent->start,
623 async_extent->start +
624 async_extent->ram_size - 1, 0);
625
626 ret = btrfs_reserve_extent(trans, root,
627 async_extent->compressed_size,
628 async_extent->compressed_size,
629 0, alloc_hint,
630 (u64)-1, &ins, 1);
631 BUG_ON(ret);
632 em = alloc_extent_map(GFP_NOFS);
633 em->start = async_extent->start;
634 em->len = async_extent->ram_size;
Chris Mason445a6942008-11-10 11:53:33 -0500635 em->orig_start = em->start;
Chris Mason771ed682008-11-06 22:02:51 -0500636
637 em->block_start = ins.objectid;
638 em->block_len = ins.offset;
639 em->bdev = root->fs_info->fs_devices->latest_bdev;
640 set_bit(EXTENT_FLAG_PINNED, &em->flags);
641 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
642
Chris Masond3977122009-01-05 21:25:51 -0500643 while (1) {
Chris Mason771ed682008-11-06 22:02:51 -0500644 spin_lock(&em_tree->lock);
645 ret = add_extent_mapping(em_tree, em);
646 spin_unlock(&em_tree->lock);
647 if (ret != -EEXIST) {
648 free_extent_map(em);
649 break;
650 }
651 btrfs_drop_extent_cache(inode, async_extent->start,
652 async_extent->start +
653 async_extent->ram_size - 1, 0);
654 }
655
656 ret = btrfs_add_ordered_extent(inode, async_extent->start,
657 ins.objectid,
658 async_extent->ram_size,
659 ins.offset,
660 BTRFS_ORDERED_COMPRESSED);
661 BUG_ON(ret);
662
663 btrfs_end_transaction(trans, root);
664
665 /*
666 * clear dirty, set writeback and unlock the pages.
667 */
668 extent_clear_unlock_delalloc(inode,
669 &BTRFS_I(inode)->io_tree,
670 async_extent->start,
671 async_extent->start +
672 async_extent->ram_size - 1,
673 NULL, 1, 1, 0, 1, 1, 0);
674
675 ret = btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500676 async_extent->start,
677 async_extent->ram_size,
678 ins.objectid,
679 ins.offset, async_extent->pages,
680 async_extent->nr_pages);
Chris Mason771ed682008-11-06 22:02:51 -0500681
682 BUG_ON(ret);
683 trans = btrfs_join_transaction(root, 1);
684 alloc_hint = ins.objectid + ins.offset;
685 kfree(async_extent);
686 cond_resched();
687 }
688
689 btrfs_end_transaction(trans, root);
690 return 0;
691}
692
693/*
694 * when extent_io.c finds a delayed allocation range in the file,
695 * the call backs end up in this code. The basic idea is to
696 * allocate extents on disk for the range, and create ordered data structs
697 * in ram to track those extents.
698 *
699 * locked_page is the page that writepage had locked already. We use
700 * it to make sure we don't do extra locks or unlocks.
701 *
702 * *page_started is set to one if we unlock locked_page and do everything
703 * required to start IO on it. It may be clean and already done with
704 * IO when we return.
705 */
706static noinline int cow_file_range(struct inode *inode,
707 struct page *locked_page,
708 u64 start, u64 end, int *page_started,
709 unsigned long *nr_written,
710 int unlock)
711{
712 struct btrfs_root *root = BTRFS_I(inode)->root;
713 struct btrfs_trans_handle *trans;
714 u64 alloc_hint = 0;
715 u64 num_bytes;
716 unsigned long ram_size;
717 u64 disk_num_bytes;
718 u64 cur_alloc_size;
719 u64 blocksize = root->sectorsize;
720 u64 actual_end;
Chris Mason42dc7ba2008-12-15 11:44:56 -0500721 u64 isize = i_size_read(inode);
Chris Mason771ed682008-11-06 22:02:51 -0500722 struct btrfs_key ins;
723 struct extent_map *em;
724 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
725 int ret = 0;
726
727 trans = btrfs_join_transaction(root, 1);
728 BUG_ON(!trans);
729 btrfs_set_trans_block_group(trans, inode);
730
Chris Mason42dc7ba2008-12-15 11:44:56 -0500731 actual_end = min_t(u64, isize, end + 1);
Chris Mason771ed682008-11-06 22:02:51 -0500732
733 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
734 num_bytes = max(blocksize, num_bytes);
735 disk_num_bytes = num_bytes;
736 ret = 0;
737
738 if (start == 0) {
739 /* lets try to make an inline extent */
740 ret = cow_file_range_inline(trans, root, inode,
741 start, end, 0, NULL);
742 if (ret == 0) {
743 extent_clear_unlock_delalloc(inode,
744 &BTRFS_I(inode)->io_tree,
745 start, end, NULL, 1, 1,
746 1, 1, 1, 1);
747 *nr_written = *nr_written +
748 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
749 *page_started = 1;
750 ret = 0;
751 goto out;
752 }
753 }
Chris Masonc8b97812008-10-29 14:49:59 -0400754
755 BUG_ON(disk_num_bytes >
756 btrfs_super_total_bytes(&root->fs_info->super_copy));
757
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400758 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -0400759
Chris Masond3977122009-01-05 21:25:51 -0500760 while (disk_num_bytes > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400761 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400762 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Chris Mason771ed682008-11-06 22:02:51 -0500763 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400764 (u64)-1, &ins, 1);
Chris Masond3977122009-01-05 21:25:51 -0500765 BUG_ON(ret);
766
Chris Masone6dcd2d2008-07-17 12:53:50 -0400767 em = alloc_extent_map(GFP_NOFS);
768 em->start = start;
Chris Mason445a6942008-11-10 11:53:33 -0500769 em->orig_start = em->start;
Chris Masonc8b97812008-10-29 14:49:59 -0400770
Chris Mason771ed682008-11-06 22:02:51 -0500771 ram_size = ins.offset;
772 em->len = ins.offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400773
Chris Masone6dcd2d2008-07-17 12:53:50 -0400774 em->block_start = ins.objectid;
Chris Masonc8b97812008-10-29 14:49:59 -0400775 em->block_len = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400776 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400777 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -0400778
Chris Masond3977122009-01-05 21:25:51 -0500779 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400780 spin_lock(&em_tree->lock);
781 ret = add_extent_mapping(em_tree, em);
782 spin_unlock(&em_tree->lock);
783 if (ret != -EEXIST) {
784 free_extent_map(em);
785 break;
786 }
787 btrfs_drop_extent_cache(inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400788 start + ram_size - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400789 }
790
Chris Mason98d20f62008-04-14 09:46:10 -0400791 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400792 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -0500793 ram_size, cur_alloc_size, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400794 BUG_ON(ret);
Chris Masonc8b97812008-10-29 14:49:59 -0400795
Yan Zheng17d217f2008-12-12 10:03:38 -0500796 if (root->root_key.objectid ==
797 BTRFS_DATA_RELOC_TREE_OBJECTID) {
798 ret = btrfs_reloc_clone_csums(inode, start,
799 cur_alloc_size);
800 BUG_ON(ret);
801 }
802
Chris Masond3977122009-01-05 21:25:51 -0500803 if (disk_num_bytes < cur_alloc_size)
Chris Mason3b951512008-04-17 11:29:12 -0400804 break;
Chris Masond3977122009-01-05 21:25:51 -0500805
Chris Masonc8b97812008-10-29 14:49:59 -0400806 /* we're not doing compressed IO, don't unlock the first
807 * page (which the caller expects to stay locked), don't
808 * clear any dirty bits and don't set any writeback bits
809 */
810 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
811 start, start + ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500812 locked_page, unlock, 1,
813 1, 0, 0, 0);
Chris Masonc8b97812008-10-29 14:49:59 -0400814 disk_num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -0500815 num_bytes -= cur_alloc_size;
816 alloc_hint = ins.objectid + ins.offset;
817 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400818 }
Chris Masonb888db22007-08-27 16:49:44 -0400819out:
Chris Mason771ed682008-11-06 22:02:51 -0500820 ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400821 btrfs_end_transaction(trans, root);
Chris Masonc8b97812008-10-29 14:49:59 -0400822
Chris Masonbe20aa92007-12-17 20:14:01 -0500823 return ret;
Chris Mason771ed682008-11-06 22:02:51 -0500824}
Chris Masonc8b97812008-10-29 14:49:59 -0400825
Chris Mason771ed682008-11-06 22:02:51 -0500826/*
827 * work queue call back to started compression on a file and pages
828 */
829static noinline void async_cow_start(struct btrfs_work *work)
830{
831 struct async_cow *async_cow;
832 int num_added = 0;
833 async_cow = container_of(work, struct async_cow, work);
834
835 compress_file_range(async_cow->inode, async_cow->locked_page,
836 async_cow->start, async_cow->end, async_cow,
837 &num_added);
838 if (num_added == 0)
839 async_cow->inode = NULL;
840}
841
842/*
843 * work queue call back to submit previously compressed pages
844 */
845static noinline void async_cow_submit(struct btrfs_work *work)
846{
847 struct async_cow *async_cow;
848 struct btrfs_root *root;
849 unsigned long nr_pages;
850
851 async_cow = container_of(work, struct async_cow, work);
852
853 root = async_cow->root;
854 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
855 PAGE_CACHE_SHIFT;
856
857 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
858
859 if (atomic_read(&root->fs_info->async_delalloc_pages) <
860 5 * 1042 * 1024 &&
861 waitqueue_active(&root->fs_info->async_submit_wait))
862 wake_up(&root->fs_info->async_submit_wait);
863
Chris Masond3977122009-01-05 21:25:51 -0500864 if (async_cow->inode)
Chris Mason771ed682008-11-06 22:02:51 -0500865 submit_compressed_extents(async_cow->inode, async_cow);
Chris Mason771ed682008-11-06 22:02:51 -0500866}
Chris Masonc8b97812008-10-29 14:49:59 -0400867
Chris Mason771ed682008-11-06 22:02:51 -0500868static noinline void async_cow_free(struct btrfs_work *work)
869{
870 struct async_cow *async_cow;
871 async_cow = container_of(work, struct async_cow, work);
872 kfree(async_cow);
873}
874
875static int cow_file_range_async(struct inode *inode, struct page *locked_page,
876 u64 start, u64 end, int *page_started,
877 unsigned long *nr_written)
878{
879 struct async_cow *async_cow;
880 struct btrfs_root *root = BTRFS_I(inode)->root;
881 unsigned long nr_pages;
882 u64 cur_end;
883 int limit = 10 * 1024 * 1042;
884
885 if (!btrfs_test_opt(root, COMPRESS)) {
886 return cow_file_range(inode, locked_page, start, end,
887 page_started, nr_written, 1);
888 }
889
890 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED |
891 EXTENT_DELALLOC, 1, 0, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500892 while (start < end) {
Chris Mason771ed682008-11-06 22:02:51 -0500893 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
894 async_cow->inode = inode;
895 async_cow->root = root;
896 async_cow->locked_page = locked_page;
897 async_cow->start = start;
898
899 if (btrfs_test_flag(inode, NOCOMPRESS))
900 cur_end = end;
901 else
902 cur_end = min(end, start + 512 * 1024 - 1);
903
904 async_cow->end = cur_end;
905 INIT_LIST_HEAD(&async_cow->extents);
906
907 async_cow->work.func = async_cow_start;
908 async_cow->work.ordered_func = async_cow_submit;
909 async_cow->work.ordered_free = async_cow_free;
910 async_cow->work.flags = 0;
911
Chris Mason771ed682008-11-06 22:02:51 -0500912 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
913 PAGE_CACHE_SHIFT;
914 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
915
916 btrfs_queue_worker(&root->fs_info->delalloc_workers,
917 &async_cow->work);
918
919 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
920 wait_event(root->fs_info->async_submit_wait,
921 (atomic_read(&root->fs_info->async_delalloc_pages) <
922 limit));
923 }
924
Chris Masond3977122009-01-05 21:25:51 -0500925 while (atomic_read(&root->fs_info->async_submit_draining) &&
Chris Mason771ed682008-11-06 22:02:51 -0500926 atomic_read(&root->fs_info->async_delalloc_pages)) {
927 wait_event(root->fs_info->async_submit_wait,
928 (atomic_read(&root->fs_info->async_delalloc_pages) ==
929 0));
930 }
931
932 *nr_written += nr_pages;
933 start = cur_end + 1;
934 }
935 *page_started = 1;
936 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500937}
938
Chris Masond3977122009-01-05 21:25:51 -0500939static noinline int csum_exist_in_range(struct btrfs_root *root,
Yan Zheng17d217f2008-12-12 10:03:38 -0500940 u64 bytenr, u64 num_bytes)
941{
942 int ret;
943 struct btrfs_ordered_sum *sums;
944 LIST_HEAD(list);
945
Yan Zheng07d400a2009-01-06 11:42:00 -0500946 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
947 bytenr + num_bytes - 1, &list);
Yan Zheng17d217f2008-12-12 10:03:38 -0500948 if (ret == 0 && list_empty(&list))
949 return 0;
950
951 while (!list_empty(&list)) {
952 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
953 list_del(&sums->list);
954 kfree(sums);
955 }
956 return 1;
957}
958
Chris Masond352ac62008-09-29 15:18:18 -0400959/*
960 * when nowcow writeback call back. This checks for snapshots or COW copies
961 * of the extents that exist in the file, and COWs the file as required.
962 *
963 * If no cow copies or snapshots exist, we write directly to the existing
964 * blocks on disk
965 */
Chris Masonc8b97812008-10-29 14:49:59 -0400966static int run_delalloc_nocow(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -0500967 u64 start, u64 end, int *page_started, int force,
968 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -0500969{
Chris Masonbe20aa92007-12-17 20:14:01 -0500970 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400971 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -0500972 struct extent_buffer *leaf;
Chris Masonbe20aa92007-12-17 20:14:01 -0500973 struct btrfs_path *path;
Yan Zheng80ff3852008-10-30 14:20:02 -0400974 struct btrfs_file_extent_item *fi;
Chris Masonbe20aa92007-12-17 20:14:01 -0500975 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -0400976 u64 cow_start;
977 u64 cur_offset;
978 u64 extent_end;
979 u64 disk_bytenr;
980 u64 num_bytes;
981 int extent_type;
982 int ret;
Yan Zhengd899e052008-10-30 14:25:28 -0400983 int type;
Yan Zheng80ff3852008-10-30 14:20:02 -0400984 int nocow;
985 int check_prev = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500986
987 path = btrfs_alloc_path();
988 BUG_ON(!path);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400989 trans = btrfs_join_transaction(root, 1);
990 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500991
Yan Zheng80ff3852008-10-30 14:20:02 -0400992 cow_start = (u64)-1;
993 cur_offset = start;
994 while (1) {
995 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
996 cur_offset, 0);
997 BUG_ON(ret < 0);
998 if (ret > 0 && path->slots[0] > 0 && check_prev) {
999 leaf = path->nodes[0];
1000 btrfs_item_key_to_cpu(leaf, &found_key,
1001 path->slots[0] - 1);
1002 if (found_key.objectid == inode->i_ino &&
1003 found_key.type == BTRFS_EXTENT_DATA_KEY)
1004 path->slots[0]--;
1005 }
1006 check_prev = 0;
1007next_slot:
1008 leaf = path->nodes[0];
1009 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1010 ret = btrfs_next_leaf(root, path);
1011 if (ret < 0)
1012 BUG_ON(1);
1013 if (ret > 0)
1014 break;
1015 leaf = path->nodes[0];
1016 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001017
Yan Zheng80ff3852008-10-30 14:20:02 -04001018 nocow = 0;
1019 disk_bytenr = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -05001020 num_bytes = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001021 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001022
Yan Zheng80ff3852008-10-30 14:20:02 -04001023 if (found_key.objectid > inode->i_ino ||
1024 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1025 found_key.offset > end)
1026 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001027
Yan Zheng80ff3852008-10-30 14:20:02 -04001028 if (found_key.offset > cur_offset) {
1029 extent_end = found_key.offset;
1030 goto out_check;
1031 }
Chris Masonc31f8832008-01-08 15:46:31 -05001032
Yan Zheng80ff3852008-10-30 14:20:02 -04001033 fi = btrfs_item_ptr(leaf, path->slots[0],
1034 struct btrfs_file_extent_item);
1035 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001036
Yan Zhengd899e052008-10-30 14:25:28 -04001037 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1038 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001039 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1040 extent_end = found_key.offset +
1041 btrfs_file_extent_num_bytes(leaf, fi);
1042 if (extent_end <= start) {
1043 path->slots[0]++;
1044 goto next_slot;
1045 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001046 if (disk_bytenr == 0)
1047 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001048 if (btrfs_file_extent_compression(leaf, fi) ||
1049 btrfs_file_extent_encryption(leaf, fi) ||
1050 btrfs_file_extent_other_encoding(leaf, fi))
1051 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001052 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1053 goto out_check;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001054 if (btrfs_extent_readonly(root, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001055 goto out_check;
Yan Zheng17d217f2008-12-12 10:03:38 -05001056 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
1057 disk_bytenr))
1058 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001059 disk_bytenr += btrfs_file_extent_offset(leaf, fi);
Yan Zheng17d217f2008-12-12 10:03:38 -05001060 disk_bytenr += cur_offset - found_key.offset;
1061 num_bytes = min(end + 1, extent_end) - cur_offset;
1062 /*
1063 * force cow if csum exists in the range.
1064 * this ensure that csum for a given extent are
1065 * either valid or do not exist.
1066 */
1067 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1068 goto out_check;
Yan Zheng80ff3852008-10-30 14:20:02 -04001069 nocow = 1;
1070 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1071 extent_end = found_key.offset +
1072 btrfs_file_extent_inline_len(leaf, fi);
1073 extent_end = ALIGN(extent_end, root->sectorsize);
1074 } else {
1075 BUG_ON(1);
1076 }
1077out_check:
1078 if (extent_end <= start) {
1079 path->slots[0]++;
1080 goto next_slot;
1081 }
1082 if (!nocow) {
1083 if (cow_start == (u64)-1)
1084 cow_start = cur_offset;
1085 cur_offset = extent_end;
1086 if (cur_offset > end)
1087 break;
1088 path->slots[0]++;
1089 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001090 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001091
Yan Zheng7ea394f2008-08-05 13:05:02 -04001092 btrfs_release_path(root, path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001093 if (cow_start != (u64)-1) {
1094 ret = cow_file_range(inode, locked_page, cow_start,
Chris Mason771ed682008-11-06 22:02:51 -05001095 found_key.offset - 1, page_started,
1096 nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001097 BUG_ON(ret);
1098 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001099 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001100
Yan Zhengd899e052008-10-30 14:25:28 -04001101 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1102 struct extent_map *em;
1103 struct extent_map_tree *em_tree;
1104 em_tree = &BTRFS_I(inode)->extent_tree;
1105 em = alloc_extent_map(GFP_NOFS);
1106 em->start = cur_offset;
Chris Mason445a6942008-11-10 11:53:33 -05001107 em->orig_start = em->start;
Yan Zhengd899e052008-10-30 14:25:28 -04001108 em->len = num_bytes;
1109 em->block_len = num_bytes;
1110 em->block_start = disk_bytenr;
1111 em->bdev = root->fs_info->fs_devices->latest_bdev;
1112 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1113 while (1) {
1114 spin_lock(&em_tree->lock);
1115 ret = add_extent_mapping(em_tree, em);
1116 spin_unlock(&em_tree->lock);
1117 if (ret != -EEXIST) {
1118 free_extent_map(em);
1119 break;
1120 }
1121 btrfs_drop_extent_cache(inode, em->start,
1122 em->start + em->len - 1, 0);
1123 }
1124 type = BTRFS_ORDERED_PREALLOC;
1125 } else {
1126 type = BTRFS_ORDERED_NOCOW;
1127 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001128
1129 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
Yan Zhengd899e052008-10-30 14:25:28 -04001130 num_bytes, num_bytes, type);
1131 BUG_ON(ret);
Chris Mason771ed682008-11-06 22:02:51 -05001132
Yan Zhengd899e052008-10-30 14:25:28 -04001133 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
1134 cur_offset, cur_offset + num_bytes - 1,
Chris Mason771ed682008-11-06 22:02:51 -05001135 locked_page, 1, 1, 1, 0, 0, 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04001136 cur_offset = extent_end;
1137 if (cur_offset > end)
1138 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001139 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001140 btrfs_release_path(root, path);
1141
1142 if (cur_offset <= end && cow_start == (u64)-1)
1143 cow_start = cur_offset;
1144 if (cow_start != (u64)-1) {
1145 ret = cow_file_range(inode, locked_page, cow_start, end,
Chris Mason771ed682008-11-06 22:02:51 -05001146 page_started, nr_written, 1);
Yan Zheng80ff3852008-10-30 14:20:02 -04001147 BUG_ON(ret);
1148 }
1149
1150 ret = btrfs_end_transaction(trans, root);
1151 BUG_ON(ret);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001152 btrfs_free_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001153 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001154}
1155
Chris Masond352ac62008-09-29 15:18:18 -04001156/*
1157 * extent_io.c call back to do delayed allocation processing
1158 */
Chris Masonc8b97812008-10-29 14:49:59 -04001159static int run_delalloc_range(struct inode *inode, struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001160 u64 start, u64 end, int *page_started,
1161 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001162{
Chris Masonbe20aa92007-12-17 20:14:01 -05001163 int ret;
Chris Masona2135012008-06-25 16:01:30 -04001164
Yan Zheng17d217f2008-12-12 10:03:38 -05001165 if (btrfs_test_flag(inode, NODATACOW))
Chris Masonc8b97812008-10-29 14:49:59 -04001166 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001167 page_started, 1, nr_written);
Yan Zhengd899e052008-10-30 14:25:28 -04001168 else if (btrfs_test_flag(inode, PREALLOC))
1169 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001170 page_started, 0, nr_written);
Chris Masonbe20aa92007-12-17 20:14:01 -05001171 else
Chris Mason771ed682008-11-06 22:02:51 -05001172 ret = cow_file_range_async(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001173 page_started, nr_written);
Chris Mason1832a6d2007-12-21 16:27:21 -05001174
Chris Masonb888db22007-08-27 16:49:44 -04001175 return ret;
1176}
1177
Chris Masond352ac62008-09-29 15:18:18 -04001178/*
1179 * extent_io.c set_bit_hook, used to track delayed allocation
1180 * bytes in this file, and to maintain the list of inodes that
1181 * have pending delalloc work to be done.
1182 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001183static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -05001184 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001185{
Chris Mason75eff682008-12-15 15:54:40 -05001186 /*
1187 * set_bit and clear bit hooks normally require _irqsave/restore
1188 * but in this case, we are only testeing for the DELALLOC
1189 * bit, which is only set or cleared with irqs on
1190 */
Chris Masonb0c68f82008-01-31 11:05:37 -05001191 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001192 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason75eff682008-12-15 15:54:40 -05001193 spin_lock(&root->fs_info->delalloc_lock);
Chris Mason90692182008-02-08 13:49:28 -05001194 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -05001195 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonea8c2812008-08-04 23:17:27 -04001196 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1197 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1198 &root->fs_info->delalloc_inodes);
1199 }
Chris Mason75eff682008-12-15 15:54:40 -05001200 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001201 }
1202 return 0;
1203}
1204
Chris Masond352ac62008-09-29 15:18:18 -04001205/*
1206 * extent_io.c clear_bit_hook, see set_bit_hook for why
1207 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001208static int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -05001209 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -05001210{
Chris Mason75eff682008-12-15 15:54:40 -05001211 /*
1212 * set_bit and clear bit hooks normally require _irqsave/restore
1213 * but in this case, we are only testeing for the DELALLOC
1214 * bit, which is only set or cleared with irqs on
1215 */
Chris Masonb0c68f82008-01-31 11:05:37 -05001216 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001217 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -04001218
Chris Mason75eff682008-12-15 15:54:40 -05001219 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonb0c68f82008-01-31 11:05:37 -05001220 if (end - start + 1 > root->fs_info->delalloc_bytes) {
Chris Masond3977122009-01-05 21:25:51 -05001221 printk(KERN_INFO "btrfs warning: delalloc account "
1222 "%llu %llu\n",
1223 (unsigned long long)end - start + 1,
1224 (unsigned long long)
1225 root->fs_info->delalloc_bytes);
Chris Masonb0c68f82008-01-31 11:05:37 -05001226 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -05001227 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -05001228 } else {
1229 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -05001230 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -05001231 }
Chris Masonea8c2812008-08-04 23:17:27 -04001232 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1233 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1234 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1235 }
Chris Mason75eff682008-12-15 15:54:40 -05001236 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason291d6732008-01-29 15:55:23 -05001237 }
1238 return 0;
1239}
1240
Chris Masond352ac62008-09-29 15:18:18 -04001241/*
1242 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1243 * we don't create bios that span stripes or chunks
1244 */
Chris Mason239b14b2008-03-24 15:02:07 -04001245int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
Chris Masonc8b97812008-10-29 14:49:59 -04001246 size_t size, struct bio *bio,
1247 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04001248{
1249 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1250 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -04001251 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04001252 u64 length = 0;
1253 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04001254 int ret;
1255
Chris Mason771ed682008-11-06 22:02:51 -05001256 if (bio_flags & EXTENT_BIO_COMPRESSED)
1257 return 0;
1258
Chris Masonf2d8d742008-04-21 10:03:05 -04001259 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04001260 map_tree = &root->fs_info->mapping_tree;
1261 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04001262 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -04001263 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -04001264
Chris Masond3977122009-01-05 21:25:51 -05001265 if (map_length < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04001266 return 1;
Chris Mason239b14b2008-03-24 15:02:07 -04001267 return 0;
1268}
1269
Chris Masond352ac62008-09-29 15:18:18 -04001270/*
1271 * in order to insert checksums into the metadata in large chunks,
1272 * we wait until bio submission time. All the pages in the bio are
1273 * checksummed and sums are attached onto the ordered extent record.
1274 *
1275 * At IO completion time the cums attached on the ordered extent record
1276 * are inserted into the btree
1277 */
Chris Masond3977122009-01-05 21:25:51 -05001278static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1279 struct bio *bio, int mirror_num,
1280 unsigned long bio_flags)
Chris Mason065631f2008-02-20 12:07:25 -05001281{
Chris Mason065631f2008-02-20 12:07:25 -05001282 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -05001283 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04001284
Chris Masond20f7042008-12-08 16:58:54 -05001285 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
Chris Mason44b8bd72008-04-16 11:14:51 -04001286 BUG_ON(ret);
Chris Mason4a69a412008-11-06 22:03:00 -05001287 return 0;
1288}
Chris Masone0156402008-04-16 11:15:20 -04001289
Chris Mason4a69a412008-11-06 22:03:00 -05001290/*
1291 * in order to insert checksums into the metadata in large chunks,
1292 * we wait until bio submission time. All the pages in the bio are
1293 * checksummed and sums are attached onto the ordered extent record.
1294 *
1295 * At IO completion time the cums attached on the ordered extent record
1296 * are inserted into the btree
1297 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001298static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
Chris Mason4a69a412008-11-06 22:03:00 -05001299 int mirror_num, unsigned long bio_flags)
1300{
1301 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8b712842008-06-11 16:50:36 -04001302 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -04001303}
1304
Chris Masond352ac62008-09-29 15:18:18 -04001305/*
Chris Masoncad321a2008-12-17 14:51:42 -05001306 * extent_io.c submission hook. This does the right thing for csum calculation
1307 * on write, or reading the csums from the tree before a read
Chris Masond352ac62008-09-29 15:18:18 -04001308 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001309static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001310 int mirror_num, unsigned long bio_flags)
Chris Mason44b8bd72008-04-16 11:14:51 -04001311{
1312 struct btrfs_root *root = BTRFS_I(inode)->root;
1313 int ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001314 int skip_sum;
Chris Mason44b8bd72008-04-16 11:14:51 -04001315
Chris Masoncad321a2008-12-17 14:51:42 -05001316 skip_sum = btrfs_test_flag(inode, NODATASUM);
1317
Chris Masone6dcd2d2008-07-17 12:53:50 -04001318 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1319 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -05001320
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001321 if (!(rw & (1 << BIO_RW))) {
Chris Masond20f7042008-12-08 16:58:54 -05001322 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Chris Masonc8b97812008-10-29 14:49:59 -04001323 return btrfs_submit_compressed_read(inode, bio,
1324 mirror_num, bio_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001325 } else if (!skip_sum)
1326 btrfs_lookup_bio_sums(root, inode, bio, NULL);
Chris Mason4d1b5fb2008-08-20 09:44:52 -04001327 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001328 } else if (!skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05001329 /* csum items have already been cloned */
1330 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1331 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04001332 /* we're doing a write, do the async checksumming */
1333 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
Chris Mason44b8bd72008-04-16 11:14:51 -04001334 inode, rw, bio, mirror_num,
Chris Mason4a69a412008-11-06 22:03:00 -05001335 bio_flags, __btrfs_submit_bio_start,
1336 __btrfs_submit_bio_done);
Chris Mason19b9bdb2008-10-30 14:23:13 -04001337 }
1338
Chris Mason0b86a832008-03-24 15:01:56 -04001339mapit:
Chris Mason8b712842008-06-11 16:50:36 -04001340 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -05001341}
Chris Mason6885f302008-02-20 16:11:05 -05001342
Chris Masond352ac62008-09-29 15:18:18 -04001343/*
1344 * given a list of ordered sums record them in the inode. This happens
1345 * at IO completion time based on sums calculated at bio submission time.
1346 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001347static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001348 struct inode *inode, u64 file_offset,
1349 struct list_head *list)
1350{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001351 struct btrfs_ordered_sum *sum;
1352
1353 btrfs_set_trans_block_group(trans, inode);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001354
1355 list_for_each_entry(sum, list, list) {
Chris Masond20f7042008-12-08 16:58:54 -05001356 btrfs_csum_file_blocks(trans,
1357 BTRFS_I(inode)->root->fs_info->csum_root, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001358 }
1359 return 0;
1360}
1361
Chris Masonea8c2812008-08-04 23:17:27 -04001362int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
1363{
Chris Masond3977122009-01-05 21:25:51 -05001364 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001365 WARN_ON(1);
Chris Masonea8c2812008-08-04 23:17:27 -04001366 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1367 GFP_NOFS);
1368}
1369
Chris Masond352ac62008-09-29 15:18:18 -04001370/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04001371struct btrfs_writepage_fixup {
1372 struct page *page;
1373 struct btrfs_work work;
1374};
1375
Christoph Hellwigb2950862008-12-02 09:54:17 -05001376static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04001377{
1378 struct btrfs_writepage_fixup *fixup;
1379 struct btrfs_ordered_extent *ordered;
1380 struct page *page;
1381 struct inode *inode;
1382 u64 page_start;
1383 u64 page_end;
1384
1385 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1386 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -04001387again:
Chris Mason247e7432008-07-17 12:53:51 -04001388 lock_page(page);
1389 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1390 ClearPageChecked(page);
1391 goto out_page;
1392 }
1393
1394 inode = page->mapping->host;
1395 page_start = page_offset(page);
1396 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1397
1398 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -04001399
1400 /* already ordered? We're done */
1401 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
1402 EXTENT_ORDERED, 0)) {
Chris Mason247e7432008-07-17 12:53:51 -04001403 goto out;
Chris Mason4a096752008-07-21 10:29:44 -04001404 }
1405
1406 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1407 if (ordered) {
1408 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
1409 page_end, GFP_NOFS);
1410 unlock_page(page);
1411 btrfs_start_ordered_extent(inode, ordered, 1);
1412 goto again;
1413 }
Chris Mason247e7432008-07-17 12:53:51 -04001414
Chris Masonea8c2812008-08-04 23:17:27 -04001415 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Mason247e7432008-07-17 12:53:51 -04001416 ClearPageChecked(page);
1417out:
1418 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
1419out_page:
1420 unlock_page(page);
1421 page_cache_release(page);
1422}
1423
1424/*
1425 * There are a few paths in the higher layers of the kernel that directly
1426 * set the page dirty bit without asking the filesystem if it is a
1427 * good idea. This causes problems because we want to make sure COW
1428 * properly happens and the data=ordered rules are followed.
1429 *
Chris Masonc8b97812008-10-29 14:49:59 -04001430 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04001431 * hasn't been properly setup for IO. We kick off an async process
1432 * to fix it up. The async helper will wait for ordered extents, set
1433 * the delalloc bit and make it safe to write the page.
1434 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001435static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04001436{
1437 struct inode *inode = page->mapping->host;
1438 struct btrfs_writepage_fixup *fixup;
1439 struct btrfs_root *root = BTRFS_I(inode)->root;
1440 int ret;
1441
1442 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1443 EXTENT_ORDERED, 0);
1444 if (ret)
1445 return 0;
1446
1447 if (PageChecked(page))
1448 return -EAGAIN;
1449
1450 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1451 if (!fixup)
1452 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04001453
Chris Mason247e7432008-07-17 12:53:51 -04001454 SetPageChecked(page);
1455 page_cache_get(page);
1456 fixup->work.func = btrfs_writepage_fixup_worker;
1457 fixup->page = page;
1458 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1459 return -EAGAIN;
1460}
1461
Yan Zhengd899e052008-10-30 14:25:28 -04001462static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1463 struct inode *inode, u64 file_pos,
1464 u64 disk_bytenr, u64 disk_num_bytes,
1465 u64 num_bytes, u64 ram_bytes,
1466 u8 compression, u8 encryption,
1467 u16 other_encoding, int extent_type)
1468{
1469 struct btrfs_root *root = BTRFS_I(inode)->root;
1470 struct btrfs_file_extent_item *fi;
1471 struct btrfs_path *path;
1472 struct extent_buffer *leaf;
1473 struct btrfs_key ins;
1474 u64 hint;
1475 int ret;
1476
1477 path = btrfs_alloc_path();
1478 BUG_ON(!path);
1479
1480 ret = btrfs_drop_extents(trans, root, inode, file_pos,
1481 file_pos + num_bytes, file_pos, &hint);
1482 BUG_ON(ret);
1483
1484 ins.objectid = inode->i_ino;
1485 ins.offset = file_pos;
1486 ins.type = BTRFS_EXTENT_DATA_KEY;
1487 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1488 BUG_ON(ret);
1489 leaf = path->nodes[0];
1490 fi = btrfs_item_ptr(leaf, path->slots[0],
1491 struct btrfs_file_extent_item);
1492 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1493 btrfs_set_file_extent_type(leaf, fi, extent_type);
1494 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1495 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1496 btrfs_set_file_extent_offset(leaf, fi, 0);
1497 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1498 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1499 btrfs_set_file_extent_compression(leaf, fi, compression);
1500 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1501 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
1502 btrfs_mark_buffer_dirty(leaf);
1503
1504 inode_add_bytes(inode, num_bytes);
1505 btrfs_drop_extent_cache(inode, file_pos, file_pos + num_bytes - 1, 0);
1506
1507 ins.objectid = disk_bytenr;
1508 ins.offset = disk_num_bytes;
1509 ins.type = BTRFS_EXTENT_ITEM_KEY;
1510 ret = btrfs_alloc_reserved_extent(trans, root, leaf->start,
1511 root->root_key.objectid,
1512 trans->transid, inode->i_ino, &ins);
1513 BUG_ON(ret);
1514
1515 btrfs_free_path(path);
1516 return 0;
1517}
1518
Chris Masond352ac62008-09-29 15:18:18 -04001519/* as ordered data IO finishes, this gets called so we can finish
1520 * an ordered extent if the range of bytes in the file it covers are
1521 * fully written.
1522 */
Chris Mason211f90e2008-07-18 11:56:15 -04001523static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001524{
Chris Masone6dcd2d2008-07-17 12:53:50 -04001525 struct btrfs_root *root = BTRFS_I(inode)->root;
1526 struct btrfs_trans_handle *trans;
1527 struct btrfs_ordered_extent *ordered_extent;
1528 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan Zhengd899e052008-10-30 14:25:28 -04001529 int compressed = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001530 int ret;
1531
1532 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -04001533 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -04001534 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001535
Chris Masonf9295742008-07-17 12:54:14 -04001536 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001537
1538 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
1539 BUG_ON(!ordered_extent);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001540 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
1541 goto nocow;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001542
1543 lock_extent(io_tree, ordered_extent->file_offset,
1544 ordered_extent->file_offset + ordered_extent->len - 1,
1545 GFP_NOFS);
1546
Chris Masonc8b97812008-10-29 14:49:59 -04001547 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Yan Zhengd899e052008-10-30 14:25:28 -04001548 compressed = 1;
1549 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1550 BUG_ON(compressed);
1551 ret = btrfs_mark_extent_written(trans, root, inode,
1552 ordered_extent->file_offset,
1553 ordered_extent->file_offset +
1554 ordered_extent->len);
1555 BUG_ON(ret);
1556 } else {
1557 ret = insert_reserved_file_extent(trans, inode,
1558 ordered_extent->file_offset,
1559 ordered_extent->start,
1560 ordered_extent->disk_len,
1561 ordered_extent->len,
1562 ordered_extent->len,
1563 compressed, 0, 0,
1564 BTRFS_FILE_EXTENT_REG);
1565 BUG_ON(ret);
1566 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001567 unlock_extent(io_tree, ordered_extent->file_offset,
1568 ordered_extent->file_offset + ordered_extent->len - 1,
1569 GFP_NOFS);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001570nocow:
Chris Masone6dcd2d2008-07-17 12:53:50 -04001571 add_pending_csums(trans, inode, ordered_extent->file_offset,
1572 &ordered_extent->list);
1573
Chris Mason34353022008-09-23 20:19:49 -04001574 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masondbe674a2008-07-17 12:54:05 -04001575 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone02119d2008-09-05 16:13:11 -04001576 btrfs_update_inode(trans, root, inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001577 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason34353022008-09-23 20:19:49 -04001578 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -04001579
Chris Masone6dcd2d2008-07-17 12:53:50 -04001580 /* once for us */
1581 btrfs_put_ordered_extent(ordered_extent);
1582 /* once for the tree */
1583 btrfs_put_ordered_extent(ordered_extent);
1584
Chris Masone6dcd2d2008-07-17 12:53:50 -04001585 btrfs_end_transaction(trans, root);
1586 return 0;
1587}
1588
Christoph Hellwigb2950862008-12-02 09:54:17 -05001589static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason211f90e2008-07-18 11:56:15 -04001590 struct extent_state *state, int uptodate)
1591{
1592 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1593}
1594
Chris Masond352ac62008-09-29 15:18:18 -04001595/*
1596 * When IO fails, either with EIO or csum verification fails, we
1597 * try other mirrors that might have a good copy of the data. This
1598 * io_failure_record is used to record state as we go through all the
1599 * mirrors. If another mirror has good data, the page is set up to date
1600 * and things continue. If a good mirror can't be found, the original
1601 * bio end_io callback is called to indicate things have failed.
1602 */
Chris Mason7e383262008-04-09 16:28:12 -04001603struct io_failure_record {
1604 struct page *page;
1605 u64 start;
1606 u64 len;
1607 u64 logical;
Chris Masond20f7042008-12-08 16:58:54 -05001608 unsigned long bio_flags;
Chris Mason7e383262008-04-09 16:28:12 -04001609 int last_mirror;
1610};
1611
Christoph Hellwigb2950862008-12-02 09:54:17 -05001612static int btrfs_io_failed_hook(struct bio *failed_bio,
Chris Mason1259ab72008-05-12 13:39:03 -04001613 struct page *page, u64 start, u64 end,
1614 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -04001615{
1616 struct io_failure_record *failrec = NULL;
1617 u64 private;
1618 struct extent_map *em;
1619 struct inode *inode = page->mapping->host;
1620 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -04001621 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -04001622 struct bio *bio;
1623 int num_copies;
1624 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -04001625 int rw;
Chris Mason7e383262008-04-09 16:28:12 -04001626 u64 logical;
1627
1628 ret = get_state_private(failure_tree, start, &private);
1629 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -04001630 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1631 if (!failrec)
1632 return -ENOMEM;
1633 failrec->start = start;
1634 failrec->len = end - start + 1;
1635 failrec->last_mirror = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001636 failrec->bio_flags = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001637
Chris Mason3b951512008-04-17 11:29:12 -04001638 spin_lock(&em_tree->lock);
1639 em = lookup_extent_mapping(em_tree, start, failrec->len);
1640 if (em->start > start || em->start + em->len < start) {
1641 free_extent_map(em);
1642 em = NULL;
1643 }
1644 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -04001645
1646 if (!em || IS_ERR(em)) {
1647 kfree(failrec);
1648 return -EIO;
1649 }
1650 logical = start - em->start;
1651 logical = em->block_start + logical;
Chris Masond20f7042008-12-08 16:58:54 -05001652 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1653 logical = em->block_start;
1654 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1655 }
Chris Mason7e383262008-04-09 16:28:12 -04001656 failrec->logical = logical;
1657 free_extent_map(em);
1658 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1659 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -04001660 set_state_private(failure_tree, start,
1661 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -04001662 } else {
Chris Mason587f7702008-04-11 12:16:46 -04001663 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -04001664 }
1665 num_copies = btrfs_num_copies(
1666 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1667 failrec->logical, failrec->len);
1668 failrec->last_mirror++;
1669 if (!state) {
Chris Masoncad321a2008-12-17 14:51:42 -05001670 spin_lock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001671 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1672 failrec->start,
1673 EXTENT_LOCKED);
1674 if (state && state->start != failrec->start)
1675 state = NULL;
Chris Masoncad321a2008-12-17 14:51:42 -05001676 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
Chris Mason7e383262008-04-09 16:28:12 -04001677 }
1678 if (!state || failrec->last_mirror > num_copies) {
1679 set_state_private(failure_tree, failrec->start, 0);
1680 clear_extent_bits(failure_tree, failrec->start,
1681 failrec->start + failrec->len - 1,
1682 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1683 kfree(failrec);
1684 return -EIO;
1685 }
1686 bio = bio_alloc(GFP_NOFS, 1);
1687 bio->bi_private = state;
1688 bio->bi_end_io = failed_bio->bi_end_io;
1689 bio->bi_sector = failrec->logical >> 9;
1690 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -04001691 bio->bi_size = 0;
Chris Masond20f7042008-12-08 16:58:54 -05001692
Chris Mason7e383262008-04-09 16:28:12 -04001693 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -04001694 if (failed_bio->bi_rw & (1 << BIO_RW))
1695 rw = WRITE;
1696 else
1697 rw = READ;
1698
1699 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
Chris Masonc8b97812008-10-29 14:49:59 -04001700 failrec->last_mirror,
Chris Masond20f7042008-12-08 16:58:54 -05001701 failrec->bio_flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001702 return 0;
1703}
1704
Chris Masond352ac62008-09-29 15:18:18 -04001705/*
1706 * each time an IO finishes, we do a fast check in the IO failure tree
1707 * to see if we need to process or clean up an io_failure_record
1708 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001709static int btrfs_clean_io_failures(struct inode *inode, u64 start)
Chris Mason1259ab72008-05-12 13:39:03 -04001710{
1711 u64 private;
1712 u64 private_failure;
1713 struct io_failure_record *failure;
1714 int ret;
1715
1716 private = 0;
1717 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1718 (u64)-1, 1, EXTENT_DIRTY)) {
1719 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1720 start, &private_failure);
1721 if (ret == 0) {
1722 failure = (struct io_failure_record *)(unsigned long)
1723 private_failure;
1724 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1725 failure->start, 0);
1726 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1727 failure->start,
1728 failure->start + failure->len - 1,
1729 EXTENT_DIRTY | EXTENT_LOCKED,
1730 GFP_NOFS);
1731 kfree(failure);
1732 }
1733 }
Chris Mason7e383262008-04-09 16:28:12 -04001734 return 0;
1735}
1736
Chris Masond352ac62008-09-29 15:18:18 -04001737/*
1738 * when reads are done, we need to check csums to verify the data is correct
1739 * if there's a match, we allow the bio to finish. If not, we go through
1740 * the io_failure_record routines to find good copies
1741 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001742static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
Chris Mason70dec802008-01-29 09:59:12 -05001743 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -04001744{
Chris Mason35ebb932007-10-30 16:56:53 -04001745 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -04001746 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05001747 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -04001748 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -05001749 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -04001750 int ret;
Chris Masonff79f812007-10-15 16:22:25 -04001751 struct btrfs_root *root = BTRFS_I(inode)->root;
1752 u32 csum = ~(u32)0;
Chris Masond1310b22008-01-24 16:13:08 -05001753
Chris Masond20f7042008-12-08 16:58:54 -05001754 if (PageChecked(page)) {
1755 ClearPageChecked(page);
1756 goto good;
1757 }
Yan Zheng17d217f2008-12-12 10:03:38 -05001758 if (btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -05001759 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05001760
Yan Zheng17d217f2008-12-12 10:03:38 -05001761 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
1762 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1)) {
1763 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1764 GFP_NOFS);
1765 return 0;
1766 }
1767
Yanc2e639f2008-02-04 08:57:25 -05001768 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -05001769 private = state->private;
1770 ret = 0;
1771 } else {
1772 ret = get_state_private(io_tree, start, &private);
1773 }
Chris Mason9ab86c82009-01-07 09:48:51 -05001774 kaddr = kmap_atomic(page, KM_USER0);
Chris Masond3977122009-01-05 21:25:51 -05001775 if (ret)
Chris Mason07157aa2007-08-30 08:50:51 -04001776 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001777
Chris Masonff79f812007-10-15 16:22:25 -04001778 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1779 btrfs_csum_final(csum, (char *)&csum);
Chris Masond3977122009-01-05 21:25:51 -05001780 if (csum != private)
Chris Mason07157aa2007-08-30 08:50:51 -04001781 goto zeroit;
Chris Masond3977122009-01-05 21:25:51 -05001782
Chris Mason9ab86c82009-01-07 09:48:51 -05001783 kunmap_atomic(kaddr, KM_USER0);
Chris Masond20f7042008-12-08 16:58:54 -05001784good:
Chris Mason7e383262008-04-09 16:28:12 -04001785 /* if the io failure tree for this inode is non-empty,
1786 * check to see if we've recovered from a failed IO
1787 */
Chris Mason1259ab72008-05-12 13:39:03 -04001788 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -04001789 return 0;
1790
1791zeroit:
Chris Masond3977122009-01-05 21:25:51 -05001792 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1793 "private %llu\n", page->mapping->host->i_ino,
1794 (unsigned long long)start, csum,
1795 (unsigned long long)private);
Chris Masondb945352007-10-15 16:15:53 -04001796 memset(kaddr + offset, 1, end - start + 1);
1797 flush_dcache_page(page);
Chris Mason9ab86c82009-01-07 09:48:51 -05001798 kunmap_atomic(kaddr, KM_USER0);
Chris Mason3b951512008-04-17 11:29:12 -04001799 if (private == 0)
1800 return 0;
Chris Mason7e383262008-04-09 16:28:12 -04001801 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -04001802}
Chris Masonb888db22007-08-27 16:49:44 -04001803
Josef Bacik7b128762008-07-24 12:17:14 -04001804/*
1805 * This creates an orphan entry for the given inode in case something goes
1806 * wrong in the middle of an unlink/truncate.
1807 */
1808int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
1809{
1810 struct btrfs_root *root = BTRFS_I(inode)->root;
1811 int ret = 0;
1812
Yanbcc63ab2008-07-30 16:29:20 -04001813 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001814
1815 /* already on the orphan list, we're good */
1816 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04001817 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001818 return 0;
1819 }
1820
1821 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
1822
Yanbcc63ab2008-07-30 16:29:20 -04001823 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001824
1825 /*
1826 * insert an orphan item to track this unlinked/truncated file
1827 */
1828 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
1829
1830 return ret;
1831}
1832
1833/*
1834 * We have done the truncate/delete so we can go ahead and remove the orphan
1835 * item for this particular inode.
1836 */
1837int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
1838{
1839 struct btrfs_root *root = BTRFS_I(inode)->root;
1840 int ret = 0;
1841
Yanbcc63ab2008-07-30 16:29:20 -04001842 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001843
1844 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -04001845 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001846 return 0;
1847 }
1848
1849 list_del_init(&BTRFS_I(inode)->i_orphan);
1850 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -04001851 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001852 return 0;
1853 }
1854
Yanbcc63ab2008-07-30 16:29:20 -04001855 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001856
1857 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
1858
1859 return ret;
1860}
1861
1862/*
1863 * this cleans up any orphans that may be left on the list from the last use
1864 * of this root.
1865 */
1866void btrfs_orphan_cleanup(struct btrfs_root *root)
1867{
1868 struct btrfs_path *path;
1869 struct extent_buffer *leaf;
1870 struct btrfs_item *item;
1871 struct btrfs_key key, found_key;
1872 struct btrfs_trans_handle *trans;
1873 struct inode *inode;
1874 int ret = 0, nr_unlink = 0, nr_truncate = 0;
1875
Josef Bacik7b128762008-07-24 12:17:14 -04001876 path = btrfs_alloc_path();
1877 if (!path)
1878 return;
1879 path->reada = -1;
1880
1881 key.objectid = BTRFS_ORPHAN_OBJECTID;
1882 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1883 key.offset = (u64)-1;
1884
Josef Bacik7b128762008-07-24 12:17:14 -04001885
1886 while (1) {
1887 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1888 if (ret < 0) {
1889 printk(KERN_ERR "Error searching slot for orphan: %d"
1890 "\n", ret);
1891 break;
1892 }
1893
1894 /*
1895 * if ret == 0 means we found what we were searching for, which
1896 * is weird, but possible, so only screw with path if we didnt
1897 * find the key and see if we have stuff that matches
1898 */
1899 if (ret > 0) {
1900 if (path->slots[0] == 0)
1901 break;
1902 path->slots[0]--;
1903 }
1904
1905 /* pull out the item */
1906 leaf = path->nodes[0];
1907 item = btrfs_item_nr(leaf, path->slots[0]);
1908 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1909
1910 /* make sure the item matches what we want */
1911 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
1912 break;
1913 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
1914 break;
1915
1916 /* release the path since we're done with it */
1917 btrfs_release_path(root, path);
1918
1919 /*
1920 * this is where we are basically btrfs_lookup, without the
1921 * crossing root thing. we store the inode number in the
1922 * offset of the orphan item.
1923 */
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001924 inode = btrfs_iget_locked(root->fs_info->sb,
Josef Bacik7b128762008-07-24 12:17:14 -04001925 found_key.offset, root);
1926 if (!inode)
1927 break;
1928
1929 if (inode->i_state & I_NEW) {
1930 BTRFS_I(inode)->root = root;
1931
1932 /* have to set the location manually */
1933 BTRFS_I(inode)->location.objectid = inode->i_ino;
1934 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
1935 BTRFS_I(inode)->location.offset = 0;
1936
1937 btrfs_read_locked_inode(inode);
1938 unlock_new_inode(inode);
1939 }
1940
1941 /*
1942 * add this inode to the orphan list so btrfs_orphan_del does
1943 * the proper thing when we hit it
1944 */
Yanbcc63ab2008-07-30 16:29:20 -04001945 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001946 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -04001947 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001948
1949 /*
1950 * if this is a bad inode, means we actually succeeded in
1951 * removing the inode, but not the orphan record, which means
1952 * we need to manually delete the orphan since iput will just
1953 * do a destroy_inode
1954 */
1955 if (is_bad_inode(inode)) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001956 trans = btrfs_start_transaction(root, 1);
Josef Bacik7b128762008-07-24 12:17:14 -04001957 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001958 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04001959 iput(inode);
1960 continue;
1961 }
1962
1963 /* if we have links, this was a truncate, lets do that */
1964 if (inode->i_nlink) {
1965 nr_truncate++;
1966 btrfs_truncate(inode);
1967 } else {
1968 nr_unlink++;
1969 }
1970
1971 /* this will do delete_inode and everything for us */
1972 iput(inode);
1973 }
1974
1975 if (nr_unlink)
1976 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
1977 if (nr_truncate)
1978 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
1979
1980 btrfs_free_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04001981}
1982
Chris Masond352ac62008-09-29 15:18:18 -04001983/*
1984 * read an inode from the btree into the in-memory inode
1985 */
Chris Mason39279cc2007-06-12 06:35:45 -04001986void btrfs_read_locked_inode(struct inode *inode)
1987{
1988 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001989 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001990 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04001991 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04001992 struct btrfs_root *root = BTRFS_I(inode)->root;
1993 struct btrfs_key location;
1994 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04001995 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04001996 int ret;
1997
1998 path = btrfs_alloc_path();
1999 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002000 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05002001
Chris Mason39279cc2007-06-12 06:35:45 -04002002 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002003 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04002004 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04002005
Chris Mason5f39d392007-10-15 16:14:19 -04002006 leaf = path->nodes[0];
2007 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2008 struct btrfs_inode_item);
2009
2010 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2011 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2012 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2013 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04002014 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002015
2016 tspec = btrfs_inode_atime(inode_item);
2017 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2018 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2019
2020 tspec = btrfs_inode_mtime(inode_item);
2021 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2022 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2023
2024 tspec = btrfs_inode_ctime(inode_item);
2025 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2026 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2027
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002028 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04002029 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Chris Masonc3027eb2008-12-08 16:40:21 -05002030 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002031 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04002032 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002033 rdev = btrfs_inode_rdev(leaf, inode_item);
2034
Josef Bacikaec74772008-07-24 12:12:38 -04002035 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002036 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Josef Bacikaec74772008-07-24 12:12:38 -04002037
Chris Mason5f39d392007-10-15 16:14:19 -04002038 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002039
Yan Zhengd2fb3432008-12-11 16:30:39 -05002040 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2041 alloc_group_block, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002042 btrfs_free_path(path);
2043 inode_item = NULL;
2044
Chris Mason39279cc2007-06-12 06:35:45 -04002045 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04002046 case S_IFREG:
2047 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002048 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05002049 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002050 inode->i_fop = &btrfs_file_operations;
2051 inode->i_op = &btrfs_file_inode_operations;
2052 break;
2053 case S_IFDIR:
2054 inode->i_fop = &btrfs_dir_file_operations;
2055 if (root == root->fs_info->tree_root)
2056 inode->i_op = &btrfs_dir_ro_inode_operations;
2057 else
2058 inode->i_op = &btrfs_dir_inode_operations;
2059 break;
2060 case S_IFLNK:
2061 inode->i_op = &btrfs_symlink_inode_operations;
2062 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002063 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002064 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04002065 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05002066 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04002067 init_special_inode(inode, inode->i_mode, rdev);
2068 break;
Chris Mason39279cc2007-06-12 06:35:45 -04002069 }
2070 return;
2071
2072make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04002073 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002074 make_bad_inode(inode);
2075}
2076
Chris Masond352ac62008-09-29 15:18:18 -04002077/*
2078 * given a leaf and an inode, copy the inode fields into the leaf
2079 */
Chris Masone02119d2008-09-05 16:13:11 -04002080static void fill_inode_item(struct btrfs_trans_handle *trans,
2081 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04002082 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04002083 struct inode *inode)
2084{
Chris Mason5f39d392007-10-15 16:14:19 -04002085 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2086 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04002087 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04002088 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2089 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2090
2091 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2092 inode->i_atime.tv_sec);
2093 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2094 inode->i_atime.tv_nsec);
2095
2096 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2097 inode->i_mtime.tv_sec);
2098 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2099 inode->i_mtime.tv_nsec);
2100
2101 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2102 inode->i_ctime.tv_sec);
2103 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2104 inode->i_ctime.tv_nsec);
2105
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002106 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04002107 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
Chris Masonc3027eb2008-12-08 16:40:21 -05002108 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
Chris Masone02119d2008-09-05 16:13:11 -04002109 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002110 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05002111 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002112 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
Chris Mason39279cc2007-06-12 06:35:45 -04002113}
2114
Chris Masond352ac62008-09-29 15:18:18 -04002115/*
2116 * copy everything in the in-memory inode into the btree.
2117 */
Chris Masond3977122009-01-05 21:25:51 -05002118noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2119 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002120{
2121 struct btrfs_inode_item *inode_item;
2122 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04002123 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002124 int ret;
2125
2126 path = btrfs_alloc_path();
2127 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04002128 ret = btrfs_lookup_inode(trans, root, path,
2129 &BTRFS_I(inode)->location, 1);
2130 if (ret) {
2131 if (ret > 0)
2132 ret = -ENOENT;
2133 goto failed;
2134 }
2135
Chris Masonb4ce94d2009-02-04 09:25:08 -05002136 btrfs_unlock_up_safe(path, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002137 leaf = path->nodes[0];
2138 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002139 struct btrfs_inode_item);
2140
Chris Masone02119d2008-09-05 16:13:11 -04002141 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002142 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002143 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002144 ret = 0;
2145failed:
Chris Mason39279cc2007-06-12 06:35:45 -04002146 btrfs_free_path(path);
2147 return ret;
2148}
2149
2150
Chris Masond352ac62008-09-29 15:18:18 -04002151/*
2152 * unlink helper that gets used here in inode.c and in the tree logging
2153 * recovery code. It remove a link in a directory with a given name, and
2154 * also drops the back refs in the inode to the directory
2155 */
Chris Masone02119d2008-09-05 16:13:11 -04002156int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2157 struct btrfs_root *root,
2158 struct inode *dir, struct inode *inode,
2159 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04002160{
2161 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002162 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002163 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002164 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04002165 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04002166 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04002167
2168 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002169 if (!path) {
2170 ret = -ENOMEM;
2171 goto err;
2172 }
2173
Chris Mason39279cc2007-06-12 06:35:45 -04002174 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2175 name, name_len, -1);
2176 if (IS_ERR(di)) {
2177 ret = PTR_ERR(di);
2178 goto err;
2179 }
2180 if (!di) {
2181 ret = -ENOENT;
2182 goto err;
2183 }
Chris Mason5f39d392007-10-15 16:14:19 -04002184 leaf = path->nodes[0];
2185 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04002186 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04002187 if (ret)
2188 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04002189 btrfs_release_path(root, path);
2190
Josef Bacikaec74772008-07-24 12:12:38 -04002191 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04002192 inode->i_ino,
2193 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002194 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002195 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
Josef Bacikaec74772008-07-24 12:12:38 -04002196 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04002197 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04002198 goto err;
2199 }
2200
Chris Mason39279cc2007-06-12 06:35:45 -04002201 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002202 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04002203 if (IS_ERR(di)) {
2204 ret = PTR_ERR(di);
2205 goto err;
2206 }
2207 if (!di) {
2208 ret = -ENOENT;
2209 goto err;
2210 }
2211 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04002212 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04002213
Chris Masone02119d2008-09-05 16:13:11 -04002214 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2215 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04002216 BUG_ON(ret != 0 && ret != -ENOENT);
2217 if (ret != -ENOENT)
2218 BTRFS_I(dir)->log_dirty_trans = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04002219
2220 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2221 dir, index);
2222 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002223err:
2224 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04002225 if (ret)
2226 goto out;
2227
2228 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2229 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2230 btrfs_update_inode(trans, root, dir);
2231 btrfs_drop_nlink(inode);
2232 ret = btrfs_update_inode(trans, root, inode);
2233 dir->i_sb->s_dirt = 1;
2234out:
Chris Mason39279cc2007-06-12 06:35:45 -04002235 return ret;
2236}
2237
2238static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2239{
2240 struct btrfs_root *root;
2241 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04002242 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002243 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05002244 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002245
2246 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002247
2248 ret = btrfs_check_free_space(root, 1, 1);
2249 if (ret)
2250 goto fail;
2251
Chris Mason39279cc2007-06-12 06:35:45 -04002252 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002253
Chris Mason39279cc2007-06-12 06:35:45 -04002254 btrfs_set_trans_block_group(trans, dir);
Chris Masone02119d2008-09-05 16:13:11 -04002255 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2256 dentry->d_name.name, dentry->d_name.len);
Josef Bacik7b128762008-07-24 12:17:14 -04002257
2258 if (inode->i_nlink == 0)
2259 ret = btrfs_orphan_add(trans, inode);
2260
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002261 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002262
Chris Mason89ce8a62008-06-25 16:01:31 -04002263 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002264fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002265 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002266 return ret;
2267}
2268
2269static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2270{
2271 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002272 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002273 int ret;
2274 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002275 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05002276 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002277
Chris Mason3394e162008-11-17 20:42:26 -05002278 /*
2279 * the FIRST_FREE_OBJECTID check makes sure we don't try to rmdir
2280 * the root of a subvolume or snapshot
2281 */
2282 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
2283 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
Yan134d4512007-10-25 15:49:25 -04002284 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04002285 }
Yan134d4512007-10-25 15:49:25 -04002286
Chris Mason1832a6d2007-12-21 16:27:21 -05002287 ret = btrfs_check_free_space(root, 1, 1);
2288 if (ret)
2289 goto fail;
2290
Chris Mason39279cc2007-06-12 06:35:45 -04002291 trans = btrfs_start_transaction(root, 1);
2292 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002293
Josef Bacik7b128762008-07-24 12:17:14 -04002294 err = btrfs_orphan_add(trans, inode);
2295 if (err)
2296 goto fail_trans;
2297
Chris Mason39279cc2007-06-12 06:35:45 -04002298 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04002299 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2300 dentry->d_name.name, dentry->d_name.len);
Chris Masond3977122009-01-05 21:25:51 -05002301 if (!err)
Chris Masondbe674a2008-07-17 12:54:05 -04002302 btrfs_i_size_write(inode, 0);
Chris Mason39544012007-12-12 14:38:19 -05002303
Josef Bacik7b128762008-07-24 12:17:14 -04002304fail_trans:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002305 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002306 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002307fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002308 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05002309
Chris Mason39279cc2007-06-12 06:35:45 -04002310 if (ret && !err)
2311 err = ret;
2312 return err;
2313}
2314
Chris Masond20f7042008-12-08 16:58:54 -05002315#if 0
Chris Mason39279cc2007-06-12 06:35:45 -04002316/*
Chris Mason323ac952008-10-01 19:05:46 -04002317 * when truncating bytes in a file, it is possible to avoid reading
2318 * the leaves that contain only checksum items. This can be the
2319 * majority of the IO required to delete a large file, but it must
2320 * be done carefully.
2321 *
2322 * The keys in the level just above the leaves are checked to make sure
2323 * the lowest key in a given leaf is a csum key, and starts at an offset
2324 * after the new size.
2325 *
2326 * Then the key for the next leaf is checked to make sure it also has
2327 * a checksum item for the same file. If it does, we know our target leaf
2328 * contains only checksum items, and it can be safely freed without reading
2329 * it.
2330 *
2331 * This is just an optimization targeted at large files. It may do
2332 * nothing. It will return 0 unless things went badly.
2333 */
2334static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
2335 struct btrfs_root *root,
2336 struct btrfs_path *path,
2337 struct inode *inode, u64 new_size)
2338{
2339 struct btrfs_key key;
2340 int ret;
2341 int nritems;
2342 struct btrfs_key found_key;
2343 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002344 struct btrfs_leaf_ref *ref;
2345 u64 leaf_gen;
2346 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04002347
2348 path->lowest_level = 1;
2349 key.objectid = inode->i_ino;
2350 key.type = BTRFS_CSUM_ITEM_KEY;
2351 key.offset = new_size;
2352again:
2353 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2354 if (ret < 0)
2355 goto out;
2356
2357 if (path->nodes[1] == NULL) {
2358 ret = 0;
2359 goto out;
2360 }
2361 ret = 0;
2362 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
2363 nritems = btrfs_header_nritems(path->nodes[1]);
2364
2365 if (!nritems)
2366 goto out;
2367
2368 if (path->slots[1] >= nritems)
2369 goto next_node;
2370
2371 /* did we find a key greater than anything we want to delete? */
2372 if (found_key.objectid > inode->i_ino ||
2373 (found_key.objectid == inode->i_ino && found_key.type > key.type))
2374 goto out;
2375
2376 /* we check the next key in the node to make sure the leave contains
2377 * only checksum items. This comparison doesn't work if our
2378 * leaf is the last one in the node
2379 */
2380 if (path->slots[1] + 1 >= nritems) {
2381next_node:
2382 /* search forward from the last key in the node, this
2383 * will bring us into the next node in the tree
2384 */
2385 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
2386
2387 /* unlikely, but we inc below, so check to be safe */
2388 if (found_key.offset == (u64)-1)
2389 goto out;
2390
2391 /* search_forward needs a path with locks held, do the
2392 * search again for the original key. It is possible
2393 * this will race with a balance and return a path that
2394 * we could modify, but this drop is just an optimization
2395 * and is allowed to miss some leaves.
2396 */
2397 btrfs_release_path(root, path);
2398 found_key.offset++;
2399
2400 /* setup a max key for search_forward */
2401 other_key.offset = (u64)-1;
2402 other_key.type = key.type;
2403 other_key.objectid = key.objectid;
2404
2405 path->keep_locks = 1;
2406 ret = btrfs_search_forward(root, &found_key, &other_key,
2407 path, 0, 0);
2408 path->keep_locks = 0;
2409 if (ret || found_key.objectid != key.objectid ||
2410 found_key.type != key.type) {
2411 ret = 0;
2412 goto out;
2413 }
2414
2415 key.offset = found_key.offset;
2416 btrfs_release_path(root, path);
2417 cond_resched();
2418 goto again;
2419 }
2420
2421 /* we know there's one more slot after us in the tree,
2422 * read that key so we can verify it is also a checksum item
2423 */
2424 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
2425
2426 if (found_key.objectid < inode->i_ino)
2427 goto next_key;
2428
2429 if (found_key.type != key.type || found_key.offset < new_size)
2430 goto next_key;
2431
2432 /*
2433 * if the key for the next leaf isn't a csum key from this objectid,
2434 * we can't be sure there aren't good items inside this leaf.
2435 * Bail out
2436 */
2437 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
2438 goto out;
2439
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002440 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
2441 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04002442 /*
2443 * it is safe to delete this leaf, it contains only
2444 * csum items from this inode at an offset >= new_size
2445 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002446 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04002447 BUG_ON(ret);
2448
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002449 if (root->ref_cows && leaf_gen < trans->transid) {
2450 ref = btrfs_alloc_leaf_ref(root, 0);
2451 if (ref) {
2452 ref->root_gen = root->root_key.offset;
2453 ref->bytenr = leaf_start;
2454 ref->owner = 0;
2455 ref->generation = leaf_gen;
2456 ref->nritems = 0;
2457
Chris Masonbd56b302009-02-04 09:27:02 -05002458 btrfs_sort_leaf_ref(ref);
2459
Yan Zheng5b84e8d2008-10-09 11:46:19 -04002460 ret = btrfs_add_leaf_ref(root, ref, 0);
2461 WARN_ON(ret);
2462 btrfs_free_leaf_ref(root, ref);
2463 } else {
2464 WARN_ON(1);
2465 }
2466 }
Chris Mason323ac952008-10-01 19:05:46 -04002467next_key:
2468 btrfs_release_path(root, path);
2469
2470 if (other_key.objectid == inode->i_ino &&
2471 other_key.type == key.type && other_key.offset > key.offset) {
2472 key.offset = other_key.offset;
2473 cond_resched();
2474 goto again;
2475 }
2476 ret = 0;
2477out:
2478 /* fixup any changes we've made to the path */
2479 path->lowest_level = 0;
2480 path->keep_locks = 0;
2481 btrfs_release_path(root, path);
2482 return ret;
2483}
2484
Chris Masond20f7042008-12-08 16:58:54 -05002485#endif
2486
Chris Mason323ac952008-10-01 19:05:46 -04002487/*
Chris Mason39279cc2007-06-12 06:35:45 -04002488 * this can truncate away extent items, csum items and directory items.
2489 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04002490 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04002491 *
2492 * csum items that cross the new i_size are truncated to the new size
2493 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04002494 *
2495 * min_type is the minimum key type to truncate down to. If set to 0, this
2496 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04002497 */
Chris Masone02119d2008-09-05 16:13:11 -04002498noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2499 struct btrfs_root *root,
2500 struct inode *inode,
2501 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002502{
2503 int ret;
2504 struct btrfs_path *path;
2505 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002506 struct btrfs_key found_key;
Chris Mason06d9a8d2009-02-04 09:30:58 -05002507 u32 found_type = (u8)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04002508 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002509 struct btrfs_file_extent_item *fi;
2510 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04002511 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002512 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002513 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05002514 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002515 int found_extent;
2516 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05002517 int pending_del_nr = 0;
2518 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04002519 int extent_type = -1;
Chris Mason771ed682008-11-06 22:02:51 -05002520 int encoding;
Chris Mason3b951512008-04-17 11:29:12 -04002521 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002522
Chris Masone02119d2008-09-05 16:13:11 -04002523 if (root->ref_cows)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002524 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002525 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04002526 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04002527 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04002528
Chris Mason39279cc2007-06-12 06:35:45 -04002529 /* FIXME, add redo link to tree so we don't leak on crash */
2530 key.objectid = inode->i_ino;
2531 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04002532 key.type = (u8)-1;
2533
Chris Mason85e21ba2008-01-29 15:11:36 -05002534 btrfs_init_path(path);
Chris Mason323ac952008-10-01 19:05:46 -04002535
Chris Mason85e21ba2008-01-29 15:11:36 -05002536search_again:
2537 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masond3977122009-01-05 21:25:51 -05002538 if (ret < 0)
Chris Mason85e21ba2008-01-29 15:11:36 -05002539 goto error;
Chris Masond3977122009-01-05 21:25:51 -05002540
Chris Mason85e21ba2008-01-29 15:11:36 -05002541 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002542 /* there are no items in the tree for us to truncate, we're
2543 * done
2544 */
2545 if (path->slots[0] == 0) {
2546 ret = 0;
2547 goto error;
2548 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002549 path->slots[0]--;
2550 }
2551
Chris Masond3977122009-01-05 21:25:51 -05002552 while (1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002553 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04002554 leaf = path->nodes[0];
2555 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2556 found_type = btrfs_key_type(&found_key);
Chris Mason771ed682008-11-06 22:02:51 -05002557 encoding = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002558
Chris Mason5f39d392007-10-15 16:14:19 -04002559 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04002560 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002561
Chris Mason85e21ba2008-01-29 15:11:36 -05002562 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002563 break;
2564
Chris Mason5f39d392007-10-15 16:14:19 -04002565 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04002566 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04002567 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04002568 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04002569 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002570 encoding = btrfs_file_extent_compression(leaf, fi);
2571 encoding |= btrfs_file_extent_encryption(leaf, fi);
2572 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
2573
Chris Mason179e29e2007-11-01 11:28:41 -04002574 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04002575 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04002576 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04002577 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason179e29e2007-11-01 11:28:41 -04002578 item_end += btrfs_file_extent_inline_len(leaf,
Chris Masonc8b97812008-10-29 14:49:59 -04002579 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002580 }
Yan008630c2007-11-07 13:31:09 -05002581 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04002582 }
Chris Masone02119d2008-09-05 16:13:11 -04002583 if (item_end < new_size) {
Chris Masond3977122009-01-05 21:25:51 -05002584 if (found_type == BTRFS_DIR_ITEM_KEY)
Chris Masonb888db22007-08-27 16:49:44 -04002585 found_type = BTRFS_INODE_ITEM_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002586 else if (found_type == BTRFS_EXTENT_ITEM_KEY)
Chris Masond20f7042008-12-08 16:58:54 -05002587 found_type = BTRFS_EXTENT_DATA_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002588 else if (found_type == BTRFS_EXTENT_DATA_KEY)
Chris Mason85e21ba2008-01-29 15:11:36 -05002589 found_type = BTRFS_XATTR_ITEM_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002590 else if (found_type == BTRFS_XATTR_ITEM_KEY)
Chris Mason85e21ba2008-01-29 15:11:36 -05002591 found_type = BTRFS_INODE_REF_KEY;
Chris Masond3977122009-01-05 21:25:51 -05002592 else if (found_type)
Chris Masonb888db22007-08-27 16:49:44 -04002593 found_type--;
Chris Masond3977122009-01-05 21:25:51 -05002594 else
Chris Masonb888db22007-08-27 16:49:44 -04002595 break;
Yana61721d2007-09-17 11:08:38 -04002596 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05002597 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04002598 }
Chris Masone02119d2008-09-05 16:13:11 -04002599 if (found_key.offset >= new_size)
Chris Mason39279cc2007-06-12 06:35:45 -04002600 del_item = 1;
2601 else
2602 del_item = 0;
2603 found_extent = 0;
2604
2605 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04002606 if (found_type != BTRFS_EXTENT_DATA_KEY)
2607 goto delete;
2608
2609 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04002610 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04002611 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason771ed682008-11-06 22:02:51 -05002612 if (!del_item && !encoding) {
Chris Masondb945352007-10-15 16:15:53 -04002613 u64 orig_num_bytes =
2614 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04002615 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04002616 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05002617 extent_num_bytes = extent_num_bytes &
2618 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04002619 btrfs_set_file_extent_num_bytes(leaf, fi,
2620 extent_num_bytes);
2621 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05002622 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04002623 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002624 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04002625 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002626 } else {
Chris Masondb945352007-10-15 16:15:53 -04002627 extent_num_bytes =
2628 btrfs_file_extent_disk_num_bytes(leaf,
2629 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002630 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05002631 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04002632 if (extent_start != 0) {
2633 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04002634 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002635 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04002636 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002637 root_gen = btrfs_header_generation(leaf);
Chris Masond8d5f3e2007-12-11 12:42:00 -05002638 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002639 }
Chris Mason90692182008-02-08 13:49:28 -05002640 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002641 /*
2642 * we can't truncate inline items that have had
2643 * special encodings
2644 */
2645 if (!del_item &&
2646 btrfs_file_extent_compression(leaf, fi) == 0 &&
2647 btrfs_file_extent_encryption(leaf, fi) == 0 &&
2648 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002649 u32 size = new_size - found_key.offset;
2650
2651 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002652 inode_sub_bytes(inode, item_end + 1 -
2653 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04002654 }
2655 size =
2656 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05002657 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04002658 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05002659 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04002660 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002661 inode_sub_bytes(inode, item_end + 1 -
2662 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05002663 }
Chris Mason39279cc2007-06-12 06:35:45 -04002664 }
Chris Mason179e29e2007-11-01 11:28:41 -04002665delete:
Chris Mason39279cc2007-06-12 06:35:45 -04002666 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05002667 if (!pending_del_nr) {
2668 /* no pending yet, add ourselves */
2669 pending_del_slot = path->slots[0];
2670 pending_del_nr = 1;
2671 } else if (pending_del_nr &&
2672 path->slots[0] + 1 == pending_del_slot) {
2673 /* hop on the pending chunk */
2674 pending_del_nr++;
2675 pending_del_slot = path->slots[0];
2676 } else {
Chris Masond3977122009-01-05 21:25:51 -05002677 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05002678 }
Chris Mason39279cc2007-06-12 06:35:45 -04002679 } else {
2680 break;
2681 }
Chris Mason39279cc2007-06-12 06:35:45 -04002682 if (found_extent) {
2683 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05002684 extent_num_bytes,
Zheng Yan31840ae2008-09-23 13:14:14 -04002685 leaf->start, root_owner,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002686 root_gen, inode->i_ino, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002687 BUG_ON(ret);
2688 }
Chris Mason85e21ba2008-01-29 15:11:36 -05002689next:
2690 if (path->slots[0] == 0) {
2691 if (pending_del_nr)
2692 goto del_pending;
2693 btrfs_release_path(root, path);
Chris Mason06d9a8d2009-02-04 09:30:58 -05002694 if (found_type == BTRFS_INODE_ITEM_KEY)
2695 break;
Chris Mason85e21ba2008-01-29 15:11:36 -05002696 goto search_again;
2697 }
2698
2699 path->slots[0]--;
2700 if (pending_del_nr &&
2701 path->slots[0] + 1 != pending_del_slot) {
2702 struct btrfs_key debug;
2703del_pending:
2704 btrfs_item_key_to_cpu(path->nodes[0], &debug,
2705 pending_del_slot);
2706 ret = btrfs_del_items(trans, root, path,
2707 pending_del_slot,
2708 pending_del_nr);
2709 BUG_ON(ret);
2710 pending_del_nr = 0;
2711 btrfs_release_path(root, path);
Chris Mason06d9a8d2009-02-04 09:30:58 -05002712 if (found_type == BTRFS_INODE_ITEM_KEY)
2713 break;
Chris Mason85e21ba2008-01-29 15:11:36 -05002714 goto search_again;
2715 }
Chris Mason39279cc2007-06-12 06:35:45 -04002716 }
2717 ret = 0;
2718error:
Chris Mason85e21ba2008-01-29 15:11:36 -05002719 if (pending_del_nr) {
2720 ret = btrfs_del_items(trans, root, path, pending_del_slot,
2721 pending_del_nr);
2722 }
Chris Mason39279cc2007-06-12 06:35:45 -04002723 btrfs_free_path(path);
2724 inode->i_sb->s_dirt = 1;
2725 return ret;
2726}
2727
Chris Masona52d9a82007-08-27 16:49:44 -04002728/*
2729 * taken from block_truncate_page, but does cow as it zeros out
2730 * any bytes left in the last page in the file.
2731 */
2732static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
2733{
2734 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04002735 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002736 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2737 struct btrfs_ordered_extent *ordered;
2738 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04002739 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04002740 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2741 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2742 struct page *page;
2743 int ret = 0;
2744 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002745 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04002746
2747 if ((offset & (blocksize - 1)) == 0)
2748 goto out;
2749
2750 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04002751again:
Chris Masona52d9a82007-08-27 16:49:44 -04002752 page = grab_cache_page(mapping, index);
2753 if (!page)
2754 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002755
2756 page_start = page_offset(page);
2757 page_end = page_start + PAGE_CACHE_SIZE - 1;
2758
Chris Masona52d9a82007-08-27 16:49:44 -04002759 if (!PageUptodate(page)) {
2760 ret = btrfs_readpage(NULL, page);
2761 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04002762 if (page->mapping != mapping) {
2763 unlock_page(page);
2764 page_cache_release(page);
2765 goto again;
2766 }
Chris Masona52d9a82007-08-27 16:49:44 -04002767 if (!PageUptodate(page)) {
2768 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04002769 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04002770 }
2771 }
Chris Mason211c17f2008-05-15 09:13:45 -04002772 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002773
2774 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2775 set_page_extent_mapped(page);
2776
2777 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2778 if (ordered) {
2779 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2780 unlock_page(page);
2781 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04002782 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002783 btrfs_put_ordered_extent(ordered);
2784 goto again;
2785 }
2786
Chris Masonea8c2812008-08-04 23:17:27 -04002787 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002788 ret = 0;
2789 if (offset != PAGE_CACHE_SIZE) {
2790 kaddr = kmap(page);
2791 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
2792 flush_dcache_page(page);
2793 kunmap(page);
2794 }
Chris Mason247e7432008-07-17 12:53:51 -04002795 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002796 set_page_dirty(page);
2797 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002798
Chris Mason89642222008-07-24 09:41:53 -04002799out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002800 unlock_page(page);
2801 page_cache_release(page);
2802out:
2803 return ret;
2804}
2805
Yan Zheng9036c102008-10-30 14:19:41 -04002806int btrfs_cont_expand(struct inode *inode, loff_t size)
2807{
2808 struct btrfs_trans_handle *trans;
2809 struct btrfs_root *root = BTRFS_I(inode)->root;
2810 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2811 struct extent_map *em;
2812 u64 mask = root->sectorsize - 1;
2813 u64 hole_start = (inode->i_size + mask) & ~mask;
2814 u64 block_end = (size + mask) & ~mask;
2815 u64 last_byte;
2816 u64 cur_offset;
2817 u64 hole_size;
2818 int err;
2819
2820 if (size <= hole_start)
2821 return 0;
2822
2823 err = btrfs_check_free_space(root, 1, 0);
2824 if (err)
2825 return err;
2826
2827 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2828
2829 while (1) {
2830 struct btrfs_ordered_extent *ordered;
2831 btrfs_wait_ordered_range(inode, hole_start,
2832 block_end - hole_start);
2833 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2834 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
2835 if (!ordered)
2836 break;
2837 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2838 btrfs_put_ordered_extent(ordered);
2839 }
2840
2841 trans = btrfs_start_transaction(root, 1);
2842 btrfs_set_trans_block_group(trans, inode);
2843
2844 cur_offset = hole_start;
2845 while (1) {
2846 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
2847 block_end - cur_offset, 0);
2848 BUG_ON(IS_ERR(em) || !em);
2849 last_byte = min(extent_map_end(em), block_end);
2850 last_byte = (last_byte + mask) & ~mask;
2851 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
Chris Mason771ed682008-11-06 22:02:51 -05002852 u64 hint_byte = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04002853 hole_size = last_byte - cur_offset;
Chris Mason771ed682008-11-06 22:02:51 -05002854 err = btrfs_drop_extents(trans, root, inode,
2855 cur_offset,
2856 cur_offset + hole_size,
2857 cur_offset, &hint_byte);
2858 if (err)
2859 break;
Yan Zheng9036c102008-10-30 14:19:41 -04002860 err = btrfs_insert_file_extent(trans, root,
2861 inode->i_ino, cur_offset, 0,
2862 0, hole_size, 0, hole_size,
2863 0, 0, 0);
2864 btrfs_drop_extent_cache(inode, hole_start,
2865 last_byte - 1, 0);
2866 }
2867 free_extent_map(em);
2868 cur_offset = last_byte;
2869 if (err || cur_offset >= block_end)
2870 break;
2871 }
2872
2873 btrfs_end_transaction(trans, root);
2874 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2875 return err;
2876}
2877
Chris Mason39279cc2007-06-12 06:35:45 -04002878static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
2879{
2880 struct inode *inode = dentry->d_inode;
2881 int err;
2882
2883 err = inode_change_ok(inode, attr);
2884 if (err)
2885 return err;
2886
2887 if (S_ISREG(inode->i_mode) &&
2888 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
Yan Zheng9036c102008-10-30 14:19:41 -04002889 err = btrfs_cont_expand(inode, attr->ia_size);
Chris Mason54aa1f42007-06-22 14:16:25 -04002890 if (err)
2891 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04002892 }
Yan Zheng9036c102008-10-30 14:19:41 -04002893
Chris Mason39279cc2007-06-12 06:35:45 -04002894 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04002895
2896 if (!err && ((attr->ia_valid & ATTR_MODE)))
2897 err = btrfs_acl_chmod(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002898 return err;
2899}
Chris Mason61295eb2008-01-14 16:24:38 -05002900
Chris Mason39279cc2007-06-12 06:35:45 -04002901void btrfs_delete_inode(struct inode *inode)
2902{
2903 struct btrfs_trans_handle *trans;
2904 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002905 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002906 int ret;
2907
2908 truncate_inode_pages(&inode->i_data, 0);
2909 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04002910 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002911 goto no_delete;
2912 }
Chris Mason4a096752008-07-21 10:29:44 -04002913 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04002914
Chris Masondbe674a2008-07-17 12:54:05 -04002915 btrfs_i_size_write(inode, 0);
Yan Zheng180591b2009-01-06 09:58:06 -05002916 trans = btrfs_join_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002917
Chris Mason39279cc2007-06-12 06:35:45 -04002918 btrfs_set_trans_block_group(trans, inode);
Chris Masone02119d2008-09-05 16:13:11 -04002919 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04002920 if (ret) {
2921 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002922 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04002923 }
2924
2925 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05002926
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002927 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05002928 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002929
Chris Mason39279cc2007-06-12 06:35:45 -04002930 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002931 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002932 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04002933
2934no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002935 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002936 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002937 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002938no_delete:
2939 clear_inode(inode);
2940}
2941
2942/*
2943 * this returns the key found in the dir entry in the location pointer.
2944 * If no dir entries were found, location->objectid is 0.
2945 */
2946static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
2947 struct btrfs_key *location)
2948{
2949 const char *name = dentry->d_name.name;
2950 int namelen = dentry->d_name.len;
2951 struct btrfs_dir_item *di;
2952 struct btrfs_path *path;
2953 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04002954 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002955
2956 path = btrfs_alloc_path();
2957 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05002958
Chris Mason39279cc2007-06-12 06:35:45 -04002959 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
2960 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04002961 if (IS_ERR(di))
2962 ret = PTR_ERR(di);
Chris Masond3977122009-01-05 21:25:51 -05002963
2964 if (!di || IS_ERR(di))
Chris Mason39544012007-12-12 14:38:19 -05002965 goto out_err;
Chris Masond3977122009-01-05 21:25:51 -05002966
Chris Mason5f39d392007-10-15 16:14:19 -04002967 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04002968out:
Chris Mason39279cc2007-06-12 06:35:45 -04002969 btrfs_free_path(path);
2970 return ret;
Chris Mason39544012007-12-12 14:38:19 -05002971out_err:
2972 location->objectid = 0;
2973 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04002974}
2975
2976/*
2977 * when we hit a tree root in a directory, the btrfs part of the inode
2978 * needs to be changed to reflect the root directory of the tree root. This
2979 * is kind of like crossing a mount point.
2980 */
2981static int fixup_tree_root_location(struct btrfs_root *root,
2982 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04002983 struct btrfs_root **sub_root,
2984 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04002985{
Chris Mason39279cc2007-06-12 06:35:45 -04002986 struct btrfs_root_item *ri;
2987
2988 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
2989 return 0;
2990 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
2991 return 0;
2992
Josef Bacik58176a92007-08-29 15:47:34 -04002993 *sub_root = btrfs_read_fs_root(root->fs_info, location,
2994 dentry->d_name.name,
2995 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04002996 if (IS_ERR(*sub_root))
2997 return PTR_ERR(*sub_root);
2998
2999 ri = &(*sub_root)->root_item;
3000 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04003001 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
3002 location->offset = 0;
3003
Chris Mason39279cc2007-06-12 06:35:45 -04003004 return 0;
3005}
3006
Chris Masone02119d2008-09-05 16:13:11 -04003007static noinline void init_btrfs_i(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003008{
Chris Masone02119d2008-09-05 16:13:11 -04003009 struct btrfs_inode *bi = BTRFS_I(inode);
3010
3011 bi->i_acl = NULL;
3012 bi->i_default_acl = NULL;
3013
3014 bi->generation = 0;
Chris Masonc3027eb2008-12-08 16:40:21 -05003015 bi->sequence = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003016 bi->last_trans = 0;
3017 bi->logged_trans = 0;
3018 bi->delalloc_bytes = 0;
3019 bi->disk_i_size = 0;
3020 bi->flags = 0;
3021 bi->index_cnt = (u64)-1;
Chris Mason49eb7e42008-09-11 15:53:12 -04003022 bi->log_dirty_trans = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003023 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3024 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04003025 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003026 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3027 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04003028 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Masonba1da2f2008-07-17 12:54:15 -04003029 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Masonee6e6502008-07-17 12:54:40 -04003030 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003031 mutex_init(&BTRFS_I(inode)->log_mutex);
3032}
3033
3034static int btrfs_init_locked_inode(struct inode *inode, void *p)
3035{
3036 struct btrfs_iget_args *args = p;
3037 inode->i_ino = args->ino;
3038 init_btrfs_i(inode);
3039 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003040 return 0;
3041}
3042
3043static int btrfs_find_actor(struct inode *inode, void *opaque)
3044{
3045 struct btrfs_iget_args *args = opaque;
Chris Masond3977122009-01-05 21:25:51 -05003046 return args->ino == inode->i_ino &&
3047 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003048}
3049
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003050struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
3051 struct btrfs_root *root, int wait)
3052{
3053 struct inode *inode;
3054 struct btrfs_iget_args args;
3055 args.ino = objectid;
3056 args.root = root;
3057
3058 if (wait) {
3059 inode = ilookup5(s, objectid, btrfs_find_actor,
3060 (void *)&args);
3061 } else {
3062 inode = ilookup5_nowait(s, objectid, btrfs_find_actor,
3063 (void *)&args);
3064 }
3065 return inode;
3066}
3067
Chris Mason39279cc2007-06-12 06:35:45 -04003068struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
3069 struct btrfs_root *root)
3070{
3071 struct inode *inode;
3072 struct btrfs_iget_args args;
3073 args.ino = objectid;
3074 args.root = root;
3075
3076 inode = iget5_locked(s, objectid, btrfs_find_actor,
3077 btrfs_init_locked_inode,
3078 (void *)&args);
3079 return inode;
3080}
3081
Balaji Rao1a54ef82008-07-21 02:01:04 +05303082/* Get an inode object given its location and corresponding root.
3083 * Returns in *is_new if the inode was read from disk
3084 */
3085struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
3086 struct btrfs_root *root, int *is_new)
3087{
3088 struct inode *inode;
3089
3090 inode = btrfs_iget_locked(s, location->objectid, root);
3091 if (!inode)
3092 return ERR_PTR(-EACCES);
3093
3094 if (inode->i_state & I_NEW) {
3095 BTRFS_I(inode)->root = root;
3096 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
3097 btrfs_read_locked_inode(inode);
3098 unlock_new_inode(inode);
3099 if (is_new)
3100 *is_new = 1;
3101 } else {
3102 if (is_new)
3103 *is_new = 0;
3104 }
3105
3106 return inode;
3107}
3108
Chris Mason3de45862008-11-17 21:02:50 -05003109struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04003110{
Chris Masond3977122009-01-05 21:25:51 -05003111 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003112 struct btrfs_inode *bi = BTRFS_I(dir);
3113 struct btrfs_root *root = bi->root;
3114 struct btrfs_root *sub_root = root;
3115 struct btrfs_key location;
Yan Zhengc146afa2008-11-12 14:34:12 -05003116 int ret, new;
Chris Mason39279cc2007-06-12 06:35:45 -04003117
3118 if (dentry->d_name.len > BTRFS_NAME_LEN)
3119 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04003120
Chris Mason39279cc2007-06-12 06:35:45 -04003121 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04003122
Chris Mason39279cc2007-06-12 06:35:45 -04003123 if (ret < 0)
3124 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04003125
Chris Mason39279cc2007-06-12 06:35:45 -04003126 inode = NULL;
3127 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04003128 ret = fixup_tree_root_location(root, &location, &sub_root,
3129 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04003130 if (ret < 0)
3131 return ERR_PTR(ret);
3132 if (ret > 0)
3133 return ERR_PTR(-ENOENT);
Balaji Rao1a54ef82008-07-21 02:01:04 +05303134 inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
3135 if (IS_ERR(inode))
3136 return ERR_CAST(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003137 }
Chris Mason3de45862008-11-17 21:02:50 -05003138 return inode;
3139}
3140
3141static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
3142 struct nameidata *nd)
3143{
3144 struct inode *inode;
3145
3146 if (dentry->d_name.len > BTRFS_NAME_LEN)
3147 return ERR_PTR(-ENAMETOOLONG);
3148
3149 inode = btrfs_lookup_dentry(dir, dentry);
3150 if (IS_ERR(inode))
3151 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003152
Chris Mason39279cc2007-06-12 06:35:45 -04003153 return d_splice_alias(inode, dentry);
3154}
3155
Chris Mason39279cc2007-06-12 06:35:45 -04003156static unsigned char btrfs_filetype_table[] = {
3157 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
3158};
3159
David Woodhousecbdf5a22008-08-06 19:42:33 +01003160static int btrfs_real_readdir(struct file *filp, void *dirent,
3161 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04003162{
Chris Mason6da6aba2007-12-18 16:15:09 -05003163 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003164 struct btrfs_root *root = BTRFS_I(inode)->root;
3165 struct btrfs_item *item;
3166 struct btrfs_dir_item *di;
3167 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003168 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04003169 struct btrfs_path *path;
3170 int ret;
3171 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003172 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003173 int slot;
3174 int advance;
3175 unsigned char d_type;
3176 int over = 0;
3177 u32 di_cur;
3178 u32 di_total;
3179 u32 di_len;
3180 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003181 char tmp_name[32];
3182 char *name_ptr;
3183 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04003184
3185 /* FIXME, use a real flag for deciding about the key type */
3186 if (root->fs_info->tree_root == root)
3187 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04003188
Chris Mason39544012007-12-12 14:38:19 -05003189 /* special case for "." */
3190 if (filp->f_pos == 0) {
3191 over = filldir(dirent, ".", 1,
3192 1, inode->i_ino,
3193 DT_DIR);
3194 if (over)
3195 return 0;
3196 filp->f_pos = 1;
3197 }
Chris Mason39544012007-12-12 14:38:19 -05003198 /* special case for .., just use the back ref */
3199 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003200 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05003201 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01003202 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05003203 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01003204 return 0;
Chris Mason39544012007-12-12 14:38:19 -05003205 filp->f_pos = 2;
3206 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003207 path = btrfs_alloc_path();
3208 path->reada = 2;
3209
Chris Mason39279cc2007-06-12 06:35:45 -04003210 btrfs_set_key_type(&key, key_type);
3211 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01003212 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04003213
Chris Mason39279cc2007-06-12 06:35:45 -04003214 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3215 if (ret < 0)
3216 goto err;
3217 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01003218
3219 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04003220 leaf = path->nodes[0];
3221 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003222 slot = path->slots[0];
3223 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01003224 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04003225 ret = btrfs_next_leaf(root, path);
3226 if (ret)
3227 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003228 leaf = path->nodes[0];
3229 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003230 slot = path->slots[0];
3231 } else {
3232 slot++;
3233 path->slots[0]++;
3234 }
3235 }
Chris Mason3de45862008-11-17 21:02:50 -05003236
Chris Mason39279cc2007-06-12 06:35:45 -04003237 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003238 item = btrfs_item_nr(leaf, slot);
3239 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3240
3241 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04003242 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003243 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04003244 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003245 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04003246 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003247
3248 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01003249
Chris Mason39279cc2007-06-12 06:35:45 -04003250 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
3251 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003252 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01003253
3254 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04003255 struct btrfs_key location;
3256
3257 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01003258 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04003259 name_ptr = tmp_name;
3260 } else {
3261 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01003262 if (!name_ptr) {
3263 ret = -ENOMEM;
3264 goto err;
3265 }
Chris Mason5f39d392007-10-15 16:14:19 -04003266 }
3267 read_extent_buffer(leaf, name_ptr,
3268 (unsigned long)(di + 1), name_len);
3269
3270 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
3271 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason3de45862008-11-17 21:02:50 -05003272
3273 /* is this a reference to our own snapshot? If so
3274 * skip it
3275 */
3276 if (location.type == BTRFS_ROOT_ITEM_KEY &&
3277 location.objectid == root->root_key.objectid) {
3278 over = 0;
3279 goto skip;
3280 }
Chris Mason5f39d392007-10-15 16:14:19 -04003281 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01003282 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003283 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04003284
Chris Mason3de45862008-11-17 21:02:50 -05003285skip:
Chris Mason5f39d392007-10-15 16:14:19 -04003286 if (name_ptr != tmp_name)
3287 kfree(name_ptr);
3288
Chris Mason39279cc2007-06-12 06:35:45 -04003289 if (over)
3290 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05003291 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01003292 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04003293 di_cur += di_len;
3294 di = (struct btrfs_dir_item *)((char *)di + di_len);
3295 }
3296 }
David Woodhouse49593bf2008-08-17 17:08:36 +01003297
3298 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05003299 if (key_type == BTRFS_DIR_INDEX_KEY)
Chris Mason89f135d2009-01-28 15:34:27 -05003300 filp->f_pos = INT_LIMIT(off_t);
Yan Zheng5e591a02008-02-19 11:41:02 -05003301 else
3302 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04003303nopos:
3304 ret = 0;
3305err:
Chris Mason39279cc2007-06-12 06:35:45 -04003306 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003307 return ret;
3308}
3309
3310int btrfs_write_inode(struct inode *inode, int wait)
3311{
3312 struct btrfs_root *root = BTRFS_I(inode)->root;
3313 struct btrfs_trans_handle *trans;
3314 int ret = 0;
3315
Yan Zhengc146afa2008-11-12 14:34:12 -05003316 if (root->fs_info->btree_inode == inode)
Chris Mason4ca8b412008-08-05 13:30:48 -04003317 return 0;
3318
Chris Mason39279cc2007-06-12 06:35:45 -04003319 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04003320 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003321 btrfs_set_trans_block_group(trans, inode);
3322 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003323 }
3324 return ret;
3325}
3326
3327/*
Chris Mason54aa1f42007-06-22 14:16:25 -04003328 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04003329 * inode changes. But, it is most likely to find the inode in cache.
3330 * FIXME, needs more benchmarking...there are no reasons other than performance
3331 * to keep or drop this code.
3332 */
3333void btrfs_dirty_inode(struct inode *inode)
3334{
3335 struct btrfs_root *root = BTRFS_I(inode)->root;
3336 struct btrfs_trans_handle *trans;
3337
Chris Masonf9295742008-07-17 12:54:14 -04003338 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003339 btrfs_set_trans_block_group(trans, inode);
3340 btrfs_update_inode(trans, root, inode);
3341 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003342}
3343
Chris Masond352ac62008-09-29 15:18:18 -04003344/*
3345 * find the highest existing sequence number in a directory
3346 * and then set the in-memory index_cnt variable to reflect
3347 * free sequence numbers
3348 */
Josef Bacikaec74772008-07-24 12:12:38 -04003349static int btrfs_set_inode_index_count(struct inode *inode)
3350{
3351 struct btrfs_root *root = BTRFS_I(inode)->root;
3352 struct btrfs_key key, found_key;
3353 struct btrfs_path *path;
3354 struct extent_buffer *leaf;
3355 int ret;
3356
3357 key.objectid = inode->i_ino;
3358 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
3359 key.offset = (u64)-1;
3360
3361 path = btrfs_alloc_path();
3362 if (!path)
3363 return -ENOMEM;
3364
3365 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3366 if (ret < 0)
3367 goto out;
3368 /* FIXME: we should be able to handle this */
3369 if (ret == 0)
3370 goto out;
3371 ret = 0;
3372
3373 /*
3374 * MAGIC NUMBER EXPLANATION:
3375 * since we search a directory based on f_pos we have to start at 2
3376 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
3377 * else has to start at 2
3378 */
3379 if (path->slots[0] == 0) {
3380 BTRFS_I(inode)->index_cnt = 2;
3381 goto out;
3382 }
3383
3384 path->slots[0]--;
3385
3386 leaf = path->nodes[0];
3387 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3388
3389 if (found_key.objectid != inode->i_ino ||
3390 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
3391 BTRFS_I(inode)->index_cnt = 2;
3392 goto out;
3393 }
3394
3395 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
3396out:
3397 btrfs_free_path(path);
3398 return ret;
3399}
3400
Chris Masond352ac62008-09-29 15:18:18 -04003401/*
3402 * helper to find a free sequence number in a given directory. This current
3403 * code is very simple, later versions will do smarter things in the btree
3404 */
Chris Mason3de45862008-11-17 21:02:50 -05003405int btrfs_set_inode_index(struct inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04003406{
3407 int ret = 0;
3408
3409 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
3410 ret = btrfs_set_inode_index_count(dir);
Chris Masond3977122009-01-05 21:25:51 -05003411 if (ret)
Josef Bacikaec74772008-07-24 12:12:38 -04003412 return ret;
3413 }
3414
Chris Mason00e4e6b2008-08-05 11:18:09 -04003415 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04003416 BTRFS_I(dir)->index_cnt++;
3417
3418 return ret;
3419}
3420
Chris Mason39279cc2007-06-12 06:35:45 -04003421static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
3422 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04003423 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05003424 const char *name, int name_len,
Yan Zhengd2fb3432008-12-11 16:30:39 -05003425 u64 ref_objectid, u64 objectid,
3426 u64 alloc_hint, int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04003427{
3428 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04003429 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04003430 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04003431 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05003432 struct btrfs_inode_ref *ref;
3433 struct btrfs_key key[2];
3434 u32 sizes[2];
3435 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003436 int ret;
3437 int owner;
3438
Chris Mason5f39d392007-10-15 16:14:19 -04003439 path = btrfs_alloc_path();
3440 BUG_ON(!path);
3441
Chris Mason39279cc2007-06-12 06:35:45 -04003442 inode = new_inode(root->fs_info->sb);
3443 if (!inode)
3444 return ERR_PTR(-ENOMEM);
3445
Josef Bacikaec74772008-07-24 12:12:38 -04003446 if (dir) {
Chris Mason3de45862008-11-17 21:02:50 -05003447 ret = btrfs_set_inode_index(dir, index);
Josef Bacikaec74772008-07-24 12:12:38 -04003448 if (ret)
3449 return ERR_PTR(ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003450 }
3451 /*
3452 * index_cnt is ignored for everything but a dir,
3453 * btrfs_get_inode_index_count has an explanation for the magic
3454 * number
3455 */
Chris Masone02119d2008-09-05 16:13:11 -04003456 init_btrfs_i(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04003457 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04003458 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04003459 BTRFS_I(inode)->generation = trans->transid;
Chris Masonb888db22007-08-27 16:49:44 -04003460
Chris Mason39279cc2007-06-12 06:35:45 -04003461 if (mode & S_IFDIR)
3462 owner = 0;
3463 else
3464 owner = 1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003465 BTRFS_I(inode)->block_group =
3466 btrfs_find_block_group(root, 0, alloc_hint, owner);
Yan Zheng17d217f2008-12-12 10:03:38 -05003467 if ((mode & S_IFREG)) {
3468 if (btrfs_test_opt(root, NODATASUM))
3469 btrfs_set_flag(inode, NODATASUM);
3470 if (btrfs_test_opt(root, NODATACOW))
3471 btrfs_set_flag(inode, NODATACOW);
3472 }
Chris Mason9c583092008-01-29 15:15:18 -05003473
3474 key[0].objectid = objectid;
3475 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
3476 key[0].offset = 0;
3477
3478 key[1].objectid = objectid;
3479 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
3480 key[1].offset = ref_objectid;
3481
3482 sizes[0] = sizeof(struct btrfs_inode_item);
3483 sizes[1] = name_len + sizeof(*ref);
3484
3485 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
3486 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04003487 goto fail;
3488
Chris Mason9c583092008-01-29 15:15:18 -05003489 if (objectid > root->highest_inode)
3490 root->highest_inode = objectid;
3491
Chris Mason79683f22008-11-19 22:00:53 -05003492 inode->i_uid = current_fsuid();
Chris Ball8c087b52009-02-04 09:29:54 -05003493
Chris Mason42f15d72009-02-06 11:35:57 -05003494 if (dir && (dir->i_mode & S_ISGID)) {
Chris Ball8c087b52009-02-04 09:29:54 -05003495 inode->i_gid = dir->i_gid;
3496 if (S_ISDIR(mode))
3497 mode |= S_ISGID;
3498 } else
3499 inode->i_gid = current_fsgid();
3500
Chris Mason39279cc2007-06-12 06:35:45 -04003501 inode->i_mode = mode;
3502 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003503 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003504 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04003505 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3506 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04003507 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003508
3509 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3510 struct btrfs_inode_ref);
3511 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003512 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05003513 ptr = (unsigned long)(ref + 1);
3514 write_extent_buffer(path->nodes[0], name, ptr, name_len);
3515
Chris Mason5f39d392007-10-15 16:14:19 -04003516 btrfs_mark_buffer_dirty(path->nodes[0]);
3517 btrfs_free_path(path);
3518
Chris Mason39279cc2007-06-12 06:35:45 -04003519 location = &BTRFS_I(inode)->location;
3520 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04003521 location->offset = 0;
3522 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
3523
Chris Mason39279cc2007-06-12 06:35:45 -04003524 insert_inode_hash(inode);
3525 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04003526fail:
Josef Bacikaec74772008-07-24 12:12:38 -04003527 if (dir)
3528 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04003529 btrfs_free_path(path);
3530 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003531}
3532
3533static inline u8 btrfs_inode_type(struct inode *inode)
3534{
3535 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
3536}
3537
Chris Masond352ac62008-09-29 15:18:18 -04003538/*
3539 * utility function to add 'inode' into 'parent_inode' with
3540 * a give name and a given sequence number.
3541 * if 'add_backref' is true, also insert a backref from the
3542 * inode to the parent directory.
3543 */
Chris Masone02119d2008-09-05 16:13:11 -04003544int btrfs_add_link(struct btrfs_trans_handle *trans,
3545 struct inode *parent_inode, struct inode *inode,
3546 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04003547{
3548 int ret;
3549 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04003550 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04003551
Chris Mason39279cc2007-06-12 06:35:45 -04003552 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04003553 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
3554 key.offset = 0;
3555
Chris Masone02119d2008-09-05 16:13:11 -04003556 ret = btrfs_insert_dir_item(trans, root, name, name_len,
3557 parent_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04003558 &key, btrfs_inode_type(inode),
Chris Mason00e4e6b2008-08-05 11:18:09 -04003559 index);
Chris Mason39279cc2007-06-12 06:35:45 -04003560 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05003561 if (add_backref) {
3562 ret = btrfs_insert_inode_ref(trans, root,
Chris Masone02119d2008-09-05 16:13:11 -04003563 name, name_len,
3564 inode->i_ino,
3565 parent_inode->i_ino,
3566 index);
Chris Mason9c583092008-01-29 15:15:18 -05003567 }
Chris Masondbe674a2008-07-17 12:54:05 -04003568 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04003569 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04003570 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04003571 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003572 }
3573 return ret;
3574}
3575
3576static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05003577 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003578 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04003579{
Chris Masone02119d2008-09-05 16:13:11 -04003580 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3581 inode, dentry->d_name.name,
3582 dentry->d_name.len, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003583 if (!err) {
3584 d_instantiate(dentry, inode);
3585 return 0;
3586 }
3587 if (err > 0)
3588 err = -EEXIST;
3589 return err;
3590}
3591
Josef Bacik618e21d2007-07-11 10:18:17 -04003592static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
3593 int mode, dev_t rdev)
3594{
3595 struct btrfs_trans_handle *trans;
3596 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05003597 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04003598 int err;
3599 int drop_inode = 0;
3600 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05003601 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003602 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04003603
3604 if (!new_valid_dev(rdev))
3605 return -EINVAL;
3606
Chris Mason1832a6d2007-12-21 16:27:21 -05003607 err = btrfs_check_free_space(root, 1, 0);
3608 if (err)
3609 goto fail;
3610
Josef Bacik618e21d2007-07-11 10:18:17 -04003611 trans = btrfs_start_transaction(root, 1);
3612 btrfs_set_trans_block_group(trans, dir);
3613
3614 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3615 if (err) {
3616 err = -ENOSPC;
3617 goto out_unlock;
3618 }
3619
Josef Bacikaec74772008-07-24 12:12:38 -04003620 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003621 dentry->d_name.len,
3622 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003623 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04003624 err = PTR_ERR(inode);
3625 if (IS_ERR(inode))
3626 goto out_unlock;
3627
Jim Owens0279b4c2009-02-04 09:29:13 -05003628 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04003629 if (err) {
3630 drop_inode = 1;
3631 goto out_unlock;
3632 }
3633
Josef Bacik618e21d2007-07-11 10:18:17 -04003634 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003635 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04003636 if (err)
3637 drop_inode = 1;
3638 else {
3639 inode->i_op = &btrfs_special_inode_operations;
3640 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04003641 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04003642 }
3643 dir->i_sb->s_dirt = 1;
3644 btrfs_update_inode_block_group(trans, inode);
3645 btrfs_update_inode_block_group(trans, dir);
3646out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003647 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003648 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003649fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04003650 if (drop_inode) {
3651 inode_dec_link_count(inode);
3652 iput(inode);
3653 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003654 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04003655 return err;
3656}
3657
Chris Mason39279cc2007-06-12 06:35:45 -04003658static int btrfs_create(struct inode *dir, struct dentry *dentry,
3659 int mode, struct nameidata *nd)
3660{
3661 struct btrfs_trans_handle *trans;
3662 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05003663 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003664 int err;
3665 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05003666 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003667 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003668 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003669
Chris Mason1832a6d2007-12-21 16:27:21 -05003670 err = btrfs_check_free_space(root, 1, 0);
3671 if (err)
3672 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04003673 trans = btrfs_start_transaction(root, 1);
3674 btrfs_set_trans_block_group(trans, dir);
3675
3676 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3677 if (err) {
3678 err = -ENOSPC;
3679 goto out_unlock;
3680 }
3681
Josef Bacikaec74772008-07-24 12:12:38 -04003682 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003683 dentry->d_name.len,
3684 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003685 objectid, BTRFS_I(dir)->block_group, mode,
3686 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003687 err = PTR_ERR(inode);
3688 if (IS_ERR(inode))
3689 goto out_unlock;
3690
Jim Owens0279b4c2009-02-04 09:29:13 -05003691 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04003692 if (err) {
3693 drop_inode = 1;
3694 goto out_unlock;
3695 }
3696
Chris Mason39279cc2007-06-12 06:35:45 -04003697 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003698 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003699 if (err)
3700 drop_inode = 1;
3701 else {
3702 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003703 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003704 inode->i_fop = &btrfs_file_operations;
3705 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003706 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003707 }
3708 dir->i_sb->s_dirt = 1;
3709 btrfs_update_inode_block_group(trans, inode);
3710 btrfs_update_inode_block_group(trans, dir);
3711out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003712 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003713 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003714fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003715 if (drop_inode) {
3716 inode_dec_link_count(inode);
3717 iput(inode);
3718 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003719 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003720 return err;
3721}
3722
3723static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
3724 struct dentry *dentry)
3725{
3726 struct btrfs_trans_handle *trans;
3727 struct btrfs_root *root = BTRFS_I(dir)->root;
3728 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003729 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05003730 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003731 int err;
3732 int drop_inode = 0;
3733
3734 if (inode->i_nlink == 0)
3735 return -ENOENT;
3736
Chris Masone02119d2008-09-05 16:13:11 -04003737 btrfs_inc_nlink(inode);
Chris Mason1832a6d2007-12-21 16:27:21 -05003738 err = btrfs_check_free_space(root, 1, 0);
3739 if (err)
3740 goto fail;
Chris Mason3de45862008-11-17 21:02:50 -05003741 err = btrfs_set_inode_index(dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003742 if (err)
3743 goto fail;
3744
Chris Mason39279cc2007-06-12 06:35:45 -04003745 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003746
Chris Mason39279cc2007-06-12 06:35:45 -04003747 btrfs_set_trans_block_group(trans, dir);
3748 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04003749
Chris Mason00e4e6b2008-08-05 11:18:09 -04003750 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04003751
Chris Mason39279cc2007-06-12 06:35:45 -04003752 if (err)
3753 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04003754
Chris Mason39279cc2007-06-12 06:35:45 -04003755 dir->i_sb->s_dirt = 1;
3756 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04003757 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003758
Chris Mason54aa1f42007-06-22 14:16:25 -04003759 if (err)
3760 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003761
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003762 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003763 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003764fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003765 if (drop_inode) {
3766 inode_dec_link_count(inode);
3767 iput(inode);
3768 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003769 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003770 return err;
3771}
3772
Chris Mason39279cc2007-06-12 06:35:45 -04003773static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
3774{
Chris Masonb9d86662008-05-02 16:13:49 -04003775 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003776 struct btrfs_trans_handle *trans;
3777 struct btrfs_root *root = BTRFS_I(dir)->root;
3778 int err = 0;
3779 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04003780 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003781 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003782 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003783
Chris Mason1832a6d2007-12-21 16:27:21 -05003784 err = btrfs_check_free_space(root, 1, 0);
3785 if (err)
3786 goto out_unlock;
3787
Chris Mason39279cc2007-06-12 06:35:45 -04003788 trans = btrfs_start_transaction(root, 1);
3789 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04003790
Chris Mason39279cc2007-06-12 06:35:45 -04003791 if (IS_ERR(trans)) {
3792 err = PTR_ERR(trans);
3793 goto out_unlock;
3794 }
3795
3796 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3797 if (err) {
3798 err = -ENOSPC;
3799 goto out_unlock;
3800 }
3801
Josef Bacikaec74772008-07-24 12:12:38 -04003802 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003803 dentry->d_name.len,
3804 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003805 BTRFS_I(dir)->block_group, S_IFDIR | mode,
3806 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003807 if (IS_ERR(inode)) {
3808 err = PTR_ERR(inode);
3809 goto out_fail;
3810 }
Chris Mason5f39d392007-10-15 16:14:19 -04003811
Chris Mason39279cc2007-06-12 06:35:45 -04003812 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04003813
Jim Owens0279b4c2009-02-04 09:29:13 -05003814 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04003815 if (err)
3816 goto out_fail;
3817
Chris Mason39279cc2007-06-12 06:35:45 -04003818 inode->i_op = &btrfs_dir_inode_operations;
3819 inode->i_fop = &btrfs_dir_file_operations;
3820 btrfs_set_trans_block_group(trans, inode);
3821
Chris Masondbe674a2008-07-17 12:54:05 -04003822 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003823 err = btrfs_update_inode(trans, root, inode);
3824 if (err)
3825 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04003826
Chris Masone02119d2008-09-05 16:13:11 -04003827 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3828 inode, dentry->d_name.name,
3829 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003830 if (err)
3831 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04003832
Chris Mason39279cc2007-06-12 06:35:45 -04003833 d_instantiate(dentry, inode);
3834 drop_on_err = 0;
3835 dir->i_sb->s_dirt = 1;
3836 btrfs_update_inode_block_group(trans, inode);
3837 btrfs_update_inode_block_group(trans, dir);
3838
3839out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003840 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003841 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04003842
Chris Mason39279cc2007-06-12 06:35:45 -04003843out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003844 if (drop_on_err)
3845 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003846 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003847 return err;
3848}
3849
Chris Masond352ac62008-09-29 15:18:18 -04003850/* helper for btfs_get_extent. Given an existing extent in the tree,
3851 * and an extent that you want to insert, deal with overlap and insert
3852 * the new extent into the tree.
3853 */
Chris Mason3b951512008-04-17 11:29:12 -04003854static int merge_extent_mapping(struct extent_map_tree *em_tree,
3855 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003856 struct extent_map *em,
3857 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04003858{
3859 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04003860
Chris Masone6dcd2d2008-07-17 12:53:50 -04003861 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
3862 start_diff = map_start - em->start;
3863 em->start = map_start;
3864 em->len = map_len;
Chris Masonc8b97812008-10-29 14:49:59 -04003865 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
3866 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003867 em->block_start += start_diff;
Chris Masonc8b97812008-10-29 14:49:59 -04003868 em->block_len -= start_diff;
3869 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003870 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04003871}
3872
Chris Masonc8b97812008-10-29 14:49:59 -04003873static noinline int uncompress_inline(struct btrfs_path *path,
3874 struct inode *inode, struct page *page,
3875 size_t pg_offset, u64 extent_offset,
3876 struct btrfs_file_extent_item *item)
3877{
3878 int ret;
3879 struct extent_buffer *leaf = path->nodes[0];
3880 char *tmp;
3881 size_t max_size;
3882 unsigned long inline_size;
3883 unsigned long ptr;
3884
3885 WARN_ON(pg_offset != 0);
3886 max_size = btrfs_file_extent_ram_bytes(leaf, item);
3887 inline_size = btrfs_file_extent_inline_item_len(leaf,
3888 btrfs_item_nr(leaf, path->slots[0]));
3889 tmp = kmalloc(inline_size, GFP_NOFS);
3890 ptr = btrfs_file_extent_inline_start(item);
3891
3892 read_extent_buffer(leaf, tmp, ptr, inline_size);
3893
Chris Mason5b050f02008-11-11 09:34:41 -05003894 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
Chris Masonc8b97812008-10-29 14:49:59 -04003895 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
3896 inline_size, max_size);
3897 if (ret) {
3898 char *kaddr = kmap_atomic(page, KM_USER0);
3899 unsigned long copy_size = min_t(u64,
3900 PAGE_CACHE_SIZE - pg_offset,
3901 max_size - extent_offset);
3902 memset(kaddr + pg_offset, 0, copy_size);
3903 kunmap_atomic(kaddr, KM_USER0);
3904 }
3905 kfree(tmp);
3906 return 0;
3907}
3908
Chris Masond352ac62008-09-29 15:18:18 -04003909/*
3910 * a bit scary, this does extent mapping from logical file offset to the disk.
Chris Masond3977122009-01-05 21:25:51 -05003911 * the ugly parts come from merging extents from the disk with the in-ram
3912 * representation. This gets more complex because of the data=ordered code,
Chris Masond352ac62008-09-29 15:18:18 -04003913 * where the in-ram extents might be locked pending data=ordered completion.
3914 *
3915 * This also copies inline extents directly into the page.
3916 */
Chris Masond3977122009-01-05 21:25:51 -05003917
Chris Masona52d9a82007-08-27 16:49:44 -04003918struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05003919 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04003920 int create)
3921{
3922 int ret;
3923 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04003924 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04003925 u64 extent_start = 0;
3926 u64 extent_end = 0;
3927 u64 objectid = inode->i_ino;
3928 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04003929 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04003930 struct btrfs_root *root = BTRFS_I(inode)->root;
3931 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04003932 struct extent_buffer *leaf;
3933 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04003934 struct extent_map *em = NULL;
3935 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05003936 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04003937 struct btrfs_trans_handle *trans = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003938 int compressed;
Chris Masona52d9a82007-08-27 16:49:44 -04003939
Chris Masona52d9a82007-08-27 16:49:44 -04003940again:
Chris Masond1310b22008-01-24 16:13:08 -05003941 spin_lock(&em_tree->lock);
3942 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04003943 if (em)
3944 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05003945 spin_unlock(&em_tree->lock);
3946
Chris Masona52d9a82007-08-27 16:49:44 -04003947 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04003948 if (em->start > start || em->start + em->len <= start)
3949 free_extent_map(em);
3950 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05003951 free_extent_map(em);
3952 else
3953 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003954 }
Chris Masond1310b22008-01-24 16:13:08 -05003955 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04003956 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05003957 err = -ENOMEM;
3958 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04003959 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003960 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05003961 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05003962 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05003963 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04003964 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04003965
3966 if (!path) {
3967 path = btrfs_alloc_path();
3968 BUG_ON(!path);
3969 }
3970
Chris Mason179e29e2007-11-01 11:28:41 -04003971 ret = btrfs_lookup_file_extent(trans, root, path,
3972 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04003973 if (ret < 0) {
3974 err = ret;
3975 goto out;
3976 }
3977
3978 if (ret != 0) {
3979 if (path->slots[0] == 0)
3980 goto not_found;
3981 path->slots[0]--;
3982 }
3983
Chris Mason5f39d392007-10-15 16:14:19 -04003984 leaf = path->nodes[0];
3985 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04003986 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04003987 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04003988 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3989 found_type = btrfs_key_type(&found_key);
3990 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04003991 found_type != BTRFS_EXTENT_DATA_KEY) {
3992 goto not_found;
3993 }
3994
Chris Mason5f39d392007-10-15 16:14:19 -04003995 found_type = btrfs_file_extent_type(leaf, item);
3996 extent_start = found_key.offset;
Chris Masonc8b97812008-10-29 14:49:59 -04003997 compressed = btrfs_file_extent_compression(leaf, item);
Yan Zhengd899e052008-10-30 14:25:28 -04003998 if (found_type == BTRFS_FILE_EXTENT_REG ||
3999 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04004000 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04004001 btrfs_file_extent_num_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04004002 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
4003 size_t size;
4004 size = btrfs_file_extent_inline_len(leaf, item);
4005 extent_end = (extent_start + size + root->sectorsize - 1) &
4006 ~((u64)root->sectorsize - 1);
4007 }
4008
4009 if (start >= extent_end) {
4010 path->slots[0]++;
4011 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
4012 ret = btrfs_next_leaf(root, path);
4013 if (ret < 0) {
4014 err = ret;
4015 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004016 }
Yan Zheng9036c102008-10-30 14:19:41 -04004017 if (ret > 0)
4018 goto not_found;
4019 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04004020 }
Yan Zheng9036c102008-10-30 14:19:41 -04004021 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4022 if (found_key.objectid != objectid ||
4023 found_key.type != BTRFS_EXTENT_DATA_KEY)
4024 goto not_found;
4025 if (start + len <= found_key.offset)
4026 goto not_found;
4027 em->start = start;
4028 em->len = found_key.offset - start;
4029 goto not_found_em;
4030 }
4031
Yan Zhengd899e052008-10-30 14:25:28 -04004032 if (found_type == BTRFS_FILE_EXTENT_REG ||
4033 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng9036c102008-10-30 14:19:41 -04004034 em->start = extent_start;
4035 em->len = extent_end - extent_start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004036 em->orig_start = extent_start -
4037 btrfs_file_extent_offset(leaf, item);
Chris Masondb945352007-10-15 16:15:53 -04004038 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
4039 if (bytenr == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004040 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04004041 goto insert;
4042 }
Chris Masonc8b97812008-10-29 14:49:59 -04004043 if (compressed) {
4044 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
4045 em->block_start = bytenr;
4046 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
4047 item);
4048 } else {
4049 bytenr += btrfs_file_extent_offset(leaf, item);
4050 em->block_start = bytenr;
4051 em->block_len = em->len;
Yan Zhengd899e052008-10-30 14:25:28 -04004052 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
4053 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonc8b97812008-10-29 14:49:59 -04004054 }
Chris Masona52d9a82007-08-27 16:49:44 -04004055 goto insert;
4056 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004057 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04004058 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04004059 size_t size;
4060 size_t extent_offset;
4061 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04004062
Chris Masona52d9a82007-08-27 16:49:44 -04004063 em->block_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004064 if (!page || create) {
Yan689f9342007-10-29 11:41:07 -04004065 em->start = extent_start;
Yan Zheng9036c102008-10-30 14:19:41 -04004066 em->len = extent_end - extent_start;
Yan689f9342007-10-29 11:41:07 -04004067 goto out;
4068 }
4069
Yan Zheng9036c102008-10-30 14:19:41 -04004070 size = btrfs_file_extent_inline_len(leaf, item);
4071 extent_offset = page_offset(page) + pg_offset - extent_start;
Chris Mason70dec802008-01-29 09:59:12 -05004072 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04004073 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04004074 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05004075 em->len = (copy_size + root->sectorsize - 1) &
4076 ~((u64)root->sectorsize - 1);
Yan Zhengff5b7ee2008-11-10 07:34:43 -05004077 em->orig_start = EXTENT_MAP_INLINE;
Chris Masonc8b97812008-10-29 14:49:59 -04004078 if (compressed)
4079 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Yan689f9342007-10-29 11:41:07 -04004080 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04004081 if (create == 0 && !PageUptodate(page)) {
Chris Masonc8b97812008-10-29 14:49:59 -04004082 if (btrfs_file_extent_compression(leaf, item) ==
4083 BTRFS_COMPRESS_ZLIB) {
4084 ret = uncompress_inline(path, inode, page,
4085 pg_offset,
4086 extent_offset, item);
4087 BUG_ON(ret);
4088 } else {
4089 map = kmap(page);
4090 read_extent_buffer(leaf, map + pg_offset, ptr,
4091 copy_size);
4092 kunmap(page);
4093 }
Chris Mason179e29e2007-11-01 11:28:41 -04004094 flush_dcache_page(page);
4095 } else if (create && PageUptodate(page)) {
4096 if (!trans) {
4097 kunmap(page);
4098 free_extent_map(em);
4099 em = NULL;
4100 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04004101 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004102 goto again;
4103 }
Chris Masonc8b97812008-10-29 14:49:59 -04004104 map = kmap(page);
Chris Mason70dec802008-01-29 09:59:12 -05004105 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04004106 copy_size);
Chris Masonc8b97812008-10-29 14:49:59 -04004107 kunmap(page);
Chris Mason179e29e2007-11-01 11:28:41 -04004108 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04004109 }
Chris Masond1310b22008-01-24 16:13:08 -05004110 set_extent_uptodate(io_tree, em->start,
4111 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04004112 goto insert;
4113 } else {
Chris Masond3977122009-01-05 21:25:51 -05004114 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
Chris Masona52d9a82007-08-27 16:49:44 -04004115 WARN_ON(1);
4116 }
4117not_found:
4118 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05004119 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04004120not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04004121 em->block_start = EXTENT_MAP_HOLE;
Yan Zheng9036c102008-10-30 14:19:41 -04004122 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004123insert:
4124 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05004125 if (em->start > start || extent_map_end(em) <= start) {
Chris Masond3977122009-01-05 21:25:51 -05004126 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
4127 "[%llu %llu]\n", (unsigned long long)em->start,
4128 (unsigned long long)em->len,
4129 (unsigned long long)start,
4130 (unsigned long long)len);
Chris Masona52d9a82007-08-27 16:49:44 -04004131 err = -EIO;
4132 goto out;
4133 }
Chris Masond1310b22008-01-24 16:13:08 -05004134
4135 err = 0;
4136 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004137 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04004138 /* it is possible that someone inserted the extent into the tree
4139 * while we had the lock dropped. It is also possible that
4140 * an overlapping map exists in the tree
4141 */
Chris Masona52d9a82007-08-27 16:49:44 -04004142 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04004143 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004144
4145 ret = 0;
4146
Chris Mason3b951512008-04-17 11:29:12 -04004147 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04004148 if (existing && (existing->start > start ||
4149 existing->start + existing->len <= start)) {
4150 free_extent_map(existing);
4151 existing = NULL;
4152 }
Chris Mason3b951512008-04-17 11:29:12 -04004153 if (!existing) {
4154 existing = lookup_extent_mapping(em_tree, em->start,
4155 em->len);
4156 if (existing) {
4157 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04004158 em, start,
4159 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04004160 free_extent_map(existing);
4161 if (err) {
4162 free_extent_map(em);
4163 em = NULL;
4164 }
4165 } else {
4166 err = -EIO;
Chris Mason3b951512008-04-17 11:29:12 -04004167 free_extent_map(em);
4168 em = NULL;
4169 }
4170 } else {
4171 free_extent_map(em);
4172 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004173 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04004174 }
Chris Masona52d9a82007-08-27 16:49:44 -04004175 }
Chris Masond1310b22008-01-24 16:13:08 -05004176 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04004177out:
Chris Masonf4219502008-07-22 11:18:09 -04004178 if (path)
4179 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04004180 if (trans) {
4181 ret = btrfs_end_transaction(trans, root);
Chris Masond3977122009-01-05 21:25:51 -05004182 if (!err)
Chris Masona52d9a82007-08-27 16:49:44 -04004183 err = ret;
4184 }
Chris Masona52d9a82007-08-27 16:49:44 -04004185 if (err) {
4186 free_extent_map(em);
4187 WARN_ON(1);
4188 return ERR_PTR(err);
4189 }
4190 return em;
4191}
4192
Chris Mason16432982008-04-10 10:23:21 -04004193static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
4194 const struct iovec *iov, loff_t offset,
4195 unsigned long nr_segs)
4196{
Chris Masone1c4b742008-04-22 13:26:46 -04004197 return -EINVAL;
Chris Mason16432982008-04-10 10:23:21 -04004198}
4199
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004200static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4201 __u64 start, __u64 len)
4202{
4203 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
4204}
4205
Chris Mason9ebefb182007-06-15 13:50:00 -04004206int btrfs_readpage(struct file *file, struct page *page)
4207{
Chris Masond1310b22008-01-24 16:13:08 -05004208 struct extent_io_tree *tree;
4209 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004210 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04004211}
Chris Mason1832a6d2007-12-21 16:27:21 -05004212
Chris Mason39279cc2007-06-12 06:35:45 -04004213static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
4214{
Chris Masond1310b22008-01-24 16:13:08 -05004215 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04004216
4217
4218 if (current->flags & PF_MEMALLOC) {
4219 redirty_page_for_writepage(wbc, page);
4220 unlock_page(page);
4221 return 0;
4222 }
Chris Masond1310b22008-01-24 16:13:08 -05004223 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04004224 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
4225}
Chris Mason39279cc2007-06-12 06:35:45 -04004226
Chris Masonf4219502008-07-22 11:18:09 -04004227int btrfs_writepages(struct address_space *mapping,
4228 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04004229{
Chris Masond1310b22008-01-24 16:13:08 -05004230 struct extent_io_tree *tree;
Chris Mason771ed682008-11-06 22:02:51 -05004231
Chris Masond1310b22008-01-24 16:13:08 -05004232 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04004233 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
4234}
4235
Chris Mason3ab2fb52007-11-08 10:59:22 -05004236static int
4237btrfs_readpages(struct file *file, struct address_space *mapping,
4238 struct list_head *pages, unsigned nr_pages)
4239{
Chris Masond1310b22008-01-24 16:13:08 -05004240 struct extent_io_tree *tree;
4241 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05004242 return extent_readpages(tree, mapping, pages, nr_pages,
4243 btrfs_get_extent);
4244}
Chris Masone6dcd2d2008-07-17 12:53:50 -04004245static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04004246{
Chris Masond1310b22008-01-24 16:13:08 -05004247 struct extent_io_tree *tree;
4248 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04004249 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004250
Chris Masond1310b22008-01-24 16:13:08 -05004251 tree = &BTRFS_I(page->mapping->host)->io_tree;
4252 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05004253 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04004254 if (ret == 1) {
4255 ClearPagePrivate(page);
4256 set_page_private(page, 0);
4257 page_cache_release(page);
4258 }
4259 return ret;
4260}
Chris Mason39279cc2007-06-12 06:35:45 -04004261
Chris Masone6dcd2d2008-07-17 12:53:50 -04004262static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
4263{
Chris Mason98509cf2008-09-11 15:51:43 -04004264 if (PageWriteback(page) || PageDirty(page))
4265 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004266 return __btrfs_releasepage(page, gfp_flags);
4267}
4268
Chris Masona52d9a82007-08-27 16:49:44 -04004269static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4270{
Chris Masond1310b22008-01-24 16:13:08 -05004271 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004272 struct btrfs_ordered_extent *ordered;
4273 u64 page_start = page_offset(page);
4274 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04004275
Chris Masone6dcd2d2008-07-17 12:53:50 -04004276 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05004277 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004278 if (offset) {
4279 btrfs_releasepage(page, GFP_NOFS);
4280 return;
4281 }
4282
4283 lock_extent(tree, page_start, page_end, GFP_NOFS);
4284 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
4285 page_offset(page));
4286 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04004287 /*
4288 * IO on this page will never be started, so we need
4289 * to account for any ordered extents now
4290 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004291 clear_extent_bit(tree, page_start, page_end,
4292 EXTENT_DIRTY | EXTENT_DELALLOC |
4293 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04004294 btrfs_finish_ordered_io(page->mapping->host,
4295 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004296 btrfs_put_ordered_extent(ordered);
4297 lock_extent(tree, page_start, page_end, GFP_NOFS);
4298 }
4299 clear_extent_bit(tree, page_start, page_end,
4300 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4301 EXTENT_ORDERED,
4302 1, 1, GFP_NOFS);
4303 __btrfs_releasepage(page, GFP_NOFS);
4304
Chris Mason4a096752008-07-21 10:29:44 -04004305 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04004306 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04004307 ClearPagePrivate(page);
4308 set_page_private(page, 0);
4309 page_cache_release(page);
4310 }
Chris Mason39279cc2007-06-12 06:35:45 -04004311}
4312
Chris Mason9ebefb182007-06-15 13:50:00 -04004313/*
4314 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
4315 * called from a page fault handler when a page is first dirtied. Hence we must
4316 * be careful to check for EOF conditions here. We set the page up correctly
4317 * for a written page which means we get ENOSPC checking when writing into
4318 * holes and correct delalloc and unwritten extent mapping on filesystems that
4319 * support these features.
4320 *
4321 * We are not allowed to take the i_mutex here so we have to play games to
4322 * protect against truncate races as the page could now be beyond EOF. Because
4323 * vmtruncate() writes the inode size before removing pages, once we have the
4324 * page lock we can determine safely if the page is beyond EOF. If it is not
4325 * beyond EOF, then the page is guaranteed safe against truncation until we
4326 * unlock the page.
4327 */
4328int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
4329{
Chris Mason6da6aba2007-12-18 16:15:09 -05004330 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05004331 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004332 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4333 struct btrfs_ordered_extent *ordered;
4334 char *kaddr;
4335 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04004336 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05004337 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04004338 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004339 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04004340
Chris Mason1832a6d2007-12-21 16:27:21 -05004341 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05004342 if (ret)
4343 goto out;
4344
4345 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004346again:
Chris Mason9ebefb182007-06-15 13:50:00 -04004347 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04004348 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004349 page_start = page_offset(page);
4350 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04004351
Chris Mason9ebefb182007-06-15 13:50:00 -04004352 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04004353 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04004354 /* page got truncated out from underneath us */
4355 goto out_unlock;
4356 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004357 wait_on_page_writeback(page);
4358
4359 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
4360 set_page_extent_mapped(page);
4361
Chris Masoneb84ae02008-07-17 13:53:27 -04004362 /*
4363 * we can't set the delalloc bits if there are pending ordered
4364 * extents. Drop our locks and wait for them to finish
4365 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04004366 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4367 if (ordered) {
4368 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4369 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004370 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004371 btrfs_put_ordered_extent(ordered);
4372 goto again;
4373 }
4374
Chris Masonea8c2812008-08-04 23:17:27 -04004375 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004376 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04004377
4378 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04004379 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04004380 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04004381 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04004382 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04004383
Chris Masone6dcd2d2008-07-17 12:53:50 -04004384 if (zero_start != PAGE_CACHE_SIZE) {
4385 kaddr = kmap(page);
4386 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
4387 flush_dcache_page(page);
4388 kunmap(page);
4389 }
Chris Mason247e7432008-07-17 12:53:51 -04004390 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004391 set_page_dirty(page);
4392 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04004393
4394out_unlock:
4395 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05004396out:
Chris Mason9ebefb182007-06-15 13:50:00 -04004397 return ret;
4398}
4399
Chris Mason39279cc2007-06-12 06:35:45 -04004400static void btrfs_truncate(struct inode *inode)
4401{
4402 struct btrfs_root *root = BTRFS_I(inode)->root;
4403 int ret;
4404 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004405 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04004406 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004407
4408 if (!S_ISREG(inode->i_mode))
4409 return;
4410 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4411 return;
4412
4413 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04004414 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04004415
Chris Mason39279cc2007-06-12 06:35:45 -04004416 trans = btrfs_start_transaction(root, 1);
4417 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04004418 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004419
Josef Bacik7b128762008-07-24 12:17:14 -04004420 ret = btrfs_orphan_add(trans, inode);
4421 if (ret)
4422 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04004423 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Masone02119d2008-09-05 16:13:11 -04004424 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
Chris Mason85e21ba2008-01-29 15:11:36 -05004425 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004426 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04004427
Josef Bacik7b128762008-07-24 12:17:14 -04004428 ret = btrfs_orphan_del(trans, inode);
4429 BUG_ON(ret);
4430
4431out:
4432 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04004433 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04004434 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004435 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004436}
4437
Sven Wegener3b963622008-06-09 21:57:42 -04004438/*
Chris Masond352ac62008-09-29 15:18:18 -04004439 * create a new subvolume directory/inode (helper for the ioctl).
4440 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05004441int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
4442 struct btrfs_root *new_root, struct dentry *dentry,
4443 u64 new_dirid, u64 alloc_hint)
Chris Mason39279cc2007-06-12 06:35:45 -04004444{
Chris Mason39279cc2007-06-12 06:35:45 -04004445 struct inode *inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04004446 int error;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004447 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004448
Josef Bacikaec74772008-07-24 12:12:38 -04004449 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Yan Zhengd2fb3432008-12-11 16:30:39 -05004450 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04004451 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04004452 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004453 inode->i_op = &btrfs_dir_inode_operations;
4454 inode->i_fop = &btrfs_dir_file_operations;
4455
Chris Mason39279cc2007-06-12 06:35:45 -04004456 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04004457 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04004458
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04004459 error = btrfs_update_inode(trans, new_root, inode);
4460 if (error)
4461 return error;
4462
4463 d_instantiate(dentry, inode);
4464 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004465}
4466
Chris Masond352ac62008-09-29 15:18:18 -04004467/* helper function for file defrag and space balancing. This
4468 * forces readahead on a given range of bytes in an inode
4469 */
Chris Masonedbd8d42007-12-21 16:27:24 -05004470unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04004471 struct file_ra_state *ra, struct file *file,
4472 pgoff_t offset, pgoff_t last_index)
4473{
Chris Mason8e7bf942008-04-28 09:02:36 -04004474 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04004475
Chris Mason86479a02007-09-10 19:58:16 -04004476 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
4477 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04004478}
4479
Chris Mason39279cc2007-06-12 06:35:45 -04004480struct inode *btrfs_alloc_inode(struct super_block *sb)
4481{
4482 struct btrfs_inode *ei;
4483
4484 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
4485 if (!ei)
4486 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04004487 ei->last_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004488 ei->logged_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004489 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik33268ea2008-07-24 12:16:36 -04004490 ei->i_acl = BTRFS_ACL_NOT_CACHED;
4491 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
Josef Bacik7b128762008-07-24 12:17:14 -04004492 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason39279cc2007-06-12 06:35:45 -04004493 return &ei->vfs_inode;
4494}
4495
4496void btrfs_destroy_inode(struct inode *inode)
4497{
Chris Masone6dcd2d2008-07-17 12:53:50 -04004498 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04004499 WARN_ON(!list_empty(&inode->i_dentry));
4500 WARN_ON(inode->i_data.nrpages);
4501
Josef Bacik33268ea2008-07-24 12:16:36 -04004502 if (BTRFS_I(inode)->i_acl &&
4503 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
4504 posix_acl_release(BTRFS_I(inode)->i_acl);
4505 if (BTRFS_I(inode)->i_default_acl &&
4506 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
4507 posix_acl_release(BTRFS_I(inode)->i_default_acl);
4508
Yanbcc63ab2008-07-30 16:29:20 -04004509 spin_lock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04004510 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
4511 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
4512 " list\n", inode->i_ino);
4513 dump_stack();
4514 }
Yanbcc63ab2008-07-30 16:29:20 -04004515 spin_unlock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04004516
Chris Masond3977122009-01-05 21:25:51 -05004517 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04004518 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
4519 if (!ordered)
4520 break;
4521 else {
Chris Masond3977122009-01-05 21:25:51 -05004522 printk(KERN_ERR "btrfs found ordered "
4523 "extent %llu %llu on inode cleanup\n",
4524 (unsigned long long)ordered->file_offset,
4525 (unsigned long long)ordered->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004526 btrfs_remove_ordered_extent(inode, ordered);
4527 btrfs_put_ordered_extent(ordered);
4528 btrfs_put_ordered_extent(ordered);
4529 }
4530 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004531 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04004532 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
4533}
4534
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004535static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04004536{
4537 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
4538
4539 inode_init_once(&ei->vfs_inode);
4540}
4541
4542void btrfs_destroy_cachep(void)
4543{
4544 if (btrfs_inode_cachep)
4545 kmem_cache_destroy(btrfs_inode_cachep);
4546 if (btrfs_trans_handle_cachep)
4547 kmem_cache_destroy(btrfs_trans_handle_cachep);
4548 if (btrfs_transaction_cachep)
4549 kmem_cache_destroy(btrfs_transaction_cachep);
4550 if (btrfs_bit_radix_cachep)
4551 kmem_cache_destroy(btrfs_bit_radix_cachep);
4552 if (btrfs_path_cachep)
4553 kmem_cache_destroy(btrfs_path_cachep);
4554}
4555
Chris Mason86479a02007-09-10 19:58:16 -04004556struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04004557 unsigned long extra_flags,
Chris Mason2b1f55b2008-09-24 11:48:04 -04004558 void (*ctor)(void *))
Chris Mason92fee662007-07-25 12:31:35 -04004559{
4560 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
Chris Mason2b1f55b2008-09-24 11:48:04 -04004561 SLAB_MEM_SPREAD | extra_flags), ctor);
Chris Mason92fee662007-07-25 12:31:35 -04004562}
4563
Chris Mason39279cc2007-06-12 06:35:45 -04004564int btrfs_init_cachep(void)
4565{
Chris Mason86479a02007-09-10 19:58:16 -04004566 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04004567 sizeof(struct btrfs_inode),
4568 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04004569 if (!btrfs_inode_cachep)
4570 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004571 btrfs_trans_handle_cachep =
4572 btrfs_cache_create("btrfs_trans_handle_cache",
4573 sizeof(struct btrfs_trans_handle),
4574 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004575 if (!btrfs_trans_handle_cachep)
4576 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004577 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04004578 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04004579 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004580 if (!btrfs_transaction_cachep)
4581 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004582 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04004583 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04004584 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004585 if (!btrfs_path_cachep)
4586 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04004587 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04004588 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04004589 if (!btrfs_bit_radix_cachep)
4590 goto fail;
4591 return 0;
4592fail:
4593 btrfs_destroy_cachep();
4594 return -ENOMEM;
4595}
4596
4597static int btrfs_getattr(struct vfsmount *mnt,
4598 struct dentry *dentry, struct kstat *stat)
4599{
4600 struct inode *inode = dentry->d_inode;
4601 generic_fillattr(inode, stat);
Chris Mason3394e162008-11-17 20:42:26 -05004602 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
Chris Masond6667462008-01-03 14:51:00 -05004603 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004604 stat->blocks = (inode_get_bytes(inode) +
4605 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04004606 return 0;
4607}
4608
Chris Masond3977122009-01-05 21:25:51 -05004609static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4610 struct inode *new_dir, struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04004611{
4612 struct btrfs_trans_handle *trans;
4613 struct btrfs_root *root = BTRFS_I(old_dir)->root;
4614 struct inode *new_inode = new_dentry->d_inode;
4615 struct inode *old_inode = old_dentry->d_inode;
4616 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004617 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004618 int ret;
4619
Chris Mason3394e162008-11-17 20:42:26 -05004620 /* we're not allowed to rename between subvolumes */
4621 if (BTRFS_I(old_inode)->root->root_key.objectid !=
4622 BTRFS_I(new_dir)->root->root_key.objectid)
4623 return -EXDEV;
4624
Chris Mason39279cc2007-06-12 06:35:45 -04004625 if (S_ISDIR(old_inode->i_mode) && new_inode &&
4626 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
4627 return -ENOTEMPTY;
4628 }
Chris Mason5f39d392007-10-15 16:14:19 -04004629
Chris Mason0660b5a2008-11-17 20:37:39 -05004630 /* to rename a snapshot or subvolume, we need to juggle the
4631 * backrefs. This isn't coded yet
4632 */
4633 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
4634 return -EXDEV;
4635
Chris Mason1832a6d2007-12-21 16:27:21 -05004636 ret = btrfs_check_free_space(root, 1, 0);
4637 if (ret)
4638 goto out_unlock;
4639
Chris Mason39279cc2007-06-12 06:35:45 -04004640 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004641
Chris Mason39279cc2007-06-12 06:35:45 -04004642 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04004643
Chris Masone02119d2008-09-05 16:13:11 -04004644 btrfs_inc_nlink(old_dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04004645 old_dir->i_ctime = old_dir->i_mtime = ctime;
4646 new_dir->i_ctime = new_dir->i_mtime = ctime;
4647 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04004648
Chris Masone02119d2008-09-05 16:13:11 -04004649 ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode,
4650 old_dentry->d_name.name,
4651 old_dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04004652 if (ret)
4653 goto out_fail;
4654
4655 if (new_inode) {
4656 new_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04004657 ret = btrfs_unlink_inode(trans, root, new_dir,
4658 new_dentry->d_inode,
4659 new_dentry->d_name.name,
4660 new_dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04004661 if (ret)
4662 goto out_fail;
Josef Bacik7b128762008-07-24 12:17:14 -04004663 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004664 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Josef Bacik7b128762008-07-24 12:17:14 -04004665 if (ret)
4666 goto out_fail;
4667 }
Chris Masone02119d2008-09-05 16:13:11 -04004668
Chris Mason39279cc2007-06-12 06:35:45 -04004669 }
Chris Mason3de45862008-11-17 21:02:50 -05004670 ret = btrfs_set_inode_index(new_dir, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04004671 if (ret)
4672 goto out_fail;
4673
Chris Masone02119d2008-09-05 16:13:11 -04004674 ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode,
4675 old_inode, new_dentry->d_name.name,
4676 new_dentry->d_name.len, 1, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004677 if (ret)
4678 goto out_fail;
4679
4680out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04004681 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004682out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04004683 return ret;
4684}
4685
Chris Masond352ac62008-09-29 15:18:18 -04004686/*
4687 * some fairly slow code that needs optimization. This walks the list
4688 * of all the inodes with pending delalloc and forces them to disk.
4689 */
Chris Masonea8c2812008-08-04 23:17:27 -04004690int btrfs_start_delalloc_inodes(struct btrfs_root *root)
4691{
4692 struct list_head *head = &root->fs_info->delalloc_inodes;
4693 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004694 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04004695
Yan Zhengc146afa2008-11-12 14:34:12 -05004696 if (root->fs_info->sb->s_flags & MS_RDONLY)
4697 return -EROFS;
4698
Chris Mason75eff682008-12-15 15:54:40 -05004699 spin_lock(&root->fs_info->delalloc_lock);
Chris Masond3977122009-01-05 21:25:51 -05004700 while (!list_empty(head)) {
Chris Masonea8c2812008-08-04 23:17:27 -04004701 binode = list_entry(head->next, struct btrfs_inode,
4702 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004703 inode = igrab(&binode->vfs_inode);
4704 if (!inode)
4705 list_del_init(&binode->delalloc_inodes);
Chris Mason75eff682008-12-15 15:54:40 -05004706 spin_unlock(&root->fs_info->delalloc_lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004707 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04004708 filemap_flush(inode->i_mapping);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04004709 iput(inode);
4710 }
4711 cond_resched();
Chris Mason75eff682008-12-15 15:54:40 -05004712 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04004713 }
Chris Mason75eff682008-12-15 15:54:40 -05004714 spin_unlock(&root->fs_info->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04004715
4716 /* the filemap_flush will queue IO into the worker threads, but
4717 * we have to make sure the IO is actually started and that
4718 * ordered extents get created before we return
4719 */
4720 atomic_inc(&root->fs_info->async_submit_draining);
Chris Masond3977122009-01-05 21:25:51 -05004721 while (atomic_read(&root->fs_info->nr_async_submits) ||
Chris Mason771ed682008-11-06 22:02:51 -05004722 atomic_read(&root->fs_info->async_delalloc_pages)) {
Chris Mason8c8bee12008-09-29 11:19:10 -04004723 wait_event(root->fs_info->async_submit_wait,
Chris Mason771ed682008-11-06 22:02:51 -05004724 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
4725 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
Chris Mason8c8bee12008-09-29 11:19:10 -04004726 }
4727 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04004728 return 0;
4729}
4730
Chris Mason39279cc2007-06-12 06:35:45 -04004731static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
4732 const char *symname)
4733{
4734 struct btrfs_trans_handle *trans;
4735 struct btrfs_root *root = BTRFS_I(dir)->root;
4736 struct btrfs_path *path;
4737 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05004738 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04004739 int err;
4740 int drop_inode = 0;
4741 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04004742 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04004743 int name_len;
4744 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04004745 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04004746 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04004747 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05004748 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004749
4750 name_len = strlen(symname) + 1;
4751 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
4752 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05004753
Chris Mason1832a6d2007-12-21 16:27:21 -05004754 err = btrfs_check_free_space(root, 1, 0);
4755 if (err)
4756 goto out_fail;
4757
Chris Mason39279cc2007-06-12 06:35:45 -04004758 trans = btrfs_start_transaction(root, 1);
4759 btrfs_set_trans_block_group(trans, dir);
4760
4761 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4762 if (err) {
4763 err = -ENOSPC;
4764 goto out_unlock;
4765 }
4766
Josef Bacikaec74772008-07-24 12:12:38 -04004767 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05004768 dentry->d_name.len,
4769 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04004770 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
4771 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04004772 err = PTR_ERR(inode);
4773 if (IS_ERR(inode))
4774 goto out_unlock;
4775
Jim Owens0279b4c2009-02-04 09:29:13 -05004776 err = btrfs_init_inode_security(inode, dir);
Josef Bacik33268ea2008-07-24 12:16:36 -04004777 if (err) {
4778 drop_inode = 1;
4779 goto out_unlock;
4780 }
4781
Chris Mason39279cc2007-06-12 06:35:45 -04004782 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04004783 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04004784 if (err)
4785 drop_inode = 1;
4786 else {
4787 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04004788 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04004789 inode->i_fop = &btrfs_file_operations;
4790 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05004791 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04004792 }
4793 dir->i_sb->s_dirt = 1;
4794 btrfs_update_inode_block_group(trans, inode);
4795 btrfs_update_inode_block_group(trans, dir);
4796 if (drop_inode)
4797 goto out_unlock;
4798
4799 path = btrfs_alloc_path();
4800 BUG_ON(!path);
4801 key.objectid = inode->i_ino;
4802 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004803 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
4804 datasize = btrfs_file_extent_calc_inline_size(name_len);
4805 err = btrfs_insert_empty_item(trans, root, path, &key,
4806 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04004807 if (err) {
4808 drop_inode = 1;
4809 goto out_unlock;
4810 }
Chris Mason5f39d392007-10-15 16:14:19 -04004811 leaf = path->nodes[0];
4812 ei = btrfs_item_ptr(leaf, path->slots[0],
4813 struct btrfs_file_extent_item);
4814 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
4815 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04004816 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04004817 btrfs_set_file_extent_encryption(leaf, ei, 0);
4818 btrfs_set_file_extent_compression(leaf, ei, 0);
4819 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
4820 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
4821
Chris Mason39279cc2007-06-12 06:35:45 -04004822 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04004823 write_extent_buffer(leaf, symname, ptr, name_len);
4824 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004825 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04004826
Chris Mason39279cc2007-06-12 06:35:45 -04004827 inode->i_op = &btrfs_symlink_inode_operations;
4828 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04004829 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Yan Zhengd899e052008-10-30 14:25:28 -04004830 inode_set_bytes(inode, name_len);
Chris Masondbe674a2008-07-17 12:54:05 -04004831 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004832 err = btrfs_update_inode(trans, root, inode);
4833 if (err)
4834 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04004835
4836out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004837 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04004838 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05004839out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04004840 if (drop_inode) {
4841 inode_dec_link_count(inode);
4842 iput(inode);
4843 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04004844 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04004845 return err;
4846}
Chris Mason16432982008-04-10 10:23:21 -04004847
Yan Zhengd899e052008-10-30 14:25:28 -04004848static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
4849 u64 alloc_hint, int mode)
4850{
4851 struct btrfs_trans_handle *trans;
4852 struct btrfs_root *root = BTRFS_I(inode)->root;
4853 struct btrfs_key ins;
4854 u64 alloc_size;
4855 u64 cur_offset = start;
4856 u64 num_bytes = end - start;
4857 int ret = 0;
4858
4859 trans = btrfs_join_transaction(root, 1);
4860 BUG_ON(!trans);
4861 btrfs_set_trans_block_group(trans, inode);
4862
4863 while (num_bytes > 0) {
4864 alloc_size = min(num_bytes, root->fs_info->max_extent);
4865 ret = btrfs_reserve_extent(trans, root, alloc_size,
4866 root->sectorsize, 0, alloc_hint,
4867 (u64)-1, &ins, 1);
4868 if (ret) {
4869 WARN_ON(1);
4870 goto out;
4871 }
4872 ret = insert_reserved_file_extent(trans, inode,
4873 cur_offset, ins.objectid,
4874 ins.offset, ins.offset,
4875 ins.offset, 0, 0, 0,
4876 BTRFS_FILE_EXTENT_PREALLOC);
4877 BUG_ON(ret);
4878 num_bytes -= ins.offset;
4879 cur_offset += ins.offset;
4880 alloc_hint = ins.objectid + ins.offset;
4881 }
4882out:
4883 if (cur_offset > start) {
4884 inode->i_ctime = CURRENT_TIME;
4885 btrfs_set_flag(inode, PREALLOC);
4886 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
4887 cur_offset > i_size_read(inode))
4888 btrfs_i_size_write(inode, cur_offset);
4889 ret = btrfs_update_inode(trans, root, inode);
4890 BUG_ON(ret);
4891 }
4892
4893 btrfs_end_transaction(trans, root);
4894 return ret;
4895}
4896
4897static long btrfs_fallocate(struct inode *inode, int mode,
4898 loff_t offset, loff_t len)
4899{
4900 u64 cur_offset;
4901 u64 last_byte;
4902 u64 alloc_start;
4903 u64 alloc_end;
4904 u64 alloc_hint = 0;
4905 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
4906 struct extent_map *em;
4907 int ret;
4908
4909 alloc_start = offset & ~mask;
4910 alloc_end = (offset + len + mask) & ~mask;
4911
4912 mutex_lock(&inode->i_mutex);
4913 if (alloc_start > inode->i_size) {
4914 ret = btrfs_cont_expand(inode, alloc_start);
4915 if (ret)
4916 goto out;
4917 }
4918
4919 while (1) {
4920 struct btrfs_ordered_extent *ordered;
4921 lock_extent(&BTRFS_I(inode)->io_tree, alloc_start,
4922 alloc_end - 1, GFP_NOFS);
4923 ordered = btrfs_lookup_first_ordered_extent(inode,
4924 alloc_end - 1);
4925 if (ordered &&
4926 ordered->file_offset + ordered->len > alloc_start &&
4927 ordered->file_offset < alloc_end) {
4928 btrfs_put_ordered_extent(ordered);
4929 unlock_extent(&BTRFS_I(inode)->io_tree,
4930 alloc_start, alloc_end - 1, GFP_NOFS);
4931 btrfs_wait_ordered_range(inode, alloc_start,
4932 alloc_end - alloc_start);
4933 } else {
4934 if (ordered)
4935 btrfs_put_ordered_extent(ordered);
4936 break;
4937 }
4938 }
4939
4940 cur_offset = alloc_start;
4941 while (1) {
4942 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4943 alloc_end - cur_offset, 0);
4944 BUG_ON(IS_ERR(em) || !em);
4945 last_byte = min(extent_map_end(em), alloc_end);
4946 last_byte = (last_byte + mask) & ~mask;
4947 if (em->block_start == EXTENT_MAP_HOLE) {
4948 ret = prealloc_file_range(inode, cur_offset,
4949 last_byte, alloc_hint, mode);
4950 if (ret < 0) {
4951 free_extent_map(em);
4952 break;
4953 }
4954 }
4955 if (em->block_start <= EXTENT_MAP_LAST_BYTE)
4956 alloc_hint = em->block_start;
4957 free_extent_map(em);
4958
4959 cur_offset = last_byte;
4960 if (cur_offset >= alloc_end) {
4961 ret = 0;
4962 break;
4963 }
4964 }
4965 unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, alloc_end - 1,
4966 GFP_NOFS);
4967out:
4968 mutex_unlock(&inode->i_mutex);
4969 return ret;
4970}
4971
Chris Masone6dcd2d2008-07-17 12:53:50 -04004972static int btrfs_set_page_dirty(struct page *page)
4973{
Chris Masone6dcd2d2008-07-17 12:53:50 -04004974 return __set_page_dirty_nobuffers(page);
4975}
4976
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004977static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05004978{
4979 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
4980 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04004981 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05004982}
Chris Mason39279cc2007-06-12 06:35:45 -04004983
4984static struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -05004985 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -04004986 .lookup = btrfs_lookup,
4987 .create = btrfs_create,
4988 .unlink = btrfs_unlink,
4989 .link = btrfs_link,
4990 .mkdir = btrfs_mkdir,
4991 .rmdir = btrfs_rmdir,
4992 .rename = btrfs_rename,
4993 .symlink = btrfs_symlink,
4994 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04004995 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004996 .setxattr = btrfs_setxattr,
4997 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05004998 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04004999 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05005000 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04005001};
Chris Mason39279cc2007-06-12 06:35:45 -04005002static struct inode_operations btrfs_dir_ro_inode_operations = {
5003 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05005004 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04005005};
Chris Mason39279cc2007-06-12 06:35:45 -04005006static struct file_operations btrfs_dir_file_operations = {
5007 .llseek = generic_file_llseek,
5008 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01005009 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04005010 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04005011#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04005012 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04005013#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04005014 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04005015 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04005016};
5017
Chris Masond1310b22008-01-24 16:13:08 -05005018static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04005019 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05005020 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04005021 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04005022 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005023 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04005024 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04005025 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05005026 .set_bit_hook = btrfs_set_bit_hook,
5027 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04005028};
5029
Chris Mason35054392009-01-21 13:11:13 -05005030/*
5031 * btrfs doesn't support the bmap operation because swapfiles
5032 * use bmap to make a mapping of extents in the file. They assume
5033 * these extents won't change over the life of the file and they
5034 * use the bmap result to do IO directly to the drive.
5035 *
5036 * the btrfs bmap call would return logical addresses that aren't
5037 * suitable for IO and they also will change frequently as COW
5038 * operations happen. So, swapfile + btrfs == corruption.
5039 *
5040 * For now we're avoiding this by dropping bmap.
5041 */
Chris Mason39279cc2007-06-12 06:35:45 -04005042static struct address_space_operations btrfs_aops = {
5043 .readpage = btrfs_readpage,
5044 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04005045 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05005046 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04005047 .sync_page = block_sync_page,
Chris Mason16432982008-04-10 10:23:21 -04005048 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04005049 .invalidatepage = btrfs_invalidatepage,
5050 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04005051 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04005052};
5053
5054static struct address_space_operations btrfs_symlink_aops = {
5055 .readpage = btrfs_readpage,
5056 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04005057 .invalidatepage = btrfs_invalidatepage,
5058 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04005059};
5060
5061static struct inode_operations btrfs_file_inode_operations = {
5062 .truncate = btrfs_truncate,
5063 .getattr = btrfs_getattr,
5064 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005065 .setxattr = btrfs_setxattr,
5066 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05005067 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005068 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05005069 .permission = btrfs_permission,
Yan Zhengd899e052008-10-30 14:25:28 -04005070 .fallocate = btrfs_fallocate,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05005071 .fiemap = btrfs_fiemap,
Chris Mason39279cc2007-06-12 06:35:45 -04005072};
Josef Bacik618e21d2007-07-11 10:18:17 -04005073static struct inode_operations btrfs_special_inode_operations = {
5074 .getattr = btrfs_getattr,
5075 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05005076 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005077 .setxattr = btrfs_setxattr,
5078 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04005079 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04005080 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04005081};
Chris Mason39279cc2007-06-12 06:35:45 -04005082static struct inode_operations btrfs_symlink_inode_operations = {
5083 .readlink = generic_readlink,
5084 .follow_link = page_follow_link_light,
5085 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05005086 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -05005087 .setxattr = btrfs_setxattr,
5088 .getxattr = btrfs_getxattr,
5089 .listxattr = btrfs_listxattr,
5090 .removexattr = btrfs_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04005091};